The Objective Caml language will only produce stack traces if you ask for them just right - what are the requirements for both bytecode and native code?
Compile with -g and set environment variable OCAMLRUNPARAM=b
Some Printexc functions let you do this programmatically.
Printexc.record_backtrace true
. –
Huang Because it looks like you can only get traces for exceptions on unix you can fork and throw the exception in the second process. This way the main process can continue:
export OCAMLRUNPARAM=b
# compile with -g
flush_all(); let r = Unix.fork() in if r == 0 then raise Exit
If you are using Ocamlbuild instead of invoking compiler directly, you can use the debug
tag. From the manual:
With OCamlbuild, you can simply add the debug tag to your program’s targets, and it will sort out when to insert the -g flag or not.
For example, if you are building a file foo.ml
with package bar
then your _tags
file will have a line:
<foo.ml>: package(bar), debug
This will insert the appropriate -g
flags while building bytecode/native files. However, you still need to set the environment variable using export OCAMLRUNPARAM=b
as mentioned in the other answers.
As noted in other answers, you need to compile your project with debugging info and run it with the OCAMLRUNPARAM=b
environment variable.
A convenient way to have Ocamlbuild compile an entire project with debugging info but without editing the _tags
file is to specify a special debug target. From the manual:
The preferred way of compiling code suitable for debugging with
ocamldebug
or profiling native code withocamlprof
is to use the appropriate target extensions,.d.byte
for debugging or.p.native
.
I use this technique for quick compile-run cycles on the command line. For example, to run foo.ml
:
export OCAMLRUNPARAM=b
ocamlbuild -no-links foo.d.byte && _build/foo.d.byte
© 2022 - 2024 — McMap. All rights reserved.