7 devel.moduleddebug_backtrace($return = FALSE, $pop = 0, $options = TRUE)
5 devel.moduleddebug_backtrace()
6 devel.moduleddebug_backtrace($pop = 0)
8 devel.moduleddebug_backtrace($return = FALSE, $pop = 0, $options = DEBUG_BACKTRACE_PROVIDE_OBJECT)

Prints the function call stack.

Parameters

$return: Pass TRUE to return the formatted backtrace rather than displaying it in the browser via kprint_r().

$pop: How many items to pop from the top of the stack; useful when calling from an error handler.

$options: Options to pass on to PHP's debug_backtrace(), depending on your PHP version.

Return value

string|NULL The formatted backtrace, if requested, or NULL.

See also

http://php.net/manual/en/function.debug-backtrace.php

1 call to ddebug_backtrace()

File

./devel.module, line 1951
This module holds functions useful for Drupal development. Please contribute!

Code

function ddebug_backtrace($return = FALSE, $pop = 0, $options = TRUE) {
  if (user_access('access devel information')) {
    $backtrace = debug_backtrace($options);
    while ($pop-- > 0) {
      array_shift($backtrace);
    }
    $counter = count($backtrace);
    $path = $backtrace[$counter - 1]['file'];
    $path = substr($path, 0, strlen($path) - 10);
    $paths[$path] = strlen($path) + 1;
    $paths[DRUPAL_ROOT] = strlen(DRUPAL_ROOT) + 1;
    $nbsp = "\xC2\xA0";

    // Show message if error_level is ERROR_REPORTING_DISPLAY_SOME or higher.
    // (This is Drupal's error_level, which is different from $error_level,
    // and we purposely ignore the difference between _SOME and _ALL,
    // see #970688!)
    if (variable_get('error_level', 1) >= 1) {
      while (!empty($backtrace)) {
        $call = array();
        if (isset($backtrace[0]['file'])) {
          $call['file'] = $backtrace[0]['file'];
          foreach ($paths as $path => $len) {
            if (strpos($backtrace[0]['file'], $path) === 0) {
              $call['file'] = substr($backtrace[0]['file'], $len);
            }
          }
          $call['file'] .= ':' . $backtrace[0]['line'];
        }
        if (isset($backtrace[1])) {
          if (isset($backtrace[1]['class'])) {
            $function = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . '()';
          }
          else {
            $function = $backtrace[1]['function'] . '()';
          }
          $backtrace[1] += array('args' => array());
          $call['args'] = $backtrace[1]['args'];
        }
        else {
          $function = 'main()';
          $call['args'] = $_GET;
        }
        $nicetrace[($counter <= 10 ? $nbsp : '') . --$counter . ': ' . $function] = $call;
        array_shift($backtrace);
      }
      if ($return) {
        return $nicetrace;
      }
      kprint_r($nicetrace);
    }
  }
}

Comments

This function is *essential* to a developers toolkit, double essential if you don't use a PHP debugger!

Please permit me to agree with and even magnify your comment @Elijah Lynn. The function is triple essential!

dpm() is handy when you generate an error in a module you're writing and you know exactly what to debug. Well, what happens when your module interacts with a core or contrib module in some unanticipated way? And to further confound you, the system-generated errors only point to the core or contrib module? You temporarily hack the file that reports the error and put in ddebug_backtrace(); and BLAM! you can trace back through the function calls until you find your custom module that was causing the problem in the first place.

Yay for ddebug_backtrace().

-James