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