GDB 和 QEMU 调试 ARM 汇编

1 篇文章 0 订阅
1 篇文章 0 订阅

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
# ...
  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值