| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
data |
| 104 |
else |
| 105 |
|
| 106 |
|
| 107 |
Store.empty |
| 108 |
|
| 109 |
let capture e = |
| 110 |
if !is_recording then |
| 111 |
|
| 112 |
|
| 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 |