1995-09-14 04:53:55 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-09-14 04:53:55 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-09-14 04:53:55 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* The "trace" facility *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-09-14 04:53:55 -07:00
|
|
|
open Misc
|
|
|
|
open Longident
|
1996-09-23 04:32:19 -07:00
|
|
|
open Types
|
1995-09-14 04:53:55 -07:00
|
|
|
open Toploop
|
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
type codeptr = Obj.t
|
|
|
|
|
1995-09-14 04:53:55 -07:00
|
|
|
type traced_function =
|
1998-09-07 00:55:38 -07:00
|
|
|
{ path: Path.t; (* Name under which it is traced *)
|
|
|
|
closure: Obj.t; (* Its function closure (patched) *)
|
|
|
|
actual_code: codeptr; (* Its original code pointer *)
|
|
|
|
instrumented_fun: codeptr -> Obj.t -> Obj.t -> Obj.t }
|
|
|
|
(* Printing function *)
|
1995-09-14 04:53:55 -07:00
|
|
|
|
|
|
|
let traced_functions = ref ([] : traced_function list)
|
|
|
|
|
1995-10-17 03:02:02 -07:00
|
|
|
(* Check if a function is already traced *)
|
|
|
|
|
1996-04-29 06:24:01 -07:00
|
|
|
let is_traced clos =
|
1995-10-17 03:02:02 -07:00
|
|
|
let rec is_traced = function
|
1996-04-29 06:24:01 -07:00
|
|
|
[] -> None
|
|
|
|
| tf :: rem -> if tf.closure == clos then Some tf.path else is_traced rem
|
1995-10-17 03:02:02 -07:00
|
|
|
in is_traced !traced_functions
|
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
(* Get or overwrite the code pointer of a closure *)
|
|
|
|
|
|
|
|
let get_code_pointer cls = Obj.field cls 0
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
let set_code_pointer cls ptr = Obj.set_field cls 0 ptr
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
(* Call a traced function (use old code pointer, but new closure as
|
|
|
|
environment so that recursive calls are also traced).
|
|
|
|
It is necessary to wrap Meta.invoke_traced_function in an ML function
|
|
|
|
so that the RETURN at the end of the ML wrapper takes us to the
|
|
|
|
code of the function. *)
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
let invoke_traced_function codeptr env arg =
|
|
|
|
Meta.invoke_traced_function codeptr env arg
|
1995-09-14 04:53:55 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_label ppf l = if l <> "" then fprintf ppf "%s:" l
|
1999-11-30 08:07:38 -08:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
(* If a function returns a functional value, wrap it into a trace code *)
|
1995-09-14 04:53:55 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let rec instrument_result env name ppf clos_typ =
|
1997-03-07 14:43:27 -08:00
|
|
|
match (Ctype.repr(Ctype.expand_head env clos_typ)).desc with
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow(l, t1, t2, _) ->
|
1995-09-14 04:53:55 -07:00
|
|
|
let starred_name =
|
|
|
|
match name with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Lident s -> Lident(s ^ "*")
|
1995-09-14 04:53:55 -07:00
|
|
|
| Ldot(lid, s) -> Ldot(lid, s ^ "*")
|
1998-09-07 00:55:38 -07:00
|
|
|
| Lapply(l1, l2) -> fatal_error "Trace.instrument_result" in
|
2000-03-06 14:12:09 -08:00
|
|
|
let trace_res = instrument_result env starred_name ppf t2 in
|
1995-09-14 04:53:55 -07:00
|
|
|
(fun clos_val ->
|
1998-09-07 00:55:38 -07:00
|
|
|
Obj.repr (fun arg ->
|
2001-02-12 06:32:38 -08:00
|
|
|
if not !may_trace then
|
|
|
|
(Obj.magic clos_val : Obj.t -> Obj.t) arg
|
|
|
|
else begin
|
|
|
|
may_trace := false;
|
|
|
|
try
|
|
|
|
fprintf ppf "@[<2>%a <--@ %a%a@]@."
|
|
|
|
Printtyp.longident starred_name
|
|
|
|
print_label l
|
|
|
|
(print_value !toplevel_env arg) t1;
|
|
|
|
may_trace := true;
|
|
|
|
let res = (Obj.magic clos_val : Obj.t -> Obj.t) arg in
|
|
|
|
may_trace := false;
|
|
|
|
fprintf ppf "@[<2>%a -->@ %a@]@."
|
|
|
|
Printtyp.longident starred_name
|
|
|
|
(print_value !toplevel_env res) t2;
|
|
|
|
may_trace := true;
|
|
|
|
trace_res res
|
|
|
|
with exn ->
|
|
|
|
may_trace := false;
|
|
|
|
fprintf ppf "@[<2>%a raises@ %a@]@."
|
|
|
|
Printtyp.longident starred_name
|
|
|
|
(print_value !toplevel_env (Obj.repr exn)) Predef.type_exn;
|
|
|
|
may_trace := true;
|
|
|
|
raise exn
|
|
|
|
end))
|
1998-09-07 00:55:38 -07:00
|
|
|
| _ -> (fun v -> v)
|
|
|
|
|
|
|
|
(* Same as instrument_result, but for a toplevel closure (modified in place) *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let instrument_closure env name ppf clos_typ =
|
1998-09-07 00:55:38 -07:00
|
|
|
match (Ctype.repr(Ctype.expand_head env clos_typ)).desc with
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow(l, t1, t2, _) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let trace_res = instrument_result env name ppf t2 in
|
1998-09-07 00:55:38 -07:00
|
|
|
(fun actual_code closure arg ->
|
2001-02-13 00:33:20 -08:00
|
|
|
if not !may_trace then begin
|
|
|
|
let res = invoke_traced_function actual_code closure arg
|
|
|
|
in res (* do not remove let, prevents tail-call to invoke_traced_ *)
|
|
|
|
end else begin
|
2001-02-12 06:32:38 -08:00
|
|
|
may_trace := false;
|
|
|
|
try
|
|
|
|
fprintf ppf "@[<2>%a <--@ %a%a@]@."
|
|
|
|
Printtyp.longident name
|
|
|
|
print_label l
|
|
|
|
(print_value !toplevel_env arg) t1;
|
|
|
|
may_trace := true;
|
|
|
|
let res = invoke_traced_function actual_code closure arg in
|
|
|
|
may_trace := false;
|
|
|
|
fprintf ppf "@[<2>%a -->@ %a@]@."
|
|
|
|
Printtyp.longident name
|
|
|
|
(print_value !toplevel_env res) t2;
|
|
|
|
may_trace := true;
|
|
|
|
trace_res res
|
|
|
|
with exn ->
|
|
|
|
may_trace := false;
|
|
|
|
fprintf ppf "@[<2>%a raises@ %a@]@."
|
|
|
|
Printtyp.longident name
|
|
|
|
(print_value !toplevel_env (Obj.repr exn)) Predef.type_exn;
|
|
|
|
may_trace := true;
|
|
|
|
raise exn
|
|
|
|
end)
|
1998-09-07 00:55:38 -07:00
|
|
|
| _ -> assert false
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
(* Given the address of a closure, find its tracing info *)
|
1995-09-14 04:53:55 -07:00
|
|
|
|
|
|
|
let rec find_traced_closure clos = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| [] -> fatal_error "Trace.find_traced_closure"
|
1998-09-07 00:55:38 -07:00
|
|
|
| f :: rem -> if f.closure == clos then f else find_traced_closure clos rem
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
(* Trace the application of an (instrumented) closure to an argument *)
|
1995-09-14 04:53:55 -07:00
|
|
|
|
1998-09-07 00:55:38 -07:00
|
|
|
let print_trace clos arg =
|
|
|
|
let f = find_traced_closure clos !traced_functions in
|
|
|
|
f.instrumented_fun f.actual_code clos arg
|