summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_utils.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_utils.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_utils.c')
-rw-r--r--sapi/phpdbg/phpdbg_utils.c489
1 files changed, 352 insertions, 137 deletions
diff --git a/sapi/phpdbg/phpdbg_utils.c b/sapi/phpdbg/phpdbg_utils.c
index b57986b55c..bff971d4cf 100644
--- a/sapi/phpdbg/phpdbg_utils.c
+++ b/sapi/phpdbg/phpdbg_utils.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 7 |
+ | PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
@@ -18,19 +18,14 @@
+----------------------------------------------------------------------+
*/
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
#include "zend.h"
+
#include "php.h"
-#include "spprintf.h"
#include "phpdbg.h"
#include "phpdbg_opcode.h"
#include "phpdbg_utils.h"
-#ifdef _WIN32
-# include "win32/time.h"
-#elif defined(HAVE_SYS_IOCTL_H)
+#if defined(HAVE_SYS_IOCTL_H)
# include "sys/ioctl.h"
# ifndef GWINSZ_IN_SYS_IOCTL
# include <termios.h>
@@ -128,12 +123,12 @@ PHPDBG_API int phpdbg_is_class_method(const char *str, size_t len, char **class,
}
if (class != NULL) {
-
+
if (str[0] == '\\') {
str++;
len--;
}
-
+
*class = estrndup(str, sep - str);
(*class)[sep - str] = 0;
}
@@ -170,27 +165,25 @@ PHPDBG_API const char *phpdbg_current_file(TSRMLS_D) /* {{{ */
PHPDBG_API const zend_function *phpdbg_get_function(const char *fname, const char *cname TSRMLS_DC) /* {{{ */
{
zend_function *func = NULL;
- size_t fname_len = strlen(fname);
- char *lcname = zend_str_tolower_dup(fname, fname_len);
+ zend_string *lfname = zend_string_alloc(strlen(fname), 0);
+ memcpy(lfname->val, zend_str_tolower_dup(fname, lfname->len), lfname->len + 1);
if (cname) {
- zend_class_entry **ce;
- size_t cname_len = strlen(cname);
- char *lc_cname = zend_str_tolower_dup(cname, cname_len);
- int ret = zend_lookup_class(lc_cname, cname_len, &ce TSRMLS_CC);
+ zend_class_entry *ce;
+ zend_string *lcname = zend_string_alloc(strlen(cname), 0);
+ memcpy(lcname->val, zend_str_tolower_dup(cname, lcname->len), lcname->len + 1);
+ ce = zend_lookup_class(lcname TSRMLS_CC);
- efree(lc_cname);
+ efree(lcname);
- if (ret == SUCCESS) {
- zend_hash_find(&(*ce)->function_table, lcname, fname_len+1,
- (void**)&func);
+ if (ce) {
+ func = zend_hash_find_ptr(&ce->function_table, lfname);
}
} else {
- zend_hash_find(EG(function_table), lcname, fname_len+1,
- (void**)&func);
+ func = zend_hash_find_ptr(EG(function_table), lfname);
}
- efree(lcname);
+ efree(lfname);
return func;
} /* }}} */
@@ -224,103 +217,6 @@ PHPDBG_API char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{
} /* }}} */
-PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */
-{
- int rc = 0;
- char *buffer = NULL;
- va_list args;
-
- if (format != NULL && strlen(format) > 0L) {
- va_start(args, format);
- vspprintf(&buffer, 0, format, args);
- va_end(args);
- }
-
- /* TODO(anyone) colours */
-
- switch (type) {
- case P_ERROR:
- if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
- rc = fprintf(fp,
- "\033[%sm[%s]\033[0m\n",
- PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, buffer);
- } else {
- rc = fprintf(fp, "[%s]\n", buffer);
- }
- break;
-
- case P_NOTICE:
- if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) {
- rc = fprintf(fp,
- "\033[%sm[%s]\033[0m\n",
- PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, buffer);
- } else {
- rc = fprintf(fp, "[%s]\n", buffer);
- }
- break;
-
- case P_WRITELN: {
- if (buffer) {
- rc = fprintf(fp, "%s\n", buffer);
- } else {
- rc = fprintf(fp, "\n");
- }
- } break;
-
- case P_WRITE:
- if (buffer) {
- rc = fprintf(fp, "%s", buffer);
- }
- break;
-
- /* no formatting on logging output */
- case P_LOG:
- if (buffer) {
- struct timeval tp;
- if (gettimeofday(&tp, NULL) == SUCCESS) {
- rc = fprintf(fp, "[%ld %.8F]: %s\n", tp.tv_sec, tp.tv_usec / 1000000.00, buffer);
- } else {
- rc = FAILURE;
- }
- }
- break;
- }
-
- if (buffer) {
- efree(buffer);
- }
-
- return rc;
-} /* }}} */
-
-PHPDBG_API int phpdbg_rlog(FILE *fp, const char *fmt, ...) { /* {{{ */
- int rc = 0;
-
- va_list args;
- struct timeval tp;
-
- va_start(args, fmt);
- if (gettimeofday(&tp, NULL) == SUCCESS) {
- char friendly[100];
- char *format = NULL, *buffer = NULL;
- const time_t tt = tp.tv_sec;
-
- strftime(friendly, 100, "%a %b %d %T.%%04d %Y", localtime(&tt));
- asprintf(
- &buffer, friendly, tp.tv_usec/1000);
- asprintf(
- &format, "[%s]: %s\n", buffer, fmt);
- rc = vfprintf(
- fp, format, args);
-
- free(format);
- free(buffer);
- }
- va_end(args);
-
- return rc;
-} /* }}} */
-
PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_length TSRMLS_DC) /* {{{ */
{
const phpdbg_color_t *color = colors;
@@ -328,15 +224,13 @@ PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_
while (color && color->name) {
if (name_length == color->name_length &&
memcmp(name, color->name, name_length) == SUCCESS) {
- phpdbg_debug(
- "phpdbg_get_color(%s, %lu): %s", name, name_length, color->code);
+ phpdbg_debug("phpdbg_get_color(%s, %lu): %s", name, name_length, color->code);
return color;
}
++color;
}
- phpdbg_debug(
- "phpdbg_get_color(%s, %lu): failed", name, name_length);
+ phpdbg_debug("phpdbg_get_color(%s, %lu): failed", name, name_length);
return NULL;
} /* }}} */
@@ -362,7 +256,7 @@ PHPDBG_API const phpdbg_color_t* phpdbg_get_colors(TSRMLS_D) /* {{{ */
PHPDBG_API int phpdbg_get_element(const char *name, size_t len TSRMLS_DC) {
const phpdbg_element_t *element = elements;
-
+
while (element && element->name) {
if (len == element->name_length) {
if (strncasecmp(name, element->name, len) == SUCCESS) {
@@ -371,7 +265,7 @@ PHPDBG_API int phpdbg_get_element(const char *name, size_t len TSRMLS_DC) {
}
element++;
}
-
+
return PHPDBG_COLOR_INVALID;
}
@@ -419,18 +313,14 @@ PHPDBG_API const char *phpdbg_get_prompt(TSRMLS_D) /* {{{ */
} /* }}} */
int phpdbg_rebuild_symtable(TSRMLS_D) {
- if (!EG(active_op_array)) {
- phpdbg_error("No active op array!");
+ if (!EG(current_execute_data) || !EG(current_execute_data)->func) {
+ phpdbg_error("inactive", "type=\"op_array\"", "No active op array!");
return FAILURE;
}
- if (!EG(active_symbol_table)) {
- zend_rebuild_symbol_table(TSRMLS_C);
-
- if (!EG(active_symbol_table)) {
- phpdbg_error("No active symbol table!");
- return FAILURE;
- }
+ if (!zend_rebuild_symbol_table(TSRMLS_C)) {
+ phpdbg_error("inactive", "type=\"symbol_table\"", "No active symbol table!");
+ return FAILURE;
}
return SUCCESS;
@@ -438,13 +328,13 @@ int phpdbg_rebuild_symtable(TSRMLS_D) {
PHPDBG_API int phpdbg_get_terminal_width(TSRMLS_D) /* {{{ */
{
- int columns;
+ int columns;
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
-#elif defined(HAVE_SYS_IOCTL_H) && defined (TIOCGWINSZ)
+#elif defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
struct winsize w;
columns = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_col : 80;
@@ -453,3 +343,328 @@ PHPDBG_API int phpdbg_get_terminal_width(TSRMLS_D) /* {{{ */
#endif
return columns;
} /* }}} */
+
+PHPDBG_API void phpdbg_set_async_io(int fd) {
+#ifndef _WIN32
+ int flags;
+ fcntl(STDIN_FILENO, F_SETOWN, getpid());
+ flags = fcntl(STDIN_FILENO, F_GETFL);
+ fcntl(STDIN_FILENO, F_SETFL, flags | FASYNC);
+#endif
+}
+
+int phpdbg_safe_class_lookup(const char *name, int name_length, zend_class_entry **ce TSRMLS_DC) {
+ if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) {
+ char *lc_name, *lc_free;
+ int lc_length;
+
+ if (name == NULL || !name_length) {
+ return FAILURE;
+ }
+
+ lc_free = lc_name = emalloc(name_length + 1);
+ zend_str_tolower_copy(lc_name, name, name_length);
+ lc_length = name_length + 1;
+
+ if (lc_name[0] == '\\') {
+ lc_name += 1;
+ lc_length -= 1;
+ }
+
+ phpdbg_try_access {
+ *ce = zend_hash_str_find_ptr(EG(class_table), lc_name, lc_length);
+ } phpdbg_catch_access {
+ phpdbg_error("signalsegv", "class=\"%.*s\"", "Could not fetch class %.*s, invalid data source", name_length, name);
+ } phpdbg_end_try_access();
+
+ efree(lc_free);
+ } else {
+ zend_string *str_name = zend_string_init(name, name_length, 0);
+ *ce = zend_lookup_class(str_name TSRMLS_CC);
+ efree(str_name);
+ }
+
+ return ce ? SUCCESS : FAILURE;
+}
+
+char *phpdbg_get_property_key(char *key) {
+ if (*key != 0) {
+ return key;
+ }
+ return strchr(key + 1, 0) + 1;
+}
+
+static int phpdbg_parse_variable_arg_wrapper(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval *zv, phpdbg_parse_var_func callback TSRMLS_DC) {
+ return callback(name, len, keyname, keylen, parent, zv TSRMLS_CC);
+}
+
+PHPDBG_API int phpdbg_parse_variable(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_func callback, zend_bool silent TSRMLS_DC) {
+ return phpdbg_parse_variable_with_arg(input, len, parent, i, (phpdbg_parse_var_with_arg_func) phpdbg_parse_variable_arg_wrapper, silent, callback TSRMLS_CC);
+}
+
+PHPDBG_API int phpdbg_parse_variable_with_arg(char *input, size_t len, HashTable *parent, size_t i, phpdbg_parse_var_with_arg_func callback, zend_bool silent, void *arg TSRMLS_DC) {
+ int ret = FAILURE;
+ zend_bool new_index = 1;
+ char *last_index;
+ size_t index_len = 0;
+ zval *zv;
+
+ if (len < 2 || *input != '$') {
+ goto error;
+ }
+
+ while (i++ < len) {
+ if (i == len) {
+ new_index = 1;
+ } else {
+ switch (input[i]) {
+ case '[':
+ new_index = 1;
+ break;
+ case ']':
+ break;
+ case '>':
+ if (last_index[index_len - 1] == '-') {
+ new_index = 1;
+ index_len--;
+ }
+ break;
+
+ default:
+ if (new_index) {
+ last_index = input + i;
+ new_index = 0;
+ }
+ if (input[i - 1] == ']') {
+ goto error;
+ }
+ index_len++;
+ }
+ }
+
+ if (new_index && index_len == 0) {
+ zend_ulong numkey;
+ zend_string *strkey;
+ ZEND_HASH_FOREACH_KEY_PTR(parent, numkey, strkey, zv) {
+ while (Z_TYPE_P(zv) == IS_INDIRECT) {
+ zv = Z_INDIRECT_P(zv);
+ }
+
+ if (i == len || (i == len - 1 && input[len - 1] == ']')) {
+ char *key, *propkey;
+ size_t namelen, keylen;
+ char *name;
+ char *keyname = estrndup(last_index, index_len);
+ if (strkey) {
+ key = strkey->val;
+ keylen = strkey->len;
+ } else {
+ keylen = spprintf(&key, 0, "%llu", numkey);
+ }
+ propkey = phpdbg_get_property_key(key);
+ name = emalloc(i + keylen + 2);
+ namelen = sprintf(name, "%.*s%.*s%s", (int) i, input, keylen - (propkey - key), propkey, input[len - 1] == ']'?"]":"");
+ if (!strkey) {
+ efree(key);
+ }
+
+ ret = callback(name, namelen, keyname, index_len, parent, zv, arg TSRMLS_CC) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
+ } else if (Z_TYPE_P(zv) == IS_OBJECT) {
+ phpdbg_parse_variable_with_arg(input, len, Z_OBJPROP_P(zv), i, callback, silent, arg TSRMLS_CC);
+ } else if (Z_TYPE_P(zv) == IS_ARRAY) {
+ phpdbg_parse_variable_with_arg(input, len, Z_ARRVAL_P(zv), i, callback, silent, arg TSRMLS_CC);
+ } else {
+ /* Ignore silently */
+ }
+ } ZEND_HASH_FOREACH_END();
+ return ret;
+ } else if (new_index) {
+ char last_chr = last_index[index_len];
+ last_index[index_len] = 0;
+ if (!(zv = zend_symtable_str_find(parent, last_index, index_len))) {
+ if (!silent) {
+ phpdbg_error("variable", "type=\"undefined\" variable=\"%.*s\"", "%.*s is undefined", (int) i, input);
+ }
+ return FAILURE;
+ }
+ while (Z_TYPE_P(zv) == IS_INDIRECT) {
+ zv = Z_INDIRECT_P(zv);
+ }
+
+ last_index[index_len] = last_chr;
+ if (i == len) {
+ char *name = estrndup(input, len);
+ char *keyname = estrndup(last_index, index_len);
+
+ ret = callback(name, len, keyname, index_len, parent, zv, arg TSRMLS_CC) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE;
+ } else if (Z_TYPE_P(zv) == IS_OBJECT) {
+ parent = Z_OBJPROP_P(zv);
+ } else if (Z_TYPE_P(zv) == IS_ARRAY) {
+ parent = Z_ARRVAL_P(zv);
+ } else {
+ phpdbg_error("variable", "type=\"notiterable\" variable=\"%.*s\"", "%.*s is nor an array nor an object", (int) i, input);
+ return FAILURE;
+ }
+ index_len = 0;
+ }
+ }
+
+ return ret;
+ error:
+ phpdbg_error("variable", "type=\"invalidinput\"", "Malformed input");
+ return FAILURE;
+}
+
+int phpdbg_is_auto_global(char *name, int len TSRMLS_DC) {
+ int ret;
+ zend_string *str = zend_string_init(name, len, 0);
+ ret = zend_is_auto_global(str TSRMLS_CC);
+ efree(str);
+ return ret;
+}
+
+static int phpdbg_xml_array_element_dump(zval *zv, zend_string *key, zend_ulong num TSRMLS_DC) {
+ phpdbg_xml("<element");
+
+ phpdbg_try_access {
+ if (key) { /* string key */
+ phpdbg_xml(" name=\"%.*s\"", key->len, key->val);
+ } else { /* numeric key */
+ phpdbg_xml(" name=\"%ld\"", num);
+ }
+ } phpdbg_catch_access {
+ phpdbg_xml(" severity=\"error\" ></element>");
+ return 0;
+ } phpdbg_end_try_access();
+
+ phpdbg_xml(">");
+
+ phpdbg_xml_var_dump(zv TSRMLS_CC);
+
+ phpdbg_xml("</element>");
+
+ return 0;
+}
+
+static int phpdbg_xml_object_property_dump(zval *zv, zend_string *key, zend_ulong num TSRMLS_DC) {
+ phpdbg_xml("<property");
+
+ phpdbg_try_access {
+ if (key) { /* string key */
+ const char *prop_name, *class_name;
+ int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
+
+ if (class_name && unmangle == SUCCESS) {
+ phpdbg_xml(" name=\"%s\"", prop_name);
+ if (class_name[0] == '*') {
+ phpdbg_xml(" protection=\"protected\"");
+ } else {
+ phpdbg_xml(" class=\"%s\" protection=\"private\"", class_name);
+ }
+ } else {
+ phpdbg_xml(" name=\"%.*s\" protection=\"public\"", key->len, key->val);
+ }
+ } else { /* numeric key */
+ phpdbg_xml(" name=\"%ld\" protection=\"public\"", num);
+ }
+ } phpdbg_catch_access {
+ phpdbg_xml(" severity=\"error\" ></property>");
+ return 0;
+ } phpdbg_end_try_access();
+
+ phpdbg_xml(">");
+
+ phpdbg_xml_var_dump(zv TSRMLS_CC);
+
+ phpdbg_xml("</property>");
+
+ return 0;
+}
+
+#define COMMON (is_ref ? "&" : "")
+
+PHPDBG_API void phpdbg_xml_var_dump(zval *zv TSRMLS_DC) {
+ HashTable *myht;
+ zend_string *class_name, *key;
+ zend_ulong num;
+ zval *val;
+ int (*element_dump_func)(zval *zv, zend_string *key, zend_ulong num TSRMLS_DC);
+ zend_bool is_ref = 0;
+
+ int is_temp;
+
+ phpdbg_try_access {
+ is_ref = Z_ISREF_P(zv) && GC_REFCOUNT(Z_COUNTED_P(zv)) > 1;
+ ZVAL_DEREF(zv);
+
+ switch (Z_TYPE_P(zv)) {
+ case IS_TRUE:
+ phpdbg_xml("<bool refstatus=\"%s\" value=\"true\" />", COMMON);
+ break;
+ case IS_FALSE:
+ phpdbg_xml("<bool refstatus=\"%s\" value=\"false\" />", COMMON);
+ break;
+ case IS_NULL:
+ phpdbg_xml("<null refstatus=\"%s\" />", COMMON);
+ break;
+ case IS_LONG:
+ phpdbg_xml("<int refstatus=\"%s\" value=\"" ZEND_LONG_FMT "\" />", COMMON, Z_LVAL_P(zv));
+ break;
+ case IS_DOUBLE:
+ phpdbg_xml("<float refstatus=\"%s\" value=\"%.*G\" />", COMMON, (int) EG(precision), Z_DVAL_P(zv));
+ break;
+ case IS_STRING:
+ phpdbg_xml("<string refstatus=\"%s\" length=\"%d\" value=\"%.*s\" />", COMMON, Z_STRLEN_P(zv), Z_STRLEN_P(zv), Z_STRVAL_P(zv));
+ break;
+ case IS_ARRAY:
+ myht = Z_ARRVAL_P(zv);
+ if (ZEND_HASH_APPLY_PROTECTION(myht) && ++myht->u.v.nApplyCount > 1) {
+ phpdbg_xml("<recursion />");
+ --myht->u.v.nApplyCount;
+ break;
+ }
+ phpdbg_xml("<array refstatus=\"%s\" num=\"%d\">", COMMON, zend_hash_num_elements(myht));
+ element_dump_func = phpdbg_xml_array_element_dump;
+ is_temp = 0;
+ goto head_done;
+ case IS_OBJECT:
+ myht = Z_OBJDEBUG_P(zv, is_temp);
+ if (myht && ++myht->u.v.nApplyCount > 1) {
+ phpdbg_xml("<recursion />");
+ --myht->u.v.nApplyCount;
+ break;
+ }
+
+ class_name = Z_OBJ_HANDLER_P(zv, get_class_name)(Z_OBJ_P(zv) TSRMLS_CC);
+ phpdbg_xml("<object refstatus=\"%s\" class=\"%.*s\" id=\"%d\" num=\"%d\">", COMMON, class_name->len, class_name->val, Z_OBJ_HANDLE_P(zv), myht ? zend_hash_num_elements(myht) : 0);
+ zend_string_release(class_name);
+
+ element_dump_func = phpdbg_xml_object_property_dump;
+head_done:
+ if (myht) {
+ ZEND_HASH_FOREACH_KEY_VAL_IND(myht, num, key, val) {
+ element_dump_func(val, key, num TSRMLS_CC);
+ } ZEND_HASH_FOREACH_END();
+ zend_hash_apply_with_arguments(myht TSRMLS_CC, (apply_func_args_t) element_dump_func, 0);
+ --myht->u.v.nApplyCount;
+ if (is_temp) {
+ zend_hash_destroy(myht);
+ efree(myht);
+ }
+ }
+ if (Z_TYPE_P(zv) == IS_ARRAY) {
+ phpdbg_xml("</array>");
+ } else {
+ phpdbg_xml("</object>");
+ }
+ break;
+ case IS_RESOURCE: {
+ const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(zv) TSRMLS_CC);
+ phpdbg_xml("<resource refstatus=\"%s\" id=\"%pd\" type=\"%ld\" />", COMMON, Z_RES_P(zv)->handle, type_name ? type_name : "unknown");
+ break;
+ }
+ default:
+ break;
+ }
+ } phpdbg_end_try_access();
+}