Same name and namespace in other branches
  1. 5.x-1.x devel.module \ddebug_backtrace()
  2. 6.x-1.x devel.module \ddebug_backtrace()
  3. 7.x-1.x devel.module \ddebug_backtrace()

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().

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()
backtrace_error_handler in ./devel.module
Displays backtrace showing the route of calls to the current error.

File

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

Code

function ddebug_backtrace($return = FALSE, $pop = 0, $options = DEBUG_BACKTRACE_PROVIDE_OBJECT) {
  if (\Drupal::currentUser()
    ->hasPermission('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 = " ";

    // 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 (\Drupal::config('system.logging')
      ->get('error_level') != 'hide') {
      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(),
          );
          foreach ($backtrace[1]['args'] as $key => $value) {
            $call['args'][$key] = $value;
          }
        }
        else {
          $function = 'main()';
          $call['args'] = $_GET;
        }
        $nicetrace[($counter <= 10 ? $nbsp : '') . --$counter . ': ' . $function] = $call;
        array_shift($backtrace);
      }
      if ($return) {
        return $nicetrace;
      }
      kpr($nicetrace);
    }
  }
}