Datasheet
544
Exploring the Software-Development Tools in Linux
Preparing to debug a program
If you want to debug a program by using gdb, you have to ensure that the
compiler generates and places debugging information in the executable. The
debugging information contains the names of variables in your program and
the mapping of addresses in the executable file to lines of code in the source
file. gdb needs this information to perform its functions, such as stopping
after executing a specified line of source code.
To make sure that the executable is properly prepared for debugging,
use the -g option with GCC. You can do this task by defining the variable
CFLAGS in the makefile as
CFLAGS=
-g
Running gdb
The most common way to debug a program is to run gdb by using the fol-
lowing command:
gdb progname
progname is the name of the program’s executable file. After progname
runs, gdb displays the following message and prompts you for a command:
GNU gdb 6.3
Copyright 2004 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 “i586-suse-linux”.
(gdb)
You can type gdb commands at the (gdb) prompt. One useful command,
help, displays a list of commands, as the next listing shows:
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type “help” followed by a class name for a list of commands in that class.
Type “help” followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)
42_770191-bk08ch01.indd 54442_770191-bk08ch01.indd 544 8/6/10 9:51 AM8/6/10 9:51 AM