xref: /PHP-5.6/sapi/phpdbg/phpdbg_opcode.c (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 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 
27 ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
28 
phpdbg_decode_literal(zend_op_array * ops,zend_literal * literal TSRMLS_DC)29 static inline zend_uint phpdbg_decode_literal(zend_op_array *ops, zend_literal *literal TSRMLS_DC) /* {{{ */
30 {
31 	int iter = 0;
32 
33 	while (iter < ops->last_literal) {
34 		if (literal == &ops->literals[iter]) {
35 			return iter;
36 		}
37 		iter++;
38 	}
39 
40 	return 0;
41 } /* }}} */
42 
phpdbg_decode_op(zend_op_array * ops,znode_op * op,zend_uint type,HashTable * vars TSRMLS_DC)43 static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, zend_uint type, HashTable *vars TSRMLS_DC) /* {{{ */
44 {
45 	char *decode = NULL;
46 
47 	switch (type &~ EXT_TYPE_UNUSED) {
48 		case IS_CV:
49 			asprintf(&decode, "$%s", ops->vars[op->var].name);
50 		break;
51 
52 		case IS_VAR:
53 		case IS_TMP_VAR: {
54 			zend_ulong id = 0, *pid = NULL;
55 			if (vars != NULL) {
56 				if (zend_hash_index_find(vars, (zend_ulong) ops->vars - op->var, (void**) &pid) != SUCCESS) {
57 					id = zend_hash_num_elements(vars);
58 					zend_hash_index_update(
59 						vars, (zend_ulong) ops->vars - op->var,
60 						(void**) &id,
61 						sizeof(zend_ulong), NULL);
62 				} else id = *pid;
63 			}
64 			asprintf(&decode, "@%lu", id);
65 		} break;
66 
67 		case IS_CONST:
68 			asprintf(&decode, "C%u", phpdbg_decode_literal(ops, op->literal TSRMLS_CC));
69 		break;
70 
71 		case IS_UNUSED:
72 			asprintf(&decode, "<unused>");
73 		break;
74 	}
75 	return decode;
76 } /* }}} */
77 
phpdbg_decode_opline(zend_op_array * ops,zend_op * op,HashTable * vars TSRMLS_DC)78 char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op, HashTable *vars TSRMLS_DC) /*{{{ */
79 {
80 	char *decode[4] = {NULL, NULL, NULL, NULL};
81 
82 	switch (op->opcode) {
83 	case ZEND_JMP:
84 #ifdef ZEND_GOTO
85 	case ZEND_GOTO:
86 #endif
87 #ifdef ZEND_FAST_CALL
88 	case ZEND_FAST_CALL:
89 #endif
90 			asprintf(&decode[1], "J%ld", op->op1.jmp_addr - ops->opcodes);
91 		goto format;
92 
93 	case ZEND_JMPZNZ:
94 			decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars TSRMLS_CC);
95 			asprintf(
96 				&decode[2], "J%u or J%lu", op->op2.opline_num, op->extended_value);
97 		goto result;
98 
99 	case ZEND_JMPZ:
100 	case ZEND_JMPNZ:
101 	case ZEND_JMPZ_EX:
102 	case ZEND_JMPNZ_EX:
103 
104 #ifdef ZEND_JMP_SET
105 	case ZEND_JMP_SET:
106 #endif
107 #ifdef ZEND_JMP_SET_VAR
108 	case ZEND_JMP_SET_VAR:
109 #endif
110 		decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars TSRMLS_CC);
111 		asprintf(
112 			&decode[2], "J%ld", op->op2.jmp_addr - ops->opcodes);
113 	goto result;
114 
115 	case ZEND_RECV_INIT:
116 		goto result;
117 
118 		default: {
119 			decode[1] = phpdbg_decode_op(ops, &op->op1, op->op1_type, vars TSRMLS_CC);
120 			decode[2] = phpdbg_decode_op(ops, &op->op2, op->op2_type, vars TSRMLS_CC);
121 result:
122 			decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type, vars TSRMLS_CC);
123 format:
124 			asprintf(
125 				&decode[0],
126 				"%-20s %-20s %-20s",
127 				decode[1] ? decode[1] : "",
128 				decode[2] ? decode[2] : "",
129 				decode[3] ? decode[3] : "");
130 		}
131 	}
132 
133 	if (decode[1])
134 		free(decode[1]);
135 	if (decode[2])
136 		free(decode[2]);
137 	if (decode[3])
138 		free(decode[3]);
139 
140 	return decode[0];
141 } /* }}} */
142 
phpdbg_print_opline_ex(zend_execute_data * execute_data,HashTable * vars,zend_bool ignore_flags TSRMLS_DC)143 void phpdbg_print_opline_ex(zend_execute_data *execute_data, HashTable *vars, zend_bool ignore_flags TSRMLS_DC) /* {{{ */
144 {
145 	/* force out a line while stepping so the user knows what is happening */
146 	if (ignore_flags ||
147 		(!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
148 		(PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ||
149 		(PHPDBG_G(oplog)))) {
150 
151 		zend_op *opline = execute_data->opline;
152 		char *decode = phpdbg_decode_opline(execute_data->op_array, opline, vars TSRMLS_CC);
153 
154 		if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) || (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
155 			/* output line info */
156 			phpdbg_notice("L%-5u %16p %-30s %s %s",
157 			   opline->lineno,
158 			   opline,
159 			   phpdbg_decode_opcode(opline->opcode),
160 			   decode,
161 			   execute_data->op_array->filename ? execute_data->op_array->filename : "unknown");
162 		}
163 
164 		if (!ignore_flags && PHPDBG_G(oplog)) {
165 			phpdbg_log_ex(PHPDBG_G(oplog), "L%-5u %16p %-30s %s %s",
166 				opline->lineno,
167 				opline,
168 				phpdbg_decode_opcode(opline->opcode),
169 				decode,
170 				execute_data->op_array->filename ? execute_data->op_array->filename : "unknown");
171 		}
172 
173 		if (decode) {
174 			free(decode);
175 		}
176 	}
177 } /* }}} */
178 
phpdbg_print_opline(zend_execute_data * execute_data,zend_bool ignore_flags TSRMLS_DC)179 void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags TSRMLS_DC) /* {{{ */
180 {
181 	phpdbg_print_opline_ex(execute_data, NULL, ignore_flags TSRMLS_CC);
182 } /* }}} */
183 
phpdbg_decode_opcode(zend_uchar opcode)184 const char *phpdbg_decode_opcode(zend_uchar opcode) /* {{{ */
185 {
186 #if ZEND_EXTENSION_API_NO <= PHP_5_5_API_NO
187 #define CASE(s) case s: return #s
188 	switch (opcode) {
189 		CASE(ZEND_NOP);
190 		CASE(ZEND_ADD);
191 		CASE(ZEND_SUB);
192 		CASE(ZEND_MUL);
193 		CASE(ZEND_DIV);
194 		CASE(ZEND_MOD);
195 		CASE(ZEND_SL);
196 		CASE(ZEND_SR);
197 		CASE(ZEND_CONCAT);
198 		CASE(ZEND_BW_OR);
199 		CASE(ZEND_BW_AND);
200 		CASE(ZEND_BW_XOR);
201 		CASE(ZEND_BW_NOT);
202 		CASE(ZEND_BOOL_NOT);
203 		CASE(ZEND_BOOL_XOR);
204 		CASE(ZEND_IS_IDENTICAL);
205 		CASE(ZEND_IS_NOT_IDENTICAL);
206 		CASE(ZEND_IS_EQUAL);
207 		CASE(ZEND_IS_NOT_EQUAL);
208 		CASE(ZEND_IS_SMALLER);
209 		CASE(ZEND_IS_SMALLER_OR_EQUAL);
210 		CASE(ZEND_CAST);
211 		CASE(ZEND_QM_ASSIGN);
212 		CASE(ZEND_ASSIGN_ADD);
213 		CASE(ZEND_ASSIGN_SUB);
214 		CASE(ZEND_ASSIGN_MUL);
215 		CASE(ZEND_ASSIGN_DIV);
216 		CASE(ZEND_ASSIGN_MOD);
217 		CASE(ZEND_ASSIGN_SL);
218 		CASE(ZEND_ASSIGN_SR);
219 		CASE(ZEND_ASSIGN_CONCAT);
220 		CASE(ZEND_ASSIGN_BW_OR);
221 		CASE(ZEND_ASSIGN_BW_AND);
222 		CASE(ZEND_ASSIGN_BW_XOR);
223 		CASE(ZEND_PRE_INC);
224 		CASE(ZEND_PRE_DEC);
225 		CASE(ZEND_POST_INC);
226 		CASE(ZEND_POST_DEC);
227 		CASE(ZEND_ASSIGN);
228 		CASE(ZEND_ASSIGN_REF);
229 		CASE(ZEND_ECHO);
230 		CASE(ZEND_PRINT);
231 		CASE(ZEND_JMP);
232 		CASE(ZEND_JMPZ);
233 		CASE(ZEND_JMPNZ);
234 		CASE(ZEND_JMPZNZ);
235 		CASE(ZEND_JMPZ_EX);
236 		CASE(ZEND_JMPNZ_EX);
237 		CASE(ZEND_CASE);
238 		CASE(ZEND_SWITCH_FREE);
239 		CASE(ZEND_BRK);
240 		CASE(ZEND_CONT);
241 		CASE(ZEND_BOOL);
242 		CASE(ZEND_INIT_STRING);
243 		CASE(ZEND_ADD_CHAR);
244 		CASE(ZEND_ADD_STRING);
245 		CASE(ZEND_ADD_VAR);
246 		CASE(ZEND_BEGIN_SILENCE);
247 		CASE(ZEND_END_SILENCE);
248 		CASE(ZEND_INIT_FCALL_BY_NAME);
249 		CASE(ZEND_DO_FCALL);
250 		CASE(ZEND_DO_FCALL_BY_NAME);
251 		CASE(ZEND_RETURN);
252 		CASE(ZEND_RECV);
253 		CASE(ZEND_RECV_INIT);
254 		CASE(ZEND_SEND_VAL);
255 		CASE(ZEND_SEND_VAR);
256 		CASE(ZEND_SEND_REF);
257 		CASE(ZEND_NEW);
258 		CASE(ZEND_INIT_NS_FCALL_BY_NAME);
259 		CASE(ZEND_FREE);
260 		CASE(ZEND_INIT_ARRAY);
261 		CASE(ZEND_ADD_ARRAY_ELEMENT);
262 		CASE(ZEND_INCLUDE_OR_EVAL);
263 		CASE(ZEND_UNSET_VAR);
264 		CASE(ZEND_UNSET_DIM);
265 		CASE(ZEND_UNSET_OBJ);
266 		CASE(ZEND_FE_RESET);
267 		CASE(ZEND_FE_FETCH);
268 		CASE(ZEND_EXIT);
269 		CASE(ZEND_FETCH_R);
270 		CASE(ZEND_FETCH_DIM_R);
271 		CASE(ZEND_FETCH_OBJ_R);
272 		CASE(ZEND_FETCH_W);
273 		CASE(ZEND_FETCH_DIM_W);
274 		CASE(ZEND_FETCH_OBJ_W);
275 		CASE(ZEND_FETCH_RW);
276 		CASE(ZEND_FETCH_DIM_RW);
277 		CASE(ZEND_FETCH_OBJ_RW);
278 		CASE(ZEND_FETCH_IS);
279 		CASE(ZEND_FETCH_DIM_IS);
280 		CASE(ZEND_FETCH_OBJ_IS);
281 		CASE(ZEND_FETCH_FUNC_ARG);
282 		CASE(ZEND_FETCH_DIM_FUNC_ARG);
283 		CASE(ZEND_FETCH_OBJ_FUNC_ARG);
284 		CASE(ZEND_FETCH_UNSET);
285 		CASE(ZEND_FETCH_DIM_UNSET);
286 		CASE(ZEND_FETCH_OBJ_UNSET);
287 		CASE(ZEND_FETCH_DIM_TMP_VAR);
288 		CASE(ZEND_FETCH_CONSTANT);
289 		CASE(ZEND_GOTO);
290 		CASE(ZEND_EXT_STMT);
291 		CASE(ZEND_EXT_FCALL_BEGIN);
292 		CASE(ZEND_EXT_FCALL_END);
293 		CASE(ZEND_EXT_NOP);
294 		CASE(ZEND_TICKS);
295 		CASE(ZEND_SEND_VAR_NO_REF);
296 		CASE(ZEND_CATCH);
297 		CASE(ZEND_THROW);
298 		CASE(ZEND_FETCH_CLASS);
299 		CASE(ZEND_CLONE);
300 		CASE(ZEND_RETURN_BY_REF);
301 		CASE(ZEND_INIT_METHOD_CALL);
302 		CASE(ZEND_INIT_STATIC_METHOD_CALL);
303 		CASE(ZEND_ISSET_ISEMPTY_VAR);
304 		CASE(ZEND_ISSET_ISEMPTY_DIM_OBJ);
305 		CASE(ZEND_PRE_INC_OBJ);
306 		CASE(ZEND_PRE_DEC_OBJ);
307 		CASE(ZEND_POST_INC_OBJ);
308 		CASE(ZEND_POST_DEC_OBJ);
309 		CASE(ZEND_ASSIGN_OBJ);
310 		CASE(ZEND_INSTANCEOF);
311 		CASE(ZEND_DECLARE_CLASS);
312 		CASE(ZEND_DECLARE_INHERITED_CLASS);
313 		CASE(ZEND_DECLARE_FUNCTION);
314 		CASE(ZEND_RAISE_ABSTRACT_ERROR);
315 		CASE(ZEND_DECLARE_CONST);
316 		CASE(ZEND_ADD_INTERFACE);
317 		CASE(ZEND_DECLARE_INHERITED_CLASS_DELAYED);
318 		CASE(ZEND_VERIFY_ABSTRACT_CLASS);
319 		CASE(ZEND_ASSIGN_DIM);
320 		CASE(ZEND_ISSET_ISEMPTY_PROP_OBJ);
321 		CASE(ZEND_HANDLE_EXCEPTION);
322 		CASE(ZEND_USER_OPCODE);
323 #ifdef ZEND_JMP_SET
324 		CASE(ZEND_JMP_SET);
325 #endif
326 		CASE(ZEND_DECLARE_LAMBDA_FUNCTION);
327 #ifdef ZEND_ADD_TRAIT
328 		CASE(ZEND_ADD_TRAIT);
329 #endif
330 #ifdef ZEND_BIND_TRAITS
331 		CASE(ZEND_BIND_TRAITS);
332 #endif
333 #ifdef ZEND_SEPARATE
334 		CASE(ZEND_SEPARATE);
335 #endif
336 #ifdef ZEND_QM_ASSIGN_VAR
337 		CASE(ZEND_QM_ASSIGN_VAR);
338 #endif
339 #ifdef ZEND_JMP_SET_VAR
340 		CASE(ZEND_JMP_SET_VAR);
341 #endif
342 #ifdef ZEND_DISCARD_EXCEPTION
343 		CASE(ZEND_DISCARD_EXCEPTION);
344 #endif
345 #ifdef ZEND_YIELD
346 		CASE(ZEND_YIELD);
347 #endif
348 #ifdef ZEND_GENERATOR_RETURN
349 		CASE(ZEND_GENERATOR_RETURN);
350 #endif
351 #ifdef ZEND_FAST_CALL
352 		CASE(ZEND_FAST_CALL);
353 #endif
354 #ifdef ZEND_FAST_RET
355 		CASE(ZEND_FAST_RET);
356 #endif
357 #ifdef ZEND_RECV_VARIADIC
358 		CASE(ZEND_RECV_VARIADIC);
359 #endif
360 		CASE(ZEND_OP_DATA);
361 		default:
362 			return "UNKNOWN";
363 	}
364 #else
365 	const char *ret = zend_get_opcode_name(opcode);
366 	return ret?ret:"UNKNOWN";
367 #endif
368 } /* }}} */
369