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