author | wenzelm |
Tue, 11 Aug 2015 20:32:56 +0200 | |
changeset 60900 | 11a0f333de6f |
parent 60899 | 84569dbe1e30 |
child 60901 | ce8abd005c5d |
permissions | -rw-r--r-- |
60749 | 1 |
/* Title: Pure/Tools/debugger.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Interactive debugger for Isabelle/ML. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Debugger |
|
11 |
{ |
|
60834 | 12 |
/* global state */ |
13 |
||
60842 | 14 |
sealed case class Debug_State( |
15 |
pos: Position.T, |
|
16 |
function: String) |
|
17 |
||
60835 | 18 |
sealed case class State( |
19 |
session: Session = new Session(Resources.empty), |
|
60876 | 20 |
active: Int = 0, |
21 |
active_breakpoints: Set[Long] = Set.empty, |
|
60875 | 22 |
focus: Option[Position.T] = None, // position of active GUI component |
60842 | 23 |
threads: Map[String, List[Debug_State]] = Map.empty, // thread name ~> stack of debug states |
60834 | 24 |
output: Map[String, Command.Results] = Map.empty) // thread name ~> output messages |
25 |
{ |
|
60842 | 26 |
def set_session(new_session: Session): State = |
27 |
copy(session = new_session) |
|
28 |
||
60898 | 29 |
def is_active: Boolean = active > 0 |
30 |
def inc_active: State = copy(active = active + 1) |
|
31 |
def dec_active: State = copy(active = active - 1) |
|
60876 | 32 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
33 |
def toggle_breakpoint(breakpoint: Long): (Boolean, State) = |
60876 | 34 |
{ |
35 |
val active_breakpoints1 = |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
36 |
if (active_breakpoints(breakpoint)) active_breakpoints - breakpoint |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
37 |
else active_breakpoints + breakpoint |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
38 |
(active_breakpoints1(breakpoint), copy(active_breakpoints = active_breakpoints1)) |
60876 | 39 |
} |
40 |
||
60875 | 41 |
def set_focus(new_focus: Option[Position.T]): State = |
42 |
copy(focus = new_focus) |
|
43 |
||
60842 | 44 |
def get_thread(thread_name: String): List[Debug_State] = |
45 |
threads.getOrElse(thread_name, Nil) |
|
46 |
||
47 |
def update_thread(thread_name: String, debug_states: List[Debug_State]): State = |
|
48 |
copy(threads = threads + (thread_name -> debug_states)) |
|
49 |
||
50 |
def get_output(thread_name: String): Command.Results = |
|
51 |
output.getOrElse(thread_name, Command.Results.empty) |
|
52 |
||
53 |
def add_output(thread_name: String, entry: Command.Results.Entry): State = |
|
54 |
copy(output = output + (thread_name -> (get_output(thread_name) + entry))) |
|
55 |
||
56 |
def clear_output(thread_name: String): State = |
|
57 |
copy(output = output - thread_name) |
|
58 |
||
59 |
def purge(thread_name: String): State = |
|
60 |
if (get_thread(thread_name).isEmpty) |
|
61 |
copy(threads = threads - thread_name, output = output - thread_name) |
|
62 |
else this |
|
60834 | 63 |
} |
64 |
||
65 |
private val global_state = Synchronized(State()) |
|
66 |
||
67 |
||
60835 | 68 |
/* protocol handler */ |
60749 | 69 |
|
60900 | 70 |
case object Update |
60749 | 71 |
|
72 |
class Handler extends Session.Protocol_Handler |
|
73 |
{ |
|
60835 | 74 |
private val delay_update = |
60898 | 75 |
Simple_Thread.delay_first(global_state.value.session.output_delay) { |
60900 | 76 |
global_state.value.session.debugger_updates.post(Update) |
60835 | 77 |
} |
78 |
||
60842 | 79 |
private def debugger_state(prover: Prover, msg: Prover.Protocol_Output): Boolean = |
80 |
{ |
|
81 |
msg.properties match { |
|
82 |
case Markup.Debugger_State(thread_name) => |
|
60863 | 83 |
val msg_body = |
84 |
YXML.parse_body_failsafe(Symbol.decode(UTF8.decode_permissive(msg.bytes))) |
|
60842 | 85 |
val debug_states = |
86 |
{ |
|
87 |
import XML.Decode._ |
|
88 |
list(pair(properties, Symbol.decode_string))(msg_body).map({ |
|
89 |
case (pos, function) => Debug_State(pos, function) |
|
90 |
}) |
|
91 |
} |
|
92 |
global_state.change(_.update_thread(thread_name, debug_states)) |
|
60900 | 93 |
delay_update.invoke() |
60842 | 94 |
true |
95 |
case _ => false |
|
96 |
} |
|
97 |
} |
|
98 |
||
60830 | 99 |
private def debugger_output(prover: Prover, msg: Prover.Protocol_Output): Boolean = |
100 |
{ |
|
101 |
msg.properties match { |
|
60835 | 102 |
case Markup.Debugger_Output(thread_name) => |
60834 | 103 |
val msg_body = |
104 |
prover.xml_cache.body( |
|
60863 | 105 |
YXML.parse_body_failsafe(Symbol.decode(UTF8.decode_permissive(msg.bytes)))) |
60834 | 106 |
msg_body match { |
107 |
case List(XML.Elem(Markup(name, props @ Markup.Serial(i)), body)) => |
|
108 |
val message = XML.Elem(Markup(Markup.message(name), props), body) |
|
60835 | 109 |
global_state.change(_.add_output(thread_name, i -> message)) |
60900 | 110 |
delay_update.invoke() |
60834 | 111 |
true |
112 |
case _ => false |
|
113 |
} |
|
60830 | 114 |
case _ => false |
115 |
} |
|
116 |
} |
|
117 |
||
118 |
val functions = |
|
60842 | 119 |
Map( |
120 |
Markup.DEBUGGER_STATE -> debugger_state _, |
|
121 |
Markup.DEBUGGER_OUTPUT -> debugger_output _) |
|
60749 | 122 |
} |
60765 | 123 |
|
124 |
||
125 |
/* protocol commands */ |
|
126 |
||
60889 | 127 |
def set_session(session: Session): Unit = |
60842 | 128 |
global_state.change(_.set_session(session)) |
60765 | 129 |
|
60898 | 130 |
def is_active(): Boolean = global_state.value.is_active |
60889 | 131 |
|
132 |
def inc_active(): Unit = |
|
133 |
global_state.change(state => |
|
134 |
{ |
|
60898 | 135 |
val state1 = state.inc_active |
136 |
if (!state.is_active && state1.is_active) |
|
137 |
state1.session.protocol_command("Debugger.init") |
|
60889 | 138 |
state1 |
139 |
}) |
|
140 |
||
141 |
def dec_active(): Unit = |
|
142 |
global_state.change(state => |
|
143 |
{ |
|
60898 | 144 |
val state1 = state.dec_active |
145 |
if (state.is_active && !state1.is_active) |
|
146 |
state1.session.protocol_command("Debugger.exit") |
|
60889 | 147 |
state1 |
148 |
}) |
|
60876 | 149 |
|
60882 | 150 |
def active_breakpoint_state(breakpoint: Long): Option[Boolean] = |
60876 | 151 |
{ |
60898 | 152 |
val state = global_state.value |
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
153 |
if (state.active > 0) Some(state.active_breakpoints(breakpoint)) else None |
60876 | 154 |
} |
155 |
||
60882 | 156 |
def breakpoint_state(breakpoint: Long): Boolean = |
60898 | 157 |
global_state.value.active_breakpoints(breakpoint) |
60882 | 158 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
159 |
def toggle_breakpoint(command: Command, breakpoint: Long) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
160 |
{ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
161 |
global_state.change(state => |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
162 |
{ |
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
163 |
val (breakpoint_state, state1) = state.toggle_breakpoint(breakpoint) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
164 |
state1.session.protocol_command( |
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
165 |
"Debugger.breakpoint", |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
166 |
Symbol.encode(command.node_name.node), |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
167 |
Document_ID(command.id), |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
168 |
Properties.Value.Long(breakpoint), |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
169 |
Properties.Value.Boolean(breakpoint_state)) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
170 |
state1 |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
171 |
}) |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
172 |
} |
60876 | 173 |
|
60875 | 174 |
def focus(new_focus: Option[Position.T]): Boolean = |
175 |
global_state.change_result(state => (state.focus != new_focus, state.set_focus(new_focus))) |
|
176 |
||
60898 | 177 |
def threads(): Map[String, List[Debug_State]] = global_state.value.threads |
178 |
||
179 |
def output(): Map[String, Command.Results] = global_state.value.output |
|
180 |
||
60854 | 181 |
def input(thread_name: String, msg: String*): Unit = |
60898 | 182 |
global_state.value.session.protocol_command("Debugger.input", (thread_name :: msg.toList):_*) |
60854 | 183 |
|
60899 | 184 |
def continue(thread_name: String): Unit = input(thread_name, "continue") |
60869 | 185 |
def step(thread_name: String): Unit = input(thread_name, "step") |
186 |
def step_over(thread_name: String): Unit = input(thread_name, "step_over") |
|
187 |
def step_out(thread_name: String): Unit = input(thread_name, "step_out") |
|
60856 | 188 |
|
60896 | 189 |
def eval(thread_name: String, index: Int, SML: Boolean, context: String, expression: String) |
60856 | 190 |
{ |
60896 | 191 |
global_state.change(state => { |
192 |
input(thread_name, "eval", |
|
193 |
index.toString, SML.toString, Symbol.encode(context), Symbol.encode(expression)) |
|
194 |
state.clear_output(thread_name) |
|
195 |
}) |
|
60856 | 196 |
} |
60897 | 197 |
|
198 |
def print_vals(thread_name: String, index: Int, SML: Boolean, context: String) |
|
199 |
{ |
|
200 |
global_state.change(state => { |
|
201 |
input(thread_name, "print_vals", index.toString, SML.toString, Symbol.encode(context)) |
|
202 |
state.clear_output(thread_name) |
|
203 |
}) |
|
204 |
} |
|
60749 | 205 |
} |