60749
|
1 |
(* Title: Pure/Tools/debugger.ML
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Interactive debugger for Isabelle/ML.
|
|
5 |
*)
|
|
6 |
|
60830
|
7 |
signature DEBUGGER =
|
|
8 |
sig
|
60834
|
9 |
val writeln_message: string -> unit
|
|
10 |
val warning_message: string -> unit
|
|
11 |
val error_message: string -> unit
|
60830
|
12 |
end;
|
|
13 |
|
|
14 |
structure Debugger: DEBUGGER =
|
60765
|
15 |
struct
|
|
16 |
|
60830
|
17 |
(* messages *)
|
|
18 |
|
60856
|
19 |
val _ = Session.protocol_handler "isabelle.Debugger$Handler";
|
|
20 |
|
60834
|
21 |
fun output_message kind msg =
|
60830
|
22 |
Output.protocol_message
|
60834
|
23 |
(Markup.debugger_output (Simple_Thread.the_name ()))
|
|
24 |
[Markup.markup (kind, Markup.serial_properties (serial ())) msg];
|
|
25 |
|
|
26 |
val writeln_message = output_message Markup.writelnN;
|
|
27 |
val warning_message = output_message Markup.warningN;
|
|
28 |
val error_message = output_message Markup.errorN;
|
60830
|
29 |
|
60856
|
30 |
fun error_wrapper e = e ()
|
|
31 |
handle exn =>
|
|
32 |
if Exn.is_interrupt exn then reraise exn
|
|
33 |
else error_message (Runtime.exn_message exn);
|
|
34 |
|
60830
|
35 |
|
60765
|
36 |
(* global state *)
|
|
37 |
|
|
38 |
datatype state =
|
|
39 |
State of {
|
60829
|
40 |
threads: Thread.thread Symtab.table, (*thread name ~> thread*)
|
|
41 |
input: string list Queue.T Symtab.table (*thread name ~> input queue*)
|
60765
|
42 |
};
|
|
43 |
|
|
44 |
fun make_state (threads, input) = State {threads = threads, input = input};
|
|
45 |
val init_state = make_state (Symtab.empty, Symtab.empty);
|
|
46 |
fun map_state f (State {threads, input}) = make_state (f (threads, input));
|
|
47 |
|
|
48 |
val global_state = Synchronized.var "Debugger.state" init_state;
|
|
49 |
|
60842
|
50 |
fun cancel thread_name =
|
60765
|
51 |
Synchronized.change global_state (tap (fn State {threads, ...} =>
|
60842
|
52 |
(case Symtab.lookup threads thread_name of
|
60765
|
53 |
NONE => ()
|
|
54 |
| SOME thread => Simple_Thread.interrupt_unsynchronized thread)));
|
|
55 |
|
60842
|
56 |
fun input thread_name msg =
|
60765
|
57 |
Synchronized.change global_state (map_state (fn (threads, input) =>
|
60842
|
58 |
let val input' = Symtab.map_default (thread_name, Queue.empty) (Queue.enqueue msg) input;
|
60765
|
59 |
in (threads, input') end));
|
|
60 |
|
60842
|
61 |
fun get_input thread_name =
|
60765
|
62 |
Synchronized.guarded_access global_state (fn State {threads, input} =>
|
60842
|
63 |
(case Symtab.lookup input thread_name of
|
60765
|
64 |
NONE => NONE
|
|
65 |
| SOME queue =>
|
|
66 |
let
|
|
67 |
val (msg, queue') = Queue.dequeue queue;
|
|
68 |
val input' =
|
60842
|
69 |
if Queue.is_empty queue' then Symtab.delete_safe thread_name input
|
|
70 |
else Symtab.update (thread_name, queue') input;
|
60765
|
71 |
in SOME (msg, make_state (threads, input')) end));
|
|
72 |
|
|
73 |
|
60856
|
74 |
(* thread state *)
|
|
75 |
|
|
76 |
local
|
|
77 |
val tag = Universal.tag () : ML_Debugger.state list Universal.tag;
|
|
78 |
in
|
60765
|
79 |
|
60856
|
80 |
fun get_debugging () = the_default [] (Thread.getLocal tag);
|
|
81 |
val is_debugging = not o null o get_debugging;
|
60765
|
82 |
|
60856
|
83 |
fun with_debugging e =
|
|
84 |
setmp_thread_data tag (get_debugging ()) (ML_Debugger.state (Thread.self ())) e ();
|
60765
|
85 |
|
60749
|
86 |
end;
|
|
87 |
|
60765
|
88 |
|
60856
|
89 |
(* eval ML *)
|
60765
|
90 |
|
60856
|
91 |
fun eval opt_index SML context expression = (* FIXME *)
|
|
92 |
writeln_message ("context = " ^ context ^ "\nexpression = " ^ expression);
|
60749
|
93 |
|
60765
|
94 |
|
|
95 |
(* main entry point *)
|
|
96 |
|
60842
|
97 |
fun debugger_state thread_name =
|
|
98 |
Output.protocol_message (Markup.debugger_state thread_name)
|
60856
|
99 |
[get_debugging ()
|
60842
|
100 |
|> map (fn st =>
|
|
101 |
(Position.properties_of (Exn_Properties.position_of (ML_Debugger.debug_location st)),
|
|
102 |
ML_Debugger.debug_function st))
|
|
103 |
|> let open XML.Encode in list (pair properties string) end
|
|
104 |
|> YXML.string_of_body];
|
|
105 |
|
|
106 |
fun debugger_loop thread_name =
|
60856
|
107 |
let
|
|
108 |
fun loop () =
|
|
109 |
(debugger_state thread_name;
|
|
110 |
case get_input thread_name of
|
|
111 |
["continue"] => ()
|
|
112 |
| ["eval", index, language, context, expression] =>
|
|
113 |
(error_wrapper (fn () =>
|
|
114 |
eval (try Markup.parse_int index) (language = "SML") context expression); loop ())
|
|
115 |
| bad =>
|
|
116 |
(Output.system_message
|
|
117 |
("Debugger: bad input " ^ ML_Syntax.print_list ML_Syntax.print_string bad);
|
|
118 |
loop ()));
|
|
119 |
in with_debugging loop; debugger_state thread_name end;
|
60765
|
120 |
|
|
121 |
fun debugger cond =
|
60856
|
122 |
if is_debugging () orelse not (cond ()) orelse
|
60765
|
123 |
not (Options.default_bool @{system_option ML_debugger_active})
|
|
124 |
then ()
|
|
125 |
else
|
60856
|
126 |
(case Simple_Thread.get_name () of
|
|
127 |
NONE => ()
|
|
128 |
| SOME thread_name => debugger_loop thread_name);
|
60765
|
129 |
|
|
130 |
fun init () =
|
|
131 |
ML_Debugger.on_breakpoint
|
|
132 |
(SOME (fn (_, b) =>
|
|
133 |
debugger (fn () => ! b orelse Options.default_bool @{system_option ML_debugger_stepping})));
|
|
134 |
|
|
135 |
|
|
136 |
(* protocol commands *)
|
|
137 |
|
|
138 |
val _ =
|
|
139 |
Isabelle_Process.protocol_command "Debugger.init"
|
|
140 |
(fn [] => init ());
|
|
141 |
|
|
142 |
val _ =
|
|
143 |
Isabelle_Process.protocol_command "Debugger.cancel"
|
60842
|
144 |
(fn [thread_name] => cancel thread_name);
|
60765
|
145 |
|
|
146 |
val _ =
|
|
147 |
Isabelle_Process.protocol_command "Debugger.input"
|
60842
|
148 |
(fn thread_name :: msg => input thread_name msg);
|
60765
|
149 |
|
60749
|
150 |
end;
|