Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- trace :: String -> a -> a
- traceId :: String -> String
- traceShow :: Show a => a -> b -> b
- traceShowId :: Show a => a -> a
- traceStack :: String -> a -> a
- traceM :: Applicative f => String -> f ()
- traceShowM :: (Show a, Applicative f) => a -> f ()
- traceEvent :: String -> a -> a
- traceEventIO :: String -> IO ()
- traceMarker :: String -> a -> a
- traceMarkerIO :: String -> IO ()
- data CallStack
- currentCallStack :: IO [String]
- whoCreated :: a -> IO [String]
- type HasCallStack = ?callStack :: CallStack
- callStack :: HasCallStack -> CallStack
- emptyCallStack :: CallStack
- freezeCallStack :: CallStack -> CallStack
- fromCallSiteList :: [([Char], SrcLoc)] -> CallStack
- getCallStack :: CallStack -> [([Char], SrcLoc)]
- popCallStack :: CallStack -> CallStack
- prettyCallStack :: CallStack -> String
- pushCallStack :: ([Char], SrcLoc) -> CallStack -> CallStack
- withFrozenCallStack :: HasCallStack => (HasCallStack -> a) -> a
- data SrcLoc = SrcLoc {
- srcLocPackage :: [Char]
- srcLocModule :: [Char]
- srcLocFile :: [Char]
- srcLocStartLine :: Int
- srcLocStartCol :: Int
- srcLocEndLine :: Int
- srcLocEndCol :: Int
- prettySrcLoc :: SrcLoc -> String
- data Location = Location {
- objectName :: String
- functionName :: String
- srcLoc :: Maybe (String, Int, Int)
- getStackTrace :: IO (Maybe [Location])
- showStackTrace :: IO (Maybe String)
Tracing strings
The trace
function outputs the trace message given as its first argument,
before returning the second argument as its result.
For example, this returns the value of f x
but first outputs the message.
>>>
let x = 123; f = show
>>>
trace ("calling f with x = " ++ show x) (f x)
"calling f with x = 123 123"
The trace
function should only be used for debugging, or for monitoring
execution. The function is not referentially transparent: its type indicates
that it is a pure function but it has the side effect of outputting the
trace message.
Like trace
but returns the message instead of a third value.
>>>
traceId "hello"
"hello hello"
Since: base-4.7.0.0
traceShowId :: Show a => a -> a #
Like traceShow
but returns the shown value instead of a third value.
>>>
traceShowId (1+2+3, "hello" ++ "world")
(6,"helloworld") (6,"helloworld")
Since: base-4.7.0.0
traceStack :: String -> a -> a #
like trace
, but additionally prints a call stack if one is
available.
In the current GHC implementation, the call stack is only
available if the program was compiled with -prof
; otherwise
traceStack
behaves exactly like trace
. Entries in the call
stack correspond to SCC
annotations, so it is a good idea to use
-fprof-auto
or -fprof-auto-calls
to add SCC annotations automatically.
Since: base-4.5.0.0
traceM :: Applicative f => String -> f () #
Like trace
but returning unit in an arbitrary Applicative
context. Allows
for convenient use in do-notation.
Note that the application of traceM
is not an action in the Applicative
context, as traceIO
is in the IO
type. While the fresh bindings in the
following example will force the traceM
expressions to be reduced every time
the do
-block is executed, traceM "not crashed"
would only be reduced once,
and the message would only be printed once. If your monad is in MonadIO
,
liftIO . traceIO
may be a better option.
>>>
:{
do x <- Just 3 traceM ("x: " ++ show x) y <- pure 12 traceM ("y: " ++ show y) pure (x*2 + y) :} x: 3 y: 12 Just 18
Since: base-4.7.0.0
traceShowM :: (Show a, Applicative f) => a -> f () #
traceEvent :: String -> a -> a #
The traceEvent
function behaves like trace
with the difference that
the message is emitted to the eventlog, if eventlog profiling is available
and enabled at runtime.
It is suitable for use in pure code. In an IO context use traceEventIO
instead.
Note that when using GHC's SMP runtime, it is possible (but rare) to get
duplicate events emitted if two CPUs simultaneously evaluate the same thunk
that uses traceEvent
.
Since: base-4.5.0.0
traceEventIO :: String -> IO () #
The traceEventIO
function emits a message to the eventlog, if eventlog
profiling is available and enabled at runtime.
Compared to traceEvent
, traceEventIO
sequences the event with respect to
other IO actions.
Since: base-4.5.0.0
traceMarker :: String -> a -> a #
The traceMarker
function emits a marker to the eventlog, if eventlog
profiling is available and enabled at runtime. The String
is the name of
the marker. The name is just used in the profiling tools to help you keep
clear which marker is which.
This function is suitable for use in pure code. In an IO context use
traceMarkerIO
instead.
Note that when using GHC's SMP runtime, it is possible (but rare) to get
duplicate events emitted if two CPUs simultaneously evaluate the same thunk
that uses traceMarker
.
Since: base-4.7.0.0
traceMarkerIO :: String -> IO () #
The traceMarkerIO
function emits a marker to the eventlog, if eventlog
profiling is available and enabled at runtime.
Compared to traceMarker
, traceMarkerIO
sequences the event with respect to
other IO actions.
Since: base-4.7.0.0
Simulated call stack
CallStack
s are a lightweight method of obtaining a
partial call-stack at any point in the program.
A function can request its call-site with the HasCallStack
constraint.
For example, we can define
putStrLnWithCallStack :: HasCallStack => String -> IO ()
as a variant of putStrLn
that will get its call-site and print it,
along with the string given as argument. We can access the
call-stack inside putStrLnWithCallStack
with callStack
.
putStrLnWithCallStack :: HasCallStack => String -> IO () putStrLnWithCallStack msg = do putStrLn msg putStrLn (prettyCallStack callStack)
Thus, if we call putStrLnWithCallStack
we will get a formatted call-stack
alongside our string.
>>>
putStrLnWithCallStack "hello"
hello CallStack (from HasCallStack): putStrLnWithCallStack, called at <interactive>:2:1 in interactive:Ghci1
GHC solves HasCallStack
constraints in three steps:
- If there is a
CallStack
in scope -- i.e. the enclosing function has aHasCallStack
constraint -- GHC will append the new call-site to the existingCallStack
. - If there is no
CallStack
in scope -- e.g. in the GHCi session above -- and the enclosing definition does not have an explicit type signature, GHC will infer aHasCallStack
constraint for the enclosing definition (subject to the monomorphism restriction). - If there is no
CallStack
in scope and the enclosing definition has an explicit type signature, GHC will solve theHasCallStack
constraint for the singletonCallStack
containing just the current call-site.
CallStack
s do not interact with the RTS and do not require compilation
with -prof
. On the other hand, as they are built up explicitly via the
HasCallStack
constraints, they will generally not contain as much
information as the simulated call-stacks maintained by the RTS.
A CallStack
is a [(String, SrcLoc)]
. 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.
NOTE: The intrepid user may notice that HasCallStack
is just an
alias for an implicit parameter ?callStack :: CallStack
. This is an
implementation detail and should not be considered part of the
CallStack
API, we may decide to change the implementation in the
future.
Since: base-4.8.1.0
currentCallStack :: IO [String] #
Returns a [String]
representing the current call stack. This
can be useful for debugging.
The implementation uses the call-stack simulation maintained 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.
Since: base-4.5.0.0
whoCreated :: a -> IO [String] #
Get the stack trace attached to an object.
Since: base-4.5.0.0
type HasCallStack = ?callStack :: CallStack #
Request a CallStack.
NOTE: The implicit parameter ?callStack :: CallStack
is an
implementation detail and should not be considered part of the
CallStack
API, we may decide to change the implementation in the
future.
Since: base-4.9.0.0
callStack :: HasCallStack -> CallStack #
The empty CallStack
.
Since: base-4.9.0.0
freezeCallStack :: CallStack -> CallStack #
Freeze a call-stack, preventing any further call-sites from being appended.
pushCallStack callSite (freezeCallStack callStack) = freezeCallStack callStack
Since: base-4.9.0.0
fromCallSiteList :: [([Char], SrcLoc)] -> CallStack #
Convert a list of call-sites to a CallStack
.
Since: base-4.9.0.0
getCallStack :: CallStack -> [([Char], SrcLoc)] #
Extract a list of call-sites from the CallStack
.
The list is ordered by most recent call.
Since: base-4.8.1.0
popCallStack :: CallStack -> CallStack #
Pop the most recent call-site off the CallStack
.
This function, like pushCallStack
, has no effect on a frozen CallStack
.
Since: base-4.9.0.0
prettyCallStack :: CallStack -> String #
Pretty print a CallStack
.
Since: base-4.9.0.0
pushCallStack :: ([Char], SrcLoc) -> CallStack -> CallStack #
Push a call-site onto the stack.
This function has no effect on a frozen CallStack
.
Since: base-4.9.0.0
withFrozenCallStack :: HasCallStack => (HasCallStack -> a) -> a #
Perform some computation without adding new entries to the CallStack
.
Since: base-4.9.0.0
A single location in the source code.
Since: base-4.8.1.0
SrcLoc | |
|
prettySrcLoc :: SrcLoc -> String #
Pretty print a SrcLoc
.
Since: base-4.9.0.0
Execution stack (requires libdw
)
Location | |
|
showStackTrace :: IO (Maybe String) #
Get a string representation of the current execution stack state.