GDB Debugger
Disclaimer
The GNU GDB debugger is provided with most Unix systems by the Free Software Foundation.
Much of the following information is summarized from the gdb man pages from linux3.
Type the command man gdb at the Unix prompt to view the man pages
directly.
The complete "official" GDB user's manual from the Free Software Foundation is
available at
http://www.gnu.org/software/gdb/documentation.
Introduction
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 four main kinds of things (plus other things in support of these) to
help you catch bugs in the act:
- Start your program, specifying anything that might affect its behavior.
- Make your program stop on specified conditions.
- Examine what has happened, when your program has stopped.
- Change things in your program, so you can experiment with correcting the
effects of one bug and go on to learn about another.
To use the debugger, your program must be compiled and linked with the "-g" switch.
This tells the compiler and linker to keep information that the debugger needs.
The "-g" switch should be part of the CCFLAGS in your makefile.
Debugger Basics
Let's assume that the name of your executable is a.out. To debug a.out type the command
gdb a.out. This command starts GDB which takes control of a.out.
You can now use GDB commands to control the execution of a.out. You can force a.out
to stop executing at a particular line of code, look at the contents of variables, etc.
Many commonly used commands can be abbreviated with a single lette.
All GDB commands can be invoked using the shortest unique abbreviation for the command.
The most frequently used GDB commands are
- list (or l)
Displays lines of code. The command list<LineNumber> displays
a few lines of code before and after LineNumber in the current source file.
You may also specify the filename with the command list <FileName>:<LineNumber>.
The command list <Function> displays the first few of lines of Function.
The command list (with no parameters) lists the next few lines of code.
- break (or b)
The break command sets a breakpoint in a.out. When a.out is executing and reaches a
a breakpoint, the execution stops. Breakpoints can be a line number or the name of a
function. The command break <LineNumber> sets a breakpoint at the specified line
number in the currently file. For example, break 22 causes a.out to stop executing
if/when it
reaches line 22 in the current file. The command break <function> sets a breakpoint
at the beginning of the specified function. For example, break print causes
a.out to stop executing if/when it reaches the first line of code in the function print().
Breakpoints are numbered by gdb for future reference in other command (e.g. delete)
The command "info breakpoints" will display all breakpoints currently set.
- run [arglist]
The run command starts your program from the beginning with the specified
arglist (command line arguments), if any. You may restart your program at the beginning
at any time without re-entering the command line arguments.
- delete n
removes breakpoint n
WARNING delete with no argument removes all breakpoints
- print (or p)
The print command displays the value of an expression. Most often, the expression
is the name of a variable. In this case, print prints the current value stored
in that variable.
- display
The display <variable> command displays the contents of variable every time
a executions stops at a breakpoint.
- continue (or c)
The continue command continues running your program (after stopping, e.g. at a breakpoint).
- next (or n)
The nextexecutes the next line of code (after stopping). If the next line of code is
a function call, the function call is completed.
- step (or s)
The step command is similar to the next command with one important difference.
It also executes the next line of code (after stopping), but if the next line of code is
a function call, the debugger steps into the function.
- backtrace (or bt or where)
Displays the call-back stack that shows the "history" of current functions calls which were made in your
program. This is especially helpful when your program crashes from a segmentation fault.
- help [name]
Show information about GDB command name, or general information about using GDB.
- kill
stops execution of gdb without exiting. Use this command when you want to
edit and recompile; it's faster than exiting and restarting gdb.
- quit Exit from GDB.
When your program crashes
When your program crashes (eg segmentation fault) a very large file named "core" is
left behind. You can use GDB to examine this core file to determine where and hopefully
why your program crashed.
Again assume that your executable is named a.out. You run a.out and get a segementation
fault and a core file is left behind. To examine the core file, execute the command
gdb a.out just like when you're debugging a.out. GDB will detect the core
file and see that it came from a.out and will read it.
You can now use the GDB command where to display the function call stack and
determine which function (and which line number in that function) was executing when
your program crashed. You can use the GDB command up and down to move
up and down in the stack to look at variables, etc.
Dennis Frey
Last modified: Tues Nov 11, 2003 3:20pm