How to get useful backtraces?

If you compile your program with -g and run your process with OCAMLRUNPARAM=b (see documentation), then a forked process terminating with an uncaught exception will print a backtrace as it should – and the backtrace should start from the start of both processes, not at fork point.

Repro case:

let rec g n =
  if n = 0 then raise Not_found
  else try g (n - 1) with exn -> raise exn (* line 3 *)

let rec f n =
  if n > 0 then try f (n - 1) with exn -> raise exn (* line 6 *)
  else begin
  match Unix.fork () with
  | 0 -> 
    g 2
  | n -> exit 0
end

let () =
  f 2
$ ocamlopt unix.cmxa -g -o test test.ml; OCAMLRUNPARAM=b ./test
Fatal error: exception Not_found
Raised at file "test.ml", line 2, characters 22-31
Called from file "test.ml", line 3, characters 11-20
Re-raised at file "test.ml", line 3, characters 39-42
Called from file "test.ml", line 3, characters 11-20
Re-raised at file "test.ml", line 3, characters 39-42
Called from file "test.ml", line 6, characters 20-29
Re-raised at file "test.ml", line 6, characters 48-51
Called from file "test.ml", line 6, characters 20-29
Re-raised at file "test.ml", line 6, characters 48-51
Called from file "test.ml", line 15, characters 2-5
3 Likes