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
|
|
9 |
val output: string -> unit
|
|
10 |
end;
|
|
11 |
|
|
12 |
structure Debugger: DEBUGGER =
|
60765
|
13 |
struct
|
|
14 |
|
60830
|
15 |
(* messages *)
|
|
16 |
|
|
17 |
fun output msg =
|
|
18 |
Output.protocol_message
|
|
19 |
(Markup.debugger_output (Simple_Thread.the_name ()) (serial ())) [msg];
|
|
20 |
|
|
21 |
|
60765
|
22 |
(* global state *)
|
|
23 |
|
|
24 |
datatype state =
|
|
25 |
State of {
|
60829
|
26 |
threads: Thread.thread Symtab.table, (*thread name ~> thread*)
|
|
27 |
input: string list Queue.T Symtab.table (*thread name ~> input queue*)
|
60765
|
28 |
};
|
|
29 |
|
|
30 |
fun make_state (threads, input) = State {threads = threads, input = input};
|
|
31 |
val init_state = make_state (Symtab.empty, Symtab.empty);
|
|
32 |
fun map_state f (State {threads, input}) = make_state (f (threads, input));
|
|
33 |
|
|
34 |
val global_state = Synchronized.var "Debugger.state" init_state;
|
|
35 |
|
60829
|
36 |
fun cancel name =
|
60765
|
37 |
Synchronized.change global_state (tap (fn State {threads, ...} =>
|
60829
|
38 |
(case Symtab.lookup threads name of
|
60765
|
39 |
NONE => ()
|
|
40 |
| SOME thread => Simple_Thread.interrupt_unsynchronized thread)));
|
|
41 |
|
60829
|
42 |
fun input name msg =
|
60765
|
43 |
Synchronized.change global_state (map_state (fn (threads, input) =>
|
60829
|
44 |
let val input' = Symtab.map_default (name, Queue.empty) (Queue.enqueue msg) input;
|
60765
|
45 |
in (threads, input') end));
|
|
46 |
|
60829
|
47 |
fun get_input name =
|
60765
|
48 |
Synchronized.guarded_access global_state (fn State {threads, input} =>
|
60829
|
49 |
(case Symtab.lookup input name of
|
60765
|
50 |
NONE => NONE
|
|
51 |
| SOME queue =>
|
|
52 |
let
|
|
53 |
val (msg, queue') = Queue.dequeue queue;
|
|
54 |
val input' =
|
60829
|
55 |
if Queue.is_empty queue' then Symtab.delete_safe name input
|
|
56 |
else Symtab.update (name, queue') input;
|
60765
|
57 |
in SOME (msg, make_state (threads, input')) end));
|
|
58 |
|
|
59 |
|
|
60 |
(* thread data *)
|
|
61 |
|
|
62 |
local val tag = Universal.tag () : ML_Debugger.state list Universal.tag in
|
|
63 |
|
|
64 |
fun get_data () = the_default [] (Thread.getLocal tag);
|
|
65 |
fun setmp_data data f x = setmp_thread_data tag (get_data ()) data f x;
|
|
66 |
|
60749
|
67 |
end;
|
|
68 |
|
60765
|
69 |
val debugging = not o null o get_data;
|
|
70 |
fun with_debugging e = setmp_data (ML_Debugger.state (Thread.self ())) e ();
|
|
71 |
|
|
72 |
|
|
73 |
(* protocol messages *)
|
60749
|
74 |
|
|
75 |
val _ = Session.protocol_handler "isabelle.Debugger$Handler";
|
|
76 |
|
60765
|
77 |
|
|
78 |
(* main entry point *)
|
|
79 |
|
60829
|
80 |
fun debug_loop name =
|
|
81 |
(case get_input name of
|
60765
|
82 |
["continue"] => ()
|
|
83 |
| bad =>
|
|
84 |
(Output.system_message
|
|
85 |
("Debugger: bad input " ^ ML_Syntax.print_list ML_Syntax.print_string bad);
|
60829
|
86 |
debug_loop name));
|
60765
|
87 |
|
|
88 |
fun debugger cond =
|
|
89 |
if debugging () orelse not (cond ()) orelse
|
|
90 |
not (Options.default_bool @{system_option ML_debugger_active})
|
|
91 |
then ()
|
|
92 |
else
|
|
93 |
with_debugging (fn () =>
|
60830
|
94 |
(case Simple_Thread.get_name () of
|
60765
|
95 |
NONE => ()
|
60829
|
96 |
| SOME name => debug_loop name));
|
60765
|
97 |
|
|
98 |
fun init () =
|
|
99 |
ML_Debugger.on_breakpoint
|
|
100 |
(SOME (fn (_, b) =>
|
|
101 |
debugger (fn () => ! b orelse Options.default_bool @{system_option ML_debugger_stepping})));
|
|
102 |
|
|
103 |
|
|
104 |
(* protocol commands *)
|
|
105 |
|
|
106 |
val _ =
|
|
107 |
Isabelle_Process.protocol_command "Debugger.init"
|
|
108 |
(fn [] => init ());
|
|
109 |
|
|
110 |
val _ =
|
|
111 |
Isabelle_Process.protocol_command "Debugger.cancel"
|
60829
|
112 |
(fn [name] => cancel name);
|
60765
|
113 |
|
|
114 |
val _ =
|
|
115 |
Isabelle_Process.protocol_command "Debugger.input"
|
60829
|
116 |
(fn name :: msg => input name msg);
|
60765
|
117 |
|
60749
|
118 |
end;
|