More stack traces in OCaml
January 2, 2008 | Be the first to comment
Following up on our earlier patch, here is a patch to show backtraces in the OCaml top level and for dynamically-loaded code (this patch subsumes the previous one). This feature is often requested on the OCaml mailing list, and we have found it very useful in development to get a quick idea of where something is breaking.
- download the patch and save it as
backtrace.patch - unpack a fresh OCaml 3.10.0 tree
patch -p0 < backtrace.patchin the same directory where you unpacked OCaml.cdinto the OCaml tree./configuremake core promote- remove the lines in
stdlib/printexc.mlithat are markedUNCOMMENT make world.optor whatever you normally do to build OCaml
In the top level, you will want to run
#capture_backtrace true#debug true
The first turns on backtrace capturing (it just calls Printexc.capture_backtrace); the second sets the compiler so it generates debugging events when code is compiled in the top level.
Now if you #load code (or load code with Dynlink) that has debugging events, or #use an external file (or enter an expression into the toplevel, but see the caveat below), you should see a backtrace on an uncaught exception.
For programs other than the top level this facility works as before, and additionally works with code loaded through Dynlink.
Caveats:
- You’ll need to rebuild external libraries with debugging if you want complete backtraces (the stdlib has it already).
- Backtrace locations in code entered at the top level are not very helpful, since there is no filename to refer to; you’ll get better results with
#use. - In the stock OCaml runtime, debugging events are loaded from the executable when a backtrace is to be generated, and then garbage collected. With this patch they are loaded when the code is loaded, and kept around forever, taking up some memory.
- The caveat on the previous patch about changing directories no longer applies, since debugging events are loaded on startup.
Posted by: Jake