gdb Quick Reference

What is gdb

According to GNU , GDB, the GNU Project debugger, allows you to see what is going on “inside” another program while it executes - or what another program was doing at the moment it crashed.

Compiling For gdb

gdb needs information that is not ordinarily provided by the compiler. So, if you want to use gdb you need to include the -g switch when compiling. For example:

g++ -c -g Driver.cpp

Starting gdb

To start gdb, simply enter the gdb command in a terminal window. It has one paramter, the name of the executable. For example:

gdb Driver

Basic gdb Commands

The following commands are listed in alphabetical order:

CommandPurpose
EnterRe-execute the previous gdb command
break xxSet a breakpoint at line xx in the current file
break functionnameSet a breakpoint at the start of functionname
break classname::functionnameSet a breakpoint at the start of a method
break filename:xxSet a breakpoint at line xx in a specified file
continueResume running/executing the program
disable xxDisable breakpoint number xx
enable xxEnable breakpoint number xx
helpDisplay a list of gdb commands
help commandGet help on a specific command
info breakList all breakpoints
info sourceShow the name of the current source file
info sourcesList the name of all source files in use
listList the next source lines
list xxList source lines starting at line xx
list xx,yyList sources lines from xx to yy
list filename:xxList source lines in the specified file starting at line xx
nextExecute the current statement and then stop
print variablePrint the value of a variable
quitQuit gdb
runRun/execute the program starting from the beginning
set variable = valueAssign a new value to a variable
stepExecute the current statement (entering a function if possible)

A Sample Session

A small sample session of gdb (with an executable named Driver) might look something like the following [where $ is the shell prompt and (gdb) is the gdb prompt]:

$ gdb Driver
(gdb) break 1
(gdb) run
(gdb) next
(gdb) print i
(gdb) next
(gdb) print i
(gdb) quit