Debian Codegraph @

clib/exninfo.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** Enriched exceptions have an additional field at the end of their usual data
12 containing a pair composed of the distinguishing [token] and the backtrace
13 information. We discriminate the token by pointer equality. *)
14
15 module Store = Store.Make ()
16
17 type 'a t = 'a Store.field
18
19 type info = Store.t
20
21 type iexn = exn * info
22
23 let make = Store.field
24 let add = Store.set
25 let get = Store.get
26 let null = Store.empty
27
28 exception Unique
29
30 let dummy = (Unique, Store.empty)
31
32 let current : (int * iexn) list ref = ref []
33 (** List associating to each thread id the latest exception raised by an
34 instrumented raise (i.e. {!raise} from this module). It is shared between
35 threads, so we must take care of this when modifying it.
36
37 Invariants: all index keys are unique in the list.
38 *)
39
40 let lock = Mutex.create ()
41
42 let rec remove_assoc (i : int) = function
43 | [] -> []
44 | (j, v) :: rem as l ->
45 if i = j then rem
46 else
47 let ans = remove_assoc i rem in
48 if rem == ans then l
49 else (j, v) :: ans
50
51 let rec find_and_remove_assoc (i : int) = function
52 | [] -> dummy, []
53 | (j, v) :: rem as l ->
54 if i = j then (v, rem)
55 else
56 let (r, ans) = find_and_remove_assoc i rem in
57 if rem == ans then (r, l)
58 else (r, (j, v) :: ans)
59
60 type backtrace = Printexc.raw_backtrace
61 let backtrace_to_string = Printexc.raw_backtrace_to_string
62
63 let backtrace_info : backtrace t = make "exninfo_backtrace"
64
65 let is_recording = ref false
66
67 let record_backtrace b =
68 let () = Printexc.record_backtrace b in
69 is_recording := b
70
71 let get_backtrace e = get e backtrace_info
72
73 let iraise (e,i) =
74 CThread.with_lock lock ~scope:(fun () ->
75 let id = Thread.id (Thread.self ()) in
76 current := (id, (e,i)) :: remove_assoc id !current);
77 match get i backtrace_info with
78 | None ->
79 raise e
80 | Some bt ->
81 Printexc.raise_with_backtrace e bt
82
83 let find_and_remove () =
84 CThread.with_lock lock ~scope:(fun () ->
85 let id = Thread.id (Thread.self ()) in
86 let (v, l) = find_and_remove_assoc id !current in
87 let () = current := l in
88 v)
89
90 let info e =
91 let (src, data) = find_and_remove () in
92 if src == e then
93 (* Slightly unsound, some exceptions may not be unique up to pointer
94 equality. Though, it should be quite exceptional to be in a situation
95 where the following holds:
96
97 1. An argument-free exception is raised through the enriched {!raise};
98 2. It is not captured by any enriched with-clause (which would reset
99 the current data);
100 3. The same exception is raised through the standard raise, accessing
101 the wrong data.
102 . *)
103 data
104 else
105 (* Mismatch: the raised exception is not the one stored, either because the
106 previous raise was not instrumented, or because something went wrong. *)
107 Store.empty
108
109 let capture e =
110 if !is_recording then
111 (* This must be the first function call, otherwise the stack may be
112 destroyed *)
113 let bt = Printexc.get_raw_backtrace () in
114 let info = info e in
115 e, add info backtrace_info bt
116 else
117 e, info e
118
119 let reify () =
120 if !is_recording then
121 let bt = Printexc.get_callstack 50 in
122 add null backtrace_info bt
123 else
124 null