Print the current call stack in OCaml

Is there a way in OCaml to get the current call stack programmatically? Thus, I do not mean inside the debugger, but as a function call inside the program that will print the current call stack. I suggest that this should not go beyond the capabilities of the byte code interpreter, especially if debugging characters are available.

+8
ocaml
source share
4 answers

I came to this question looking for the same, here is my solution

Printexc.get_callstack 5 |> Printexc.raw_backtrace_to_string

(This is actually a pretty good way to get to know the new code base)

+2
source share

You can also use ocamldebug, with which you can run your code compiled in bytecode. In this environment, Printexc.get_backtrace () is much more complete.

+1
source share

Glibc backtrace can be used for native code, although it may not print all stack frames correctly.

0
source share

Unfortunately, the only way to get the return line inside the code is when an exception occurs, you can use Printexc.get_backtrace (). This will not give you, although the function names, just a place in the code of what is on the stack, and only if OCaml was able to restore them ...

0
source share

All Articles