ExecutionStack.hs 1.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.ExecutionStack
-- Copyright   :  (c) The University of Glasgow 2013-2015
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC Extensions)
--
-- This is a module for efficient stack traces. This stack trace implementation
-- is considered low overhead. Basic usage looks like this:
--
-- @
-- import GHC.ExecutionStack
--
-- myFunction :: IO ()
-- myFunction = do
--      putStrLn =<< showStackTrace
-- @
--
-- Your GHC must have been built with @libdw@ support for this to work.
--
-- @
25
-- user@host:~$ ghc --info | grep libdw
26
27
28
--  ,("RTS expects libdw","YES")
-- @
--
29
-- @since 4.9.0.0
30
31
32
33
34
35
36
37
38
-----------------------------------------------------------------------------

module GHC.ExecutionStack (
    Location (..)
  , SrcLoc (..)
  , getStackTrace
  , showStackTrace
  ) where

39
import Control.Monad (join)
40
41
42
43
44
45
import GHC.ExecutionStack.Internal

-- | Get a trace of the current execution stack state.
--
-- Returns @Nothing@ if stack trace support isn't available on host machine.
getStackTrace :: IO (Maybe [Location])
46
getStackTrace = (join . fmap stackFrames) `fmap` collectStackTrace
47
48
49

-- | Get a string representation of the current execution stack state.
showStackTrace :: IO (Maybe String)
50
showStackTrace = fmap (\st -> showStackFrames st "") `fmap` getStackTrace