[Haskell-cafe] How to debug GHC

dagit at eecs.oregonstate.edu dagit at eecs.oregonstate.edu
Fri Sep 2 12:56:32 EDT 2005


Quoting Nils Anders Danielsson <nad at cs.chalmers.se>:

> On Thu, 01 Sep 2005, Frederik Eaton <frederik at a5.repetae.net> wrote:
>
> > But getting a stack backtrace when there is an error should be a
> > pretty basic feature. It's very hard to debug a large program when you
> > can randomly get messages like "*** Exception: Prelude.head: empty
> > list" and have no idea where they came from.
>
> >From the GHC FAQ:
>
> My program is failing with head [], or an array bounds error, or some
> other random error, and I have no idea how to find the bug. Can you
> help?
>
>     Compile your program with -prof -auto-all (make sure you have the
>     profiling libraries installed), and run it with +RTS -xc -RTS to
>     get a ¡Èstack trace¡É at the point at which the exception was
>     raised. See Section 4.14.4, ¡ÈRTS options for hackers, debuggers,
>     and over-interested souls¡É for more details.
>
> I tried this out under GHC 6.4/Linux and got a segmentation fault
> instead of a stack trace. Under GHC 6.2.2 it seemed to work, though.

I was trying to debug a smallish program where I was getting this exact error
and the trick with profiling did "work" on my system, but I remember it being
almost useless for me.  What what did end up working for me was:
myhead :: [a] -> String -> a
myhead [] s = error s
myhead xs _ = head xs

And then I did a M-x occur head <RET>, replaced all the calls to head with
|myhead xs "myhead callsite n"| and incremented n appropriately.

This technique is pedestrian, but it generalizes quite well and it will work in
any situation given enough time to do all the search/replace.  There are
certainly better methods and it's not a replacement for a real debugger, but
this one is easy for a beginner to come up with and it does work.

Maybe you'll find it useful.

Jason


More information about the Haskell-Cafe mailing list