Single-stepping with QEMU and GDB

From Compilers
Jump to: navigation, search

It's possible to put the ARM emulator qemu-arm into a mode where it accepts commands from an instance of the Gnu debugger gdb running on the host machine. This makes it possible to run an ARM program written in assembly language – such as the output of the Lab 4 compiler – and trace its action one instruction at a time. I tried this experiment on a machine running Debian Stretch, but other Linux variants ought to work much the same.


  1. Install the needed packages with sudo apt-get install qemu-user gdb-multiarch gcc-arm-linux-gnueabihf. (Probably you've got some of those installed already.)
  2. In the lab4 directory, edit the compile script so that the last line reads (note the -g flag)
    ./ppc $* >b.s && $ARMGCC -g b.s pas0.o -static -o b.out
  3. Run a command such as ./compile -O test/mult.p to compile a test program.
  4. Now start two terminal windows. In one of them, start qemu-arm with the command
    $ qemu-arm -g 1234 ./b.out
  5. In the other terminal window, start gdb with the command
    $ gdb-multiarch ./b.out
  6. Connect to the QEMU session with the command
    (gdb) target remote localhost:1234
  7. Use this command to activate GDB's display of source and registers
    (gdb) tui reg general
  8. Skip to the start of your code with
    (gdb) advance pmain
  9. At this point, you can use the command step to execute your program one instruction at a time, and watch the register contents as you do so. It helps to enlarge the terminal window so that all the registers are visible. Once you have given the step command once, just pressing <Return> will step repeatedly.
  10. When boredom overcomes you, use quit to quit.


  • Single stepping will skip over library routines such as print_num that are written in C, unless you also recompile pas0.c with the -g flag.
  • All the power of GDB is available to set breakpoints, examine and even modify memory, redirect the flow of control, etc. Don't get carried away!