3

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

glennsl
  • 28,186
  • 12
  • 57
  • 75
Robert White
  • 125
  • 1
  • 5

1 Answers1

4

#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.

ivg
  • 34,431
  • 2
  • 35
  • 63