Skip to content

Proposal: Add debug_backtrace_depth(int $limit=0): int #6653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Jan 30, 2021

This can be polyfilled with
min($limit, count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))).
or less accurately as count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit))

  • For debug_backtrace, the limit is to the number of frames scanned,
    not the size of the returned array.
  • For debug_backtrace_depth, the limit is on the returned depth)

It's faster just to not read the filenames, lines, create temporary arrays, etc.

This can be useful if you are checking for infinite recursion
to investigate a bug in an application.
E.g. return debug_backtrace_depth(1000) >= 1000 would check
if php is more than 1000 stack frames of debug_backtrace deep.

This may have other uses, such as more efficiently
computing an indentation level in temporary debug logging statements.

Processing Node A
  Processing Node B
    Processing Leaf C
    Processing Leaf D

Note that php stack frames are a linked list - the amount of time to compute the
depth is proportional to the depth, and some frames do not show up in backtraces.
This PHP stack is separate from the C stack.

RFC: https://wiki.php.net/rfc/debug_backtrace_depth

@cmb69
Copy link
Member

cmb69 commented Mar 10, 2021

It might be reasonable to propose this on internals, if not already done.

Also, please fix the merge conflicts. :)

This can be polyfilled with
`min($limit, count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)))`.

- For debug_backtrace, the limit is to the number of frames scanned,
  not the size of the returned array.
- For debug_backtrace_depth, the limit is on the returned depth)

It's faster just to not read the filenames, lines, create temporary arrays, etc.

This can be useful if you are checking for infinite recursion
to investigate a bug in an application.
E.g. `return debug_backtrace_depth(1000) >= 1000` would check
if php is more than 1000 stack frames of debug_backtrace deep.

This may have other uses, such as more efficiently
computing an indentation level in logging statements.

```
Processing Node A
  Processing Node B
    Processing Leaf C
    Processing Leaf D
```

Note that php stack frames are a linked list - the amount of time to compute the
depth is proportional to the depth, and some frames do not show up in backtraces.
This PHP stack is separate from the C stack.
@TysonAndre TysonAndre force-pushed the debug_backtrace_depth branch from 17df31b to 6bf14b6 Compare March 13, 2021 16:21
@github-actions
Copy link

github-actions bot commented Feb 5, 2022

There has not been any recent activity in this PR. It will automatically be closed in 7 days if no further action is taken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants