Debugging a Linux Application with MontaVista 5.0

From Texas Instruments Wiki
Jump to: navigation, search

DVSDK provided filesystem with MontaVista 5.0 (MVL 5.0) does not contain GDB and gdbserver for debugging ARM application. This page will guide you through the steps for cross compiling gdb/gdbserver and finally a example commands that can used for debugging a simple application.

What is GDB

GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

The program being debugged can be written in Ada, C, C++, Objective-C, Pascal (and many other languages). Those programs might be executing on the same machine as GDB (native) or on another machine (remote). GDB can run on most popular UNIX and Microsoft Windows variants.


What is gdbserver

GDBserver, a program that lets you run GDB on a different machine than the one on which your program is running. The advantage of using GDBserver is that it needs just a fraction of the target resources that GDB consumes, because it implements only the low-level functionality of the debugger -- namely setting breakpoints and accessing the target processor registers and read/write application memory. GDBserver takes control of the application being debugged, then waits for instructions from a remote instance of GDB running on a development workstation.

Cross compiling gdb and gdbserver

  • Configure and cross compile gdb, note that the prefix value used in the configure commands below should reflect the locations you want to use in your actual install, the paths shown here are based on the getting started guide.
 host# cd /home/user/workdir/filesys
 host# tar zxvf gdb-6.6.tar.gz 
 host# cd gdb-6.6
 host# ./configure --host=i686-pc-linux-gnu --target=armv5tl-montavista-linux-gnueabi --prefix=/opt/mv_pro_5.0/montavista/pro/devkit/arm/v5t_le

Note that the install command is expecting to be able to write to your MontaVista installation directory, you may have to change permissions on the install directory if you are building this as a user that cannot write to /opt.

 host# make && make install
  • Configure and cross compile gdbserver
 host# cd gdb/gdbserver
 host# CC=/opt/mv_pro_5.0/montavista/pro/devkit/arm/v5t_le/bin/arm_v5t_le-gcc ./configure  --host=armv5tl-montavista-linux-gnueabi --prefix=/home/user/workdir/filesys

Note that the install command is expecting to be able to write to your NFS mounted filesystem, you may have to change the permissions of your filesystem to allow your user rights to write to the filesystem directories (namely /home/user/workdir/filesys/bin).

 host# make && make install

Assumption

  • Montavista toolchain is installed at /opt/mv_pro_5.0/montavista/pro/devkit/arm/v5t_le/bin
  • Filesystem is installed at /home/user/workdir/filesys

Debugging application using gdb and gdbserver

Compile and build application

Compile and build application using "-g" option.

  • Use the below simple example to proof concept
host# mkdir -p /home/user/targetfs/opt/sample
host# cd /home/user/targetfs/opt/sample
host# cat sample.c 
#include <stdio.h>
int main (int argc, char **argv) {
  int x, y;
  x = 20, y = 10;
  printf("%d+%d=%d\n", x, y, x+y);
  /* Set the break point here and change value of x from gdb */
  printf("%d*%d=%d\n", x, y, x*y);
   /* Set the break point here and change value of y from gdb */
  printf("%d/%d=%d\n", x, y, x/y);
  return 0;
}
  • Compile application using
host# arm_v5t_le-gcc -g sample.c -o sample

Debugging

host IP address: 192.168.100.1 target IP address: 192.168.100.2

  • Run gdbserver on target
target# cd /opt/sample
target# gdbserver 192.168.100.1:1000 ./sample
 Process ./sample created; pid = 733
 Listening on port 1000

At this point of time target is waiting for connection from the host

  • Run gdb on host.
 host# /home/user/targetfs/opt/sample
 host# /opt/mv_pro_5.0/montavista/pro/devkit/arm/v5t_le/bin/armv5tl-montavista-linux-gnueabi-gdb ./sample
 GNU gdb 6.6
 Copyright (C) 2006 Free Software Foundation, Inc.
 GDB is free software, covered by the GNU General Public License, and you are
 welcome to change it and/or distribute copies of it under certain conditions.
 Type "show copying" to see the conditions.
 There is absolutely no warranty for GDB.  Type "show warranty" for details.
 This GDB was configured as "--host=i686-pc-linux-gnu --target=armv5tl-montavista-linux-gnueabi"...
 (gdb) set solib-search-path /home/user/workdir/filesys/lib
 (gdb) target remote 192.168.100.2:1000
 Remote debugging using 192.68.100.1:1000
 0x400007e0 in _start ()
 from /home/user/targetfs/lib/ld-linux.so.3
 (gdb)

At this point host is successfully connected to target gdbserver and now you can use gdb command to run the application.

For more information on gdb commands refer GNU GDB Debugger Command

Also see Debugging remotely on DaVinci using gdb for another example.