Get system backtrace in formated view
@param   bool $trace     Custom php backtrace
@param   bool $addObject Show objects in result
@return  JBDump
23
public static function trace($trace = null, $addObject = false)
    {
        if (!self::isDebug()) {
            return false;
        }

        $_this = self::i();

        $trace = $trace ? $trace : debug_backtrace($addObject);
        unset($trace[0]);

        $result = $_this->convertTrace($trace, $addObject);

        return $_this->dump($result, '! backtrace !');
    }
683064
public static function getPluginNameFromBacktrace($backtrace)
    {
        foreach ($backtrace as $tracepoint) {
            // try and discern the plugin name
            if (isset($tracepoint['class'])) {
                $className = self::getPluginNameFromNamespace($tracepoint['class']);
                if ($className) {
                    return $className;
                }
            }
        }
        return false;
    }
271094
public static function filter_backtrace($bt, $ignoredFunctions = null)
    {
        $defaultIgnoredFunctions = array(
            'SilverStripe\\Logging\\Log::log',
            'SilverStripe\\Dev\\Backtrace::backtrace',
            'SilverStripe\\Dev\\Backtrace::filtered_backtrace',
            'Zend_Log_Writer_Abstract->write',
            'Zend_Log->log',
            'Zend_Log->__call',
            'Zend_Log->err',
            'SilverStripe\\Dev\\DebugView->writeTrace',
            'SilverStripe\\Dev\\CliDebugView->writeTrace',
            'SilverStripe\\Dev\\Debug::emailError',
            'SilverStripe\\Dev\\Debug::warningHandler',
            'SilverStripe\\Dev\\Debug::noticeHandler',
            'SilverStripe\\Dev\\Debug::fatalHandler',
            'errorHandler',
            'SilverStripe\\Dev\\Debug::showError',
            'SilverStripe\\Dev\\Debug::backtrace',
            'exceptionHandler'
        );

        if ($ignoredFunctions) {
            foreach ($ignoredFunctions as $ignoredFunction) {
                $defaultIgnoredFunctions[] = $ignoredFunction;
            }
        }

        while ($bt && in_array(self::full_func_name($bt[0]), $defaultIgnoredFunctions)) {
            array_shift($bt);
        }

        $ignoredArgs = static::config()->get('ignore_function_args');

        // Filter out arguments
        foreach ($bt as $i => $frame) {
            $match = false;
            if (!empty($bt[$i]['class'])) {
                foreach ($ignoredArgs as $fnSpec) {
                    if (is_array($fnSpec) &&
                        ('*' == $fnSpec[0] || $bt[$i]['class'] == $fnSpec[0]) &&
                        $bt[$i]['function'] == $fnSpec[1]
                    ) {
                        $match = true;
                    }
                }
            } else {
                if (in_array($bt[$i]['function'], $ignoredArgs)) {
                    $match = true;
                }
            }
            if ($match) {
                foreach ($bt[$i]['args'] as $j => $arg) {
                    $bt[$i]['args'][$j] = '<filtered>';
                }
            }
        }

        return $bt;
    }
476044
protected function renderBacktrace($backtrace)
    {
        $output = '';
        if ($this->backtraceRenderer !== null) {
            $output = $this->backtraceRenderer->__invoke($backtrace);
        }

        return $output;
    }
1360065
public static function trace()
    {
        if (!self::$enabled_mode) {
            return 0;
        }

        Utils::normalizeAliases(self::$aliases);

        $args = \func_get_args();

        $call_info = self::getCallInfo(self::$aliases, \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), \count($args));

        $statics = self::getStatics();

        if (\in_array('~', $call_info['modifiers'], true)) {
            $statics['enabled_mode'] = self::MODE_TEXT;
        }

        $kintstance = self::createFromStatics($statics);
        if (!$kintstance) {
            // Should never happen
            return 0; // @codeCoverageIgnore
        }

        if (\in_array('-', $call_info['modifiers'], true)) {
            while (\ob_get_level()) {
                \ob_end_clean();
            }
        }

        $kintstance->setStatesFromStatics($statics);
        $kintstance->setStatesFromCallInfo($call_info);

        $trimmed_trace = array();
        $trace = \debug_backtrace(true);

        foreach ($trace as $frame) {
            if (Utils::traceFrameIsListed($frame, self::$aliases)) {
                $trimmed_trace = array();
            }

            $trimmed_trace[] = $frame;
        }

        $output = $kintstance->dumpAll(
            array($trimmed_trace),
            array(BasicObject::blank('Kint\\Kint::trace()', 'debug_backtrace(true)'))
        );

        if (self::$return || \in_array('@', $call_info['modifiers'], true)) {
            return $output;
        }

        echo $output;

        if (\in_array('-', $call_info['modifiers'], true)) {
            \flush(); // @codeCoverageIgnore
        }

        return 0;
    }
1204356
private function findTraced($className = null)
    {
        $traces     = debug_backtrace();
        $reflection = new ReflectionClass($className);
        $filename   = $reflection->getFileName();
        $traced     = null;


        foreach ($traces as $trace) {
            if (isset($trace['file']) and $filename == $trace['file']) {
                $traced = $trace;
                break;
            }
        }
        return $traced;
    }
423001
protected function getFixtureCaller($traceIndex = 6)
    {
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $traceIndex + 1);
        $caller = $trace[$traceIndex];

        if (isset($caller['class'])) {
            $result = @$caller['class'] . '->' . @$caller['function'] . '()';
        } else {
            $result = @$caller['function'] . '()';
        }

        return $result;
    }
37477
public static function getClassesToRead(\ReflectionObject $reflectionObject)
    {
        $cacheId = md5("classesToRead:" . $reflectionObject->getName());
        $objectClasses = self::getFromCache($cacheId);
        if ($objectClasses !== null) {
            return $objectClasses;
        }

        $objectClasses = array($reflectionObject);
        $objectTraits = $reflectionObject->getTraits();
        if (!empty($objectTraits)) {
            foreach ($objectTraits as $trait) {
                $objectClasses[] = $trait;
            }
        }

        $parentClass = $reflectionObject->getParentClass();
        while ($parentClass) {
            $objectClasses[] = $parentClass;

            $parentTraits = $parentClass->getTraits();
            if (!empty($parentTraits)) {
                foreach ($parentTraits as $trait) {
                    $objectClasses[] = $trait;
                }
            }

            $parentClass = $parentClass->getParentClass();
        }

        self::saveToCache($cacheId, $objectClasses);

        return $objectClasses;
    }
127752
public static function getCalledFrom($index = 0)
    {
        $backtrace = debug_backtrace();

        if (!isset($backtrace[$index])) {
            return 'Unknown trace with index: ' . $index;
        }

        return self::getCalledFromTrace($backtrace[$index]);
    }
945897
public static function backtrace($trace = null)
	{
		$trace or $trace = debug_backtrace();

		if (\Fuel::$is_cli) {
			// Special case for CLI since the var_dump of a backtrace is of little use.
			$str = '';
			foreach ($trace as $i => $frame)
			{
				$line = "#$i\t";

				if ( ! isset($frame['file']))
				{
					$line .= "[internal function]";
				}
				else
				{
					$line .= $frame['file'] . ":" . $frame['line'];
				}

				$line .= "\t";

				if (isset($frame['function']))
				{
					if (isset($frame['class']))
					{
						$line .= $frame['class'] . '::';
					}

					$line .= $frame['function'] . "()";
				}

				$str .= $line . "\n";

			}

			return $str;
		}
		else
		{
			return static::dump($trace);
		}
	}
709681
public function setPHPTrace(string $php_trace = "F") : CNabuDataObject
    {
        if ($php_trace === null) {
            throw new ENabuCoreException(
                    ENabuCoreException::ERROR_NULL_VALUE_NOT_ALLOWED_IN,
                    array("\$php_trace")
            );
        }
        $this->setValue('nb_site_target_php_trace', $php_trace);
        
        return $this;
    }
37362
static public function traceFiles()
    {
        $backtrace = debug_backtrace();
        array_shift($backtrace);
        $message = 'load files:' . PHP_EOL;
        if ($backtrace) foreach ($backtrace as $v) {
            $message .= sprintf('[%s] %s: %s', filesize($v['file']), $v['file'], $v['function']) . PHP_EOL;
        }
        Ioc::logger('trace')->debug(sprintf('[%s]%s', 'file', $message));
    }
650116
public static function caller(int $back = 0)
    {
        $bt = debug_backtrace();
        $trace = $bt[$back] ?? null;

        $file = $trace['file'] ?? '';
        $line = $trace['line'] ?? -1;

        return array( $file, intval($line) );
    }
155617
private function getStackTrace(MockObject $mock): array
    {
        $mockName = (new \ReflectionObject($mock))->getShortName();

        $trace = [];
        $enableTrace = false;
        foreach (debug_backtrace() as $i => $row) {
            if (isset($row['class']) && $mockName === $row['class']) {
                $enableTrace = true;
            }

            if ($enableTrace) {
                $traceRow = '';

                if (isset($row['class'])) {
                    $traceRow .= $row['class'];
                }

                if (isset($row['type'])) {
                    $traceRow .= $row['type'];
                }

                if (isset($row['function'])) {
                    $traceRow .= $row['function'];
                }

                if (isset($row['file'])) {
                    $traceRow .= sprintf(' (%s:%d)', $row['file'], $row['line']);
                }

                $trace[] = $traceRow;
            }
        }

        krsort($trace);

        return array_values($trace);
    }
881047
private function traceComponentMakeSane( $trace )
    {
        // debugging wondering why some traces's don't have a args object set
        if( isset( $trace['args'] ) ) {
            $trace['args'] = array_map(
                function($arg){
                    switch(true) {
                        case is_object($arg):
                            return get_class($arg);
                        case is_bool($arg):
                            return $arg ? 'true' : 'false';
                        case is_scalar($arg):
                            return $arg;
                        default:
                            return gettype($arg);
                    }
                },
                $trace['args']
            );
        } else {
            $trace['args'] = array();
        }
        if( isset( $trace['file'] ) and false !== $pos = strpos( $trace['file'], '/Symfony/' ) ) {
            $trace['file'] = ".".substr( $trace['file'], $pos );
        }

        // set the file and line properties if they don't exist
        if( !array_key_exists( 'file', $trace ) ) {
            $trace['file'] =  null;
        }
        if( !array_key_exists( 'line', $trace ) ) {
            $trace['line'] =  null;
        }

        // attempt to add file and line back in
        // not sure why this would be required - it seems very hacky but seemingly corrects some inexplicatbly missing information
        if( !$trace['file'] and !$trace['line'] and isset( $trace['class'] ) and isset( $trace['function'] ) ) {
            try {
                $reflMethod = new \ReflectionMethod( $trace['class'], $trace['function'] );
                $trace['file'] = $reflMethod->getFileName();
                $trace['line'] = $reflMethod->getStartLine();
            } catch ( \Exception $e ) {
            }
        }

        // unset the object property - not sure why this has appeared. It wasn't present in the output of the original sane_debug_backtrace()
        unset( $trace['object'] );
        return $trace;
    }
470399
protected function backtrace($string, $level = 1)
    {
        if (isset($this->trace[$string])) {
            return $this;
        }

        $info = [
            'position' => null,
            'called' => null,
            'args' => [],
        ];

        if (function_exists('debug_backtrace')) {
            $defaultTrace = [
                'file' => null,
                'line' => null,
                'function' => null,
                'class' => null,
                'object' => null,
                'type' => null,
                'args' => [],
            ];

            $trace = debug_backtrace();

            // Find where called this object
            if (isset($trace[$level])) {
                $traceData = array_merge($defaultTrace, $trace[$level]);

                $ref = null;

                if (method_exists($traceData['class'], $traceData['function'])) {
                    $ref = new \ReflectionMethod($traceData['class'], $traceData['function']);
                } elseif (function_exists($traceData['function'])) {
                    $ref = new \ReflectionFunction($traceData['function']);
                }

                $info['position'] = [
                    'file' => $ref ? $ref->getFileName() : $traceData['file'],
                    'class' => $traceData['class'],
                    'function' => $traceData['function'],
                    'line' => $ref ? $ref->getStartLine() : $traceData['line'],
                ];
            }

            if (isset($trace[$level - 1])) {
                $traceData = array_merge($defaultTrace, $trace[$level - 1]);

                $info['called'] = [
                    'file' => $traceData['file'],
                    'class' => $traceData['class'],
                    'function' => $traceData['function'],
                    'line' => $traceData['line'],
                    'args' => $traceData['args'],
                ];

                $info['args'] = $traceData['args'];
            }
        }

        return $info;
    }
361429
protected static function get_calling_module_from_trace($backtrace)
    {
        if (!isset($backtrace[1]['file'])) {
            return null;
        }

        $callingfile = realpath($backtrace[1]['file']);

        return ModuleLoader::inst()->getManifest()->getModuleByPath($callingfile);
    }