Skip to content

Commit 4665589

Browse files
fendorbgamari
authored andcommitted
Store StackTrace and StackSnapshot in Backtraces
Instead of decoding the stack traces when collecting the `Backtraces`, defer this decoding until actually showing the `Backtraces`. This allows users to customise how `Backtraces` are displayed by using a custom implementation of `displayExceptionWithInfo`, overwriting the default implementation for `Backtraces` (`displayBacktraces`). (cherry picked from commit c91e265)
1 parent 362cfff commit 4665589

File tree

1 file changed

+9
-8
lines changed
  • libraries/ghc-internal/src/GHC/Internal/Exception

1 file changed

+9
-8
lines changed

libraries/ghc-internal/src/GHC/Internal/Exception/Backtrace.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import GHC.Internal.IORef
1111
import GHC.Internal.IO.Unsafe (unsafePerformIO)
1212
import GHC.Internal.Exception.Context
1313
import GHC.Internal.Ptr
14+
import GHC.Internal.Data.Maybe (fromMaybe)
1415
import GHC.Internal.Stack.Types as GHC.Stack (CallStack)
1516
import qualified GHC.Internal.Stack as HCS
16-
import qualified GHC.Internal.ExecutionStack as ExecStack
1717
import qualified GHC.Internal.ExecutionStack.Internal as ExecStack
1818
import qualified GHC.Internal.Stack.CloneStack as CloneStack
1919
import qualified GHC.Internal.Stack.CCS as CCS
@@ -91,8 +91,8 @@ data Backtraces =
9191
Backtraces {
9292
btrCostCentre :: Maybe (Ptr CCS.CostCentreStack),
9393
btrHasCallStack :: Maybe HCS.CallStack,
94-
btrExecutionStack :: Maybe [ExecStack.Location],
95-
btrIpe :: Maybe [CloneStack.StackEntry]
94+
btrExecutionStack :: Maybe ExecStack.StackTrace,
95+
btrIpe :: Maybe CloneStack.StackSnapshot
9696
}
9797

9898
-- | Render a set of backtraces to a human-readable string.
@@ -109,8 +109,10 @@ displayBacktraces bts = concat
109109

110110
-- The unsafePerformIO here is safe as we don't currently unload cost-centres.
111111
displayCc = unlines . map (indent 2) . unsafePerformIO . CCS.ccsToStrings
112-
displayExec = unlines . map (indent 2 . flip ExecStack.showLocation "")
113-
displayIpe = unlines . map (indent 2 . CloneStack.prettyStackEntry)
112+
displayExec = unlines . map (indent 2 . flip ExecStack.showLocation "") . fromMaybe [] . ExecStack.stackFrames
113+
-- The unsafePerformIO here is safe as 'StackSnapshot' makes sure neither the stack frames nor
114+
-- references closures can be garbage collected.
115+
displayIpe = unlines . map (indent 2 . CloneStack.prettyStackEntry) . unsafePerformIO . CloneStack.decode
114116
displayHsc = unlines . map (indent 2 . prettyCallSite) . HCS.getCallStack
115117
where prettyCallSite (f, loc) = f ++ ", called at " ++ HCS.prettySrcLoc loc
116118

@@ -140,12 +142,11 @@ collectBacktraces' enabled = HCS.withFrozenCallStack $ do
140142
Just `fmap` CCS.getCurrentCCS ()
141143

142144
exec <- collect ExecutionBacktrace $ do
143-
ExecStack.getStackTrace
145+
ExecStack.collectStackTrace
144146

145147
ipe <- collect IPEBacktrace $ do
146148
stack <- CloneStack.cloneMyStack
147-
stackEntries <- CloneStack.decode stack
148-
return (Just stackEntries)
149+
return (Just stack)
149150

150151
hcs <- collect HasCallStackBacktrace $ do
151152
return (Just ?callStack)

0 commit comments

Comments
 (0)