Tracing each function calls in PHP

3.4k Views Asked by At

I am trying to create a tracer log in my PHP application. Turning on the debug mode will start to track the flow of the function.

Below is what I am expecting as my implementation:

function callA(x) { ... }  //XYZ.php
function callB() { ... callA(1) ... }  //ABC.php

callB(); //main.php

I want to track the flow of the PHP code.

I know that I can use debug_backtrace, but in this case, I need to explicitely call debug_backtrace in one of the function.

However, what I want is to automatically track the flow of the code.

Is there any way to do it.

1

There are 1 best solutions below

1
On

Normally, this would be done with a profiler like xdebug that runs as a PHP extension. But if you must trace function calls through code written in PHP itself, the best you can do is use register_tick_function which calls your "tick function"every few native CPU cycles instead of waiting for another function call.

declare(ticks = 1);
$calls = array();
function tracer() {
    global $calls;
    $calls[] = array_shift(debug_backtrace());
}
register_tick_function('tracer');