Debugging the RTS

Jost Berthold berthold at
Thu May 14 14:21:23 UTC 2015

Hi Eric,

definitely not bit-rotted - the #if DEBUG code is what you get when you 
compile with -debug.

Short roadmap:

In general, GHC is built in many "ways", the -debug way is one.
Some ways (like -threaded, -eventlog, -debug) are RTS-only, others (like 
-prof) lead to different library code.

Ways are defined in compiler/main/DynFlags.hs as a Haskell data structure.
In _dynamic_flags_, the actual flag strings for the ghc invocation are 
defined (like -prof, -threaded), they will activate the respective _Way_.
_wayTag_, in turn, defines short names used as a suffix for *.o and *.a 
files, for instance *.p_o for profiling, *.l_o for eventlog.
A number of other functions in there customise behaviour depending on 
the ways. Note _wayOptc_ which sets some options for the C compiler, 
like -DTRACING for the -eventlog way. However, it does not define DEBUG 
for the debug way!

In mk/ you will find all the short names, and some more options 
are defined (WAY_*_HC_OPTS). These definitions are for the driver 
script, and pass on the right (long-name) options to the Haskell 
compiler to activate what is inside DynFlags (like -prof for WAY_p_HC_OPTS).
Here we find
WAY_debug_HC_OPTS= -static -optc-DDEBUG -ticky -DTICKY_TICKY
which is what you were looking for

To build a GHC with debug-way RTS, you need to add GhcRtsWays += debug 
to your (also see mk/{.in}, which gathers the default 
ways to build depending on platform and build configuration).
I guess this might produce quite a number of new warnings and errors if 
you do it on a new platform...

Once you have built this, you can compile with -debug and pass RTS 
options to get debug traces and activate sanity checks, like so:
ghc -debug -myprogram.hs -o myprogramDebug
./myprogramDebug +RTS -Ds -DS
(-DS = sanity checks on, -Ds = scheduler tracing)
The usage message (see /rts/RtsFlags.c) tells you more options you can use.

Happy debugging!
/ Jost

On 05/14/2015 10:00 PM, ghc-devs-request at wrote:
> Date: Thu, 14 May 2015 18:36:04 +1000
> From: Erik de Castro Lopo <mle+hs at>
> To: ghc-devs at
> Subject: Debugging the RTS
> Message-ID: <20150514183604.2476baaaacd6a85ef1430b37 at>
> Content-Type: text/plain; charset=US-ASCII
> Hi all,
> I'm trying to debug a AArch64/Linux specific RTS issue and digging
> around in the rts C code, I see a large amount of code wrapped in
> "#if DEBUG" type statements, but no way to enable in either
> mk/ or it seems in any of the other build related settings
> files in the mk/ directory.
> The only way I have found to enable this debug code that actually
> works is to enable this DEBUG code is to edit mk/ as follows:
> However, once I do that I get a bunch of C synatx errors.
> I am doing this wrong or has the DEBUG code bit rotted?
> Cheers,
> Erik

More information about the ghc-devs mailing list