caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Stack backtrace for exception in a running program
@ 2005-10-25 22:26 Robert Schneck-McConnell
  0 siblings, 0 replies; only message in thread
From: Robert Schneck-McConnell @ 2005-10-25 22:26 UTC (permalink / raw)
  To: caml-list

I saw that back in January someone asked about printing a backtrace
for an exception without killing the program.  I recently needed to do
this and found that the suggestion from that earlier thread to write
a C extension (which didn't seem to work for the original poster)
worked fine for me.

In case anyone else needs this information, here's how.

(1) You need this short C file, call it "ml_backtrace.c"
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/fail.h>

CAMLextern void caml_print_exception_backtrace(void);

CAMLprim value ml_print_exception_backtrace (value unit)
{
  caml_print_exception_backtrace();
  return Val_unit;
}


(2) In your OCaml program, include the line
external print_exception_backtrace : unit -> unit =
"ml_print_exception_backtrace"

(3) Call "print_exception_backtrace ()" anytime you need it after
catching an exception.

(4) Compile the OCaml program in bytecode with -g, link with
-g -custom and against the ml_backtrace.o file.  In one line,
ocamlc -o program -g -custom program.ml ml_backtrace.c

(5) Make sure the environment variable OCAMLRUNPARAM=b is set when
you run the program.

Best,
Robert


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-10-25 22:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-25 22:26 Stack backtrace for exception in a running program Robert Schneck-McConnell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).