1234567891011121314151617181920212223242526272829303132333435363738394041424344open!ImportmoduleSys=Sys0typet=Caml.Printexc.raw_backtraceletelide=refam_testingletelided_message="<backtrace elided in test>"letget?(at_most_num_frames=Int.max_value)()=Caml.Printexc.get_callstackat_most_num_frames;;letto_stringt=if!elidethenelided_messageelseCaml.Printexc.raw_backtrace_to_stringt;;letto_string_listt=String.split_lines(to_stringt)letsexp_of_tt=Sexp.List(List.map(to_string_listt)~f:(funx->Sexp.Atomx))moduleExn=structletset_recording=Caml.Printexc.record_backtraceletam_recording=Caml.Printexc.backtrace_statusletmost_recent()=Caml.Printexc.get_raw_backtrace()(* We turn on backtraces by default if OCAMLRUNPARAM doesn't explicitly mention them. *)letmaybe_set_recording()=letocamlrunparam_mentions_backtraces=matchSys.getenv"OCAMLRUNPARAM"with|None->false|Somex->List.exists(String.splitx~on:',')~f:(String.is_prefix~prefix:"b")inifnotocamlrunparam_mentions_backtracesthenset_recordingtrue;;(* the caller set something, they are responsible *)letwith_recordingb~f=letsaved=am_recording()inset_recordingb;Exn.protect~f~finally:(fun()->set_recordingsaved);;endletinitialize_module()=Exn.maybe_set_recording()