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