How to print function calls and backtrace in the toplevel?

416 views Asked by At

I'm trying to get the backtrace of function calls. I wonder if anyone knows how to do that in the toplevel.

1

There are 1 answers

0
ivg On

#trace directive is useful to trace functions, e.g.,

# let rec f x = if x > 0 then f (x - 1) else "done";;
val f : int -> string = <fun>
# #trace f;;
f is now traced.
# f 12;;
f <-- 12
f <-- 11
f <-- 10
...

To trace several functions, use #trace for all of them, e.g.,

#trace f;;
#trace g;;

Do not forget, that after you redefined a function, you need to invoke #trace again, since from the toplevel perspective this is a new function, although it has the same name.

To untrace function f use #untrace f, to untrace all functions, that are currently traced use #untrace_all

Also, you may find useful Printexc.get_callstack function, that, if you enabled trace recording with Printexc.record_bactrace true, will show you current call stack.