gdbserver - Unix, Linux Command



NAME

gdbserver - Remote Server for the GNU Debugger
TagDescription
gdbserver
  tty prog [args...]
gdbserver tty --attach PID

DESCRIPTION

GDBSERVER is a program that allows you to run GDB on a different machine than the one which is running the program being debugged.

Usage (server (target) side):

First, you need to have a copy of the program you want to debug put onto the target system. The program can be stripped to save space if needed, as GDBserver doesn’t care about symbols. All symbol handling is taken care of by the GDB running on the host system.

To use the server, you log on to the target system, and run the ‘gdbserver’ program. You must tell it (a) how to communicate with GDB, (b) the name of your program, and (c) its arguments. The general syntax is:

        target> gdbserver COMM PROGRAM [ARGS ...]

For example, using a serial port, you might say:

        target> gdbserver /dev/com1 emacs foo.txt

This tells gdbserver to debug emacs with an argument of foo.txt, and to communicate with GDB via /dev/com1. Gdbserver now waits patiently for the host GDB to communicate with it.

To use a TCP connection, you could say:

        target> gdbserver host:2345 emacs foo.txt

This says pretty much the same thing as the last example, except that we are going to communicate with the host GDB via TCP. The ‘host:2345’ argument means that we are expecting to see a TCP connection from ‘host’ to local TCP port 2345. (Currently, the ‘host’ part is ignored.) You can choose any number you want for the port number as long as it does not conflict with any existing TCP ports on the target system. This same port number must be used in the host GDBs ‘target remote’ command, which will be described shortly. Note that if you chose a port number that conflicts with another service, gdbserver will print an error message and exit.

On some targets, gdbserver can also attach to running programs. This is accomplished via the --attach argument. The syntax is:

        target> gdbserver COMM --attach PID

PID is the process ID of a currently running process. It isn’t necessary to point gdbserver at a binary for the running process.

Usage (host side):

You need an unstripped copy of the target program on your host system, since GDB needs to examine it’s symbol tables and such. Start up GDB as you normally would, with the target program as the first argument. (You may need to use the --baud option if the serial line is running at anything except 9600 baud.) Ie: ‘gdb TARGET-PROG’, or ‘gdb --baud BAUD TARGET-PROG’. After that, the only new command you need to know about is ‘target remote’. It’s argument is either a device name (usually a serial device, like ‘/dev/ttyb’), or a HOST:PORT descriptor. For example:

        (gdb) target remote /dev/ttyb

communicates with the server via serial line /dev/ttyb, and:

        (gdb) target remote the-target:2345

communicates via a TCP connection to port 2345 on host ‘the-target’, where you previously started up gdbserver with the same port number. Note that for TCP connections, you must start up gdbserver prior to using the ‘target remote’ command, otherwise you may get an error that looks something like ‘Connection refused’.

OPTIONS

You have to supply the name of the program to debug and the tty to communicate on; the remote GDB will do everything else. Any remaining arguments will be passed to the program verbatim.

EXAMPLES

Remote debugging is particularly useful in cases of embedded applications where resources are scarce. So in such cases the program can run on the target machine and you can debug it from remote host machine.

Below are steps involved in debugging with gdbserver

Step 1 - Start the gdbserver on target machine

$ gdbserver localhost:2000 gdb_example -l 20 -b 10
Process gdb_example created; pid = 4674
Listening on port 2000

Step 2 - Make sure host machine has the program binary with debugging enabled

$ gcc -o gdb_example -g gdb_example.c

Step 3 - Run gdb on host machine

$ gdb -q gdb_example
Reading symbols from /home/gdb_example...done.
(gdb)

Step 4 - Connect to the target and start debugging

(gdb) target remote 15.77.28.37:2000

Below is a sample debugging session:

Target machine:
$ gdbserver localhost:2000 gdb_example -l 20 -b 10
Process gdb_example created; pid = 4674
Listening on port 2000
Remote debugging from host 15.77.28.36
Area: 20 * 10 = 200

Child exited with status 0
GDBserver exiting

Host machine:
$ gdb -q gdb_example
Reading symbols from /home/gdb_example...done.
(gdb) target remote 15.77.28.37:2000
Remote debugging using 15.77.28.37:2000
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00ab3810 in _start () from /lib/ld-linux.so.2
(gdb) b main
Breakpoint 1 at 0x80484fd: file gdb_example.c, line 18.
(gdb) c
Continuing.

Breakpoint 1, main (argc=5, argv=0xbfff8724) at gdb_example.c:18
18              if (argc != 5) {
(gdb) n
23              while ((c = getopt (argc, argv, "l:b:")) != -1) {
(gdb) n
24                      switch( c ) {
(gdb) c
Continuing.

Program exited normally.
Advertisements