Skip to content

Commit 077886d

Browse files
committed
Support extra named params in backtraces
1 parent 141ded0 commit 077886d

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
Extra named params in backtraces
3+
--FILE--
4+
<?php
5+
6+
function test($a, ...$rest) {
7+
var_dump(debug_backtrace());
8+
debug_print_backtrace();
9+
throw new Exception("Test");
10+
}
11+
12+
try {
13+
test(1, 2, x: 3, y: 4);
14+
} catch (Exception $e) {
15+
var_dump($e->getTrace());
16+
echo $e, "\n";
17+
}
18+
19+
?>
20+
--EXPECTF--
21+
array(1) {
22+
[0]=>
23+
array(4) {
24+
["file"]=>
25+
string(%d) "%s"
26+
["line"]=>
27+
int(10)
28+
["function"]=>
29+
string(4) "test"
30+
["args"]=>
31+
array(4) {
32+
[0]=>
33+
int(1)
34+
[1]=>
35+
int(2)
36+
["x"]=>
37+
int(3)
38+
["y"]=>
39+
int(4)
40+
}
41+
}
42+
}
43+
#0 test(1, 2, x: 3, y: 4) called at [%s:10]
44+
array(1) {
45+
[0]=>
46+
array(4) {
47+
["file"]=>
48+
string(%d) "%s"
49+
["line"]=>
50+
int(10)
51+
["function"]=>
52+
string(4) "test"
53+
["args"]=>
54+
array(4) {
55+
[0]=>
56+
int(1)
57+
[1]=>
58+
int(2)
59+
["x"]=>
60+
int(3)
61+
["y"]=>
62+
int(4)
63+
}
64+
}
65+
}
66+
Exception: Test in %s:%d
67+
Stack trace:
68+
#0 %s(%d): test(1, 2, x: 3, y: 4)
69+
#1 {main}

Zend/zend_builtin_functions.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,18 +1640,34 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
16401640
} else {
16411641
ZVAL_EMPTY_ARRAY(arg_array);
16421642
}
1643+
1644+
if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
1645+
zend_string *name;
1646+
zval *arg;
1647+
SEPARATE_ARRAY(arg_array);
1648+
ZEND_HASH_FOREACH_STR_KEY_VAL(call->extra_named_params, name, arg) {
1649+
ZVAL_DEREF(arg);
1650+
Z_TRY_ADDREF_P(arg);
1651+
zend_hash_add_new(Z_ARRVAL_P(arg_array), name, arg);
1652+
} ZEND_HASH_FOREACH_END();
1653+
}
16431654
}
16441655
/* }}} */
16451656

16461657
void debug_print_backtrace_args(zval *arg_array) /* {{{ */
16471658
{
1659+
zend_string *name;
16481660
zval *tmp;
16491661
int i = 0;
16501662

1651-
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arg_array), tmp) {
1663+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(arg_array), name, tmp) {
16521664
if (i++) {
16531665
ZEND_PUTS(", ");
16541666
}
1667+
if (name) {
1668+
ZEND_PUTS(ZSTR_VAL(name));
1669+
ZEND_PUTS(": ");
1670+
}
16551671
zend_print_flat_zval_r(tmp);
16561672
} ZEND_HASH_FOREACH_END();
16571673
}

Zend/zend_exceptions.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,14 @@ static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /*
566566
if (tmp) {
567567
if (Z_TYPE_P(tmp) == IS_ARRAY) {
568568
size_t last_len = ZSTR_LEN(str->s);
569+
zend_string *name;
569570
zval *arg;
570571

571-
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmp), arg) {
572+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
573+
if (name) {
574+
smart_str_append(str, name);
575+
smart_str_appends(str, ": ");
576+
}
572577
_build_trace_args(arg, str);
573578
} ZEND_HASH_FOREACH_END();
574579

0 commit comments

Comments
 (0)