testsuite/tests/backtrace: new test to exercize Printexc.get_raw_backtrace

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13812 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Gabriel Scherer 2013-06-19 14:10:27 +00:00
parent 75ee032332
commit b06015bacb
3 changed files with 80 additions and 1 deletions

View File

@ -14,7 +14,7 @@ BASEDIR=../..
EXECNAME=program$(EXE)
ABCDFILES=backtrace.ml
OTHERFILES=backtrace2.ml
OTHERFILES=backtrace2.ml raw_backtrace.ml
default:
$(MAKE) byte

View File

@ -0,0 +1,52 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Gallium, INRIA Rocquencourt *)
(* *)
(* Copyright 2008 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the Q Public License version 1.0. *)
(* *)
(***********************************************************************)
(* A test for stack backtraces *)
exception Error of string
let rec f msg n =
if n = 0 then raise(Error msg) else 1 + f msg (n-1)
let g msg =
try
f msg 5
with Error "a" -> print_string "a"; print_newline(); 0
| Error "b" as exn -> print_string "b"; print_newline(); raise exn
| Error "c" -> raise (Error "c")
let backtrace args =
try
ignore (g args.(0)); None
with exn ->
let exn = Printexc.to_string exn in
let trace = Printexc.get_raw_backtrace () in
Some (exn, trace)
let run args =
match backtrace args with
| None -> print_string "No exception\n"
| Some (exn, trace) ->
begin
(* raise another exception to stash the global backtrace *)
try ignore (f "c" 5); assert false with Error _ -> ();
end;
Printf.printf "Uncaught exception %s\n" exn;
Printexc.print_raw_backtrace stdout trace
let _ =
Printexc.record_backtrace true;
run [| "a" |];
run [| "b" |];
run [| "c" |];
run [| "d" |];
run [| |]

View File

@ -0,0 +1,27 @@
a
No exception
b
Uncaught exception Raw_backtrace.Error("b")
Raised at file "raw_backtrace.ml", line 18, characters 21-32
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 22, characters 4-11
Re-raised at file "raw_backtrace.ml", line 24, characters 68-71
Called from file "raw_backtrace.ml", line 29, characters 11-23
Uncaught exception Raw_backtrace.Error("c")
Raised at file "raw_backtrace.ml", line 25, characters 26-37
Called from file "raw_backtrace.ml", line 29, characters 11-23
Uncaught exception Raw_backtrace.Error("d")
Raised at file "raw_backtrace.ml", line 18, characters 21-32
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 18, characters 42-53
Called from file "raw_backtrace.ml", line 22, characters 4-11
Called from file "raw_backtrace.ml", line 29, characters 11-23
Uncaught exception Invalid_argument("index out of bounds")
Raised by primitive operation at file "raw_backtrace.ml", line 29, characters 14-22