xref: /PHP-7.0/sapi/phpdbg/phpdbg_print.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 "phpdbg.h"
22 #include "phpdbg_print.h"
23 #include "phpdbg_utils.h"
24 #include "phpdbg_opcode.h"
25 #include "phpdbg_prompt.h"
26 
27 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
28 
29 #define PHPDBG_PRINT_COMMAND_D(f, h, a, m, l, s, flags) \
30 	PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[8], flags)
31 
32 const phpdbg_command_t phpdbg_print_commands[] = {
33 	PHPDBG_PRINT_COMMAND_D(exec,       "print out the instructions in the main execution context", 'e', print_exec,   NULL, 0, PHPDBG_ASYNC_SAFE),
34 	PHPDBG_PRINT_COMMAND_D(opline,     "print out the instruction in the current opline",          'o', print_opline, NULL, 0, PHPDBG_ASYNC_SAFE),
35 	PHPDBG_PRINT_COMMAND_D(class,      "print out the instructions in the specified class",        'c', print_class,  NULL, "s", PHPDBG_ASYNC_SAFE),
36 	PHPDBG_PRINT_COMMAND_D(method,     "print out the instructions in the specified method",       'm', print_method, NULL, "m", PHPDBG_ASYNC_SAFE),
37 	PHPDBG_PRINT_COMMAND_D(func,       "print out the instructions in the specified function",     'f', print_func,   NULL, "s", PHPDBG_ASYNC_SAFE),
38 	PHPDBG_PRINT_COMMAND_D(stack,      "print out the instructions in the current stack",          's', print_stack,  NULL, 0, PHPDBG_ASYNC_SAFE),
39 	PHPDBG_END_COMMAND
40 };
41 
PHPDBG_PRINT(opline)42 PHPDBG_PRINT(opline) /* {{{ */
43 {
44 	if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
45 		phpdbg_print_opline(phpdbg_user_execute_data(EG(current_execute_data)), 1);
46 	} else {
47 		phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
48 	}
49 
50 	return SUCCESS;
51 } /* }}} */
52 
phpdbg_print_function_helper(zend_function * method)53 static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
54 {
55 	switch (method->type) {
56 		case ZEND_USER_FUNCTION: {
57 			zend_op_array* op_array = &(method->op_array);
58 
59 			if (op_array) {
60 				zend_op *opline = &(op_array->opcodes[0]);
61 				uint32_t opcode = 0,
62 				end = op_array->last-1;
63 
64 				if (method->common.scope) {
65 					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",
66 						op_array->line_start,
67 						op_array->line_end,
68 						ZSTR_VAL(method->common.scope->name),
69 						ZSTR_VAL(method->common.function_name),
70 						op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
71 						opline,
72 						op_array->last);
73 				} else {
74 					phpdbg_writeln("printoplineinfo", "type=\"User\" startline=\"%d\" endline=\"%d\" function=\"%s\" file=\"%s\" opline=\"%p\"", "L%d-%d %s() %s - %p + %d ops",
75 						op_array->line_start,
76 						op_array->line_end,
77 						method->common.function_name ? ZSTR_VAL(method->common.function_name) : "{main}",
78 						op_array->filename ? ZSTR_VAL(op_array->filename) : "unknown",
79 						opline,
80 						op_array->last);
81 				}
82 
83 				do {
84 					char *decode = phpdbg_decode_opline(op_array, opline);
85 					phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
86 						opline->lineno,
87 						opcode,
88 						decode);
89 					efree(decode);
90 					opline++;
91 				} while (opcode++ < end);
92 			}
93 		} break;
94 
95 		default: {
96 			if (method->common.scope) {
97 				phpdbg_writeln("printoplineinfo", "type=\"Internal\" method=\"%s::%s\"", "\tInternal %s::%s()", ZSTR_VAL(method->common.scope->name), ZSTR_VAL(method->common.function_name));
98 			} else {
99 				phpdbg_writeln("printoplineinfo", "type=\"Internal\" function=\"%s\"", "\tInternal %s()", ZSTR_VAL(method->common.function_name));
100 			}
101 		}
102 	}
103 } /* }}} */
104 
PHPDBG_PRINT(exec)105 PHPDBG_PRINT(exec) /* {{{ */
106 {
107 	if (PHPDBG_G(exec)) {
108 		if (!PHPDBG_G(ops) && !(PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER)) {
109 			phpdbg_compile();
110 		}
111 
112 		if (PHPDBG_G(ops)) {
113 			phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Context %s (%d ops)", PHPDBG_G(exec), PHPDBG_G(ops)->last);
114 
115 			phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops));
116 		}
117 	} else {
118 		phpdbg_error("inactive", "type=\"nocontext\"", "No execution context set");
119 	}
120 
121 return SUCCESS;
122 } /* }}} */
123 
PHPDBG_PRINT(stack)124 PHPDBG_PRINT(stack) /* {{{ */
125 {
126 	if (PHPDBG_G(in_execution) && EG(current_execute_data)) {
127 		zend_op_array *ops = &phpdbg_user_execute_data(EG(current_execute_data))->func->op_array;
128 		if (ops->function_name) {
129 			if (ops->scope) {
130 				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);
131 			} else {
132 				phpdbg_notice("printinfo", "function=\"%s\" num=\"%d\"", "Stack in %s() (%d ops)", ZSTR_VAL(ops->function_name), ops->last);
133 			}
134 		} else {
135 			if (ops->filename) {
136 				phpdbg_notice("printinfo", "file=\"%s\" num=\"%d\"", "Stack in %s (%d ops)", ZSTR_VAL(ops->filename), ops->last);
137 			} else {
138 				phpdbg_notice("printinfo", "opline=\"%p\" num=\"%d\"", "Stack @ %p (%d ops)", ops, ops->last);
139 			}
140 		}
141 		phpdbg_print_function_helper((zend_function*) ops);
142 	} else {
143 		phpdbg_error("inactive", "type=\"execution\"", "Not Executing!");
144 	}
145 
146 	return SUCCESS;
147 } /* }}} */
148 
PHPDBG_PRINT(class)149 PHPDBG_PRINT(class) /* {{{ */
150 {
151 	zend_class_entry *ce;
152 
153 	if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
154 		phpdbg_notice("printinfo", "type=\"%s\" flag=\"%s\" class=\"%s\" num=\"%d\"", "%s %s: %s (%d methods)",
155 			(ce->type == ZEND_USER_CLASS) ?
156 				"User" : "Internal",
157 			(ce->ce_flags & ZEND_ACC_INTERFACE) ?
158 				"Interface" :
159 				(ce->ce_flags & ZEND_ACC_ABSTRACT) ?
160 					"Abstract Class" :
161 					"Class",
162 			ZSTR_VAL(ce->name),
163 			zend_hash_num_elements(&ce->function_table));
164 
165 		phpdbg_xml("<printmethods %r>");
166 
167 		if (zend_hash_num_elements(&ce->function_table)) {
168 			zend_function *method;
169 
170 			ZEND_HASH_FOREACH_PTR(&ce->function_table, method) {
171 				phpdbg_print_function_helper(method);
172 			} ZEND_HASH_FOREACH_END();
173 		}
174 
175 		phpdbg_xml("</printmethods>");
176 	} else {
177 		phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->str);
178 	}
179 
180 	return SUCCESS;
181 } /* }}} */
182 
PHPDBG_PRINT(method)183 PHPDBG_PRINT(method) /* {{{ */
184 {
185 	zend_class_entry *ce;
186 
187 	if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
188 		zend_function *fbc;
189 		zend_string *lcname = zend_string_alloc(strlen(param->method.name), 0);
190 		zend_str_tolower_copy(ZSTR_VAL(lcname), param->method.name, ZSTR_LEN(lcname));
191 
192 		if ((fbc = zend_hash_find_ptr(&ce->function_table, lcname))) {
193 			phpdbg_notice("printinfo", "type=\"%s\" flags=\"Method\" symbol=\"%s\" num=\"%d\"", "%s Method %s (%d ops)",
194 				(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
195 				ZSTR_VAL(fbc->common.function_name),
196 				(fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0);
197 
198 			phpdbg_print_function_helper(fbc);
199 		} else {
200 			phpdbg_error("print", "type=\"nomethod\" method=\"%s::%s\"", "The method %s::%s could not be found", param->method.class, param->method.name);
201 		}
202 
203 		zend_string_release(lcname);
204 	} else {
205 		phpdbg_error("print", "type=\"noclass\" class=\"%s\"", "The class %s could not be found", param->method.class);
206 	}
207 
208 	return SUCCESS;
209 } /* }}} */
210 
PHPDBG_PRINT(func)211 PHPDBG_PRINT(func) /* {{{ */
212 {
213 	HashTable *func_table = EG(function_table);
214 	zend_function* fbc;
215 	const char *func_name = param->str;
216 	size_t func_name_len = param->len;
217 	zend_string *lcname;
218 	/* search active scope if begins with period */
219 	if (func_name[0] == '.') {
220 		if (EG(scope)) {
221 			func_name++;
222 			func_name_len--;
223 
224 			func_table = &EG(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(char * function)378 PHPDBG_API void phpdbg_print_opcodes(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 		function = zend_str_tolower_dup(function, strlen(function));
405 
406 		if (strstr(function, "::") == NULL) {
407 			phpdbg_print_opcodes_function(function, strlen(function));
408 		} else {
409 			char *method_name, *class_name = strtok(function, "::");
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);
418 	}
419 }
420