Module 10 · Lesson 9 of 112

Debugging the boot in GDB

Pause QEMU at reset, inspect the OpenSBI handoff registers, and follow the kernel from its assembly entry into Rust.

  • Beginner to advanced
  • 9 min read
  • 15 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM, qemu-system-riscv64, gdb-multiarch, and the kernel ELF and launch command from the banner lesson

Your place in the module
By the end you can 4 objectives
  • Attach a RISC-V-capable GDB to a paused QEMU guest
  • Capture the hart ID and device tree pointer at the first instruction of _start
  • Trace the transfer from assembly entry to the Rust main routine
  • Identify the host terminal that receives guest UART output
Video coming soonDebugging the boot in GDB
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from The first printed banner
  • The first kernel-owned character demonstrates that the kernel reached its output call
  • _start supplies a 16-byte-aligned stack before ordinary Rust code executes
  • The legacy SBI console putchar call uses a7 for the call ID and a0 for the byte
01

Stop QEMU before OpenSBI runs

3 min read

The last lesson printed the first banner. Now you attach a debugger to watch entry, contrast this firmware handoff with xv6's own M-mode entry, and see where the serial console actually goes.

A blank terminal during boot leaves several possibilities open. The firmware might still be running, the kernel might be stuck in its entry code, or the Rust routine might have run without printing anything. Linux documents a RISC-V entry convention in which two registers describe the boot hart and the location of the device tree. By stopping on the first instruction of your kernel and reading those registers, you can observe the handoff before your own code modifies them. RISC-V kernel boot requirements

Use two terminals in Ubuntu. In the first terminal, take the QEMU command that printed your banner and append -s -S. Keep its machine, firmware, kernel, and console options. The example below shows the command shape for the virt machine with QEMU's default OpenSBI firmware. Replace ./kernel.elf with the path to the ELF you actually booted.

bash
qemu-system-riscv64 -machine virt -bios default \
  -kernel ./kernel.elf -nographic -s -S

The lowercase -s starts QEMU's GDB stub on TCP port 1234. The stub accepts debugger requests to read guest registers, examine memory, set breakpoints, and control execution. The uppercase -S leaves the guest CPUs stopped at startup. With both switches present, the first terminal can sit quietly while QEMU waits for the debugger. Keep the guest on this machine for the lab.

In the second terminal, start the RISC-V-capable debugger, gdb-multiarch. Give it the same kernel ELF so that GDB can associate guest addresses with symbols and, when the ELF contains debug information, source lines. The ./kernel.elf path is relative to this terminal's working directory.

bash
gdb-multiarch ./kernel.elf

At the GDB prompt, enter:

text
target remote :1234
break _start
info breakpoints

target remote :1234 connects to the local QEMU stub. The program counter at attachment can be in the guest's reset code. GDB can still resolve _start from the kernel ELF because it has the symbol address before the guest reaches it.

For the register capture, verify that break _start resolved to the very first instruction. A function breakpoint can be placed after its prologue when GDB has source information. Compare the address in info breakpoints with the entry address shown by info address _start or the first address in the following disassembly:

text
info address _start
x/3i &_start

If the addresses differ, delete the breakpoint using the number shown by info breakpoints, then set break *_start to stop at the exact symbol address. If GDB cannot resolve the symbol at all, check which executable it opened and whether that ELF still contains the entry symbol. If the connection itself fails, check that QEMU is still running and started with -s. A QEMU startup error appears in the first terminal.

text
continue

continue lets the guest run through reset and OpenSBI. At the breakpoint, GDB returns to its prompt with the CPU stopped at _start. Each fresh run with -S gives you the chance to set that breakpoint before any guest instruction executes.

From reset to the kernel breakpointQEMU startsGuest CPUsstoppedGDBattachesTCP port1234Set entrybreakpointContinueguestOpenSBIrunsStop at_startQEMU startsGuest CPUsstoppedGDBattachesTCP port1234Set entrybreakpointContinueguestOpenSBIrunsStop at_start
GDB attaches while the guest is stopped, then resumes it to the kernel entry breakpoint
02

Read the handoff and step into Rust

5 min read

At _start, inspect the current instruction and dump the handoff registers before stepping. The program counter, $pc, identifies the instruction the CPU is about to execute. x/i shows the instruction at that address.

text
x/i $pc
info registers a0 a1
p/u $a0
p/x $a1
set $boot_hart = $a0
set $boot_fdt = $a1

a0 contains the boot hart ID. A is the RISC-V unit identified by that number. a1 contains the address of the . Write down the actual values from your run. p/u formats the hart ID as an unsigned decimal number and p/x formats the address in hexadecimal. $boot_hart and $boot_fdt are GDB convenience variables. They hold copies for later inspection even if the guest reuses a0 or a1.

You can also read the first four bytes at the saved pointer:

text
x/4xb $boot_fdt

The FDT header starts with the bytes d0 0d fe ed. This is a format signature, and the address at which you see it comes from your run. Reading individual bytes shows the order in memory. Printing $boot_fdt alone gives you the pointer value. Examining memory at $boot_fdt gives you the bytes stored at that location.

In M3, xv6's RISC-V boot path uses its own entry.S and start.c. The assembly begins in M-mode, sets up a stack, and enters the C startup routine. That routine prepares the privilege transition and uses mret to enter xv6's main in S-mode. In this kernel, OpenSBI replaces xv6's embedded M-mode startup. Your _start runs after OpenSBI transfers control to S-mode, and your entry code handles the setup needed to call Rust. Providing an SBI implementation as firmware is a modern RISC-V boot arrangement. Linux on RISC-V also boots with OpenSBI as its M-mode firmware, for example on QEMU virt.

Firmware handoff to RustOpenSBIRuns inM-mode_starta0 hart ID,a1 FDTpointerSet stackpointerKernel runsin S-modeRust mainOpenSBIRuns inM-mode_starta0 hart ID,a1 FDTpointerSet stackpointerKernel runsin S-modeRust main
OpenSBI supplies a0 and a1 at _start, and the kernel entry code transfers control to Rust

Now step through the entry code at instruction granularity. Use display/i $pc to have GDB show the current instruction each time execution stops. Use disassemble _start to see the linked instructions and locate the transfer into Rust.

text
display/i $pc
disassemble _start
info registers sp
si

Here is a real session against the banner kernel from the previous lesson (QEMU 8.2.2, gdb-multiarch 15.1 on Ubuntu 24.04). Your addresses will match if your linker origin is 0x80200000; the symbol offsets depend on your entry code:

text
(gdb) target remote :1234
0x0000000000001000 in ?? ()
(gdb) break _start
Breakpoint 1 at 0x80200000
(gdb) continue
Breakpoint 1, 0x0000000080200000 in _start ()
(gdb) x/i $pc
=> 0x80200000 <_start>: csrci   sstatus,2
(gdb) info registers a0 a1
a0             0x0  0
a1             0x87e00000   2279604224
(gdb) set $boot_fdt = $a1
(gdb) x/4xb $boot_fdt
0x87e00000: 0xd0    0x0d    0xfe    0xed
(gdb) disassemble _start
   0x0000000080200000 <+0>: csrci   sstatus,2
   0x0000000080200004 <+4>: auipc   sp,0x4
   0x0000000080200008 <+8>: addi    sp,sp,188 # 0x802040c0
   0x000000008020000c <+12>:    auipc   t1,0x0
   0x0000000080200010 <+16>:    jr  8(t1) # 0x80200014 <main>

Three things to notice. First, the attach stops at 0x1000, the reset ROM from the earlier lesson, and continue runs OpenSBI until the breakpoint. Its banner appears in the QEMU terminal during that continue. Second, a0 = 0 is the boot hart and a1 = 0x87e00000 is the device tree, whose first four bytes are the FDT magic 0xd00dfeed. Third, the assembler expanded lla sp, __boot_stack_top into two instructions (auipc and addi) and tail main into auipc t1 plus jr 8(t1), so one source line can need several si steps. This release build has no debug information, so info line *$pc answers No line number information available; build without --release and with debug info to list and step through the Rust source.

Repeat si to execute one instruction at a time. As you pass the instruction or sequence that initializes the stack pointer, use info registers sp again to see the value your entry code established. The Rust routine can then use the stack for its compiled function code. Follow the disassembly for your actual build. An assembly source line that appears to load an address or call a function can expand into multiple machine instructions, so one si advances by one linked instruction.

When the current instruction transfers control into the Rust main routine, use si to execute it. Then run info symbol $pc to identify the destination. Use info line *$pc to ask GDB whether the destination has source line information. The Rust symbol may appear as main, or it may include a module or crate name. Use the symbol reported for your ELF. With line information available, list can show the Rust source and step can advance by source line. If line information is absent, you can still follow the instructions with si. For source stepping, repeat the run using a build with debug information and give that same ELF to both QEMU and GDB.

Compare the live a0 and a1 with the saved values if you want to see what survived the entry code. Whether Rust receives those values as arguments depends on the assembly and the Rust function signature. The register dump taken at _start is the record of the firmware handoff. At the Rust routine, the disassembly tells you how the entry code got there, and the saved copies still show what OpenSBI supplied.

03

Follow the banner to the QEMU terminal

2 min read

At a breakpoint before the banner code, the QEMU terminal may already contain OpenSBI output. OpenSBI ran before the kernel breakpoint, so its output can appear while your own banner is still pending. Leave GDB stopped at the Rust entry and look at the first terminal. Then return to GDB and enter continue. As the guest runs the banner code, the text appears in the terminal where you launched QEMU.

With the launch options used here, -nographic disables graphical display and redirects the guest serial console to QEMU's standard input and output. On virt, the serial device involved is the emulated 16550 UART at 0x10000000. OpenSBI v1.3 confirms it in its own banner with the line Platform Console Device : uart8250. Even when the kernel asks OpenSBI to emit a character, the console output on this machine goes through that emulated serial device. QEMU then delivers the bytes to the host terminal. In a graphical configuration, display output and serial output can be presented through different windows or backends. The serial destination depends on the launch options.

Serial output in the host terminalGuestbannerEmulated16550QEMU stdioQEMUterminalGuestbannerEmulated16550QEMU stdioQEMUterminal
The emulated 16550 sends the guest banner to the host terminal running QEMU

GDB continues to show its own prompts, register dumps, and disassembly in the second terminal. Those are debugger messages exchanged with QEMU over the GDB connection. The banner arrives by the serial route. If continue leaves the GDB terminal waiting while the banner is visible in QEMU, the guest may simply be executing its idle code. Press Ctrl-C in the GDB terminal to interrupt the guest and recover the debugger prompt. Use x/i $pc to see where it stopped. To repeat the run from reset, quit GDB, exit QEMU with Ctrl-a followed by x in the QEMU terminal with the default -nographic multiplexer, and launch again with -s -S.

If the kernel banner stays absent, use the last breakpoint you reached to decide what to inspect. Reaching _start establishes that the firmware transferred control to your entry address. Reaching Rust establishes that the assembly reached its destination. If both happened, resume execution through the actual print code and watch the QEMU terminal. These observations let you locate the point at which the boot stopped progressing before you change any code.

The next lesson, The 16550 UART registers (M10-S02A), examines the device behind that terminal output and begins the full UART driver work. You will see how the kernel can send bytes to the same emulated serial device directly.

Key takeaways

  • QEMU's -s enables the GDB stub on TCP port 1234 and -S keeps the guest CPUs stopped at startup
  • OpenSBI passes the boot hart ID in a0 and the FDT address in a1 at kernel entry
  • The course kernel enters in S-mode through OpenSBI, while xv6 supplies its own M-mode startup
  • With the course launch options, -nographic sends the emulated serial console to the QEMU terminal
Reflect
  1. Explain to a colleague, in two sentences: attach a RISC-V-capable GDB to a paused QEMU guest
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close