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 gdbto get help on gdb at the unix command level
g++ -g filename.cppto compile & link with the debug option
gdb a.outto execute the debugger with a.out

Basic gdb commands -- entered after the (gdb) prompt:

helpto display a list of gdb commands
help commandto get help on a specified gdb command
runto run/execute the program starting from the beginning
continueto resume running/executing the program
nextto execute the current statement and stop at the next statement
stepsame as next, but step into a function
list xxlist source lines starting at line xx
listto list the next source lines
list xx,yyto list sources lines from line xx to line yy
list filename:xxto list source lines in the specified file starting at line xx
quitto quit gdb and revert to the unix command level
break functionnameto set a breakpoint at the start of a function
break classname::functionnameto set a breakpoint at the start of a member function
break filename:xxto set a breakpoint at line xx in the specified file
break xxto set a breakpoint at line xx in the current file
break 1to set a breakpoint at the first line in the current file (declaration or executable statement)
info breakto list all breakpoints (including those disabled); breakpoints are numbered #1, #2, #3, etc.
disable xxto disable breakpoint #xx
enable xxto enable breakpoint #xx
print v1to print the value of a specified variable
info sourceto show the name of the current source file
info sourcesto list the name of all source files in use
set variable = valueto 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

Updated: 06/21/2018 01:07PM