CLOSE

Debugging a bootloader can be tricky because it runs in a bare-metal environment however there are some common methods to do it effectively:

1 Use QEMU with GDB for Debugging

QEMU provides an easy way to run and debug the bootloader with GDB (GNU Debugger).

Step 1: Start QEMU with GDB Stub

Run QEMU with the -s -S options:

qemu-system-i386 -drive format=raw,file=bootloader.bin -s -S
  • -s: Starts a GDB server at port 1234.
  • -S: Stops the CPU at startup, allowing you to connect GDB.

Step 2: Start GDB

In another terminal, start GDB and load the bootloader's symbols:

gdb

Inside GDB:

target remote localhost:1234
set architecture i8086
break *0x7c00   # Break at bootloader entry point
continue

This allows us to step through the code, set breakpoint, and inspect registers.

2 Debug with Serial Output

If using a real machine or emulator, we can print debug messages to the serial port.

Step 1: Write Debug Output to COM1

Modify the bootloader to send messages to the serial port (COM1):

debug_msg:
    mov dx, 0x3F8   ; COM1
    mov al, 'H'
    out dx, al
    ret

Step 2: Capture Serial Output in QEMU

qemu-system-i386 -drive format=raw,file=bootloader.bin -serial stdio

This will print serial output to the terminal.