summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/ExecutionStack/Internal.hsc
blob: 7a30feaafedb54af473632a7bcd94dce43de5011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.ExecutionStack.Internal
-- 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)
--
-- Internals of the `GHC.ExecutionStack` module
--
-- /Since: 4.11.0.0/
-----------------------------------------------------------------------------

#include "HsFFI.h"
#include "HsBaseConfig.h"
#include "rts/Libdw.h"

{-# LANGUAGE MultiWayIf #-}

module GHC.ExecutionStack.Internal (
  -- * Internal
    Location (..)
  , SrcLoc (..)
  , StackTrace
  , stackFrames
  , stackDepth
  , collectStackTrace
  , showStackFrames
  , invalidateDebugCache
  ) where

import Data.Word
import Foreign.C.Types
import Foreign.C.String (peekCString, CString)
import Foreign.Ptr (Ptr, nullPtr, castPtr, plusPtr, FunPtr)
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Storable (Storable(..))
import System.IO.Unsafe (unsafePerformIO, unsafeInterleaveIO)

-- N.B. See includes/rts/Libdw.h for notes on stack representation.

-- | A location in the original program source.
data SrcLoc = SrcLoc { sourceFile   :: String
                     , sourceLine   :: Int
                     , sourceColumn :: Int
                     }

-- | Location information about an addresss from a backtrace.
data Location = Location { objectName   :: String
                         , functionName :: String
                         , srcLoc       :: Maybe SrcLoc
                         }

-- | A chunk of backtrace frames
data Chunk = Chunk { chunkFrames     :: !Word
                   , chunkNext       :: !(Ptr Chunk)
                   , chunkFirstFrame :: !(Ptr Addr)
                   }

-- | The state of the execution stack
newtype StackTrace = StackTrace (ForeignPtr StackTrace)

-- | An address
type Addr = Ptr ()

withSession :: (ForeignPtr Session -> IO a) -> IO a
withSession action = do
    ptr <- libdw_pool_take
    fptr <- newForeignPtr libdw_pool_release ptr
    action fptr

-- | How many stack frames in the given 'StackTrace'
stackDepth :: StackTrace -> Int
stackDepth (StackTrace fptr) =
    unsafePerformIO $ withForeignPtr fptr $ \ptr ->
        fromIntegral . asWord <$> (#peek Backtrace,n_frames) ptr
  where
    asWord = id :: Word -> Word

peekChunk :: Ptr Chunk -> IO Chunk
peekChunk ptr =
    Chunk <$> (#peek BacktraceChunk,n_frames) ptr
          <*> (#peek BacktraceChunk,next) ptr
          <*> pure (castPtr $ (#ptr BacktraceChunk,frames) ptr)

-- | Return a list of the chunks of a backtrace, from the outer-most to
-- inner-most chunk.
chunksList :: StackTrace -> IO [Chunk]
chunksList (StackTrace fptr) = withForeignPtr fptr $ \ptr ->
    go [] =<< (#peek Backtrace,last) ptr
  where
    go accum ptr
      | ptr == nullPtr = return accum
      | otherwise = do
            chunk <- peekChunk ptr
            go (chunk : accum) (chunkNext chunk)

-- | Unpack the given 'Location' in the Haskell representation
peekLocation :: Ptr Location -> IO Location
peekLocation ptr = do
    let peekCStringPtr :: CString -> IO String
        peekCStringPtr p
          | p /= nullPtr = peekCString $ castPtr p
          | otherwise    = return ""
    objFile <- peekCStringPtr =<< (#peek Location,object_file) ptr
    function <- peekCStringPtr =<< (#peek Location,function) ptr
    srcFile <- peekCStringPtr =<< (#peek Location,source_file) ptr
    lineNo <- (#peek Location,lineno) ptr :: IO Word32
    colNo <- (#peek Location,colno) ptr :: IO Word32
    let _srcLoc
          | null srcFile = Nothing
          | otherwise = Just $ SrcLoc { sourceFile = srcFile
                                      , sourceLine = fromIntegral lineNo
                                      , sourceColumn = fromIntegral colNo
                                      }
    return Location { objectName = objFile
                    , functionName = function
                    , srcLoc = _srcLoc
                    }

-- | The size in bytes of a 'locationSize'
locationSize :: Int
locationSize = (#const sizeof(Location))

-- | List the frames of a stack trace.
stackFrames :: StackTrace -> [Location]
stackFrames st@(StackTrace fptr) = unsafePerformIO $ withSession $ \sess -> do
    chunks <- chunksList st
    go sess (reverse chunks)
  where
    go :: ForeignPtr Session -> [Chunk] -> IO [Location]
    go _ [] = return []
    go sess (chunk : chunks) = do
        this <- iterChunk sess chunk
        rest <- unsafeInterleaveIO (go sess chunks)
        return (this ++ rest)

    {-
    Here we lazily lookup the location information associated with each address
    as this can be rather costly. This does mean, however, that if the set of
    loaded modules changes between the time that we capture the stack and the
    time we reach here, we may end up with nonsense (mostly likely merely
    unknown symbols). I think this is a reasonable price to pay, however, as
    module loading/unloading is a rather rare event.

    Morover, we stand to gain a great deal by lazy lookups as the stack frames
    may never even be requested, meaning the only effort wasted is the
    collection of the stack frames themselves.

    The only slightly tricky thing here is to ensure that the ForeignPtr
    stays alive until we reach the end.
    -}
    iterChunk :: ForeignPtr Session -> Chunk -> IO [Location]
    iterChunk sess chunk = iterFrames (chunkFrames chunk) (chunkFirstFrame chunk)
      where
        iterFrames :: Word -> Ptr Addr -> IO [Location]
        iterFrames 0 _ = return []
        iterFrames n frame = do
            pc <- peek frame :: IO Addr
            mframe <- lookupFrame pc
            rest <- unsafeInterleaveIO (iterFrames (n-1) frame')
            return $ maybe rest (:rest) mframe
          where
            frame' = frame `plusPtr` sizeOf (undefined :: Addr)

        lookupFrame :: Addr -> IO (Maybe Location)
        lookupFrame pc = withForeignPtr fptr $ const $ do
            allocaBytes locationSize $ \buf -> do
                ret <- withForeignPtr sess $ \sessPtr -> libdw_lookup_location sessPtr buf pc
                case ret of
                  0 -> Just <$> peekLocation buf
                  _ -> return Nothing

-- | A LibdwSession from the runtime system
data Session

foreign import ccall unsafe "libdwPoolTake"
    libdw_pool_take :: IO (Ptr Session)

foreign import ccall unsafe "&libdwPoolRelease"
    libdw_pool_release :: FunPtr (Ptr Session -> IO ())

foreign import ccall unsafe "libdwPoolClear"
    libdw_pool_clear :: IO ()

foreign import ccall unsafe "libdwLookupLocation"
    libdw_lookup_location :: Ptr Session -> Ptr Location -> Addr -> IO CInt

foreign import ccall unsafe "libdwGetBacktrace"
    libdw_get_backtrace :: Ptr Session -> IO (Ptr StackTrace)

foreign import ccall unsafe "&backtraceFree"
    backtrace_free :: FunPtr (Ptr StackTrace -> IO ())

-- | Get an execution stack.
collectStackTrace :: IO (Maybe StackTrace)
collectStackTrace = withSession $ \sess -> do
    st <- withForeignPtr sess libdw_get_backtrace
    if | st == nullPtr -> return Nothing
       | otherwise     -> Just . StackTrace <$> newForeignPtr backtrace_free st

-- | Free the cached debug data.
invalidateDebugCache :: IO ()
invalidateDebugCache = libdw_pool_clear

-- | Render a stacktrace as a string
showStackFrames :: [Location] -> ShowS
showStackFrames frames =
    showString "Stack trace:\n"
    . foldr (.) id (map showFrame frames)
  where
    showFrame loc =
      showString "    " . showLocation loc . showChar '\n'

-- | Render a 'Location' as a string
showLocation :: Location -> ShowS
showLocation loc =
        showString (functionName loc)
      . maybe id showSrcLoc (srcLoc loc)
      . showString " in "
      . showString (objectName loc)
  where
    showSrcLoc :: SrcLoc -> ShowS
    showSrcLoc sloc =
        showString " ("
      . showString (sourceFile sloc)
      . showString ":"
      . shows (sourceLine sloc)
      . showString "."
      . shows (sourceColumn sloc)
      . showString ")"