rfc:debug_backtrace_depth

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:debug_backtrace_depth [2021/03/13 17:12] tandrerfc:debug_backtrace_depth [2021/06/02 02:05] (current) tandre
Line 3: Line 3:
   * Date: 2021-03-13   * Date: 2021-03-13
   * Author: Tyson Andre, tandre@php.net   * Author: Tyson Andre, tandre@php.net
-  * Status: Under Discussion+  * Status: Withdrawn
   * First Published at: https://wiki.php.net/rfc/debug_backtrace_depth   * First Published at: https://wiki.php.net/rfc/debug_backtrace_depth
   * Implementation: https://github.com/php/php-src/pull/6653   * Implementation: https://github.com/php/php-src/pull/6653
Line 9: Line 9:
 ===== Introduction ===== ===== Introduction =====
  
-Inspecting the current stack trace depth is occasionally useful for manually debugging, checking for potential infinite recursion, or for checking if code would hit stack frame limits in extensions such as XDebug (''xdebug.max_nesting_limit''). It is currently possible to compute the depth through ''count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit=0))'', but this is verbose and inefficient compared to returning the depth directly.+Inspecting the current stack trace depth is occasionally useful for manually debugging, checking for potential infinite recursion, or for checking if code would hit stack frame limits in extensions such as Xdebug (''xdebug.max_nesting_limit''). It is currently possible to compute the depth through ''count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit=0))'', but this is verbose and inefficient compared to returning the depth directly.
  
 ===== Proposal ===== ===== Proposal =====
Line 24: Line 24:
 When ''$limit <= 0'', there is no limit. When ''$limit <= 0'', there is no limit.
  
-Internally, PHP's stack frames are represented like a linked list (i.e. to count them, you need to iterate through the frames), and this RFC does not change that representation. The amount of time needed to compute the depth is proportional to the depth, which is why this RFC supports the optional ''$limit''. Counting the frames is still much faster than constructing an array with the ''file'', ''line'', ''class'', etc. with ''debug_backtrace()''.+Internally, PHP's stack frames are represented like a singly linked list (i.e. to count them, you need to iterate through the frames), and this RFC does not change that representation. The amount of time needed to compute the depth is proportional to the depth, which is why this RFC supports the optional ''$limit''. Counting the frames is still much faster than constructing an array with the ''file'', ''line'', ''class'', etc. with ''debug_backtrace()''.
  
 ==== Example use cases ==== ==== Example use cases ====
Line 53: Line 53:
 === Checking for compatibility with extensions with stack trace limits === === Checking for compatibility with extensions with stack trace limits ===
  
-For example, in XDebug https://xdebug.org/docs/all_settings#max_nesting_level has a default value of 256 - if you are developing an application or library which cannot override max_nesting_level you may want to ensure your library does not use deep recursion.+For example, in Xdebug https://xdebug.org/docs/all_settings#max_nesting_level has a default value of 256 - if you are developing an application or library which cannot override max_nesting_level you may want to ensure your library does not use deep recursion.
  
 <code php> <code php>
Line 59: Line 59:
 function my_common_function() { function my_common_function() {
     if (is_running_as_unit_test() && debug_backtrace_depth(250) >= 250) {     if (is_running_as_unit_test() && debug_backtrace_depth(250) >= 250) {
-        throw new TestFrameworkError("Refactor this code, it's likely to hit the default xdebug.max_nesting_level in other environments");+        throw new TestFrameworkError("Refactor this code, xdebug.max_nesting_level may be reached in other environments");
     }     }
     // body of my_common_function()     // body of my_common_function()
Line 107: Line 107:
 ===== Future Scope ===== ===== Future Scope =====
 The behavior of ''debug_backtrace'' and ''debug_print_backtrace'' could be changed to make $limit be consistent with the documentation in a separate RFC. https://www.php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters. The behavior of ''debug_backtrace'' and ''debug_print_backtrace'' could be changed to make $limit be consistent with the documentation in a separate RFC. https://www.php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters.
 +
 +===== Proposed PHP Version =====
 +8.1
 +
 +===== Discussion =====
 +
 +==== Limited to a specific use case ====
 +
 +https://externals.io/message/113502#113821
 +
 +<blockquote>
 +There hasn't been much discussion on this one, probably because the
 +functionality and use cases are so specific. I'm not really convinced by
 +your examples.
 +
 +For the purpose of printing debug logs, using an engine-provided depth is
 +non-ideal, because any intermediate helper functions will count towards the
 +depth. Tracking your own depth will result in a more meaningful/predictable
 +output.
 +
 +For tracking down infinite recursion, isn't this what the xdebug recursion
 +limit is for? You'll directly get your recursive stack trace, which should
 +make it obvious where the infinite recursion occurs. Using
 +debug_backtrace_depth() for this purpose means that you actually already
 +need to know where you are infinitely recursing in order to place the guard.
 +
 +If you want to find places where your code would hit the xdebug recursion
 +limit, then why not run it under xdebug and find out, rather than trying to
 +simulate the same behavior manually? That again requires that you actually
 +place relevant guards in the first place, which makes this another chicken
 +and egg problem.
 +</blockquote>
 +
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
 Yes/No, requiring a 2/3 majority Yes/No, requiring a 2/3 majority
rfc/debug_backtrace_depth.1615655567.txt.gz · Last modified: 2021/03/13 17:12 by tandre