function ddebug_backtrace

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

Prints the function call stack.

Parameters

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

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

int $options: Options (treated as a bit mask) to pass on to PHP's debug_backtrace().

Return value

array|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.
1 string reference to 'ddebug_backtrace'
DevelDumperBase::getInternalFunctions in src/DevelDumperBase.php
Returns a list of internal functions.

File

./devel.module, line 570

Code

function ddebug_backtrace(bool $return = FALSE, int $pop = 0, int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT) : ?array {
  if (Drupal::currentUser()->hasPermission('access devel information') === FALSE) {
    return NULL;
  }
  $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') === ERROR_REPORTING_HIDE) {
    return NULL;
  }
  $nicetrace = [];
  while (!empty($backtrace)) {
    $call = [];
    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] += [
        'args' => [],
      ];
      foreach ($backtrace[1]['args'] as $key => $value) {
        $call['args'][$key] = $value;
      }
    }
    else {
      $function = 'main()';
      $requestStack = Drupal::service('request_stack');
      $call['args'] = $requestStack->getCurrentRequest()->query
        ->all();
    }
    $nicetrace[($counter <= 10 ? $nbsp : '') . --$counter . ': ' . $function] = $call;
    array_shift($backtrace);
  }
  if ($return) {
    return $nicetrace;
  }
  Drupal::service('devel.dumper')->dumpOrExport(input: $nicetrace, export: FALSE);
  return NULL;
}