GDB 和 QEMU 调试 ARM 汇编
安装 GDB
下载
https://sourceware.org/pub/gdb/releases/
编译&安装
# 配置
./configure --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --with-expat --prefix=/usr/arm-linux-gnueabi-gdb-12.1
# --target :目标架构,例如 arm-linux-gnueabi(特定架构),或 all(所有架构)
# --with-expat : 支持解析XML,否则在远程调试或 qemu 调试时,有可能会出现: gdb XML support was disabled at compile time,Remote 'g' packet reply is too long
# --program-prefix :GDB 二进制程序前缀,例如:arm-linux-gnueabi-gdb
# --prefix :安装路径前缀
# 编译
make
# 安装
make install
查看GDB编译选项
# gdb --configuration
This GDB was configured as follows:
configure --host=aarch64-linux-gnu --target=aarch64-linux-gnu
--with-auto-load-dir=$debugdir:$datadir/auto-load
--with-auto-load-safe-path=$debugdir:$datadir/auto-load
--with-expat
--with-gdb-datadir=/usr/share/gdb (relocatable)
--with-jit-reader-dir=/usr/lib/gdb (relocatable)
--without-libunwind-ia64
--with-lzma
--with-babeltrace
--without-intel-pt
--with-mpfr
--with-xxhash
--with-python=/usr (relocatable)
--with-python-libdir=/usr/lib (relocatable)
--with-debuginfod
--without-guile
--enable-source-highlight
--enable-threading
--with-separate-debug-dir=/usr/lib/debug (relocatable)
--with-system-gdbinit=/etc/gdb/gdbinit
--with-system-gdbinit-dir=/etc/gdb/gdbinit.d
("Relocatable" means the directory can be moved with the GDB installation
tree, and GDB will still find it.)
常见问题
无法解析XML
gdb XML support was disabled at compile time,Remote 'g' packet reply is too long
加上 --with-expat
选项重新编译
./configure --with-expat
重新编译有可能会出现,找不到 expat
库
configure: error: expat is missing or unusable
安装 expat
库
sudo apt-get install libexpat1-dev
如果还是报错
configure: error: expat is missing or unusable
尝试换个更新的版本的GDB
目标版本不匹配
(gdb) b 4
Breakpoint 1 at 0x0: file test.S, line 4.
(gdb) r
Starting program: /root/study/assembly/test.elf
warning: Selected architecture arm is not compatible with reported target architecture aarch64
warning: Architecture rejected target-supplied description
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x0
(gdb)
加上 --target=<目标架构>
或 --target=all
选项重新编译,例如:
./configure --target=arm-linux-gnueabi
安装 QEMU
sudo apt install qemu-system-arm
示例
test.S
.global _start
_start:
mov r1,#0x80000
mov r3,#16
ldr r0,[r1]
ldr r2, [r1,#8]
ldr r4, [r1,r3]
Makefile
ARMGNU ?= arm-linux-gnueabi
test.elf: test.o
$(ARMGNU)-ld -Ttext 0x0 -o test.elf $^
%.o : %.S
$(ARMGNU)-gcc -o $@ $< -g -c
clean:
rm *.o *.elf -f
调试
启动 QEMU
qemu-system-arm -machine vexpress-a9 -nographic -kernel test.elf -S -s
qemu-system-arm
:启动QEMU系统仿真器。-machine vexpress-a9
:指定要模拟的ARM计算机型号为vexpress-a9。-nographic
:表示以无图形界面模式运行QEMU。-kernel test.elf
:指定要加载的内核文件为test.elf
。-S
:在启动时暂停模拟器的执行,等待GDB连接。-s
:在端口1234
上启动GDB服务器,以便远程GDB连接进行调试。
启动 GDB
gdb test.elf
连接本机 QEMU 的 GDB 服务器
target remote :1234 # target remote 127.0.0.1:1234
# (gdb) target remote :1234
# Remote debugging using :1234
# _start () at test.S:3
# 3 mov r1,#0x80000
# (gdb) s
# _start () at test.S:4
# 4 mov r3,#16
# (gdb) info registers
# r0 0x0 0
# r1 0x80000 524288
# r2 0x0 0
# r3 0x0 0
# r4 0x0 0
# r5 0x0 0
# r6 0x0 0
# r7 0x0 0
# r8 0x0 0
# r9 0x0 0
# r10 0x0 0
# r11 0x0 0
# r12 0x0 0
# ...