The Linux Page

Trace PHP code with xdebug

xdebug

XDebug "logo"I'm very much working on Drupal and once in a while, I just have no clue what the code does... especially when there are callbacks that call callbacks in forever loops.

In those situation, I want to track what's going on. PHP, by default, does not really offer any way of tracking what's happening. Yet, there is a way: xdebug. This is a 3rd party library which you can install with apt-get on Ubuntu and other Debian like systems:

  sudo apt-get install php5-xdebug

Once installed, you may want to change a few things in the .ini file in regard to that new module. I don't think I had to change anything though. I think the defaults are pretty much what you need.

Now, to trace code, you want to use the following two functions:

  xdebug_start_trace($output_filename_with_extension [, $flags]);
  [... php code being traced ...]
  xdebug_stop_trace();

The result of the trace is saved in the specified file: $output_filename_with_extension. As mentioned, the filename must include an extension which is ignored whether or not it is .tx (what xdebug adds). But without an extension, somehow, xdebug ignores the filename and does not trace anything.

Remember that it is likely to generate enormous files. So be careful.

You can find detailed information in the xdebug website.

What about a stack trace?

If what you are looking for is just a stack trace, then you use look into the debug_print_backtrace() function instead. This is part of PHP by default and thus you don't even need to install anything to get to work.

SMS From Me Logo

SMS From Me

To automatically start one on one conversations with your online leads.