Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get PHP to produce a backtrace upon errors?

Trying to debug PHP using its default current-line-only error messages is horrible. How can I get PHP to produce a backtrace (stack trace) when errors are produced?

like image 228
chaos Avatar asked Jul 21 '09 13:07

chaos


People also ask

What is a PHP backtrace?

Definition and Usage. The debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. Returns an array of associative arrays.

How can I trace PHP code?

In any function you can see the whole backtrace by using debug_backtrace(...) Or you can use Xdebug profiler to profile your PHP scripts. debug_backtrace is put on a specific place in a PHP script but I want to see a log of whole code execution - from start to the end.

What is stack trace PHP?

So the stack trace is a list of the functions in the call stack at the point an exception is triggered. Throwing an exception is how the interpreter tells you an error has happened. This could be from a mistyped variable, incorrect input, or any type of exception that you've defined in your code.


2 Answers

My script for installing an error handler that produces a backtrace:

<?php function process_error_backtrace($errno, $errstr, $errfile, $errline, $errcontext) {     if(!(error_reporting() & $errno))         return;     switch($errno) {     case E_WARNING      :     case E_USER_WARNING :     case E_STRICT       :     case E_NOTICE       :     case E_USER_NOTICE  :         $type = 'warning';         $fatal = false;         break;     default             :         $type = 'fatal error';         $fatal = true;         break;     }     $trace = array_reverse(debug_backtrace());     array_pop($trace);     if(php_sapi_name() == 'cli') {         echo 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";         foreach($trace as $item)             echo '  ' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()' . "\n";     } else {         echo '<p class="error_backtrace">' . "\n";         echo '  Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";         echo '  <ol>' . "\n";         foreach($trace as $item)             echo '    <li>' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()</li>' . "\n";         echo '  </ol>' . "\n";         echo '</p>' . "\n";     }     if(ini_get('log_errors')) {         $items = array();         foreach($trace as $item)             $items[] = (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()';         $message = 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ': ' . join(' | ', $items);         error_log($message);     }     if($fatal)         exit(1); }  set_error_handler('process_error_backtrace'); ?> 

Caveat: it is powerless to affect various 'PHP Fatal Errors', since Zend in their wisdom decided that these would ignore set_error_handler(). So you still get useless final-location-only errors with those.

like image 51
chaos Avatar answered Oct 14 '22 02:10

chaos


Xdebug prints a backtrace table on errors, and you don't have to write any PHP code to implement it.

Downside is you have to install it as a PHP extension.

like image 42
patcoll Avatar answered Oct 14 '22 03:10

patcoll