Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Stack backtrace for exception in a running program
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Robert Schneck-McConnell <schneck@g...>
Subject: Stack backtrace for exception in a running program
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)
  return Val_unit;

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

(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 ml_backtrace.c

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