Home | Projects | Notes > Unix/Linux > GDB Cheat Sheet
11$ g++ -g source.cpp -o program2$ gdb program.exe
-goption embeds symbolic debug information into the output fileprogram. This debug info includes:
Function names
Variable names and types
Line numbers in source code
Mapping between binary instructions and source code
| Command | Description |
|---|---|
break <location> or b <location> | Set a breakpoint (e.g., b main, b 11, b file.cpp:25, b func). |
run or r | Start the program from the beginning. |
delete | Delete all breakpoints. Use delete <n> to remove a specific one. |
disable <n> / enable <n> | Disable or enable a breakpoint. |
info breakpoints or i b | List current breakpoints and their status. |
next or n | Execute the next line (step over function calls). |
step or s | Step into the function call on the current line. |
finish | Run until the current function returns. |
continue or c | Resume execution after a breakpoint. |
print <expr> or p <expr> | Print the value of an expression or variable. |
display <expr> | Auto-print the value of an expression each time the program stops. |
undisplay <n> | Remove a display expression. |
backtrace or bt | Show call stack (current function and callers). |
frame <n> | Switch to frame number n in the call stack. |
up / down | Move one frame up or down (in terms of stack number where #0 being the top frame) the stack. |
info locals | Show local variables in the current frame. |
info args | Show function arguments in the current frame. |
list or l | Show source code near the current line or a specific location. |
set var <var>=<value> | Change the value of a variable. |
watch <expr> | Set a watchpoint (pause when the expression changes). |
info watchpoints | List watchpoints. |
stepi or si | Step one machine instruction. |
nexti or ni | Step over one machine instruction. |
quit or q | Exit GDB. |
gdb.cpp)12using namespace std;3
4void f3()5{6 int f3_var = 30;7 cout << f3_var << end;8}9
10void f2()11{12 int f2_var = 20;13 cout << f2_var << end;14 f3();15}16
17void f1()18{19 int f1_var = 10;20 cout << f1_var << end;21 f2();22}23
24int main(int argc, char *argv[])25{26 int main_var = 50;27 cout << main_var << endl;28 f1();29 return 0;30}Compile the program:
11g++ -g gdb.cppAttach GDB:
11gdb .\a.exeSet breakpoints:
41(gdb) break main2Breakpoint 1 at 0x14000152c: file gdb.cpp, line 26.3(gdb) b f34Breakpoint 2 at 0x140001468: file gdb.cpp, line 6.View breakpoints:
11(gdb) info breakpoints2Num Type Disp Enb Address What31 breakpoint keep y 0x000000014000152c in main(int, char**) at gdb.cpp:2642 breakpoint keep y 0x0000000140001468 in f3() at gdb.cpp:6Run the program:
11(gdb) run2Starting program: D:\workspace\cpp\a.exe 3[New Thread 23204.0x21f4]4[New Thread 23204.0x1ef0]5[New Thread 23204.0x34e4]6
7Thread 1 hit Breakpoint 1, main (argc=1, argv=0xf2080) at gdb.cpp:26826 int main_var = 50;Continue to the next breakpoint:
11(gdb) continue2Continuing.3504105206
7Thread 1 hit Breakpoint 2, f3 () at gdb.cpp:686 int f3_var = 30;View call stack:
11(gdb) backtrace2#0 f3 () at gdb.cpp:63#1 0x00007ff677a314d2 in f2 () at gdb.cpp:144#2 0x00007ff677a31511 in f1 () at gdb.cpp:215#3 0x00007ff677a3155c in main (argc=1, argv=0xf2080) at gdb.cpp:28
#nrefers to each stack frame number.
View local variables:
11(gdb) info locals2f3_var = 0View the value of a local variable in a different stack frame:
11(gdb) frame 22#2 0x00007ff677a31511 in f1 () at gdb.cpp:21321 f2();4(gdb) print f1_var5$1 = 10First, you need to go to the stack frame in which the variable you want to print is declared.
View the source code near the current line:
x
1(gdb) list216317 void f1()418 {519 int f1_var = 10;620 cout << f1_var << endl;721 f2();822 }9231024 int main(int argc, char *argv[])1125 {View the current line:
11(gdb) frame2#2 0x00007ff677a31511 in f1 () at gdb.cpp:21321 f2();Return to the top stack frame:
11(gdb) f 0 2#0 f3 () at gdb.cpp:636 int f3_var = 30;Run until the current function returns:
61(gdb) finish2Run till exit from #0 f3 () at gdb.cpp:6330[New Thread 23204.0x2738]4
5f2 () at gdb.cpp:15615 }