xref: /PHP-7.4/sapi/phpdbg/phpdbg_frame.c (revision c09b6359)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 "zend.h"
22 #include "phpdbg.h"
23 #include "phpdbg_utils.h"
24 #include "phpdbg_frame.h"
25 #include "phpdbg_list.h"
26 #include "zend_smart_str.h"
27 
ZEND_EXTERN_MODULE_GLOBALS(phpdbg)28 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
29 
30 static inline void phpdbg_append_individual_arg(smart_str *s, uint32_t i, zend_function *func, zval *arg) {
31 	const zend_arg_info *arginfo = func->common.arg_info;
32 	char *arg_name = NULL;
33 
34 	if (i) {
35 		smart_str_appends(s, ", ");
36 	}
37 	if (i < func->common.num_args) {
38 		if (arginfo) {
39 			if (func->type == ZEND_INTERNAL_FUNCTION) {
40 				arg_name = (char *) ((zend_internal_arg_info *) &arginfo[i])->name;
41 			} else {
42 				arg_name = ZSTR_VAL(arginfo[i].name);
43 			}
44 		}
45 		smart_str_appends(s, arg_name ? arg_name : "?");
46 		smart_str_appendc(s, '=');
47 	}
48 	{
49 		char *arg_print = phpdbg_short_zval_print(arg, 40);
50 		smart_str_appends(s, arg_print);
51 		efree(arg_print);
52 	}
53 }
54 
phpdbg_compile_stackframe(zend_execute_data * ex)55 zend_string *phpdbg_compile_stackframe(zend_execute_data *ex) {
56 	smart_str s = {0};
57 	zend_op_array *op_array = &ex->func->op_array;
58 	uint32_t i = 0, first_extra_arg = op_array->num_args, num_args = ZEND_CALL_NUM_ARGS(ex);
59 	zval *p = ZEND_CALL_ARG(ex, 1);
60 
61 	if (op_array->scope) {
62 		smart_str_append(&s, op_array->scope->name);
63 		smart_str_appends(&s, "::");
64 	}
65 	smart_str_append(&s, op_array->function_name);
66 	smart_str_appendc(&s, '(');
67 	if (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg) {
68 		while (i < first_extra_arg) {
69 			phpdbg_append_individual_arg(&s, i, ex->func, p);
70 			p++;
71 			i++;
72 		}
73 		p = ZEND_CALL_VAR_NUM(ex, op_array->last_var + op_array->T);
74 	}
75 	while (i < num_args) {
76 		phpdbg_append_individual_arg(&s, i, ex->func, p);
77 		p++;
78 		i++;
79 	}
80 	smart_str_appendc(&s, ')');
81 
82 	if (ex->func->type == ZEND_USER_FUNCTION) {
83 		smart_str_appends(&s, " at ");
84 		smart_str_append(&s, op_array->filename);
85 		smart_str_appendc(&s, ':');
86 		smart_str_append_unsigned(&s, ex->opline->lineno);
87 	} else {
88 		smart_str_appends(&s, " [internal function]");
89 	}
90 
91 	return s.s;
92 }
93 
phpdbg_print_cur_frame_info()94 void phpdbg_print_cur_frame_info() {
95 	const char *file_chr = zend_get_executed_filename();
96 	zend_string *file = zend_string_init(file_chr, strlen(file_chr), 0);
97 
98 	phpdbg_list_file(file, 3, zend_get_executed_lineno() - 1, zend_get_executed_lineno());
99 	efree(file);
100 }
101 
phpdbg_restore_frame(void)102 void phpdbg_restore_frame(void) /* {{{ */
103 {
104 	if (PHPDBG_FRAME(num) == 0) {
105 		return;
106 	}
107 
108 	if (PHPDBG_FRAME(generator)) {
109 		if (PHPDBG_FRAME(generator)->execute_data->call) {
110 			PHPDBG_FRAME(generator)->frozen_call_stack = zend_generator_freeze_call_stack(PHPDBG_FRAME(generator)->execute_data);
111 		}
112 		PHPDBG_FRAME(generator) = NULL;
113 	}
114 
115 	PHPDBG_FRAME(num) = 0;
116 
117 	/* move things back */
118 	EG(current_execute_data) = PHPDBG_FRAME(execute_data);
119 } /* }}} */
120 
phpdbg_switch_frame(int frame)121 void phpdbg_switch_frame(int frame) /* {{{ */
122 {
123 	zend_execute_data *execute_data = PHPDBG_FRAME(num) ? PHPDBG_FRAME(execute_data) : EG(current_execute_data);
124 	int i = 0;
125 
126 	if (PHPDBG_FRAME(num) == frame) {
127 		phpdbg_notice("frame", "id=\"%d\"", "Already in frame #%d", frame);
128 		return;
129 	}
130 
131 	phpdbg_try_access {
132 		while (execute_data) {
133 			if (i++ == frame) {
134 				break;
135 			}
136 
137 			do {
138 				execute_data = execute_data->prev_execute_data;
139 			} while (execute_data && execute_data->opline == NULL);
140 		}
141 	} phpdbg_catch_access {
142 		phpdbg_error("signalsegv", "", "Couldn't switch frames, invalid data source");
143 		return;
144 	} phpdbg_end_try_access();
145 
146 	if (execute_data == NULL) {
147 		phpdbg_error("frame", "type=\"maxnum\" id=\"%d\"", "No frame #%d", frame);
148 		return;
149 	}
150 
151 	phpdbg_restore_frame();
152 
153 	if (frame > 0) {
154 		PHPDBG_FRAME(num) = frame;
155 
156 		/* backup things and jump back */
157 		PHPDBG_FRAME(execute_data) = EG(current_execute_data);
158 		EG(current_execute_data) = execute_data;
159 	}
160 
161 	phpdbg_try_access {
162 		zend_string *s = phpdbg_compile_stackframe(EG(current_execute_data));
163 		phpdbg_notice("frame", "id=\"%d\" frameinfo=\"%.*s\"", "Switched to frame #%d: %.*s", frame, (int) ZSTR_LEN(s), ZSTR_VAL(s));
164 		zend_string_release(s);
165 	} phpdbg_catch_access {
166 		phpdbg_notice("frame", "id=\"%d\"", "Switched to frame #%d", frame);
167 	} phpdbg_end_try_access();
168 
169 	phpdbg_print_cur_frame_info();
170 } /* }}} */
171 
phpdbg_dump_prototype(zval * tmp)172 static void phpdbg_dump_prototype(zval *tmp) /* {{{ */
173 {
174 	zval *funcname, *class, class_zv, *args, *argstmp;
175 
176 	funcname = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("function"));
177 
178 	if ((class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("object")))) {
179 		ZVAL_NEW_STR(&class_zv, Z_OBJCE_P(class)->name);
180 		class = &class_zv;
181 	} else {
182 		class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("class"));
183 	}
184 
185 	if (class) {
186 		zval *type = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("type"));
187 
188 		phpdbg_xml(" symbol=\"%s%s%s\"", Z_STRVAL_P(class), Z_STRVAL_P(type), Z_STRVAL_P(funcname));
189 		phpdbg_out("%s%s%s(", Z_STRVAL_P(class), Z_STRVAL_P(type), Z_STRVAL_P(funcname));
190 	} else {
191 		phpdbg_xml(" symbol=\"%s\"", Z_STRVAL_P(funcname));
192 		phpdbg_out("%s(", Z_STRVAL_P(funcname));
193 	}
194 
195 	args = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("args"));
196 	if (args) {
197 		phpdbg_xml(">");
198 	} else {
199 		phpdbg_xml(" />");
200 	}
201 
202 	if (args) {
203 		const zend_function *func = NULL;
204 		const zend_arg_info *arginfo = NULL;
205 		zend_bool is_variadic = 0;
206 		int j = 0, m;
207 
208 		phpdbg_try_access {
209 			/* assuming no autoloader call is necessary, class should have been loaded if it's in backtrace ... */
210 			if ((func = phpdbg_get_function(Z_STRVAL_P(funcname), class ? Z_STRVAL_P(class) : NULL))) {
211 				arginfo = func->common.arg_info;
212 			}
213 		} phpdbg_end_try_access();
214 
215 		m = func ? func->common.num_args : 0;
216 
217 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), argstmp) {
218 			if (j) {
219 				phpdbg_out(", ");
220 			}
221 			phpdbg_xml("<arg %r");
222 			if (m && j < m) {
223 				char *arg_name = NULL;
224 
225 				if (arginfo) {
226 					if (func->type == ZEND_INTERNAL_FUNCTION) {
227 						arg_name = (char *)((zend_internal_arg_info *)&arginfo[j])->name;
228 					} else {
229 						arg_name = ZSTR_VAL(arginfo[j].name);
230 					}
231 				}
232 
233 				if (!is_variadic) {
234 					is_variadic = arginfo ? arginfo[j].is_variadic : 0;
235 				}
236 
237 				phpdbg_xml(" variadic=\"%s\" name=\"%s\">", is_variadic ? "variadic" : "", arg_name ? arg_name : "");
238 				phpdbg_out("%s=%s", arg_name ? arg_name : "?", is_variadic ? "[": "");
239 
240 			} else {
241 				phpdbg_xml(">");
242 			}
243 			++j;
244 
245 			{
246 				char *arg_print = phpdbg_short_zval_print(argstmp, 40);
247 				php_printf("%s", arg_print);
248 				efree(arg_print);
249 			}
250 
251 			phpdbg_xml("</arg>");
252 		} ZEND_HASH_FOREACH_END();
253 
254 		if (is_variadic) {
255 			phpdbg_out("]");
256 		}
257 		phpdbg_xml("</frame>");
258 	}
259 	phpdbg_out(")");
260 }
261 
phpdbg_dump_backtrace(size_t num)262 void phpdbg_dump_backtrace(size_t num) /* {{{ */
263 {
264 	HashPosition position;
265 	zval zbacktrace;
266 	zval *tmp;
267 	zval startline, startfile;
268 	const char *startfilename;
269 	zval *file = &startfile, *line = &startline;
270 	int i = 0, limit = num;
271 
272 	PHPDBG_OUTPUT_BACKUP();
273 
274 	if (limit < 0) {
275 		phpdbg_error("backtrace", "type=\"minnum\"", "Invalid backtrace size %d", limit);
276 
277 		PHPDBG_OUTPUT_BACKUP_RESTORE();
278 		return;
279 	}
280 
281 	phpdbg_try_access {
282 		zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit);
283 	} phpdbg_catch_access {
284 		phpdbg_error("signalsegv", "", "Couldn't fetch backtrace, invalid data source");
285 		return;
286 	} phpdbg_end_try_access();
287 
288 	phpdbg_xml("<backtrace %r>");
289 
290 	Z_LVAL(startline) = zend_get_executed_lineno();
291 	startfilename = zend_get_executed_filename();
292 	Z_STR(startfile) = zend_string_init(startfilename, strlen(startfilename), 0);
293 
294 	zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position);
295 	tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position);
296 	while ((tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position))) {
297 		if (file) { /* userland */
298 			phpdbg_out("frame #%d: ", i);
299 			phpdbg_xml("<frame %r id=\"%d\" file=\"%s\" line=\"" ZEND_LONG_FMT "\"", i, Z_STRVAL_P(file), Z_LVAL_P(line));
300 			phpdbg_dump_prototype(tmp);
301 			phpdbg_out(" at %s:%ld\n", Z_STRVAL_P(file), Z_LVAL_P(line));
302 			i++;
303 		} else {
304 			phpdbg_out(" => ");
305 			phpdbg_xml("<frame %r id=\"%d\" internal=\"internal\"", i);
306 			phpdbg_dump_prototype(tmp);
307 			phpdbg_out(" (internal function)\n");
308 		}
309 
310 		file = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("file"));
311 		line = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("line"));
312 		zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position);
313 	}
314 
315 	phpdbg_writeln("frame", "id=\"%d\" symbol=\"{main}\" file=\"%s\" line=\"%d\"", "frame #%d: {main} at %s:%ld", i, Z_STRVAL_P(file), Z_LVAL_P(line));
316 	phpdbg_xml("</backtrace>");
317 
318 	zval_ptr_dtor_nogc(&zbacktrace);
319 	zend_string_release(Z_STR(startfile));
320 
321 	PHPDBG_OUTPUT_BACKUP_RESTORE();
322 } /* }}} */
323 
phpdbg_open_generator_frame(zend_generator * gen)324 void phpdbg_open_generator_frame(zend_generator *gen) {
325 	zend_string *s;
326 
327 	if (EG(current_execute_data) == gen->execute_data) {
328 		return;
329 	}
330 
331 	phpdbg_restore_frame();
332 
333 	PHPDBG_FRAME(num) = -1;
334 	PHPDBG_FRAME(generator) = gen;
335 
336 	EG(current_execute_data) = gen->execute_data;
337 	if (gen->frozen_call_stack) {
338 		zend_generator_restore_call_stack(gen);
339 	}
340 	gen->execute_data->prev_execute_data = NULL;
341 
342 	s = phpdbg_compile_stackframe(EG(current_execute_data));
343 	phpdbg_notice("frame", "handle=\"%d\" frameinfo=\"%.*s\"", "Switched to generator with handle #%d: %.*s", gen->std.handle, (int) ZSTR_LEN(s), ZSTR_VAL(s));
344 	zend_string_release(s);
345 	phpdbg_print_cur_frame_info();
346 }
347