如何在QEMU上的GDB中单步ARM组装?

时间:2013-12-15 01:09:54

标签: linux assembly gdb arm qemu

我正在尝试使用GNU汇编程序学习ARM汇编程序编程。我用QEmu设置了我的PC,并且有一个Debian ARM-HF chroot环境。

如果我汇编并链接我的测试程序:

.text
.global _start
_start:
        mov     r0, #6
        bx      lr

使用:

as test.s -o test.o
ld test.o -o test

然后将文件加载到gdb并在_start上设置断点:

root@Latitude-E6420:/root# gdb test
GNU gdb (GDB) 7.6.1 (Debian 7.6.1-1)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
For bug reporting instructions, please see:
...
Reading symbols from /root/test...(no debugging symbols found)...done.
(gdb) break _start
Breakpoint 1 at 0x8054
(gdb)

如何单步执行代码,显示汇编程序源代码并监视寄存器? 我尝试了一些基本的命令而且它们不起作用:

(gdb) break _start
Breakpoint 1 at 0x8054
(gdb) info regi
The program has no registers now.
(gdb) stepi
The program is not being run.
(gdb) disas
No frame selected.
(gdb) r
Starting program: /root/test 
qemu: Unsupported syscall: 26
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
qemu: Unsupported syscall: 26
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) 

4 个答案:

答案 0 :(得分:10)

您的问题是您尝试在QEMU的用户模式仿真下运行ARM gdb。 QEMU不支持ptrace系统调用(这就是系统调用号26),所以这永远不会起作用。

您需要做的是在QEMU下使用QEMU选项运行测试二进制文件,以启用QEMU自己的内置gdb存根,该存根将侦听TCP端口。然后,您可以运行编译为在主机系统上运行但支持ARM目标的gdb,并告诉它连接到TCP端口。

(在QEMU中模拟ptrace在技术上非常棘手,它不会提供很多你无法通过QEMU内置gdbstub实现的额外功能。它不太可能实现。)

答案 1 :(得分:3)

最小化QEMU用户模式示例

我错过了-fno-pie -no-pie选项:

sudo apt-get install gdb-multiarch gcc-arm-linux-gnueabihf qemu-user
printf '
#include <stdio.h>
#include <stdlib.h>

int main() {
    puts("hello world");
    return EXIT_SUCCESS;
}
' >  hello_world.c
arm-linux-gnueabihf-gcc -fno-pie -ggdb3 -no-pie -o hello_world hello_world.c
qemu-arm -L /usr/arm-linux-gnueabihf -g 1234 ./hello_world

在另一个终端上:

gdb-multiarch -q --nh \
  -ex 'set architecture arm' \
  -ex 'set sysroot /usr/arm-linux-gnueabihf' \
  -ex 'file hello_world' \
  -ex 'target remote localhost:1234' \
  -ex 'break main' \
  -ex continue \
  -ex 'layout split'
;

由于main,这使我们进入layout split的拆分代码/反汇编视图中。您还将对以下内容感兴趣:

layout regs

显示寄存器。

但是,归根结底,GDB仪表板更加灵活和可靠:gdb split view with code

-fno-pie -no-pie是必需的,因为默认情况下打包的Ubuntu GCC使用-fpie -pie,并且由于QEMU错误而失败:How to GDB step debug a dynamically linked executable in QEMU user mode?

QEMU 2.11上的QEMU GDB存根没有类似gdbserver --multi的功能:How to restart QEMU user mode programs from the GDB stub as in gdbserver --multi?

对于那些学习ARM汇编程序的人,我将通过断言开始一些可运行的示例,并在以下位置使用IO的C标准库:https://github.com/cirosantilli/arm-assembly-cheat

在Ubuntu 18.04,gdb-multiarch 8.1,gcc-arm-linux-gnueabihf 7.3.0,qemu-user 2.11上进行了测试。

独立的QEMU用户模式示例

此类似过程也可用于ARM独立式(无标准库)示例:

printf '
.data
    msg:
        .ascii "hello world\\n"
    len = . - msg
.text
.global _start
_start:
    /* write syscall */
    mov r0, #1     /* stdout */
    ldr r1, =msg   /* buffer */
    ldr r2, =len   /* len */
    mov r7, #4     /* Syscall ID. */
    swi #0

    /* exit syscall */
    mov r0, #0 /* Status. */
    mov r7, #1 /* Syscall ID. */
    swi #0
' >  hello_world.S
arm-linux-gnueabihf-gcc -ggdb3 -nostdlib -o hello_world -static hello_world.S
qemu-arm -g 1234 ./hello_world

在另一个终端上:

gdb-multiarch -q --nh \
  -ex 'set architecture arm' \
  -ex 'file hello_world' \
  -ex 'target remote localhost:1234' \
  -ex 'layout split' \
;

我们现在留在了程序的第一条指令上。

QEMU完整系统示例

答案 2 :(得分:2)

汇编指令的单步执行 stepi disas 将在当前的PC周围进行反汇编。 info regi 将显示当前的注册状态。我的博客上为我的ELLCC交叉开发工具链项目提供了各种处理器的示例。

答案 3 :(得分:0)

您也应该为组装添加-g选项。否则,不包括代码行信息。 崩溃可能来自于在代码行之后运行一些垃圾。 也许你应该添加退出系统调用:

mov eax, 1 ; exit
mov ebx, 0 ; returm value
int 0x80 ; system call