123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108openBasemodulePrintexc=Stdlib.PrintexcmoduleMode=structtypet=|Disabled|Top_of_stack|Full_traceend(* Default caller_id mode is [Disabled]. It can be enabled by setting the environment var
[HARDCAML_DEBUG]. If it's value is [top] then [Top_of_stack] is used - otherwise
[Full_trace]. *)letmode=letdefault=matchSys.getenv"HARDCAML_DEBUG"with|Somevalue->ifString.(uppercasevalue="TOP")thenMode.Top_of_stackelseMode.Full_trace|None->Disabledinrefdefault;;letset_modem=mode:=m(* Small helper to find out who is the caller of a function *)typet=|Top_of_stackofPrintexc.location|Full_traceofPrintexc.locationoptionlistletsexp_of_location(t:Printexc.location)=letloc=Printf.sprintf"%s:%i:%i"t.filenamet.line_numbert.start_charin[%sexp(loc:string)];;letsexp_of_t(t:t)=matchtwith|Top_of_stacks->[%sexp(s:location)]|Full_traces->[%sexp(s:locationoptionlist)];;letbasic_skipped_modules=["list.ml";"list0.ml";"array.ml";"comb.ml";"interface.ml";"signal.ml";"bits.ml";"with_valid.ml";"scope.ml";"parameter.ml";"hierarchy.ml";Stdlib.__FILE__];;letget_skipped_modulesskip=matchskipwith|[]->basic_skipped_modules|_->basic_skipped_modules@skip;;letget_backtrace()=letstack=Printexc.get_callstack16inletlen=Printexc.raw_backtrace_lengthstackinstack,len;;lettopskip=letskip=get_skipped_modulesskipinletstack,len=get_backtrace()inletrectoppos=ifpos=lenthenNoneelse(matchPrintexc.get_raw_backtrace_slotstackpos|>Printexc.convert_raw_backtrace_slot|>Printexc.Slot.locationwith|None->None|Someloc->ifList.mem~equal:String.equalskiploc.filenamethentop(pos+1)elseSomeloc)intop0|>Option.map~f:(funs->Top_of_stacks);;letfull()=letstack,len=get_backtrace()inletrecfullpos=ifpos=lenthen[]else(Printexc.get_raw_backtrace_slotstackpos|>Printexc.convert_raw_backtrace_slot|>Printexc.Slot.location)::full(pos+1)inSome(Full_trace(full0));;letget?(skip=[])()=match!modewith|Disabled->None|Top_of_stack->topskip|Full_trace->full();;