Using GDB Server for Debugging

The GDB server utility provided with Sourcery G++ Lite can be used to debug a GNU/Linux application. While Sourcery G++ runs on your host system, gdbserver and the target application run on your target system. Even though Sourcery G++ and your application run on different systems, the debugging experience when using gdbserver is very similar to debugging a native application.

Running GDB Server

The GDB server executables are included in the sysroot in ABI-specific subdirectories of sysroot/usr. Use the executable from the sysroot that matches your program. See the section called “Library Configurations” for details.

You must copy the sysroot to your target system as described in the section called “Installing the Sysroot”. You must also copy the executable you want to debug to your target system.

If you have installed the sysroot in the root directory of the filesystem on the target, you can invoke gdbserver as:

> gdbserver :10000 program arg1 arg2 ...

where program is the path to the program you want to debug and arg1 arg2 ... are the arguments you want to pass to it. The :10000 argument indicates that gdbserver should listen for connections from GDB on port 10000. You can use a different port, if you prefer.

If you have installed the sysroot in an alternate directory, invoking gdbserver becomes more complicated. You must build your application using the link-time options to specify the location of the sysroot, as described in the section called “Using Linker Options to Specify the Sysroot Location”. You must also invoke gdbserver itself using the dynamic linker provided in the Sourcery G++ sysroot, as described in the section called “Specifying the Sysroot Location at Runtime”. In other words, the command to invoke gdbserver in this case would be similar to:

> sysroot/lib/ld.so.1 \
  --library-path sysroot/lib:sysroot/usr/lib \
  sysroot/usr/lib/bin/gdbserver :10000 program arg1 arg2 ...

Connecting to GDB Server from the Debugger

You can connect to GDB server by using the following command from within GDB:

(gdb) target remote target:10000

where target is the host name or IP address of your target system.

When your program exits, gdbserver exits too. If you want to debug the program again, you must restart gdbserver on the target. Then, in GDB, reissue the target command shown above.

Setting the Sysroot in the Debugger

If you have installed the sysroot in the root filesystem on the target, as described in the section called “Installing the Sysroot”, you can enable debugging of shared libraries and support for multi-threaded debugging by using the set sysroot GDB command:

(gdb) set sysroot pathname

The pathname is the pathname to a copy of the sysroot on the host, or the unstripped original sysroot files included with your Sourcery G++ Lite distribution if you have installed a stripped copy on the target.

The pathname is used as a prefix for all file names GDB reads from your target. If you installed the sysroot in an alternate location on the target, use set sysroot to point to the root of your target's filesystem. For instance, if the Sourcery G++ libraries are located in /opt/codesourcery on your target and /data/software/opt/codesourcery on your host, use set sysroot /data/software. If the Sourcery G++ libraries are located at the same path on both host and target, you do not need set sysroot.