From defc4c318022cb8d56d38b90dbbd55a195c8529e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 10 Nov 2013 16:06:41 -0200 Subject: [PATCH] - Added separated file to handle breakpoint feature --- config.m4 | 2 +- phpdbg.h | 6 +- phpdbg_bp.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++ phpdbg_bp.h | 39 +++++++++++++ phpdbg_help.h | 2 + phpdbg_prompt.c | 121 ++------------------------------------- phpdbg_prompt.h | 17 ------ 7 files changed, 200 insertions(+), 134 deletions(-) create mode 100644 phpdbg_bp.c create mode 100644 phpdbg_bp.h diff --git a/config.m4 b/config.m4 index b62d099ad40..4f98bb92bf7 100644 --- a/config.m4 +++ b/config.m4 @@ -9,7 +9,7 @@ if test "$PHP_PHPDBG" != "no"; then AC_DEFINE(HAVE_PHPDBG, 1, [ ]) PHP_PHPDBG_CFLAGS=-I$abs_srcdir/sapi/phpdbg - PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c phpdbg_help.c" + PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c phpdbg_help.c phpdbg_bp.c" PHP_ADD_MAKEFILE_FRAGMENT([$abs_srcdir/sapi/phpdbg/Makefile.frag]) PHP_SELECT_SAPI(phpdbg, program, $PHP_PHPDBG_FILES, $PHP_PHPDBG_CFLAGS, [$(SAPI_PHPDBG_PATH)]) diff --git a/phpdbg.h b/phpdbg.h index 427d22aa968..437fe8a2441 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -15,6 +15,10 @@ | Authors: Joe Watkins | +----------------------------------------------------------------------+ */ + +#ifndef PHPDBG_H +#define PHPDBG_H + #include "php.h" #include "php_globals.h" #include "php_variables.h" @@ -54,4 +58,4 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) zend_bool quitting; /* quitting flag */ ZEND_END_MODULE_GLOBALS(phpdbg) -#include "phpdbg_prompt.h" +#endif /* PHPDBG_H */ diff --git a/phpdbg_bp.c b/phpdbg_bp.c new file mode 100644 index 00000000000..a45ab164f7e --- /dev/null +++ b/phpdbg_bp.c @@ -0,0 +1,147 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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: Felipe Pena | + | Authors: Joe Watkins | + +----------------------------------------------------------------------+ +*/ + +#include "zend.h" +#include "zend_hash.h" +#include "phpdbg.h" +#include "phpdbg_bp.h" + +ZEND_EXTERN_MODULE_GLOBALS(phpdbg); + +static void phpdbg_llist_breakfile_dtor(void *data) /* {{{ */ +{ + phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) data; + + efree((char*)bp->filename); +} /* }}} */ + +static void phpdbg_llist_breaksym_dtor(void *data) /* {{{ */ +{ + phpdbg_breaksymbol_t *bp = (phpdbg_breaksymbol_t*) data; + + efree((char*)bp->symbol); +} /* }}} */ + +void phpdbg_set_breakpoint_file(const char *expr, const char *line_pos TSRMLS_DC) /* {{{ */ +{ + char resolved_name[MAXPATHLEN]; + long line_num = strtol(line_pos+1, NULL, 0); + phpdbg_breakfile_t new_break; + zend_llist *break_files_ptr; + size_t name_len; + char *path = estrndup(expr, line_pos - expr); + + if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) { + efree(path); + return; + } + efree(path); + + name_len = strlen(resolved_name); + new_break.filename = estrndup(resolved_name, name_len + 1); + new_break.line = line_num; + + PHPDBG_G(has_file_bp) = 1; + + if (zend_hash_find(&PHPDBG_G(bp_files), + new_break.filename, name_len, (void**)&break_files_ptr) == FAILURE) { + zend_llist break_files; + + zend_llist_init(&break_files, sizeof(phpdbg_breakfile_t), + phpdbg_llist_breakfile_dtor, 0); + + zend_hash_update(&PHPDBG_G(bp_files), + new_break.filename, name_len, &break_files, sizeof(zend_llist), + (void**)&break_files_ptr); + } + zend_llist_add_element(break_files_ptr, &new_break); +} /* }}} */ + +void phpdbg_set_breakpoint_symbol(const char *expr, const char *opline_num_pos TSRMLS_DC) /* {{{ */ +{ + long opline_num = opline_num_pos ? strtol(opline_num_pos+1, NULL, 0) : 0; + phpdbg_breaksymbol_t new_break; + zend_llist *break_sym_ptr; + size_t name_len = opline_num_pos ? opline_num_pos - expr : strlen(expr); + + new_break.symbol = estrndup(expr, name_len); + new_break.opline_num = opline_num; + + PHPDBG_G(has_sym_bp) = 1; + + if (zend_hash_find(&PHPDBG_G(bp_symbols), + new_break.symbol, name_len, (void**)&break_sym_ptr) == FAILURE) { + zend_llist break_syms; + + zend_llist_init(&break_syms, sizeof(phpdbg_breaksymbol_t), + phpdbg_llist_breaksym_dtor, 0); + + zend_hash_update(&PHPDBG_G(bp_symbols), + new_break.symbol, name_len, &break_syms, sizeof(zend_llist), + (void**)&break_sym_ptr); + } + zend_llist_add_element(break_sym_ptr, &new_break); +} /* }}} */ + +int phpdbg_breakpoint_file(zend_op_array *op_array TSRMLS_DC) /* {{{ */ +{ + size_t name_len = strlen(op_array->filename); + zend_llist *break_list; + + if (zend_hash_find(&PHPDBG_G(bp_files), op_array->filename, name_len, + (void**)&break_list) == SUCCESS) { + zend_llist_element *le; + + for (le = break_list->head; le; le = le->next) { + phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) le->data; + + if (bp->line == (*EG(opline_ptr))->lineno) { + printf("breakpoint reached!\n"); + return SUCCESS; + } + } + } + + return FAILURE; +} /* }}} */ + +int phpdbg_breakpoint_symbol(zend_function *fbc TSRMLS_DC) /* {{{ */ +{ + const char *fname; + zend_llist *break_list; + + if (fbc->type != ZEND_USER_FUNCTION) { + return FAILURE; + } + + fname = ((zend_op_array*)fbc)->function_name; + + if (!fname) { + fname = "main"; + } + + if (zend_hash_find(&PHPDBG_G(bp_symbols), fname, strlen(fname), + (void**)&break_list) == SUCCESS) { + printf("breakpoint reached!\n"); + return SUCCESS; + } + + return FAILURE; +} +/* }}} */ diff --git a/phpdbg_bp.h b/phpdbg_bp.h new file mode 100644 index 00000000000..87aea82d0f9 --- /dev/null +++ b/phpdbg_bp.h @@ -0,0 +1,39 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 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: Felipe Pena | + | Authors: Joe Watkins | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHPDBG_BP_H +#define PHPDBG_BP_H + +/** + * Breakpoint file-based representation + */ +typedef struct _phpdbg_breakfile_t { + const char *filename; + long line; +} phpdbg_breakfile_t; + +/** + * Breakpoint symbol-based representation + */ +typedef struct _phpdbg_breaksymbol_t { + const char *symbol; + long opline_num; +} phpdbg_breaksymbol_t; + +#endif /* PHPDBG_BP_H */ diff --git a/phpdbg_help.h b/phpdbg_help.h index 1051d61fc93..8ce6f9691d2 100644 --- a/phpdbg_help.h +++ b/phpdbg_help.h @@ -20,6 +20,8 @@ #ifndef PHPDBG_HELP_H #define PHPDBG_HELP_H +#include "phpdbg_prompt.h" + /** * Command Declarators */ diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index fe81c8cb9dd..5e795b624b3 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -23,25 +23,12 @@ #include "zend_compile.h" #include "phpdbg.h" #include "phpdbg_help.h" +#include "phpdbg_bp.h" static const phpdbg_command_t phpdbg_prompt_commands[]; ZEND_EXTERN_MODULE_GLOBALS(phpdbg); -static void phpdbg_llist_breakfile_dtor(void *data) -{ - phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) data; - - efree((char*)bp->filename); -} - -static void phpdbg_llist_breaksym_dtor(void *data) -{ - phpdbg_breaksymbol_t *bp = (phpdbg_breaksymbol_t*) data; - - efree((char*)bp->symbol); -} - static PHPDBG_COMMAND(exec) { /* {{{ */ if (PHPDBG_G(exec)) { printf( @@ -142,7 +129,7 @@ static PHPDBG_COMMAND(run) { /* {{{ */ static PHPDBG_COMMAND(eval) { /* {{{ */ zval retval; - + if (expr) { if (zend_eval_stringl((char*)expr, expr_len-1, &retval, "eval()'d code" TSRMLS_CC) == SUCCESS) { printf("Success: "); @@ -219,61 +206,11 @@ static PHPDBG_COMMAND(break) /* {{{ */ const char *line_pos = zend_memrchr(expr, ':', expr_len); if (line_pos) { - char resolved_name[MAXPATHLEN]; - long line_num = strtol(line_pos+1, NULL, 0); - phpdbg_breakfile_t new_break; - zend_llist *break_files_ptr; - size_t name_len; - char *path = estrndup(expr, line_pos - expr); - - if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) { - efree(path); - return FAILURE; - } - efree(path); - - name_len = strlen(resolved_name); - new_break.filename = estrndup(resolved_name, name_len + 1); - new_break.line = line_num; - - PHPDBG_G(has_file_bp) = 1; - - if (zend_hash_find(&PHPDBG_G(bp_files), - new_break.filename, name_len, (void**)&break_files_ptr) == FAILURE) { - zend_llist break_files; - - zend_llist_init(&break_files, sizeof(phpdbg_breakfile_t), - phpdbg_llist_breakfile_dtor, 0); - - zend_hash_update(&PHPDBG_G(bp_files), - new_break.filename, name_len, &break_files, sizeof(zend_llist), - (void**)&break_files_ptr); - } - zend_llist_add_element(break_files_ptr, &new_break); + phpdbg_set_breakpoint_file(expr, line_pos TSRMLS_CC); } else { const char *opline_num_pos = zend_memrchr(expr, '#', expr_len); - long opline_num = opline_num_pos ? strtol(opline_num_pos+1, NULL, 0) : 0; - phpdbg_breaksymbol_t new_break; - zend_llist *break_sym_ptr; - size_t name_len = opline_num_pos ? opline_num_pos - expr : strlen(expr); - new_break.symbol = estrndup(expr, name_len); - new_break.opline_num = opline_num; - - PHPDBG_G(has_sym_bp) = 1; - - if (zend_hash_find(&PHPDBG_G(bp_symbols), - new_break.symbol, name_len, (void**)&break_sym_ptr) == FAILURE) { - zend_llist break_syms; - - zend_llist_init(&break_syms, sizeof(phpdbg_breaksymbol_t), - phpdbg_llist_breaksym_dtor, 0); - - zend_hash_update(&PHPDBG_G(bp_symbols), - new_break.symbol, name_len, &break_syms, sizeof(zend_llist), - (void**)&break_sym_ptr); - } - zend_llist_add_element(break_sym_ptr, &new_break); + phpdbg_set_breakpoint_symbol(expr, opline_num_pos TSRMLS_CC); } return SUCCESS; @@ -282,7 +219,7 @@ static PHPDBG_COMMAND(break) /* {{{ */ static PHPDBG_COMMAND(quit) /* {{{ */ { PHPDBG_G(quitting)=1; - + zend_bailout(); return SUCCESS; @@ -354,59 +291,13 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le return FAILURE; } /* }}} */ -int phpdbg_breakpoint_file(zend_op_array *op_array TSRMLS_DC) /* {{{ */ -{ - size_t name_len = strlen(op_array->filename); - zend_llist *break_list; - - if (zend_hash_find(&PHPDBG_G(bp_files), op_array->filename, name_len, - (void**)&break_list) == SUCCESS) { - zend_llist_element *le; - - for (le = break_list->head; le; le = le->next) { - phpdbg_breakfile_t *bp = (phpdbg_breakfile_t*) le->data; - - if (bp->line == (*EG(opline_ptr))->lineno) { - printf("breakpoint reached!\n"); - return SUCCESS; - } - } - } - - return FAILURE; -} /* }}} */ - -int phpdbg_breakpoint_symbol(zend_function *fbc TSRMLS_DC) -{ - const char *fname; - zend_llist *break_list; - - if (fbc->type != ZEND_USER_FUNCTION) { - return FAILURE; - } - - fname = ((zend_op_array*)fbc)->function_name; - - if (!fname) { - fname = "main"; - } - - if (zend_hash_find(&PHPDBG_G(bp_symbols), fname, strlen(fname), - (void**)&break_list) == SUCCESS) { - printf("breakpoint reached!\n"); - return SUCCESS; - } - - return FAILURE; -} - int phpdbg_interactive(int argc, char **argv TSRMLS_DC) /* {{{ */ { char cmd[PHPDBG_MAX_CMD]; printf("phpdbg> "); - while (!PHPDBG_G(quitting) && + while (!PHPDBG_G(quitting) && fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL) { size_t cmd_len = strlen(cmd) - 1; diff --git a/phpdbg_prompt.h b/phpdbg_prompt.h index 0f938e0051e..b6921d671e4 100644 --- a/phpdbg_prompt.h +++ b/phpdbg_prompt.h @@ -43,23 +43,6 @@ typedef struct _phpdbg_command_t { phpdbg_command_handler_t handler; /* Command handler */ } phpdbg_command_t; - -/** - * Breakpoint file-based representation - */ -typedef struct _phpdbg_breakfile_t { - const char *filename; - long line; -} phpdbg_breakfile_t; - -/** - * Breakpoint symbol-based representation - */ -typedef struct _phpdbg_breaksymbol_t { - const char *symbol; - long opline_num; -} phpdbg_breaksymbol_t; - /** * Command Executor */