logo

Using the GDB Debugger

The gdb debugger is available for use with programs compiled with the g++ compiler. It is used to locate run-time errors. The C++ program must be compiled with the -g flag in order to use the debugger.

A brief overview of gdb debugger commands:

unix level commands -- entered after the bgunix $ prompt:

man gdb to get help on gdb at the unix command level
g++ -g filename.cpp to compile & link with the debug option
gdb a.out to execute the debugger with a.out
Basic gdb commands -- entered after the (gdb) prompt:
help to display a list of gdb commands
help command to get help on a specified gdb command
run to run/execute the program starting from the beginning
continue to resume running/executing the program
next to execute the current statement and stop at the next statement
step same as next, but step into a function
list xx list source lines starting at line xx
list to list the next source lines
list xx,yy to list sources lines from line xx to line yy
list filename:xx to list source lines in the specified file starting at line xx
quit to quit gdb and revert to the unix command level
break functionname to set a breakpoint at the start of a function
break classname::functionname to set a breakpoint at the start of a member function
break filename:xx to set a breakpoint at line xx in the specified file
break xx to set a breakpoint at line xx in the current file
break 1 to set a breakpoint at the first line in the current file (declaration or executable statement)
info break to list all breakpoints (including those disabled); breakpoints are numbered #1, #2, #3, etc.
disable xx to disable breakpoint #xx
enable xx to enable breakpoint #xx
print v1 to print the value of a specified variable
info source to show the name of the current source file
info sources to list the name of all source files in use
set variable = value to assign a new value to a specified variable
(return) to re-execute the previous gdb command; this is particularly useful if the previous gdb command was next or step

You can also execute most gdb commands by entering only the first letter of the command.

The complete gdb user manual is available online at http://sources.redhat.com/gdb/onlinedocs/gdb_toc.html

See also: Finding a run-time error with gdb