| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
exception Anomaly of string option * Pp.t |
| 16 |
|
| 17 |
let _ = |
| 18 |
let pr = function |
| 19 |
| Anomaly (s, pp) -> Some ("\"Anomaly: " ^ string_of_ppcmds pp ^ "\"") |
| 20 |
| _ -> None |
| 21 |
in |
| 22 |
Printexc.register_printer pr |
| 23 |
|
| 24 |
let anomaly ?loc ?info ?label pp = |
| 25 |
let info = Option.default Exninfo.null info in |
| 26 |
let info = Option.cata (Loc.add_loc info) info loc in |
| 27 |
Exninfo.iraise (Anomaly (label, pp), info) |
| 28 |
|
| 29 |
exception UserError of Pp.t |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
let _ = Printexc.register_printer (function |
| 36 |
| UserError msg -> |
| 37 |
Some (Format.asprintf "@[Rocq Error: %a@]" Pp.pp_with msg) |
| 38 |
| _ -> None) |
| 39 |
|
| 40 |
let user_err ?loc ?info strm = |
| 41 |
let info = Option.default Exninfo.null info in |
| 42 |
let info = Option.cata (Loc.add_loc info) info loc in |
| 43 |
Exninfo.iraise (UserError strm, info) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
let where = function |
| 50 |
| None -> mt () |
| 51 |
| Some s -> |
| 52 |
str "in " ++ str s ++ str ":" ++ spc () |
| 53 |
|
| 54 |
let raw_anomaly e = match e with |
| 55 |
| Anomaly (s, pps) -> |
| 56 |
where s ++ pps |
| 57 |
| Assert_failure _ | Match_failure _ -> |
| 58 |
str (Printexc.to_string e) ++ str "." |
| 59 |
| _ -> |
| 60 |
str "Uncaught exception " ++ str (Printexc.to_string e) ++ str "." |
| 61 |
|
| 62 |
let print_backtrace e = match Exninfo.get_backtrace e with |
| 63 |
| None -> None |
| 64 |
| Some bt -> |
| 65 |
let bt = str (Exninfo.backtrace_to_string bt) in |
| 66 |
Some (hov 0 bt) |
| 67 |
|
| 68 |
let print_anomaly askreport e = |
| 69 |
if askreport then |
| 70 |
hov 0 (str "Anomaly" ++ spc () ++ quote (raw_anomaly e)) ++ spc () ++ |
| 71 |
hov 0 (str "Please report at " ++ str Coq_config.wwwbugtracker ++ str ".") |
| 72 |
else |
| 73 |
hov 0 (raw_anomaly e) |
| 74 |
|
| 75 |
let handle_stack = ref [] |
| 76 |
|
| 77 |
let register_handler h = handle_stack := h::!handle_stack |
| 78 |
|
| 79 |
let is_handled e = |
| 80 |
let is_handled_by h = Option.has_some (h e) in |
| 81 |
match e with |
| 82 |
| Anomaly _ -> false |
| 83 |
| _ -> List.exists is_handled_by !handle_stack |
| 84 |
|
| 85 |
let is_async = function |
| 86 |
| Control.Timeout | Sys.Break | Out_of_memory | Stack_overflow -> true |
| 87 |
| e -> Memprof_coq.is_interrupted() |
| 88 |
|
| 89 |
let exit_code e = if is_handled e then 1 else 129 |
| 90 |
|
| 91 |
let is_sync_anomaly e = not (is_async e) && not (is_handled e) |
| 92 |
|
| 93 |
|
| 94 |
let additional_error_info_handler = ref [] |
| 95 |
|
| 96 |
let register_additional_error_info (f : Exninfo.info -> Pp.t option) = |
| 97 |
additional_error_info_handler := f :: !additional_error_info_handler |
| 98 |
|
| 99 |
let () = register_additional_error_info print_backtrace |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
let rec print_gen ~anomaly stk e = |
| 106 |
match stk with |
| 107 |
| [] -> |
| 108 |
print_anomaly anomaly e |
| 109 |
| h::stk' -> |
| 110 |
match h e with |
| 111 |
| Some err_msg -> err_msg |
| 112 |
| None -> print_gen ~anomaly stk' e |
| 113 |
|
| 114 |
let print_extra info = |
| 115 |
let pp = |
| 116 |
CList.map_filter (fun f -> f info) !additional_error_info_handler |
| 117 |
in |
| 118 |
if CList.is_empty pp then mt() |
| 119 |
else fnl() ++ prlist_with_sep fnl (fun x -> x) pp |
| 120 |
|
| 121 |
let print_gen ~anomaly (e, info) = |
| 122 |
let extra_msg = print_extra info in |
| 123 |
try |
| 124 |
print_gen ~anomaly !handle_stack e ++ extra_msg |
| 125 |
with exn -> |
| 126 |
let exn, info = Exninfo.capture exn in |
| 127 |
|
| 128 |
str "<in exception printer>:" ++ spc() ++ print_anomaly anomaly exn ++ print_extra info ++ fnl() ++ |
| 129 |
str "<original exception>:" ++ spc() ++ print_anomaly false e ++ extra_msg |
| 130 |
|
| 131 |
let print_quickfix = function |
| 132 |
| [] -> mt () |
| 133 |
| qf -> fnl () ++ Quickfix.print qf |
| 134 |
|
| 135 |
|
| 136 |
let iprint (e, info) = |
| 137 |
print_gen ~anomaly:true (e,info) ++ |
| 138 |
if !Flags.test_mode then |
| 139 |
Result.fold ~ok:print_quickfix ~error:(print_gen ~anomaly:true) (Quickfix.from_exception e) |
| 140 |
else |
| 141 |
mt () |
| 142 |
|
| 143 |
let print e = |
| 144 |
iprint (e, Exninfo.info e) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
let iprint_no_report (e, info) = |
| 149 |
print_gen ~anomaly:false (e,info) |
| 150 |
let print_no_report e = iprint_no_report (e, Exninfo.info e) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
let _ = register_handler begin function |
| 155 |
| UserError pps -> |
| 156 |
Some pps |
| 157 |
| _ -> None |
| 158 |
end |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
[@@@ocaml.warning "-52"] |
| 165 |
let noncritical = function |
| 166 |
| Sys.Break | Out_of_memory | Stack_overflow |
| 167 |
| Assert_failure _ | Match_failure _ | Anomaly _ |
| 168 |
| Control.Timeout -> false |
| 169 |
| Invalid_argument "equal: functional value" -> false |
| 170 |
| _ -> not (Memprof_coq.is_interrupted ()) |
| 171 |
[@@@ocaml.warning "+52"] |
| 172 |
|
| 173 |
|
| 174 |
let to_result ~f x = |
| 175 |
try Ok (f x) |
| 176 |
with exn when noncritical exn -> |
| 177 |
let iexn = Exninfo.capture exn in |
| 178 |
Error iexn |