[Template fetch failed for http://spivey.oriel.ox.ac.uk/corner/Template:Sitenotice?action=render: HTTP 404]
Single-stepping with QEMU and GDB
Jump to navigation
Jump to 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.
Instructions
- 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.) - In the
lab4
directory, edit thecompile
script so that the last line reads (note the-g
flag)./ppc $* >b.s && $ARMGCC -g b.s pas0.o -static -o b.out
- Run a command such as
./compile -O test/mult.p
to compile a test program. - Now start two terminal windows. In one of them, start
qemu-arm
with the command$ qemu-arm -g 1234 ./b.out
- In the other terminal window, start
gdb
with the command$ gdb-multiarch ./b.out
- Connect to the QEMU session with the command
(gdb) target remote localhost:1234
- Use this command to activate GDB's display of source and registers
(gdb) tui reg general
- Skip to the start of your code with
(gdb) advance pmain
- 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. - When boredom overcomes you, use
quit
to quit.
Notes
- Single stepping will skip over library routines such as
print_num
that are written in C, unless you also recompilepas0.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!