summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_list.c
diff options
context:
space:
mode:
authorRasmus Lerdorf <rasmus@php.net>2014-10-26 16:58:45 -0700
committerRasmus Lerdorf <rasmus@php.net>2014-10-26 16:58:45 -0700
commit8d84b1f67adb71bdb2394b62faad04b22f6226f3 (patch)
tree0c1c1ed2aaef8eee971575eb5e336631295f4951 /sapi/phpdbg/phpdbg_list.c
parentfb85d0322d39d49f37e32df6f68c9769f2cce0e4 (diff)
parent09da8952d0434b53e740ffaca66a8051cd39cf93 (diff)
downloadphp-git-8d84b1f67adb71bdb2394b62faad04b22f6226f3.tar.gz
Merge branch 'master' of git.php.net:php-src
* 'master' of git.php.net:php-src: (240 commits) Do not execute anything after quit or clean command Fix last commit, and do not output unnecessary information Stabilize execution, always run destructors and extended file breakpoints Fix nullptr dereference in clean without exec context remove dodgy param parser, bring userland breakpoint api inline with PHP7 disable output buffering by default Add question to reset execution in run/exec/clean - Updated to version 2014.9 (2014i) actually remove this disable output buffering, better breakpoint api for userland, remove hand parsing of params Fix phpdbg output when outputting via php with active output handlers Fixed Closure::call() NEWS/UPGRADING Set engine back to initial state after fatal-ed ev command Fix eventual stack overflow after clean cmd Fix listing of files with no absolute path updated libmagic.patch in master updated libmagic.patch in 5.6 updated libmagic.patch in 5.5 NEWS NEWS ...
Diffstat (limited to 'sapi/phpdbg/phpdbg_list.c')
-rw-r--r--sapi/phpdbg/phpdbg_list.c244
1 files changed, 160 insertions, 84 deletions
diff --git a/sapi/phpdbg/phpdbg_list.c b/sapi/phpdbg/phpdbg_list.c
index eac17ba6a7..f201be9850 100644
--- a/sapi/phpdbg/phpdbg_list.c
+++ b/sapi/phpdbg/phpdbg_list.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 7 |
+ | PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
@@ -34,35 +34,37 @@
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
-#define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s) \
- PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[13])
+#define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s, flags) \
+ PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[13], flags)
const phpdbg_command_t phpdbg_list_commands[] = {
- PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l"),
- PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s"),
- PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m"),
- PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s"),
+ PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l", PHPDBG_ASYNC_SAFE),
+ PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s", PHPDBG_ASYNC_SAFE),
+ PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m", PHPDBG_ASYNC_SAFE),
+ PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s", PHPDBG_ASYNC_SAFE),
PHPDBG_END_COMMAND
};
PHPDBG_LIST(lines) /* {{{ */
{
if (!PHPDBG_G(exec) && !zend_is_executing(TSRMLS_C)) {
- phpdbg_error("Not executing, and execution context not set");
+ phpdbg_error("inactive", "type=\"execution\"", "Not executing, and execution context not set");
return SUCCESS;
}
switch (param->type) {
- case NUMERIC_PARAM:
- phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
- (param->num < 0 ? 1 - param->num : param->num),
- (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
- 0 TSRMLS_CC);
- break;
-
- case FILE_PARAM:
- phpdbg_list_file(param->file.name, param->file.line, 0, 0 TSRMLS_CC);
- break;
+ case NUMERIC_PARAM: {
+ const char *char_file = phpdbg_current_file(TSRMLS_C);
+ zend_string *file = zend_string_init(char_file, strlen(char_file), 0);
+ phpdbg_list_file(file, param->num < 0 ? 1 - param->num : param->num, (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C), 0 TSRMLS_CC);
+ efree(file);
+ } break;
+
+ case FILE_PARAM: {
+ zend_string *file = zend_string_init(param->file.name, strlen(param->file.name), 0);
+ phpdbg_list_file(file, param->file.line, 0, 0 TSRMLS_CC);
+ efree(file);
+ } break;
phpdbg_default_switch_case();
}
@@ -79,21 +81,21 @@ PHPDBG_LIST(func) /* {{{ */
PHPDBG_LIST(method) /* {{{ */
{
- zend_class_entry **ce;
+ zend_class_entry *ce;
- if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
+ if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
zend_function *function;
char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
- if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**) &function) == SUCCESS) {
+ if ((function = zend_hash_str_find_ptr(&ce->function_table, lcname, strlen(lcname)))) {
phpdbg_list_function(function TSRMLS_CC);
} else {
- phpdbg_error("Could not find %s::%s", param->method.class, param->method.name);
+ phpdbg_error("list", "type=\"notfound\" method=\"%s::%s\"", "Could not find %s::%s", param->method.class, param->method.name);
}
efree(lcname);
} else {
- phpdbg_error("Could not find the class %s", param->method.class);
+ phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "Could not find the class %s", param->method.class);
}
return SUCCESS;
@@ -101,82 +103,77 @@ PHPDBG_LIST(method) /* {{{ */
PHPDBG_LIST(class) /* {{{ */
{
- zend_class_entry **ce;
-
- if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
- if ((*ce)->type == ZEND_USER_CLASS) {
- if ((*ce)->info.user.filename) {
- phpdbg_list_file(
- (*ce)->info.user.filename,
- (*ce)->info.user.line_end - (*ce)->info.user.line_start + 1,
- (*ce)->info.user.line_start, 0 TSRMLS_CC
- );
+ zend_class_entry *ce;
+
+ if (phpdbg_safe_class_lookup(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
+ if (ce->type == ZEND_USER_CLASS) {
+ if (ce->info.user.filename) {
+ phpdbg_list_file(ce->info.user.filename, ce->info.user.line_end - ce->info.user.line_start + 1, ce->info.user.line_start, 0 TSRMLS_CC);
} else {
- phpdbg_error("The source of the requested class (%s) cannot be found", (*ce)->name);
+ phpdbg_error("list", "type=\"nosource\" class=\"%s\"", "The source of the requested class (%s) cannot be found", ce->name);
}
} else {
- phpdbg_error("The class requested (%s) is not user defined", (*ce)->name);
+ phpdbg_error("list", "type=\"internalclass\" class=\"%s\"", "The class requested (%s) is not user defined", ce->name);
}
} else {
- phpdbg_error("The requested class (%s) could not be found", param->str);
+ phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "The requested class (%s) could not be found", param->str);
}
return SUCCESS;
} /* }}} */
-void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
+void phpdbg_list_file(zend_string *filename, uint count, int offset, uint highlight TSRMLS_DC) /* {{{ */
{
- struct stat st;
- char *opened = NULL;
- char buffer[8096] = {0,};
- long line = 0;
-
- php_stream *stream = NULL;
-
- if (VCWD_STAT(filename, &st) == FAILURE) {
- phpdbg_error("Failed to stat file %s", filename);
- return;
+ uint line, lastline;
+ phpdbg_file_source *data;
+ char resolved_path_buf[MAXPATHLEN];
+ const char *abspath;
+
+ if (VCWD_REALPATH(filename->val, resolved_path_buf)) {
+ abspath = resolved_path_buf;
+ } else {
+ abspath = filename->val;
}
- stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
-
- if (!stream) {
- phpdbg_error("Failed to open file %s to list", filename);
+ if (!(data = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), abspath, strlen(abspath)))) {
+ phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file...");
return;
}
-
+
if (offset < 0) {
count += offset;
offset = 0;
}
-
- while (php_stream_gets(stream, buffer, sizeof(buffer)) != NULL) {
- long linelen = strlen(buffer);
- ++line;
-
- if (offset <= line) {
- if (!highlight) {
- phpdbg_write("%05ld: %s", line, buffer);
- } else {
- if (highlight != line) {
- phpdbg_write(" %05ld: %s", line, buffer);
- } else {
- phpdbg_write(">%05ld: %s", line, buffer);
- }
- }
+ lastline = offset + count;
+
+ if (lastline > data->lines) {
+ lastline = data->lines;
+ }
+
+ phpdbg_xml("<list %r file=\"%s\">", filename);
- if (buffer[linelen - 1] != '\n') {
- phpdbg_write("\n");
+ for (line = offset; line < lastline;) {
+ uint linestart = data->line[line++];
+ uint linelen = data->line[line] - linestart;
+ char *buffer = data->buf + linestart;
+
+ if (!highlight) {
+ phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
+ } else {
+ if (highlight != line) {
+ phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
+ } else {
+ phpdbg_write("line", "line=\"%u\" code=\"%.*s\" current=\"current\"", ">%05u: %.*s", line, linelen, buffer);
}
}
- if (count > 0 && count + offset - 1 < line) {
- break;
+ if (*(buffer + linelen - 1) != '\n' || !linelen) {
+ phpdbg_out("\n");
}
}
-
- php_stream_close(stream);
+
+ phpdbg_xml("</list>");
} /* }}} */
void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */
@@ -184,14 +181,13 @@ void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */
const zend_op_array *ops;
if (fbc->type != ZEND_USER_FUNCTION) {
- phpdbg_error("The function requested (%s) is not user defined", fbc->common.function_name);
+ phpdbg_error("list", "type=\"internalfunction\" function=\"%s\"", "The function requested (%s) is not user defined", fbc->common.function_name);
return;
}
- ops = (zend_op_array*)fbc;
+ ops = (zend_op_array *) fbc;
- phpdbg_list_file(ops->filename,
- ops->line_end - ops->line_start + 1, ops->line_start, 0 TSRMLS_CC);
+ phpdbg_list_file(ops->filename, ops->line_end - ops->line_start + 1, ops->line_start, 0 TSRMLS_CC);
} /* }}} */
void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ */
@@ -209,11 +205,11 @@ void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ *
func_table = &EG(scope)->function_table;
} else {
- phpdbg_error("No active class");
+ phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
return;
}
} else if (!EG(function_table)) {
- phpdbg_error("No function table loaded");
+ phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
return;
} else {
func_table = EG(function_table);
@@ -222,12 +218,92 @@ void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ *
/* use lowercase names, case insensitive */
func_name = zend_str_tolower_dup(func_name, func_name_len);
- if (zend_hash_find(func_table, func_name, func_name_len+1, (void**)&fbc) == SUCCESS) {
- phpdbg_list_function(fbc TSRMLS_CC);
- } else {
- phpdbg_error("Function %s not found", func_name);
- }
+ phpdbg_try_access {
+ if ((fbc = zend_hash_str_find_ptr(func_table, func_name, func_name_len))) {
+ phpdbg_list_function(fbc TSRMLS_CC);
+ } else {
+ phpdbg_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name);
+ }
+ } phpdbg_catch_access {
+ phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name);
+ } phpdbg_end_try_access();
efree(func_name);
} /* }}} */
+zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type TSRMLS_DC) {
+ phpdbg_file_source data, *dataptr;
+ zend_file_handle fake = {{0}};
+ zend_op_array *ret;
+ char *filename = (char *)(file->opened_path ? file->opened_path : file->filename);
+ uint line;
+ char *bufptr, *endptr;
+ char resolved_path_buf[MAXPATHLEN];
+
+ zend_stream_fixup(file, &data.buf, &data.len TSRMLS_CC);
+
+ data.filename = filename;
+ data.line[0] = 0;
+
+ if (file->handle.stream.mmap.old_closer) {
+ /* do not unmap */
+ file->handle.stream.closer = file->handle.stream.mmap.old_closer;
+ }
+
+#if HAVE_MMAP
+ if (file->handle.stream.mmap.map) {
+ data.map = file->handle.stream.mmap.map;
+ }
+#endif
+
+ fake.type = ZEND_HANDLE_MAPPED;
+ fake.handle.stream.mmap.buf = data.buf;
+ fake.handle.stream.mmap.len = data.len;
+ fake.free_filename = 0;
+ fake.opened_path = file->opened_path;
+ fake.filename = filename;
+ fake.opened_path = file->opened_path;
+
+ *(dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint) * data.len)) = data;
+ if (VCWD_REALPATH(filename, resolved_path_buf)) {
+ filename = resolved_path_buf;
+ }
+
+ for (line = 0, bufptr = data.buf - 1, endptr = data.buf + data.len; ++bufptr < endptr;) {
+ if (*bufptr == '\n') {
+ dataptr->line[++line] = (uint)(bufptr - data.buf) + 1;
+ }
+ }
+ dataptr->lines = ++line;
+ dataptr->line[line] = endptr - data.buf;
+ dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
+
+ zend_hash_str_add_ptr(&PHPDBG_G(file_sources), filename, strlen(filename), dataptr);
+ phpdbg_resolve_pending_file_break(filename TSRMLS_CC);
+
+ ret = PHPDBG_G(compile_file)(&fake, type TSRMLS_CC);
+
+ fake.opened_path = NULL;
+ zend_file_handle_dtor(&fake TSRMLS_CC);
+
+ return ret;
+}
+
+void phpdbg_free_file_source(phpdbg_file_source *data) {
+#if HAVE_MMAP
+ if (data->map) {
+ munmap(data->map, data->len + ZEND_MMAP_AHEAD);
+ } else
+#endif
+ if (data->buf) {
+ efree(data->buf);
+ }
+
+ efree(data);
+}
+
+void phpdbg_init_list(TSRMLS_D) {
+ PHPDBG_G(compile_file) = zend_compile_file;
+ zend_hash_init(&PHPDBG_G(file_sources), 1, NULL, (dtor_func_t) phpdbg_free_file_source, 0);
+ zend_compile_file = phpdbg_compile_file;
+}