Why is debug_backtrace() not including line number sometimes?

Regardless of any "bug", "feature", whatever the folks who maintain PHP would like to say, the debug_backtrace() does not work as I would expect.

Here is my solution (it is ugly, but it works for me):

function dbg($msg="")
{
    ob_start();
    debug_print_backtrace(0,1);
    $_ = ob_get_clean();
    list($ignore,$line_number) = explode(':', $_);
    $line_number += 0;

    $backtrace = debug_backtrace(0);
    extract($backtrace[1]);
    echo "<pre>$class::$function($msg) : $line_number</pre>";
}

The PHP function debug_print_backtrace(0,1); will produce something like this:

#0 dbg->ping(290) called at[/path/to/filename.php:290]

Since it only echos the trace, I have to ob_get_clean() it as a string. Then I parse it.

In my implementation, I only want to know the class, function, line number, and, optionally, a string message from the calling function. debug_backtrace() provides class and function correctly, but not line-number. That is why I have to get the line number from the debug_print_backtrace() function.

For bonus points.... how is it that debug_print_backtrace() function "knows" the line number, but debug_backtrace() [occasionally] does not, eh??? ... it's a mystery...


Consider following code:

<?
class BtTest
{
  public function getTheItem()
  {
    var_dump( debug_backtrace( false ) );
    $bt = debug_backtrace( false );
    return $bt[1];
  }

  public function __call( $methodName, $methodArgs )
  {
    return $this->getTheItem();
  }
}

$o = new BtTest();
$bti = $o->test();

assert( 'array_key_exists("function", $bti)' );
assert( 'array_key_exists("line", $bti)' );
assert( 'array_key_exists("file", $bti)' );

The execution of above example generates following output:

array(3) {
  [0]=>
  array(6) {
    ["file"]=>
    string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(10) "getTheItem"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["function"]=>
    string(6) "__call"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(2) {
      [0]=>
      &string(4) "test"
      [1]=>
      &array(0) {
      }
    }
  }
  [2]=>
  array(6) {
    ["file"]=>
    string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(4) "test"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
PHP Warning:  assert(): Assertion "array_key_exists("line", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 21
PHP Warning:  assert(): Assertion "array_key_exists("file", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 22

The first backtrace item (index 0) says indirectly (through the line and file items) that the getTheItem method was called from the __call method.

The second backtrace item (index 1) says that the __call method was called from somewhere (missing line and file items).

The third backtrace item (index 2) says that the test method was called from the global scope of the script.

The place of the __call method call is probably in some method resolution code somewhere in the php interpreter code. There are two possibilities of fixing it. Either the second item should refer interpreter's source code file and line or the second and the third backtrace items should be merged into one. I personally would prefer the second solution as the interpreter's internals are not interesting for me (this is how they seem to do it in python's traceback), however I understand that sometimes the first solution provides more explicit trace (especially when it's a callback which is called from the internals).

So or so, it seems that the developer(s) responsible for (or at least maintaining) the code of the debug_backtrace function doesn't perceive it as a bug or maybe has no easy way to fix it. It would be ok to fill the line and file items with some place holder values (e.g. <unknown-file> and 0 or even nulls) and emphasize it in the documentation. Unless someone will successfully convince them to do it, you just have to handle the special case in your code.

I wrote above just to share my understanding of the strange behaviour of the function. If someone has a willingness to fight for a slightly better world, here are links to some related bug reports:

  • #39070 debug_backtrace output when call_user_func or error handler involved
  • #24214 debug_backtrace() fails to report __FILE__, __LINE__
  • #24405 debug_backtrace - missing info
  • #38047 "file" and "line" sometimes not set in backtrace from inside error handler
  • #44428 "file" and "line" missing in debug_backtrace() output

The oldest report is from 2003, so you shouldn't count on a fast fix :)


I think this is listed as a PHP Bug

The debug backtrace shows filename and lineno of calling script. In case function is called from inside internal function (may be as callback) no filename and lineno may be set.


After checking a bunch of related issues on bugs.php.net on various PHP versions, this problem seems to have been fixed on PHP 7.0 and later, including 8.0, which always provide a 'file', even if it is fake like 'Standard input code' for code evaluated from stdin.

Tags:

Php

Debugging