author | wenzelm |
Mon, 24 Aug 2015 00:20:20 +0200 | |
changeset 61011 | 018b0c996b54 |
parent 61010 | cccfd7f6317d |
child 61014 | 39f67bb4e609 |
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 |
{ |
|
61010 | 12 |
/* context */ |
60834 | 13 |
|
60842 | 14 |
sealed case class Debug_State( |
15 |
pos: Position.T, |
|
16 |
function: String) |
|
17 |
||
61010 | 18 |
sealed case class Context(thread_name: String, debug_states: List[Debug_State], index: Int = 0) |
19 |
{ |
|
20 |
def size: Int = debug_states.length + 1 |
|
21 |
def reset: Context = copy(index = 0) |
|
22 |
def select(i: Int) = copy(index = i + 1) |
|
23 |
||
24 |
def thread_state: Option[Debug_State] = debug_states.headOption |
|
25 |
||
26 |
def stack_state: Option[Debug_State] = |
|
27 |
if (1 <= index && index <= debug_states.length) |
|
28 |
Some(debug_states(index - 1)) |
|
29 |
else None |
|
30 |
||
31 |
def debug_state_index: Option[Int] = |
|
32 |
if (stack_state.isDefined) Some(index - 1) |
|
33 |
else if (debug_states.nonEmpty) Some(0) |
|
34 |
else None |
|
35 |
||
36 |
def debug_state: Option[Debug_State] = stack_state orElse thread_state |
|
37 |
||
38 |
override def toString: String = |
|
39 |
stack_state match { |
|
40 |
case None => thread_name |
|
41 |
case Some(d) => d.function |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
||
46 |
/* global state */ |
|
47 |
||
60835 | 48 |
sealed case class State( |
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
49 |
session: Session = new Session(Resources.empty), // implicit session |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
50 |
active: Int = 0, // active views |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
51 |
break: Boolean = false, // break at next possible breakpoint |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
52 |
active_breakpoints: Set[Long] = Set.empty, // explicit breakpoint state |
60875 | 53 |
focus: Option[Position.T] = None, // position of active GUI component |
60842 | 54 |
threads: Map[String, List[Debug_State]] = Map.empty, // thread name ~> stack of debug states |
60834 | 55 |
output: Map[String, Command.Results] = Map.empty) // thread name ~> output messages |
56 |
{ |
|
60842 | 57 |
def set_session(new_session: Session): State = |
58 |
copy(session = new_session) |
|
59 |
||
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
60 |
def set_break(b: Boolean): State = copy(break = b) |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
61 |
|
60910 | 62 |
def is_active: Boolean = active > 0 && session.is_ready |
60898 | 63 |
def inc_active: State = copy(active = active + 1) |
64 |
def dec_active: State = copy(active = active - 1) |
|
60876 | 65 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
66 |
def toggle_breakpoint(breakpoint: Long): (Boolean, State) = |
60876 | 67 |
{ |
68 |
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
|
69 |
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
|
70 |
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
|
71 |
(active_breakpoints1(breakpoint), copy(active_breakpoints = active_breakpoints1)) |
60876 | 72 |
} |
73 |
||
60875 | 74 |
def set_focus(new_focus: Option[Position.T]): State = |
75 |
copy(focus = new_focus) |
|
76 |
||
60842 | 77 |
def get_thread(thread_name: String): List[Debug_State] = |
78 |
threads.getOrElse(thread_name, Nil) |
|
79 |
||
80 |
def update_thread(thread_name: String, debug_states: List[Debug_State]): State = |
|
60903 | 81 |
if (debug_states.isEmpty) copy(threads = threads - thread_name) |
82 |
else copy(threads = threads + (thread_name -> debug_states)) |
|
60842 | 83 |
|
84 |
def get_output(thread_name: String): Command.Results = |
|
85 |
output.getOrElse(thread_name, Command.Results.empty) |
|
86 |
||
87 |
def add_output(thread_name: String, entry: Command.Results.Entry): State = |
|
88 |
copy(output = output + (thread_name -> (get_output(thread_name) + entry))) |
|
89 |
||
90 |
def clear_output(thread_name: String): State = |
|
91 |
copy(output = output - thread_name) |
|
60834 | 92 |
} |
93 |
||
94 |
private val global_state = Synchronized(State()) |
|
95 |
||
96 |
||
60902 | 97 |
/* update events */ |
60749 | 98 |
|
60900 | 99 |
case object Update |
60749 | 100 |
|
60902 | 101 |
private val delay_update = |
102 |
Simple_Thread.delay_first(global_state.value.session.output_delay) { |
|
103 |
global_state.value.session.debugger_updates.post(Update) |
|
104 |
} |
|
105 |
||
106 |
||
107 |
/* protocol handler */ |
|
108 |
||
60749 | 109 |
class Handler extends Session.Protocol_Handler |
110 |
{ |
|
60842 | 111 |
private def debugger_state(prover: Prover, msg: Prover.Protocol_Output): Boolean = |
112 |
{ |
|
113 |
msg.properties match { |
|
114 |
case Markup.Debugger_State(thread_name) => |
|
60863 | 115 |
val msg_body = |
116 |
YXML.parse_body_failsafe(Symbol.decode(UTF8.decode_permissive(msg.bytes))) |
|
60842 | 117 |
val debug_states = |
118 |
{ |
|
119 |
import XML.Decode._ |
|
120 |
list(pair(properties, Symbol.decode_string))(msg_body).map({ |
|
121 |
case (pos, function) => Debug_State(pos, function) |
|
122 |
}) |
|
123 |
} |
|
124 |
global_state.change(_.update_thread(thread_name, debug_states)) |
|
60900 | 125 |
delay_update.invoke() |
60842 | 126 |
true |
127 |
case _ => false |
|
128 |
} |
|
129 |
} |
|
130 |
||
60830 | 131 |
private def debugger_output(prover: Prover, msg: Prover.Protocol_Output): Boolean = |
132 |
{ |
|
133 |
msg.properties match { |
|
60835 | 134 |
case Markup.Debugger_Output(thread_name) => |
60834 | 135 |
val msg_body = |
136 |
prover.xml_cache.body( |
|
60863 | 137 |
YXML.parse_body_failsafe(Symbol.decode(UTF8.decode_permissive(msg.bytes)))) |
60834 | 138 |
msg_body match { |
139 |
case List(XML.Elem(Markup(name, props @ Markup.Serial(i)), body)) => |
|
140 |
val message = XML.Elem(Markup(Markup.message(name), props), body) |
|
60835 | 141 |
global_state.change(_.add_output(thread_name, i -> message)) |
60900 | 142 |
delay_update.invoke() |
60834 | 143 |
true |
144 |
case _ => false |
|
145 |
} |
|
60830 | 146 |
case _ => false |
147 |
} |
|
148 |
} |
|
149 |
||
150 |
val functions = |
|
60842 | 151 |
Map( |
152 |
Markup.DEBUGGER_STATE -> debugger_state _, |
|
153 |
Markup.DEBUGGER_OUTPUT -> debugger_output _) |
|
60749 | 154 |
} |
60765 | 155 |
|
156 |
||
157 |
/* protocol commands */ |
|
158 |
||
60898 | 159 |
def is_active(): Boolean = global_state.value.is_active |
60889 | 160 |
|
60910 | 161 |
def init_session(session: Session) |
162 |
{ |
|
163 |
global_state.change(state => |
|
164 |
{ |
|
165 |
val state1 = state.set_session(session) |
|
166 |
if (!state.session.is_ready && state1.session.is_ready && state1.is_active) |
|
167 |
state1.session.protocol_command("Debugger.init") |
|
168 |
state1 |
|
169 |
}) |
|
170 |
} |
|
171 |
||
172 |
def init(): Unit = |
|
60889 | 173 |
global_state.change(state => |
174 |
{ |
|
60898 | 175 |
val state1 = state.inc_active |
176 |
if (!state.is_active && state1.is_active) |
|
177 |
state1.session.protocol_command("Debugger.init") |
|
60889 | 178 |
state1 |
179 |
}) |
|
180 |
||
60910 | 181 |
def exit(): Unit = |
60889 | 182 |
global_state.change(state => |
183 |
{ |
|
60898 | 184 |
val state1 = state.dec_active |
185 |
if (state.is_active && !state1.is_active) |
|
186 |
state1.session.protocol_command("Debugger.exit") |
|
60889 | 187 |
state1 |
188 |
}) |
|
60876 | 189 |
|
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
190 |
def is_break(): Boolean = global_state.value.break |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
191 |
def set_break(b: Boolean) |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
192 |
{ |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
193 |
global_state.change(state => { |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
194 |
val state1 = state.set_break(b) |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
195 |
state1.session.protocol_command("Debugger.break", b.toString) |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
196 |
state1 |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
197 |
}) |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
198 |
delay_update.invoke() |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
199 |
} |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60912
diff
changeset
|
200 |
|
60882 | 201 |
def active_breakpoint_state(breakpoint: Long): Option[Boolean] = |
60876 | 202 |
{ |
60898 | 203 |
val state = global_state.value |
60912 | 204 |
if (state.is_active) Some(state.active_breakpoints(breakpoint)) else None |
60876 | 205 |
} |
206 |
||
60882 | 207 |
def breakpoint_state(breakpoint: Long): Boolean = |
60898 | 208 |
global_state.value.active_breakpoints(breakpoint) |
60882 | 209 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
210 |
def toggle_breakpoint(command: Command, breakpoint: Long) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
211 |
{ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
212 |
global_state.change(state => |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
213 |
{ |
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
214 |
val (breakpoint_state, state1) = state.toggle_breakpoint(breakpoint) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
215 |
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
|
216 |
"Debugger.breakpoint", |
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
217 |
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
|
218 |
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
|
219 |
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
|
220 |
Properties.Value.Boolean(breakpoint_state)) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
221 |
state1 |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
222 |
}) |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
223 |
} |
60876 | 224 |
|
61011 | 225 |
def focus(): Option[Position.T] = global_state.value.focus |
226 |
||
61008 | 227 |
def set_focus(focus: Option[Position.T]) |
228 |
{ |
|
61007 | 229 |
global_state.change(_.set_focus(focus)) |
61008 | 230 |
delay_update.invoke() |
231 |
} |
|
60875 | 232 |
|
60898 | 233 |
def threads(): Map[String, List[Debug_State]] = global_state.value.threads |
234 |
||
235 |
def output(): Map[String, Command.Results] = global_state.value.output |
|
236 |
||
60854 | 237 |
def input(thread_name: String, msg: String*): Unit = |
60898 | 238 |
global_state.value.session.protocol_command("Debugger.input", (thread_name :: msg.toList):_*) |
60854 | 239 |
|
60899 | 240 |
def continue(thread_name: String): Unit = input(thread_name, "continue") |
60869 | 241 |
def step(thread_name: String): Unit = input(thread_name, "step") |
242 |
def step_over(thread_name: String): Unit = input(thread_name, "step_over") |
|
243 |
def step_out(thread_name: String): Unit = input(thread_name, "step_out") |
|
60856 | 244 |
|
60901 | 245 |
def clear_output(thread_name: String) |
246 |
{ |
|
247 |
global_state.change(_.clear_output(thread_name)) |
|
60902 | 248 |
delay_update.invoke() |
60901 | 249 |
} |
250 |
||
61010 | 251 |
def eval(c: Context, SML: Boolean, context: String, expression: String) |
60856 | 252 |
{ |
60896 | 253 |
global_state.change(state => { |
61010 | 254 |
input(c.thread_name, "eval", c.debug_state_index.getOrElse(0).toString, |
255 |
SML.toString, Symbol.encode(context), Symbol.encode(expression)) |
|
256 |
state.clear_output(c.thread_name) |
|
60896 | 257 |
}) |
60902 | 258 |
delay_update.invoke() |
60856 | 259 |
} |
60897 | 260 |
|
61010 | 261 |
def print_vals(c: Context, SML: Boolean, context: String) |
60897 | 262 |
{ |
61010 | 263 |
require(c.debug_state_index.isDefined) |
264 |
||
60897 | 265 |
global_state.change(state => { |
61010 | 266 |
input(c.thread_name, "print_vals", c.debug_state_index.getOrElse(0).toString, |
267 |
SML.toString, Symbol.encode(context)) |
|
268 |
state.clear_output(c.thread_name) |
|
60897 | 269 |
}) |
60902 | 270 |
delay_update.invoke() |
60897 | 271 |
} |
60749 | 272 |
} |