Backtraces in script run by ocaml

How do I get proper backtraces when running script with ocaml?

For example I have

let f () = failwith "test"
let proc () = f ()
let () = proc ()

When I run ocaml with env OCAMLRUNPARAM=b ocaml I get

Exception: Failure "test".
Raised at file "", line 29, characters 22-33
Called from unknown location
Called from file "toplevel/", line 212, characters 17-27

How do I get proper location in my script?

I noticed that I get proper backtrace for my actual script when I #use in toplevel. That doesn’t work for the test script above though. My actual script rises Exception: Invalid_argument "index out of bounds"

Here’s the link to my original script

I’m using ocaml-base-compiler.4.10.0 in local switch


Tried both OCAMLRUNPARAM=b and OCAMLRUNPARAM=b. These do enable collecting backtraces, but I get Called from unknown location instead of proper location in my script.

@Serpent7776 backtraces are only available when debug symbols are available.

If you compile your with ocamlc -g, and set OCAMLRUNPARAM=b, you should get a backtrace.

1 Like

This appears to be a regression in OCaml 4.08. Older OCaml versions (starting from 4.03) would correctly show backtraces for script files run through ocaml. @Serpent7776, would you care to open an issue on the OCaml bugtracker, or would you prefer if I did it myself?

1 Like

@ahem ocamlc seems to work fine, but I don’t want compilation step
@gasche Opened a bug here


The issue is that the bytecode is released before the backtrace is retrieved and printed, as a result no debugging information is availbable.

Documented everything on the issue.

1 Like