Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is debug_backtrace() not including line number sometimes?

Tags:

php

debugging

I'm finding that sometimes debug_backtrace() is not including the line number for a call. Is there some reason why this is and any way to correct for it?

Thanks in advance.

P.S. And yes, the calls that it is omit line numbers for are my own code, not internal PHP code.

like image 995
MikeSchinkel Avatar asked Jan 03 '11 03:01

MikeSchinkel


People also ask

How do I get PHP to produce a Backtrace upon error?

For more advanced solution, you can use XDebug extension for PHP. By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug. auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.

What is stack trace in PHP?

But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened.

What is the Backtrace in PHP?

Definition and UsageThe debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. The current call type.

How do you debug a backtrace?

To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .


2 Answers

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 :)

like image 130
Dariusz Walczak Avatar answered Nov 10 '22 15:11

Dariusz Walczak


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.

like image 44
Phill Pafford Avatar answered Nov 10 '22 14:11

Phill Pafford