caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: xclerc <xavier.clerc@inria.fr>
To: Ashish Agarwal <agarwal1975@gmail.com>
Cc: xclerc <xavier.clerc@inria.fr>, Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] printing an exception changes printed backtrace
Date: Mon, 17 Jan 2011 16:37:33 +0100	[thread overview]
Message-ID: <0AA39519-6C34-4620-9CFA-A99A3EC217CA@inria.fr> (raw)
In-Reply-To: <AANLkTikznXV-ORo1P=ar49kAYnq7_nKygJvS--z4r7kY@mail.gmail.com>


Le 14 janv. 2011 à 22:15, Ashish Agarwal a écrit :

> I observe the following unexpected behavior:
> 
> --- a.ml ---
> try
>   … some code ...
> with e ->
> (*  Printf.eprintf "%s\n" (Printexc.to_string e); *)
>   Printexc.print_backtrace Legacy.stderr
> ------
> 
> Compiling as ocamlopt -g a.ml gives:
> $ ./a.out
> Raised at file "fool.ml", line 162, characters 13-22
> Called from file "bar.ml", line 13, characters 5-56
> Called from file "bar.ml", line 43, characters 4-63
> 
> Now this doesn't say which exception was raised, so I uncomment the commented line. I'm expecting the identical output, but with the exception printed first. However, I get:
> 
> $ ./a.out
> Not_found
> Raised at file "hashtbl.ml", line 93, characters 19-28
> 
> So it appears that printing an exception changes the backtrace. Is that possible?
> 
> Unfortunately, I cannot reproduce this behavior in a minimal example. The following script behaves as expected:

It seems possible to reproduce the problem with the following source code:


let custom_printer = true

let () =
  if custom_printer then
    Printexc.register_printer
      (function
        | Not_found -> failwith "silently"
        | _ -> None)

let rec f () =
  ignore (raise Not_found);
  f ()

let () =
  try
    f ()
  with e ->
    Printf.eprintf "%s\n%!" (Printexc.to_string e);
    Printexc.print_backtrace stderr



You will get different backtraces according to the value of "custom_printer".
The underlying problem is that "Printexc.print_backtrace" prints the backtrace
for the last exception raised... which can be one raised by "Printf.eprintf" or
"Printexc.to_string", but locally caught and not exposed to the outside world.

In the code sample above, an exception is raised by the register custom
printer. By design of "Printexc.to_string", such exceptions are silently
ignored... but change the value of the last raised exception (and thus its
associated backtrace).

Your experience of the backtrace actually vanishing may be an instance of
the same problem, where it just happens that the code for the custom printer
has not been compiled with the "-g" option.


Hope this helps,

Xavier Clerc



  parent reply	other threads:[~2011-01-17 15:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-14 21:15 Ashish Agarwal
2011-01-14 21:59 ` Daniel Bünzli
2011-01-14 22:05   ` Ashish Agarwal
2011-01-17 15:37 ` xclerc [this message]
2011-01-17 16:41   ` Ashish Agarwal
2011-01-17 17:02     ` Xavier Clerc
2011-01-17 17:12       ` Ashish Agarwal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0AA39519-6C34-4620-9CFA-A99A3EC217CA@inria.fr \
    --to=xavier.clerc@inria.fr \
    --cc=agarwal1975@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).