From ab2a73a6629850bfd131996a54ca649cfd455f01 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 17 Feb 2014 11:05:48 +0800 Subject: [PATCH] Use better data structures (incomplete) --- sapi/phpdbg/phpdbg.c | 34 ++++++++-------- sapi/phpdbg/phpdbg.h | 2 +- sapi/phpdbg/phpdbg_help.c | 9 ++--- sapi/phpdbg/phpdbg_print.c | 61 ++++++++++++++++------------- sapi/phpdbg/phpdbg_prompt.c | 77 ++++++++++++++++--------------------- sapi/phpdbg/phpdbg_prompt.h | 2 +- 6 files changed, 91 insertions(+), 94 deletions(-) diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index e7f841c0bc..64babced64 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -98,30 +98,29 @@ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ return SUCCESS; } /* }}} */ -static void php_phpdbg_destroy_bp_file(void *brake) /* {{{ */ +static void php_phpdbg_destroy_bp_file(zval *brake) /* {{{ */ { - zend_hash_destroy((HashTable*)brake); + zend_hash_destroy((HashTable*)Z_PTR_P(brake)); } /* }}} */ -static void php_phpdbg_destroy_bp_symbol(void *brake) /* {{{ */ +static void php_phpdbg_destroy_bp_symbol(zval *brake) /* {{{ */ { - efree((char*)((phpdbg_breaksymbol_t*)brake)->symbol); + efree((char*)((phpdbg_breaksymbol_t*)Z_PTR_P(brake))->symbol); } /* }}} */ -static void php_phpdbg_destroy_bp_opcode(void *brake) /* {{{ */ +static void php_phpdbg_destroy_bp_opcode(zval *brake) /* {{{ */ { - efree((char*)((phpdbg_breakop_t*)brake)->name); + efree((char*)((phpdbg_breakop_t*)Z_PTR_P(brake))->name); } /* }}} */ - -static void php_phpdbg_destroy_bp_methods(void *brake) /* {{{ */ +static void php_phpdbg_destroy_bp_methods(zval *brake) /* {{{ */ { - zend_hash_destroy((HashTable*)brake); + zend_hash_destroy((HashTable*)Z_PTR_P(brake)); } /* }}} */ -static void php_phpdbg_destroy_bp_condition(void *data) /* {{{ */ +static void php_phpdbg_destroy_bp_condition(zval *data) /* {{{ */ { - phpdbg_breakcond_t *brake = (phpdbg_breakcond_t*) data; + phpdbg_breakcond_t *brake = (phpdbg_breakcond_t*)Z_PTR_P(data); if (brake) { if (brake->ops) { @@ -135,9 +134,9 @@ static void php_phpdbg_destroy_bp_condition(void *data) /* {{{ */ } } /* }}} */ -static void php_phpdbg_destroy_registered(void *data) /* {{{ */ +static void php_phpdbg_destroy_registered(zval *data) /* {{{ */ { - zend_function *function = (zend_function*) data; + zend_function *function = (zend_function*)Z_PTR_P(data); TSRMLS_FETCH(); destroy_zend_function( @@ -146,7 +145,7 @@ static void php_phpdbg_destroy_registered(void *data) /* {{{ */ static PHP_RINIT_FUNCTION(phpdbg) /* {{{ */ { - zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], 8, NULL, php_phpdbg_destroy_bp_file, 0); + zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], 8, NULL, php_phpdbg_destroy_bp_file, 0); zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_SYM], 8, NULL, php_phpdbg_destroy_bp_symbol, 0); zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_FUNCTION_OPLINE], 8, NULL, php_phpdbg_destroy_bp_methods, 0); zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_METHOD_OPLINE], 8, NULL, php_phpdbg_destroy_bp_methods, 0); @@ -229,7 +228,7 @@ static PHP_FUNCTION(phpdbg_exec) if (VCWD_STAT(exec, &sb) != FAILURE) { if (sb.st_mode & (S_IFREG|S_IFLNK)) { if (PHPDBG_G(exec)) { - ZVAL_STRINGL(return_value, PHPDBG_G(exec), PHPDBG_G(exec_len), 1); + ZVAL_STRINGL(return_value, PHPDBG_G(exec), PHPDBG_G(exec_len)); efree(PHPDBG_G(exec)); result = 0; } @@ -615,9 +614,8 @@ const char phpdbg_ini_hardcoded[] = /* 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); + ZVAL_STRINGL(&tmp, value, sizeof(value) - 1); \ + zend_hash_str_update(configuration_hash, name, sizeof(name) - 1, &tmp); void phpdbg_ini_defaults(HashTable *configuration_hash) /* {{{ */ { diff --git a/sapi/phpdbg/phpdbg.h b/sapi/phpdbg/phpdbg.h index 6b7afb2370..5e48850fea 100644 --- a/sapi/phpdbg/phpdbg.h +++ b/sapi/phpdbg/phpdbg.h @@ -169,7 +169,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) char *exec; /* file to execute */ size_t exec_len; /* size of exec */ zend_op_array *ops; /* op_array */ - zval *retval; /* return value */ + zval retval; /* return value */ int bp_count; /* breakpoint count */ int vmret; /* return from last opcode handler execution */ diff --git a/sapi/phpdbg/phpdbg_help.c b/sapi/phpdbg/phpdbg_help.c index a02cb41c14..93efd4ca64 100644 --- a/sapi/phpdbg/phpdbg_help.c +++ b/sapi/phpdbg/phpdbg_help.c @@ -518,15 +518,14 @@ PHPDBG_HELP(register) /* {{{ */ phpdbg_writeln("Note: arguments passed as strings, return (if present) print_r'd on console"); if (zend_hash_num_elements(&PHPDBG_G(registered))) { HashPosition position; - char *name = NULL; - zend_uint name_len = 0; + zend_string *name; phpdbg_notice("Registered Functions (%d)", zend_hash_num_elements(&PHPDBG_G(registered))); for (zend_hash_internal_pointer_reset_ex(&PHPDBG_G(registered), &position); - zend_hash_get_current_key_ex(&PHPDBG_G(registered), &name, &name_len, NULL, 1, &position) == HASH_KEY_IS_STRING; + zend_hash_get_current_key_ex(&PHPDBG_G(registered), &name, NULL, 1, &position) == HASH_KEY_IS_STRING; zend_hash_move_forward_ex(&PHPDBG_G(registered), &position)) { - phpdbg_writeln("|-------> %s", name); - efree(name); + phpdbg_writeln("|-------> %s", name->val); + STR_RELEASE(name); } } diff --git a/sapi/phpdbg/phpdbg_print.c b/sapi/phpdbg/phpdbg_print.c index c4925fe5fd..8836eed137 100644 --- a/sapi/phpdbg/phpdbg_print.c +++ b/sapi/phpdbg/phpdbg_print.c @@ -52,15 +52,15 @@ static inline void phpdbg_print_function_helper(zend_function *method TSRMLS_DC) if (method->common.scope) { phpdbg_writeln("\tL%d-%d %s::%s() %s", op_array->line_start, op_array->line_end, - method->common.scope->name, - method->common.function_name, - op_array->filename ? op_array->filename : "unknown"); + method->common.scope->name->val, + method->common.function_name->val, + op_array->filename ? op_array->filename->val : "unknown"); } else { phpdbg_writeln("\tL%d-%d %s() %s", method->common.function_name ? op_array->line_start : 0, method->common.function_name ? op_array->line_end : 0, - method->common.function_name ? method->common.function_name : "{main}", - op_array->filename ? op_array->filename : "unknown"); + method->common.function_name ? method->common.function_name->val : "{main}", + op_array->filename ? op_array->filename->val : "unknown"); } zend_hash_init(&vars, op_array->last, NULL, NULL, 0); @@ -84,9 +84,9 @@ static inline void phpdbg_print_function_helper(zend_function *method TSRMLS_DC) default: { if (method->common.scope) { - phpdbg_writeln("\tInternal %s::%s()", method->common.scope->name, method->common.function_name); + phpdbg_writeln("\tInternal %s::%s()", method->common.scope->name->val, method->common.function_name->val); } else { - phpdbg_writeln("\tInternal %s()", method->common.function_name); + phpdbg_writeln("\tInternal %s()", method->common.function_name->val); } } } @@ -118,13 +118,13 @@ PHPDBG_PRINT(stack) /* {{{ */ if (EG(in_execution) && ops) { if (ops->function_name) { if (ops->scope) { - phpdbg_notice("Stack in %s::%s()", ops->scope->name, ops->function_name); + phpdbg_notice("Stack in %s::%s()", ops->scope->name->val, ops->function_name->val); } else { - phpdbg_notice("Stack in %s()", ops->function_name); + phpdbg_notice("Stack in %s()", ops->function_name->val); } } else { if (ops->filename) { - phpdbg_notice("Stack in %s", ops->filename); + phpdbg_notice("Stack in %s", ops->filename->val); } else { phpdbg_notice("Stack @ %p", ops); } @@ -139,34 +139,38 @@ PHPDBG_PRINT(stack) /* {{{ */ PHPDBG_PRINT(class) /* {{{ */ { - zend_class_entry **ce; + zend_class_entry *ce; switch (param->type) { case STR_PARAM: { - if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) { + zval tmp; /* param->str need to be zend_string later */ + ZVAL_STRINGL(&tmp, param->str, param->len); + if ((ce = zend_lookup_class(Z_STR(tmp) TSRMLS_CC)) != NULL) { + zval_ptr_dtor(&tmp); phpdbg_notice("%s %s: %s", - ((*ce)->type == ZEND_USER_CLASS) ? + (ce->type == ZEND_USER_CLASS) ? "User" : "Internal", - ((*ce)->ce_flags & ZEND_ACC_INTERFACE) ? + (ce->ce_flags & ZEND_ACC_INTERFACE) ? "Interface" : - ((*ce)->ce_flags & ZEND_ACC_ABSTRACT) ? + (ce->ce_flags & ZEND_ACC_ABSTRACT) ? "Abstract Class" : "Class", - (*ce)->name); + *ce->name->val); - phpdbg_writeln("Methods (%d):", zend_hash_num_elements(&(*ce)->function_table)); - if (zend_hash_num_elements(&(*ce)->function_table)) { + phpdbg_writeln("Methods (%d):", zend_hash_num_elements(&ce->function_table)); + if (zend_hash_num_elements(&ce->function_table)) { HashPosition position; zend_function *method; - for (zend_hash_internal_pointer_reset_ex(&(*ce)->function_table, &position); - zend_hash_get_current_data_ex(&(*ce)->function_table, (void**) &method, &position) == SUCCESS; - zend_hash_move_forward_ex(&(*ce)->function_table, &position)) { + for (zend_hash_internal_pointer_reset_ex(&ce->function_table, &position); + (method = zend_hash_get_current_data_ptr_ex(&ce->function_table, &position)) != NULL; + zend_hash_move_forward_ex(&ce->function_table, &position)) { phpdbg_print_function_helper(method TSRMLS_CC); } } } else { phpdbg_error("The class %s could not be found", param->str); + zval_ptr_dtor(&tmp); } } break; @@ -180,13 +184,17 @@ PHPDBG_PRINT(method) /* {{{ */ { switch (param->type) { case METHOD_PARAM: { - zend_class_entry **ce; + zend_class_entry *ce; + zval tmp; - if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) { + ZVAL_STRING(&tmp, param->method.class); + if ((ce = zend_lookup_class(Z_STR(tmp) TSRMLS_CC)) != NULL) { zend_function *fbc; char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name)); - if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) { + zval_ptr_dtor(&tmp); + + if ((fbc = zend_hash_str_find_ptr(&ce->function_table, lcname, strlen(lcname))) != NULL) { phpdbg_notice("%s Method %s", (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal", fbc->common.function_name); @@ -198,6 +206,7 @@ PHPDBG_PRINT(method) /* {{{ */ efree(lcname); } else { + zval_ptr_dtor(&tmp); phpdbg_error("The class %s could not be found", param->method.class); } } break; @@ -237,11 +246,11 @@ PHPDBG_PRINT(func) /* {{{ */ lcname = zend_str_tolower_dup(func_name, func_name_len); - if (zend_hash_find(func_table, lcname, strlen(lcname)+1, (void**)&fbc) == SUCCESS) { + if ((fbc = zend_hash_str_find_ptr(func_table, lcname, strlen(lcname))) != NULL) { phpdbg_notice("%s %s %s", (fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal", (fbc->common.scope) ? "Method" : "Function", - fbc->common.function_name); + fbc->common.function_name->val); phpdbg_print_function_helper(fbc TSRMLS_CC); } else { diff --git a/sapi/phpdbg/phpdbg_prompt.c b/sapi/phpdbg/phpdbg_prompt.c index cb46407957..78aa4d87f3 100644 --- a/sapi/phpdbg/phpdbg_prompt.c +++ b/sapi/phpdbg/phpdbg_prompt.c @@ -72,22 +72,20 @@ static inline int phpdbg_call_register(phpdbg_input_t *input TSRMLS_DC) /* {{{ * { phpdbg_input_t *function = input->argv[0]; - if (zend_hash_exists( - &PHPDBG_G(registered), function->string, function->length+1)) { + if (zend_hash_str_exists( + &PHPDBG_G(registered), function->string, function->length)) { - zval fname, *fretval; + zval fretval; zend_fcall_info fci; - ZVAL_STRINGL(&fname, function->string, function->length, 1); - memset(&fci, 0, sizeof(zend_fcall_info)); fci.size = sizeof(zend_fcall_info); fci.function_table = &PHPDBG_G(registered); - fci.function_name = &fname; + ZVAL_STRINGL(&fci.function_name, function->string, function->length); fci.symbol_table = EG(active_symbol_table); fci.object_ptr = NULL; - fci.retval_ptr_ptr = &fretval; + fci.retval = &fretval; fci.no_separation = 1; if (input->argc > 1) { @@ -119,13 +117,13 @@ static inline int phpdbg_call_register(phpdbg_input_t *input TSRMLS_DC) /* {{{ * zend_call_function(&fci, NULL TSRMLS_CC); - if (fretval) { + if (!ZVAL_IS_UNDEF(&fretval)) { zend_print_zval_r( - fretval, 0 TSRMLS_CC); + &fretval, 0 TSRMLS_CC); phpdbg_writeln(EMPTY); } - zval_dtor(&fname); + zval_ptr_dtor(&fci.function_name); return SUCCESS; } @@ -394,11 +392,11 @@ PHPDBG_COMMAND(until) /* {{{ */ for (next = self; next < EG(active_op_array)->last; next++) { if (EG(active_op_array)->opcodes[next].lineno != opline->lineno) { - zend_hash_index_update( + zend_hash_index_update_mem( &PHPDBG_G(seek), (zend_ulong) &EG(active_op_array)->opcodes[next], - &EG(active_op_array)->opcodes[next], - sizeof(zend_op), NULL); + (void *)&EG(active_op_array)->opcodes[next], + sizeof(zend_op)); break; } } @@ -427,11 +425,11 @@ PHPDBG_COMMAND(finish) /* {{{ */ #ifdef ZEND_YIELD case ZEND_YIELD: #endif - zend_hash_index_update( + zend_hash_index_update_mem( &PHPDBG_G(seek), (zend_ulong) &EG(active_op_array)->opcodes[next], - &EG(active_op_array)->opcodes[next], - sizeof(zend_op), NULL); + (void *)&EG(active_op_array)->opcodes[next], + sizeof(zend_op)); break; } } @@ -460,11 +458,11 @@ PHPDBG_COMMAND(leave) /* {{{ */ #ifdef ZEND_YIELD case ZEND_YIELD: #endif - zend_hash_index_update( + zend_hash_index_update_mem( &PHPDBG_G(seek), (zend_ulong) &EG(active_op_array)->opcodes[next], - &EG(active_op_array)->opcodes[next], - sizeof(zend_op), NULL); + (void *)&EG(active_op_array)->opcodes[next], + sizeof(zend_op)); break; } } @@ -494,8 +492,7 @@ static inline void phpdbg_handle_exception(TSRMLS_D) /* }}} */ { zend_fcall_info fci; - zval fname, - *trace, + zval trace, exception; /* get filename and linenumber before unsetting exception */ @@ -503,40 +500,38 @@ static inline void phpdbg_handle_exception(TSRMLS_D) /* }}} */ zend_uint lineno = zend_get_executed_lineno(TSRMLS_C); /* copy exception */ - exception = *EG(exception); - zval_copy_ctor(&exception); + ZVAL_OBJ(&exception, EG(exception)); EG(exception) = NULL; phpdbg_error( "Uncaught %s!", - Z_OBJCE(exception)->name); + Z_OBJCE(exception)->name->val); /* call __toString */ - ZVAL_STRINGL(&fname, "__tostring", sizeof("__tostring")-1, 1); fci.size = sizeof(fci); fci.function_table = &Z_OBJCE(exception)->function_table; - fci.function_name = &fname; + ZVAL_STRINGL(&fci.function_name, "__tostring", sizeof("__tostring") - 1); fci.symbol_table = NULL; fci.object_ptr = &exception; - fci.retval_ptr_ptr = &trace; + fci.retval = &trace; fci.param_count = 0; fci.params = NULL; fci.no_separation = 1; zend_call_function(&fci, NULL TSRMLS_CC); - if (trace) { + if (!ZVAL_IS_UNDEF(&trace) && Z_TYPE(trace) == IS_STRING) { phpdbg_writeln( - "Uncaught %s", Z_STRVAL_P(trace)); + "Uncaught %s", Z_STRVAL(trace)); /* remember to dtor trace */ zval_ptr_dtor(&trace); } + zval_ptr_dtor(&trace); /* output useful information about address */ phpdbg_writeln( "Stacked entered at %p in %s on line %u", EG(active_op_array)->opcodes, filename, lineno); - zval_dtor(&fname); zval_dtor(&exception); } /* }}} */ @@ -550,7 +545,6 @@ PHPDBG_COMMAND(run) /* {{{ */ if (PHPDBG_G(ops) || PHPDBG_G(exec)) { zend_op **orig_opline = EG(opline_ptr); zend_op_array *orig_op_array = EG(active_op_array); - zval **orig_retval_ptr = EG(return_value_ptr_ptr); zend_bool restore = 1; if (!PHPDBG_G(ops)) { @@ -561,7 +555,6 @@ PHPDBG_COMMAND(run) /* {{{ */ } EG(active_op_array) = PHPDBG_G(ops); - EG(return_value_ptr_ptr) = &PHPDBG_G(retval); if (!EG(active_symbol_table)) { zend_rebuild_symbol_table(TSRMLS_C); } @@ -577,13 +570,12 @@ PHPDBG_COMMAND(run) /* {{{ */ zend_try { php_output_activate(TSRMLS_C); PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; - zend_execute(EG(active_op_array) TSRMLS_CC); + zend_execute(EG(active_op_array), &PHPDBG_G(retval) TSRMLS_CC); PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; php_output_deactivate(TSRMLS_C); } zend_catch { EG(active_op_array) = orig_op_array; EG(opline_ptr) = orig_opline; - EG(return_value_ptr_ptr) = orig_retval_ptr; if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) { phpdbg_error("Caught exit/error from VM"); @@ -598,7 +590,6 @@ PHPDBG_COMMAND(run) /* {{{ */ EG(active_op_array) = orig_op_array; EG(opline_ptr) = orig_opline; - EG(return_value_ptr_ptr) = orig_retval_ptr; } } else { phpdbg_error("Nothing to execute!"); @@ -827,10 +818,10 @@ PHPDBG_COMMAND(register) /* {{{ */ char *lcname = zend_str_tolower_dup(param->str, param->len); size_t lcname_len = strlen(lcname); - if (!zend_hash_exists(&PHPDBG_G(registered), lcname, lcname_len+1)) { - if (zend_hash_find(EG(function_table), lcname, lcname_len+1, (void**) &function) == SUCCESS) { - zend_hash_update( - &PHPDBG_G(registered), lcname, lcname_len+1, (void*)&function, sizeof(zend_function), NULL); + if (!zend_hash_str_exists(&PHPDBG_G(registered), lcname, lcname_len)) { + if ((function = zend_hash_str_find_ptr(EG(function_table), lcname, lcname_len)) != NULL) { + zend_hash_str_update_mem( + &PHPDBG_G(registered), lcname, lcname_len, function, sizeof(zend_function)); function_add_ref(function); phpdbg_notice( @@ -1088,10 +1079,10 @@ void phpdbg_clean(zend_bool full TSRMLS_DC) /* {{{ */ } } /* }}} */ -static inline zend_execute_data *phpdbg_create_execute_data(zend_op_array *op_array, zend_bool nested TSRMLS_DC) /* {{{ */ +static inline zend_execute_data *phpdbg_create_execute_data(zend_op_array *op_array, zval *return_value, zend_bool nested TSRMLS_DC) /* {{{ */ { #if PHP_VERSION_ID >= 50500 - return zend_create_execute_data_from_op_array(op_array, nested TSRMLS_CC); + return zend_create_execute_data_from_op_array(op_array, return_value, nested TSRMLS_CC); #else #undef EX @@ -1150,7 +1141,7 @@ static inline zend_execute_data *phpdbg_create_execute_data(zend_op_array *op_ar } /* }}} */ #if PHP_VERSION_ID >= 50500 -void phpdbg_execute_ex(zend_execute_data *execute_data TSRMLS_DC) /* {{{ */ +void phpdbg_execute_ex(zend_execute_data *execute_data, zval *return_value TSRMLS_DC) /* {{{ */ { #else void phpdbg_execute_ex(zend_op_array *op_array TSRMLS_DC) /* {{{ */ @@ -1174,7 +1165,7 @@ void phpdbg_execute_ex(zend_op_array *op_array TSRMLS_DC) /* {{{ */ #if PHP_VERSION_ID >= 50500 if (0) { zend_vm_enter: - execute_data = phpdbg_create_execute_data(EG(active_op_array), 1 TSRMLS_CC); + execute_data = phpdbg_create_execute_data(EG(active_op_array), return_value, 1 TSRMLS_CC); } zend_hash_init(&vars, EG(active_op_array)->last, NULL, NULL, 0); #else diff --git a/sapi/phpdbg/phpdbg_prompt.h b/sapi/phpdbg/phpdbg_prompt.h index 6807d88f41..69e9bddf70 100644 --- a/sapi/phpdbg/phpdbg_prompt.h +++ b/sapi/phpdbg/phpdbg_prompt.h @@ -60,7 +60,7 @@ extern const phpdbg_command_t phpdbg_prompt_commands[]; /* }}} */ /* {{{ */ #if PHP_VERSION_ID >= 50500 -void phpdbg_execute_ex(zend_execute_data *execute_data TSRMLS_DC); +void phpdbg_execute_ex(zend_execute_data *execute_data, zval *return_value TSRMLS_DC); #else void phpdbg_execute_ex(zend_op_array *op_array TSRMLS_DC); #endif /* }}} */ -- 2.40.0