11

I'm trying to learn about ARM assembler programming using the GNU assembler. I've setup my PC with QEmu and have a Debian ARM-HF chroot environment.

If I assemble and link my test program:

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

with:

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

Then load the file into gdb and set a breakpoint on _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)

How do I single step the code, display the assembler source code and monitor the registers? I tried some basic commands and they did not work:

(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) 
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
fred basset
  • 9,774
  • 28
  • 88
  • 138

4 Answers4

16

Your problem here is that you're trying to run an ARM gdb under QEMU's user-mode emulation. QEMU doesn't support the ptrace syscall (that's what syscall number 26 is), so this is never going to work.

What you need to do is run your test binary under QEMU with the QEMU options to enable QEMU's own builtin gdb stub which will listen on a TCP port. Then you can run a gdb compiled to run on your host system but with support for ARM targets, and tell that to connect to the TCP port.

(Emulating ptrace within QEMU is technically very tricky, and it would not provide much extra functionality that you can't already achieve via the QEMU builtin gdbstub. It's very unlikely it'll ever be implemented.)

Peter Maydell
  • 9,707
  • 1
  • 19
  • 25
  • A discussion of this is also here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=832710 – Serge Rogatch Nov 03 '16 at 19:39
  • related: walkthrough of actually setting up QEMU + GDB cross-debugging on an x86-64 Linux host: [How to run a single line of assembly, then see \[R1\] and condition flags](https://stackoverflow.com/q/39503997) – Peter Cordes Jul 05 '18 at 07:05
11

Minimal working QEMU user mode example

I was missing the -fno-pie -no-pie options:

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

On another terminal:

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'
;

This leaves us at main, in a split code / disassembly view due to layout split. You will also interested in:

layout regs

which shows the registers.

At the end of the day however, GDB Dashboard is more flexible and reliable: gdb split view with code

-fno-pie -no-pie is required because the packaged Ubuntu GCC uses -fpie -pie by default, and those fail due to a QEMU bug: How to GDB step debug a dynamically linked executable in QEMU user mode?

There was no gdbserver --multi-like functionality for the QEMU GDB stub on QEMU 2.11: How to restart QEMU user mode programs from the GDB stub as in gdbserver --multi?

For those learning ARM assembly, I am starting some runnable examples with assertions and using the C standard library for IO at: https://github.com/cirosantilli/arm-assembly-cheat

Tested on Ubuntu 18.04, gdb-multiarch 8.1, gcc-arm-linux-gnueabihf 7.3.0, qemu-user 2.11.

Freestanding QEMU user mode example

This analogous procedure also works on an ARM freestanding (no standard library) example:

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

On another terminal:

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

We are now left at the first instruction of the program.

QEMU full system examples

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    My answer on [How to run a single line of assembly, then see \[R1\] and condition flags](https://stackoverflow.com/q/39503997) is another walk-through of qemu-user + gdb remote-debugging. – Peter Cordes Sep 04 '18 at 05:44
3

Single step of an assembly instruction is done with stepi. disas will disassemble around the current PC. info regi will display the current register state. There are some examples for various processors on my blog for my ELLCC cross development tool chain project.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
-2

You should add the -g option too to the assembling. Otherwise the codeline info is not included. That crash probably comes from running some garbage after the code lines. Maybe you should add the exit system call:

mov eax, 1 ; exit
mov ebx, 0 ; returm value
int 0x80 ; system call
turboscrew
  • 676
  • 4
  • 13
  • 3
    What does debug info have to do with assembly language level? What does your x86 assembly code have to do with ARM? – Ruslan Aug 17 '17 at 16:40