<?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/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"
  prev="2003/12/1a50c3506ead0173fd35d09e8a1fee0e"
  next="2003/12/bee38859b007b41ffefe31e5b16980d4"
  prev-in-thread="2003/12/bd06ee9099e75c3d3e1ca4d8b3a79021"
  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 Wed, Dec 03, 2003 at 12:19:05PM +0900, Nicolas Cannasse wrote:
&gt; &gt; &gt; Is there a reason why stack traces are available only on a crash?  I
&gt; &gt; &gt; have a project (a distributed OpenPGP keyserver system,
&gt; &gt; &gt; http://www.nongnu.org/sks/) that is a long-running daemon.  Unexpected
&gt; &gt; &gt; errors are caught and logged, but unfortunately, there's no way of
&gt; &gt; &gt; getting a stack-trace, since I don't let the exceptions kill the
&gt; &gt; &gt; program.  This makes debugging much more difficult, and is one of the
&gt; &gt; &gt; single largest difficulties I have with ocaml.  Is there a technical
&gt; &gt; &gt; reason that a bytecode-compiled executable couldn't have access to the
&gt; &gt; &gt; stack trace during execution?
&gt; &gt;
&gt; &gt; I have a partial solution:
&gt; [...]
&gt; &gt; It's not quite right yet because it only prints out the
&gt; &gt; place where the exception was raised (which will be in
&gt; &gt; Pervasives if you use failwith), and the place where it
&gt; &gt; was caught and printed.  Does someone know how to make it
&gt; &gt; print the whole stack?
&gt; 
&gt; I think this is because you're adding an additional call to "print", that is
&gt; writing on the stack (!). Since you can't write on the stack, you can't call
&gt; any ML handler between catching and printing the exception.
&gt; You should then have no backtrace.ml and only a mli with :
&gt; 
&gt; external print_stack_strace : unit -&gt; unit = "internal_print"
&gt; 
&gt; the user can then print the exception using a Printexc... after printing the
&gt; stack.

This doesn't seem to be the problem.  When I try it I get basically the
same result as before.  Here's the modified code:

==== Makefile ====
sources=backtrace.mli backtrace_stubs.c test_bt.ml
 
test_bt: $(sources)
        ocamlc -custom -g -o test_bt $(sources)
 
clean:
        rm -f test_bt *.o *.cmo *.cmi
 
==== backtrace.mli ====
external print : exn -&gt; unit = "internal_print"
 
==== backtrace_stubs.c ====
#include &lt;caml/mlvalues.h&gt;
 
void print_exception_backtrace(void);
 
value internal_print(value exn)
{
  print_exception_backtrace();
  return Val_unit;
}
 
==== 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
 
-- 
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>

