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 | http://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()92 void phpdbg_print_cur_frame_info() {
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("frame", "id=\"%d\"", "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("signalsegv", "", "Couldn't switch frames, invalid data source");
141 return;
142 } phpdbg_end_try_access();
143
144 if (execute_data == NULL) {
145 phpdbg_error("frame", "type=\"maxnum\" id=\"%d\"", "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("frame", "id=\"%d\" frameinfo=\"%.*s\"", "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("frame", "id=\"%d\"", "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_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("function"));
175
176 if ((class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("object")))) {
177 ZVAL_NEW_STR(&class_zv, Z_OBJCE_P(class)->name);
178 class = &class_zv;
179 } else {
180 class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("class"));
181 }
182
183 if (class) {
184 zval *type = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("type"));
185
186 phpdbg_xml(" symbol=\"%s%s%s\"", Z_STRVAL_P(class), Z_STRVAL_P(type), Z_STRVAL_P(funcname));
187 phpdbg_out("%s%s%s(", Z_STRVAL_P(class), Z_STRVAL_P(type), Z_STRVAL_P(funcname));
188 } else {
189 phpdbg_xml(" symbol=\"%s\"", Z_STRVAL_P(funcname));
190 phpdbg_out("%s(", Z_STRVAL_P(funcname));
191 }
192
193 args = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("args"));
194 if (args) {
195 phpdbg_xml(">");
196 } else {
197 phpdbg_xml(" />");
198 }
199
200 if (args) {
201 const zend_function *func = NULL;
202 const zend_arg_info *arginfo = NULL;
203 zend_bool is_variadic = 0;
204 int j = 0, m;
205
206 phpdbg_try_access {
207 /* assuming no autoloader call is necessary, class should have been loaded if it's in backtrace ... */
208 if ((func = phpdbg_get_function(Z_STRVAL_P(funcname), class ? Z_STRVAL_P(class) : NULL))) {
209 arginfo = func->common.arg_info;
210 }
211 } phpdbg_end_try_access();
212
213 m = func ? func->common.num_args : 0;
214
215 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), argstmp) {
216 if (j) {
217 phpdbg_out(", ");
218 }
219 phpdbg_xml("<arg %r");
220 if (m && j < m) {
221 char *arg_name = NULL;
222
223 if (arginfo) {
224 if (func->type == ZEND_INTERNAL_FUNCTION) {
225 arg_name = (char *)((zend_internal_arg_info *)&arginfo[j])->name;
226 } else {
227 arg_name = ZSTR_VAL(arginfo[j].name);
228 }
229 }
230
231 if (!is_variadic) {
232 is_variadic = arginfo ? ZEND_ARG_IS_VARIADIC(&arginfo[j]) : 0;
233 }
234
235 phpdbg_xml(" variadic=\"%s\" name=\"%s\">", is_variadic ? "variadic" : "", arg_name ? arg_name : "");
236 phpdbg_out("%s=%s", arg_name ? arg_name : "?", is_variadic ? "[": "");
237
238 } else {
239 phpdbg_xml(">");
240 }
241 ++j;
242
243 {
244 char *arg_print = phpdbg_short_zval_print(argstmp, 40);
245 php_printf("%s", arg_print);
246 efree(arg_print);
247 }
248
249 phpdbg_xml("</arg>");
250 } ZEND_HASH_FOREACH_END();
251
252 if (is_variadic) {
253 phpdbg_out("]");
254 }
255 phpdbg_xml("</frame>");
256 }
257 phpdbg_out(")");
258 }
259
phpdbg_dump_backtrace(size_t num)260 void phpdbg_dump_backtrace(size_t num) /* {{{ */
261 {
262 HashPosition position;
263 zval zbacktrace;
264 zval *tmp;
265 zval startline, startfile;
266 const char *startfilename;
267 zval *file = &startfile, *line = &startline;
268 int i = 0, limit = num;
269
270 PHPDBG_OUTPUT_BACKUP();
271
272 if (limit < 0) {
273 phpdbg_error("backtrace", "type=\"minnum\"", "Invalid backtrace size %d", limit);
274
275 PHPDBG_OUTPUT_BACKUP_RESTORE();
276 return;
277 }
278
279 phpdbg_try_access {
280 zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit);
281 } phpdbg_catch_access {
282 phpdbg_error("signalsegv", "", "Couldn't fetch backtrace, invalid data source");
283 return;
284 } phpdbg_end_try_access();
285
286 phpdbg_xml("<backtrace %r>");
287
288 Z_LVAL(startline) = zend_get_executed_lineno();
289 startfilename = zend_get_executed_filename();
290 Z_STR(startfile) = zend_string_init(startfilename, strlen(startfilename), 0);
291
292 zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position);
293 tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position);
294 while ((tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position))) {
295 if (file) { /* userland */
296 phpdbg_out("frame #%d: ", i);
297 phpdbg_xml("<frame %r id=\"%d\" file=\"%s\" line=\"" ZEND_LONG_FMT "\"", i, Z_STRVAL_P(file), Z_LVAL_P(line));
298 phpdbg_dump_prototype(tmp);
299 phpdbg_out(" at %s:%ld\n", Z_STRVAL_P(file), Z_LVAL_P(line));
300 i++;
301 } else {
302 phpdbg_out(" => ");
303 phpdbg_xml("<frame %r id=\"%d\" internal=\"internal\"", i);
304 phpdbg_dump_prototype(tmp);
305 phpdbg_out(" (internal function)\n");
306 }
307
308 file = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("file"));
309 line = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("line"));
310 zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position);
311 }
312
313 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));
314 phpdbg_xml("</backtrace>");
315
316 zval_ptr_dtor_nogc(&zbacktrace);
317 zend_string_release(Z_STR(startfile));
318
319 PHPDBG_OUTPUT_BACKUP_RESTORE();
320 } /* }}} */
321
phpdbg_open_generator_frame(zend_generator * gen)322 void phpdbg_open_generator_frame(zend_generator *gen) {
323 zend_string *s;
324
325 if (EG(current_execute_data) == gen->execute_data) {
326 return;
327 }
328
329 phpdbg_restore_frame();
330
331 PHPDBG_FRAME(num) = -1;
332 PHPDBG_FRAME(generator) = gen;
333
334 EG(current_execute_data) = gen->execute_data;
335 if (gen->frozen_call_stack) {
336 zend_generator_restore_call_stack(gen);
337 }
338 gen->execute_data->prev_execute_data = NULL;
339
340 s = phpdbg_compile_stackframe(EG(current_execute_data));
341 phpdbg_notice("frame", "handle=\"%d\" frameinfo=\"%.*s\"", "Switched to generator with handle #%d: %.*s", gen->std.handle, (int) ZSTR_LEN(s), ZSTR_VAL(s));
342 zend_string_release(s);
343 phpdbg_print_cur_frame_info();
344 }
345