|
| 1 | +(**************************************************************************) |
| 2 | +(* *) |
| 3 | +(* OCaml *) |
| 4 | +(* *) |
| 5 | +(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) |
| 6 | +(* *) |
| 7 | +(* Copyright 1996 Institut National de Recherche en Informatique et *) |
| 8 | +(* en Automatique. *) |
| 9 | +(* *) |
| 10 | +(* All rights reserved. This file is distributed under the terms of *) |
| 11 | +(* the GNU Lesser General Public License version 2.1, with the *) |
| 12 | +(* special exception on linking described in the file LICENSE. *) |
| 13 | +(* *) |
| 14 | +(**************************************************************************) |
| 15 | + |
| 16 | + |
| 17 | +(** The trace for OCaml 4.13.0 or above. |
| 18 | +
|
| 19 | + [#trace] directive implemented at |
| 20 | +
|
| 21 | + - [toplevel/topdirs.ml] on 4.12.0-, and |
| 22 | + - [toplevel/byte/trace.ml] and [toplevel/byte/topmain.ml] on 4.13.0+. |
| 23 | +
|
| 24 | + This file is a part of [toplevel/byte/topmain.ml], migrated for |
| 25 | + ocaml-jupyter. *) |
| 26 | + |
| 27 | +open Types |
| 28 | +open Trace |
| 29 | +open Toploop |
| 30 | + |
| 31 | +external current_environment: unit -> Obj.t = "caml_get_current_environment" |
| 32 | + |
| 33 | +let tracing_function_ptr = |
| 34 | + get_code_pointer |
| 35 | + (Obj.repr (fun arg -> Trace.print_trace (current_environment()) arg)) |
| 36 | + |
| 37 | +let dir_trace ppf lid = |
| 38 | + match Env.find_value_by_name lid !toplevel_env with |
| 39 | + | (path, desc) -> begin |
| 40 | + (* Check if this is a primitive *) |
| 41 | + match desc.Types.val_kind with |
| 42 | + | Types.Val_prim _ -> |
| 43 | + Format.fprintf ppf |
| 44 | + "%a is an external function and cannot be traced.@." |
| 45 | + Printtyp.longident lid |
| 46 | + | _ -> |
| 47 | + let clos = Toploop.eval_value_path !toplevel_env path in |
| 48 | + (* Nothing to do if it's not a closure *) |
| 49 | + if Obj.is_block clos |
| 50 | + && (Obj.tag clos = Obj.closure_tag || Obj.tag clos = Obj.infix_tag) |
| 51 | + && (match |
| 52 | + Compat.types_get_desc |
| 53 | + (Ctype.expand_head !toplevel_env desc.val_type) |
| 54 | + with Tarrow _ -> true | _ -> false) |
| 55 | + then begin |
| 56 | + match is_traced clos with |
| 57 | + | Some opath -> |
| 58 | + Format.fprintf ppf "%a is already traced (under the name %a).@." |
| 59 | + Printtyp.path path |
| 60 | + Printtyp.path opath |
| 61 | + | None -> |
| 62 | + (* Instrument the old closure *) |
| 63 | + traced_functions := |
| 64 | + { path = path; |
| 65 | + closure = clos; |
| 66 | + actual_code = get_code_pointer clos; |
| 67 | + instrumented_fun = |
| 68 | + instrument_closure |
| 69 | + !toplevel_env lid ppf desc.val_type } |
| 70 | + :: !traced_functions; |
| 71 | + (* Redirect the code field of the closure to point |
| 72 | + to the instrumentation function *) |
| 73 | + set_code_pointer clos tracing_function_ptr; |
| 74 | + Format.fprintf ppf "%a is now traced.@." Printtyp.longident lid |
| 75 | + end else |
| 76 | + Format.fprintf ppf "%a is not a function.@." Printtyp.longident lid |
| 77 | + end |
| 78 | + | exception Not_found -> |
| 79 | + Format.fprintf ppf "Unbound value %a.@." Printtyp.longident lid |
| 80 | + |
| 81 | +let dir_untrace ppf lid = |
| 82 | + match Env.find_value_by_name lid !toplevel_env with |
| 83 | + | (path, _desc) -> |
| 84 | + let rec remove = function |
| 85 | + | [] -> |
| 86 | + Format.fprintf ppf "%a was not traced.@." Printtyp.longident lid; |
| 87 | + [] |
| 88 | + | f :: rem -> |
| 89 | + if Path.same f.path path then begin |
| 90 | + set_code_pointer f.closure f.actual_code; |
| 91 | + Format.fprintf ppf "%a is no longer traced.@." |
| 92 | + Printtyp.longident lid; |
| 93 | + rem |
| 94 | + end else f :: remove rem in |
| 95 | + traced_functions := remove !traced_functions |
| 96 | + | exception Not_found -> |
| 97 | + Format.fprintf ppf "Unbound value %a.@." Printtyp.longident lid |
| 98 | + |
| 99 | +let dir_untrace_all ppf () = |
| 100 | + List.iter |
| 101 | + (fun f -> |
| 102 | + set_code_pointer f.closure f.actual_code; |
| 103 | + Format.fprintf ppf "%a is no longer traced.@." Printtyp.path f.path) |
| 104 | + !traced_functions; |
| 105 | + traced_functions := [] |
| 106 | + |
| 107 | +let add_directives ppf = |
| 108 | + let _ = add_directive "trace" |
| 109 | + (Directive_ident (dir_trace ppf)) |
| 110 | + { |
| 111 | + section = Compat.section_trace; |
| 112 | + doc = "All calls to the function \ |
| 113 | + named function-name will be traced."; |
| 114 | + } in |
| 115 | + |
| 116 | + let _ = add_directive "untrace" |
| 117 | + (Directive_ident (dir_untrace ppf)) |
| 118 | + { |
| 119 | + section = Compat.section_trace; |
| 120 | + doc = "Stop tracing the given function."; |
| 121 | + } in |
| 122 | + |
| 123 | + let _ = add_directive "untrace_all" |
| 124 | + (Directive_none (dir_untrace_all ppf)) |
| 125 | + { |
| 126 | + section = Compat.section_trace; |
| 127 | + doc = "Stop tracing all functions traced so far."; |
| 128 | + } in |
| 129 | + () |
0 commit comments