Reference

https://sourceware.org/gdb/current/onlinedocs/gdb/
https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf

Connect to QEMU Target

-s : shorthand for -gdb tcp::1234
-S : freeze CPU at startup (use ‘c’ to start execution)

ARM64 Example

  • Boot Linux Image with parameter “-s -S”

qemu-system-aarch64 -machine virt,gic_version=3 \
-machine virtualization=true -cpu cortex-a53 -machine type=virt -m 1024M -smp 2 \
-kernel ./Image_baremetal -initrd rootfs.cpio \
-append “rw root=/dev/ram rdinit=/sbin/init earlyprintk=serial,ttyAMA0 console=ttyAMA0” \
-no-reboot -nographic -s -S

  • Connect GDB to QEMU

set arch aarch64
target remote :1234
image.png

RISC-V Example

  • Boot os.bin with parameter “-s -S”

qemu-system-riscv64 -machine virt -nographic -bios ../bootloader/rustsbi-qemu.bin \
-device loader,file=target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000 \
-s -S

  • Connect GDB to QEMU

set arch riscv:rv64
target remote :1234
file target/riscv64gc-unknown-none-elf/release/os
image.png

GDB vs QEMU Monitor

GDB 优势(QEMU Monitor劣势):可以加载符号表文件, 可以查看更多寄存器(包括所有系统寄存器),可以查看调用栈(bt, info frame),可以加断点。
GDB 劣势(QEMU Monitor 优势):GDB 不能查看物理地址!

Commands

info registers Print the names and values of all registers except floating-point and vector registers
info reg $HCR_EL2 Print the value of each specified register regname.
x /10i $pc print the instruction to be executed next with
info thread print threads
thread 2 switch to thread 2
info frame all about the selected stack frame
file target/riscv64gc-unknown-none-elf/release/os Load symble files