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 "php.h"
20 #include "phpdbg.h"
21 #include "phpdbg_utils.h"
22 #include "phpdbg_info.h"
23 #include "phpdbg_bp.h"
24 #include "phpdbg_prompt.h"
25
26 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
27
28 #define PHPDBG_INFO_COMMAND_D(f, h, a, m, l, s, flags) \
29 PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[13], flags)
30
31 const phpdbg_command_t phpdbg_info_commands[] = {
32 PHPDBG_INFO_COMMAND_D(break, "show breakpoints", 'b', info_break, NULL, 0, PHPDBG_ASYNC_SAFE),
33 PHPDBG_INFO_COMMAND_D(files, "show included files", 'F', info_files, NULL, 0, PHPDBG_ASYNC_SAFE),
34 PHPDBG_INFO_COMMAND_D(classes, "show loaded classes", 'c', info_classes, NULL, 0, PHPDBG_ASYNC_SAFE),
35 PHPDBG_INFO_COMMAND_D(funcs, "show loaded classes", 'f', info_funcs, NULL, 0, PHPDBG_ASYNC_SAFE),
36 PHPDBG_INFO_COMMAND_D(error, "show last error", 'e', info_error, NULL, 0, PHPDBG_ASYNC_SAFE),
37 PHPDBG_INFO_COMMAND_D(constants, "show user defined constants", 'd', info_constants, NULL, 0, PHPDBG_ASYNC_SAFE),
38 PHPDBG_INFO_COMMAND_D(vars, "show active variables", 'v', info_vars, NULL, 0, PHPDBG_ASYNC_SAFE),
39 PHPDBG_INFO_COMMAND_D(globals, "show superglobals", 'g', info_globals, NULL, 0, PHPDBG_ASYNC_SAFE),
40 PHPDBG_INFO_COMMAND_D(literal, "show active literal constants", 'l', info_literal, NULL, 0, PHPDBG_ASYNC_SAFE),
41 PHPDBG_INFO_COMMAND_D(memory, "show memory manager stats", 'm', info_memory, NULL, 0, PHPDBG_ASYNC_SAFE),
42 PHPDBG_END_COMMAND
43 };
44
PHPDBG_INFO(break)45 PHPDBG_INFO(break) /* {{{ */
46 {
47 phpdbg_print_breakpoints(PHPDBG_BREAK_FILE);
48 phpdbg_print_breakpoints(PHPDBG_BREAK_SYM);
49 phpdbg_print_breakpoints(PHPDBG_BREAK_METHOD);
50 phpdbg_print_breakpoints(PHPDBG_BREAK_OPLINE);
51 phpdbg_print_breakpoints(PHPDBG_BREAK_FILE_OPLINE);
52 phpdbg_print_breakpoints(PHPDBG_BREAK_FUNCTION_OPLINE);
53 phpdbg_print_breakpoints(PHPDBG_BREAK_METHOD_OPLINE);
54 phpdbg_print_breakpoints(PHPDBG_BREAK_COND);
55 phpdbg_print_breakpoints(PHPDBG_BREAK_OPCODE);
56
57 return SUCCESS;
58 } /* }}} */
59
PHPDBG_INFO(files)60 PHPDBG_INFO(files) /* {{{ */
61 {
62 zend_string *fname;
63
64 phpdbg_try_access {
65 phpdbg_notice("includedfilecount", "num=\"%d\"", "Included files: %d", zend_hash_num_elements(&EG(included_files)));
66 } phpdbg_catch_access {
67 phpdbg_error("signalsegv", "", "Could not fetch included file count, invalid data source");
68 return SUCCESS;
69 } phpdbg_end_try_access();
70
71 phpdbg_try_access {
72 ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), fname) {
73 phpdbg_writeln("includedfile", "name=\"%s\"", "File: %s", ZSTR_VAL(fname));
74 } ZEND_HASH_FOREACH_END();
75 } phpdbg_catch_access {
76 phpdbg_error("signalsegv", "", "Could not fetch file name, invalid data source, aborting included file listing");
77 } phpdbg_end_try_access();
78
79 return SUCCESS;
80 } /* }}} */
81
PHPDBG_INFO(error)82 PHPDBG_INFO(error) /* {{{ */
83 {
84 if (PG(last_error_message)) {
85 phpdbg_try_access {
86 phpdbg_writeln("lasterror", "error=\"%s\" file=\"%s\" line=\"%d\"", "Last error: %s at %s line %d", PG(last_error_message), PG(last_error_file), PG(last_error_lineno));
87 } phpdbg_catch_access {
88 phpdbg_notice("lasterror", "error=\"\"", "No error found!");
89 } phpdbg_end_try_access();
90 } else {
91 phpdbg_notice("lasterror", "error=\"\"", "No error found!");
92 }
93 return SUCCESS;
94 } /* }}} */
95
PHPDBG_INFO(constants)96 PHPDBG_INFO(constants) /* {{{ */
97 {
98 HashTable consts;
99 zend_constant *data;
100
101 zend_hash_init(&consts, 8, NULL, NULL, 0);
102
103 if (EG(zend_constants)) {
104 phpdbg_try_access {
105 ZEND_HASH_FOREACH_PTR(EG(zend_constants), data) {
106 if (ZEND_CONSTANT_MODULE_NUMBER(data) == PHP_USER_CONSTANT) {
107 zend_hash_update_ptr(&consts, data->name, data);
108 }
109 } ZEND_HASH_FOREACH_END();
110 } phpdbg_catch_access {
111 phpdbg_error("signalsegv", "", "Cannot fetch all the constants, invalid data source");
112 } phpdbg_end_try_access();
113 }
114
115 phpdbg_notice("constantinfo", "num=\"%d\"", "User-defined constants (%d)", zend_hash_num_elements(&consts));
116
117 if (zend_hash_num_elements(&consts)) {
118 phpdbg_out("Address Refs Type Constant\n");
119 ZEND_HASH_FOREACH_PTR(&consts, data) {
120
121 #define VARIABLEINFO(attrs, msg, ...) \
122 phpdbg_writeln("constant", \
123 "address=\"%p\" refcount=\"%d\" type=\"%s\" name=\"%.*s\" " attrs, \
124 "%-18p %-7d %-9s %.*s" msg, &data->value, \
125 Z_REFCOUNTED(data->value) ? Z_REFCOUNT(data->value) : 1, \
126 zend_get_type_by_const(Z_TYPE(data->value)), \
127 (int) ZSTR_LEN(data->name), ZSTR_VAL(data->name), ##__VA_ARGS__)
128
129 switch (Z_TYPE(data->value)) {
130 case IS_STRING:
131 phpdbg_try_access {
132 VARIABLEINFO("length=\"%zd\" value=\"%.*s\"", "\nstring (%zd) \"%.*s%s\"", Z_STRLEN(data->value), Z_STRLEN(data->value) < 255 ? (int) Z_STRLEN(data->value) : 255, Z_STRVAL(data->value), Z_STRLEN(data->value) > 255 ? "..." : "");
133 } phpdbg_catch_access {
134 VARIABLEINFO("", "");
135 } phpdbg_end_try_access();
136 break;
137 case IS_TRUE:
138 VARIABLEINFO("value=\"true\"", "\nbool (true)");
139 break;
140 case IS_FALSE:
141 VARIABLEINFO("value=\"false\"", "\nbool (false)");
142 break;
143 case IS_LONG:
144 VARIABLEINFO("value=\"%ld\"", "\nint (%ld)", Z_LVAL(data->value));
145 break;
146 case IS_DOUBLE:
147 VARIABLEINFO("value=\"%lf\"", "\ndouble (%lf)", Z_DVAL(data->value));
148 break;
149 default:
150 VARIABLEINFO("", "");
151
152 #undef VARIABLEINFO
153 }
154 } ZEND_HASH_FOREACH_END();
155 }
156
157 return SUCCESS;
158 } /* }}} */
159
phpdbg_arm_auto_global(zval * ptrzv)160 static int phpdbg_arm_auto_global(zval *ptrzv) {
161 zend_auto_global *auto_global = Z_PTR_P(ptrzv);
162
163 if (auto_global->armed) {
164 if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
165 phpdbg_notice("variableinfo", "unreachable=\"%.*s\"", "Cannot show information about superglobal variable %.*s", (int) ZSTR_LEN(auto_global->name), ZSTR_VAL(auto_global->name));
166 } else {
167 auto_global->armed = auto_global->auto_global_callback(auto_global->name);
168 }
169 }
170
171 return 0;
172 }
173
phpdbg_print_symbols(zend_bool show_globals)174 static int phpdbg_print_symbols(zend_bool show_globals) {
175 HashTable vars;
176 zend_array *symtable;
177 zend_string *var;
178 zval *data;
179
180 if (!EG(current_execute_data) || !EG(current_execute_data)->func) {
181 phpdbg_error("inactive", "type=\"op_array\"", "No active op array!");
182 return SUCCESS;
183 }
184
185 if (show_globals) {
186 /* that array should only be manipulated during init, so safe for async access during execution */
187 zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_arm_auto_global);
188 symtable = &EG(symbol_table);
189 } else if (!(symtable = zend_rebuild_symbol_table())) {
190 phpdbg_error("inactive", "type=\"symbol_table\"", "No active symbol table!");
191 return SUCCESS;
192 }
193
194 zend_hash_init(&vars, 8, NULL, NULL, 0);
195
196 phpdbg_try_access {
197 ZEND_HASH_FOREACH_STR_KEY_VAL(symtable, var, data) {
198 if (zend_is_auto_global(var) ^ !show_globals) {
199 zend_hash_update(&vars, var, data);
200 }
201 } ZEND_HASH_FOREACH_END();
202 } phpdbg_catch_access {
203 phpdbg_error("signalsegv", "", "Cannot fetch all data from the symbol table, invalid data source");
204 } phpdbg_end_try_access();
205
206 if (show_globals) {
207 phpdbg_notice("variableinfo", "num=\"%d\"", "Superglobal variables (%d)", zend_hash_num_elements(&vars));
208 } else {
209 zend_op_array *ops = &EG(current_execute_data)->func->op_array;
210
211 if (ops->function_name) {
212 if (ops->scope) {
213 phpdbg_notice("variableinfo", "method=\"%s::%s\" num=\"%d\"", "Variables in %s::%s() (%d)", ops->scope->name->val, ops->function_name->val, zend_hash_num_elements(&vars));
214 } else {
215 phpdbg_notice("variableinfo", "function=\"%s\" num=\"%d\"", "Variables in %s() (%d)", ZSTR_VAL(ops->function_name), zend_hash_num_elements(&vars));
216 }
217 } else {
218 if (ops->filename) {
219 phpdbg_notice("variableinfo", "file=\"%s\" num=\"%d\"", "Variables in %s (%d)", ZSTR_VAL(ops->filename), zend_hash_num_elements(&vars));
220 } else {
221 phpdbg_notice("variableinfo", "opline=\"%p\" num=\"%d\"", "Variables @ %p (%d)", ops, zend_hash_num_elements(&vars));
222 }
223 }
224 }
225
226 if (zend_hash_num_elements(&vars)) {
227 phpdbg_out("Address Refs Type Variable\n");
228 ZEND_HASH_FOREACH_STR_KEY_VAL(&vars, var, data) {
229 phpdbg_try_access {
230 const char *isref = "";
231 #define VARIABLEINFO(attrs, msg, ...) \
232 phpdbg_writeln("variable", \
233 "address=\"%p\" refcount=\"%d\" type=\"%s\" refstatus=\"%s\" name=\"%.*s\" " attrs, \
234 "%-18p %-7d %-9s %s$%.*s" msg, data, Z_REFCOUNTED_P(data) ? Z_REFCOUNT_P(data) : 1, zend_get_type_by_const(Z_TYPE_P(data)), isref, (int) ZSTR_LEN(var), ZSTR_VAL(var), ##__VA_ARGS__)
235 retry_switch:
236 switch (Z_TYPE_P(data)) {
237 case IS_RESOURCE:
238 phpdbg_try_access {
239 const char *type = zend_rsrc_list_get_rsrc_type(Z_RES_P(data));
240 VARIABLEINFO("type=\"%s\"", "\n|-------(typeof)------> (%s)\n", type ? type : "unknown");
241 } phpdbg_catch_access {
242 VARIABLEINFO("type=\"unknown\"", "\n|-------(typeof)------> (unknown)\n");
243 } phpdbg_end_try_access();
244 break;
245 case IS_OBJECT:
246 phpdbg_try_access {
247 VARIABLEINFO("instanceof=\"%s\"", "\n|-----(instanceof)----> (%s)\n", ZSTR_VAL(Z_OBJCE_P(data)->name));
248 } phpdbg_catch_access {
249 VARIABLEINFO("instanceof=\"%s\"", "\n|-----(instanceof)----> (unknown)\n");
250 } phpdbg_end_try_access();
251 break;
252 case IS_STRING:
253 phpdbg_try_access {
254 VARIABLEINFO("length=\"%zd\" value=\"%.*s\"", "\nstring (%zd) \"%.*s%s\"", Z_STRLEN_P(data), Z_STRLEN_P(data) < 255 ? (int) Z_STRLEN_P(data) : 255, Z_STRVAL_P(data), Z_STRLEN_P(data) > 255 ? "..." : "");
255 } phpdbg_catch_access {
256 VARIABLEINFO("", "");
257 } phpdbg_end_try_access();
258 break;
259 case IS_TRUE:
260 VARIABLEINFO("value=\"true\"", "\nbool (true)");
261 break;
262 case IS_FALSE:
263 VARIABLEINFO("value=\"false\"", "\nbool (false)");
264 break;
265 case IS_LONG:
266 VARIABLEINFO("value=\"%ld\"", "\nint (%ld)", Z_LVAL_P(data));
267 break;
268 case IS_DOUBLE:
269 VARIABLEINFO("value=\"%lf\"", "\ndouble (%lf)", Z_DVAL_P(data));
270 break;
271 case IS_REFERENCE:
272 isref = "&";
273 data = Z_REFVAL_P(data);
274 goto retry_switch;
275 case IS_INDIRECT:
276 data = Z_INDIRECT_P(data);
277 goto retry_switch;
278 default:
279 VARIABLEINFO("", "");
280 }
281
282 #undef VARIABLEINFO
283 } phpdbg_catch_access {
284 phpdbg_writeln("variable", "address=\"%p\" name=\"%s\"", "%p\tn/a\tn/a\t$%s", data, ZSTR_VAL(var));
285 } phpdbg_end_try_access();
286 } ZEND_HASH_FOREACH_END();
287 }
288
289 zend_hash_destroy(&vars);
290
291 return SUCCESS;
292 } /* }}} */
293
PHPDBG_INFO(vars)294 PHPDBG_INFO(vars) /* {{{ */
295 {
296 return phpdbg_print_symbols(0);
297 }
298
PHPDBG_INFO(globals)299 PHPDBG_INFO(globals) /* {{{ */
300 {
301 return phpdbg_print_symbols(1);
302 }
303
PHPDBG_INFO(literal)304 PHPDBG_INFO(literal) /* {{{ */
305 {
306 /* literals are assumed to not be manipulated during executing of their op_array and as such async safe */
307 zend_bool in_executor = PHPDBG_G(in_execution) && EG(current_execute_data) && EG(current_execute_data)->func;
308 if (in_executor || PHPDBG_G(ops)) {
309 zend_op_array *ops = in_executor ? &EG(current_execute_data)->func->op_array : PHPDBG_G(ops);
310 int literal = 0, count = ops->last_literal - 1;
311
312 if (ops->function_name) {
313 if (ops->scope) {
314 phpdbg_notice("literalinfo", "method=\"%s::%s\" num=\"%d\"", "Literal Constants in %s::%s() (%d)", ops->scope->name->val, ops->function_name->val, count);
315 } else {
316 phpdbg_notice("literalinfo", "function=\"%s\" num=\"%d\"", "Literal Constants in %s() (%d)", ops->function_name->val, count);
317 }
318 } else {
319 if (ops->filename) {
320 phpdbg_notice("literalinfo", "file=\"%s\" num=\"%d\"", "Literal Constants in %s (%d)", ZSTR_VAL(ops->filename), count);
321 } else {
322 phpdbg_notice("literalinfo", "opline=\"%p\" num=\"%d\"", "Literal Constants @ %p (%d)", ops, count);
323 }
324 }
325
326 while (literal < ops->last_literal) {
327 if (Z_TYPE(ops->literals[literal]) != IS_NULL) {
328 phpdbg_write("literal", "id=\"%u\"", "|-------- C%u -------> [", literal);
329 zend_print_zval(&ops->literals[literal], 0);
330 phpdbg_out("]\n");
331 }
332 literal++;
333 }
334 } else {
335 phpdbg_error("inactive", "type=\"execution\"", "Not executing!");
336 }
337
338 return SUCCESS;
339 } /* }}} */
340
PHPDBG_INFO(memory)341 PHPDBG_INFO(memory) /* {{{ */
342 {
343 size_t used, real, peak_used, peak_real;
344 zend_mm_heap *orig_heap = NULL;
345 zend_bool is_mm;
346
347 if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
348 orig_heap = zend_mm_set_heap(phpdbg_original_heap_sigsafe_mem());
349 }
350 if ((is_mm = is_zend_mm())) {
351 used = zend_memory_usage(0);
352 real = zend_memory_usage(1);
353 peak_used = zend_memory_peak_usage(0);
354 peak_real = zend_memory_peak_usage(1);
355 }
356 if (orig_heap) {
357 zend_mm_set_heap(orig_heap);
358 }
359
360 if (is_mm) {
361 phpdbg_notice("meminfo", "", "Memory Manager Information");
362 phpdbg_notice("current", "", "Current");
363 phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (used / 1024));
364 phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (real / 1024));
365 phpdbg_notice("peak", "", "Peak");
366 phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (peak_used / 1024));
367 phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (peak_real / 1024));
368 } else {
369 phpdbg_error("inactive", "type=\"memory_manager\"", "Memory Manager Disabled!");
370 }
371 return SUCCESS;
372 } /* }}} */
373
phpdbg_print_class_name(zend_class_entry * ce)374 static inline void phpdbg_print_class_name(zend_class_entry *ce) /* {{{ */
375 {
376 const char *visibility = ce->type == ZEND_USER_CLASS ? "User" : "Internal";
377 const char *type = (ce->ce_flags & ZEND_ACC_INTERFACE) ? "Interface" : (ce->ce_flags & ZEND_ACC_ABSTRACT) ? "Abstract Class" : "Class";
378
379 phpdbg_writeln("class", "type=\"%s\" flags=\"%s\" name=\"%.*s\" methodcount=\"%d\"", "%s %s %.*s (%d)", visibility, type, (int) ZSTR_LEN(ce->name), ZSTR_VAL(ce->name), zend_hash_num_elements(&ce->function_table));
380 } /* }}} */
381
PHPDBG_INFO(classes)382 PHPDBG_INFO(classes) /* {{{ */
383 {
384 zend_class_entry *ce;
385 HashTable classes;
386
387 zend_hash_init(&classes, 8, NULL, NULL, 0);
388
389 phpdbg_try_access {
390 ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
391 if (ce->type == ZEND_USER_CLASS) {
392 zend_hash_next_index_insert_ptr(&classes, ce);
393 }
394 } ZEND_HASH_FOREACH_END();
395 } phpdbg_catch_access {
396 phpdbg_notice("signalsegv", "", "Not all classes could be fetched, possibly invalid data source");
397 } phpdbg_end_try_access();
398
399 phpdbg_notice("classinfo", "num=\"%d\"", "User Classes (%d)", zend_hash_num_elements(&classes));
400
401 /* once added, assume that classes are stable... until shutdown. */
402 ZEND_HASH_FOREACH_PTR(&classes, ce) {
403 phpdbg_print_class_name(ce);
404
405 if (ce->parent) {
406 zend_class_entry *pce;
407 phpdbg_xml("<parents %r>");
408 pce = ce->parent;
409 do {
410 phpdbg_out("|-------- ");
411 phpdbg_print_class_name(pce);
412 } while ((pce = pce->parent));
413 phpdbg_xml("</parents>");
414 }
415
416 if (ce->info.user.filename) {
417 phpdbg_writeln("classsource", "file=\"%s\" line=\"%u\"", "|---- in %s on line %u", ZSTR_VAL(ce->info.user.filename), ce->info.user.line_start);
418 } else {
419 phpdbg_writeln("classsource", "", "|---- no source code");
420 }
421 } ZEND_HASH_FOREACH_END();
422
423 zend_hash_destroy(&classes);
424
425 return SUCCESS;
426 } /* }}} */
427
PHPDBG_INFO(funcs)428 PHPDBG_INFO(funcs) /* {{{ */
429 {
430 zend_function *zf;
431 HashTable functions;
432
433 zend_hash_init(&functions, 8, NULL, NULL, 0);
434
435 phpdbg_try_access {
436 ZEND_HASH_FOREACH_PTR(EG(function_table), zf) {
437 if (zf->type == ZEND_USER_FUNCTION) {
438 zend_hash_next_index_insert_ptr(&functions, zf);
439 }
440 } ZEND_HASH_FOREACH_END();
441 } phpdbg_catch_access {
442 phpdbg_notice("signalsegv", "", "Not all functions could be fetched, possibly invalid data source");
443 } phpdbg_end_try_access();
444
445 phpdbg_notice("functioninfo", "num=\"%d\"", "User Functions (%d)", zend_hash_num_elements(&functions));
446
447 ZEND_HASH_FOREACH_PTR(&functions, zf) {
448 zend_op_array *op_array = &zf->op_array;
449
450 phpdbg_write("function", "name=\"%s\"", "|-------- %s", op_array->function_name ? ZSTR_VAL(op_array->function_name) : "{main}");
451
452 if (op_array->filename) {
453 phpdbg_writeln("functionsource", "file=\"%s\" line=\"%d\"", " in %s on line %d", ZSTR_VAL(op_array->filename), op_array->line_start);
454 } else {
455 phpdbg_writeln("functionsource", "", " (no source code)");
456 }
457 } ZEND_HASH_FOREACH_END();
458
459 zend_hash_destroy(&functions);
460
461 return SUCCESS;
462 } /* }}} */
463