caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] getting stack traces in running code
@ 2003-12-02 20:50 Yaron M. Minsky
  2003-12-02 21:45 ` Richard Jones
  2003-12-02 22:26 ` Issac Trotts
  0 siblings, 2 replies; 6+ messages in thread
From: Yaron M. Minsky @ 2003-12-02 20:50 UTC (permalink / raw)
  To: Caml List

Is there a reason why stack traces are available only on a crash?  I
have a project (a distributed OpenPGP keyserver system,
http://www.nongnu.org/sks/) that is a long-running daemon.  Unexpected
errors are caught and logged, but unfortunately, there's no way of
getting a stack-trace, since I don't let the exceptions kill the
program.  This makes debugging much more difficult, and is one of the
single largest difficulties I have with ocaml.  Is there a technical
reason that a bytecode-compiled executable couldn't have access to the
stack trace during execution?

y

-- 
|--------/            Yaron M. Minsky              \--------|
|--------\ http://www.cs.cornell.edu/home/yminsky/ /--------|

Open PGP --- KeyID B1FFD916
Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916


-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] getting stack traces in running code
  2003-12-02 20:50 [Caml-list] getting stack traces in running code Yaron M. Minsky
@ 2003-12-02 21:45 ` Richard Jones
  2003-12-02 22:26 ` Issac Trotts
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Jones @ 2003-12-02 21:45 UTC (permalink / raw)
  To: Yaron M. Minsky; +Cc: Caml List

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

Us too.

A backtrace facility for exceptions would be enormously useful.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
"One serious obstacle to the adoption of good programming languages is
the notion that everything has to be sacrificed for speed. In computer
languages as in life, speed kills." -- Mike Vanier

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] getting stack traces in running code
  2003-12-02 20:50 [Caml-list] getting stack traces in running code Yaron M. Minsky
  2003-12-02 21:45 ` Richard Jones
@ 2003-12-02 22:26 ` Issac Trotts
  2003-12-03  3:19   ` Nicolas Cannasse
  1 sibling, 1 reply; 6+ messages in thread
From: Issac Trotts @ 2003-12-02 22:26 UTC (permalink / raw)
  To: caml-list

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

I have a partial solution:

ijtrotts@manzanita:~/tmp/backtrace$ cat backtrace.ml
 
external internal_print : exn -> 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 -> unit
 
ijtrotts@manzanita:~/tmp/backtrace$ cat bt_stubs.c
/* File generated from bt.idl */
 
#include <stddef.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/callback.h>
#ifdef Custom_tag
#include <caml/custom.h>
#include <caml/bigarray.h>
#endif
 
#include <stdio.h>
 
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 -> ()
    | k ->
      begin try foo() with Failure _ as e -> 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 $<
 
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] getting stack traces in running code
  2003-12-02 22:26 ` Issac Trotts
@ 2003-12-03  3:19   ` Nicolas Cannasse
  2003-12-03 14:19     ` Damien Doligez
  2003-12-03 18:26     ` Issac Trotts
  0 siblings, 2 replies; 6+ messages in thread
From: Nicolas Cannasse @ 2003-12-03  3:19 UTC (permalink / raw)
  To: caml-list, Issac Trotts

> > Is there a reason why stack traces are available only on a crash?  I
> > have a project (a distributed OpenPGP keyserver system,
> > http://www.nongnu.org/sks/) that is a long-running daemon.  Unexpected
> > errors are caught and logged, but unfortunately, there's no way of
> > getting a stack-trace, since I don't let the exceptions kill the
> > program.  This makes debugging much more difficult, and is one of the
> > single largest difficulties I have with ocaml.  Is there a technical
> > reason that a bytecode-compiled executable couldn't have access to the
> > stack trace during execution?
>
> I have a partial solution:
[...]
> 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?

I think this is because you're adding an additional call to "print", that is
writing on the stack (!). Since you can't write on the stack, you can't call
any ML handler between catching and printing the exception.
You should then have no backtrace.ml and only a mli with :

external print_stack_strace : unit -> unit = "internal_print"

the user can then print the exception using a Printexc... after printing the
stack.

BTW, if the "print_exception_backtrace" of the caml sources was exported as
Primitive, we would be able to do it without the need of any C stub : the
other way to do it is then to run a modified interpreter where CAMLexport
have been replaced with CAMLprim in backtrace.c....

Nicolas Cannasse

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] getting stack traces in running code
  2003-12-03  3:19   ` Nicolas Cannasse
@ 2003-12-03 14:19     ` Damien Doligez
  2003-12-03 18:26     ` Issac Trotts
  1 sibling, 0 replies; 6+ messages in thread
From: Damien Doligez @ 2003-12-03 14:19 UTC (permalink / raw)
  To: caml-list

On Wednesday, December 3, 2003, at 04:19 AM, Nicolas Cannasse wrote:

> BTW, if the "print_exception_backtrace" of the caml sources was 
> exported as
> Primitive, we would be able to do it without the need of any C stub : 
> the
> other way to do it is then to run a modified interpreter where 
> CAMLexport
> have been replaced with CAMLprim in backtrace.c....

Don't do that.  print_exception_backtrace returns void, but a CAMLprim
must return a value.  You'll have to make it return Val_unit.

-- Damien

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] getting stack traces in running code
  2003-12-03  3:19   ` Nicolas Cannasse
  2003-12-03 14:19     ` Damien Doligez
@ 2003-12-03 18:26     ` Issac Trotts
  1 sibling, 0 replies; 6+ messages in thread
From: Issac Trotts @ 2003-12-03 18:26 UTC (permalink / raw)
  To: caml-list

On Wed, Dec 03, 2003 at 12:19:05PM +0900, Nicolas Cannasse wrote:
> > > Is there a reason why stack traces are available only on a crash?  I
> > > have a project (a distributed OpenPGP keyserver system,
> > > http://www.nongnu.org/sks/) that is a long-running daemon.  Unexpected
> > > errors are caught and logged, but unfortunately, there's no way of
> > > getting a stack-trace, since I don't let the exceptions kill the
> > > program.  This makes debugging much more difficult, and is one of the
> > > single largest difficulties I have with ocaml.  Is there a technical
> > > reason that a bytecode-compiled executable couldn't have access to the
> > > stack trace during execution?
> >
> > I have a partial solution:
> [...]
> > 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?
> 
> I think this is because you're adding an additional call to "print", that is
> writing on the stack (!). Since you can't write on the stack, you can't call
> any ML handler between catching and printing the exception.
> You should then have no backtrace.ml and only a mli with :
> 
> external print_stack_strace : unit -> unit = "internal_print"
> 
> the user can then print the exception using a Printexc... after printing the
> 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 -> unit = "internal_print"
 
==== backtrace_stubs.c ====
#include <caml/mlvalues.h>
 
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 -> ()
    | k ->
      begin try foo() with Failure _ as e -> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-12-04 22:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-02 20:50 [Caml-list] getting stack traces in running code Yaron M. Minsky
2003-12-02 21:45 ` Richard Jones
2003-12-02 22:26 ` Issac Trotts
2003-12-03  3:19   ` Nicolas Cannasse
2003-12-03 14:19     ` Damien Doligez
2003-12-03 18:26     ` Issac Trotts

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).