base-4.7.0.0: Basic libraries

Copyright(c) The University of Glasgow 2001
License(c) The University of Glasgow 2001
Maintainerlibraries@haskell.org
Stabilityprovisional
Portabilityportable
Safe HaskellUnsafe

Debug.Trace

Contents

Description

Functions for tracing and monitoring execution.

These can be useful for investigating bugs or performance problems. They should not be used in production code.

Synopsis

Tracing

The trace, traceShow and traceIO functions print messages to an output stream. They are intended for "printf debugging", that is: tracing the flow of execution and printing interesting values.

trace :: String -> a -> aSource

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.

 trace ("calling f with x = " ++ show x) (f x)

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.

traceId :: String -> StringSource

Like trace but returns the message instead of a third value.

traceShow :: Show a => a -> b -> bSource

Like trace, but uses show on the argument to convert it to a String.

This makes it convenient for printing the values of interesting variables or expressions inside a function. For example here we print the value of the variables x and z:

 f x y =
     traceShow (x, z) $ result
   where
     z = ...
     ...

traceShowId :: Show a => a -> aSource

Like traceShow but returns the shown value instead of a third value.

traceStack :: String -> a -> aSource

like trace, but additionally prints a call stack if one is available.

In the current GHC implementation, the call stack is only availble 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.

traceIO :: String -> IO ()Source

The traceIO function outputs the trace message from the IO monad. This sequences the output with respect to other IO actions.

traceM :: Monad m => String -> m ()Source

Like trace but returning unit in an arbitrary monad. Allows for convenient use in do-notation. Note that the application of trace is not an action in the monad, as traceIO is in the IO monad.

 ... = do
   x <- ...
   traceM $ "x: " ++ show x
   y <- ...
   traceM $ "y: " ++ show y

traceShowM :: (Show a, Monad m) => a -> m ()Source

Like traceM, but uses show on the argument to convert it to a String.

 ... = do
   x <- ...
   traceMShow $ x
   y <- ...
   traceMShow $ x + y

putTraceMsg :: String -> IO ()Source

Deprecated: Use Debug.Trace.traceIO

Deprecated. Use traceIO.

Eventlog tracing

Eventlog tracing is a performance profiling system. These functions emit extra events into the eventlog. In combination with eventlog profiling tools these functions can be used for monitoring execution and investigating performance problems.

Currently only GHC provides eventlog profiling, see the GHC user guide for details on how to use it. These function exists for other Haskell implementations but no events are emitted. Note that the string message is always evaluated, whether or not profiling is available or enabled.

traceEvent :: String -> a -> aSource

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.

traceEventIO :: String -> IO ()Source

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.

Execution phase markers

When looking at a profile for the execution of a program we often want to be able to mark certain points or phases in the execution and see that visually in the profile.

traceMarker :: String -> a -> aSource

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.

traceMarkerIO :: String -> IO ()Source

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.