aboutsummaryrefslogtreecommitdiffhomepage
path: root/clib/backtrace.mli
blob: cd31511628c4b41403fda29366128c8abbb34c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2018       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)

(** * Low-level management of OCaml backtraces.

  Currently, OCaml manages its backtraces in a very imperative way. That is to
  say, it only keeps track of the stack destroyed by the last raised exception.
  So we have to be very careful when using this module not to do silly things.

  Basically, you need to manually handle the reraising of exceptions. In order
  to do so, each time the backtrace is lost, you must [push] the stack fragment.
  This essentially occurs whenever a [with] handler is crossed.

*)

(** {5 Backtrace information} *)

type location = {
  loc_filename : string;
  loc_line : int;
  loc_start : int;
  loc_end : int;
}
(** OCaml debugging information for function calls. *)

type frame = { frame_location : location option; frame_raised : bool; }
(** A frame contains two informations: its optional physical location, and
    whether it raised the exception or let it pass through. *)

type t
(** Type of backtraces. They're essentially stack of frames. *)

val empty : t
(** Empty frame stack. *)

val push : t -> t
(** Add the current backtrace information to a given backtrace. *)

val repr : t -> frame list
(** Represent a backtrace as a list of frames. Leftmost element is the outermost
    call. *)

(** {5 Utilities} *)

val print_frame : frame -> string
(** Represent a frame. *)

(** {5 Exception handling} *)

val record_backtrace : bool -> unit
(** Whether to activate the backtrace recording mechanism. Note that it will
    only work whenever the program was compiled with the [debug] flag. *)

val get_backtrace : Exninfo.info -> t option
(** Retrieve the optional backtrace coming with the exception. *)

val add_backtrace : exn -> Exninfo.iexn
(** Add the current backtrace information to the given exception.

    The intended use case is of the form: {[

    try foo
      with
      | Bar -> bar
      | err -> let err = add_backtrace 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 = add_backtrace err in
        match err with
        | Bar -> bar
        | err -> baz

    ]}

    I admit that's a bit heavy, but there is not much to do...

*)

val app_backtrace : src:Exninfo.info -> dst:Exninfo.info -> Exninfo.info
(** Append the backtrace from [src] to [dst]. The returned exception is [dst]
    except for its backtrace information. This is targeted at container
    exceptions, that is, exceptions that contain exceptions. This way, one can
    transfer the backtrace from the container to the underlying exception, as if
    the latter was the one originally raised. *)