Much of the content of this page was extracted from John W. Clark's
GDB page.
It in turn is a summary of the GNU GDB info manual, which can
be accessed by typing
info gdb
The purpose of a debugger such as GDB is to allow 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 five main kinds of things (plus other things in support of these) to help you catch bugs in the act:
In order to debug a program effectively, you need to generate debugging
information when you compile it. This debugging information is stored in the
object file; it describes the data type of each variable or function and the
correspondence between source line numbers and addresses in the executable
code. To request debugging information, specify the `-g' option when you run
the compiler; for instance,
gcc -g -o tcp_card_dealer tcp_card_dealer.c -L. -lmy_net
break FUNCTION
break LINENUM
break FILENAME:LINENUM
break FILENAME:FUNCTION
break ... if COND
watch EXPR
info breakpoints [N]
info break [N]
info watchpoints [N]
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483d4 in main at abc.c:5
breakpoint already hit 1 time
clear FUNCTION
clear FILENAME:FUNCTION
clear LINENUM
clear FILENAME:LINENUM
step [COUNT] (shorthand s)
n)
c)
p)
disp)
set width=47
l)
Here are the forms of the `list' command most commonly used:
list LINENUM
list FUNCTION
list
list -
When your program has stopped, the first thing you need to know is where it stopped and how it got there.
Each time your program performs a function call, information about the call is generated. That information includes the location of the call in your program, the arguments of the call, and the local variables of the function being called. The information is saved in a block of data called a "stack frame". The stack frames are allocated in a region of memory called the "call stack".
bt, where)
If your program crashes (due to a segmentation fault, for example) and "dumps core" (produces a core file), you can load that file into gdb and do many of the same operations you can do by starting the program in the debugger and running it until it crashes. These operations include looking at the stack trace, moving up through the trace, examining the values in variables, and printing source code lines.
To examine a core file, start gdb normally, specifying the name of the executable that generated the core. Then enter
core <core_file_name>
warning: core file may not match specified executable file.
Core was generated by `<executable_name>'.
This simple example has a segmentation fault - suppose the file name is abc.c:
#include
int main( int argc, char ** argv )
{
char * abc = NULL;
strcpy( abc, "DEF" );
printf( "abc is %s\n", abc );
}
First, compile the code with the debug information, then run the executable in the debugger. The program dies with a segmentation fault; this will demonstrate how you might be able to track down the problem with gdb.
bwall@esus:~/www/cs440/labs$ gcc -o abc -g abc.c
bwall@esus:~/www/cs440/labs$ gdb abc
GNU gdb 6.3-debian
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 "i386-linux"...Using host libthread_db library "/lib>
(gdb) r
Starting program: /students/bwall/www/cs440/labs/abc
Program received signal SIGSEGV, Segmentation fault.
0x400945d6 in strcpy () from /lib/libc.so.6
(gdb) where
#0 0x400945d6 in strcpy () from /lib/libc.so.6
#1 0x080483ee in main (argc=1, argv=0xbffff074) at abc.c:7
(gdb) up
#1 0x080483ee in main (argc=1, argv=0xbffff074) at abc.c:7
7 strcpy( abc, "DEF" );
(gdb) p abc
$1 = 0x0
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) start main
Breakpoint 1 at 0x80483d4: file abc.c, line 5.
Starting program: /students/bwall/www/cs440/labs/abc main
main (argc=2, argv=0xbffffa84) at abc.c:5
5 char * abc = NULL;
(gdb) n
7 strcpy( abc, "DEF" );
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) quit
bwall@esus:~/www/cs440/labs$ exit
Script done on Mon Oct 10 22:28:10 2005
When the program is run, it generates the segmentation fault, and the gdb
where command (you can also use bt or
backtracke) shows the call stack. This shows that the program
crashed in the strcpy() library function. Doing up moves
up one stack frame, where you can examine the values of program variables.
The p command (short for print) displays the contents of
variables. Obviously the pointer abc has a value of NULL (0), which
caused the error in strcpy(). The program is killed and restarted
using the start command to stop upon entry into main().
The n (short for next) command is used to stop over lines one
at a time.