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