Reputation: 120576
I have an OUnit test that doesn't halt and I'd like to be able to get a stacktrace.
I'm compiling with -g
and I get a stacktrace when a test completes abnormally.
I tried trapping signals, but can't figure out how to dump stacks for live threads
let () = begin
(* Report stacktraces in test failures *)
Printexc.record_backtrace true;
(* Exit on Ctrl-C. *)
let flush_and_abort _ =
prerr_string "aborted by signal\n";
(***** What do I do here to dump stacks for live threads? ****)
flush stderr;
flush stdout;
exit ~-1 in
Sys.set_signal Sys.sigint (Sys.Signal_handle flush_and_abort);
Sys.set_signal Sys.sigquit (Sys.Signal_handle flush_and_abort);
end
Upvotes: 4
Views: 257
Reputation: 31469
I don't think signal handlers are executed in a context where you can get the backtrace. The natural choice would be to use Printexc.print_backtrace stderr
, but (on my machine at least) it doesn't work.
My advice would be to use runtime profiling instead: compile with ocamlopt -p
(or .p.native
ocamlbuild's target), run your program, interrupt it, and use gprof <executable>
to see the profiling info. It should highlight in which function was most of the time spent, and with visualization tool such as gprof2dot you can get reasonable call graphs. Not optimal, but may still solve your problems.
PS: I think you still need your signal catching routine for profiling to work: if the program is killed instead of interrupting itself it may not spit out the profiling information.
Upvotes: 2