xref: /PHP-5.6/sapi/phpdbg/phpdbg_print.c (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 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) \
30 	PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[9])
31 
32 const phpdbg_command_t phpdbg_print_commands[] = {
33 	PHPDBG_PRINT_COMMAND_D(exec,       "print out the instructions in the execution context",  'e', print_exec,   NULL, 0),
34 	PHPDBG_PRINT_COMMAND_D(opline,     "print out the instruction in the current opline",      'o', print_opline, NULL, 0),
35 	PHPDBG_PRINT_COMMAND_D(class,      "print out the instructions in the specified class",    'c', print_class,  NULL, "s"),
36 	PHPDBG_PRINT_COMMAND_D(method,     "print out the instructions in the specified method",   'm', print_method, NULL, "m"),
37 	PHPDBG_PRINT_COMMAND_D(func,       "print out the instructions in the specified function", 'f', print_func,   NULL, "s"),
38 	PHPDBG_PRINT_COMMAND_D(stack,      "print out the instructions in the current stack",      's', print_stack,  NULL, 0),
39 	PHPDBG_END_COMMAND
40 };
41 
PHPDBG_PRINT(opline)42 PHPDBG_PRINT(opline) /* {{{ */
43 {
44 	if (EG(in_execution) && EG(current_execute_data)) {
45 		phpdbg_print_opline(EG(current_execute_data), 1 TSRMLS_CC);
46 	} else {
47 		phpdbg_error("Not Executing!");
48 	}
49 
50 	return SUCCESS;
51 } /* }}} */
52 
phpdbg_print_function_helper(zend_function * method TSRMLS_DC)53 static inline void phpdbg_print_function_helper(zend_function *method TSRMLS_DC) /* {{{ */
54 {
55 	switch (method->type) {
56 		case ZEND_USER_FUNCTION: {
57 			zend_op_array* op_array = &(method->op_array);
58 			HashTable vars;
59 
60 			if (op_array) {
61 				zend_op *opline = &(op_array->opcodes[0]);
62 				zend_uint opcode = 0,
63 				end = op_array->last-1;
64 
65 				if (method->common.scope) {
66 					phpdbg_writeln("\tL%d-%d %s::%s() %s",
67 						op_array->line_start, op_array->line_end,
68 						method->common.scope->name,
69 						method->common.function_name,
70 						op_array->filename ? op_array->filename : "unknown");
71 				} else {
72 					phpdbg_writeln("\tL%d-%d %s() %s",
73 						method->common.function_name ? op_array->line_start : 0,
74 						method->common.function_name ? op_array->line_end : 0,
75 						method->common.function_name ? method->common.function_name : "{main}",
76 						op_array->filename ? op_array->filename : "unknown");
77 				}
78 
79 				zend_hash_init(&vars, op_array->last, NULL, NULL, 0);
80 				do {
81 					char *decode = phpdbg_decode_opline(op_array, opline, &vars TSRMLS_CC);
82 					if (decode != NULL) {
83 						phpdbg_writeln("\t\tL%u\t%p %-30s %s",
84 							opline->lineno,
85 							opline,
86 							phpdbg_decode_opcode(opline->opcode),
87 							decode);
88 						free(decode);
89 					} else {
90 						phpdbg_error("\tFailed to decode opline %16p", opline);
91 					}
92 					opline++;
93 				} while (opcode++ < end);
94 				zend_hash_destroy(&vars);
95 			}
96 		} break;
97 
98 		default: {
99 			if (method->common.scope) {
100 				phpdbg_writeln("\tInternal %s::%s()", method->common.scope->name, method->common.function_name);
101 			} else {
102 				phpdbg_writeln("\tInternal %s()", method->common.function_name);
103 			}
104 		}
105 	}
106 } /* }}} */
107 
PHPDBG_PRINT(exec)108 PHPDBG_PRINT(exec) /* {{{ */
109 {
110 	if (PHPDBG_G(exec)) {
111 		if (!PHPDBG_G(ops)) {
112 			phpdbg_compile(TSRMLS_C);
113 		}
114 
115 		if (PHPDBG_G(ops)) {
116 			phpdbg_notice("Context %s", PHPDBG_G(exec));
117 
118 			phpdbg_print_function_helper((zend_function*) PHPDBG_G(ops) TSRMLS_CC);
119 		}
120 	} else {
121 		phpdbg_error("No execution context set");
122 	}
123 
124 return SUCCESS;
125 } /* }}} */
126 
PHPDBG_PRINT(stack)127 PHPDBG_PRINT(stack) /* {{{ */
128 {
129 	zend_op_array *ops = EG(active_op_array);
130 
131 	if (EG(in_execution) && ops) {
132 		if (ops->function_name) {
133 			if (ops->scope) {
134 				phpdbg_notice("Stack in %s::%s()", ops->scope->name, ops->function_name);
135 			} else {
136 				phpdbg_notice("Stack in %s()", ops->function_name);
137 			}
138 		} else {
139 			if (ops->filename) {
140 				phpdbg_notice("Stack in %s", ops->filename);
141 			} else {
142 				phpdbg_notice("Stack @ %p", ops);
143 			}
144 		}
145 		phpdbg_print_function_helper((zend_function*) ops TSRMLS_CC);
146 	} else {
147 		phpdbg_error("Not Executing!");
148 	}
149 
150 	return SUCCESS;
151 } /* }}} */
152 
PHPDBG_PRINT(class)153 PHPDBG_PRINT(class) /* {{{ */
154 {
155 	zend_class_entry **ce;
156 
157 	if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
158 		phpdbg_notice("%s %s: %s",
159 			((*ce)->type == ZEND_USER_CLASS) ?
160 				"User" : "Internal",
161 			((*ce)->ce_flags & ZEND_ACC_INTERFACE) ?
162 				"Interface" :
163 				((*ce)->ce_flags & ZEND_ACC_ABSTRACT) ?
164 					"Abstract Class" :
165 					"Class",
166 			(*ce)->name);
167 
168 		phpdbg_writeln("Methods (%d):", zend_hash_num_elements(&(*ce)->function_table));
169 		if (zend_hash_num_elements(&(*ce)->function_table)) {
170 			HashPosition position;
171 			zend_function *method;
172 
173 			for (zend_hash_internal_pointer_reset_ex(&(*ce)->function_table, &position);
174 			     zend_hash_get_current_data_ex(&(*ce)->function_table, (void**) &method, &position) == SUCCESS;
175 			     zend_hash_move_forward_ex(&(*ce)->function_table, &position)) {
176 				phpdbg_print_function_helper(method TSRMLS_CC);
177 			}
178 		}
179 	} else {
180 		phpdbg_error("The class %s could not be found", param->str);
181 	}
182 
183 	return SUCCESS;
184 } /* }}} */
185 
PHPDBG_PRINT(method)186 PHPDBG_PRINT(method) /* {{{ */
187 {
188 	zend_class_entry **ce;
189 
190 	if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
191 		zend_function *fbc;
192 		char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
193 
194 		if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
195 			phpdbg_notice("%s Method %s",
196 				(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
197 				fbc->common.function_name);
198 
199 			phpdbg_print_function_helper(fbc TSRMLS_CC);
200 		} else {
201 			phpdbg_error("The method %s could not be found", param->method.name);
202 		}
203 
204 		efree(lcname);
205 	} else {
206 		phpdbg_error("The class %s could not be found", param->method.class);
207 	}
208 
209 	return SUCCESS;
210 } /* }}} */
211 
PHPDBG_PRINT(func)212 PHPDBG_PRINT(func) /* {{{ */
213 {
214 	HashTable *func_table = EG(function_table);
215 	zend_function* fbc;
216 	const char *func_name = param->str;
217 	size_t func_name_len = param->len;
218 	char *lcname;
219 	/* search active scope if begins with period */
220 	if (func_name[0] == '.') {
221 		if (EG(scope)) {
222 			func_name++;
223 			func_name_len--;
224 
225 			func_table = &EG(scope)->function_table;
226 		} else {
227 			phpdbg_error("No active class");
228 			return SUCCESS;
229 		}
230 	} else if (!EG(function_table)) {
231 		phpdbg_error("No function table loaded");
232 		return SUCCESS;
233 	} else {
234 		func_table = EG(function_table);
235 	}
236 
237 	lcname  = zend_str_tolower_dup(func_name, func_name_len);
238 
239 	if (zend_hash_find(func_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) {
240 		phpdbg_notice("%s %s %s",
241 			(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
242 			(fbc->common.scope) ? "Method" : "Function",
243 			fbc->common.function_name);
244 
245 		phpdbg_print_function_helper(fbc TSRMLS_CC);
246 	} else {
247 		phpdbg_error("The function %s could not be found", func_name);
248 	}
249 
250 	efree(lcname);
251 
252 	return SUCCESS;
253 } /* }}} */
254