From 5cca4190f42e5ba41927e66645ff6902188dc2d7 Mon Sep 17 00:00:00 2001 From: krakjoe Date: Sun, 10 Nov 2013 16:30:36 +0000 Subject: [PATCH 1/5] fix interference while quitting --- phpdbg.c | 1 + phpdbg.h | 3 ++- phpdbg_prompt.c | 11 ++++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/phpdbg.c b/phpdbg.c index a0419f313ea..fc780bfed92 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -29,6 +29,7 @@ static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */ pg->ops = NULL; pg->stepping = 0; pg->vmret = 0; + pg->quitting = 0; } /* }}} */ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ diff --git a/phpdbg.h b/phpdbg.h index f34fa4c84e0..5721ff5fd52 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -46,9 +46,10 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) size_t exec_len; /* size of exec */ zend_op_array *ops; /* op_array */ zval *retval; /* return value */ - zend_bool stepping; /* stepping */ + int stepping; /* stepping */ int vmret; /* return from last opcode handler execution */ zend_bool has_file_bp; /* file-based breakpoint has been set */ + zend_bool quitting; /* quitting flag */ ZEND_END_MODULE_GLOBALS(phpdbg) #include "phpdbg_prompt.h" diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 4a145ac78a2..e5c3142eda8 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -120,8 +120,10 @@ static PHPDBG_COMMAND(run) { /* {{{ */ zend_try { zend_execute(EG(active_op_array) TSRMLS_CC); } zend_catch { - printf("Caught excetion in VM\n"); - return FAILURE; + if (!PHPDBG_G(quitting)) { + printf("Caught excetion in VM\n"); + return FAILURE; + } else return SUCCESS; } zend_end_try(); return SUCCESS; @@ -245,6 +247,8 @@ static PHPDBG_COMMAND(break) /* {{{ */ static PHPDBG_COMMAND(quit) /* {{{ */ { + PHPDBG_G(quitting)=1; + zend_bailout(); return SUCCESS; @@ -344,7 +348,8 @@ int phpdbg_interactive(int argc, char **argv TSRMLS_DC) /* {{{ */ printf("phpdbg> "); - while (fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL) { + while (!PHPDBG_G(quitting) && + fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL) { size_t cmd_len = strlen(cmd) - 1; while (cmd[cmd_len] == '\n') { From 782ea75c9e8833ffd32a84875ab9c3c2712c4b0a Mon Sep 17 00:00:00 2001 From: krakjoe Date: Sun, 10 Nov 2013 16:33:35 +0000 Subject: [PATCH 2/5] ... --- phpdbg_prompt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index e5c3142eda8..a176be4c9e1 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -367,7 +367,9 @@ int phpdbg_interactive(int argc, char **argv TSRMLS_DC) /* {{{ */ } } - printf("phpdbg> "); + if (!PHPDBG_G(quitting)) { + printf("phpdbg> "); + } } return SUCCESS; From 5f6c416b1463286f56bd14e22b8abe7bb9609032 Mon Sep 17 00:00:00 2001 From: krakjoe Date: Sun, 10 Nov 2013 16:34:12 +0000 Subject: [PATCH 3/5] consistency --- phpdbg_prompt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index a176be4c9e1..4566fc7ef26 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -359,7 +359,10 @@ int phpdbg_interactive(int argc, char **argv TSRMLS_DC) /* {{{ */ if (cmd_len) { switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) { case FAILURE: - printf("error executing %s !\n", cmd); + if (!PHPDBG_G(quitting)) { + printf( + "Failed to execute %s !\n", cmd); + } break; case PHPDBG_NEXT: From c6f443db2ecbd4207498ed07715f432ee0f0ef25 Mon Sep 17 00:00:00 2001 From: krakjoe Date: Sun, 10 Nov 2013 16:47:39 +0000 Subject: [PATCH 4/5] improve output from eval() --- phpdbg_prompt.c | 7 +++++-- test.php | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 4566fc7ef26..51c160582f8 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -135,10 +135,13 @@ 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\n"); + printf("Success: "); + zend_print_zval_r( + &retval, 0 TSRMLS_CC); + printf("\n"); zval_dtor(&retval); } } else { diff --git a/test.php b/test.php index cbb2641e4fa..e907c3b6bf2 100644 --- a/test.php +++ b/test.php @@ -3,4 +3,5 @@ echo "Hello World\n"; if (isset($greeting)) { echo $greeting; } +return true; ?> From 3716646d3ccd1eb1861fb54e5eda7115a5d8ce60 Mon Sep 17 00:00:00 2001 From: krakjoe Date: Sun, 10 Nov 2013 17:45:06 +0000 Subject: [PATCH 5/5] ... --- phpdbg.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- phpdbg.h | 1 + 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/phpdbg.c b/phpdbg.c index fc780bfed92..45377503898 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -126,9 +126,36 @@ static sapi_module_struct phpdbg_sapi_module = { }; /* }}} */ +const opt_struct OPTIONS[] = { /* }}} */ + {'c', 1, "ini path override"}, + {'d', 1, "define ini entry on command line"}, + {'-', 0, NULL} +}; /* }}} */ + +/* overwriteable ini defaults must be set in phpdbg_ini_defaults() */ +#define INI_DEFAULT(name,value)\ + Z_SET_REFCOUNT(tmp, 0);\ + Z_UNSET_ISREF(tmp); \ + ZVAL_STRINGL(&tmp, zend_strndup(value, sizeof(value)-1), sizeof(value)-1, 0);\ + zend_hash_update(configuration_hash, name, sizeof(name), &tmp, sizeof(zval), NULL);\ + +void phpdbg_ini_defaults(HashTable *configuration_hash) { /* {{{ */ + zval tmp; + INI_DEFAULT("report_zend_debug", "0"); + INI_DEFAULT("display_errors", "1"); +} /* }}} */ + int main(int argc, char **argv) /* {{{ */ { sapi_module_struct *phpdbg = &phpdbg_sapi_module; + char *ini_file = NULL; + char *ini_entries = NULL; + int ini_entries_len = 0; + char *ini_path_override = NULL; + char *php_optarg = NULL; + int php_optind = 0; + int opt; + #ifdef ZTS void ***tsrm_ls; tsrm_startup(1, 1, 0, NULL); @@ -143,10 +170,59 @@ int main(int argc, char **argv) /* {{{ */ setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */ #endif - phpdbg->executable_location = argv[0]; - + while ((opt = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 1)) != -1) { + switch (opt) { + case 'c': + if (ini_path_override) { + free(ini_path_override); + } + ini_path_override = strdup(php_optarg); + break; + case 'd': { + int len = strlen(php_optarg); + char *val; + + if ((val = strchr(php_optarg, '='))) { + val++; + if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') { + ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0")); + memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg)); + ini_entries_len += (val - php_optarg); + memcpy(ini_entries + ini_entries_len, "\"", 1); + ini_entries_len++; + memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg)); + ini_entries_len += len - (val - php_optarg); + memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0")); + ini_entries_len += sizeof("\n\0\"") - 2; + } else { + ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0")); + memcpy(ini_entries + ini_entries_len, php_optarg, len); + memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0")); + ini_entries_len += len + sizeof("\n\0") - 2; + } + } else { + ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0")); + memcpy(ini_entries + ini_entries_len, php_optarg, len); + memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0")); + ini_entries_len += len + sizeof("=1\n\0") - 2; + } + } break; + } + } + + phpdbg->ini_defaults = phpdbg_ini_defaults; + phpdbg->php_ini_path_override = ini_path_override; + phpdbg->phpinfo_as_text = 1; + phpdbg->php_ini_ignore_cwd = 1; + sapi_startup(phpdbg); + phpdbg->executable_location = argv[0]; + phpdbg->phpinfo_as_text = 1; + phpdbg->php_ini_ignore_cwd = 0; + phpdbg->php_ini_ignore = 0; + phpdbg->ini_entries = ini_entries; + if (phpdbg->startup(phpdbg) == SUCCESS) { zend_activate(TSRMLS_C); @@ -165,6 +241,14 @@ int main(int argc, char **argv) /* {{{ */ zend_try { phpdbg_interactive(argc, argv TSRMLS_CC); } zend_end_try(); + + if (ini_file) { + pefree(ini_file, 1); + } + + if (ini_entries) { + free(ini_entries); + } if (PG(modules_activated)) { zend_try { diff --git a/phpdbg.h b/phpdbg.h index 5721ff5fd52..087323009e4 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -18,6 +18,7 @@ #include "php.h" #include "php_globals.h" #include "php_variables.h" +#include "php_getopt.h" #include "zend_modules.h" #include "zend_ini_scanner.h" #include "zend_globals.h"