Backtrace module deals with two different kinds of backtraces:
1. snapshots of the stack obtained on demand (
2. the stack frames unwound when an exception is raised (
Backtrace.tis a snapshot of the stack obtained by calling
Backtrace.get. It is represented as a string with newlines separating the frames.
sexp_of_tsplits the string at newlines and removes some of the cruft, leaving a human-friendly list of frames, but
val get :
(unit -> t) Core_kernel.Std.Or_error.t
val get_opt :
unit -> t option
get_opt ()returns a backtrace if
val to_string :
t -> string
module Exn :
Backtrace.Exnhas functions for controlling and printing the backtrace of the most recently raised exception.
When an exception is raised, the runtime "unwinds" the stack, i.e. removes stack frames, until it reaches a frame with an exception handler. It then matches the exception against the patterns in the handler. If the exception matches, then the program continues. If not, then the runtime continues unwinding the stack to the next handler.
am_recording () = true, then the runtime, while it is unwinding the stack, keeps
track of the part of the stack that is unwound. This is available as a human-readable
most_recent (). Calling
am_recording () = false will
yield the empty string.
am_recording () = true, OCaml keeps only a backtrace for the most recently
raised exception. When one raises an exception, OCaml checks if it is physically
equal to the most recently raised exception. If they are equal, then OCaml appends
the string representation of the stack unwound by the current raise to the stored
backtrace. If the exception being raised is not physically equally to the most
recently raised exception, then OCaml starts recording a new backtrace. Thus one must
most_recent before a subsequent
raise of a (physically) distinct exception,
or the backtrace is lost.
The initial value of
am_recording () is determined by the setting of the
environment variable OCAMLRUNPARAM. If OCAMLRUNPARAM is set, then
= true iff the character "b" occurs in OCAMLRUNPARAM. If OCAMLRUNPARAM is not set,
am_recording () is initially true.
This is the same functionality as provided by the OCaml stdlib
val am_recording :
unit -> bool
val set_recording :
bool -> unit
val most_recent :
unit -> string
most_recent ()returns a string containing the stack that was unwound by the most recently raised exception.
val sexp_of_t :
t -> Sexplib.Sexp.t