Backtraces in script run by ocaml

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

For example I have test.ml

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

When I run ocaml with env OCAMLRUNPARAM=b ocaml test.ml I get

Exception: Failure "test".
Raised at file "stdlib.ml", line 29, characters 22-33
Called from unknown location
Called from file "toplevel/toploop.ml", 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 script.ml 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 https://godbolt.org/z/nhcPby

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

See https://ocaml.org/learn/tutorials/error_handling.html#Stacktraces

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.

https://caml.inria.fr/pub/docs/manual-ocaml/runtime.html

If you compile your test.ml with ocamlc -g test.ml, 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 https://github.com/ocaml/ocaml/issues/9701

3 Likes