Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging haskell: Display a function's name at each call

Tags:

haskell

Is there a tool that automatically "marks" some functions so that I can get an approximation of a "call stack".

Actually, I would like to have a behavior similar to the one you get by writing fct = Debug.trace "fct" $ ... without having to add it before each function.

I know that profiling does something similar with -fprof-auto, but I need it to be displayed while the application is running.

Some time, I have infinite loops and having this display could show me immediately which function(s) is faulty. Using hlist and breakpoints isn't really helpfull since you already have to know the name of one of the functions in the cycle.

like image 525
Jeremy Cochoy Avatar asked Sep 03 '13 15:09

Jeremy Cochoy


1 Answers

Here is something incredibly ugly ;-), and it only gives you the line number instead of the function name, but I was surprised to find out that it works, so I thought I'd share it. And it's still better than nothing. You can use the C preprocessor just like in good old C days:

{-# LANGUAGE CPP #-}

#define traceLoc trace (__FILE__ ++":"++ show __LINE__)

import Debug.Trace

f 0 = traceLoc $ 1
f n = traceLoc $ g (n-1)
g 0 = traceLoc $ 2
g n = traceLoc $ 2 * f (n-1)    

Now,

*Main> f 3
Test.hs:16
Test.hs:18
Test.hs:16
Test.hs:17
4
like image 70
firefrorefiddle Avatar answered Oct 06 '22 00:10

firefrorefiddle