com php-src: Merge branch 'master' into xml-protocol: phpdbg.c phpdbg.h phpdbg_cmd.c phpdbg_cmd.h phpdbg_frame.c phpd bg_info.c phpdbg_list.c
phpdbg_opcode.c phpdb g_print.c phpdbg_prompt.c phpdbg_set.c phpdbg _utils.c phpdbg_utils.h phpdbg_wait.c phpdbg _watch.c xml.md

From: Date: Tue, 07 Oct 2014 11:08:30 +0000
Subject: com php-src: Merge branch 'master' into xml-protocol: phpdbg.c phpdbg.h phpdbg_cmd.c phpdbg_cmd.h phpdbg_frame.c phpd bg_info.c phpdbg_list.c
phpdbg_opcode.c phpdb g_print.c phpdbg_prompt.c phpdbg_set.c phpdbg _utils.c phpdbg_utils.h phpdbg_wait.c phpdbg _watch.c xml.md
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-82570@lists.php.net to get a copy of this message
Commit: 8bc2881260d64ab801eb67ee3aa7691c80188f20 Author: Bob Weinand <bobwei9@hotmail.com> Tue, 7 Oct 2014 13:08:30 +0200 Parents: 2422c70c4f722c3afbdc5547116c6ecadfaf4954 6f584e22fe6ef71bd73be9c8287c8aec11d0d279 Branches: PHP-5.6 Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=8bc2881260d64ab801eb67ee3aa7691c80188f20 Log: Merge branch 'master' into xml-protocol Conflicts: phpdbg.c phpdbg.h phpdbg_cmd.c phpdbg_cmd.h phpdbg_frame.c phpdbg_info.c phpdbg_list.c phpdbg_print.c phpdbg_prompt.c phpdbg_utils.c phpdbg_utils.h phpdbg_watch.c Changed paths: MM phpdbg.c MM phpdbg.h MM phpdbg_cmd.c MM phpdbg_cmd.h MM phpdbg_frame.c MM phpdbg_info.c MM phpdbg_list.c MM phpdbg_opcode.c MM phpdbg_print.c MM phpdbg_prompt.c MM phpdbg_set.c MM phpdbg_utils.c MM phpdbg_utils.h AM phpdbg_wait.c MM phpdbg_watch.c MA xml.md

diff --cc phpdbg.c index 06eeb63,36e4bf8..3f2a209 --- a/phpdbg.c +++ b/phpdbg.c @@@ -69,14 -74,15 +74,19 @@@ static inline void php_phpdbg_globals_c pg->bp_count = 0; pg->flags = PHPDBG_DEFAULT_FLAGS; pg->oplog = NULL; - pg->io[PHPDBG_STDIN] = NULL; - pg->io[PHPDBG_STDOUT] = NULL; - pg->io[PHPDBG_STDERR] = NULL; + memset(pg->io, 0, sizeof(pg->io)); pg->frame.num = 0; + pg->sapi_name_ptr = NULL; + pg->socket_fd = -1; + pg->socket_server_fd = -1; + pg->req_id = 0; + pg->err_buf.active = 0; + pg->err_buf.type = 0; ++ + pg->input_buflen = 0; + pg->sigsafe_mem.mem = NULL; + pg->sigsegv_bailout = NULL; } /* }}} */ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ @@@ -521,39 -530,12 +534,42 @@@ static void php_sapi_phpdbg_register_va static inline int php_sapi_phpdbg_ub_write(const char *message, unsigned int length TSRMLS_DC) /* {{{ */ { + if (PHPDBG_G(socket_fd) != -1 && !(PHPDBG_G(flags) & PHPDBG_IS_INTERACTIVE)) { + send(PHPDBG_G(socket_fd), message, length, 0); + } - return phpdbg_write("%s", message); + return phpdbg_script(P_STDOUT, "%.*s", length, message); } /* }}} */ +/* beginning of struct, see main/streams/plain_wrapper.c line 111 */ +typedef struct { + FILE *file; + int fd; +} php_stdio_stream_data; + +static size_t phpdbg_stdiop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) { + php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract; + + while (data->fd >= 0) { + struct stat stat[3]; + memset(stat, 0, sizeof(stat)); + if (((fstat(fileno(stderr), &stat[2]) < 0) & (fstat(fileno(stdout), &stat[0]) < 0)) | (fstat(data->fd, &stat[1]) < 0)) { + break; + } + + if (stat[0].st_dev == stat[1].st_dev && stat[0].st_ino == stat[1].st_ino) { + phpdbg_script(P_STDOUT, "%.*s", (int) count, buf); + return count; + } + if (stat[2].st_dev == stat[1].st_dev && stat[2].st_ino == stat[1].st_ino) { + phpdbg_script(P_STDERR, "%.*s", (int) count, buf); + return count; + } + break; + } + + return PHPDBG_G(php_stdiop_write)(stream, buf, count TSRMLS_CC); +} + #if PHP_VERSION_ID >= 50700 static inline void php_sapi_phpdbg_flush(void *context TSRMLS_DC) /* {{{ */ { @@@ -1246,15 -1298,12 +1343,17 @@@ phpdbg_main sigaction(SIGBUS, &signal_struct, &PHPDBG_G(old_sigsegv_signal)); #endif + PHPDBG_G(sapi_name_ptr) = sapi_name; + + php_output_activate(TSRMLS_C); + php_output_deactivate(TSRMLS_C); + + php_output_activate(TSRMLS_C); + if (php_request_startup(TSRMLS_C) == SUCCESS) { int i; - - SG(request_info).argc = argc - php_optind + 1; + + SG(request_info).argc = argc - php_optind + 1; SG(request_info).argv = emalloc(SG(request_info).argc * sizeof(char *)); for (i = SG(request_info).argc; --i;) { SG(request_info).argv[i] = estrdup(argv[php_optind - 1 + i]); @@@ -1292,15 -1347,13 +1391,18 @@@ } #endif - PHPDBG_G(io)[PHPDBG_STDIN] = stdin; - PHPDBG_G(io)[PHPDBG_STDOUT] = stdout; - PHPDBG_G(io)[PHPDBG_STDERR] = stderr; + PHPDBG_G(io)[PHPDBG_STDIN].ptr = stdin; + PHPDBG_G(io)[PHPDBG_STDIN].fd = fileno(stdin); + PHPDBG_G(io)[PHPDBG_STDOUT].ptr = stdout; + PHPDBG_G(io)[PHPDBG_STDOUT].fd = fileno(stdout); + PHPDBG_G(io)[PHPDBG_STDERR].ptr = stderr; + PHPDBG_G(io)[PHPDBG_STDERR].fd = fileno(stderr); +#ifndef _WIN32 + PHPDBG_G(php_stdiop_write) = php_stream_stdio_ops.write; + php_stream_stdio_ops.write = phpdbg_stdiop_write; +#endif + if (exec) { /* set execution context */ PHPDBG_G(exec) = phpdbg_resolve_path(exec TSRMLS_CC); PHPDBG_G(exec_len) = strlen(PHPDBG_G(exec)); diff --cc phpdbg.h index b56d6d8,e079899..e567e22 --- a/phpdbg.h +++ b/phpdbg.h @@@ -72,14 -72,24 +72,25 @@@ # include <editline/readline.h> #endif - #include "phpdbg_lexer.h" - #include "phpdbg_cmd.h" - #include "phpdbg_bp.h" - #include "phpdbg_utils.h" - #include "phpdbg_btree.h" - #include "phpdbg_watch.h" + /* {{{ remote console headers */ + #ifndef _WIN32 + # include <sys/socket.h> + # include <sys/un.h> + # include <sys/select.h> + # include <sys/types.h> + #endif /* }}} */ - int phpdbg_do_parse(phpdbg_param_t *stack, char *input TSRMLS_DC); + /* {{{ strings */ + #define PHPDBG_NAME "phpdbg" + #define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */ + #define PHPDBG_URL "http://phpdbg.com" + #define PHPDBG_ISSUES "http://github.com/krakjoe/phpdbg/issues" + #define PHPDBG_VERSION "0.4.0" + #define PHPDBG_INIT_FILENAME ".phpdbginit" ++#define PHPDBG_DEFAULT_PROMPT "prompt>" + /* }}} */ + + #if !defined(PHPDBG_WEBDATA_TRANSFER_H) && !defined(PHPDBG_WEBHELPER_H) #ifdef ZTS # define PHPDBG_G(v) TSRMG(phpdbg_globals_id, zend_phpdbg_globals *, v) @@@ -133,12 -165,13 +166,14 @@@ int phpdbg_do_parse(phpdbg_param_t *sta #define PHPDBG_IS_BP_ENABLED (1<<26) #define PHPDBG_IS_REMOTE (1<<27) #define PHPDBG_IS_DISCONNECTED (1<<28) +#define PHPDBG_WRITE_XML (1<<29) -#define PHPDBG_SHOW_REFCOUNTS (1<<29) +#define PHPDBG_SHOW_REFCOUNTS (1<<30) + #define PHPDBG_IN_SIGNAL_HANDLER (1<<30) + #define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL|PHPDBG_IN_FINISH|PHPDBG_IN_LEAVE) - #define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP|PHPDBG_HAS_METHOD_OPLINE_BP|PHPDBG_HAS_FILE_OPLINE_BP) + #define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP|PHPDBG_HAS_METHOD_OPLINE_BP|PHPDBG_HAS_FILE_OPLINE_BP) #define PHPDBG_BP_MASK (PHPDBG_HAS_FILE_BP|PHPDBG_HAS_SYM_BP|PHPDBG_HAS_METHOD_BP|PHPDBG_HAS_OPLINE_BP|PHPDBG_HAS_COND_BP|PHPDBG_HAS_OPCODE_BP|PHPDBG_HAS_FUNCTION_OPLINE_BP|PHPDBG_HAS_METHOD_OPLINE_BP|PHPDBG_HAS_FILE_OPLINE_BP) #ifndef _WIN32 @@@ -192,23 -230,14 +232,29 @@@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg int bp_count; /* breakpoint count */ int vmret; /* return from last opcode handler execution */ + zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); + HashTable file_sources; + FILE *oplog; /* opline log */ - FILE *io[PHPDBG_IO_FDS]; /* io */ + struct { + FILE *ptr; + int fd; + } io[PHPDBG_IO_FDS]; /* io */ +#ifndef _WIN32 + size_t (*php_stdiop_write)(php_stream *, const char *, size_t TSRMLS_DC); +#endif + int in_script_xml; /* in <stream> output mode */ + struct { + zend_bool active; + int type; - FILE *fp; ++ int fd; + char *tag; + char *msg; + int msglen; + char *xml; + int xmllen; + } err_buf; /* error buffer */ + zend_ulong req_id; /* "request id" to keep track of commands */ char *prompt[2]; /* prompt */ const phpdbg_color_t *colors[PHPDBG_COLORS]; /* colors */ diff --cc phpdbg_cmd.c index 43f5523,8ada61a..abb41e4 --- a/phpdbg_cmd.c +++ b/phpdbg_cmd.c @@@ -636,7 -639,7 +636,7 @@@ PHPDBG_API int phpdbg_stack_verify(cons } /* {{{ */ - PHPDBG_API const phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top TSRMLS_DC) { -PHPDBG_API const phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top, char **why) { ++PHPDBG_API const phpdbg_command_t *phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top TSRMLS_DC) { const phpdbg_command_t *command = commands; phpdbg_param_t *name = *top; const phpdbg_command_t *matched[3] = {NULL, NULL, NULL}; @@@ -655,8 -658,8 +655,7 @@@ if (strncasecmp(command->name, name->str, name->len) == SUCCESS) { if (matches < 3) { /* only allow abbreviating commands that can be aliased */ -- if (((name->len != command->name_len) && command->alias) || -- (name->len == command->name_len)) { ++ if ((name->len != command->name_len && command->alias) || name->len == command->name_len) { matched[matches] = command; matches++; } @@@ -677,11 -681,9 +677,9 @@@ switch (matches) { case 0: if (parent) { - phpdbg_error("command", "type=\"notfound\" command=\"%s\" subcommand=\"%s\"", "The command \"%s %s\" could not be found", - parent->name, name->str); - spprintf(why, 0, "The command \"%s %s\" could not be found", parent->name, name->str); ++ phpdbg_error("command", "type=\"notfound\" command=\"%s\" subcommand=\"%s\"", "The command \"%s %s\" could not be found", parent->name, name->str); } else { - phpdbg_error("command", "type=\"notfound\" command=\"%s\"", "The command \"%s\" could not be found", - name->str); - spprintf(why, 0, "The command \"%s\" could not be found", name->str); ++ phpdbg_error("command", "type=\"notfound\" command=\"%s\"", "The command \"%s\" could not be found", name->str); } return parent; @@@ -713,9 -715,7 +711,8 @@@ it++; } - spprintf(why, 0, "The command \"%s\" is ambigious, matching %lu commands (%s)", name->str, matches, list); + /* ", " separated matches */ - phpdbg_error("command", "type=\"ambiguous\" command=\"%s\" matches=\"%lu\" matched=\"%s\"", "The command \"%s\" is ambigious, matching %lu commands (%s)", - name->str, matches, list); ++ phpdbg_error("command", "type=\"ambiguous\" command=\"%s\" matches=\"%lu\" matched=\"%s\"", "The command \"%s\" is ambigious, matching %lu commands (%s)", name->str, matches, list); efree(list); return NULL; @@@ -732,7 -732,7 +729,7 @@@ } /* }}} */ /* {{{ */ - PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack TSRMLS_DC) { -PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, char **why, zend_bool allow_async_unsafe TSRMLS_DC) { ++PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, zend_bool allow_async_unsafe TSRMLS_DC) { phpdbg_param_t *top = NULL; const phpdbg_command_t *handler = NULL; @@@ -746,7 -746,7 +743,7 @@@ return FAILURE; } -- top = (phpdbg_param_t*) stack->next; ++ top = (phpdbg_param_t *) stack->next; switch (top->type) { case EVAL_PARAM: @@@ -755,22 -753,29 +752,34 @@@ return PHPDBG_COMMAND_HANDLER(ev)(top TSRMLS_CC); case RUN_PARAM: - if (allow_async_unsafe) { - return PHPDBG_COMMAND_HANDLER(run)(top TSRMLS_CC); ++ if (!allow_async_unsafe) { ++ phpdbg_error("signalsegv", "command=\"run\"", "run command is disallowed during hard interrupt"); + } - spprintf(why, 0, "run command is disallowed during hard interrupt"); - return FAILURE; + phpdbg_activate_err_buf(0 TSRMLS_CC); + phpdbg_free_err_buf(TSRMLS_C); + return PHPDBG_COMMAND_HANDLER(run)(top TSRMLS_CC); case SHELL_PARAM: - if (allow_async_unsafe) { - return PHPDBG_COMMAND_HANDLER(sh)(top TSRMLS_CC); ++ if (!allow_async_unsafe) { ++ phpdbg_error("signalsegv", "command=\"sh\"", "sh command is disallowed during hard interrupt"); ++ return FAILURE; + } - spprintf(why, 0, "sh command is disallowed during hard interrupt"); - return FAILURE; + phpdbg_activate_err_buf(0 TSRMLS_CC); + phpdbg_free_err_buf(TSRMLS_C); + return PHPDBG_COMMAND_HANDLER(sh)(top TSRMLS_CC); case STR_PARAM: { - handler = phpdbg_stack_resolve(phpdbg_prompt_commands, NULL, &top, why); + handler = phpdbg_stack_resolve(phpdbg_prompt_commands, NULL, &top TSRMLS_CC); if (handler) { + if (!allow_async_unsafe && !(handler->flags & PHPDBG_ASYNC_SAFE)) { - spprintf(why, 0, "%s command is disallowed during hard interrupt", handler->name); ++ phpdbg_error("signalsegv", "command=\"%s\"", "%s command is disallowed during hard interrupt", handler->name); + return FAILURE; + } + - if (phpdbg_stack_verify(handler, &top, why TSRMLS_CC) == SUCCESS) { ++ phpdbg_activate_err_buf(0 TSRMLS_CC); ++ phpdbg_free_err_buf(TSRMLS_C); + if (phpdbg_stack_verify(handler, &top TSRMLS_CC) == SUCCESS) { - phpdbg_activate_err_buf(0 TSRMLS_CC); - phpdbg_free_err_buf(TSRMLS_C); return handler->handler(top TSRMLS_CC); } } @@@ -784,12 -789,9 +793,9 @@@ return SUCCESS; } /* }}} */ -PHPDBG_API char* phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */ +PHPDBG_API char *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */ { char *cmd = NULL; - #if !defined(HAVE_LIBREADLINE) && !defined(HAVE_LIBEDIT) - char buf[PHPDBG_MAX_CMD]; - #endif char *buffer = NULL; if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) { @@@ -806,9 -807,10 +811,10 @@@ disconnect return NULL; } - #if !defined(HAVE_LIBREADLINE) && !defined(HAVE_LIBEDIT) + #define USE_LIB_STAR (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)) + #if !USE_LIB_STAR if (!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) { - if (!phpdbg_write("%s", phpdbg_get_prompt(TSRMLS_C))) { + if (!phpdbg_out("%s", phpdbg_get_prompt(TSRMLS_C))) { goto disconnect; } PHPDBG_G(last_was_newline) = 1; diff --cc phpdbg_cmd.h index 4390560,45f1579..e164691 --- a/phpdbg_cmd.h +++ b/phpdbg_cmd.h @@@ -133,9 -136,9 +136,9 @@@ PHPDBG_API void phpdbg_destroy_input(ch * Stack Management */ PHPDBG_API void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param); - PHPDBG_API const phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top TSRMLS_DC); -PHPDBG_API const phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top, char **why); -PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack, char **why TSRMLS_DC); -PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, char **why, zend_bool allow_async_unsafe TSRMLS_DC); ++PHPDBG_API const phpdbg_command_t *phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top TSRMLS_DC); +PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack TSRMLS_DC); - PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack TSRMLS_DC); ++PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, zend_bool allow_async_unsafe TSRMLS_DC); PHPDBG_API void phpdbg_stack_free(phpdbg_param_t *stack); /* diff --cc phpdbg_frame.c index ea63122,62b3a66..429061e --- a/phpdbg_frame.c +++ b/phpdbg_frame.c @@@ -56,18 -56,23 +56,23 @@@ void phpdbg_switch_frame(int frame TSRM return; } - while (execute_data) { - if (i++ == frame) { - break; - } + phpdbg_try_access { + while (execute_data) { + if (i++ == frame) { + break; + } - do { - execute_data = execute_data->prev_execute_data; - } while (execute_data && execute_data->opline == NULL); - } + do { + execute_data = execute_data->prev_execute_data; + } while (execute_data && execute_data->opline == NULL); + } + } phpdbg_catch_access { - phpdbg_error("Couldn't switch frames, invalid data source"); ++ phpdbg_error("signalsegv", "", "Couldn't switch frames, invalid data source"); + return; + } phpdbg_end_try_access(); if (execute_data == NULL) { - phpdbg_error("No frame #%d", frame); + phpdbg_error("frame", "type=\"maxnum\" id=\"%d\"", "No frame #%d", frame); return; } @@@ -104,18 -109,13 +109,14 @@@ static void phpdbg_dump_prototype(zval { zval **funcname, **class, **type, **args, **argstmp; char is_class; + int has_args = FAILURE; - zend_hash_find(Z_ARRVAL_PP(tmp), "function", sizeof("function"), - (void **)&funcname); + zend_hash_find(Z_ARRVAL_PP(tmp), "function", sizeof("function"), (void **) &funcname); - if ((is_class = zend_hash_find(Z_ARRVAL_PP(tmp), - "object", sizeof("object"), (void **)&class)) == FAILURE) { - is_class = zend_hash_find(Z_ARRVAL_PP(tmp), "class", sizeof("class"), - (void **)&class); + if ((is_class = zend_hash_find(Z_ARRVAL_PP(tmp), "object", sizeof("object"), (void **) &class)) == FAILURE) { + is_class = zend_hash_find(Z_ARRVAL_PP(tmp), "class", sizeof("class"), (void **)&class); } else { - zend_get_object_classname(*class, (const char **)&Z_STRVAL_PP(class), - (uint32_t *)&Z_STRLEN_PP(class) TSRMLS_CC); + zend_get_object_classname(*class, (const char **) &Z_STRVAL_PP(class), (zend_uint *) &Z_STRLEN_PP(class) TSRMLS_CC); } if (is_class == SUCCESS) { @@@ -141,30 -128,31 +142,37 @@@ Z_STRVAL_PP(funcname) ); - if (zend_hash_find(Z_ARRVAL_PP(tmp), "args", sizeof("args"), (void **) &args) == SUCCESS) { + if (has_args) { HashPosition iterator; - const zend_function *func = phpdbg_get_function( - Z_STRVAL_PP(funcname), is_class == FAILURE ? NULL : Z_STRVAL_PP(class) TSRMLS_CC); - const zend_arg_info *arginfo = func ? func->common.arg_info : NULL; - int j = 0, m = func ? func->common.num_args : 0; - const zend_function *func; ++ const zend_function *func = NULL; + const zend_arg_info *arginfo = NULL; - int j = 0, m = func ? func->common.num_args : 0; ++ int j = 0, m; zend_bool is_variadic = 0; + phpdbg_try_access { + /* assuming no autoloader call is necessary, class should have been loaded if it's in backtrace ... */ + if ((func = phpdbg_get_function(Z_STRVAL_PP(funcname), is_class == FAILURE ? NULL : Z_STRVAL_PP(class) TSRMLS_CC))) { + arginfo = func->common.arg_info; + } + } phpdbg_end_try_access(); + ++ m = func ? func->common.num_args : 0; ++ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(args), &iterator); - while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(args), - (void **) &argstmp, &iterator) == SUCCESS) { + while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(args), (void **) &argstmp, &iterator) == SUCCESS) { if (j) { - phpdbg_write(", "); + phpdbg_out(", "); } + phpdbg_xml("<arg %r"); if (m && j < m) { #if PHP_VERSION_ID >= 50600 -- is_variadic = arginfo[j].is_variadic; ++ is_variadic = arginfo ? arginfo[j].is_variadic : 0; #endif - phpdbg_xml(" variadic=\"%s\" name=\"%s\">", is_variadic ? "variadic" : "", arginfo[j].name); - phpdbg_out("%s=%s", arginfo[j].name, is_variadic ? "[": ""); - phpdbg_write("%s=%s", - arginfo[j].name, is_variadic ? "[": ""); ++ phpdbg_xml(" variadic=\"%s\" name=\"%s\">", is_variadic ? "variadic" : "", arginfo ? arginfo[j].name : ""); ++ phpdbg_out("%s=%s", arginfo ? arginfo[j].name : "?", is_variadic ? "[": ""); + + } else { + phpdbg_xml(">"); } ++j; @@@ -191,24 -176,28 +199,28 @@@ void phpdbg_dump_backtrace(size_t num T int user_defined; if (limit < 0) { - phpdbg_error("Invalid backtrace size %d", limit); + phpdbg_error("backtrace", "type=\"minnum\"", "Invalid backtrace size %d", limit); ++ return; } - phpdbg_xml("<backtrace %r>"); + phpdbg_try_access { + zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit TSRMLS_CC); + } phpdbg_catch_access { - phpdbg_try_access { - zval_dtor(&zbacktrace); - } phpdbg_end_try_access(); - phpdbg_error("Couldn't fetch backtrace, invalid data source"); ++ phpdbg_error("signalsegv", "", "Couldn't fetch backtrace, invalid data source"); + return; + } phpdbg_end_try_access(); - zend_fetch_debug_backtrace( - &zbacktrace, 0, 0, limit TSRMLS_CC); ++ phpdbg_xml("<backtrace %r>"); + zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position); -- zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), (void**)&tmp, &position); ++ zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), (void **) &tmp, &position); while (1) { -- user_defined = zend_hash_find(Z_ARRVAL_PP(tmp), "file", sizeof("file"), (void **)&file); -- zend_hash_find(Z_ARRVAL_PP(tmp), "line", sizeof("line"), (void **)&line); ++ user_defined = zend_hash_find(Z_ARRVAL_PP(tmp), "file", sizeof("file"), (void **) &file); ++ zend_hash_find(Z_ARRVAL_PP(tmp), "line", sizeof("line"), (void **) &line); zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position); - if (zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), - (void**)&tmp, &position) == FAILURE) { - if (zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), (void**)&tmp, &position) == FAILURE) { - phpdbg_write("frame #%d: {main} at %s:%ld", i, Z_STRVAL_PP(file), Z_LVAL_PP(line)); ++ if (zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), (void **) &tmp, &position) == FAILURE) { + phpdbg_write("frame", "id=\"%d\" symbol=\"{main}\" file=\"%s\" line=\"%d\"", "frame #%d: {main} at %s:%ld", i, Z_STRVAL_PP(file), Z_LVAL_PP(line)); break; } @@@ -225,8 -212,6 +237,8 @@@ } } - phpdbg_writeln(EMPTY); + phpdbg_out("\n"); - zval_dtor(&zbacktrace); - + phpdbg_xml("</backtrace>"); ++ + zval_dtor(&zbacktrace); } /* }}} */ diff --cc phpdbg_info.c index 5204bc1,8d4bc53..9c17e42 --- a/phpdbg_info.c +++ b/phpdbg_info.c @@@ -63,15 -63,21 +63,21 @@@ PHPDBG_INFO(files) /* {{{ * HashPosition pos; char *fname; - phpdbg_notice("includedfilecount", "num=\"%d\"", "Included files: %d", - zend_hash_num_elements(&EG(included_files))); - - zend_hash_internal_pointer_reset_ex(&EG(included_files), &pos); - while (zend_hash_get_current_key_ex(&EG(included_files), &fname, - NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) { - phpdbg_writeln("includedfile", "name=\"%s\"", "File: %s", fname); - zend_hash_move_forward_ex(&EG(included_files), &pos); - } + phpdbg_try_access { - phpdbg_notice("Included files: %d", zend_hash_num_elements(&EG(included_files))); ++ phpdbg_notice("includedfilecount", "num=\"%d\"", "Included files: %d", zend_hash_num_elements(&EG(included_files))); + } phpdbg_catch_access { - phpdbg_error("Could not fetch included file count, invalid data source"); ++ phpdbg_error("signalsegv", "", "Could not fetch included file count, invalid data source"); + } phpdbg_end_try_access(); + + phpdbg_try_access { + zend_hash_internal_pointer_reset_ex(&EG(included_files), &pos); + while (zend_hash_get_current_key_ex(&EG(included_files), &fname, NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) { - phpdbg_writeln("File: %s", fname); ++ phpdbg_writeln("includedfile", "name=\"%s\"", "File: %s", fname); + zend_hash_move_forward_ex(&EG(included_files), &pos); + } + } phpdbg_catch_access { - phpdbg_error("Could not fetch file name, invalid data source, aborting included file listing"); ++ phpdbg_error("signalsegv", "", "Could not fetch file name, invalid data source, aborting included file listing"); + } phpdbg_end_try_access(); return SUCCESS; } /* }}} */ @@@ -79,17 -85,24 +85,24 @@@ PHPDBG_INFO(error) /* {{{ */ { if (PG(last_error_message)) { - phpdbg_writeln("lasterror", "error=\"%s\" file=\"%s\" line=\"%d\"", "Last error: %s at %s line %d", - PG(last_error_message), PG(last_error_file), PG(last_error_lineno)); + phpdbg_try_access { - phpdbg_writeln("Last error: %s at %s line %d", PG(last_error_message), PG(last_error_file), PG(last_error_lineno)); ++ phpdbg_writeln("lasterror", "error=\"%s\" file=\"%s\" line=\"%d\"", "Last error: %s at %s line %d", PG(last_error_message), PG(last_error_file), PG(last_error_lineno)); + } phpdbg_catch_access { - phpdbg_notice("No error found!"); ++ phpdbg_notice("lasterror", "error=\"\"", "No error found!"); + } phpdbg_end_try_access(); } else { - phpdbg_notice("No error found!"); + phpdbg_notice("lasterror", "error=\"\"", "No error found!"); } return SUCCESS; } /* }}} */ static int phpdbg_arm_auto_global(zend_auto_global *auto_global TSRMLS_DC) { if (auto_global->armed) { - auto_global->armed = auto_global->auto_global_callback(auto_global->name, auto_global->name_len TSRMLS_CC); + if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) { - phpdbg_notice("Cannot show information about superglobal variable %.*s", auto_global->name_len, auto_global->name); ++ phpdbg_notice("variableinfo", "unreachable=\"%.*s\"", "Cannot show information about superglobal variable %.*s", auto_global->name_len, auto_global->name); + } else { + auto_global->armed = auto_global->auto_global_callback(auto_global->name, auto_global->name_len TSRMLS_CC); + } } return 0; @@@ -125,18 -139,21 +139,21 @@@ static int phpdbg_print_symbols(zend_bo zend_hash_init(&vars, 8, NULL, NULL, 0); - zend_hash_internal_pointer_reset_ex(symtable, &pos); - while (zend_hash_get_current_key_ex(symtable, &var, - NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) { - zend_hash_get_current_data_ex(symtable, (void **)&data, &pos); - if (zend_is_auto_global(var, strlen(var) TSRMLS_CC) ^ !show_globals) { - zend_hash_update(&vars, var, strlen(var)+1, (void**)data, sizeof(zval*), NULL); + phpdbg_try_access { + zend_hash_internal_pointer_reset_ex(symtable, &pos); + while (zend_hash_get_current_key_ex(symtable, &var, NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) { + zend_hash_get_current_data_ex(symtable, (void **)&data, &pos); + if (zend_is_auto_global(var, strlen(var) TSRMLS_CC) ^ !show_globals) { + zend_hash_update(&vars, var, strlen(var)+1, (void**)data, sizeof(zval*), NULL); + } + zend_hash_move_forward_ex(symtable, &pos); } - zend_hash_move_forward_ex(symtable, &pos); - } + } phpdbg_catch_access { - phpdbg_error("Cannot fetch all data from the symbol table, invalid data source"); ++ phpdbg_error("signalsegv", "", "Cannot fetch all data from the symbol table, invalid data source"); + } phpdbg_end_try_access(); if (show_globals) { - phpdbg_notice("Superglobal variables (%d)", zend_hash_num_elements(&vars)); + phpdbg_notice("variableinfo", "num=\"%d\"", "Superglobal variables (%d)", zend_hash_num_elements(&vars)); } else { zend_op_array *ops = EG(active_op_array); @@@ -164,20 -186,46 +182,31 @@@ zend_hash_get_current_key_ex(&vars, &var, NULL, NULL, 0, &pos); - if (*data) { - phpdbg_writeln("variable", "address=\"%p\" refcount=\"%d\" type=\"%s\"", "%p\t%d\t(%s)", *data, Z_REFCOUNT_PP(data), zend_zval_type_name(*data)); - - if (Z_TYPE_PP(data) == IS_RESOURCE) { - int type; - - phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" type=\"%s\"", "%s$%s\n|-------(typeof)------> (%s)\n", Z_ISREF_PP(data) ? "&": "", var, zend_list_find(Z_RESVAL_PP(data), &type) ? zend_rsrc_list_get_rsrc_type(type TSRMLS_CC) : "unknown"); - } else if (Z_TYPE_PP(data) == IS_OBJECT) { - phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" instanceof=\"%s\"", "%s$%s\n|-----(instanceof)----> (%s)\n", Z_ISREF_PP(data) ? "&": "", var, Z_OBJCE_PP(data)->name); - } else { - phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\"", "%s$%s", Z_ISREF_PP(data) ? "&": "", var); + phpdbg_try_access { + if (!(invalid_data = !*data)) { - phpdbg_write("%p\t%d\t", *data, Z_REFCOUNT_PP(data)); - - switch (Z_TYPE_PP(data)) { - case IS_STRING: phpdbg_write("(string)\t"); break; - case IS_LONG: phpdbg_write("(integer)\t"); break; - case IS_DOUBLE: phpdbg_write("(float)\t"); break; - case IS_RESOURCE: phpdbg_write("(resource)\t"); break; - case IS_ARRAY: phpdbg_write("(array)\t"); break; - case IS_OBJECT: phpdbg_write("(object)\t"); break; - case IS_NULL: phpdbg_write("(null)\t"); break; - } - - phpdbg_writeln("%s$%s", Z_ISREF_PP(data) ? "&": "", var); ++ phpdbg_writeln("variable", "address=\"%p\" refcount=\"%d\" type=\"%s\"", "%p\t%d\t(%s)", *data, Z_REFCOUNT_PP(data), zend_zval_type_name(*data)); + + if (Z_TYPE_PP(data) == IS_RESOURCE) { - int type; - + phpdbg_try_access { - if (zend_list_find(Z_RESVAL_PP(data), &type)) { - phpdbg_writeln("|-------(typeof)------> (%s)", zend_rsrc_list_get_rsrc_type(type TSRMLS_CC)); - } else { - phpdbg_writeln("|-------(typeof)------> (unknown)"); - } ++ int type; ++ phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" type=\"%s\"", "%s$%s\n|-------(typeof)------> (%s)\n", Z_ISREF_PP(data) ? "&": "", var, zend_list_find(Z_RESVAL_PP(data), &type) ? zend_rsrc_list_get_rsrc_type(type TSRMLS_CC) : "unknown"); + } phpdbg_catch_access { - phpdbg_writeln("|-------(typeof)------> (unknown)"); ++ phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" type=\"unknown\"", "%s$%s\n|-------(typeof)------> (unknown)\n", Z_ISREF_PP(data) ? "&": "", var); + } phpdbg_end_try_access(); + } else if (Z_TYPE_PP(data) == IS_OBJECT) { + phpdbg_try_access { - phpdbg_writeln("|-----(instanceof)----> (%s)", Z_OBJCE_PP(data)->name); ++ phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" instanceof=\"%s\"", "%s$%s\n|-----(instanceof)----> (%s)\n", Z_ISREF_PP(data) ? "&": "", var, Z_OBJCE_PP(data)->name); + } phpdbg_catch_access { - phpdbg_writeln("|-----(instanceof)----> (unknown)"); ++ phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\" instanceof=\"unknown\"", "%s$%s\n|-----(instanceof)----> (unknown)\n", Z_ISREF_PP(data) ? "&": "", var); + } phpdbg_end_try_access(); ++ } else { ++ phpdbg_writeln("variabledetails", "refstatus=\"%s\" name=\"%s\"", "%s$%s", Z_ISREF_PP(data) ? "&": "", var); + } } - } else { - phpdbg_writeln("variable", "address=\"\" type=\"unknown\" name=\"%s\"", "n/a\tn/a\tn/a\t$%s", var); + } phpdbg_end_try_access(); + + if (invalid_data) { - phpdbg_writeln("n/a\tn/a\tn/a\t$%s", var); ++ phpdbg_writeln("variabledetails", "name=\"%s\"", "n/a\tn/a\tn/a\t$%s", var); } } } @@@ -219,10 -272,10 +249,9 @@@ PHPDBG_INFO(literal) /* {{{ * while (literal < ops->last_literal) { if (Z_TYPE(ops->literals[literal].constant) != IS_NULL) { - phpdbg_write("|-------- C%u -------> [", literal); + phpdbg_write("literal", "id=\"%u\"", "|-------- C%u -------> [", literal); - zend_print_zval( - &ops->literals[literal].constant, 0); + zend_print_zval(&ops->literals[literal].constant, 0); - phpdbg_write("]"); - phpdbg_writeln(EMPTY); + phpdbg_out("]\n"); } literal++; } @@@ -235,16 -288,33 +264,33 @@@ PHPDBG_INFO(memory) /* {{{ */ { - if (is_zend_mm(TSRMLS_C)) { + size_t used, real, peak_used, peak_real; + zend_mm_heap *heap; + zend_bool is_mm; + + if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) { + heap = zend_mm_set_heap(phpdbg_original_heap_sigsafe_mem(TSRMLS_C) TSRMLS_CC); + } + if ((is_mm = is_zend_mm(TSRMLS_C))) { + used = zend_memory_usage(0 TSRMLS_CC); + real = zend_memory_usage(1 TSRMLS_CC); + peak_used = zend_memory_peak_usage(0 TSRMLS_CC); + peak_real = zend_memory_peak_usage(1 TSRMLS_CC); + } + if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) { + zend_mm_set_heap(heap TSRMLS_CC); + } + + if (is_mm) { - phpdbg_notice("Memory Manager Information"); - phpdbg_notice("Current"); - phpdbg_writeln("|-------> Used:\t%.3f kB", (float) (used / 1024)); - phpdbg_writeln("|-------> Real:\t%.3f kB", (float) (real / 1024)); - phpdbg_notice("Peak"); - phpdbg_writeln("|-------> Used:\t%.3f kB", (float) (peak_used / 1024)); - phpdbg_writeln("|-------> Real:\t%.3f kB", (float) (peak_real / 1024)); + phpdbg_notice("meminfo", "", "Memory Manager Information"); + phpdbg_notice("current", "", "Current"); - phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (zend_memory_usage(0 TSRMLS_CC)/1024)); - phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (zend_memory_usage(1 TSRMLS_CC)/1024)); ++ phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (used / 1024)); ++ phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (real / 1024)); + phpdbg_notice("peak", "", "Peak"); - phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (zend_memory_peak_usage(0 TSRMLS_CC)/1024)); - phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (zend_memory_peak_usage(1 TSRMLS_CC)/1024)); ++ phpdbg_writeln("used", "mem=\"%.3f\"", "|-------> Used:\t%.3f kB", (float) (peak_used / 1024)); ++ phpdbg_writeln("real", "mem=\"%.3f\"", "|-------> Real:\t%.3f kB", (float) (peak_real / 1024)); } else { - phpdbg_error("Memory Manager Disabled!"); + phpdbg_error("inactive", "type=\"memory_manager\"", "Memory Manager Disabled!"); } return SUCCESS; } /* }}} */ @@@ -270,31 -341,35 +316,35 @@@ PHPDBG_INFO(classes) /* {{{ * zend_hash_init(&classes, 8, NULL, NULL, 0); - for (zend_hash_internal_pointer_reset_ex(EG(class_table), &position); - zend_hash_get_current_data_ex(EG(class_table), (void**)&ce, &position) == SUCCESS; - zend_hash_move_forward_ex(EG(class_table), &position)) { - - if ((*ce)->type == ZEND_USER_CLASS) { - zend_hash_next_index_insert(&classes, ce, sizeof(ce), NULL); + phpdbg_try_access { + for (zend_hash_internal_pointer_reset_ex(EG(class_table), &position); + zend_hash_get_current_data_ex(EG(class_table), (void**)&ce, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(class_table), &position)) { + if ((*ce)->type == ZEND_USER_CLASS) { + zend_hash_next_index_insert(&classes, ce, sizeof(ce), NULL); + } } - } + } phpdbg_catch_access { - phpdbg_notice("Not all classes could be fetched, possibly invalid data source"); ++ phpdbg_notice("signalsegv", "", "Not all classes could be fetched, possibly invalid data source"); + } phpdbg_end_try_access(); - phpdbg_notice("User Classes (%d)", zend_hash_num_elements(&classes)); + phpdbg_notice("classinfo", "num=\"%d\"", "User Classes (%d)", zend_hash_num_elements(&classes)); + /* once added, assume that classes are stable... until shutdown. */ for (zend_hash_internal_pointer_reset_ex(&classes, &position); -- zend_hash_get_current_data_ex(&classes, (void**)&ce, &position) == SUCCESS; -- zend_hash_move_forward_ex(&classes, &position)) { ++ zend_hash_get_current_data_ex(&classes, (void**)&ce, &position) == SUCCESS; ++ zend_hash_move_forward_ex(&classes, &position)) { phpdbg_print_class_name(ce TSRMLS_CC); - phpdbg_writeln(EMPTY); if ((*ce)->parent) { + phpdbg_xml("<parents>"); zend_class_entry *pce = (*ce)->parent; do { - phpdbg_write("|-------- "); + phpdbg_out("|-------- "); phpdbg_print_class_name(&pce TSRMLS_CC); - phpdbg_writeln(EMPTY); } while ((pce = pce->parent)); + phpdbg_xml("</parents>"); } if ((*ce)->info.user.filename) { @@@ -319,17 -396,20 +369,19 @@@ PHPDBG_INFO(funcs) /* {{{ * zend_hash_init(&functions, 8, NULL, NULL, 0); - for (zend_hash_internal_pointer_reset_ex(EG(function_table), &position); - zend_hash_get_current_data_ex(EG(function_table), (void**)&zf, &position) == SUCCESS; - zend_hash_move_forward_ex(EG(function_table), &position)) { - - if (zf->type == ZEND_USER_FUNCTION) { - zend_hash_next_index_insert( - &functions, (void**) &zf, sizeof(zend_function), NULL); + phpdbg_try_access { + for (zend_hash_internal_pointer_reset_ex(EG(function_table), &position); + zend_hash_get_current_data_ex(EG(function_table), (void**)&zf, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(function_table), &position)) { + if (zf->type == ZEND_USER_FUNCTION) { + zend_hash_next_index_insert(&functions, (void**) &zf, sizeof(zend_function), NULL); + } } - } + } phpdbg_catch_access { - phpdbg_notice("Not all functions could be fetched, possibly invalid data source"); ++ phpdbg_notice("signalsegv", "", "Not all functions could be fetched, possibly invalid data source"); + } phpdbg_end_try_access(); - phpdbg_notice("User Functions (%d)", - zend_hash_num_elements(&functions)); + phpdbg_notice("functioninfo", "num=\"%d\"", "User Functions (%d)", zend_hash_num_elements(&functions)); for (zend_hash_internal_pointer_reset_ex(&functions, &position); zend_hash_get_current_data_ex(&functions, (void**)&pzf, &position) == SUCCESS; diff --cc phpdbg_list.c index 17a125f,6a53b8d..647a290 --- a/phpdbg_list.c +++ b/phpdbg_list.c @@@ -124,25 -124,13 +124,14 @@@ PHPDBG_LIST(class) /* {{{ * return SUCCESS; } /* }}} */ - void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */ + void phpdbg_list_file(const char *filename, uint count, uint offset, uint highlight TSRMLS_DC) /* {{{ */ { - struct stat st; - char *opened = NULL; - char buffer[8096] = {0,}; - long line = 0; + uint line, lastline; + phpdbg_file_source **data; - php_stream *stream = NULL; - - if (VCWD_STAT(filename, &st) == FAILURE) { - phpdbg_error("list", "type=\"statfailure\"", "Failed to stat file %s", filename); - return; - } - - stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened); - - if (!stream) { - phpdbg_error("list", "type=\"openfailure\"", "Failed to open file %s to list", filename); + if (zend_hash_find(&PHPDBG_G(file_sources), filename, strlen(filename), (void **) &data) == FAILURE) { - phpdbg_error("Could not find information about included file..."); ++ phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file..."); + return; } if (offset < 0) { @@@ -150,37 -138,27 +139,35 @@@ offset = 0; } - phpdbg_xml("<list file=\"%s\">", filename); + lastline = offset + count; - while (php_stream_gets(stream, buffer, sizeof(buffer)) != NULL) { - long linelen = strlen(buffer); + if (lastline > (*data)->lines) { + lastline = (*data)->lines; + } - ++line; ++ phpdbg_xml("<list %r file=\"%s\">", filename); + - if (offset <= line) { - if (!highlight) { - phpdbg_write("line", "line=\"%d\" code=\"%s\"", " %05ld: %s", line, buffer); - } else { - if (highlight != line) { - phpdbg_write("line", "line=\"%ld\" code=\"%s\"", " %05ld: %s", line, buffer); - } else { - phpdbg_write("line", "line=\"%ld\" code=\"%s\" current=\"current\"", ">%05ld: %s", line, buffer); - } - } + for (line = offset; line < lastline;) { + uint linestart = (*data)->line[line++]; + uint linelen = (*data)->line[line] - linestart; + char *buffer = (*data)->buf + linestart; - if (buffer[linelen - 1] != '\n') { - phpdbg_out("\n"); + if (!highlight) { - phpdbg_write("%05u: %.*s", line, linelen, buffer); ++ phpdbg_write("line", "line=\"%u\" code=\"%s\"", " %05u: %s", line, buffer); + } else { + if (highlight != line) { - phpdbg_write(" %05u: %.*s", line, linelen, buffer); ++ phpdbg_write("line", "line=\"%u\" code=\"%s\"", " %05u: %s", line, buffer); + } else { - phpdbg_write(">%05u: %.*s", line, linelen, buffer); ++ phpdbg_write("line", "line=\"%u\" code=\"%s\" current=\"current\"", ">%05u: %s", line, buffer); } } + - if (count > 0 && count + offset - 1 < line) { - break; ++ if (*(buffer + linelen - 1) != '\n') { ++ phpdbg_out("\n"); + } } + + phpdbg_xml("</list>"); - - php_stream_close(stream); } /* }}} */ void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */ @@@ -226,11 -203,15 +212,15 @@@ void phpdbg_list_function_byname(const /* 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("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name); - } + phpdbg_try_access { + 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_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name); + } + } phpdbg_catch_access { - phpdbg_error("Could not list function %s, invalid data source", func_name); ++ phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name); + } phpdbg_end_try_access(); efree(func_name); } /* }}} */ diff --cc phpdbg_print.c index b7ec4c5,594fc9a..799734c --- a/phpdbg_print.c +++ b/phpdbg_print.c @@@ -155,8 -154,8 +155,8 @@@ PHPDBG_PRINT(class) /* {{{ * { zend_class_entry **ce; - if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) { + if (phpdbg_safe_class_lookup(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) { - phpdbg_notice("%s %s: %s", + phpdbg_notice("printinfo", "type=\"%s\" flag=\"%s\" class=\"%s\" num=\"%d\"", "%s %s: %s (%d methods)", ((*ce)->type == ZEND_USER_CLASS) ? "User" : "Internal", ((*ce)->ce_flags & ZEND_ACC_INTERFACE) ? @@@ -240,19 -234,22 +240,23 @@@ PHPDBG_PRINT(func) /* {{{ * func_table = EG(function_table); } - lcname = zend_str_tolower_dup(func_name, func_name_len); + lcname = zend_str_tolower_dup(func_name, func_name_len); - if (zend_hash_find(func_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) { - phpdbg_notice("printinfo", "type=\"%s\" flags=\"%s\" symbol=\"%s\" num=\"%d\"", "%s %s %s (%d ops)", - (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal", - (fbc->common.scope) ? "Method" : "Function", - fbc->common.function_name, - (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0 ); + phpdbg_try_access { + if (zend_hash_find(func_table, lcname, func_name_len + 1, (void **) &fbc) == SUCCESS) { - phpdbg_notice("%s %s %s", ++ phpdbg_notice("printinfo", "type=\"%s\" flags=\"%s\" symbol=\"%s\" num=\"%d\"", "%s %s %s (%d ops)", + (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal", + (fbc->common.scope) ? "Method" : "Function", - fbc->common.function_name); ++ fbc->common.function_name, ++ (fbc->type == ZEND_USER_FUNCTION) ? fbc->op_array.last : 0); - phpdbg_print_function_helper(fbc TSRMLS_CC); - } else { - phpdbg_error("print", "type=\"nofunction\" function=\"%s\"", "The function %s could not be found", func_name); - } + phpdbg_print_function_helper(fbc TSRMLS_CC); + } else { - phpdbg_error("The function %s could not be found", func_name); ++ phpdbg_error("print", "type=\"nofunction\" function=\"%s\"", "The function %s could not be found", func_name); + } + } phpdbg_catch_access { - phpdbg_error("Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name); ++ phpdbg_error("signalsegv", "function=\"%.*s\"", "Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name); + } phpdbg_end_try_access(); efree(lcname); diff --cc phpdbg_prompt.c index c342170,2196c21..6830f1b --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@@ -243,14 -266,11 +260,14 @@@ void phpdbg_try_file_init(char *init_fi phpdbg_init_param(&stack, STACK_PARAM); + phpdbg_activate_err_buf(1 TSRMLS_CC); + if (phpdbg_do_parse(&stack, input TSRMLS_CC) <= 0) { - switch (phpdbg_stack_execute(&stack TSRMLS_CC)) { - switch (phpdbg_stack_execute(&stack, &why, 1 /* allow_async_unsafe == 1 */ TSRMLS_CC)) { ++ switch (phpdbg_stack_execute(&stack, 1 /* allow_async_unsafe == 1 */ TSRMLS_CC)) { case FAILURE: + phpdbg_activate_err_buf(0 TSRMLS_CC); if (phpdbg_call_register(&stack TSRMLS_CC) == FAILURE) { - phpdbg_error("Unrecognized command in %s:%d: %s, %s!", init_file, line, input, why); + phpdbg_output_err_buf("initfailure", "%b file=\"%s\" line=\"%d\" input=\"%s\"", "Unrecognized command in %s:%d: %s, %b!" TSRMLS_CC, init_file, line, input); } break; } @@@ -639,11 -695,30 +663,33 @@@ out return SUCCESS; } /* }}} */ + int phpdbg_output_ev_variable(char *name, size_t len, char *keyname, size_t keylen, HashTable *parent, zval **zv TSRMLS_DC) { - phpdbg_notice("Printing variable %.*s", (int) len, name); ++ phpdbg_notice("eval", "variable=\"%.*s\"", "Printing variable %.*s", (int) len, name); ++ phpdbg_xml("<eval>"); + zend_print_zval_r(*zv, 0 TSRMLS_CC); - phpdbg_writeln(EMPTY); ++ phpdbg_xml("</eval>"); ++ phpdbg_out("\n"); ++ + efree(name); + efree(keyname); + + return SUCCESS; + } + PHPDBG_COMMAND(ev) /* {{{ */ { - zend_bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING)==PHPDBG_IS_STEPPING); + zend_bool stepping = ((PHPDBG_G(flags) & PHPDBG_IS_STEPPING) == PHPDBG_IS_STEPPING); zval retval; + if (PHPDBG_G(flags) & PHPDBG_IN_SIGNAL_HANDLER) { + phpdbg_try_access { + phpdbg_parse_variable(param->str, param->len, &EG(symbol_table), 0, phpdbg_output_ev_variable, 0 TSRMLS_CC); + } phpdbg_catch_access { - phpdbg_error("Could not fetch data, invalid data source"); ++ phpdbg_error("signalsegv", "", "Could not fetch data, invalid data source"); + } phpdbg_end_try_access(); + return SUCCESS; + } + if (!(PHPDBG_G(flags) & PHPDBG_IS_STEPONEVAL)) { PHPDBG_G(flags) &= ~PHPDBG_IS_STEPPING; } @@@ -802,6 -886,190 +848,193 @@@ PHPDBG_COMMAND(sh) /* {{{ * return SUCCESS; } /* }}} */ -static int add_extension_info(zend_module_entry *module TSRMLS_DC) { - phpdbg_write("%s\n", module->name); ++static int add_module_info(zend_module_entry *module TSRMLS_DC) { ++ phpdbg_write("module", "name=\"%s\"", "%s\n", module->name); + return 0; + } + + static int add_zendext_info(zend_extension *ext TSRMLS_DC) { - phpdbg_write("%s\n", ext->name); ++ phpdbg_write("extension", "name=\"%s\"", "%s\n", ext->name); + return 0; + } + -PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, char **str TSRMLS_DC) { ++PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, char **name TSRMLS_DC) { + DL_HANDLE handle; + char *extension_dir; + + extension_dir = INI_STR("extension_dir"); + + if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) { + /* path is fine */ + } else if (extension_dir && extension_dir[0]) { + char *libpath; + int extension_dir_len = strlen(extension_dir); + if (IS_SLASH(extension_dir[extension_dir_len-1])) { + spprintf(&libpath, 0, "%s%s", extension_dir, *path); /* SAFE */ + } else { + spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, *path); /* SAFE */ + } + efree(*path); + *path = libpath; + } else { - *str = estrdup("Not a full path given or extension_dir ini setting is not set"); ++ phpdbg_error("dl", "type=\"relpath\"", "Not a full path given or extension_dir ini setting is not set"); + + return NULL; + } + + handle = DL_LOAD(*path); + + if (!handle) { + #if PHP_WIN32 + char *err = GET_DL_ERROR(); - if (err && (*err != "")) { - *str = estrdup(err); ++ if (err && *err != "") { ++ phpdbg_error("dl", "type=\"unknown\"", "%s", err); + LocalFree(err); + } else { - *str = estrdup("Unknown reason"); ++ phpdbg_error("dl", "type=\"unknown\"", "Unknown reason"); + } + #else - *str = estrdup(GET_DL_ERROR()); - GET_DL_ERROR(); /* free the buffer storing the error */ ++ phpdbg_error("dl", "type=\"unknown\"", "%s", GET_DL_ERROR()); + #endif + return NULL; + } + + #if ZEND_EXTENSIONS_SUPPORT + do { + zend_extension *new_extension; + zend_extension_version_info *extension_version_info; + + extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info"); + if (!extension_version_info) { + extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info"); + } + new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry"); + if (!new_extension) { + new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry"); + } + if (!extension_version_info || !new_extension) { + break; + } + if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) { - asprintf(str, "%s requires Zend Engine API version %d, which does not match the installed Zend Engine API version %d", new_extension->name, extension_version_info->zend_extension_api_no, ZEND_EXTENSION_API_NO); ++ phpdbg_error("dl", "type=\"wrongapi\" extension=\"%s\" apineeded=\"%d\" apiinstalled=\"%d\"", "%s requires Zend Engine API version %d, which does not match the installed Zend Engine API version %d", new_extension->name, extension_version_info->zend_extension_api_no, ZEND_EXTENSION_API_NO); + + goto quit; + } else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) && (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) { - asprintf(str, "%s was built with configuration %s, whereas running engine is %s", new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID); ++ phpdbg_error("dl", "type=\"wrongbuild\" extension=\"%s\" buildneeded=\"%s\" buildinstalled=\"%s\"", "%s was built with configuration %s, whereas running engine is %s", new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID); + + goto quit; + } + - *str = new_extension->name; ++ *name = new_extension->name; + + zend_register_extension(new_extension, handle); + + if (new_extension->startup) { + if (new_extension->startup(new_extension) != SUCCESS) { - asprintf(str, "Unable to startup Zend extension %s", new_extension->name); ++ phpdbg_error("dl", "type=\"startupfailure\" extension=\"%s\"", "Unable to startup Zend extension %s", new_extension->name); + + goto quit; + } + zend_append_version_info(new_extension); + } + + return "Zend extension"; + } while (0); + #endif + + do { + zend_module_entry *module_entry; + zend_module_entry *(*get_module)(void); + + get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module"); + if (!get_module) { + get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module"); + } + + if (!get_module) { + break; + } + + module_entry = get_module(); ++ *name = (char *) module_entry->name; + + if (strcmp(ZEND_EXTENSION_BUILD_ID, module_entry->build_id)) { - asprintf(str, "%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID); ++ phpdbg_error("dl", "type=\"wrongbuild\" module=\"%s\" buildneeded=\"%s\" buildinstalled=\"%s\"", "%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID); + + goto quit; + } + + module_entry->type = MODULE_PERSISTENT; + module_entry->module_number = zend_next_free_module(); + module_entry->handle = handle; + + if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) { - asprintf(str, "Unable to register module %s", module_entry->name); ++ phpdbg_error("dl", "type=\"registerfailure\" module=\"%s\"", "Unable to register module %s", module_entry->name); + + goto quit; + } + + if (zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) { - asprintf(str, "Unable to startup module %s", module_entry->name); ++ phpdbg_error("dl", "type=\"startupfailure\" module=\"%s\"", "Unable to startup module %s", module_entry->name); + + goto quit; + } + + if (module_entry->request_startup_func) { + if (module_entry->request_startup_func(MODULE_PERSISTENT, module_entry->module_number TSRMLS_CC) == FAILURE) { - asprintf(str, "Unable to initialize module %s", module_entry->name); ++ phpdbg_error("dl", "type=\"initfailure\" module=\"%s\"", "Unable to initialize module %s", module_entry->name); + + goto quit; + } + } + + return "module"; + } while (0); + - *str = estrdup("This shared object is nor a Zend extension nor a module"); ++ phpdbg_error("dl", "type=\"nophpso\"", "This shared object is nor a Zend extension nor a module"); + + quit: + DL_UNLOAD(handle); + return NULL; + } + + PHPDBG_COMMAND(dl) /* {{{ */ + { + const char *type; + char *name, *path; + + if (!param || param->type == EMPTY_PARAM) { - phpdbg_notice("Zend extensions"); ++ phpdbg_notice("dl", "extensiontype=\"Zend extension\"", "Zend extensions"); + zend_llist_apply(&zend_extensions, (llist_apply_func_t) add_zendext_info TSRMLS_CC); - phpdbg_writeln(EMPTY); - phpdbg_notice("Modules"); - zend_hash_apply(&module_registry, (apply_func_t) add_extension_info TSRMLS_CC); ++ phpdbg_out("\n"); ++ phpdbg_notice("dl", "extensiontype=\"module\"", "Modules"); ++ zend_hash_apply(&module_registry, (apply_func_t) add_module_info TSRMLS_CC); + } else switch (param->type) { + case STR_PARAM: + #ifdef HAVE_LIBDL + path = estrndup(param->str, param->len); + ++ phpdbg_activate_err_buf(1 TSRMLS_CC); + if ((type = phpdbg_load_module_or_extension(&path, &name TSRMLS_CC)) == NULL) { - phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", path, name); ++ phpdbg_error("dl", "path=\"%s\" %b", "Could not load %s, not found or invalid zend extension / module: %b", path); + efree(name); + } else { - phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, path); ++ phpdbg_notice("dl", "extensiontype=\"%s\" name=\"%s\" path=\"%s\"", "Successfully loaded the %s %s at path %s", type, name, path); + } ++ phpdbg_activate_err_buf(0 TSRMLS_CC); ++ phpdbg_free_err_buf(TSRMLS_C); + efree(path); + #else - phpdbg_error("Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str); ++ phpdbg_error("dl", "type=\"unsupported\" path=\"%.*s\"", "Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str); + #endif + break; + + phpdbg_default_switch_case(); + } + + return SUCCESS; + } /* }}} */ + PHPDBG_COMMAND(source) /* {{{ */ { struct stat sb; @@@ -947,9 -1214,10 +1180,9 @@@ PHPDBG_COMMAND(watch) /* {{{ * return SUCCESS; } /* }}} */ - int phpdbg_interactive(TSRMLS_D) /* {{{ */ + int phpdbg_interactive(zend_bool allow_async_unsafe TSRMLS_DC) /* {{{ */ { int ret = SUCCESS; - char *why = NULL; char *input = NULL; phpdbg_param_t stack; @@@ -962,15 -1230,20 +1195,15 @@@ phpdbg_init_param(&stack, STACK_PARAM); if (phpdbg_do_parse(&stack, input TSRMLS_CC) <= 0) { - switch (ret = phpdbg_stack_execute(&stack, &why, allow_async_unsafe TSRMLS_CC)) { + phpdbg_activate_err_buf(1 TSRMLS_CC); + - switch (ret = phpdbg_stack_execute(&stack TSRMLS_CC)) { ++ switch (ret = phpdbg_stack_execute(&stack, allow_async_unsafe TSRMLS_CC)) { case FAILURE: if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) { - if (phpdbg_call_register(&stack TSRMLS_CC) == FAILURE) { + if (!allow_async_unsafe || phpdbg_call_register(&stack TSRMLS_CC) == FAILURE) { - if (why) { - phpdbg_error("%s", why); - } + phpdbg_output_err_buf(NULL, "%b", "%b" TSRMLS_CC); } } - - if (why) { - efree(why); - why = NULL; - } break; case PHPDBG_LEAVE: @@@ -1240,14 -1517,16 +1470,16 @@@ zend_vm_enter } } - next: if (PHPDBG_G(flags) & PHPDBG_IS_SIGNALED) { + PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED; + - phpdbg_writeln(EMPTY); - phpdbg_notice("Program received signal SIGINT"); + phpdbg_out("\n"); + phpdbg_notice("signal", "type=\"SIGINT\"", "Program received signal SIGINT"); - PHPDBG_G(flags) &= ~PHPDBG_IS_SIGNALED; - DO_INTERACTIVE(); + DO_INTERACTIVE(1); } + next: + PHPDBG_G(last_line) = execute_data->opline->lineno; /* stupid hack to make zend_do_fcall_common_helper return ZEND_VM_ENTER() instead of recursively calling zend_execute() and eventually segfaulting */ @@@ -1280,3 -1567,29 +1520,29 @@@ } zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen"); } /* }}} */ + + /* only if *not* interactive and while executing */ + void phpdbg_force_interruption(TSRMLS_D) { + zend_execute_data *data = EG(current_execute_data); /* should be always readable if not NULL */ + + PHPDBG_G(flags) |= PHPDBG_IN_SIGNAL_HANDLER; + + if (data) { + if (data->op_array) { - phpdbg_notice("Current opline: %p (op #%lu) in %s:%u", data->opline, (data->opline - data->op_array->opcodes) / sizeof(data->opline), data->op_array->filename, data->opline->lineno); ++ phpdbg_notice("hardinterrupt", "opline=\"%p\" num=\"%lu\" file=\"%s\" line=\"%u\"", "Current opline: %p (op #%lu) in %s:%u", data->opline, (data->opline - data->op_array->opcodes) / sizeof(data->opline), data->op_array->filename, data->opline->lineno); + } else { - phpdbg_notice("Current opline: %p (op_array information unavailable)", data->opline); ++ phpdbg_notice("hardinterrupt", "opline=\"%p\"", "Current opline: %p (op_array information unavailable)", data->opline); + } + } else { - phpdbg_notice("No information available about executing context"); ++ phpdbg_notice("hardinterrupt", "", "No information available about executing context"); + } + + DO_INTERACTIVE(0); + + next: + PHPDBG_G(flags) &= ~PHPDBG_IN_SIGNAL_HANDLER; + + if (PHPDBG_G(flags) & PHPDBG_IS_QUITTING) { + zend_bailout(); + } + } diff --cc phpdbg_utils.c index c303bc1,aba0d60..5ec3714 --- a/phpdbg_utils.c +++ b/phpdbg_utils.c @@@ -225,1116 -224,87 +225,1159 @@@ PHPDBG_API char *phpdbg_trim(const cha } /* }}} */ -PHPDBG_API int phpdbg_print(int type TSRMLS_DC, int fd, const char *format, ...) { /* {{{ */ - int rc = 0; - char *buffer = NULL, *outbuf = NULL; - va_list args; +/* copied from php-src/main/snprintf.c and slightly modified */ +/* + * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions + * + * XXX: this is a magic number; do not decrease it + * Emax = 1023 + * NDIG = 320 + * NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1; + */ +#define NUM_BUF_SIZE 2048 - if (format != NULL && strlen(format) > 0L) { - va_start(args, format); - vspprintf(&buffer, 0, format, args); - va_end(args); +/* + * Descriptor for buffer area + */ +struct buf_area { + char *buf_end; + char *nextb; /* pointer to next byte to read/write */ +}; + +typedef struct buf_area buffy; + +/* + * The INS_CHAR macro inserts a character in the buffer and writes + * the buffer back to disk if necessary + * It uses the char pointers sp and bep: + * sp points to the next available character in the buffer + * bep points to the end-of-buffer+1 + * While using this macro, note that the nextb pointer is NOT updated. + * + * NOTE: Evaluation of the c argument should not have any side-effects + */ +#define INS_CHAR(c, sp, bep, cc) \ + { \ + if (sp < bep) \ + { \ + *sp++ = c; \ + } \ + cc++; \ + } + +#define NUM( c ) ( c - '0' ) + +#define STR_TO_DEC( str, num ) \ + num = NUM( *str++ ) ; \ + while ( isdigit((int)*str ) ) \ + { \ + num *= 10 ; \ + num += NUM( *str++ ) ; \ + } + +/* + * This macro does zero padding so that the precision + * requirement is satisfied. The padding is done by + * adding '0's to the left of the string that is going + * to be printed. + */ +#define FIX_PRECISION( adjust, precision, s, s_len ) \ + if ( adjust ) \ + while ( s_len < precision ) \ + { \ + *--s = '0' ; \ + s_len++ ; \ + } + +/* + * Macro that does padding. The padding is done by printing + * the character ch. + */ +#define PAD( width, len, ch ) do \ + { \ + INS_CHAR( ch, sp, bep, cc ) ; \ + width-- ; \ + } \ + while ( width > len ) + +/* + * Prefix the character ch to the string str + * Increase length + * Set the has_prefix flag + */ +#define PREFIX( str, length, ch ) *--str = ch ; length++ ; has_prefix = YES + + +#ifdef HAVE_LOCALE_H +#include <locale.h> +#define LCONV_DECIMAL_POINT (*lconv->decimal_point) +#else +#define LCONV_DECIMAL_POINT '.' +#endif +#define NUL '\0' +#define S_NULL "(null)" +#define S_NULL_LEN 6 +#define FLOAT_DIGITS 6 + +/* + * Do format conversion placing the output in buffer + */ +static int format_converter(register buffy *odp, const char *fmt, zend_bool escape_xml, va_list ap TSRMLS_DC) { + char *sp; + char *bep; + int cc = 0; + int i; + + char *s = NULL, *free_s = NULL; + int s_len, free_zcopy; + zval *zvp, zcopy; + + int min_width = 0; + int precision = 0; + enum { + LEFT, RIGHT + } adjust; + char pad_char; + char prefix_char; + + double fp_num; + wide_int i_num = (wide_int) 0; + u_wide_int ui_num; + + char num_buf[NUM_BUF_SIZE]; + char char_buf[2]; /* for printing %% and %<unknown> */ + +#ifdef HAVE_LOCALE_H + struct lconv *lconv = NULL; +#endif + + /* + * Flag variables + */ + length_modifier_e modifier; + boolean_e alternate_form; + boolean_e print_sign; + boolean_e print_blank; + boolean_e adjust_precision; + boolean_e adjust_width; + bool_int is_negative; + + sp = odp->nextb; + bep = odp->buf_end; + + while (*fmt) { + if (*fmt != '%') { + INS_CHAR(*fmt, sp, bep, cc); + } else { + /* + * Default variable settings + */ + adjust = RIGHT; + alternate_form = print_sign = print_blank = NO; + pad_char = ' '; + prefix_char = NUL; + free_zcopy = 0; + + fmt++; + + /* + * Try to avoid checking for flags, width or precision + */ + if (isascii((int)*fmt) && !islower((int)*fmt)) { + /* + * Recognize flags: -, #, BLANK, + + */ + for (;; fmt++) { + if (*fmt == '-') + adjust = LEFT; + else if (*fmt == '+') + print_sign = YES; + else if (*fmt == '#') + alternate_form = YES; + else if (*fmt == ' ') + print_blank = YES; + else if (*fmt == '0') + pad_char = '0'; + else + break; + } + + /* + * Check if a width was specified + */ + if (isdigit((int)*fmt)) { + STR_TO_DEC(fmt, min_width); + adjust_width = YES; + } else if (*fmt == '*') { + min_width = va_arg(ap, int); + fmt++; + adjust_width = YES; + if (min_width < 0) { + adjust = LEFT; + min_width = -min_width; + } + } else + adjust_width = NO; + + /* + * Check if a precision was specified + */ + if (*fmt == '.') { + adjust_precision = YES; + fmt++; + if (isdigit((int)*fmt)) { + STR_TO_DEC(fmt, precision); + } else if (*fmt == '*') { + precision = va_arg(ap, int); + fmt++; + if (precision < 0) + precision = 0; + } else + precision = 0; + + if (precision > FORMAT_CONV_MAX_PRECISION && *fmt != 's' && *fmt != 'v' && *fmt != 'b') { + precision = FORMAT_CONV_MAX_PRECISION; + } + } else + adjust_precision = NO; + } else + adjust_precision = adjust_width = NO; + + /* + * Modifier check + */ + switch (*fmt) { + case 'L': + fmt++; + modifier = LM_LONG_DOUBLE; + break; + case 'I': + fmt++; +#if SIZEOF_LONG_LONG + if (*fmt == '6' && *(fmt+1) == '4') { + fmt += 2; + modifier = LM_LONG_LONG; + } else +#endif + if (*fmt == '3' && *(fmt+1) == '2') { + fmt += 2; + modifier = LM_LONG; + } else { +#ifdef _WIN64 + modifier = LM_LONG_LONG; +#else + modifier = LM_LONG; +#endif + } + break; + case 'l': + fmt++; +#if SIZEOF_LONG_LONG + if (*fmt == 'l') { + fmt++; + modifier = LM_LONG_LONG; + } else +#endif + modifier = LM_LONG; + break; + case 'z': + fmt++; + modifier = LM_SIZE_T; + break; + case 'j': + fmt++; +#if SIZEOF_INTMAX_T + modifier = LM_INTMAX_T; +#else + modifier = LM_SIZE_T; +#endif + break; + case 't': + fmt++; +#if SIZEOF_PTRDIFF_T + modifier = LM_PTRDIFF_T; +#else + modifier = LM_SIZE_T; +#endif + break; + case 'h': + fmt++; + if (*fmt == 'h') { + fmt++; + } + /* these are promoted to int, so no break */ + default: + modifier = LM_STD; + break; + } + + /* + * Argument extraction and printing. + * First we determine the argument type. + * Then, we convert the argument to a string. + * On exit from the switch, s points to the string that + * must be printed, s_len has the length of the string + * The precision requirements, if any, are reflected in s_len. + * + * NOTE: pad_char may be set to '0' because of the 0 flag. + * It is reset to ' ' by non-numeric formats + */ + switch (*fmt) { + case 'Z': + zvp = (zval*) va_arg(ap, zval*); + zend_make_printable_zval(zvp, &zcopy, &free_zcopy); + if (free_zcopy) { + zvp = &zcopy; + } + s_len = Z_STRLEN_P(zvp); + s = Z_STRVAL_P(zvp); + if (adjust_precision && precision < s_len) { + s_len = precision; + } + break; + case 'u': + switch(modifier) { + default: + i_num = (wide_int) va_arg(ap, unsigned int); + break; + case LM_LONG_DOUBLE: + goto fmt_error; + case LM_LONG: + i_num = (wide_int) va_arg(ap, unsigned long int); + break; + case LM_SIZE_T: + i_num = (wide_int) va_arg(ap, size_t); + break; +#if SIZEOF_LONG_LONG + case LM_LONG_LONG: + i_num = (wide_int) va_arg(ap, u_wide_int); + break; +#endif +#if SIZEOF_INTMAX_T + case LM_INTMAX_T: + i_num = (wide_int) va_arg(ap, uintmax_t); + break; +#endif +#if SIZEOF_PTRDIFF_T + case LM_PTRDIFF_T: + i_num = (wide_int) va_arg(ap, ptrdiff_t); + break; +#endif + } + /* + * The rest also applies to other integer formats, so fall + * into that case. + */ + case 'd': + case 'i': + /* + * Get the arg if we haven't already. + */ + if ((*fmt) != 'u') { + switch(modifier) { + default: + i_num = (wide_int) va_arg(ap, int); + break; + case LM_LONG_DOUBLE: + goto fmt_error; + case LM_LONG: + i_num = (wide_int) va_arg(ap, long int); + break; + case LM_SIZE_T: +#if SIZEOF_SSIZE_T + i_num = (wide_int) va_arg(ap, ssize_t); +#else + i_num = (wide_int) va_arg(ap, size_t); +#endif + break; +#if SIZEOF_LONG_LONG + case LM_LONG_LONG: + i_num = (wide_int) va_arg(ap, wide_int); + break; +#endif +#if SIZEOF_INTMAX_T + case LM_INTMAX_T: + i_num = (wide_int) va_arg(ap, intmax_t); + break; +#endif +#if SIZEOF_PTRDIFF_T + case LM_PTRDIFF_T: + i_num = (wide_int) va_arg(ap, ptrdiff_t); + break; +#endif + } + } + s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative, + &num_buf[NUM_BUF_SIZE], &s_len); + FIX_PRECISION(adjust_precision, precision, s, s_len); + + if (*fmt != 'u') { + if (is_negative) { + prefix_char = '-'; + } else if (print_sign) { + prefix_char = '+'; + } else if (print_blank) { + prefix_char = ' '; + } + } + break; + + + case 'o': + switch(modifier) { + default: + ui_num = (u_wide_int) va_arg(ap, unsigned int); + break; + case LM_LONG_DOUBLE: + goto fmt_error; + case LM_LONG: + ui_num = (u_wide_int) va_arg(ap, unsigned long int); + break; + case LM_SIZE_T: + ui_num = (u_wide_int) va_arg(ap, size_t); + break; +#if SIZEOF_LONG_LONG + case LM_LONG_LONG: + ui_num = (u_wide_int) va_arg(ap, u_wide_int); + break; +#endif +#if SIZEOF_INTMAX_T + case LM_INTMAX_T: + ui_num = (u_wide_int) va_arg(ap, uintmax_t); + break; +#endif +#if SIZEOF_PTRDIFF_T + case LM_PTRDIFF_T: + ui_num = (u_wide_int) va_arg(ap, ptrdiff_t); + break; +#endif + } + s = ap_php_conv_p2(ui_num, 3, *fmt, &num_buf[NUM_BUF_SIZE], &s_len); + FIX_PRECISION(adjust_precision, precision, s, s_len); + if (alternate_form && *s != '0') { + *--s = '0'; + s_len++; + } + break; + + + case 'x': + case 'X': + switch(modifier) { + default: + ui_num = (u_wide_int) va_arg(ap, unsigned int); + break; + case LM_LONG_DOUBLE: + goto fmt_error; + case LM_LONG: + ui_num = (u_wide_int) va_arg(ap, unsigned long int); + break; + case LM_SIZE_T: + ui_num = (u_wide_int) va_arg(ap, size_t); + break; +#if SIZEOF_LONG_LONG + case LM_LONG_LONG: + ui_num = (u_wide_int) va_arg(ap, u_wide_int); + break; +#endif +#if SIZEOF_INTMAX_T + case LM_INTMAX_T: + ui_num = (u_wide_int) va_arg(ap, uintmax_t); + break; +#endif +#if SIZEOF_PTRDIFF_T + case LM_PTRDIFF_T: + ui_num = (u_wide_int) va_arg(ap, ptrdiff_t); + break; +#endif + } + s = ap_php_conv_p2(ui_num, 4, *fmt, &num_buf[NUM_BUF_SIZE], &s_len); + FIX_PRECISION(adjust_precision, precision, s, s_len); + if (alternate_form && i_num != 0) { + *--s = *fmt; /* 'x' or 'X' */ + *--s = '0'; + s_len += 2; + } + break; + + + case 's': + case 'v': + s = va_arg(ap, char *); + if (s != NULL) { + if (adjust_precision) { + s_len = precision; + } else { + s_len = strlen(s); + } + + if (escape_xml) { + /* added: support for xml escaping */ + + int old_slen = s_len, i = 0; + char *old_s = s, *s_ptr; - free_s = s_ptr = s = malloc(old_slen * 6 + 1); ++ free_s = s_ptr = s = emalloc(old_slen * 6 + 1); + do { + if (old_s[i] == '&' || old_s[i] == '"') { + *s_ptr++ = '&'; + if (old_s[i] == '"') { + s_len += 5; + *s_ptr++ = 'q'; + *s_ptr++ = 'u'; + *s_ptr++ = 'o'; + *s_ptr++ = 't'; + } else { + s_len += 4; + *s_ptr++ = 'a'; + *s_ptr++ = 'm'; + *s_ptr++ = 'p'; + } + *s_ptr++ = ';'; + } else { + *s_ptr++ = old_s[i]; + } + } while (i++ < old_slen); + } + } else { + s = S_NULL; + s_len = S_NULL_LEN; + } + pad_char = ' '; + break; + + + case 'b': + if (escape_xml) { + s = PHPDBG_G(err_buf).xml; + } else { + s = PHPDBG_G(err_buf).msg; + } + + if (s != NULL) { + if (escape_xml) { + s_len = PHPDBG_G(err_buf).xmllen; + } else { + s_len = PHPDBG_G(err_buf).msglen; + } + + if (adjust_precision && precision != s_len) { + s_len = precision; + } + } else { + s = S_NULL; + s_len = S_NULL_LEN; + } + pad_char = ' '; + break; + + + case 'r': - s_len = asprintf(&s, "req=\"%lu\"", PHPDBG_G(req_id)); ++ s_len = spprintf(&s, 0, "req=\"%lu\"", PHPDBG_G(req_id)); + free_s = s; + break; + + + case 'f': + case 'F': + case 'e': + case 'E': + + switch(modifier) { + case LM_LONG_DOUBLE: + fp_num = (double) va_arg(ap, long double); + break; + case LM_STD: + fp_num = va_arg(ap, double); + break; + default: + goto fmt_error; + } + + if (zend_isnan(fp_num)) { + s = "NAN"; + s_len = 3; + } else if (zend_isinf(fp_num)) { + s = "INF"; + s_len = 3; + } else { +#ifdef HAVE_LOCALE_H + if (!lconv) { + lconv = localeconv(); + } +#endif + s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form, + (adjust_precision == NO) ? FLOAT_DIGITS : precision, + (*fmt == 'f')?LCONV_DECIMAL_POINT:'.', + &is_negative, &num_buf[1], &s_len); + if (is_negative) + prefix_char = '-'; + else if (print_sign) + prefix_char = '+'; + else if (print_blank) + prefix_char = ' '; + } + break; + + + case 'g': + case 'k': + case 'G': + case 'H': + switch(modifier) { + case LM_LONG_DOUBLE: + fp_num = (double) va_arg(ap, long double); + break; + case LM_STD: + fp_num = va_arg(ap, double); + break; + default: + goto fmt_error; + } + + if (zend_isnan(fp_num)) { + s = "NAN"; + s_len = 3; + break; + } else if (zend_isinf(fp_num)) { + if (fp_num > 0) { + s = "INF"; + s_len = 3; + } else { + s = "-INF"; + s_len = 4; + } + break; + } + + if (adjust_precision == NO) { + precision = FLOAT_DIGITS; + } else if (precision == 0) { + precision = 1; + } + /* + * * We use &num_buf[ 1 ], so that we have room for the sign + */ +#ifdef HAVE_LOCALE_H + if (!lconv) { + lconv = localeconv(); + } +#endif + s = php_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]); + if (*s == '-') { + prefix_char = *s++; + } else if (print_sign) { + prefix_char = '+'; + } else if (print_blank) { + prefix_char = ' '; + } + + s_len = strlen(s); + + if (alternate_form && (strchr(s, '.')) == NULL) { + s[s_len++] = '.'; + } + break; + + + case 'c': + char_buf[0] = (char) (va_arg(ap, int)); + s = &char_buf[0]; + s_len = 1; + pad_char = ' '; + break; + + + case '%': + char_buf[0] = '%'; + s = &char_buf[0]; + s_len = 1; + pad_char = ' '; + break; + + + case 'n': + *(va_arg(ap, int *)) = cc; + goto skip_output; + + /* + * Always extract the argument as a "char *" pointer. We + * should be using "void *" but there are still machines + * that don't understand it. + * If the pointer size is equal to the size of an unsigned + * integer we convert the pointer to a hex number, otherwise + * we print "%p" to indicate that we don't handle "%p". + */ + case 'p': + if (sizeof(char *) <= sizeof(u_wide_int)) { + ui_num = (u_wide_int)((size_t) va_arg(ap, char *)); + s = ap_php_conv_p2(ui_num, 4, 'x', + &num_buf[NUM_BUF_SIZE], &s_len); + if (ui_num != 0) { + *--s = 'x'; + *--s = '0'; + s_len += 2; + } + } else { + s = "%p"; + s_len = 2; + } + pad_char = ' '; + break; + + + case NUL: + /* + * The last character of the format string was %. + * We ignore it. + */ + continue; + + +fmt_error: + php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt); + /* + * The default case is for unrecognized %'s. + * We print %<char> to help the user identify what + * option is not understood. + * This is also useful in case the user wants to pass + * the output of format_converter to another function + * that understands some other %<char> (like syslog). + * Note that we can't point s inside fmt because the + * unknown <char> could be preceded by width etc. + */ + default: + char_buf[0] = '%'; + char_buf[1] = *fmt; + s = char_buf; + s_len = 2; + pad_char = ' '; + break; + } + + if (prefix_char != NUL) { + *--s = prefix_char; + s_len++; + } + if (adjust_width && adjust == RIGHT && min_width > s_len) { + if (pad_char == '0' && prefix_char != NUL) { + INS_CHAR(*s, sp, bep, cc) + s++; + s_len--; + min_width--; + } + PAD(min_width, s_len, pad_char); + } + /* + * Print the string s. + */ + for (i = s_len; i != 0; i--) { + INS_CHAR(*s, sp, bep, cc); + s++; + } + + if (adjust_width && adjust == LEFT && min_width > s_len) + PAD(min_width, s_len, pad_char); + if (free_zcopy) { + zval_dtor(&zcopy); + } + } +skip_output: + if (free_s) { - free(free_s); ++ efree(free_s); + free_s = NULL; + } + + fmt++; + } + odp->nextb = sp; + return (cc); +} + +static void strx_printv(int *ccp, char *buf, size_t len, const char *format, zend_bool escape_xml, va_list ap TSRMLS_DC) { + buffy od; + int cc; + + /* + * First initialize the descriptor + * Notice that if no length is given, we initialize buf_end to the + * highest possible address. + */ + if (len == 0) { + od.buf_end = (char *) ~0; + od.nextb = (char *) ~0; + } else { + od.buf_end = &buf[len-1]; + od.nextb = buf; + } + + /* + * Do the conversion + */ + cc = format_converter(&od, format, escape_xml, ap TSRMLS_CC); + if (len != 0 && od.nextb <= od.buf_end) { + *(od.nextb) = '\0'; + } + if (ccp) { + *ccp = cc; + } +} + +static int phpdbg_xml_vsnprintf(char *buf, size_t len, const char *format, zend_bool escape_xml, va_list ap TSRMLS_DC) { + int cc; + + strx_printv(&cc, buf, len, format, escape_xml, ap TSRMLS_CC); + return (cc); +} + +PHPAPI int phpdbg_xml_vasprintf(char **buf, const char *format, zend_bool escape_xml, va_list ap TSRMLS_DC) { + va_list ap2; + int cc; + + va_copy(ap2, ap); + cc = phpdbg_xml_vsnprintf(NULL, 0, format, escape_xml, ap2 TSRMLS_CC); + va_end(ap2); + + *buf = NULL; + + if (cc >= 0) { - if ((*buf = malloc(++cc)) != NULL) { ++ if ((*buf = emalloc(++cc)) != NULL) { + if ((cc = phpdbg_xml_vsnprintf(*buf, cc, format, escape_xml, ap TSRMLS_CC)) < 0) { - free(*buf); ++ efree(*buf); + *buf = NULL; + } + } + } + + return cc; +} +/* copy end */ + - static int phpdbg_process_print(FILE *fp, int type, const char *tag, const char *msg, int msglen, const char *xml, int xmllen TSRMLS_DC) { - char *msgout, *buf; ++static int phpdbg_encode_xml(char **buf, char *msg, int msglen, char from, char *to) { ++ int i; ++ char *tmp = *buf = emalloc(msglen * strlen(to)); ++ int tolen = strlen(to); ++ for (i = 0; i++ < msglen; msg++) { ++ if (*msg == '&') { ++ memcpy(tmp, ZEND_STRL("&amp;")); ++ tmp += sizeof("&amp;") - 1; ++ } else if (*msg == from) { ++ memcpy(tmp, to, tolen); ++ tmp += tolen; ++ } else { ++ *tmp++ = *msg; ++ } ++ } ++ ++ { ++ int len = tmp - *buf; ++ *buf = erealloc(*buf, len); ++ return len; ++ } ++} ++ ++static int phpdbg_process_print(int fd, int type, const char *tag, const char *msg, int msglen, const char *xml, int xmllen TSRMLS_DC) { ++ char *msgout = NULL, *buf; + int msgoutlen, xmloutlen, buflen; + const char *severity; + + if ((PHPDBG_G(flags) & PHPDBG_WRITE_XML) && PHPDBG_G(in_script_xml) && PHPDBG_G(in_script_xml) != type) { - fprintf(fp, "</stream>"); ++ write(fd, ZEND_STRL("</stream>")); + PHPDBG_G(in_script_xml) = 0; } /* TODO(anyone) colours */ switch (type) { case P_ERROR: + severity = "error"; if (!PHPDBG_G(last_was_newline)) { - write(fd, "\n", 1); + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { - fprintf(fp, "<phpdbg>\n</phpdbg>"); ++ write(fd, ZEND_STRL("<phpdbg>\n</phpdbg>")); + } else { - fprintf(fp, "\n"); ++ write(fd, ZEND_STRL("\n")); + } PHPDBG_G(last_was_newline) = 1; } if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) { - msgoutlen = asprintf(&msgout, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, msglen, msg); - rc = spprintf(&outbuf, 0, "\033[%sm[%s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, buffer); ++ msgoutlen = spprintf(&msgout, 0, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_ERROR]->code, msglen, msg); } else { - msgoutlen = asprintf(&msgout, "[%.*s]\n", msglen, msg); - rc = spprintf(&outbuf, 0, "[%s]\n", buffer); ++ msgoutlen = spprintf(&msgout, 0, "[%.*s]\n", msglen, msg); } - break; + break; case P_NOTICE: + severity = "notice"; if (!PHPDBG_G(last_was_newline)) { - write(fd, "\n", 1); + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { - fprintf(fp, "<phpdbg>\n</phpdbg>"); ++ write(fd, ZEND_STRL("<phpdbg>\n</phpdbg>")); + } else { - fprintf(fp, "\n"); ++ write(fd, ZEND_STRL("\n")); + } PHPDBG_G(last_was_newline) = 1; } if (PHPDBG_G(flags) & PHPDBG_IS_COLOURED) { - msgoutlen = asprintf(&msgout, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, msglen, msg); - rc = spprintf(&outbuf, 0, "\033[%sm[%s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, buffer); ++ msgoutlen = spprintf(&msgout, 0, "\033[%sm[%.*s]\033[0m\n", PHPDBG_G(colors)[PHPDBG_COLOR_NOTICE]->code, msglen, msg); } else { - msgoutlen = asprintf(&msgout, "[%.*s]\n", msglen, msg); - rc = spprintf(&outbuf, 0, "[%s]\n", buffer); ++ msgoutlen = spprintf(&msgout, 0, "[%.*s]\n", msglen, msg); } - break; + break; - case P_WRITELN: { - if (buffer) { - rc = spprintf(&outbuf, 0, "%s\n", buffer); + case P_WRITELN: + severity = "normal"; + if (msg) { - msgoutlen = asprintf(&msgout, "%.*s\n", msglen, msg); ++ msgoutlen = spprintf(&msgout, 0, "%.*s\n", msglen, msg); } else { - rc = 1; - outbuf = estrdup("\n"); + msgoutlen = 1; - msgout = strdup("\n"); ++ msgout = estrdup("\n"); } PHPDBG_G(last_was_newline) = 1; - } break; + break; case P_WRITE: - if (buffer) { - rc = spprintf(&outbuf, 0, "%s", buffer); - PHPDBG_G(last_was_newline) = buffer[strlen(buffer) - 1] == '\n'; + severity = "normal"; + if (msg) { - msgout = strndup(msg, msglen); ++ msgout = estrndup(msg, msglen); + msgoutlen = msglen; + PHPDBG_G(last_was_newline) = msg[msglen - 1] == '\n'; + } else { + msgoutlen = 0; - msgout = strdup(""); ++ msgout = estrdup(""); + } + break; + + case P_STDOUT: + case P_STDERR: + if (msg) { + PHPDBG_G(last_was_newline) = msg[msglen - 1] == '\n'; + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { + if (PHPDBG_G(in_script_xml) != type) { - fprintf(fp, "<stream type=\"%s\">", type == P_STDERR ? "stderr" : "stdout"); ++ char *stream_buf; ++ int stream_buflen = spprintf(&stream_buf, 0, "<stream type=\"%s\">", type == P_STDERR ? "stderr" : "stdout"); ++ write(fd, stream_buf, stream_buflen); ++ efree(stream_buf); + PHPDBG_G(in_script_xml) = type; + } + buf = php_escape_html_entities((unsigned char *) msg, msglen, (size_t *) &buflen, 0, ENT_NOQUOTES, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); - fprintf(fp, "%.*s", buflen, buf); ++ write(fd, buf, buflen); + efree(buf); + } else { - fprintf(fp, "%.*s", msglen, msg); ++ write(fd, msg, msglen); + } + return msglen; } break; /* no formatting on logging output */ case P_LOG: - if (buffer) { + severity = "log"; + if (msg) { struct timeval tp; if (gettimeofday(&tp, NULL) == SUCCESS) { - msgoutlen = asprintf(&msgout, "[%ld %.8F]: %.*s\n", tp.tv_sec, tp.tv_usec / 1000000., msglen, msg); - rc = spprintf(&outbuf, 0, "[%ld %.8F]: %s\n", tp.tv_sec, tp.tv_usec / 1000000.00, buffer); ++ msgoutlen = spprintf(&msgout, 0, "[%ld %.8F]: %.*s\n", tp.tv_sec, tp.tv_usec / 1000000., msglen, msg); } else { - rc = FAILURE; + msgoutlen = FAILURE; } } break; } - - if (outbuf) { - rc = write(fd, outbuf, rc); - efree(outbuf); + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { ++ char *xmlout; ++ + if (PHPDBG_G(req_id)) { + char *xmlbuf = NULL; - xmllen = asprintf(&xmlbuf, "req=\"%lu\" %.*s", PHPDBG_G(req_id), xmllen, xml); ++ xmllen = spprintf(&xmlbuf, 0, "req=\"%lu\" %.*s", PHPDBG_G(req_id), xmllen, xml); + xml = xmlbuf; + } + if (msgout) { - buf = php_escape_html_entities((unsigned char *) msgout, msgoutlen, (size_t *) &buflen, 0, ENT_COMPAT, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); - xmloutlen = fprintf(fp, "<%s severity=\"%s\" %.*s msgout=\"%.*s\" />", tag, severity, xmllen, xml, buflen, buf); ++// unsigned char *tmp = msgout; ++// buf = php_escape_html_entities(tmp, msgoutlen, (size_t *) &buflen, 0, ENT_COMPAT, PG(internal_encoding) && PG(internal_encoding)[0] ? PG(internal_encoding) : (SG(default_charset) ? SG(default_charset) : "UTF-8") TSRMLS_CC); ++ buflen = phpdbg_encode_xml(&buf, msgout, msgoutlen, '"', "&quot;"); ++ xmloutlen = spprintf(&xmlout, 0, "<%s severity=\"%s\" %.*s msgout=\"%.*s\" />", tag, severity, xmllen, xml, buflen, buf); + + efree(buf); + } else { - xmloutlen = fprintf(fp, "<%s severity=\"%s\" %.*s msgout=\"\" />", tag, severity, xmllen, xml); ++ xmloutlen = spprintf(&xmlout, 0, "<%s severity=\"%s\" %.*s msgout=\"\" />", tag, severity, xmllen, xml); + } ++ ++ write(fd, xmlout, xmloutlen); ++ efree(xmlout); + } else if (msgout) { - fprintf(fp, "%.*s", msgoutlen, msgout); ++ write(fd, msgout, msgoutlen); } - if (buffer) { - efree(buffer); + if (PHPDBG_G(req_id) && (PHPDBG_G(flags) & PHPDBG_WRITE_XML)) { - free((char *) xml); ++ efree((char *) xml); } - return rc; + if (msgout) { - free(msgout); ++ efree(msgout); + } + + return msgout ? msgoutlen : xmloutlen; } /* }}} */ - PHPDBG_API int phpdbg_vprint(int type TSRMLS_DC, FILE *fp, const char *tag, const char *xmlfmt, const char *strfmt, va_list args) { ++PHPDBG_API int phpdbg_vprint(int type TSRMLS_DC, int fd, const char *tag, const char *xmlfmt, const char *strfmt, va_list args) { + char *msg = NULL, *xml = NULL; + int msglen = 0, xmllen = 0; + int len; + va_list argcpy; + + if (strfmt != NULL && strlen(strfmt) > 0L) { + va_copy(argcpy, args); + msglen = phpdbg_xml_vasprintf(&msg, strfmt, 0, argcpy TSRMLS_CC); + va_end(argcpy); + } + + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { + if (xmlfmt != NULL && strlen(xmlfmt) > 0L) { + va_copy(argcpy, args); + xmllen = phpdbg_xml_vasprintf(&xml, xmlfmt, 1, argcpy TSRMLS_CC); + va_end(argcpy); + } + } + + if (PHPDBG_G(err_buf).active && type != P_STDOUT && type != P_STDERR) { + PHPDBG_G(err_buf).type = type; - PHPDBG_G(err_buf).fp = fp; - PHPDBG_G(err_buf).tag = strdup(tag); ++ PHPDBG_G(err_buf).fd = fd; ++ PHPDBG_G(err_buf).tag = estrdup(tag); + PHPDBG_G(err_buf).msg = msg; + PHPDBG_G(err_buf).msglen = msglen; + PHPDBG_G(err_buf).xml = xml; + PHPDBG_G(err_buf).xmllen = xmllen; + + return msglen; + } + - len = phpdbg_process_print(fp, type, tag, msg, msglen, xml, xmllen TSRMLS_CC); ++ len = phpdbg_process_print(fd, type, tag, msg, msglen, xml, xmllen TSRMLS_CC); + + if (msg) { - free(msg); ++ efree(msg); + } + + if (xml) { - free(xml); ++ efree(xml); + } + + return len; +} + +PHPDBG_API void phpdbg_free_err_buf(TSRMLS_D) { + if (PHPDBG_G(err_buf).type == 0) { + return; + } + + PHPDBG_G(err_buf).type = 0; + - free(PHPDBG_G(err_buf).tag); - free(PHPDBG_G(err_buf).msg); - free(PHPDBG_G(err_buf).xml); ++ efree(PHPDBG_G(err_buf).tag); ++ efree(PHPDBG_G(err_buf).msg); ++ efree(PHPDBG_G(err_buf).xml); +} + +PHPDBG_API void phpdbg_activate_err_buf(zend_bool active TSRMLS_DC) { + PHPDBG_G(err_buf).active = active; +} + +PHPDBG_API int phpdbg_output_err_buf(const char *tag, const char *xmlfmt, const char *strfmt TSRMLS_DC, ...) { + int len; + va_list args; + int errbuf_active = PHPDBG_G(err_buf).active; + + PHPDBG_G(err_buf).active = 0; + +#ifdef ZTS + va_start(args, tsrm_ls); +#else + va_start(args, strfmt); +#endif - len = phpdbg_vprint(PHPDBG_G(err_buf).type TSRMLS_CC, PHPDBG_G(err_buf).fp, tag ? tag : PHPDBG_G(err_buf).tag, xmlfmt, strfmt, args); ++ len = phpdbg_vprint(PHPDBG_G(err_buf).type TSRMLS_CC, PHPDBG_G(err_buf).fd, tag ? tag : PHPDBG_G(err_buf).tag, xmlfmt, strfmt, args); + va_end(args); + + PHPDBG_G(err_buf).active = errbuf_active; + phpdbg_free_err_buf(TSRMLS_C); + + return len; +} + - PHPDBG_API int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *tag, const char *xmlfmt, const char *strfmt, ...) { ++PHPDBG_API int phpdbg_print(int type TSRMLS_DC, int fd, const char *tag, const char *xmlfmt, const char *strfmt, ...) { + va_list args; + int len; + + va_start(args, strfmt); - len = phpdbg_vprint(type TSRMLS_CC, fp, tag, xmlfmt, strfmt, args); ++ len = phpdbg_vprint(type TSRMLS_CC, fd, tag, xmlfmt, strfmt, args); + va_end(args); + + return len; +} + - PHPDBG_API int phpdbg_xml_internal(FILE *fp TSRMLS_DC, const char *fmt, ...) { ++PHPDBG_API int phpdbg_xml_internal(int fd TSRMLS_DC, const char *fmt, ...) { ++ int len = 0; ++ + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { + va_list args; + char *buffer; + int buflen; + + va_start(args, fmt); + buflen = phpdbg_xml_vasprintf(&buffer, fmt, 0, args TSRMLS_CC); + va_end(args); + + if (PHPDBG_G(in_script_xml)) { - fprintf(fp, "</stream>"); ++ write(fd, ZEND_STRL("</stream>")); + PHPDBG_G(in_script_xml) = 0; + } + - return fprintf(fp, "%.*s", buflen, buffer); ++ len = write(fd, buffer, buflen); ++ efree(buffer); + } + - return 0; ++ return len; +} + - PHPDBG_API int phpdbg_log_internal(FILE *fp TSRMLS_DC, const char *fmt, ...) { ++PHPDBG_API int phpdbg_log_internal(int fd TSRMLS_DC, const char *fmt, ...) { + va_list args; + char *buffer; + int buflen; ++ int len = 0; + + va_start(args, fmt); + buflen = vspprintf(&buffer, 0, fmt, args); + va_end(args); + - return fprintf(fp, "%.*s", buflen, buffer); ++ len = write(fd, buffer, buflen); ++ efree(buffer); ++ ++ return len; +} + - PHPDBG_API int phpdbg_out_internal(FILE *fp TSRMLS_DC, const char *fmt, ...) { ++PHPDBG_API int phpdbg_out_internal(int fd TSRMLS_DC, const char *fmt, ...) { + va_list args; + char *buffer; + int buflen; ++ int len = 0; + + va_start(args, fmt); + buflen = vspprintf(&buffer, 0, fmt, args); + va_end(args); + + if (PHPDBG_G(flags) & PHPDBG_WRITE_XML) { + if (PHPDBG_G(in_script_xml)) { - fprintf(fp, "</stream>"); ++ write(fd, ZEND_STRL("</stream>")); + PHPDBG_G(in_script_xml) = 0; + } + - fprintf(fp, "<phpdbg>%.*s</phpdbg>", buflen, buffer); - return buflen; ++ write(fd, ZEND_STRL("<phpdbg>")); ++ len = write(fd, buffer, buflen); ++ write(fd, ZEND_STRL("</phpdbg>")); + } else { - return fprintf(fp, "%.*s", buflen, buffer); ++ len = write(fd, buffer, buflen); + } ++ ++ return len; +} + + - PHPDBG_API int phpdbg_rlog(FILE *fp, const char *fmt, ...) { /* {{{ */ + PHPDBG_API int phpdbg_rlog(int fd, const char *fmt, ...) { /* {{{ */ int rc = 0; va_list args; @@@ -1489,3 -466,157 +1537,157 @@@ PHPDBG_API int phpdbg_get_terminal_widt #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, ret = FAILURE; + + 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 { + ret = zend_hash_find(EG(class_table), lc_name, lc_length, (void **) &ce); + } phpdbg_catch_access { - phpdbg_error("Could not fetch class %.*s, invalid data source", name_length, name); ++ phpdbg_error("signalsegv", "class=\"%.*s\"", "Could not fetch class %.*s, invalid data source", name_length, name); + } phpdbg_end_try_access(); + + efree(lc_free); + return ret; + } else { + return zend_lookup_class(name, name_length, ce TSRMLS_CC); + } + } + + 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) { + HashPosition position; + for (zend_hash_internal_pointer_reset_ex(parent, &position); + zend_hash_get_current_data_ex(parent, (void **)&zv, &position) == SUCCESS; + zend_hash_move_forward_ex(parent, &position)) { + if (i == len || (i == len - 1 && input[len - 1] == ']')) { + zval *key = emalloc(sizeof(zval)); + size_t namelen; + char *name; + char *keyname = estrndup(last_index, index_len); + zend_hash_get_current_key_zval_ex(parent, key, &position); + convert_to_string(key); + name = emalloc(i + Z_STRLEN_P(key) + 2); + namelen = sprintf(name, "%.*s%s%s", (int)i, input, phpdbg_get_property_key(Z_STRVAL_P(key)), input[len - 1] == ']'?"]":""); + efree(key); + + ret = callback(name, namelen, keyname, index_len, parent, zv, arg TSRMLS_CC) == SUCCESS || ret == SUCCESS?SUCCESS:FAILURE; + } else if (Z_TYPE_PP(zv) == IS_OBJECT) { + phpdbg_parse_variable_with_arg(input, len, Z_OBJPROP_PP(zv), i, callback, silent, arg TSRMLS_CC); + } else if (Z_TYPE_PP(zv) == IS_ARRAY) { + phpdbg_parse_variable_with_arg(input, len, Z_ARRVAL_PP(zv), i, callback, silent, arg TSRMLS_CC); + } else { + /* Ignore silently */ + } + } + return ret; + } else if (new_index) { + char last_chr = last_index[index_len]; + last_index[index_len] = 0; + if (zend_symtable_find(parent, last_index, index_len + 1, (void **)&zv) == FAILURE) { + if (!silent) { - phpdbg_error("%.*s is undefined", (int)i, input); ++ phpdbg_error("variable", "type=\"undefined\" variable=\"%.*s\"", "%.*s is undefined", (int) i, input); + } + return FAILURE; + } + 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_PP(zv) == IS_OBJECT) { + parent = Z_OBJPROP_PP(zv); + } else if (Z_TYPE_PP(zv) == IS_ARRAY) { + parent = Z_ARRVAL_PP(zv); + } else { - phpdbg_error("%.*s is nor an array nor an object", (int)i, input); ++ 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("Malformed input"); ++ phpdbg_error("variable", "type=\"invalidinput\"", "Malformed input"); + return FAILURE; + } diff --cc phpdbg_utils.h index 42d2416,b54a1d4..0a677b4 --- a/phpdbg_utils.h +++ b/phpdbg_utils.h @@@ -47,37 -45,27 +47,37 @@@ enum }; #ifdef ZTS - PHPDBG_API int phpdbg_print(int TSRMLS_DC, FILE *, const char *, const char *, const char *, ...) PHP_ATTRIBUTE_FORMAT(printf, 6, 7); -PHPDBG_API int phpdbg_print(int severity TSRMLS_DC, int fd, const char *fmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 4, 5); ++PHPDBG_API int phpdbg_print(int severity TSRMLS_DC, int fd, const char *tag, const char *xmlfmt, const char *strfmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 6, 7); #else - PHPDBG_API int phpdbg_print(int TSRMLS_DC, FILE *, const char *, const char *, const char *, ...) PHP_ATTRIBUTE_FORMAT(printf, 5, 6); -PHPDBG_API int phpdbg_print(int severity TSRMLS_DC, int fd, const char *fmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4); ++PHPDBG_API int phpdbg_print(int severity TSRMLS_DC, int fd, const char *tag, const char *xmlfmt, const char *strfmt, ...) PHP_ATTRIBUTE_FORMAT(printf, 5, 6); #endif - PHPDBG_API int phpdbg_xml_internal(FILE *fp TSRMLS_DC, const char *fmt, ...); - PHPDBG_API int phpdbg_log_internal(FILE *fp TSRMLS_DC, const char *fmt, ...); - PHPDBG_API int phpdbg_out_internal(FILE *fp TSRMLS_DC, const char *fmt, ...); -PHPDBG_API int phpdbg_rlog(int fd, const char *fmt, ...); ++PHPDBG_API int phpdbg_xml_internal(int fd TSRMLS_DC, const char *fmt, ...); ++PHPDBG_API int phpdbg_log_internal(int fd TSRMLS_DC, const char *fmt, ...); ++PHPDBG_API int phpdbg_out_internal(int fd TSRMLS_DC, const char *fmt, ...); - PHPDBG_API int phpdbg_rlog(FILE *stream, const char *fmt, ...); -#define phpdbg_error(fmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, fmt, ##__VA_ARGS__) -#define phpdbg_notice(fmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, fmt, ##__VA_ARGS__) -#define phpdbg_writeln(fmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, fmt, ##__VA_ARGS__) -#define phpdbg_write(fmt, ...) phpdbg_print(P_WRITE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, fmt, ##__VA_ARGS__) -#define phpdbg_log(fmt, ...) phpdbg_print(P_LOG TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, fmt, ##__VA_ARGS__) ++PHPDBG_API int phpdbg_rlog(int fd, const char *fmt, ...); - #define phpdbg_error(tag, xmlfmt, strfmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], tag, xmlfmt, strfmt, ##__VA_ARGS__) - #define phpdbg_notice(tag, xmlfmt, strfmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], tag, xmlfmt, strfmt, ##__VA_ARGS__) - #define phpdbg_writeln(tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], tag, xmlfmt, strfmt, ##__VA_ARGS__) - #define phpdbg_write(tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], tag, xmlfmt, strfmt, ##__VA_ARGS__) - #define phpdbg_script(type, fmt, ...) phpdbg_print(type TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], NULL, NULL, fmt, ##__VA_ARGS__) - #define phpdbg_log(fmt, ...) phpdbg_log_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) - #define phpdbg_xml(fmt, ...) phpdbg_xml_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) - #define phpdbg_out(fmt, ...) phpdbg_out_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) -#define phpdbg_error_ex(out, fmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, out, fmt, ##__VA_ARGS__) -#define phpdbg_notice_ex(out, fmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, out, fmt, ##__VA_ARGS__) -#define phpdbg_writeln_ex(out, fmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, out, fmt, ##__VA_ARGS__) -#define phpdbg_write_ex(out, fmt, ...) phpdbg_print(P_WRITE TSRMLS_CC, out, fmt, ##__VA_ARGS__) -#define phpdbg_log_ex(out, fmt, ...) phpdbg_print(P_LOG TSRMLS_CC, out, fmt, ##__VA_ARGS__) ++#define phpdbg_error(tag, xmlfmt, strfmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, tag, xmlfmt, strfmt, ##__VA_ARGS__) ++#define phpdbg_notice(tag, xmlfmt, strfmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, tag, xmlfmt, strfmt, ##__VA_ARGS__) ++#define phpdbg_writeln(tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, tag, xmlfmt, strfmt, ##__VA_ARGS__) ++#define phpdbg_write(tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, tag, xmlfmt, strfmt, ##__VA_ARGS__) ++#define phpdbg_script(type, fmt, ...) phpdbg_print(type TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT].fd, NULL, NULL, fmt, ##__VA_ARGS__) ++#define phpdbg_log(fmt, ...) phpdbg_log_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) ++#define phpdbg_xml(fmt, ...) phpdbg_xml_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) ++#define phpdbg_out(fmt, ...) phpdbg_out_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) + +#define phpdbg_error_ex(out, tag, xmlfmt, strfmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, out, tag, xmlfmt, strfmt, ##__VA_ARGS__) +#define phpdbg_notice_ex(out, tag, xmlfmt, strfmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, out, tag, xmlfmt, strfmt, ##__VA_ARGS__) +#define phpdbg_writeln_ex(out, tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, out, tag, xmlfmt, strfmt, ##__VA_ARGS__) +#define phpdbg_write_ex(out, tag, xmlfmt, strfmt, ...) phpdbg_print(P_WRITE TSRMLS_CC, out, tag, xmlfmt, strfmt, ##__VA_ARGS__) +#define phpdbg_script_ex(out, type, fmt, ...) phpdbg_print(type TSRMLS_CC, out, NULL, NULL, fmt, ##__VA_ARGS__) - #define phpdbg_log_ex(out, fmt, ...) phpdbg_log_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) - #define phpdbg_xml_ex(out, fmt, ...) phpdbg_xml_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) - #define phpdbg_out_ex(out, fmt, ...) phpdbg_out_internal(PHPDBG_G(io)[PHPDBG_STDOUT] TSRMLS_CC, fmt, ##__VA_ARGS__) ++#define phpdbg_log_ex(out, fmt, ...) phpdbg_log_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) ++#define phpdbg_xml_ex(out, fmt, ...) phpdbg_xml_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) ++#define phpdbg_out_ex(out, fmt, ...) phpdbg_out_internal(PHPDBG_G(io)[PHPDBG_STDOUT].fd TSRMLS_CC, fmt, ##__VA_ARGS__) #if PHPDBG_DEBUG - # define phpdbg_debug(fmt, ...) phpdbg_log_ex(PHPDBG_G(io)[PHPDBG_STDERR] TSRMLS_CC, fmt, ##__VA_ARGS__) -# define phpdbg_debug(fmt, ...) phpdbg_print(P_LOG TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDERR].fd, fmt, ##__VA_ARGS__) ++# define phpdbg_debug(fmt, ...) phpdbg_log_ex(PHPDBG_G(io)[PHPDBG_STDERR].fd TSRMLS_CC, fmt, ##__VA_ARGS__) #else # define phpdbg_debug(fmt, ...) #endif diff --cc phpdbg_wait.c index 0000000,e2bded0..98dd726 mode 000000,100644..100644 --- a/phpdbg_wait.c +++ b/phpdbg_wait.c @@@ -1,0 -1,416 +1,416 @@@ + /* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Bob Weinand <bwoebi@php.net> | + +----------------------------------------------------------------------+ + */ + + #include "phpdbg_wait.h" + #include "phpdbg_prompt.h" + #include "ext/json/JSON_parser.h" + #include "ext/standard/basic_functions.h" + + ZEND_EXTERN_MODULE_GLOBALS(phpdbg); + ZEND_EXTERN_MODULE_GLOBALS(json); + + static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_DC) { + zval **zvpp; + if (PG(http_globals)[type]) { + zval_dtor(PG(http_globals)[type]); + } + if (zend_hash_find(&EG(symbol_table), name, strlen(name) + 1, (void **) &zvpp) == SUCCESS) { + Z_SET_REFCOUNT_PP(zvpp, 2); + PG(http_globals)[type] = *zvpp; + } + } + + + static int phpdbg_dearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { + if (auto_global->name_len != sizeof("GLOBALS") - 1 || memcmp(auto_global->name, "GLOBALS", sizeof("GLOBALS") - 1)) { + auto_global->armed = 0; + } + + return ZEND_HASH_APPLY_KEEP; + } + + typedef struct { + HashTable *ht[2]; + HashPosition pos[2]; + } phpdbg_intersect_ptr; + + static int phpdbg_array_data_compare(const void *a, const void *b TSRMLS_DC) { + Bucket *f, *s; + zval result; + zval *first, *second; + + f = *((Bucket **) a); + s = *((Bucket **) b); + + first = *((zval **) f->pData); + second = *((zval **) s->pData); + + if (string_compare_function(&result, first, second TSRMLS_CC) == FAILURE) { + return 0; + } + + if (Z_LVAL(result) < 0) { + return -1; + } else if (Z_LVAL(result) > 0) { + return 1; + } + + return 0; + } + + static void phpdbg_array_intersect_init(phpdbg_intersect_ptr *info, HashTable *ht1, HashTable *ht2 TSRMLS_DC) { + info->ht[0] = ht1; + info->ht[1] = ht2; + + zend_hash_sort(info->ht[0], zend_qsort, (compare_func_t) phpdbg_array_data_compare, 0 TSRMLS_CC); + zend_hash_sort(info->ht[1], zend_qsort, (compare_func_t) phpdbg_array_data_compare, 0 TSRMLS_CC); + + zend_hash_internal_pointer_reset_ex(info->ht[0], &info->pos[0]); + zend_hash_internal_pointer_reset_ex(info->ht[1], &info->pos[1]); + } + + /* -1 => first array, 0 => both arrays equal, 1 => second array */ + static int phpdbg_array_intersect(phpdbg_intersect_ptr *info, zval ***ptr) { + int ret; + zval **zvpp[2]; + int invalid = !info->ht[0] + !info->ht[1]; + + if (invalid > 0) { + invalid = !info->ht[0]; + + if (zend_hash_get_current_data_ex(info->ht[invalid], (void **) ptr, &info->pos[invalid]) == FAILURE) { + *ptr = NULL; + return 0; + } + + zend_hash_move_forward_ex(info->ht[invalid], &info->pos[invalid]); + + return invalid ? 1 : -1; + } + + if (zend_hash_get_current_data_ex(info->ht[0], (void **) &zvpp[0], &info->pos[0]) == FAILURE) { + info->ht[0] = NULL; + return phpdbg_array_intersect(info, ptr); + } + if (zend_hash_get_current_data_ex(info->ht[1], (void **) &zvpp[1], &info->pos[1]) == FAILURE) { + info->ht[1] = NULL; + return phpdbg_array_intersect(info, ptr); + } + + ret = zend_binary_zval_strcmp(*zvpp[0], *zvpp[1]); + + if (ret <= 0) { + *ptr = zvpp[0]; + zend_hash_move_forward_ex(info->ht[0], &info->pos[0]); + } + if (ret >= 0) { + *ptr = zvpp[1]; + zend_hash_move_forward_ex(info->ht[1], &info->pos[1]); + } + + return ret; + } + + void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { + zval *free_zv = NULL; + zval zv, **zvpp; + HashTable *ht; + php_json_decode(&zv, msg, len, 1, 1000 /* enough */ TSRMLS_CC); + + if (JSON_G(error_code) != PHP_JSON_ERROR_NONE) { - phpdbg_error("Malformed JSON was sent to this socket, arborting"); ++ phpdbg_error("wait", "type=\"invaliddata\" import=\"fail\"", "Malformed JSON was sent to this socket, arborting"); + return; + } + + ht = Z_ARRVAL(zv); + + /* Reapply symbol table */ + if (zend_hash_find(ht, "GLOBALS", sizeof("GLOBALS"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + { + zval **srv; + if (zend_hash_find(Z_ARRVAL_PP(zvpp), "_SERVER", sizeof("_SERVER"), (void **) &srv) == SUCCESS && Z_TYPE_PP(srv) == IS_ARRAY) { + zval **script; + if (zend_hash_find(Z_ARRVAL_PP(srv), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &script) == SUCCESS && Z_TYPE_PP(script) == IS_STRING) { + phpdbg_param_t param; + param.str = Z_STRVAL_PP(script); + PHPDBG_COMMAND_HANDLER(exec)(&param TSRMLS_CC); + } + } + } + + PG(auto_globals_jit) = 0; + zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_dearm_autoglobals TSRMLS_CC); + + zend_hash_clean(&EG(symbol_table)); + EG(symbol_table) = *Z_ARRVAL_PP(zvpp); + + /* Rebuild cookies, env vars etc. from GLOBALS (PG(http_globals)) */ + phpdbg_rebuild_http_globals_array(TRACK_VARS_POST, "_POST" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_GET, "_GET" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_COOKIE, "_COOKIE" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_SERVER, "_SERVER" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_ENV, "_ENV" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_FILES, "_FILES" TSRMLS_CC); + + Z_ADDREF_PP(zvpp); + free_zv = *zvpp; + } + + if (zend_hash_find(ht, "input", sizeof("input"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (SG(request_info).request_body) { + php_stream_close(SG(request_info).request_body); + } + SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); + php_stream_truncate_set_size(SG(request_info).request_body, 0); + php_stream_write(SG(request_info).request_body, Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); + } + + if (zend_hash_find(ht, "cwd", sizeof("cwd"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (VCWD_CHDIR(Z_STRVAL_PP(zvpp)) == SUCCESS) { + if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) { + efree(BG(CurrentStatFile)); + BG(CurrentStatFile) = NULL; + } + if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) { + efree(BG(CurrentLStatFile)); + BG(CurrentLStatFile) = NULL; + } + } + } + + if (zend_hash_find(ht, "sapi_name", sizeof("sapi_name"), (void **) &zvpp) == SUCCESS && (Z_TYPE_PP(zvpp) == IS_STRING || Z_TYPE_PP(zvpp) == IS_NULL)) { + if (PHPDBG_G(sapi_name_ptr)) { + free(PHPDBG_G(sapi_name_ptr)); + } + if (Z_TYPE_PP(zvpp) == IS_STRING) { + PHPDBG_G(sapi_name_ptr) = sapi_module.name = strdup(Z_STRVAL_PP(zvpp)); + } else { + PHPDBG_G(sapi_name_ptr) = sapi_module.name = NULL; + } + } + + if (zend_hash_find(ht, "modules", sizeof("modules"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + phpdbg_intersect_ptr pos; + zval **module; + zend_module_entry *mod; + HashTable zv_registry; + + /* intersect modules, unregister modules loaded "too much", announce not yet registered modules (phpdbg_notice) */ + + zend_hash_init(&zv_registry, zend_hash_num_elements(&module_registry), 0, ZVAL_PTR_DTOR, 0); + for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); + zend_hash_get_current_data_ex(&module_registry, (void **) &mod, &position) == SUCCESS; + zend_hash_move_forward_ex(&module_registry, &position)) { + if (mod->name) { + zval **value = emalloc(sizeof(zval *)); + MAKE_STD_ZVAL(*value); + ZVAL_STRING(*value, mod->name, 1); + zend_hash_next_index_insert(&zv_registry, value, sizeof(zval *), NULL); + } + } + + phpdbg_array_intersect_init(&pos, &zv_registry, Z_ARRVAL_PP(zvpp) TSRMLS_CC); + do { + int mode = phpdbg_array_intersect(&pos, &module); + if (mode < 0) { + // loaded module, but not needed + if (strcmp(PHPDBG_NAME, Z_STRVAL_PP(module))) { + zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); + } + } else if (mode > 0) { + // not loaded module + if (!sapi_module.name || strcmp(sapi_module.name, Z_STRVAL_PP(module))) { - phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module/%.*s.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module), Z_STRLEN_PP(module), Z_STRVAL_PP(module)); ++ phpdbg_notice("wait", "missingmodule=\"%.*s\"", "The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module/%.*s.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module), Z_STRLEN_PP(module), Z_STRVAL_PP(module)); + } + } + } while (module); + + zend_hash_clean(&zv_registry); + } + + if (zend_hash_find(ht, "extensions", sizeof("extensions"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + zend_extension *extension; + zend_llist_position pos; + HashPosition hpos; + zval **name, key; + + extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); + while (extension) { + extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); + + /* php_serach_array() body should be in some ZEND_API function */ + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { + if (Z_TYPE_PP(name) == IS_STRING && !zend_binary_strcmp(extension->name, strlen(extension->name), Z_STRVAL_PP(name), Z_STRLEN_PP(name))) { + break; + } + } + + if (zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &zvpp, &hpos) == FAILURE) { + /* sigh, breaking the encapsulation, there aren't any functions manipulating the llist at the place of the zend_llist_position */ + zend_llist_element *elm = pos; + if (elm->prev) { + elm->prev->next = elm->next; + } else { + zend_extensions.head = elm->next; + } + if (elm->next) { + elm->next->prev = elm->prev; + } else { + zend_extensions.tail = elm->prev; + } + #if ZEND_EXTENSIONS_SUPPORT + if (extension->shutdown) { + extension->shutdown(extension); + } + #endif + if (zend_extensions.dtor) { + zend_extensions.dtor(elm->data); + } + pefree(elm, zend_extensions.persistent); + zend_extensions.count--; + } else { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &hpos); + if (Z_TYPE(key) == IS_LONG) { + zend_hash_index_del(Z_ARRVAL_PP(zvpp), Z_LVAL(key)); + } + } + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { - phpdbg_notice("The (zend) extension %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/extension.so", Z_STRLEN_PP(name), Z_STRVAL_PP(name)); ++ phpdbg_notice("wait", "missingextension=\"%.*s\"", "The Zend extension %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/extension.so", Z_STRLEN_PP(name), Z_STRVAL_PP(name)); + } + } + } + + zend_ini_deactivate(TSRMLS_C); + + if (zend_hash_find(ht, "systemini", sizeof("systemini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zend_ini_entry *original_ini; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + if (zend_hash_find(EG(ini_directives), Z_STRVAL(key), Z_STRLEN(key) + 1, (void **) &original_ini) == SUCCESS) { + if (!original_ini->on_modify || original_ini->on_modify(original_ini, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), original_ini->mh_arg1, original_ini->mh_arg2, original_ini->mh_arg3, ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) == SUCCESS) { + if (original_ini->modified && original_ini->orig_value != original_ini->value) { + efree(original_ini->value); + } + original_ini->value = Z_STRVAL_PP(ini_entry); + original_ini->value_length = Z_STRLEN_PP(ini_entry); + Z_TYPE_PP(ini_entry) = IS_NULL; /* don't free the value */ + } + } + } + efree(Z_STRVAL(key)); + } + } + } + + if (zend_hash_find(ht, "userini", sizeof("userini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + zend_alter_ini_entry_ex(Z_STRVAL(key), Z_STRLEN(key) + 1, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), ZEND_INI_PERDIR, ZEND_INI_STAGE_HTACCESS, 1 TSRMLS_CC); + } + efree(Z_STRVAL(key)); + } + } + } + + zval_dtor(&zv); + if (free_zv) { + /* separate freeing to not dtor the symtable too, just the container zval... */ + efree(free_zv); + } + + /* Reapply raw input */ + /* ??? */ + } + + PHPDBG_COMMAND(wait) /* {{{ */ + { + struct sockaddr_un local, remote; + int rlen, sr, sl; + unlink(PHPDBG_G(socket_path)); + if (PHPDBG_G(socket_server_fd) == -1) { + int len; + PHPDBG_G(socket_server_fd) = sl = socket(AF_UNIX, SOCK_STREAM, 0); + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, PHPDBG_G(socket_path)); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(sl, (struct sockaddr *)&local, len) == -1) { - phpdbg_error("Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting", PHPDBG_G(socket_path)); ++ phpdbg_error("wait", "type=\"nosocket\" import=\"fail\"", "Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting", PHPDBG_G(socket_path)); + return FAILURE; + } + + chmod(PHPDBG_G(socket_path), 0666); + + listen(sl, 2); + } else { + sl = PHPDBG_G(socket_server_fd); + } + + rlen = sizeof(remote); + sr = accept(sl, (struct sockaddr *) &remote, (socklen_t *) &rlen); + + char msglen[5]; + int recvd = 4; + + do { + recvd -= recv(sr, &(msglen[4 - recvd]), recvd, 0); + } while (recvd > 0); + + recvd = *(size_t *) msglen; + char *data = emalloc(recvd); + + do { + recvd -= recv(sr, &(data[(*(int *) msglen) - recvd]), recvd, 0); + } while (recvd > 0); + + phpdbg_webdata_decompress(data, *(int *) msglen TSRMLS_CC); + + if (PHPDBG_G(socket_fd) != -1) { + close(PHPDBG_G(socket_fd)); + } + PHPDBG_G(socket_fd) = sr; + + efree(data); + - phpdbg_notice("Successfully imported request data, stopped before executing"); ++ phpdbg_notice("wait", "import=\"success\"", "Successfully imported request data, stopped before executing"); + + return SUCCESS; + } /* }}} */ + diff --cc phpdbg_watch.c index 128a9e7,ee937f9..be37a96 --- a/phpdbg_watch.c +++ b/phpdbg_watch.c @@@ -30,7 -30,7 +30,6 @@@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg); -- typedef struct { void *page; size_t size; diff --cc xml.md index 15920d2,0000000..83bd364 mode 100644,000000..100644 --- a/xml.md +++ b/xml.md @@@ -1,595 -1,0 +1,649 @@@ +phpdbg XML format +================= + +Common attributes +================= + +severity +-------- + +- indicates the genre of phpdbg system output +- usually one of these values: + - normal + - notice + - error + +msgout +------ + +- text message output related to the xml data (e.g. &lt;intro severity="normal" help="help" msgout="To get help using phpdbg type &amp;quot;help&amp;quot; and press enter" />) + ++req ++--- ++ ++- the request id, if one was passed to the last command (via -r %d, where %d is the id) (and the output is related to that message) ++ +file +---- + +- refers to a filename + +method +------ + +- format classname::methodname +- refers to a method + +function +-------- + +- refers to a function + +symbol +------ + +- either function or method (is method if "::" are present) + +opline +------ + +- in hexadecimal format +- refers to a specific pointer to a (zend_)op + +opcode +------ + +- refers to an opcode (ZEND_*) + +type +---- + +- general attribute for most errors, describes the genre of the error + +General tags +============ + +intro +----- + +- appears on startup if -q flag wasn't provided as command line arg +- before any input possibility +- attributes may be spread over multiple tags + +### attributes ### + +- version: current phpdbg version (as string) +- help: command name for help +- report: URL for bug reporting + + +phpdbg +------ + +- general text message output from phpdbg system + +stream +------ + +- any output by PHP itself (e.g. &lt;stream type="stdout">test&lt;/stream>) + +### attributes ### + +- type: stderr or stdout + +php +--- + +- php error output + +### attributes ### + +- msg: the error message + + +General error tags +================== + +command +------- + +- general errors about commands + +### possible attributes ### + +- type + - toomanyargs: more arguments than allowed + - noarg: argument missing + - wrongarg: wrong type of argument (e.g. letters instead of integer) + - toofewargs: not enough arguments + - notfound: command (or subcommand) doesn't exist + - ambiguous: command was ambiguous + - invalidcommand: command input is totally invalid + - (nostack: should not happen: is an internal error) + - (emptystack: should not happen: is an internal error) +- command: passed command +- subcommand: passed subcommand (present if the error is related to the subcommand) +- expected: count of expected arguments +- got: type of argument for type "wrongarg" +- num: if possible, information about which parameter had a wrong argument + +inactive +-------- + +- by type + - op_array: nothing was yet compiled (probably because no execution context set) + - symbol_table: no symbol table present (not yet initiailized or already destructed) + - noexec: not in execution + - memory_manager: using the native memory manager (malloc, free, realloc) instead of e.g. the Zend MM + - notfound: file not found + - nocontext: execution context was not set (or compilation had failed) + - isrunning: command requires no running script + + +Commands +======== + +export +------ + +- tag: &lt;exportbreakpoint /> +- usually triggered by successful export command +- may appear when cleaning to temporary store breakpoints +- errors by type + - openfailure: could not create file + +### attributes ### + +- count: number of exported breakpoints + +break / info break +------------------ + +- General tag for breakpoint creation, deletion and hits is "&lt;breakpoint />" + +### possible attributes ### + +- id: the breakpoint id (if the leave command was executed, the id has the value "leave") +- num: the nth opline of a function/method/file +- add: has value "success"/"fail": a brekpoint was successfully/not added +- pending: the breakpoint is waiting for resolving (e.g. a file opline on a not yet loaded file) +- deleted: has value "success"/"fail": a breakpoint was successfully/not deleted +- eval: the condition on conditional breakpoints +- file +- opline +- opcode +- symbol +- function +- method +- line + + +- listing breakpoints always in a container element "&lt;breakpoints>" + - Child nodes: + - function + - method + - file + - opline + - methodopline + - functionopline + - fileopline + - evalfunction + - evalfunctionopline + - evalmethod + - evalmethodopline + - evalfile + - evalopline + - eval + - opcode + - attributes: + - name: name of the symbol (function/method/file/opcode) + - disabled: empty value if enabled, non-empty if enabled + +- errors (by type) + - exists: the breakpoint already exists + - maxoplines: tries to break at an opline (usedoplinenum) higher than the number of present oplines (in maxoplinenum) + - nomethod: method doesn't exist + - internalfunction: one cannot break on an opline of an internal function + - notregular: tries to set a breakpoint in not a regular file + - (invalidparameter: should not happen: is an internal error) + +frame +----- + +- General tag for frames is "&lt;frame>" +- always has id attribute; if it only has id attribute, it just indicates current frame number, no other elements follow +- may contain other elements (of type &lt;arg>) when contained in &lt;backtrace> tag +- &lt;arg> always contains a &lt;stream> element, the value of the variable + +### possible attributes ### + +- id: the frame id, current frame has id 0 (frames with internal function calls have the same id than their called frame) +- symbol ("{main}" is root frame) +- file +- line +- internal: has value "internal" when being an internal function call (one cannot inspect that frame) + +- being an error: (by type) + - maxnum: tried to access a frame with a number heigher than existing (or &lt; 0) + +### attributes on &lt;arg> ### - ++v +- variadic: has a non-empty value if the argument is variadic +- name: variable name of parameter + +info (subcommands) +------------------ + +### break ### + +- See above ("break / info break") + +### files ### + +- lists included files +- &lt;includedfileinfo num="" /> with num having an integer value, indicating the number of included files +- &lt;includedfile name=""/>: one per file, with name being the file path of the included file + +### error ### + +- gets last error +- &lt;lasterror error="" (file="" line="") /> +- error attribute contains the last error as a string, is empty if there's no last error + +### vars / globals ### + +- &lt;variableinfo num="" /> with num having an integer value, indicating the number of (local or superglobal) variables +- if info vars was used it'll have also one of these attributes: + - method + - function + - file + - opline +- for each variable there is a &lt;variable> followed by a &lt;variabledetails> element +- &lt;variable address="" refcount="" type="" /> + - address: pointer to zval (hexadecimal) + - refcount: refcount of zval + - type: the variable type (long, string, ...). If the value is "unknown", the other attributes are meaningless +- &lt;variabledetails name="" /> with name the name of the variable, optional args are: + - refstatus: empty if the zval is not a reference + - class: the class the object in the zval is an instance of + - resource: the type of the resource in the zval + +### literal ### + +- &lt;literalinfo num="" /> with num having an integer value, indicating the number of literals, optional arguments are: + - method + - function + - file + - opline +- for each literal there is a &lt;literal> followed by a &lt;stream type="stdout"> which prints the value of the literal +- &lt;literal id="" />: where id is the internal identifier of the literal + +### memory ### + +- Format: + + &lt;meminfo /> + &lt;current /> + &lt;used mem="" /> + &lt;real mem="" /> + &lt;peak /> + &lt;used mem="" /> + &lt;real mem="" /> + +- mem is an attribute whose value is a float. The memory is given in kilobytes (1 kB == 1024 bytes) + +### classes ### + +- &lt;classinfo num="" /> with num having an integer value, indicating the number of loaded user-defined classes +- Each class is enumerated with first a &lt;class>, then an optional &lt;parents> container and then a &lt;classsource> element +- The &lt;parents> container contains the &lt;class> elements of the parent of the last &lt;class> element. +- &lt;class type="" flags="" name="" methodcount="" /> + - type: either "User" or "Internal" + - flags: either "Interface", "Class" or "Abstract Class" +- &lt;classsource /> where the class was defined, if there are no attributes, location is unknown, usually defined by + - file + - line + +### funcs ### + +- &lt;functioninfo num="" /> with num having an integer value, indicating the number of loaded user-defined functions +- Each class is enumerated with first a &lt;function> and then a &lt;functionsource> element +- &lt;function name="" /> +- &lt;functionsource /> where the function was defined, if there are no attributes, location is unknown, usually defined by + - file + - line + +list +---- + +- consists of &lt;line> elements wrapped in a &lt;list> container +- &lt;list file=""> is the container element with file being the filename +- &lt;line line="" code="" /> with value of code being the whole line of code in the line specified in the line attribute + - current: this attribute is set to "current" if that line is the line where the executor currently is + +print +----- + +### without a subcommand ### + +- &lt;print> elements are wrapped in a &lt;printinfo> element +- there may be a variable number of &lt;print> elements with a variable count of args inside the &lt;printinfo> element +- possible args are: + - readline: yes/no - readline enabled or disabled + - libedit: yes/no - libedit enabled or disabled + - context: current executing context + - compiled: yes/no - are there actual compiled ops? + - stepping: @@ TODO (meaningless for now) @@ + - quiet: on/off - should it always print the opline being currently executed? + - oplog: on/off - are oplines logged in a file? + - ops: number of opcodes in current executing context + - vars: number of compiled variables (CV) + - executing: yes/no - in executor? + - vmret: the return value of the last executed opcode + - default: continue + - 1: return from vm + - 2: enter stack frame + - 3: leave stack frame + - classes: number of classes + - functions: number of functions + - constants: number of constants + - includes: number of included files + +### with a subcommand ### + +- introduced by &lt;printinfo num="" /> (except for print opline) with num being the number of opcodes and one of these args: + - file + - method + - function + - class (then also type and flags attributes, see info classes command for their meanings) + - symbol (also type and flags attributes; here the value of flags is either "Method" or "Function") +- if there is a class method, the methods are all wrapped in a &lt;printmethods> container +- then comes a &lt;printoplineinfo type="" /> where type is either "User" or "Internal" +- the &lt;printoplineinfo> has either a method or a function attribute +- if the type is "Internal" + - there are no oplines, it's an internal method or function +- if the type is "User" + - it has these attributes + - startline: the first line of code where the method or function is defined + - endline: the lastt line of code where the method or function is defined + - file: the file of code where the method or function is defined + - is followed by the oplines of that method or function (&lt;print> elements) +- &lt;print line="%u" opline="%p" opcode="%s" op="%s" /> +- in case of print opline it emits a single &lt;opline line="" opline="" opcode="" op="" file="" /> + +exec +---- + +- command executing and compiling a given file + - &lt;exec type="unset" context="" />: indicates unsetting of the old context + - &lt;exec type="unsetops" />: indicates unsetting of the old compiled opcodes + - &lt;exec type="unchanged" />: same execution context choosen again + - &lt;exec type="set" context="" />: indicates setting of the new context +- errors by tag + - &lt;compile> + - openfailure: couldn't open file + - compilefailure: The file indicated in context couldn't be compiled + - &lt;exec> + - invalid: given context (attribute) is not matching a valid file or symlink + - notfound: given context (attribute) does not exist + - run / <stop> tag - ---------------- ++run / &lt;stop> tag ++------------------- + +- runs the script (set via exec command) +- &lt;stop type="end" />: script execution ended normally +- (error) &lt;stop type="bailout" /> the VM bailed out (usually because there was some error) +- compile failures see under exec, errors, &lt;compile> + +step +---- + +- steps by one line or opcode (as defined via set stepping) default is one line +- returns back to the executor + +continue +-------- + +- returns back to the executor + +until +----- + +- temporarily disables all the breakpoints on that line until that line was left once +- returns back to the executor + +finish +------ + +- temporarily disables all the breakpoints until the end of the current frame +- returns back to the executor + +leave +------ + +- temporarily disables all the breakpoints past the end of the current frame and then stops +- returns back to the executor + +back +---- + +- prints backtrace +- see frame command + +ev +-- + +- eval()uates some code +- output wrapped in &lt;eval> tags + +sh +-- + +- executes shell command +- still pipes to stdout ... without wrapping &lt;stream> !!! (@@ TODO @@) + +source +------ + +- executes a file in .phpdbginit format +- errors by type + - notfound: file not found + +register +-------- + +- registers a function to be used like a command +- &lt;register function="" />: successfully registered function +- errors by type + - notfound: no such function + - inuse: function already registered + +quit +---- + +- quits phpdbg +- if successful connection will be closed... + +clean +----- + +- cleans environment (basically a shutdown + new startup) +- &lt;clean> tags wrapped in a &lt;cleaninfo> container +- possible attributes of &lt;clean> tag + - classes: number of classes + - functions: number of functions + - constants: number of constants + - includes: number of included files + +clear +----- + +- removes all breakpoints +- &lt;clear> tags wrapped in a &lt;clearinfo> container +- possible attributes of &lt;clear> tag (value is always the number of defined breakpoints of that type) + - files + - functions + - methods + - oplines + - fileoplines + - functionoplines + - methodoplines + - eval + +watch +----- + +- watchpoints generally are identified by a variable (one may need to switch frames first...) +- &lt;watch variable="" />, &lt;watchrecursive variable="" /> and &lt;watcharray variable="" /> (normal, array, recursive) +- &lt;watch> if error, by type: + - undefined: tried to set a watchpoint on a not (yet) defined variable + - notiterable: element which is tried to be accessed as an object or array is nor array nor object + - invalidinput: generally malformed input +- &lt;watchdelete variable="" />: when "watch delete" was used on a watchpoint +- (error) &lt;watchdelete type="nowatch" />: that watchpoint doesn't exist, so couldn't be deleted +- for hit watchpoints etc., see Other tags, &lt;watch*> +- when using watch list, &lt;watchvariable> elements are wrapped in a &lt;watchlist> container + - &lt;watchvariable variable="" on="" type="" /> + - variable: watched variable (may be a variable of another scope!) + - on: values are array or variable, depending on what is watched + - type: values are recursive or simple, depending on whether the watchpoint is checked recursively or not + +set +--- + +- a general error is type="wrongargs" where a wrong argument was passed to a subcommand; tag is then &lt;set*> + +### prompt ### + +- without other args, a &lt;setpromt str="" /> tag is emitted where the value of the str attribue is the value of the prompt +- when there is another arg, the prompt is changed to that arg, no further xml answer + +### break ### + +- enables / disables a given breakpoint silently with no further xml answer +- if the boolean switch is omitted, it emits current state in a &lt;setbreak id="" active="" /> where active is on or off +- error with type="nobreak", when no breakpoint with the given id exists + +### breaks ### + +- generally enables / disables breakpoint functionality silently with no futher xml answer +- if the boolean switch is omitted, it emits current state in a &lt;setbreaks active="" /> where active is on or off + +### color ### + +- sets the color on prompt, error or notices +- &lt;setcolor type="" color="" code="" />: code is the color code of color, type is either: + - prompt + - error + - notice +- errors by type: + - nocolor: color doesn't exist + - invalidtype: type wasn't one of the three allowed types + +### colors ### + +- generally enables / disables colors silently with no further xml answer +- if the boolean switch is omitted, it emits current state in a &lt;setcolors active="" /> where active is on or off + +### oplog ### + +- sets oplog +- (error) &lt;setoplog type="openfailure" file="" /> when it couldn't open the passed file path +- &lt;setoplog type="closingold" /> is emitted when there was a previous open oplog (and a file is passed) +- if no further argument is passed, it emits current state in a &lt;setoplog active="" /> where active is on or off + +### quiet ### + +- generally enables / disables quietness silently with no further xml answer +- if the boolean switch is omitted, it emits current state in a &lt;setquiet active="" /> where active is on or off + +### setpping ### + +- sets stepping to either opcode or line (so a step command will either advance one op or one line) +- if no further argument is passed, it emits current state in a &lt;setoplog type="" /> where active is opcode or line + +### refcount ### + +- generally enables / disables showing of refcount in watchpoint breaks silently with no further xml answer +- if the boolean switch is omitted, it emits current state in a &lt;setrefcount active="" /> where active is on or off + ++wait ++---- ++ ++- internally executes exec, so exec will output first (if binding to socket worked) ++ ++### attributes ### ++ ++- import: has value "success"/"fail" ++- missingmodule/missingextension: modules/extensions loaded in the target SAPI, but not in phpdbg ++ ++### errors (by type) ### ++ ++- nosocket: couldn't establish socket ++- invaliddata: invalid JSON passed to socket ++ ++dl ++-- ++ ++- loads a module or Zend extension at a given path ++- if a relative path is passed, it's relative to the extension_dir ini setting ++ ++### attributes ### ++ ++- extensiontype: "Zend extension" or "module" ++- name: the extension name ++- path: the path where it was loaded ++ ++### errors (by type) ### ++ ++- unsupported: dynamic extension loading is unsupported ++- relpath: relative path given, but no extension_dir defined ++- unknown: general error with internal DL_LOAD() (for message see msg attribute) ++- wrongapi: wrong Zend engine version (apineeded / apiinstalled attributes give information about the API versions) ++- wrongbuild: unmatched build versions (buildneeded / buildinstalled attributes give information about the build versions) ++- registerfailure: registering module failed ++- startupfailure: couldn't startup Zend extension / module ++- initfailure: couldn't initialize module ++- nophpso: passed shared object is not a valid Zend extension nor module ++ ++- errors may have the module or extension attribute when their name is already known at the point of failure + +Other tags +========== + +&lt;signal> - -------- ++----------- + +- received caught signal + +### attributes ### + - - type: type of signal ++- type: type of signal (e.g. SIGINT) ++ ++### by type ### ++ ++- SIGINT: interactive mode is entered... + +&lt;watch*> - -------- ++----------- + +- generally emitted on hit watchpoint +- &lt;watchdelete variable="" />: when a variable was unset, the watchpoint is removed too +- &lt;watchhit variable="" />: when ever a watched variable is changed, followed by a &lt;watchdata> container +- &lt;watchdata> may contain + - for watchpoints on variables: + - each of these &lt;watch*> tags conatins a type attribute whose value is either "old" or "new") + - &lt;watchvalue type="" inaccessible="inaccessible" />: old value is inaccessible + - &lt;watchvalue type=""> may contain a &lt;stream> element which indicates the old/new (type attribute) value of the variable + - &lt;watchrefcount type="" refcount="" isref="" />: old/new (type attribute) refcount and isref, both numbers + - isref: if the value is 0, it's not a reference, else it is one + - for watchpoints on arrays: + - &lt;watchsize> inspects size variations of an array (the sum): + - removed: number of elements removed + - added: number of elements added + - &lt;watcharrayptr>: if this tag appears, the internal pointer of the array way changed ++ ++&lt;signalsegv> ++--------------- ++ ++- generally emitted when data couldn't be fetched (e.g. by accessing inconsistent data); only used in hard interrupt mode
« previous php.cvs (#82570) next »