How to Get PHP to Produce a Backtrace Upon Errors

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

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.

How can I save a PHP backtrace to the error log?

This should generate a readable string:

error_log(print_r(debug_backtrace(), true));

Additionally, debug_print_backtrace() prints the back trace as string and its output can be captured with regular output buffer functions:

ob_start();
debug_print_backtrace();
error_log(ob_get_clean());

What is meant by backtrace in php why we use debug_backtrace() and debug_print_backtrace() functions in php?

PHP backtrace family of functions let you understand how the functions have been called in the particular request.

For example if you have called function foo() before function bar(), then if you write debug print backtrace in bar() function then you will see foo() has been called before bar() function. This also provides you what arguments you have passed to bar() function.

This helps you to trace back where and what functions are called to reach the current line of code (where you have written the backtrace function)

How do I get PHP to log a stacktrace upon fatal error

Well, as has already been pointed out, you're not getting the same formatting on the live machine because the live machine hasn't got xdebug installed. There is debug_backtrace but that wouldn't catch a fatal error.

You could install xdebug on the live server, but you'd have to be very careful to configure it to expose no functionality except for the stack trace logging. Incautious use of xdebug on a live box could pose a security risk as people could initiate a remote debug session, or its enhanced error messages could inadvertently echo out internal details of your code.

To be honest? I'd think your best option is to try and recreate the circumstances under which the error the live server logged occurred on your test server.

EDIT TO ADD: Forgot to mention that in addition to being a security risk, xDebug will also have a negative impact on your website's performance. It hooks into Zend Engine in several crucial ways to log programme state and alter its behaviour (such as overriding @ error suppression), and this will have an inevitable impact on performance. You're basically far better off trying to replicate the issue on a testing environment than you are adding debugging tools to a live one.

PHP 7.0.2 performs backtrace on error

The problem I was facing was not with PHP 7.0.2 itself, but with an extension that had somehow become enabled called XDebug. XDebug which is intended for advanced error reporting was causing problems in another project of mine and by disabling it, it seemed to solve my problem. In order to disable XDebug all you need to do is modify your php.ini file and change the values debug.remote_autostart, debug.default_enable, and debug.remote_enable from 'on' to 'off'.

Print PHP Call Stack

If you want to generate a backtrace, you are looking for debug_backtrace and/or debug_print_backtrace.


The first one will, for instance, get you an array like this one (quoting the manual) :

array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}


They will apparently not flush the I/O buffer, but you can do that yourself, with flush and/or ob_flush.

(see the manual page of the first one to find out why the "and/or" ;-) )



Related Topics



Leave a reply



Submit