-g
-O0
O
, number 0
)clang++
: clang++ -g -O0 code.cpp -o code
$gdb program
(gdb)run [arguments]
$gdb --args program [arguments]
(gdb)run
program
is the program you want to debug[arguments]
are the (optional) command line arguments to pass to the program// 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
$./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)
quit
commandquit
Exit GDB and return to the shell (terminal)
^d
means "Ctrl+d"y
if prompted:
(gdb)quit
A debugging session is active. Inferior 1 [process 31474] will be killed. Quit anyway? (y or n)y
$
print
commandprint EXPR
EXPR
is an expression. Some examples:
print 5+5/2
— mathematical expressionsprint p
— variablesprint foo(5, 7)
— function calls(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)
(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)
display
commanddisplay EXPR
Same as print
, but print EXPR
every time the program stops.
undisplay ID
Cancel a display
. Find the ID
with info display
backtrace
commandbacktrace
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)
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.
break
commandbreak file:line | function
Pauses the program just before a line (or function) executes
Some examples:
break main
— break just before the main
function is runbreak code.cpp:4
— break just before line 4 of code.cpp
break List<int>::print
— break just before List<int>
's print
function runsbreak List<T>::print
; must supply T
file:line
notation insteadinfo breakpoints
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400614 in segfault() at code.cpp:5
ID
)enable ID
disable ID
Temporarily enable or disable a breakpoint
delete ID
clear file:line | function
Delete a breakpoint
continue
, next
, step
, and finish
commandscontinue [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
help
commandhelp [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).
list LOCATION
LOCATION
next
ing and step
ping many times)^c
(Ctrl+c)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
list.cpp:142
when one->data
is 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
/