1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Felipe Pena <felipe@php.net> |
14 | Authors: Joe Watkins <joe.watkins@live.co.uk> |
15 | Authors: Bob Weinand <bwoebi@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "phpdbg.h"
20 #include "zend_vm_opcodes.h"
21 #include "zend_compile.h"
22 #include "phpdbg_opcode.h"
23 #include "phpdbg_utils.h"
24 #include "ext/standard/php_string.h"
25
ZEND_EXTERN_MODULE_GLOBALS(phpdbg)26 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
27
28 static inline const char *phpdbg_decode_opcode(zend_uchar opcode) /* {{{ */
29 {
30 const char *ret = zend_get_opcode_name(opcode);
31 if (ret) {
32 return ret + 5; /* Skip ZEND_ prefix */
33 }
34 return "UNKNOWN";
35 } /* }}} */
36
phpdbg_decode_op(zend_op_array * ops,const zend_op * opline,const znode_op * op,uint32_t type)37 static inline char *phpdbg_decode_op(
38 zend_op_array *ops, const zend_op *opline, const znode_op *op, uint32_t type) /* {{{ */
39 {
40 char *decode = NULL;
41
42 switch (type) {
43 case IS_CV: {
44 zend_string *var = ops->vars[EX_VAR_TO_NUM(op->var)];
45 spprintf(&decode, 0, "$%.*s%c",
46 ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18,
47 ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
48 } break;
49
50 case IS_VAR:
51 spprintf(&decode, 0, "@%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
52 break;
53 case IS_TMP_VAR:
54 spprintf(&decode, 0, "~%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
55 break;
56 case IS_CONST: {
57 zval *literal = RT_CONSTANT(opline, *op);
58 decode = phpdbg_short_zval_print(literal, 20);
59 } break;
60 }
61 return decode;
62 } /* }}} */
63
phpdbg_decode_input_op(zend_op_array * ops,const zend_op * opline,znode_op op,zend_uchar op_type,uint32_t flags)64 char *phpdbg_decode_input_op(
65 zend_op_array *ops, const zend_op *opline, znode_op op, zend_uchar op_type,
66 uint32_t flags) {
67 char *result = NULL;
68 if (op_type != IS_UNUSED) {
69 result = phpdbg_decode_op(ops, opline, &op, op_type);
70 } else if (ZEND_VM_OP_JMP_ADDR == (flags & ZEND_VM_OP_MASK)) {
71 spprintf(&result, 0, "J%td", OP_JMP_ADDR(opline, op) - ops->opcodes);
72 } else if (ZEND_VM_OP_NUM == (flags & ZEND_VM_OP_MASK)) {
73 spprintf(&result, 0, "%" PRIu32, op.num);
74 } else if (ZEND_VM_OP_TRY_CATCH == (flags & ZEND_VM_OP_MASK)) {
75 if (op.num != (uint32_t)-1) {
76 spprintf(&result, 0, "try-catch(%" PRIu32 ")", op.num);
77 }
78 } else if (ZEND_VM_OP_THIS == (flags & ZEND_VM_OP_MASK)) {
79 result = estrdup("THIS");
80 } else if (ZEND_VM_OP_NEXT == (flags & ZEND_VM_OP_MASK)) {
81 result = estrdup("NEXT");
82 } else if (ZEND_VM_OP_CLASS_FETCH == (flags & ZEND_VM_OP_MASK)) {
83 //zend_dump_class_fetch_type(op.num);
84 } else if (ZEND_VM_OP_CONSTRUCTOR == (flags & ZEND_VM_OP_MASK)) {
85 result = estrdup("CONSTRUCTOR");
86 }
87 return result;
88 }
89
phpdbg_decode_opline(zend_op_array * ops,zend_op * opline)90 char *phpdbg_decode_opline(zend_op_array *ops, zend_op *opline) /*{{{ */
91 {
92 const char *opcode_name = phpdbg_decode_opcode(opline->opcode);
93 uint32_t flags = zend_get_opcode_flags(opline->opcode);
94 char *result, *decode[4] = {NULL, NULL, NULL, NULL};
95
96 /* OpcodeName */
97 if (opline->extended_value) {
98 spprintf(&decode[0], 0, "%s<%" PRIi32 ">", opcode_name, opline->extended_value);
99 }
100
101 /* OP1 */
102 decode[1] = phpdbg_decode_input_op(
103 ops, opline, opline->op1, opline->op1_type, ZEND_VM_OP1_FLAGS(flags));
104
105 /* OP2 */
106 decode[2] = phpdbg_decode_input_op(
107 ops, opline, opline->op2, opline->op2_type, ZEND_VM_OP2_FLAGS(flags));
108
109 /* RESULT */
110 switch (opline->opcode) {
111 case ZEND_CATCH:
112 if (opline->extended_value & ZEND_LAST_CATCH) {
113 if (decode[2]) {
114 efree(decode[2]);
115 decode[2] = NULL;
116 }
117 }
118 decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
119 break;
120 default:
121 decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
122 break;
123 }
124
125 spprintf(&result, 0,
126 "%-23s %-20s %-20s %-20s",
127 decode[0] ? decode[0] : opcode_name,
128 decode[1] ? decode[1] : "",
129 decode[2] ? decode[2] : "",
130 decode[3] ? decode[3] : "");
131
132 if (decode[0])
133 efree(decode[0]);
134 if (decode[1])
135 efree(decode[1]);
136 if (decode[2])
137 efree(decode[2]);
138 if (decode[3])
139 efree(decode[3]);
140
141 return result;
142 } /* }}} */
143
phpdbg_print_opline_ex(zend_execute_data * execute_data,zend_bool ignore_flags)144 void phpdbg_print_opline_ex(zend_execute_data *execute_data, zend_bool ignore_flags) /* {{{ */
145 {
146 /* force out a line while stepping so the user knows what is happening */
147 if (ignore_flags ||
148 (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
149 (PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ||
150 (PHPDBG_G(oplog)))) {
151
152 zend_op *opline = (zend_op *) execute_data->opline;
153 char *decode = phpdbg_decode_opline(&execute_data->func->op_array, opline);
154
155 if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) || (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
156 /* output line info */
157 phpdbg_notice("opline", "line=\"%u\" opline=\"%p\" op=\"%s\" file=\"%s\"", "L%-5u %16p %s %s",
158 opline->lineno,
159 opline,
160 decode,
161 execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
162 }
163
164 if (!ignore_flags && PHPDBG_G(oplog)) {
165 phpdbg_log_ex(fileno(PHPDBG_G(oplog)), "L%-5u %16p %s %s\n",
166 opline->lineno,
167 opline,
168 decode,
169 execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
170 }
171
172 efree(decode);
173 }
174
175 if (PHPDBG_G(oplog_list)) {
176 phpdbg_oplog_entry *cur = zend_arena_alloc(&PHPDBG_G(oplog_arena), sizeof(phpdbg_oplog_entry));
177 zend_op_array *op_array = &execute_data->func->op_array;
178 cur->op = (zend_op *) execute_data->opline;
179 cur->opcodes = op_array->opcodes;
180 cur->filename = op_array->filename;
181 cur->scope = op_array->scope;
182 cur->function_name = op_array->function_name;
183 cur->next = NULL;
184 PHPDBG_G(oplog_cur)->next = cur;
185 PHPDBG_G(oplog_cur) = cur;
186 }
187 } /* }}} */
188
phpdbg_print_opline(zend_execute_data * execute_data,zend_bool ignore_flags)189 void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags) /* {{{ */
190 {
191 phpdbg_print_opline_ex(execute_data, ignore_flags);
192 } /* }}} */
193