From c7ab11811ab4a68d8137793e455984bfc2207073 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Tue, 21 Jul 2015 14:22:17 -0400 Subject: [PATCH 1/3] Implemented backtrace for fatal errors using xdebug --- src/Whoops/Exception/Inspector.php | 46 +++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Whoops/Exception/Inspector.php b/src/Whoops/Exception/Inspector.php index 06aeec3..e30789e 100644 --- a/src/Whoops/Exception/Inspector.php +++ b/src/Whoops/Exception/Inspector.php @@ -92,7 +92,7 @@ class Inspector public function getFrames() { if ($this->frames === null) { - $frames = $this->exception->getTrace(); + $frames = $this->getTrace($this->exception); // If we're handling an ErrorException thrown by Whoops, // get rid of the last frame, which matches the handleError method, @@ -125,6 +125,50 @@ class Inspector return $this->frames; } + /** + * Gets the backgrace from an exception. + * + * If xdebug is installed + * + * @param Exception $e + * @return array + */ + protected function getTrace(\Exception $e) + { + $traces = $e->getTrace(); + + switch ($e->getSeverity()) { + case E_ERROR: + case E_RECOVERABLE_ERROR: + case E_PARSE: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + $fatal = true; + break; + + default: + $fatal = false; + break; + } + + // Get trace from xdebug if enabled, failure exceptions only trace to the shutdown handler by default + if(!$e instanceof \ErrorException || !$fatal) { + return $traces; + } + + if (!extension_loaded('xdebug') || !xdebug_is_enabled()) { + return array(); + } + + // Use xdebug to get the full stack trace and remove the shutdown handler stack trace + $stack = array_reverse(xdebug_get_function_stack()); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $traces = array_diff_key($stack, $trace); + + return $traces; + } + /** * Given an exception, generates an array in the format * generated by Exception::getTrace() From 008ebde5756f8143031c3b4c3c20ac969c644883 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Tue, 21 Jul 2015 14:24:22 -0400 Subject: [PATCH 2/3] Added space --- src/Whoops/Exception/Inspector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Whoops/Exception/Inspector.php b/src/Whoops/Exception/Inspector.php index e30789e..c81dabb 100644 --- a/src/Whoops/Exception/Inspector.php +++ b/src/Whoops/Exception/Inspector.php @@ -153,7 +153,7 @@ class Inspector } // Get trace from xdebug if enabled, failure exceptions only trace to the shutdown handler by default - if(!$e instanceof \ErrorException || !$fatal) { + if (!$e instanceof \ErrorException || !$fatal) { return $traces; } From 3935598cde46b2532807a7797a3c6815e3aa5792 Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Tue, 21 Jul 2015 15:19:52 -0400 Subject: [PATCH 3/3] Only call getSeverity on ErrorException classes --- src/Whoops/Exception/Inspector.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Whoops/Exception/Inspector.php b/src/Whoops/Exception/Inspector.php index c81dabb..8ea70db 100644 --- a/src/Whoops/Exception/Inspector.php +++ b/src/Whoops/Exception/Inspector.php @@ -137,6 +137,11 @@ class Inspector { $traces = $e->getTrace(); + // Get trace from xdebug if enabled, failure exceptions only trace to the shutdown handler by default + if (!$e instanceof \ErrorException) { + return $traces; + } + switch ($e->getSeverity()) { case E_ERROR: case E_RECOVERABLE_ERROR: @@ -152,8 +157,7 @@ class Inspector break; } - // Get trace from xdebug if enabled, failure exceptions only trace to the shutdown handler by default - if (!$e instanceof \ErrorException || !$fatal) { + if (!$fatal) { return $traces; }