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 <stdio.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #ifndef _WIN32
23 # include <sys/mman.h>
24 # include <unistd.h>
25 #endif
26 #include <fcntl.h>
27 #include "phpdbg.h"
28 #include "phpdbg_list.h"
29 #include "phpdbg_utils.h"
30 #include "phpdbg_prompt.h"
31 #include "php_streams.h"
32 #include "zend_exceptions.h"
33
34 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
35
36 #define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s, flags) \
37 PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[12], flags)
38
39 const phpdbg_command_t phpdbg_list_commands[] = {
40 PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l", PHPDBG_ASYNC_SAFE),
41 PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s", PHPDBG_ASYNC_SAFE),
42 PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m", PHPDBG_ASYNC_SAFE),
43 PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s", PHPDBG_ASYNC_SAFE),
44 PHPDBG_END_COMMAND
45 };
46
PHPDBG_LIST(lines)47 PHPDBG_LIST(lines) /* {{{ */
48 {
49 if (!PHPDBG_G(exec) && !zend_is_executing()) {
50 phpdbg_error("inactive", "type=\"execution\"", "Not executing, and execution context not set");
51 return SUCCESS;
52 }
53
54 switch (param->type) {
55 case NUMERIC_PARAM: {
56 const char *char_file = phpdbg_current_file();
57 zend_string *file = zend_string_init(char_file, strlen(char_file), 0);
58 phpdbg_list_file(file, param->num < 0 ? 1 - param->num : param->num, (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(), 0);
59 efree(file);
60 } break;
61
62 case FILE_PARAM: {
63 zend_string *file;
64 char resolved_path_buf[MAXPATHLEN];
65 const char *abspath = param->file.name;
66 if (VCWD_REALPATH(abspath, resolved_path_buf)) {
67 abspath = resolved_path_buf;
68 }
69 file = zend_string_init(abspath, strlen(abspath), 0);
70 phpdbg_list_file(file, param->file.line, 0, 0);
71 zend_string_release(file);
72 } break;
73
74 phpdbg_default_switch_case();
75 }
76
77 return SUCCESS;
78 } /* }}} */
79
PHPDBG_LIST(func)80 PHPDBG_LIST(func) /* {{{ */
81 {
82 phpdbg_list_function_byname(param->str, param->len);
83
84 return SUCCESS;
85 } /* }}} */
86
PHPDBG_LIST(method)87 PHPDBG_LIST(method) /* {{{ */
88 {
89 zend_class_entry *ce;
90
91 if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
92 zend_function *function;
93 char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
94
95 if ((function = zend_hash_str_find_ptr(&ce->function_table, lcname, strlen(lcname)))) {
96 phpdbg_list_function(function);
97 } else {
98 phpdbg_error("list", "type=\"notfound\" method=\"%s::%s\"", "Could not find %s::%s", param->method.class, param->method.name);
99 }
100
101 efree(lcname);
102 } else {
103 phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "Could not find the class %s", param->method.class);
104 }
105
106 return SUCCESS;
107 } /* }}} */
108
PHPDBG_LIST(class)109 PHPDBG_LIST(class) /* {{{ */
110 {
111 zend_class_entry *ce;
112
113 if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
114 if (ce->type == ZEND_USER_CLASS) {
115 if (ce->info.user.filename) {
116 phpdbg_list_file(ce->info.user.filename, ce->info.user.line_end - ce->info.user.line_start + 1, ce->info.user.line_start, 0);
117 } else {
118 phpdbg_error("list", "type=\"nosource\" class=\"%s\"", "The source of the requested class (%s) cannot be found", ZSTR_VAL(ce->name));
119 }
120 } else {
121 phpdbg_error("list", "type=\"internalclass\" class=\"%s\"", "The class requested (%s) is not user defined", ZSTR_VAL(ce->name));
122 }
123 } else {
124 phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "The requested class (%s) could not be found", param->str);
125 }
126
127 return SUCCESS;
128 } /* }}} */
129
phpdbg_list_file(zend_string * filename,uint32_t count,int offset,uint32_t highlight)130 void phpdbg_list_file(zend_string *filename, uint32_t count, int offset, uint32_t highlight) /* {{{ */
131 {
132 uint32_t line, lastline;
133 phpdbg_file_source *data;
134
135 if (!(data = zend_hash_find_ptr(&PHPDBG_G(file_sources), filename))) {
136 phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file...");
137 return;
138 }
139
140 if (offset < 0) {
141 count += offset;
142 offset = 0;
143 }
144
145 lastline = offset + count;
146
147 if (lastline > data->lines) {
148 lastline = data->lines;
149 }
150
151 phpdbg_xml("<list %r file=\"%s\">", ZSTR_VAL(filename));
152
153 for (line = offset; line < lastline;) {
154 uint32_t linestart = data->line[line++];
155 uint32_t linelen = data->line[line] - linestart;
156 char *buffer = data->buf + linestart;
157
158 if (!highlight) {
159 phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
160 } else {
161 if (highlight != line) {
162 phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
163 } else {
164 phpdbg_write("line", "line=\"%u\" code=\"%.*s\" current=\"current\"", ">%05u: %.*s", line, linelen, buffer);
165 }
166 }
167
168 if (*(buffer + linelen - 1) != '\n' || !linelen) {
169 phpdbg_out("\n");
170 }
171 }
172
173 phpdbg_xml("</list>");
174 } /* }}} */
175
phpdbg_list_function(const zend_function * fbc)176 void phpdbg_list_function(const zend_function *fbc) /* {{{ */
177 {
178 const zend_op_array *ops;
179
180 if (fbc->type != ZEND_USER_FUNCTION) {
181 phpdbg_error("list", "type=\"internalfunction\" function=\"%s\"", "The function requested (%s) is not user defined", ZSTR_VAL(fbc->common.function_name));
182 return;
183 }
184
185 ops = (zend_op_array *) fbc;
186
187 phpdbg_list_file(ops->filename, ops->line_end - ops->line_start + 1, ops->line_start, 0);
188 } /* }}} */
189
phpdbg_list_function_byname(const char * str,size_t len)190 void phpdbg_list_function_byname(const char *str, size_t len) /* {{{ */
191 {
192 HashTable *func_table = EG(function_table);
193 zend_function* fbc;
194 char *func_name = (char*) str;
195 size_t func_name_len = len;
196
197 /* search active scope if begins with period */
198 if (func_name[0] == '.') {
199 zend_class_entry *scope = zend_get_executed_scope();
200 if (scope) {
201 func_name++;
202 func_name_len--;
203
204 func_table = &scope->function_table;
205 } else {
206 phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
207 return;
208 }
209 } else if (!EG(function_table)) {
210 phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
211 return;
212 } else {
213 func_table = EG(function_table);
214 }
215
216 /* use lowercase names, case insensitive */
217 func_name = zend_str_tolower_dup(func_name, func_name_len);
218
219 phpdbg_try_access {
220 if ((fbc = zend_hash_str_find_ptr(func_table, func_name, func_name_len))) {
221 phpdbg_list_function(fbc);
222 } else {
223 phpdbg_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name);
224 }
225 } phpdbg_catch_access {
226 phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name);
227 } phpdbg_end_try_access();
228
229 efree(func_name);
230 } /* }}} */
231
232 /* Note: do not free the original file handler, let original compile_file() or caller do that. Caller may rely on its value to check success */
phpdbg_compile_file(zend_file_handle * file,int type)233 zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
234 phpdbg_file_source data, *dataptr;
235 zend_op_array *ret;
236 uint32_t line;
237 char *bufptr, *endptr;
238 size_t len;
239
240 /* Copy file contents before calling original compile_file,
241 * as it may invalidate the file handle. */
242 if (zend_stream_fixup(file, &bufptr, &len) == FAILURE) {
243 if (type == ZEND_REQUIRE) {
244 zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file->filename);
245 } else {
246 zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file->filename);
247 }
248 return NULL;
249 }
250
251 data.buf = estrndup(bufptr, len);
252 data.len = len;
253
254 ret = PHPDBG_G(compile_file)(file, type);
255 if (ret == NULL) {
256 efree(data.buf);
257 return ret;
258 }
259
260 data.buf[data.len] = '\0';
261 data.line[0] = 0;
262 *(dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * data.len)) = data;
263
264 for (line = 0, bufptr = data.buf - 1, endptr = data.buf + data.len; ++bufptr < endptr;) {
265 if (*bufptr == '\n') {
266 dataptr->line[++line] = (uint32_t)(bufptr - data.buf) + 1;
267 }
268 }
269
270 dataptr->lines = ++line;
271 dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint32_t) * line);
272 dataptr->line[line] = endptr - data.buf;
273
274 zend_hash_del(&PHPDBG_G(file_sources), ret->filename);
275 zend_hash_add_ptr(&PHPDBG_G(file_sources), ret->filename, dataptr);
276 phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
277
278 return ret;
279 }
280
phpdbg_init_compile_file(zend_file_handle * file,int type)281 zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
282 char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
283 char resolved_path_buf[MAXPATHLEN];
284 zend_op_array *op_array;
285 phpdbg_file_source *dataptr;
286
287 if (VCWD_REALPATH(filename, resolved_path_buf)) {
288 filename = resolved_path_buf;
289
290 if (file->opened_path) {
291 zend_string_release(file->opened_path);
292 file->opened_path = zend_string_init(filename, strlen(filename), 0);
293 } else {
294 if (file->free_filename) {
295 efree((char *) file->filename);
296 }
297 file->free_filename = 0;
298 file->filename = filename;
299 }
300 }
301
302 op_array = PHPDBG_G(init_compile_file)(file, type);
303
304 if (op_array == NULL) {
305 return NULL;
306 }
307
308 dataptr = zend_hash_find_ptr(&PHPDBG_G(file_sources), op_array->filename);
309 ZEND_ASSERT(dataptr != NULL);
310
311 dataptr->op_array = *op_array;
312 if (dataptr->op_array.refcount) {
313 ++*dataptr->op_array.refcount;
314 }
315
316 return op_array;
317 }
318
phpdbg_compile_string(zend_string * source_string,const char * filename)319 zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *filename) {
320 zend_string *fake_name;
321 zend_op_array *op_array;
322 phpdbg_file_source *dataptr;
323 uint32_t line;
324 char *bufptr, *endptr;
325
326 if (PHPDBG_G(flags) & PHPDBG_IN_EVAL) {
327 return PHPDBG_G(compile_string)(source_string, filename);
328 }
329
330 dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * ZSTR_LEN(source_string));
331 dataptr->buf = estrndup(ZSTR_VAL(source_string), ZSTR_LEN(source_string));
332 dataptr->len = ZSTR_LEN(source_string);
333 dataptr->line[0] = 0;
334 for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
335 if (*bufptr == '\n') {
336 dataptr->line[++line] = (uint32_t)(bufptr - dataptr->buf) + 1;
337 }
338 }
339 dataptr->lines = ++line;
340 dataptr->line[line] = endptr - dataptr->buf;
341
342 op_array = PHPDBG_G(compile_string)(source_string, filename);
343
344 if (op_array == NULL) {
345 efree(dataptr->buf);
346 efree(dataptr);
347 return NULL;
348 }
349
350 fake_name = strpprintf(0, "%s%c%p", filename, 0, op_array->opcodes);
351
352 dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint32_t) * line);
353 zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);
354
355 zend_string_release(fake_name);
356
357 dataptr->op_array = *op_array;
358 if (dataptr->op_array.refcount) {
359 ++*dataptr->op_array.refcount;
360 }
361
362 return op_array;
363 }
364
phpdbg_init_list(void)365 void phpdbg_init_list(void) {
366 PHPDBG_G(compile_file) = zend_compile_file;
367 PHPDBG_G(compile_string) = zend_compile_string;
368 zend_compile_file = phpdbg_compile_file;
369 zend_compile_string = phpdbg_compile_string;
370 }
371
phpdbg_list_update(void)372 void phpdbg_list_update(void) {
373 PHPDBG_G(init_compile_file) = zend_compile_file;
374 zend_compile_file = phpdbg_init_compile_file;
375 }
376