Single stepping
If the last chapter sounds like cheating, this chapter is going to start with something real. Single stepping is typically provided by processors by setting a flag, when that flag is on, the processor will run a single instruction and then issue an interrupt, the operating system will handle that interrupt to notify the debugger a single stepping happened.
To model this, we will enrich our private interface to include single stepping and also the callback when the single stepping completes.
Interface
class debugger
{
public:
void step_instruction();
debugger_virtual_machine_interface* get_debugger_virtual_machine_interface();
};
class virtual_machine_debugging_interface
{
public:
virtual void set_single_step(bool on) = 0;
};
class debugger_virtual_machine_interface
{
public:
virtual void on_single_step() = 0;
};
Implementation
Note that a processor will not automatically reset its single stepping flag, so our virtual machine should not. This set of interface can be implemented simply by setting a flag in our virtual machine and break out of the interpretation loop if it is set.
Practical notes
This is exactly how single stepping works in real processors. It is a very important primitive to support debugging.