rfc:error_backtraces

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:error_backtraces [2020/05/26 21:29] – created maxsemrfc:error_backtraces [2022/04/17 18:30] (current) – Move to inactive ilutov
Line 1: Line 1:
-====== PHP RFC: Your Title Here ======+====== PHP RFC: Error backtraces ======
   * Version: 1.0   * Version: 1.0
   * Date: 2020-05-25   * Date: 2020-05-25
   * Author: Max Semenik, maxsem.wiki@gmail.com   * Author: Max Semenik, maxsem.wiki@gmail.com
-  * Status: Draft+  * Status: Inactive
   * First Published at: https://wiki.php.net/rfc/error_backtraces   * First Published at: https://wiki.php.net/rfc/error_backtraces
  
Line 9: Line 9:
 PHP errors, unlike exceptions, are much more limited in programmers' ability to control their handling and get detailed information about them. I propose to fix some of these issues by recording a backtrace for every error. PHP errors, unlike exceptions, are much more limited in programmers' ability to control their handling and get detailed information about them. I propose to fix some of these issues by recording a backtrace for every error.
  
-While it's possible to somewhat address this with ''set_error_handler()'' and ''register_shutdown_function()''+This problem is so serious thatfor example, Wikimedia has created [[https://github.com/wikimedia/php-wmerrors|an extension]] for this.
  
 ===== Proposal ===== ===== Proposal =====
-Introduce a new INI setting, ''debug_backtraces'' that allows to turn this feature on and off, as well as to limit the number of stack frames outputted. Zero would mean no backtraces, a positive value means output this number of frames, negative value for no limitBy default it would be on in development settings but off in production. People who don't want to have the overhead of having to generate backtraces in production can disable this functionality+Add backtraces to error output.
  
-**Error output**: traces will be formatted exactly like for exceptions, e.g.:+**Error output**: traces will be formatted exactly like for exceptions, and appended to error messages, e.g.:
 <code> <code>
 Fatal error: Allowed memory size of 134217728 bytes exhausted in /var/www/test.php on line 123 Fatal error: Allowed memory size of 134217728 bytes exhausted in /var/www/test.php on line 123
Line 22: Line 22:
 #3 {main} #3 {main}
 </code> </code>
 +
 +**Logging**: backtraces will be logged just like for exceptions.
 +
 +**''error_get_last()''** output will have another element added to it if a trace is available:
 +<code>
 +#1 {main}
 +array(5) {
 +  ["type"]=>
 +  int(1)
 +  ["message"]=>
 +  string(22) "Error, terrible error!"
 +  ["file"]=>
 +  string(17) "/var/www/test.php"
 +  ["line"]=>
 +  int(3)
 +  ["backtrace"]=>
 +  string(123) "#0 /var/www/test.php(345): $this->causeTerribleError()
 +#1 {main}"
 +}
 +</code>
 +
 +**Configuration**: while always having backtraces is nice for debugging, some people might not like the performance overhead of generating backtraces or the risk of having to generate a trace after a fatal error. I therefore propose to address this with an INI setting, ''debug_backtraces'' that allows to turn this feature on and off, as well as to limit the number of stack frames outputted:
 +  * 0 - no backtraces
 +  * positive number - output this number of frames
 +  * negative value - no limit
 +By default it would be on in development settings but off in production.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-Everything will behave exactly as before with backtraces disabled, while turning them on would introduce a few minor changes to how errors are represented. Should not be a problem.+  * In the current proposed implementation, a new error flag ''E_UNHANDLED_EXCEPTION'' is introduced to [[https://github.com/php/php-src/pull/5642/files#diff-1a9cfc6173e3a434387996e46086da56R1310|avoid outputting backtraces for unhandled exceptions twice]], though it's not included in ''E_ALL'' and is invisible for userspace. 
 +  * From the userspace POV, everything will behave exactly as before with backtraces disabled, while turning them on would introduce a few minor changes to how errors are represented.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
 8.0 8.0
  
-==== php.ini Defaults ====+===== php.ini Defaults =====
 For ''error_backtraces'': For ''error_backtraces'':
   * Default value: 0   * Default value: 0
Line 39: Line 66:
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
-Everything other the minor stuff mentioned in //Backward Incompatible Changes//.+Everything other than the minor stuff mentioned in //Backward Incompatible Changes//.
  
 ===== Future Scope ===== ===== Future Scope =====
Line 45: Line 72:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Accept this RFC (yes/no)?+  * Accept this RFC (yes/no)?
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-  * WIP patch:+  * WIP patch: https://github.com/php/php-src/pull/5642
  
 ===== References ===== ===== References =====
rfc/error_backtraces.1590528585.txt.gz · Last modified: 2020/05/26 21:29 by maxsem