xref: /PHP-7.0/sapi/phpdbg/phpdbg_list.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 <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,uint count,int offset,uint highlight)132 void phpdbg_list_file(zend_string *filename, uint count, int offset, uint highlight) /* {{{ */
133 {
134 	uint 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 		uint linestart = data->line[line++];
157 		uint 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 		if (EG(scope)) {
202 			func_name++;
203 			func_name_len--;
204 
205 			func_table = &EG(scope)->function_table;
206 		} else {
207 			phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
208 			return;
209 		}
210 	} else if (!EG(function_table)) {
211 		phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
212 		return;
213 	} else {
214 		func_table = EG(function_table);
215 	}
216 
217 	/* use lowercase names, case insensitive */
218 	func_name = zend_str_tolower_dup(func_name, func_name_len);
219 
220 	phpdbg_try_access {
221 		if ((fbc = zend_hash_str_find_ptr(func_table, func_name, func_name_len))) {
222 			phpdbg_list_function(fbc);
223 		} else {
224 			phpdbg_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name);
225 		}
226 	} phpdbg_catch_access {
227 		phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name);
228 	} phpdbg_end_try_access();
229 
230 	efree(func_name);
231 } /* }}} */
232 
233 /* 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)234 zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
235 	phpdbg_file_source data, *dataptr;
236 	zend_file_handle fake;
237 	zend_op_array *ret;
238 	char *filename;
239 	uint line;
240 	char *bufptr, *endptr;
241 
242 	if (zend_stream_fixup(file, &bufptr, &data.len) == FAILURE) {
243 		return PHPDBG_G(compile_file)(file, type);
244 	}
245 
246 	filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
247 
248 	data.buf = emalloc(data.len + ZEND_MMAP_AHEAD + 1);
249 	if (data.len > 0) {
250 		memcpy(data.buf, bufptr, data.len);
251 	}
252 	memset(data.buf + data.len, 0, ZEND_MMAP_AHEAD + 1);
253 	data.line[0] = 0;
254 
255 	memset(&fake, 0, sizeof(fake));
256 	fake.type = ZEND_HANDLE_MAPPED;
257 	fake.handle.stream.mmap.buf = data.buf;
258 	fake.handle.stream.mmap.len = data.len;
259 	fake.free_filename = 0;
260 	fake.filename = filename;
261 	fake.opened_path = file->opened_path;
262 
263 	*(dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint) * data.len)) = data;
264 
265 	for (line = 0, bufptr = data.buf - 1, endptr = data.buf + data.len; ++bufptr < endptr;) {
266 		if (*bufptr == '\n') {
267 			dataptr->line[++line] = (uint)(bufptr - data.buf) + 1;
268 		}
269 	}
270 	dataptr->lines = ++line;
271 	dataptr->line[line] = endptr - data.buf;
272 
273 	ret = PHPDBG_G(compile_file)(&fake, type);
274 
275 	if (ret == NULL) {
276 		efree(data.buf);
277 		efree(dataptr);
278 
279 		fake.opened_path = NULL;
280 		zend_file_handle_dtor(&fake);
281 
282 		return NULL;
283 	}
284 
285 	dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
286 	zend_hash_add_ptr(&PHPDBG_G(file_sources), ret->filename, dataptr);
287 	phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
288 
289 	fake.opened_path = NULL;
290 	zend_file_handle_dtor(&fake);
291 
292 	return ret;
293 }
294 
phpdbg_init_compile_file(zend_file_handle * file,int type)295 zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
296 	char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
297 	char resolved_path_buf[MAXPATHLEN];
298 	zend_op_array *op_array;
299 	phpdbg_file_source *dataptr;
300 
301 	if (VCWD_REALPATH(filename, resolved_path_buf)) {
302 		filename = resolved_path_buf;
303 
304 		if (file->opened_path) {
305 			zend_string_release(file->opened_path);
306 			file->opened_path = zend_string_init(filename, strlen(filename), 0);
307 		} else {
308 			if (file->free_filename) {
309 				efree((char *) file->filename);
310 			}
311 			file->free_filename = 0;
312 			file->filename = filename;
313 		}
314 	}
315 
316 	op_array = PHPDBG_G(init_compile_file)(file, type);
317 
318 	if (op_array == NULL) {
319 		return NULL;
320 	}
321 
322 	dataptr = zend_hash_find_ptr(&PHPDBG_G(file_sources), op_array->filename);
323 	ZEND_ASSERT(dataptr != NULL);
324 
325 	dataptr->op_array = *op_array;
326 	if (dataptr->op_array.refcount) {
327 		++*dataptr->op_array.refcount;
328 	}
329 
330 	return op_array;
331 }
332 
phpdbg_compile_string(zval * source_string,char * filename)333 zend_op_array *phpdbg_compile_string(zval *source_string, char *filename) {
334 	zend_string *fake_name;
335 	zend_op_array *op_array;
336 	phpdbg_file_source *dataptr;
337 	uint line;
338 	char *bufptr, *endptr;
339 
340 	if (PHPDBG_G(flags) & PHPDBG_IN_EVAL) {
341 		return PHPDBG_G(compile_string)(source_string, filename);
342 	}
343 
344 	dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint) * Z_STRLEN_P(source_string));
345 	dataptr->buf = estrndup(Z_STRVAL_P(source_string), Z_STRLEN_P(source_string));
346 	dataptr->len = Z_STRLEN_P(source_string);
347 	dataptr->line[0] = 0;
348 	for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
349 		if (*bufptr == '\n') {
350 			dataptr->line[++line] = (uint)(bufptr - dataptr->buf) + 1;
351 		}
352 	}
353 	dataptr->lines = ++line;
354 	dataptr->line[line] = endptr - dataptr->buf;
355 
356 	op_array = PHPDBG_G(compile_string)(source_string, filename);
357 
358 	if (op_array == NULL) {
359 		efree(dataptr->buf);
360 		efree(dataptr);
361 		return NULL;
362 	}
363 
364 	fake_name = strpprintf(0, "%s%c%p", filename, 0, op_array->opcodes);
365 
366 	dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
367 	zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);
368 
369 	zend_string_release(fake_name);
370 
371 	dataptr->op_array = *op_array;
372 	if (dataptr->op_array.refcount) {
373 		++*dataptr->op_array.refcount;
374 	}
375 
376 	return op_array;
377 }
378 
phpdbg_free_file_source(zval * zv)379 void phpdbg_free_file_source(zval *zv) {
380 	phpdbg_file_source *data = Z_PTR_P(zv);
381 
382 	if (data->buf) {
383 		efree(data->buf);
384 	}
385 
386 	destroy_op_array(&data->op_array);
387 
388 	efree(data);
389 }
390 
phpdbg_init_list(void)391 void phpdbg_init_list(void) {
392 	PHPDBG_G(compile_file) = zend_compile_file;
393 	PHPDBG_G(compile_string) = zend_compile_string;
394 	zend_hash_init(&PHPDBG_G(file_sources), 1, NULL, (dtor_func_t) phpdbg_free_file_source, 0);
395 	zend_compile_file = phpdbg_compile_file;
396 	zend_compile_string = phpdbg_compile_string;
397 }
398 
phpdbg_list_update(void)399 void phpdbg_list_update(void) {
400 	PHPDBG_G(init_compile_file) = zend_compile_file;
401 	zend_compile_file = phpdbg_init_compile_file;
402 }
403