To set up a cross debug session using GDB, it is necessary to run debugging tools on both the target Linux system and the host system.
On the target Linux system the GDB debug server, gdbserver needs to be told which port to use and which application to debug. Run gdbserver with the command:
gdbserver localhost:<port> <application>
where <port>; is the port number to use (chose a port that does not conflict with any other ports in use) and <application> is the application to be debugged. For example, to debug /root/hello using port 3278:
target% gdbserver localhost:3278 /root/hello Process application created: pid = 184 Listening on port 3278
Note: To support symbolic debugging the application must have been compiled with the -g flag. This causes DWARF debugging information to be included in the executable. See the GDB documentation for details.
On the host, run the appropriate debugger, for example sh4-linux-gdb, giving the name of the executable to be debugged as an argument. This is so that GDB can access its debug information.
When the host debugger is running, connect to gdbserver using the target remote command:
(gdb) target remote <targetip>:<port>
In this command <targetip> specifies the target name or IP address, and <port> is the port number gdbserver is using.
Using the example above, and supposing the target is 192.168.1.2:
host% sh4-linux-gdb /opt/STM/STLinux2.3/devkit/sh4/target/root/hello GNU gdb STMicroelectronics/Linux Base 6.5-32 [build Jul 22 2008] Copyright (C) 2006 Free Software Foundation, Inc. <snip> This GDB was configured as "--host=i686-pc-linux-gnu --target=sh4-linux"... (gdb) target remote 192.168.1.2:3278 Remote debugging using 192.168.1.2:3278 0x29558080 in ??() (gdb)
Initially the program is stopped at its entry point in the C runtime, so the first step is to run to main:
(gdb) break main Breakpoint 1 at 0x400656: file main.c, line 20. (gdb) continue Continuing. Breakpoint 1, main() at main.c:20 20 printf("Welcome to the application\n"); (gdb)
Note: To avoid having to enter these commands manually whenever GDB is invoked, they can be automated by creating a GDB startup script file. When GDB starts, it looks for a file named .shgdbinit in the home directory of the user, and in the current directory, and (if found) executes it. The user can specify an alternative startup script to be run, by starting GDB with the command line option --command=<script>.
The target can now be debugged in the same way as a native application. For more information on how to use GDB, see the GDB documentation. GDB also contains extensive built-in help, accessable with the help command.