caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Seeking exception source
@ 2005-11-08  2:13 skaller
  2005-11-08  2:57 ` [Caml-list] " Jon Harrop
  2005-11-08  9:06 ` Nicolas Cannasse
  0 siblings, 2 replies; 6+ messages in thread
From: skaller @ 2005-11-08  2:13 UTC (permalink / raw)
  To: caml-list

I am occasionally annoyed by Not_found propagating to my
top level function .. meaning the error could be anywhere
at all in my program.

I usually do this rubbish to fix it:

 ...... e ...... (* where e might raise an exception *)

  ==>

.... begin try e 
 with Not_found -> failwith "Not_found at So and So" end ...

and then litter the code with such things until I've found
where the problem is. This is very bad.

A better way? Something like this:

let f x ... = ....

===>

let f x ... = begin try
   ...... 
with (x:exn) -> wrap "in function f" x end

where 

let wrap s x =  raise (TaggedExn ( s,x))

The idea is that every exception continues to propagate
as normal .. but it gets a tag of where it came from
wrapped around it. The handler can then report it.

This mechanism is no use in a recursive function, nor where
some exception can legitimately escape .. however both these
could be taken into account: the general idea is to add
a symbolic stack backtrace to exceptions which represent
errors that WILL propagate to the top level (ie. underflow
the stack).

Any ideas how to do that? Perhaps a camlp4 thing to instrument
marked functions? Although it isn't clear catching exceptions
costs anything if none are thrown (other than code bloat)?

[Actually in C the macro __LINE__ solves this .. but caml
doesn't have such macros ..]

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


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

* Re: [Caml-list] Seeking exception source
  2005-11-08  2:13 Seeking exception source skaller
@ 2005-11-08  2:57 ` Jon Harrop
  2005-11-08  9:06 ` Nicolas Cannasse
  1 sibling, 0 replies; 6+ messages in thread
From: Jon Harrop @ 2005-11-08  2:57 UTC (permalink / raw)
  To: caml-list


Have you tried that static analyser for exceptions?

On Tuesday 08 November 2005 02:13, skaller wrote:
> Any ideas how to do that? Perhaps a camlp4 thing to instrument
> marked functions?

If you can do this then it could also be used to implement my allocation 
profiling idea. I'm still learning camlp4 (I'll put up a web page on what 
I've done ASAP) and I'm not up to that yet, but I think it is entirely 
feasible. One problem would be the existence of many functions with the same 
name.

> Although it isn't clear catching exceptions costs anything if none are
> thrown (other than code bloat)?

Non-tail recursion?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


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

* Re: [Caml-list] Seeking exception source
  2005-11-08  2:13 Seeking exception source skaller
  2005-11-08  2:57 ` [Caml-list] " Jon Harrop
@ 2005-11-08  9:06 ` Nicolas Cannasse
  2005-11-08 10:30   ` Alessandro Baretta
  1 sibling, 1 reply; 6+ messages in thread
From: Nicolas Cannasse @ 2005-11-08  9:06 UTC (permalink / raw)
  To: skaller, caml-list

>I am occasionally annoyed by Not_found propagating to my
> top level function .. meaning the error could be anywhere
> at all in my program.

At that time the best is to run your program in bytecode with debug infos 
using ocamlrun -b so you'll get full backtrace informations. It would be 
better for such debug informations (not all the types but only the 
bytecode -> source positions) to be included by default, and always have 
backtrace turned on.

Nicolas Cannasse 


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

* Re: [Caml-list] Seeking exception source
  2005-11-08 10:30   ` Alessandro Baretta
@ 2005-11-08 10:06     ` Nicolas Cannasse
  2005-11-08 10:34       ` skaller
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Cannasse @ 2005-11-08 10:06 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: skaller, caml-list

>>> I am occasionally annoyed by Not_found propagating to my
>>> top level function .. meaning the error could be anywhere
>>> at all in my program.
>>
>>
>> At that time the best is to run your program in bytecode with debug infos 
>> using ocamlrun -b so you'll get full backtrace informations. It would be 
>> better for such debug informations (not all the types but only the 
>> bytecode -> source positions) to be included by default, and always have 
>> backtrace turned on.
>
> In many cases this is completely inadequate. The AS/Xcaml application 
> server, for example, cannot terminate on an uncaught exception in a 
> program module. The main loop must catch the exception and do its best to 
> report it, without aborting the execution of the server. This means that I 
> need to get the backtrace within Ocaml code by applying some kind of 
> function to the exception object. I tried to write a C function providing 
> this kind of information, but I never managed to get it to work.
>
> Alex

Yes I agree, this is not all the time easy to get exceptions backtraces 
since it's only possible for uncaught ones. At some time I managed to modify 
the Ocaml interpreter to make it work, by exporting some C inner interpreter 
functions but I'm not sure it was a correct hack.

On the NekoVM which some design points are inspired by the OCaml VM, I 
recently added exception stack traces. In fact, I was thinking that this 
will cost more performances than it actualy did. The impact on the 
bootstrapping time of NekoML was quite low. It's true that the compiler 
being written in an ML language, quite often exceptions are used to control 
the flow of the program (Not_found , Eof and Exit among others). But since 
in general the position of catching is just a few calls away from the throw, 
you don't have to unwind a lot of debug informations.

It is also useful in that case to have a "reraised" semantic that will keep 
the stack when an execption is filtered. But OCaml have that. Having full 
exceptions backtraces plus call stack traces available at anytime is a big 
plus when working on the NekoML compiler. The tagged union printers 
automaticaly generated and carried with instances are helping a lot too, 
since you can "print" polymorphicaly any runtime value. I think this is very 
small things that would greatly ease the everyday ocaml programmer life.

Nicolas Cannasse 


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

* Re: [Caml-list] Seeking exception source
  2005-11-08  9:06 ` Nicolas Cannasse
@ 2005-11-08 10:30   ` Alessandro Baretta
  2005-11-08 10:06     ` Nicolas Cannasse
  0 siblings, 1 reply; 6+ messages in thread
From: Alessandro Baretta @ 2005-11-08 10:30 UTC (permalink / raw)
  To: Nicolas Cannasse; +Cc: skaller, caml-list

Nicolas Cannasse wrote:
>> I am occasionally annoyed by Not_found propagating to my
>> top level function .. meaning the error could be anywhere
>> at all in my program.
> 
> 
> At that time the best is to run your program in bytecode with debug 
> infos using ocamlrun -b so you'll get full backtrace informations. It 
> would be better for such debug informations (not all the types but only 
> the bytecode -> source positions) to be included by default, and always 
> have backtrace turned on.

In many cases this is completely inadequate. The AS/Xcaml application 
server, for example, cannot terminate on an uncaught exception in a 
program module. The main loop must catch the exception and do its best 
to report it, without aborting the execution of the server. This means 
that I need to get the backtrace within Ocaml code by applying some kind 
of function to the exception object. I tried to write a C function 
providing this kind of information, but I never managed to get it to work.

Alex


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

* Re: [Caml-list] Seeking exception source
  2005-11-08 10:06     ` Nicolas Cannasse
@ 2005-11-08 10:34       ` skaller
  0 siblings, 0 replies; 6+ messages in thread
From: skaller @ 2005-11-08 10:34 UTC (permalink / raw)
  To: Nicolas Cannasse; +Cc: Alessandro Baretta, caml-list

On Tue, 2005-11-08 at 19:06 +0900, Nicolas Cannasse wrote:
> >>> I am occasionally annoyed by Not_found propagating to my
> >>> top level function .. meaning the error could be anywhere
> >>> at all in my program.
> >>
> >>
> >> At that time the best is to run your program in bytecode with debug infos 
> >> using ocamlrun -b so you'll get full backtrace informations. 

> >
> > In many cases this is completely inadequate. The AS/Xcaml application 
> > server, for example, cannot terminate on an uncaught exception in a 
> > program module. 

> Yes I agree, this is not all the time easy to get exceptions backtraces 

However all of this misses what I seek, which is a way
to *manually* add backtrace information.

In addition to the annoying errors leading to Not_found
exceptions, there are other exceptions which are supposed
to be caught and sometimes leak, and in addition, there
are exceptions which are thrown when there are USER errors,
and these are caught in the top level and reported.
That's where I want the backtrace .. I want to unwind
it manually.



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


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

end of thread, other threads:[~2005-11-08 10:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-08  2:13 Seeking exception source skaller
2005-11-08  2:57 ` [Caml-list] " Jon Harrop
2005-11-08  9:06 ` Nicolas Cannasse
2005-11-08 10:30   ` Alessandro Baretta
2005-11-08 10:06     ` Nicolas Cannasse
2005-11-08 10:34       ` skaller

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