I often have problems with the standard backtracing functions when working with Drupal:

  • debug_print_backtrace(); often seems to get lost between pages and never prints,
  • Drupal's objects and classes are often so big that debug( debug_backtrace(), "Backtrace", true ); can kill my server with out of memory errors, and debug( debug_backtrace() ); almost always fails with a "Nested dependency" recursion error.

Is there any alternative to the above approaches that can be used reliably, ideally as a drop-in function without needing to set up any framework?


I find this useful as a less memory intensive Drupal-friendly way to backtrace PHP as a one-off code snippet to temporarily drop into a troublesome location without installing modules or server-side tools:

  $trace = debug_backtrace();
  $backtrace_lite = array();
  foreach( $trace as $call ) {
    $backtrace_lite[] = $call['function']."    ".$call['file']."    line ".$call['line'];
  debug( $backtrace_lite, "TRACE", true );

It gets the backtrace into Drupal's debugging system, but strips it down to the essentials to avoid failures when massive objects are involved.

For handy reference, the other keys within each row of the debug_backtrace() object are:

  • class
  • object
  • type
  • args

Note - this answer was first posted in the now-deleted too-broad question How do I debug Drupal?


More compact variant:

  $backtrace_lite = array_map(function($call_item) {
    return "{$call_item['function']} -> {$call_item['file']} -> line {$call_item['line']}";
  }, debug_backtrace());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.