English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
how to set breakpoint at exception throw?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-07-05 (21:24)
From: Gerd Stolpmann <gerd@g...>
Subject: Re: how to set breakpoint at exception throw?
On Tue, 04 Jul 2000, John Max Skaller wrote:
>> On Sat, 1 Jul 2000, Gerd Stolpmann wrote:
>> > Nevertheless, I have a wish: At least for programs compiled with the bytecode
>> > compiler, an automatic backtrace of uncaught exceptions would be often helpful
>> > (i.e I get a list where the last uncaught exception was raised, and all
>> > locations where it is re-raised in the "with" clause of the "try" statement).
>> > First, this type of error is very frequent, and it would save much time if the
>> > location where the problem occurs were output from the failing program itself.
>> > Second, under some environments it is difficult to run the debugger; not every
>> > program is run from the command-line (CGI programs, for example). Furthermore,
>> > such a feature would help bug searching in production environments: on such
>> > machines, there is usually no debugger installed, and it is sometimes difficult
>> > to reconstruct the failing data case.
>Hmmmm. I don't know how to do this in ocaml, but in C++ and Python it is
>you create an exception one of whose components is the caught exception,
>and throw it, thus preserving the whole exception chain. That is, you
>can do
>this in user space without special system support.

In general, you are right; the problem is that I do not have access to the
location in the source code where the exception happened. I'm interested in the
line number and the file name of the statement that originally caused the
exception. Example:

    raise Not_found
    x ->
      (* XXX: Clean-up code here *)
      raise x
  x ->
    (* XXX: Clean-up code here *)
    raise x

which would cause the following backtrace:

Uncaught exception: Not_found
Exception backtrace:
- File x, line 3: Not_found
- File x, line 7: Not_found
- File x, line 11: Not_found

One can imagine having a user space "raise" function:

let exception_list := ref []

let alt_raise exc =
  exception_list := (caller.file_name, caller.line_number, exc) ::
  raise exc

Problems: First, there is no "caller" information available without additional
help of the compiler. Second, I want to reset exception_list whenever the "with"
clause returns a regular result. (The latter is a simplification: only the last
uninterrupted chain of exceptions is reported. I think this is sufficient for

My proposal is to modify the bytecode compiler/interpreter such that every
"raise" bytecode statement has access to the location of the exception, and
that (optionally) the bytecode interpreter adds the exception to the
exception_list, or resets this list (this would require an additional bytecode
statement). This would not change the representation of exceptions, and it
would cost minimal extra execution time (the exception_list could be represented
as array with an upper limit).

I do not suggest to modify the native compiler in the same way, because it
would cost (relatively) much more time than for the bytecode system.

Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de