Monday, March 5, 2012

QEMU: Debugging your program

Lesson Number 2:
=============
This lesson assumes that reader is done with first lesson and has compiled and run HelloWorld application on QEMU's ARM emulation.

Debugging your program:
=================

To debug your program, you first need to build your program with debug information. For this gcc has -g switch. So your HelloWorld program needs rebuilding with -g compiler switch. Complete command will be:

arm-none-eabi-gcc -g3 -o HelloWorldARM HelloWorld.c -T {TOOLCHAIN_INSTALLATION}/arm-none-eabi/lib/generic-hosted.ld 


Running Program in QEMU with GDBStub:
===========================

Next step is to run this new HelloWorld ELF file on QEMU but we need to enable GDB stub in QEMU and we need to tell it to wait for GDB connection before execution of program. For this QEMU provides two switches
  • -s : QEMU will listen on port localhost:1234 for a connection by GDB
  • -S : QEMU will start as if you set a breakpoint at time zero, and you will need to use the GDB command "continue" to actually begin the simulation.
so command to launch QEMU will be:

 ./arm-softmmu/qemu-system-arm -kernel {PATH_OF_ YOUR_HELLOWORLD_ELF}-semihosting -nographic -s -S




Debug Session with Command-line GDB:
==========================

1) Go to your Sourcery toolchain for arm-none-eabi installation directory. It will be [Toolchain_installation_directory]/bin
2) Start GDB debug session using these commands:

  • ./arm-none-eabi-gdb [PATH_OF_YOUR_PROGRAM_WITH_DEBUG_INFO]
  • target remote localhost:1234
  • b main
  • c 
First command will start gdb and it will print details of GDB and a message of symbol loading.
Second command will ask GDB to connect to GDB server listening on port 1234 of localhost.
Third command will place a breakpoint at main function.
Fourth command will resume (continue) the debug session and it will then hit the breakpoint at main function.
You can then step through the code using "n" i.e. step over or "s" i.e. step into commands at GDB terminal.

Example Command line Debugsession:
========================

taimoor@pkl-tmirza-ubuntu:~/$ ./arm-none-eabi-gdb /home/taimoor/Lab_Preparation/Qemu_Stuff/Example_Programs/helloDashT

GNU gdb (Sourcery CodeBench 2011.09-60) 7.2.50.20100908-cvs
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "--host=i686-pc-linux-gnu --target=arm-none-eabi".
For bug reporting instructions, please see:
<https://support.codesourcery.com/GNUToolchain/>...
Reading symbols from /home/taimoor/Personal/@UCP/Lab_Preparation/Qemu_Stuff/Example_Programs/helloDashT...done.
GDB can't read core files on this machine.

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
__cs3_reset_generic () at /opt/codesourcery/arm-none-eabi/lib/src/generic-reset.S:34
34        b    __cs3_start_asm_sim

(gdb) b main
Breakpoint 1 at 0x274: file HelloWorld.c, line 5.

(gdb) c
Continuing.

Breakpoint 1, main () at HelloWorld.c:5
5        int i=0;
(gdb)


Debug Session with CDT/Cross GDB:
=========================

1) Start CDT
2) Go to Run->Debug Configurations...
3) Create a new debug configuration of type "C/C++ Remote Application"
4) Click Browse Button adjacent to C/C++ Application box and select your HelloWorld Application.
5) No need to specify any project.
6) Click New button next to Connection combo box and create a Local connection. Give any name to connection e.g. I used "Test" and Finish that dialog.

Your configuration should look like this:



7) Go to Debugger tab of your configuration.
8) Click Browse button next to GDB Debugger box and select "arm-none-eabi-gdb" from your toolchain's bin directory.
9) Click Apply to save your configuration and then click Debug button.
10) Your debug session will start and breakpoint in main will get hit.
11) You can step through the code using F6.

Example CDT Debug session:
===================


I modified my HelloWorld program and added a loop in it.

Enjoy Cross Debugging.

1 comment:

  1. An interesting discussion is worth comment. I think that you ought to publish more about this subject matter, it may not be a taboo subject but usually people don't speak about such topics. To the next! Best wishes!!

    ReplyDelete