Suppose a function running inside a microVM returns the wrong result. We have its source code and debug symbols. We would like to set a breakpoint, inspect its variables, and step through the function.

The familiar tools are available. The question is how to connect them.

Attaching a debugger to the host application shows the code that enters the virtual processor. The guest instructions execute in a different address space, with a different set of registers. We need a way to ask questions about that execution.

I have been adding ARM64 debugging support to Hyperlight. This first post explains where the debugging endpoint lives, how debugger requests reach the virtual processor, and how an ARM64 exception becomes a stop that GDB can understand.

Where does the debugger live?

Hyperlight runs guest code in a small virtual machine embedded in a host application. The guest has a runtime, but it does not need a full operating system.

That makes the usual remote debugging arrangement inconvenient. There is no guest shell in which to start gdbserver, and no guest network stack is required.

The debugging endpoint lives in the host.

GDB, with the guest's executable and symbols
                  |
          GDB remote protocol
                  |
       Hyperlight in the host process
                  |
           Hypervisor backend
                  |
              Guest vCPU

GDB uses the executable’s debug information to associate machine addresses with functions, source lines, and variables. Hyperlight provides the lower-level operations: read registers, read memory, install breakpoints, and control execution.

This distinction is useful. Hyperlight does not need to understand a Rust local variable. It needs to give GDB the register values and memory bytes from which GDB can reconstruct that variable.

The host uses the gdbstub crate for the remote protocol. The ARM64 work supplies the architecture and backend operations behind it.

Two threads, one virtual processor

There are two different conversations happening.

One is between GDB and the host over a socket. The other is between the host and the hypervisor that runs the guest.

Hyperlight gives the protocol its own thread. That thread translates incoming packets into internal requests. The sandbox thread owns guest execution and services those requests while the vCPU is stopped.

GDB             Protocol thread          Sandbox thread
 |                     |                        |
 | read registers      |                        |
 |-------------------->| ReadRegisters          |
 |                     |----------------------->|
 |                     |       register values  |
 |                     |<-----------------------|
 | register packet     |                        |
 |<--------------------|                        |

For continue, the sandbox thread acknowledges the request and enters the guest. When execution stops, it sends a stop notification back through the same arrangement.

The socket thread does not independently modify registers while the sandbox thread is running the vCPU. The boundary between the two threads gives debugger commands a well-defined place in the execution loop.

The first stop

There is a small but important question before any breakpoint works: where should the guest be when GDB attaches?

For a fresh guest, the useful answer is before its first instruction. Hyperlight prepares the guest registers, then reports an initial stop before entering the vCPU.

Loading an initialized snapshot is different. Guest initialization has already happened, so there is no initialization entry to stop at. The initial stop belongs at the first dispatcher entry.

Both cases use a pending flag consumed at the start of the common run path. The stop happens once, after the appropriate registers are prepared.

This matters because an initial stop is a promise about state. Reporting it before preparing the registers gives GDB the wrong starting point. Reporting it on every host-to-guest call interrupts an otherwise normal debugging session.

Following a breakpoint

Consider a software breakpoint.

ARM64 instructions are four bytes long. Hyperlight saves the instruction at the breakpoint address and replaces it with a four-byte BRK instruction.

Before:  original instruction
After:   brk #0

GDB then asks the guest to continue.

The processor executes guest instructions directly. When it reaches BRK, it raises a debug exception. The host must turn that event into a stop notification that GDB understands.

The route depends on the backend, Hyperlight’s adapter to the host virtualization API. WHP stands for Windows Hypervisor Platform. The Linux MSHV driver provides access to Microsoft Hyper-V.

Backend How the debug stop reaches Hyperlight
Linux KVM KVM reports a debug VM exit.
Windows WHP A guest exception vector forwards the event through a hypercall.
Linux MSHV A guest exception vector forwards the event through a Hyper-V hypercall intercept.

KVM exposes the debug event directly. The Hyper-V-backed implementations use a small amount of guest exception-handling code to make the event visible to the host. The guest runtime supplies an exception vector table, which tells the processor where these handlers live.

A hypercall is a request from the guest to the hypervisor. Here, the guest handler uses ARM64’s HVC instruction with a reserved immediate value. The backend recognizes that value, examines the exception information, and reports a software breakpoint, hardware breakpoint, or completed step.

After that translation, the common debugger code handles the stop. GDB does not need to know which backend produced it.

Hardware breakpoints use the processor’s address-matching registers rather than patching an instruction. They avoid changing code, but the number of available registers limits how many can be installed.

Which program counter?

Exception forwarding introduces a less obvious problem.

By the time the host sees the hypercall, the processor is executing the exception handler. Its current program counter points into that handler. That is not the instruction the user wanted to debug.

The guest runtime executes at EL1, ARM’s usual kernel privilege level. ARM64 preserves its exception return address in ELR_EL1 and the saved processor state in SPSR_EL1. For a forwarded debug exception, the backend presents those saved values as the guest’s PC and processor state.

Register writes need the same treatment. If the user changes PC, the change must affect the saved return context. Updating only the handler’s current PC would change the machinery used to deliver the stop.

When execution resumes, the handler returns through ERET, which restores the interrupted execution context.

There are therefore two useful views of the processor: the state needed to finish handling the exception, and the state the debugger presents to the user. The backend keeps them consistent.

Stepping is not adding four

Fixed-width instructions make it tempting to implement stepping by placing a breakpoint four bytes after PC.

That does not work for a branch, a call, or a return. The next executed instruction need not be the next instruction in memory.

For an instruction-step request, Hyperlight enables hardware single-step. The processor determines the next execution point and raises the corresponding debug exception.

Software breakpoint step-over is coordinated by GDB. GDB controls removing and reinserting the breakpoint around the original instruction. Hyperlight provides the breakpoint operations and hardware stepping, without a second instruction-prediction engine.

Source-level stepping is another layer above this. GDB may need several instruction-level operations to reach the next source line.

What the user sees

The intended experience is ordinary remote debugging. The host enables the gdb feature and configures a debug port for the sandbox.

With an ARM64-capable GDB and the matching guest executable, a session looks like this:

file guest.elf
target remote localhost:8080
info registers pc sp
x/8i $pc
break guest_function
continue
si

The executable supplies symbols. The remote connection supplies the live machine state. The breakpoint name must exist in that executable, and source-level inspection depends on the debug information retained in the build.

The ARM64 backends provide register and memory access, software breakpoints, two hardware breakpoint slots, and instruction stepping. Watchpoints are not advertised.

Most of the work described above exists so these familiar commands have the expected meaning.

Conclusion

A debugger needs a stable stopped-machine interface. GDB owns symbols and source-level interpretation. Hyperlight exposes registers, execution control, and stop reasons. Each backend translates those operations into its hypervisor’s representation.

On ARM64, the difficult part is not encoding BRK. It is preserving the interrupted context while an exception handler and hypervisor carry the event back to the host. Once the backend presents the correct PC and processor state, ordinary GDB commands retain their ordinary meaning.

Part 2 follows the other half of the interface: translating debugger memory requests and making a patched instruction visible to the guest processor.