From 96856c1dec77a256e07c881331af1e46bc09355b Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Fri, 4 Oct 2024 00:55:41 +0100 Subject: [PATCH 1/2] sapi/phpdbg: Use HASH_FOREACH macro --- sapi/phpdbg/phpdbg_frame.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/sapi/phpdbg/phpdbg_frame.c b/sapi/phpdbg/phpdbg_frame.c index efb35e5cdf50..2d3a1e870966 100644 --- a/sapi/phpdbg/phpdbg_frame.c +++ b/sapi/phpdbg/phpdbg_frame.c @@ -245,12 +245,10 @@ static void phpdbg_dump_prototype(zval *tmp) /* {{{ */ void phpdbg_dump_backtrace(size_t num) /* {{{ */ { - HashPosition position; zval zbacktrace; zval *tmp; - zval startline, startfile; - const char *startfilename; - zval *file = &startfile, *line = &startline; + zend_string *file = NULL; + zend_long line = 0; int i = 0, limit = num; PHPDBG_OUTPUT_BACKUP(); @@ -269,18 +267,15 @@ void phpdbg_dump_backtrace(size_t num) /* {{{ */ return; } phpdbg_end_try_access(); - Z_LVAL(startline) = zend_get_executed_lineno(); - startfilename = zend_get_executed_filename(); - Z_STR(startfile) = zend_string_init(startfilename, strlen(startfilename), 0); - - zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position); + line = zend_get_executed_lineno(); + file = zend_get_executed_filename_ex(); zval *function_name = NULL; - while ((tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position))) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL(zbacktrace), tmp) { if (file) { /* userland */ phpdbg_out("frame #%d: ", i); phpdbg_dump_prototype(tmp); - phpdbg_out(" at %s:"ZEND_LONG_FMT"\n", Z_STRVAL_P(file), Z_LVAL_P(line)); + phpdbg_out(" at %s:"ZEND_LONG_FMT"\n", ZSTR_VAL(file), line); i++; } else { phpdbg_out(" => "); @@ -288,23 +283,29 @@ void phpdbg_dump_backtrace(size_t num) /* {{{ */ phpdbg_out(" (internal function)\n"); } - file = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_FILE)); - line = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_LINE)); + zval *file_zv = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_FILE)); + if (file_zv) { + ZEND_ASSERT(Z_TYPE_P(file_zv) == IS_STRING); + file = Z_STR_P(file_zv); + } else { + file = NULL; + } + zval *line_zv = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_LINE)); + if (line_zv) { + line = Z_LVAL_P(line_zv); + } function_name = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_FUNCTION)); - - zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position); - } + } ZEND_HASH_FOREACH_END(); /* This is possible for fibers' start closure for example, which have a frame that doesn't contain the info * of which location stated the fiber if that stack frame is already torn down. same behaviour with debug_backtrace(). */ if (file == NULL) { phpdbg_writeln(" => %s (internal function)", Z_STRVAL_P(function_name)); } else { - phpdbg_writeln("frame #%d: {main} at %s:"ZEND_LONG_FMT, i, Z_STRVAL_P(file), Z_LVAL_P(line)); + phpdbg_writeln("frame #%d: {main} at %s:"ZEND_LONG_FMT, i, ZSTR_VAL(file), line); } zval_ptr_dtor_nogc(&zbacktrace); - zend_string_release(Z_STR(startfile)); PHPDBG_OUTPUT_BACKUP_RESTORE(); } /* }}} */ From bfe312b7d383f4212b41cfa95ad8981e2ffeed4a Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sat, 5 Oct 2024 13:03:42 +0100 Subject: [PATCH 2/2] Address review comment --- sapi/phpdbg/phpdbg_frame.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/sapi/phpdbg/phpdbg_frame.c b/sapi/phpdbg/phpdbg_frame.c index 2d3a1e870966..a8f1bf01433b 100644 --- a/sapi/phpdbg/phpdbg_frame.c +++ b/sapi/phpdbg/phpdbg_frame.c @@ -283,13 +283,7 @@ void phpdbg_dump_backtrace(size_t num) /* {{{ */ phpdbg_out(" (internal function)\n"); } - zval *file_zv = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_FILE)); - if (file_zv) { - ZEND_ASSERT(Z_TYPE_P(file_zv) == IS_STRING); - file = Z_STR_P(file_zv); - } else { - file = NULL; - } + file = zend_hash_find_ptr(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_FILE)); zval *line_zv = zend_hash_find(Z_ARRVAL_P(tmp), ZSTR_KNOWN(ZEND_STR_LINE)); if (line_zv) { line = Z_LVAL_P(line_zv);