Search code examples
phpdebug-backtrace

How do I get a PHP full call trace, not only stack call trace?


Is the following code PHP:

<?php

function a() {
    b();
}

function b() {
    c();
}

function c(){
    debug_print_backtrace();
}

function d() {
    echo "I am a function d(). Where on the list is my call?\n";
}

d();

a();

?>

The above example will output something similar to:

I am a function d(). Where on the list is my call?
#0  c() called at [D:\tests.php:18]
#1  b() called at [D:\tests.php:14]
#2  a() called at [D:\tests.php:31]

Is there a way to get the full call trace, not only call stack trace?


Solution

  • Yes, there is, but this can only be done by monitoring the whole script during execution time. It is called "profiling".

    XDebug is one of the tools that allows this. See http://www.xdebug.org/docs/profiler for more details. Note that XDebug writes it's analysis data into a directory, so there is no direct access from within PHP to the data during execution (and this would corrupt the profiling data as well, because these calls will also be monitored).

    There also is XHProf http://php.net/manual/en/book.xhprof.php which allows to be turned on and off within PHP, as well as provide a HTML GUI. The example looks reasonably easy.