Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Zend/tests/stack_frame_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
StackFrame read properties
--FILE--
<?php

function frames() {
return StackFrame::getTrace();
}
$frame = frames()[0];

var_dump([
'file' => $frame->file,
'line' => $frame->line,
'function' => $frame->function,
'class' => $frame->class,
'type' => $frame->type,
'object' => $frame->object,
'args' => $frame->args,
'object_class' => $frame->object_class,
'closure' => $frame->closure,
]);

--EXPECTF--
array(%d) {
["file"]=>
string(%d) "%s.php"
["line"]=>
int(%d)
["function"]=>
string(6) "frames"
["class"]=>
NULL
["type"]=>
NULL
["object"]=>
NULL
["args"]=>
array(0) {
}
["object_class"]=>
NULL
["closure"]=>
NULL
}
44 changes: 44 additions & 0 deletions Zend/tests/stack_frame_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
StackFrame read dimensions
--FILE--
<?php

function frames() {
return StackFrame::getTrace();
}
$frame = frames()[0];

var_dump([
'file' => $frame['file'],
'line' => $frame['line'],
'function' => $frame['function'],
'class' => $frame['class'],
'type' => $frame['type'],
'object' => $frame['object'],
'args' => $frame['args'],
'object_class' => $frame['object_class'],
'closure' => $frame['closure'],
]);

--EXPECTF--
array(%d) {
["file"]=>
string(%d) "%s.php"
["line"]=>
int(%d)
["function"]=>
string(6) "frames"
["class"]=>
NULL
["type"]=>
NULL
["object"]=>
NULL
["args"]=>
array(0) {
}
["object_class"]=>
NULL
["closure"]=>
NULL
}
44 changes: 44 additions & 0 deletions Zend/tests/stack_frame_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
StackFrame getters
--FILE--
<?php

function frames() {
return StackFrame::getTrace();
}
$frame = frames()[0];

var_dump([
'file' => $frame->getFile(),
'line' => $frame->getLine(),
'function' => $frame->getFunction(),
'class' => $frame->getClass(),
'type' => $frame->getType(),
'object' => $frame->getObject(),
'args' => $frame->getArgs(),
'object_class' => $frame->getObjectClass(),
'closure' => $frame->getClosure(),
]);

--EXPECTF--
array(%d) {
["file"]=>
string(%d) "%s.php"
["line"]=>
int(%d)
["function"]=>
string(6) "frames"
["class"]=>
NULL
["type"]=>
NULL
["object"]=>
NULL
["args"]=>
array(0) {
}
["object_class"]=>
NULL
["closure"]=>
NULL
}
139 changes: 104 additions & 35 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1927,16 +1927,16 @@ ZEND_FUNCTION(debug_print_backtrace)

/* }}} */

ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit, int as_objects) /* {{{ */
{
zend_execute_data *ptr, *skip, *call = NULL;
zend_object *object;
zend_object *object, *frame_object = NULL;
int lineno, frameno = 0;
zend_function *func;
zend_string *function_name;
zend_string *filename;
zend_string *include_filename = NULL;
zval stack_frame, tmp;
zval stack_frame, tmp, tmp2;

array_init(return_value);

Expand Down Expand Up @@ -1969,7 +1969,14 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int

while (ptr && (limit == 0 || frameno < limit)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using limit=0 to mean "unlimited", can we please use INT_MAX? It will simplify the code here:

	while (ptr && frameno < limit) {

We can add a constant for it if it helps comprehension.

Aside from simplifying code I much prefer it when 0 isn't used for "infinite" or "max", because as we --limit the stack length with get smaller and smaller until we hit 0, and then magically it gets bigger again? INT_MAX is a much better fit, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morrisonlevi thanks for comments on this. I'll consider it if I start with this implementation as a separate PECL extension.

frameno++;
array_init(&stack_frame);
if (as_objects == 1) {
frame_object = zend_objects_new(zend_ce_stack_frame);
ZVAL_OBJ(&stack_frame, frame_object);
object_properties_init(frame_object, zend_ce_stack_frame);
frame_object->handlers = &zend_stack_frame_handlers;
} else {
array_init(&stack_frame);
}

ptr = zend_generator_check_placeholder_frame(ptr);

Expand All @@ -1990,10 +1997,15 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
} else {
lineno = skip->opline->lineno;
}
ZVAL_STR_COPY(&tmp, filename);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
ZVAL_LONG(&tmp, lineno);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
if (as_objects == 1) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 0), filename);
ZVAL_LONG(OBJ_PROP_NUM(frame_object, 1), lineno);
} else {
ZVAL_STR_COPY(&tmp, filename);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
ZVAL_LONG(&tmp, lineno);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
}

/* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
* and debug_baktrace() might have been called by the error_handler. in this case we don't
Expand All @@ -2010,10 +2022,15 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
break;
}
if (prev->func && ZEND_USER_CODE(prev->func->common.type)) {
ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
ZVAL_LONG(&tmp, prev->opline->lineno);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
if (as_objects == 1) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 0), prev->func->op_array.filename);
ZVAL_LONG(OBJ_PROP_NUM(frame_object, 1), prev->opline->lineno);
} else {
ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
ZVAL_LONG(&tmp, prev->opline->lineno);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
}
break;
}
prev_call = prev;
Expand All @@ -2034,37 +2051,81 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
}

if (function_name) {
ZVAL_STR_COPY(&tmp, function_name);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
if (as_objects == 1) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 2), function_name);
} else {
ZVAL_STR_COPY(&tmp, function_name);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
}

if (object) {
if (func->common.scope) {
ZVAL_STR_COPY(&tmp, func->common.scope->name);
} else if (object->handlers->get_class_name == zend_std_get_class_name) {
ZVAL_STR_COPY(&tmp, object->ce->name);
if (as_objects == 1) {
if (func->common.scope) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 3), func->common.scope->name);
} else if (object->handlers->get_class_name == zend_std_get_class_name) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 3), object->ce->name);
} else {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 6), object->handlers->get_class_name(object));
}
if (object->handlers->get_class_name == zend_std_get_class_name) {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 6), object->ce->name);
} else {
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 6), object->handlers->get_class_name(object));
}
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
ZVAL_OBJ_COPY(&tmp2, object);
zend_create_closure(&tmp, func, func->common.scope, object->ce, &tmp2);
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_CALLABLE), &tmp);
}
if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
ZVAL_OBJ_COPY(&tmp, object);
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp);
}
zval_ptr_dtor(OBJ_PROP_NUM(frame_object, 4));
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 4), ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
} else {
ZVAL_STR(&tmp, object->handlers->get_class_name(object));
}
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
ZVAL_OBJ_COPY(&tmp, object);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp);
if (func->common.scope) {
ZVAL_STR_COPY(&tmp, func->common.scope->name);
} else if (object->handlers->get_class_name == zend_std_get_class_name) {
ZVAL_STR_COPY(&tmp, object->ce->name);
} else {
ZVAL_STR(&tmp, object->handlers->get_class_name(object));
}
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
ZVAL_OBJ_COPY(&tmp, object);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp);
}
ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
}

ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
} else if (func->common.scope) {
ZVAL_STR_COPY(&tmp, func->common.scope->name);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
if (as_objects == 0) {
ZVAL_STR_COPY(&tmp, func->common.scope->name);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_CLASS), &tmp);
ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_TYPE), &tmp);
} else {
zval_ptr_dtor(OBJ_PROP_NUM(frame_object, 3));
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 3), func->common.scope->name);
zval_ptr_dtor(OBJ_PROP_NUM(frame_object, 4));
ZVAL_STR_COPY(OBJ_PROP_NUM(frame_object, 4), ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
if (func->common.fn_flags & ZEND_ACC_CLOSURE) {
zend_create_closure(&tmp, func, func->common.scope, NULL, NULL);
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_CALLABLE), &tmp);
}
}
}

if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
func->type != ZEND_EVAL_CODE) {

debug_backtrace_get_args(call, &tmp);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
if (as_objects == 0) {
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
} else {
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
}
}
} else {
/* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
Expand Down Expand Up @@ -2112,11 +2173,19 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int

ZVAL_STR_COPY(&tmp, include_filename);
zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array);
if (as_objects == 0) {
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array);
} else {
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp);
}
}

ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
if (as_objects == 0) {
zend_hash_add_new(Z_ARRVAL(stack_frame), ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
} else {
zend_update_property_ex(zend_ce_stack_frame, &stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp);
}
}

zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &stack_frame);
Expand All @@ -2140,7 +2209,7 @@ ZEND_FUNCTION(debug_backtrace)
RETURN_THROWS();
}

zend_fetch_debug_backtrace(return_value, 1, options, limit);
zend_fetch_debug_backtrace(return_value, 1, options, limit, 0);
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
int zend_startup_builtin_functions(void);

BEGIN_EXTERN_C()
ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit);
ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit, int as_objects);
END_EXTERN_C()

#endif /* ZEND_BUILTIN_FUNCTIONS_H */
Loading