xref: /PHP-8.0/sapi/phpdbg/phpdbg_print.c (revision 88355dd3)
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 "phpdbg.h"
20 #include "phpdbg_print.h"
21 #include "phpdbg_utils.h"
22 #include "phpdbg_opcode.h"
23 #include "phpdbg_prompt.h"
24 
25 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
26 
27 #define PHPDBG_PRINT_COMMAND_D(f, h, a, m, l, s, flags) \
28 	PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[8], flags)
29 
30 const phpdbg_command_t phpdbg_print_commands[] = {
31 	PHPDBG_PRINT_COMMAND_D(exec,       "print out the instructions in the main execution context", 'e', print_exec,   NULL, 0, PHPDBG_ASYNC_SAFE),
32 	PHPDBG_PRINT_COMMAND_D(opline,     "print out the instruction in the current opline",          'o', print_opline, NULL, 0, PHPDBG_ASYNC_SAFE),
33 	PHPDBG_PRINT_COMMAND_D(class,      "print out the instructions in the specified class",        'c', print_class,  NULL, "s", PHPDBG_ASYNC_SAFE),
34 	PHPDBG_PRINT_COMMAND_D(method,     "print out the instructions in the specified method",       'm', print_method, NULL, "m", PHPDBG_ASYNC_SAFE),
35 	PHPDBG_PRINT_COMMAND_D(func,       "print out the instructions in the specified function",     'f', print_func,   NULL, "s", PHPDBG_ASYNC_SAFE),
36 	PHPDBG_PRINT_COMMAND_D(stack,      "print out the instructions in the current stack",          's', print_stack,  NULL, 0, PHPDBG_ASYNC_SAFE),
37 	PHPDBG_END_COMMAND
38 };
39 
PHPDBG_PRINT(opline)40 PHPDBG_PRINT(opline) /* {{{ */
41 {
42 	if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
43 		phpdbg_print_opline(phpdbg_user_execute_data(EG(current_execute_data)), 1);
44 	} else {
45 		phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
46 	}
47 
48 	return SUCCESS;
49 } /* }}} */
50 
phpdbg_print_function_helper(zend_function * method)51 static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
52 {
53 	switch (method->type) {
54 		case ZEND_USER_FUNCTION: {
55 			zend_op_array* op_array = &(method->op_array);
56 
57 			if (op_array) {
58 				zend_op *opline = &(op_array->opcodes[0]);
59 				uint32_t opcode = 0,
60 				end = op_array->last-1;
61 
62 				if (method->common.scope) {
63 					phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" method=\"%s::%s\" file=\"%s\" opline=\"%p\"", "L%d-%d %s::%s() %s - %p + %d ops",
64 						op_array->line_start,
65 						op_array->line_end,
66 						ZSTR_VAL(method->common.scope->name),
67 						ZSTR_VAL(method->common.function_name),
68 						op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
69 						opline,
70 						op_array->last);
71 				} else {
72 					phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\" opline=\"%p\"", "L%d-%d %s() %s - %p + %d ops",
73 						op_array->line_start,
74 						op_array->line_end,
75 						method->common.function_name ? ZSTR_VAL(method->common.function_name) : "{main}",
76 						op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
77 						opline,
78 						op_array->last);
79 				}
80 
81 				do {
82 					char *decode = phpdbg_decode_opline(op_array, opline);
83 					phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
84 						opline->lineno,
85 						opcode,
86 						decode);
87 					efree(decode);
88 					opline++;
89 				} while (opcode++ < end);
90 			}
91 		} break;
92 
93 		default: {
94 			if (method->common.scope) {
95 				phpdbg_writeln("printoplineinfo", "type=\"Internal\" method=\"%s::%s\"", "\tInternal %s::%s()", ZSTR_VAL(method->common.scope->name), ZSTR_VAL(method->common.function_name));
96 			} else {
97 				phpdbg_writeln("printoplineinfo", "type=\"Internal\" function=\"%s\"", "\tInternal %s()", ZSTR_VAL(method->common.function_name));
98 			}
99 		}
100 	}
101 } /* }}} */
102 
PHPDBG_PRINT(exec)103 PHPDBG_PRINT(exec) /* {{{ */
104 {
105 	if (PHPDBG_G(exec)) {
106 		if (!PHPDBG_G(ops) && !(PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER)) {
107 			phpdbg_compile();
108 		}
109 
110 		if (PHPDBG_G(ops)) {
111 			phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Context %s (%d ops)", PHPDBG_G(exec), PHPDBG_G(ops)->last);
112 
113 			phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops));
114 		}
115 	} else {
116 		phpdbg_error("inactive", "type=\"nocontext\"", "No execution context set");
117 	}
118 
119 return SUCCESS;
120 } /* }}} */
121 
PHPDBG_PRINT(stack)122 PHPDBG_PRINT(stack) /* {{{ */
123 {
124 	if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
125 		zend_op_array *ops = &phpdbg_user_execute_data(EG(current_execute_data))->func->op_array;
126 		if (ops->function_name) {
127 			if (ops->scope) {
128 				phpdbg_notice("printinfo", "method=\"%s::%s\" num=\"%d\"", "Stack in %s::%s() (%d ops)", ZSTR_VAL(ops->scope->name), ZSTR_VAL(ops->function_name), ops->last);
129 			} else {
130 				phpdbg_notice("printinfo", "function=\"%s\" num=\"%d\"", "Stack in %s() (%d ops)", ZSTR_VAL(ops->function_name), ops->last);
131 			}
132 		} else {
133 			if (ops->filename) {
134 				phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Stack in %s (%d ops)", ZSTR_VAL(ops->filename), ops->last);
135 			} else {
136 				phpdbg_notice("printinfo", "opline=\"%p\" num=\"%d\"", "Stack @ %p (%d ops)", ops, ops->last);
137 			}
138 		}
139 		phpdbg_print_function_helper((zend_function*) ops);
140 	} else {
141 		phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
142 	}
143 
144 	return SUCCESS;
145 } /* }}} */
146 
PHPDBG_PRINT(class)147 PHPDBG_PRINT(class) /* {{{ */
148 {
149 	zend_class_entry *ce;
150 
151 	if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
152 		phpdbg_notice("printinfo", "type=\"%s\" flag=\"%s\" class=\"%s\" num=\"%d\"", "%s %s: %s (%d methods)",
153 			(ce->type == ZEND_USER_CLASS) ?
154 				"User" : "Internal",
155 			(ce->ce_flags & ZEND_ACC_INTERFACE) ?
156 				"Interface" :
157 				(ce->ce_flags & ZEND_ACC_ABSTRACT) ?
158 					"Abstract Class" :
159 					"Class",
160 			ZSTR_VAL(ce->name),
161 			zend_hash_num_elements(&ce->function_table));
162 
163 		phpdbg_xml("<printmethods %r>");
164 
165 		if (zend_hash_num_elements(&ce->function_table)) {
166 			zend_function *method;
167 
168 			ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
169 				phpdbg_print_function_helper(method);
170 			} ZEND_HASH_FOREACH_END();
171 		}
172 
173 		phpdbg_xml("</printmethods>");
174 	} else {
175 		phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->str);
176 	}
177 
178 	return SUCCESS;
179 } /* }}} */
180 
PHPDBG_PRINT(method)181 PHPDBG_PRINT(method) /* {{{ */
182 {
183 	zend_class_entry *ce;
184 
185 	if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
186 		zend_function *fbc;
187 		zend_string *lcname = zend_string_alloc(strlen(param->method.name), 0);
188 		zend_str_tolower_copy(ZSTR_VAL(lcname), param->method.name, ZSTR_LEN(lcname));
189 
190 		if ((fbc = zend_hash_find_ptr(&ce->function_table, lcname))) {
191 			phpdbg_notice("printinfo", "type=\"%s\" flags=\"Method\" symbol=\"%s\" num=\"%d\"", "%s Method %s (%d ops)",
192 				(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
193 				ZSTR_VAL(fbc->common.function_name),
194 				(fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
195 
196 			phpdbg_print_function_helper(fbc);
197 		} else {
198 			phpdbg_error("print", "type=\"nomethod\" method=\"%s::%s\"", "The method %s::%s could not be found", param->method.class, param->method.name);
199 		}
200 
201 		zend_string_release(lcname);
202 	} else {
203 		phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->method.class);
204 	}
205 
206 	return SUCCESS;
207 } /* }}} */
208 
PHPDBG_PRINT(func)209 PHPDBG_PRINT(func) /* {{{ */
210 {
211 	HashTable *func_table = EG(function_table);
212 	zend_function* fbc;
213 	const char *func_name = param->str;
214 	size_t func_name_len = param->len;
215 	zend_string *lcname;
216 	/* search active scope if begins with period */
217 	if (func_name[0] == '.') {
218 		zend_class_entry *scope = zend_get_executed_scope();
219 
220 		if (scope) {
221 			func_name++;
222 			func_name_len--;
223 
224 			func_table = &scope->function_table;
225 		} else {
226 			phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
227 			return SUCCESS;
228 		}
229 	} else if (!EG(function_table)) {
230 		phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
231 		return SUCCESS;
232 	} else {
233 		func_table = EG(function_table);
234 	}
235 
236 	lcname = zend_string_alloc(func_name_len, 0);
237 	zend_str_tolower_copy(ZSTR_VAL(lcname), func_name, ZSTR_LEN(lcname));
238 
239 	phpdbg_try_access {
240 		if ((fbc = zend_hash_find_ptr(func_table, lcname))) {
241 			phpdbg_notice("printinfo", "type=\"%s\" flags=\"%s\" symbol=\"%s\" num=\"%d\"", "%s %s %s (%d ops)",
242 				(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
243 				(fbc->common.scope) ? "Method" : "Function",
244 				ZSTR_VAL(fbc->common.function_name),
245 				(fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
246 
247 			phpdbg_print_function_helper(fbc);
248 		} else {
249 			phpdbg_error("print", "type=\"nofunction\" function=\"%s\"", "The function %s could not be found", func_name);
250 		}
251 	} phpdbg_catch_access {
252 		phpdbg_error("signalsegv", "function=\"%.*s\"", "Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name);
253 	} phpdbg_end_try_access();
254 
255 	efree(lcname);
256 
257 	return SUCCESS;
258 } /* }}} */
259 
phpdbg_print_opcodes_main()260 void phpdbg_print_opcodes_main() {
261 	phpdbg_out("function name: (null)\n");
262 	phpdbg_print_function_helper((zend_function *) PHPDBG_G(ops));
263 }
264 
phpdbg_print_opcodes_function(const char * function,size_t len)265 void phpdbg_print_opcodes_function(const char *function, size_t len) {
266 	zend_function *func = zend_hash_str_find_ptr(EG(function_table), function, len);
267 
268 	if (!func) {
269 		zend_string *rt_name;
270 		ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, func) {
271 			if (func->type == ZEND_USER_FUNCTION && *rt_name->val == '\0') {
272 				if (func->op_array.function_name->len == len && !zend_binary_strcasecmp(function, len, func->op_array.function_name->val, func->op_array.function_name->len)) {
273 					phpdbg_print_opcodes_function(rt_name->val, rt_name->len);
274 				}
275 			}
276 		} ZEND_HASH_FOREACH_END();
277 
278 		return;
279 	}
280 
281 	phpdbg_out("function name: %.*s\n", (int) ZSTR_LEN(func->op_array.function_name), ZSTR_VAL(func->op_array.function_name));
282 	phpdbg_print_function_helper(func);
283 }
284 
phpdbg_print_opcodes_method_ce(zend_class_entry * ce,const char * function)285 static void phpdbg_print_opcodes_method_ce(zend_class_entry *ce, const char *function) {
286 	zend_function *func;
287 
288 	if (ce->type != ZEND_USER_CLASS) {
289 		phpdbg_out("function name: %s::%s (internal)\n", ce->name->val, function);
290 		return;
291 	}
292 
293 	if (!(func = zend_hash_str_find_ptr(&ce->function_table, function, strlen(function)))) {
294 		return;
295 	}
296 
297 	phpdbg_out("function name: %s::%s\n", ce->name->val, function);
298 	phpdbg_print_function_helper(func);
299 }
300 
phpdbg_print_opcodes_method(const char * class,const char * function)301 void phpdbg_print_opcodes_method(const char *class, const char *function) {
302 	zend_class_entry *ce;
303 
304 	if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
305 		zend_string *rt_name;
306 		ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, ce) {
307 			if (ce->type == ZEND_USER_CLASS && *rt_name->val == '\0') {
308 				if (ce->name->len == strlen(class) && !zend_binary_strcasecmp(class, strlen(class), ce->name->val, ce->name->len)) {
309 					phpdbg_print_opcodes_method_ce(ce, function);
310 				}
311 			}
312 		} ZEND_HASH_FOREACH_END();
313 
314 		return;
315 	}
316 
317 	phpdbg_print_opcodes_method_ce(ce, function);
318 }
319 
phpdbg_print_opcodes_ce(zend_class_entry * ce)320 static void phpdbg_print_opcodes_ce(zend_class_entry *ce) {
321 	zend_function *method;
322 	zend_string *method_name;
323 	zend_bool first = 1;
324 
325 	phpdbg_out("%s %s: %s\n",
326 		(ce->type == ZEND_USER_CLASS) ?
327 			"user" : "internal",
328 		(ce->ce_flags & ZEND_ACC_INTERFACE) ?
329 			"interface" :
330 			(ce->ce_flags & ZEND_ACC_ABSTRACT) ?
331 				"abstract Class" :
332 				"class",
333 		ZSTR_VAL(ce->name));
334 
335 	if (ce->type != ZEND_USER_CLASS) {
336 		return;
337 	}
338 
339 	phpdbg_out("%d methods: ", zend_hash_num_elements(&ce->function_table));
340 	ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
341 		if (first) {
342 			first = 0;
343 		} else {
344 			phpdbg_out(", ");
345 		}
346 		phpdbg_out("%s", ZSTR_VAL(method->common.function_name));
347 	} ZEND_HASH_FOREACH_END();
348 	if (first) {
349 		phpdbg_out("-");
350 	}
351 	phpdbg_out("\n");
352 
353 	ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, method_name, method) {
354 		phpdbg_out("\nfunction name: %s\n", ZSTR_VAL(method_name));
355 		phpdbg_print_function_helper(method);
356 	} ZEND_HASH_FOREACH_END();
357 }
358 
phpdbg_print_opcodes_class(const char * class)359 void phpdbg_print_opcodes_class(const char *class) {
360 	zend_class_entry *ce;
361 
362 	if (phpdbg_safe_class_lookup(class, strlen(class), &ce) != SUCCESS) {
363 		zend_string *rt_name;
364 		ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), rt_name, ce) {
365 			if (ce->type == ZEND_USER_CLASS && *rt_name->val == '\0') {
366 				if (ce->name->len == strlen(class) && !zend_binary_strcasecmp(class, strlen(class), ce->name->val, ce->name->len)) {
367 					phpdbg_print_opcodes_ce(ce);
368 				}
369 			}
370 		} ZEND_HASH_FOREACH_END();
371 
372 		return;
373 	}
374 
375 	phpdbg_print_opcodes_ce(ce);
376 }
377 
phpdbg_print_opcodes(const char * function)378 PHPDBG_API void phpdbg_print_opcodes(const char *function)
379 {
380 	if (function == NULL) {
381 		phpdbg_print_opcodes_main();
382 	} else if (function[0] == '*' && function[1] == 0) {
383 		/* all */
384 		zend_string *name;
385 		zend_function *func;
386 		zend_class_entry *ce;
387 
388 		phpdbg_print_opcodes_main();
389 
390 		ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), name, func) {
391 			if (func->type == ZEND_USER_FUNCTION) {
392 				phpdbg_out("\n");
393 				phpdbg_print_opcodes_function(ZSTR_VAL(name), ZSTR_LEN(name));
394 			}
395 		} ZEND_HASH_FOREACH_END();
396 
397 		ZEND_HASH_FOREACH_PTR(EG(class_table), ce) {
398 			if (ce->type == ZEND_USER_CLASS) {
399 				phpdbg_out("\n\n");
400 				phpdbg_print_opcodes_ce(ce);
401 			}
402 		} ZEND_HASH_FOREACH_END();
403 	} else {
404 		char *function_lowercase = zend_str_tolower_dup(function, strlen(function));
405 
406 		if (strstr(function_lowercase, "::") == NULL) {
407 			phpdbg_print_opcodes_function(function_lowercase, strlen(function_lowercase));
408 		} else {
409 			char *method_name, *class_name = strtok(function_lowercase, "::");
410 			if ((method_name = strtok(NULL, "::")) == NULL) {
411 				phpdbg_print_opcodes_class(class_name);
412 			} else {
413 				phpdbg_print_opcodes_method(class_name, method_name);
414 			}
415 		}
416 
417 		efree(function_lowercase);
418 	}
419 }
420