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 |
|
60858
|
88 |
fun the_debug_state thread_name index =
|
|
89 |
(case get_debugging () of
|
|
90 |
[] => error ("Missing debugger information for thread " ^ quote thread_name)
|
|
91 |
| states =>
|
|
92 |
if index < 0 orelse index >= length states then
|
|
93 |
error ("Bad debugger stack index " ^ signed_string_of_int index ^ " for thread " ^
|
|
94 |
quote thread_name)
|
|
95 |
else nth states index);
|
|
96 |
|
|
97 |
|
60765
|
98 |
|
60856
|
99 |
(* eval ML *)
|
60765
|
100 |
|
60858
|
101 |
fun eval thread_name index SML txt1 txt2 =
|
|
102 |
let
|
|
103 |
fun evaluate verbose =
|
|
104 |
ML_Context.eval
|
|
105 |
{SML = SML, exchange = false, redirect = false, verbose = verbose,
|
|
106 |
writeln = writeln_message, warning = warning_message}
|
|
107 |
Position.none;
|
|
108 |
|
|
109 |
val debug_state = the_debug_state thread_name index;
|
|
110 |
val context1 = ML_Context.the_generic_context ()
|
|
111 |
|> Context_Position.set_visible_generic false
|
|
112 |
|> ML_Env.add_name_space {SML = SML} (ML_Debugger.debug_name_space debug_state);
|
|
113 |
val (toks1, toks2) = apply2 (ML_Lex.read_source SML o Input.string) (txt1, txt2);
|
|
114 |
in
|
|
115 |
if null (filter_out (fn Antiquote.Text tok => ML_Lex.is_improper tok | _ => false) toks1)
|
|
116 |
then Context.setmp_thread_data (SOME context1) (fn () => evaluate true toks2) ()
|
|
117 |
else error "FIXME"
|
|
118 |
end;
|
60749
|
119 |
|
60765
|
120 |
|
60857
|
121 |
(* debugger entry point *)
|
|
122 |
|
|
123 |
local
|
60765
|
124 |
|
60842
|
125 |
fun debugger_state thread_name =
|
|
126 |
Output.protocol_message (Markup.debugger_state thread_name)
|
60856
|
127 |
[get_debugging ()
|
60842
|
128 |
|> map (fn st =>
|
|
129 |
(Position.properties_of (Exn_Properties.position_of (ML_Debugger.debug_location st)),
|
|
130 |
ML_Debugger.debug_function st))
|
|
131 |
|> let open XML.Encode in list (pair properties string) end
|
|
132 |
|> YXML.string_of_body];
|
|
133 |
|
60857
|
134 |
fun debugger_command thread_name =
|
|
135 |
(case get_input thread_name of
|
|
136 |
["continue"] => false
|
|
137 |
| ["eval", index, SML, txt1, txt2] =>
|
|
138 |
(error_wrapper (fn () =>
|
|
139 |
eval thread_name (Markup.parse_int index) (Markup.parse_bool SML) txt1 txt2); true)
|
|
140 |
| bad =>
|
|
141 |
(Output.system_message
|
|
142 |
("Debugger: bad input " ^ ML_Syntax.print_list ML_Syntax.print_string bad); true));
|
|
143 |
|
60842
|
144 |
fun debugger_loop thread_name =
|
60856
|
145 |
let
|
|
146 |
fun loop () =
|
60857
|
147 |
(debugger_state thread_name; if debugger_command thread_name then loop () else ());
|
60856
|
148 |
in with_debugging loop; debugger_state thread_name end;
|
60765
|
149 |
|
|
150 |
fun debugger cond =
|
60856
|
151 |
if is_debugging () orelse not (cond ()) orelse
|
60765
|
152 |
not (Options.default_bool @{system_option ML_debugger_active})
|
|
153 |
then ()
|
|
154 |
else
|
60856
|
155 |
(case Simple_Thread.get_name () of
|
|
156 |
NONE => ()
|
|
157 |
| SOME thread_name => debugger_loop thread_name);
|
60765
|
158 |
|
60857
|
159 |
in
|
|
160 |
|
60765
|
161 |
fun init () =
|
|
162 |
ML_Debugger.on_breakpoint
|
|
163 |
(SOME (fn (_, b) =>
|
|
164 |
debugger (fn () => ! b orelse Options.default_bool @{system_option ML_debugger_stepping})));
|
|
165 |
|
60857
|
166 |
end;
|
|
167 |
|
60765
|
168 |
|
|
169 |
(* protocol commands *)
|
|
170 |
|
|
171 |
val _ =
|
|
172 |
Isabelle_Process.protocol_command "Debugger.init"
|
|
173 |
(fn [] => init ());
|
|
174 |
|
|
175 |
val _ =
|
|
176 |
Isabelle_Process.protocol_command "Debugger.cancel"
|
60842
|
177 |
(fn [thread_name] => cancel thread_name);
|
60765
|
178 |
|
|
179 |
val _ =
|
|
180 |
Isabelle_Process.protocol_command "Debugger.input"
|
60842
|
181 |
(fn thread_name :: msg => input thread_name msg);
|
60765
|
182 |
|
60749
|
183 |
end;
|