GDB Tutorial
CS 225

What is GDB?

Compiling for GDB

Running GDB and Passing Parameters

Example

// code.cpp
#include <iostream>
using namespace std;

void segfault() {
    int *p = NULL;
    *p = 5;
}

int main() {
    segfault();
    return 0;
}

Compile with:

$ clang++ -g -O0 code.cpp -o code

Example

$ ./code
[1]    1813 segmentation fault  ./code
$ gdb code
GNU gdb (GDB) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from code...done.
(gdb) run
Starting program: code

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in segfault () at code.cpp:6
6           *p = 5;
(gdb) 

Quitting GDB: The quit command

quit

Exit GDB and return to the shell (terminal)

Starting To Debug: The print command

print EXPR

EXPR is an expression. Some examples:

Result is stored in a numbered variable, which can be referenced in later commands:
(gdb) print 5
$1 = 5
(gdb) print $1 * 2 + 3
$2 = 13
(gdb) print $2 * $1 / 7
$3 = 9
(gdb) print $1 * $2 * $3
$4 = 585
(gdb) 

Example

(gdb) run
Starting program: code

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in segfault () at code.cpp:6
6           *p = 5;
(gdb) print p
$1 = (int *) 0x0
(gdb) 

The display command

display EXPR

Same as print, but print EXPR every time the program stops.

undisplay ID

Cancel a display. Find the ID with info display

Getting Oriented: The backtrace command

backtrace

Prints a backtrace of function calls

up [N]

down [N]

Move within the list of stack frames (optionally [N] times)

frame ID

Jump to frame ID (frame 0 is where the program stopped)

Getting Oriented: The backtrace command

(gdb) run
Starting program: code

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in segfault () at code.cpp:6
6           *p = 5;
(gdb) backtrace
#0  0x0000000000400620 in segfault () at code.cpp:6
#1  0x0000000000400644 in main () at code.cpp:10

So, reading the above, we see that our program started in main, which called (on line 10) segfault. The program stopped in the segfault function (because of a segmentation fault) on line 6.

Breakpoints: The break command

break file:line | function

Pauses the program just before a line (or function) executes

Some examples:

Breakpoint Manipulation

info breakpoints

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400614 in segfault() at code.cpp:5

enable ID

disable ID

Temporarily enable or disable a breakpoint

delete ID

clear file:line | function

Delete a breakpoint

Moving Forward: The continue, next, step, and finish commands

continue [N]

Resume execution until the next breakpoint, signal (segfault), or normal exit (optionally, [N] times)

next [N]

Execute the current instruction, without entering functions (optionally, [N] times)

step [N]

Execute the current instruction, entering all functions (optionally, [N] times)

finish

Execute until the end of the current function

Stuck?: The help command

help [EXPR]

GDB's built-in documentation. Some examples:

(gdb) help display
Print value of expression EXP each time the program stops.
/FMT may be used before EXP as in the "print" command.
/FMT "i" or "s" or including a size-letter is allowed,
as in the "x" command, and then EXP is used to get the address to examine
and examining is done as in the "x" command.

With no argument, display all currently requested auto-display expressions.
Use "undisplay" to cancel display requests previously made.
(gdb) help continue
Continue program being debugged, after signal or breakpoint.
Usage: continue [N]
If proceeding from breakpoint, a number N may be used as an argument,
which means to set the ignore count of that breakpoint to N - 1 (so that
the breakpoint won't break until the Nth time it is reached).

If non-stop mode is enabled, continue only the current thread,
otherwise all the threads in the program are continued.  To
continue all stopped threads in non-stop mode, use the -a option.
Specifying -a and an ignore count simultaneously is an error.
(gdb) help step
Step program until it reaches a different source line.
Usage: step [N]
Argument N means step N times (or till program stops for another reason).

Tips and Tricks

Advanced Breaking: Conditions and Watchpoints

condition ID EXPR

break [file:line | function] if EXPR

Only break on breakpoint ID if EXPR is true

break list.cpp:142 if one->data == 2

watch EXPR

Break when the value of the expression EXPR changes

rwatch EXPR

Break when the expression EXPR is read

awatch EXPR

Break on either reading or writing

References

/