-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.py
310 lines (239 loc) · 10.1 KB
/
debugger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import urwid
from typing import Tuple, List
from vm import VirtualMachine, VirtualMachineStatus, OpCode, OpCodeArguments
SCREEN_UPDATE_INTERVAL = 1000
OPCODE_NAMES = [str(op).split(".")[1] for op in OpCode]
PALETTE = [
("opcode", "light blue", "black", ()),
("args", "light green", "black", ()),
("pos", "yellow", "black", ()),
("brk", "light red", "black", ()),
("label", "dark gray", "black", ()),
]
NUM_PADDING = 6
def disassemble_next(vm: VirtualMachine, pos: int) -> Tuple[str, List[int], int]:
op = vm.program[pos]
nargs = OpCodeArguments[op]
args = vm.program[pos + 1 : pos + 1 + nargs]
return (OPCODE_NAMES[op], args, pos + nargs + 1)
def disassemble_prev(vm: VirtualMachine, pos: int) -> Tuple[str, List[int], int]:
k = 1
while vm.program[pos - k] not in range(22):
k += 1
pos = pos - k
op = vm.program[pos]
nargs = OpCodeArguments[op]
args = vm.program[pos + 1 : pos + 1 + nargs]
return (OPCODE_NAMES[op], args, pos)
def disassemble(vm: VirtualMachine) -> List[Tuple[int, int, List[int]]]:
pos = 0
asm = []
while pos < len(vm.program):
op = vm.program[pos]
nargs = OpCodeArguments.get(op, 0)
args = vm.program[pos + 1 : pos + 1 + nargs]
asm.append((pos, op, args))
pos += nargs + 1
return asm
class DisassemblyWalker(urwid.ListWalker):
def __init__(self, vm: VirtualMachine, breakpoints: set):
self.vm = vm
self.breakpoints = breakpoints
self.asm = disassemble(self.vm)
self.focus = 0
self.reset()
def reset(self) -> None:
self.asm = disassemble(self.vm)
i = 0
for vmpos, _, _ in self.asm:
if vmpos == self.vm.pos:
break
i += 1
self.set_focus(max(0, i - 2))
def get_focus(self) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(self.focus)
def set_focus(self, focus: int) -> None:
self.focus = focus
self._modified()
def get_next(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(pos + 1)
def get_prev(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(pos - 1)
def _get_line_at(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
if pos < 0 or pos >= len(self.asm) or len(self.asm) == 0:
return None, None
vmpos, opcode, args = self.asm[pos]
if opcode in range(22):
opcode = OPCODE_NAMES[opcode]
mpos = ">" if vmpos == self.vm.pos else " "
mbrk = "o" if vmpos in self.breakpoints else " "
text = urwid.Text([("brk", mpos),
("brk", mbrk),
" ",
("pos", str(vmpos).rjust(NUM_PADDING)),
("opcode", str(opcode).rjust(NUM_PADDING)),
("args", "".join(str(a).rjust(NUM_PADDING) for a in args)),
])
return text, pos
class OutputBuffer(urwid.ListWalker):
def __init__(self):
self.lines = [urwid.Text("")]
self.focus = 0
def __len__(self) -> int:
return len(self.lines)
def get_focus(self) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(self.focus)
def set_focus(self, focus: int) -> None:
self.focus = focus
self._modified()
def get_next(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(pos + 1)
def get_prev(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
return self._get_line_at(pos - 1)
def _get_line_at(self, pos: int) -> Tuple[urwid.Text, int] | Tuple[None, None]:
if pos < 0 or len(self.lines) == 0:
return None, None
if pos < len(self.lines):
return self.lines[pos], pos
return self.lines[-1], pos
def write(self, data: str) -> None:
final_newline = self.lines.pop()
new_lines = data.splitlines()
for l in new_lines:
text = urwid.Text(l)
self.lines.append(text)
self.lines.append(final_newline)
self._modified()
class VMDebugger():
def __init__(self, vm: VirtualMachine):
self.vm = vm
self.breakpoints = set()
# Status widget
self.text_position = urwid.Text(f"Position: {vm.pos}")
self.text_ncycles = urwid.Text(f"Steps: {vm.ncycles}")
self.text_vmstatus = urwid.Text(f"Status: {str(vm.status)}")
self.text_breakpoints = urwid.Text(f"Breakpoints: {str(self.breakpoints)}")
self.status_widget = urwid.LineBox(
urwid.Pile([
self.text_position,
self.text_ncycles,
self.text_vmstatus,
self.text_breakpoints,
]),
title="Status"
)
# stdout
output_walker = OutputBuffer()
self.output_widget = urwid.ListBox(output_walker)
# stdin
self.input_widget = urwid.Edit("")
# disassembly
self.disassembly_walker = DisassemblyWalker(self.vm, self.breakpoints)
self.disassembly_widget = urwid.ListBox(self.disassembly_walker)
# breakpoint entry
self.breakpoint_widget = urwid.IntEdit("")
# registers
self.registers_line = urwid.Text([str(val).rjust(NUM_PADDING) for val in vm.registers])
self.registers_widget = urwid.Pile([
urwid.Text([("label", str(n).rjust(NUM_PADDING)) for n in range(len(vm.registers))]),
self.registers_line
])
# stack
self.stack_widget = urwid.Text("")
# status line
self.status_line = urwid.Text("")
# help text
self.help_text = urwid.Text([
"Commands: ", "[q]uit", " | ", "[r]un", " | ", "[s]tep", " | ", "[b]reakpoint"
])
self.main_pile = urwid.Pile([
self.status_widget,
urwid.LineBox(self.registers_widget, title="Registers"),
urwid.LineBox(self.stack_widget, title="Stack"),
(15, urwid.LineBox(self.output_widget, title="Output")),
urwid.LineBox(self.input_widget, title="Input"),
(15, urwid.LineBox(self.disassembly_widget, title="Disassembly")),
urwid.LineBox(self.breakpoint_widget, title="Breakpoint"),
urwid.LineBox(self.status_line),
urwid.LineBox(self.help_text),
])
self.pile_indices = {
"status": 0,
"registers": 1,
"stack": 2,
"output": 3,
"input": 4,
"disassembly": 5,
"breakpoint_input": 6,
"status": 7,
}
self.top = urwid.Filler(self.main_pile, valign="top")
self.loop = urwid.MainLoop(self.top, PALETTE, unhandled_input=self.unhandled_input)
self.loop.screen.set_terminal_properties(colors=256)
self.vm.stdout = output_walker
self.vm.break_on_input = True
self.update_status_widget(force_update=False)
def unhandled_input(self, key: str) -> None:
self.status_line.set_text(f"You pressed: {repr(key)}")
match key:
case "q":
raise urwid.ExitMainLoop()
case "s":
self.vm_step()
case "r":
self.vm_run()
case "b":
self.main_pile.focus_position = self.pile_indices["breakpoint_input"]
case "enter":
if (self.main_pile.focus_position == self.pile_indices["input"] and
self.vm.status != VirtualMachineStatus.FINISHED):
text = self.input_widget.get_edit_text() + "\n"
self.vm.input_buffer = text
self.main_pile.focus_position = self.pile_indices["output"]
# self.vm_run()
self.vm_step()
elif self.main_pile.focus_position == self.pile_indices["breakpoint_input"]:
pt = self.breakpoint_widget.value()
if pt in self.breakpoints:
self.breakpoints.remove(pt)
else:
self.breakpoints.add(pt)
self.update_status_widget()
case "esc":
self.main_pile.focus_position = self.pile_indices["output"]
case _:
self.status_line.set_text(f"You pressed: {repr(key)}")
def vm_step(self) -> None:
self.vm.step()
self.update_status_widget()
def vm_run(self) -> None:
if self.vm.status == VirtualMachineStatus.FINISHED:
return
running = self.vm.step()
while running and self.vm.pos not in self.breakpoints:
running = self.vm.step()
if self.vm.ncycles % SCREEN_UPDATE_INTERVAL == 0:
self.update_status_widget()
self.update_status_widget()
if self.vm.status == VirtualMachineStatus.EXPECTING_INPUT:
self.input_widget.set_edit_text("")
self.main_pile.focus_position = self.pile_indices["input"]
def update_status_widget(self, force_update : bool = True) -> None:
self.text_position.set_text(["Position: ", ("pos", f"{self.vm.pos}")])
self.text_ncycles.set_text(f"Cycles: {self.vm.ncycles}")
self.text_vmstatus.set_text(f"Status: {str(self.vm.status)}")
self.text_breakpoints.set_text(["Breakpoints: ", ("brk", f"{str(self.breakpoints)}")])
self.registers_line.set_text([str(val).rjust(NUM_PADDING) for val in self.vm.registers])
if len(self.vm.stack) > 0:
self.stack_widget.set_text([str(val).rjust(NUM_PADDING) for val in self.vm.stack])
else:
self.stack_widget.set_text("")
self.disassembly_walker.reset()
self.disassembly_widget.set_focus_valign("top")
self.output_widget.set_focus(len(self.output_widget.body))
if force_update:
self.loop.draw_screen()
if __name__ == "__main__":
VM = VirtualMachine.from_binary("challenge.bin")
VMD = VMDebugger(VM)
VMD.loop.run()