From 1ce2c89e8fd2f80b49fcac9e045667b7233391ef Mon Sep 17 00:00:00 2001 From: ppedrot Date: Mon, 28 Jan 2013 21:05:51 +0000 Subject: Added backtrace primitives. Using OCaml 3.11+ builtin facilities to record stack frames during exception raising, we can now recover at runtime the backtrace of an uncaught toplevel exception and display it to the user, without the infamous OCaml debugger. The backtrace is displayed when using the [-debug] flag. This requires a bit of discipline, as each time we reraise an exception we need to keep track of those frames we discarded between the previous raise and the current [try-with] branch. Currently, only [Anomaly] is handled, but this can be ported to any exception as long as we add the backtrace info into the exception, and we provide the corresponding handler to [Backtracke.register_backtrace_handler]. Hopefully this should not be to costly, as we only do little work when reraising, and only with the [-debug] flag set. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/coq/trunk@16166 85f007b7-540e-0410-9357-904b9bb8a0f7 --- lib/backtrace.ml | 89 ++++++++++++++++++++++++++++++++++++++++++++ lib/backtrace.mli | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/clib.mllib | 1 + 3 files changed, 198 insertions(+) create mode 100644 lib/backtrace.ml create mode 100644 lib/backtrace.mli (limited to 'lib') diff --git a/lib/backtrace.ml b/lib/backtrace.ml new file mode 100644 index 0000000000..84f208c9ef --- /dev/null +++ b/lib/backtrace.ml @@ -0,0 +1,89 @@ +(***********************************************************************) +(* v * The Coq Proof Assistant / The Coq Development Team *) +(* raw_frame array option + = "caml_get_exception_backtrace" + +type t = frame list option + +let empty = Some [] + +let none = None + +let of_raw = function +| Unknown_location r -> + { frame_location = None; frame_raised = r; } +| Known_location (r, file, line, st, en) -> + let loc = { + loc_filename = file; + loc_line = line; + loc_start = st; + loc_end = en; + } in + { frame_location = Some loc; frame_raised = r; } + +let push bt = match bt with +| None -> None +| Some stack -> + match get_exception_backtrace () with + | None -> bt + | Some frames -> + let len = Array.length frames in + let rec append accu i = + if i = len then accu + else append (of_raw frames.(i) :: accu) (succ i) + in + Some (append stack 0) + +(** Utilities *) + +let print_frame frame = + let raise = if frame.frame_raised then "raise" else "frame" in + match frame.frame_location with + | None -> Printf.sprintf "%s @ unknown" raise + | Some loc -> + Printf.sprintf "%s @ file \"%s\", line %d, characters %d-%d" + raise loc.loc_filename loc.loc_line loc.loc_start loc.loc_end + +(** Exception manipulation *) + +let handlers = ref [] + +let register_backtrace_handler h = + handlers := h :: !handlers + +let rec push_exn_aux e = function +| [] -> e +| f :: l -> + match f e with + | None -> push_exn_aux e l + | Some e -> e + +let push_exn e = + push_exn_aux e !handlers + +let reraise e = + let e = push_exn e in + raise e diff --git a/lib/backtrace.mli b/lib/backtrace.mli new file mode 100644 index 0000000000..d107535523 --- /dev/null +++ b/lib/backtrace.mli @@ -0,0 +1,108 @@ +(***********************************************************************) +(* v * The Coq Proof Assistant / The Coq Development Team *) +(* t +(** Add the current backtrace information to a given backtrace. *) + +(** {5 Utilities} *) + +val print_frame : frame -> string +(** Represent a frame. *) + +(** {5 Exception handling} *) + +val register_backtrace_handler : (exn -> exn option) -> unit +(** Add a handler to enrich backtrace information that may be carried by + exceptions. If the handler returns [None], this means that it is not its + duty to handle this one. Otherwise, the new exception will be used by the + functions thereafter instead of the original one. + + Handlers are called in the reverse order of their registration. If no + handler match, the original exception is returned. +*) + +val push_exn : exn -> exn +(** Add the current backtrace information to the given exception, using the + registered handlers. + + The intended use case is of the form: {[ + + try foo + with + | Bar -> bar + | err -> let err = push_exn err in baz + + ]} + + WARNING: any intermediate code between the [with] and the handler may + modify the backtrace. Yes, that includes [when] clauses. Ideally, what you + should do is something like: {[ + + try foo + with err -> + let err = push_exn err in + match err with + | Bar -> bar + | err -> baz + + ]} + + I admit that's a bit heavy, but there is not much to do... + +*) + +val reraise : exn -> 'a +(** Convenience function which covers a generic pattern in Coq code. + [reraise e] is equivalent to [raise (push_exn e)]. + + The intended use case is of the form: {[ + + try foo + with + | Bar -> bar + | err -> reraise err + + ]} + +*) diff --git a/lib/clib.mllib b/lib/clib.mllib index d40df73a01..55bfdc835e 100644 --- a/lib/clib.mllib +++ b/lib/clib.mllib @@ -1,3 +1,4 @@ +Backtrace Int Pp_control Pp -- cgit v1.2.3