base- Basic libraries

Copyright(c) The University of Glasgow 2011
Licensesee libraries/base/LICENSE
Portabilitynon-portable (GHC Extensions)
Safe HaskellTrustworthy




Access to GHC's call-stack simulation



Call stacks

Simulated by the RTS

currentCallStack :: IO [String] Source

returns a '[String]' representing the current call stack. This can be useful for debugging.

The implementation uses the call-stack simulation maintined by the profiler, so it only works if the program was compiled with -prof and contains suitable SCC annotations (e.g. by using -fprof-auto). Otherwise, the list returned is likely to be empty or uninformative.


whoCreated :: a -> IO [String] Source

Get the stack trace attached to an object.


errorWithStackTrace :: String -> a Source

Like the function error, but appends a stack trace to the error message if one is available.


Explicitly created via implicit-parameters

data CallStack Source

CallStacks are an alternate method of obtaining the call stack at a given point in the program.

When an implicit-parameter of type CallStack occurs in a program, GHC will solve it with the current location. If another CallStack implicit-parameter is in-scope (e.g. as a function argument), the new location will be appended to the one in-scope, creating an explicit call-stack. For example,

myerror :: (?loc :: CallStack) => String -> a
myerror msg = error (msg ++ "n" ++ showCallStack ?loc)

ghci> myerror "die" *** Exception: die ?loc, called at MyError.hs:7:51 in main:MyError myerror, called at interactive:2:1 in interactive:Ghci1

CallStacks do not interact with the RTS and do not require compilation with -prof. On the other hand, as they are built up explicitly using implicit-parameters, they will generally not contain as much information as the simulated call-stacks maintained by the RTS.

The CallStack type is abstract, but it can be converted into a [(String, SrcLoc)] via getCallStack. The String is the name of function that was called, the SrcLoc is the call-site. The list is ordered with the most recently called function at the head.