xref: /PHP-7.3/sapi/phpdbg/phpdbg_opcode.c (revision b0af9ac7)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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,const zend_op * opline,const znode_op * op,uint32_t type)39 static inline char *phpdbg_decode_op(
40 		zend_op_array *ops, const zend_op *opline, const znode_op *op, uint32_t type) /* {{{ */
41 {
42 	char *decode = NULL;
43 
44 	switch (type) {
45 		case IS_CV: {
46 			zend_string *var = ops->vars[EX_VAR_TO_NUM(op->var)];
47 			spprintf(&decode, 0, "$%.*s%c",
48 				ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18,
49 				ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
50 		} break;
51 
52 		case IS_VAR:
53 			spprintf(&decode, 0, "@%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
54 		break;
55 		case IS_TMP_VAR:
56 			spprintf(&decode, 0, "~%u", EX_VAR_TO_NUM(op->var) - ops->last_var);
57 		break;
58 		case IS_CONST: {
59 			zval *literal = RT_CONSTANT(opline, *op);
60 			decode = phpdbg_short_zval_print(literal, 20);
61 		} break;
62 	}
63 	return decode;
64 } /* }}} */
65 
phpdbg_decode_input_op(zend_op_array * ops,const zend_op * opline,znode_op op,zend_uchar op_type,uint32_t flags)66 char *phpdbg_decode_input_op(
67 		zend_op_array *ops, const zend_op *opline, znode_op op, zend_uchar op_type,
68 		uint32_t flags) {
69 	char *result = NULL;
70 	if (op_type != IS_UNUSED) {
71 		result = phpdbg_decode_op(ops, opline, &op, op_type);
72 	} else if (ZEND_VM_OP_JMP_ADDR == (flags & ZEND_VM_OP_MASK)) {
73 		spprintf(&result, 0, "J%td", OP_JMP_ADDR(opline, op) - ops->opcodes);
74 	} else if (ZEND_VM_OP_NUM == (flags & ZEND_VM_OP_MASK)) {
75 		spprintf(&result, 0, "%" PRIu32, op.num);
76 	} else if (ZEND_VM_OP_TRY_CATCH == (flags & ZEND_VM_OP_MASK)) {
77 		if (op.num != (uint32_t)-1) {
78 			spprintf(&result, 0, "try-catch(%" PRIu32 ")", op.num);
79 		}
80 	} else if (ZEND_VM_OP_THIS == (flags & ZEND_VM_OP_MASK)) {
81 		result = estrdup("THIS");
82 	} else if (ZEND_VM_OP_NEXT == (flags & ZEND_VM_OP_MASK)) {
83 		result = estrdup("NEXT");
84 	} else if (ZEND_VM_OP_CLASS_FETCH == (flags & ZEND_VM_OP_MASK)) {
85 		//zend_dump_class_fetch_type(op.num);
86 	} else if (ZEND_VM_OP_CONSTRUCTOR == (flags & ZEND_VM_OP_MASK)) {
87 		result = estrdup("CONSTRUCTOR");
88 	}
89 	return result;
90 }
91 
phpdbg_decode_opline(zend_op_array * ops,zend_op * opline)92 char *phpdbg_decode_opline(zend_op_array *ops, zend_op *opline) /*{{{ */
93 {
94 	const char *opcode_name = phpdbg_decode_opcode(opline->opcode);
95 	uint32_t flags = zend_get_opcode_flags(opline->opcode);
96 	char *result, *decode[4] = {NULL, NULL, NULL, NULL};
97 
98 	/* OpcodeName */
99 	if (opline->extended_value) {
100 		spprintf(&decode[0], 0, "%s<%" PRIi32 ">", opcode_name, opline->extended_value);
101 	}
102 
103 	/* OP1 */
104 	decode[1] = phpdbg_decode_input_op(
105 		ops, opline, opline->op1, opline->op1_type, ZEND_VM_OP1_FLAGS(flags));
106 
107 	/* OP2 */
108 	decode[2] = phpdbg_decode_input_op(
109 		ops, opline, opline->op2, opline->op2_type, ZEND_VM_OP2_FLAGS(flags));
110 
111 	/* RESULT */
112 	switch (opline->opcode) {
113 	case ZEND_CATCH:
114 		if (opline->extended_value & ZEND_LAST_CATCH) {
115 			if (decode[2]) {
116 				efree(decode[2]);
117 				decode[2] = NULL;
118 			}
119 		}
120 		decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
121 		break;
122 	default:
123 		decode[3] = phpdbg_decode_op(ops, opline, &opline->result, opline->result_type);
124 		break;
125 	}
126 
127 	spprintf(&result, 0,
128 		"%-23s %-20s %-20s %-20s",
129 		decode[0] ? decode[0] : opcode_name,
130 		decode[1] ? decode[1] : "",
131 		decode[2] ? decode[2] : "",
132 		decode[3] ? decode[3] : "");
133 
134 	if (decode[0])
135 		efree(decode[0]);
136 	if (decode[1])
137 		efree(decode[1]);
138 	if (decode[2])
139 		efree(decode[2]);
140 	if (decode[3])
141 		efree(decode[3]);
142 
143 	return result;
144 } /* }}} */
145 
phpdbg_print_opline_ex(zend_execute_data * execute_data,zend_bool ignore_flags)146 void phpdbg_print_opline_ex(zend_execute_data *execute_data, zend_bool ignore_flags) /* {{{ */
147 {
148 	/* force out a line while stepping so the user knows what is happening */
149 	if (ignore_flags ||
150 		(!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) ||
151 		(PHPDBG_G(flags) & PHPDBG_IS_STEPPING) ||
152 		(PHPDBG_G(oplog)))) {
153 
154 		zend_op *opline = (zend_op *) execute_data->opline;
155 		char *decode = phpdbg_decode_opline(&execute_data->func->op_array, opline);
156 
157 		if (ignore_flags || (!(PHPDBG_G(flags) & PHPDBG_IS_QUIET) || (PHPDBG_G(flags) & PHPDBG_IS_STEPPING))) {
158 			/* output line info */
159 			phpdbg_notice("opline", "line=\"%u\" opline=\"%p\" op=\"%s\" file=\"%s\"", "L%-5u %16p %s %s",
160 			   opline->lineno,
161 			   opline,
162 			   decode,
163 			   execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
164 		}
165 
166 		if (!ignore_flags && PHPDBG_G(oplog)) {
167 			phpdbg_log_ex(fileno(PHPDBG_G(oplog)), "L%-5u %16p %s %s\n",
168 				opline->lineno,
169 				opline,
170 				decode,
171 				execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
172 		}
173 
174 		efree(decode);
175 	}
176 
177 	if (PHPDBG_G(oplog_list)) {
178 		phpdbg_oplog_entry *cur = zend_arena_alloc(&PHPDBG_G(oplog_arena), sizeof(phpdbg_oplog_entry));
179 		zend_op_array *op_array = &execute_data->func->op_array;
180 		cur->op = (zend_op *) execute_data->opline;
181 		cur->opcodes = op_array->opcodes;
182 		cur->filename = op_array->filename;
183 		cur->scope = op_array->scope;
184 		cur->function_name = op_array->function_name;
185 		cur->next = NULL;
186 		PHPDBG_G(oplog_cur)->next = cur;
187 		PHPDBG_G(oplog_cur) = cur;
188 	}
189 } /* }}} */
190 
phpdbg_print_opline(zend_execute_data * execute_data,zend_bool ignore_flags)191 void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags) /* {{{ */
192 {
193 	phpdbg_print_opline_ex(execute_data, ignore_flags);
194 } /* }}} */
195