<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/1d631fdc6491f61c7bc538f7bbf45d0d"
  from="Issac Trotts &lt;ijtrotts@n...&gt;"
  author="Issac Trotts"
  date="2003-12-02T22:25:32"
  subject="Re: [Caml-list] getting stack traces in running code"
  prev="2003/12/8d160e1869335ab568e3f6d10a7c6300"
  next="2003/12/c77a2d55b7855bb7129ecef3d8503541"
  prev-in-thread="2003/12/8d160e1869335ab568e3f6d10a7c6300"
  next-in-thread="2003/12/03846a1812e0e09a7bd0b8f6f6ed02d6"
  prev-thread="2003/12/aa9745fef14bf44399b3477e558c0113"
  next-thread="2003/12/c77a2d55b7855bb7129ecef3d8503541"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] getting stack traces in running code">
<msg 
  url="2003/12/58db6af4f59fb2639367ca79e4b81602"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-12-02T20:50:44"
  subject="[Caml-list] getting stack traces in running code">
<msg 
  url="2003/12/8d160e1869335ab568e3f6d10a7c6300"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-12-02T21:45:36"
  subject="Re: [Caml-list] getting stack traces in running code">
</msg>
<msg 
  url="2003/12/1d631fdc6491f61c7bc538f7bbf45d0d"
  from="Issac Trotts &lt;ijtrotts@n...&gt;"
  author="Issac Trotts"
  date="2003-12-02T22:25:32"
  subject="Re: [Caml-list] getting stack traces in running code">
<msg 
  url="2003/12/03846a1812e0e09a7bd0b8f6f6ed02d6"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-03T03:25:26"
  subject="Re: [Caml-list] getting stack traces in running code">
<msg 
  url="2003/12/bd06ee9099e75c3d3e1ca4d8b3a79021"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2003-12-03T14:19:17"
  subject="Re: [Caml-list] getting stack traces in running code">
</msg>
<msg 
  url="2003/12/36cd132d9e5998a8717e6be5bbc8a40c"
  from="Issac Trotts &lt;ijtrotts@c...&gt;"
  author="Issac Trotts"
  date="2003-12-04T22:22:03"
  subject="Re: [Caml-list] getting stack traces in running code">
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
On Tue, Dec 02, 2003 at 03:50:40PM -0500, Yaron M. Minsky wrote:
&gt; Is there a reason why stack traces are available only on a crash?  I
&gt; have a project (a distributed OpenPGP keyserver system,
&gt; http://www.nongnu.org/sks/) that is a long-running daemon.  Unexpected
&gt; errors are caught and logged, but unfortunately, there's no way of
&gt; getting a stack-trace, since I don't let the exceptions kill the
&gt; program.  This makes debugging much more difficult, and is one of the
&gt; single largest difficulties I have with ocaml.  Is there a technical
&gt; reason that a bytecode-compiled executable couldn't have access to the
&gt; stack trace during execution?

I have a partial solution:

ijtrotts@manzanita:~/tmp/backtrace$ cat backtrace.ml
 
external internal_print : exn -&gt; unit
        = "camlidl_bt_print_exception_backtrace"
 
let print exc =
  prerr_endline (Printexc.to_string exc);
  internal_print exc;
 
ijtrotts@manzanita:~/tmp/backtrace$ cat backtrace.mli
val print : exn -&gt; unit
 
ijtrotts@manzanita:~/tmp/backtrace$ cat bt_stubs.c
/* File generated from bt.idl */
 
#include &lt;stddef.h&gt;
#include &lt;string.h&gt;
#include &lt;caml/mlvalues.h&gt;
#include &lt;caml/memory.h&gt;
#include &lt;caml/alloc.h&gt;
#include &lt;caml/fail.h&gt;
#include &lt;caml/callback.h&gt;
#ifdef Custom_tag
#include &lt;caml/custom.h&gt;
#include &lt;caml/bigarray.h&gt;
#endif
 
#include &lt;stdio.h&gt;
 
void print_exception_backtrace(void);
 
value camlidl_bt_print_exception_backtrace(value exn)
{
  print_exception_backtrace();
  return Val_unit;
}
 
ijtrotts@manzanita:~/tmp/backtrace$ cat test_bt.ml
let quux() = raise (Failure "quux has failed")
 
let baz() = quux()
 
let bar() = baz()
 
let foo() = bar()
 
let () =
  let rec loop = function
      0 -&gt; ()
    | k -&gt;
      begin try foo() with Failure _ as e -&gt; Backtrace.print e end;
      print_newline();
      loop(k-1) in
  loop 3
 
ijtrotts@manzanita:~/tmp/backtrace$ cat Makefile
test_bt: test_bt.ml backtrace.mli backtrace.ml bt_stubs.o
        ocamlc -custom -g -o test_bt bt_stubs.o backtrace.mli backtrace.ml \
          test_bt.ml
 
bt_stubs.o: bt_stubs.c
        ocamlc -c $&lt;
 
It's not quite right yet because it only prints out the 
place where the exception was raised (which will be in 
Pervasives if you use failwith), and the place where it 
was caught and printed.  Does someone know how to make it
print the whole stack?

-- 
Issac Trotts

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

