Created
June 14, 2013 10:53
-
-
Save rmbl/5780995 to your computer and use it in GitHub Desktop.
Backtrace on fatal_error in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function shutdown() { | |
$error = error_get_last(); | |
if ($error['type'] === E_ERROR) { | |
// fatal error has occured | |
$trace = array_reverse($GLOBALS['dbg_stack']); | |
array_pop($trace); | |
if(php_sapi_name() == 'cli') { | |
echo 'Backtrace for: \'' . $error['message'] . '\' at ' . $error['file'] . ':' . $error['line'] . ':' . "\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 for: \'' . $error['message'] . '\' at ' . $error['file'] . ':' . $error['line'] . ':' . "\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"; | |
} | |
} | |
} | |
register_shutdown_function('shutdown'); | |
function write_dbg_stack() { | |
$GLOBALS['dbg_stack'] = debug_backtrace(); | |
} | |
register_tick_function('write_dbg_stack'); | |
declare(ticks=1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment