Backtrace when a certain location in the code is executed

Hey, I am trying to understand haskell program not written by me. During runtime one function is called with parameters which makes the function throw an error. I want to know from where the function is called. In c++ I would set a breakpoint on the error throwing code, and let gdb print the backtrace. To I have similar options in haskell? What other options do I have? Thanks! Nathan

I'm aware of :break in ghci, and I've used it for "little" functions that I was trying to understand. I'm not sure how well it'll work for a large project though. This is the "debugging" section in ghci when you type ":help": -- Commands for debugging: :abandon at a breakpoint, abandon current computation :back go back in the history (after :trace) :break [<mod>] <l> [<col>] set a breakpoint at the specified location :break <name> set a breakpoint on the specified function :continue resume after a breakpoint :delete <number> delete the specified breakpoint :delete * delete all breakpoints :force <expr> print <expr>, forcing unevaluated parts :forward go forward in the history (after :back) :history [<n>] after :trace, show the execution history :list show the source code around current breakpoint :list identifier show the source code for <identifier> :list [<module>] <line> show the source code around line number <line> :print [<name> ...] prints a value without forcing its computation :sprint [<name> ...] simplifed version of :print :step single-step after stopping at a breakpoint :step <expr> single-step into <expr> :steplocal single-step within the current top-level binding :stepmodule single-step restricted to the current module :trace trace after stopping at a breakpoint :trace <expr> evaluate <expr> with tracing on (see :history) I hope this helps. Bryce On 8/16/12 7:31 AM, Nathan Hüsken wrote:
Hey,
I am trying to understand haskell program not written by me. During runtime one function is called with parameters which makes the function throw an error. I want to know from where the function is called.
In c++ I would set a breakpoint on the error throwing code, and let gdb print the backtrace. To I have similar options in haskell? What other options do I have?
Thanks! Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi On Thu, Aug 16, 2012 at 04:31:47PM +0200, Nathan Hüsken wrote:
I am trying to understand haskell program not written by me. During runtime one function is called with parameters which makes the function throw an error. I want to know from where the function is called.
The execution model of Haskell is completely different. Actually, code locations are not executed. Instead, expressions are evaluated. And to make things worse, this evaluation is performed lazyly. So there is no stack trace as you know it from C++. There is one nice hack, though, that often helps in such situations. The module Debug.Trace exports a handy function 'trace :: String -> a -> a'. It always returns the second argument but outputs the String to stdout as a side effect. The hack is, that this function can be called in pure code! (No IO type involved). So, go to all locations in your code where you call the function is question and wrap the calls into a trace each. Greetings Alex

On 08/17/2012 02:45 PM, alex-haskell@copton.net wrote:
Hi
On Thu, Aug 16, 2012 at 04:31:47PM +0200, Nathan Hüsken wrote:
I am trying to understand haskell program not written by me. During runtime one function is called with parameters which makes the function throw an error. I want to know from where the function is called.
The execution model of Haskell is completely different. Actually, code locations are not executed. Instead, expressions are evaluated. And to make things worse, this evaluation is performed lazyly. So there is no stack trace as you know it from C++.
Well, yes. But one could trace when a value is evaluated and for what it is evaluated (recursively to get something like a backtrace).
There is one nice hack, though, that often helps in such situations. The module Debug.Trace exports a handy function 'trace :: String -> a -> a'. It always returns the second argument but outputs the String to stdout as a side effect. The hack is, that this function can be called in pure code! (No IO type involved).
So, go to all locations in your code where you call the function is question and wrap the calls into a trace each.
That is nice, and can be pretty time-consuming for what I want to do. On the other hand, in the current problem there are not many calls to the function, so it should be doable :). Thanks! Nathan

On Thu, Aug 16, 2012 at 10:31 AM, Nathan Hüsken
I am trying to understand haskell program not written by me. During runtime one function is called with parameters which makes the function throw an error. I want to know from where the function is called.
If this is an exception, as opposed to crashing with a signal, then you can compile the program with profiling and pass it parameters "+RTS -xc" to see the evaluation stack when the exception is thrown. See the description of the -xc RTS option at http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html.... -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (4)
-
alex-haskell@copton.net
-
Brandon Allbery
-
Bryce Verdier
-
Nathan Hüsken