Skip to content

Instantly share code, notes, and snippets.

@ghulette
Created April 25, 2017 22:16
Show Gist options
  • Save ghulette/55b0419e624813f57d1bcbd7a0e00edb to your computer and use it in GitHub Desktop.
Save ghulette/55b0419e624813f57d1bcbd7a0e00edb to your computer and use it in GitHub Desktop.
How to get a backtrace out of ocaml
(*******************************************************************************
Getting a backtrace out of ocaml is not completely obvious. You
have to compile the code with -g and then run with OCAMLRUNPARAM=b,
like this:
$ ocamlc -g -o test test.ml
$ OCAMLRUNPARAM=b ./test
backtrace_status: true
Failure("nth")
Raised at file "pervasives.ml", line 32, characters 22-33
Called from file "test.ml", line 20, characters 10-29
Called from file "test.ml", line 29, characters 4-10
******************************************************************************)
open Printf
let bar () =
let i = List.nth [1;2;3] 10 in (* raise Not_found *)
printf "This should not print %d\n" i
let foo () =
bar ()
let () =
try
printf "backtrace_status: %b\n" (Printexc.backtrace_status ());
foo ()
with e ->
printf "%s\n" (Printexc.to_string e);
Printexc.print_backtrace stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment