Tracing execution with print statements is
tedious
And impossible (or nearly so) in some situations
Single-stepping/breakpoint debugger is far more effective
Build one to understand how they work
And to show how to test interactive applications
We will want non-interactive input and output for testing
So [%g refactor “refactor” %] the virtual machine of [%x vm %]
Pass an output stream (by default
sys.stdout)
[%inc vm_base.py mark=init %]
print with a call to
self.write[%inc vm_base.py mark=write %]
input function directly[%inc vm_step.py mark=init %]
[%inc vm_step.py mark=read %]
Old VM was either running or finished
New one has a third state: single-stepping
So define an [%g enumeration “enumeration” %]
[%inc architecture.py mark=state %]
New run method starts in STEPPING
state
RUNNING we could never tell it to do
otherwise[%inc vm_step.py mark=run %]
Empty line: go around again
[%g disassemble “Disassemble” %] current instruction or show memory: do that and go around again
Quit: change state to FINISHED.
Run normally: change state to RUNNING
Single-step: exit loop without changing state
[%inc vm_step.py mark=lookup %]
If we type in the number-to-instruction lookup table, it will eventually fall out of step
So build it from architecture description
[% figure slug=“debugger-table” img=“table.svg” alt=“Building a consistent lookup table” caption=“Building a consistent lookup table.” %]
Has to be an object with a write method
But can save what it’s given for later inspection
[%inc test_vm.py mark=writer %]
Need a “function” that takes a prompt and returns a string
Create a class with a __call__ method that “reads”
from a list
[%inc test_vm.py mark=reader %]
[%inc test_vm.py mark=disassemble %]
hlt instruction)Reader with the commands "d" and
"q"Writer to capture outputYes
Test that the debugger can single-step three times and then quit
[%inc test_vm.py mark=print %]
[Expect][expect] often used to script command-line applications
[Selenium][selenium] and [Cypress][cypress] for browser-based applications
Harder to set up and use than a simple
assert
But so are the things they’re testing
Move every interactive operation to a method
Return Boolean to signal whether debugger should stay in interactive mode
[%inc vm_extend.py mark=memory %]
[%inc vm_extend.py mark=step %]
interact to choose operations from a lookup
table[%inc vm_extend.py mark=interact %]
[%inc vm_extend.py mark=init %]
A [%g breakpoint “breakpoint” %] tells the computer to stop at a particular instruction
run
to check[% figure slug=“debugger-beside” img=“beside.svg” alt=“Storing breakpoint addresses beside the program” caption=“Storing breakpoints beside the program.” %]
Replace actual instruction with new brk
instruction
Look up the real instruction when we hit a
brk
[% figure slug=“debugger-break” img=“break.svg” alt=“Inserting breakpoint instructions” caption=“Inserting breakpoints into a program.” %]
Rely on parent class to initialize most of the table
Then add more entries
[%inc vm_break.py mark=init %]
[%inc vm_break.py mark=add %]
[%inc vm_break.py mark=clear %]
[%inc vm_break.py mark=run %]
[% figure slug=“debugger-concept-map” img=“concept_map.svg” alt=“Concept map for debugger” caption=“Concept map.” %]