-g-O0O, 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 codeGNU 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)runStarting program: code Program received signal SIGSEGV, Segmentation fault. 0x0000000000400620 in segfault () at code.cpp:6 6 *p = 5; (gdb)
quit commandquitExit GDB and return to the shell (terminal)
^d means "Ctrl+d"y if prompted:
(gdb)quitA debugging session is active. Inferior 1 [process 31474] will be killed. Quit anyway? (y or n)y$
print commandprint EXPREXPR 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)runStarting 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 EXPRSame as print, but print EXPR every time the program stops.
undisplay IDCancel a display. Find the ID with info display
backtrace commandbacktracePrints a backtrace of function calls
up [N]down [N]Move within the list of stack frames (optionally [N] times)
frame IDJump to frame ID (frame 0 is where the program stopped)
backtrace command(gdb)runStarting 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 | functionPauses 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.cppbreak List<int>::print — break just before List<int>'s print function runsbreak List<T>::print; must supply Tfile: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 IDdisable IDTemporarily enable or disable a breakpoint
delete IDclear file:line | functionDelete 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)
finishExecute until the end of the current function
help commandhelp [EXPR]GDB's built-in documentation. Some examples:
(gdb)help displayPrint 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 continueContinue 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 stepStep 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 LOCATIONLOCATION
nexting and stepping many times)^c (Ctrl+c)condition ID EXPRbreak [file:line | function] if EXPROnly break on breakpoint ID if EXPR is true
break list.cpp:142 if one->data == 2
list.cpp:142 when one->data is 2watch EXPRBreak when the value of the expression EXPR changes
rwatch EXPRBreak when the expression EXPR is read
awatch EXPRBreak on either reading or writing
/