Home | Projects | Notes > Unix/Linux > GDB Cheat Sheet
11$ g++ -g source.cpp -o program
2$ gdb program.exe
-g
option 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
)1
2using 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.cpp
Attach GDB:
11gdb .\a.exe
Set breakpoints:
41(gdb) break main
2Breakpoint 1 at 0x14000152c: file gdb.cpp, line 26.
3(gdb) b f3
4Breakpoint 2 at 0x140001468: file gdb.cpp, line 6.
View breakpoints:
11(gdb) info breakpoints
2Num Type Disp Enb Address What
31 breakpoint keep y 0x000000014000152c in main(int, char**) at gdb.cpp:26
42 breakpoint keep y 0x0000000140001468 in f3() at gdb.cpp:6
Run the program:
11(gdb) run
2Starting 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:26
826 int main_var = 50;
Continue to the next breakpoint:
11(gdb) continue
2Continuing.
350
410
520
6
7Thread 1 hit Breakpoint 2, f3 () at gdb.cpp:6
86 int f3_var = 30;
View call stack:
11(gdb) backtrace
2#0 f3 () at gdb.cpp:6
3#1 0x00007ff677a314d2 in f2 () at gdb.cpp:14
4#2 0x00007ff677a31511 in f1 () at gdb.cpp:21
5#3 0x00007ff677a3155c in main (argc=1, argv=0xf2080) at gdb.cpp:28
#n
refers to each stack frame number.
View local variables:
11(gdb) info locals
2f3_var = 0
View the value of a local variable in a different stack frame:
11(gdb) frame 2
2#2 0x00007ff677a31511 in f1 () at gdb.cpp:21
321 f2();
4(gdb) print f1_var
5$1 = 10
First, 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) list
216
317 void f1()
418 {
519 int f1_var = 10;
620 cout << f1_var << endl;
721 f2();
822 }
923
1024 int main(int argc, char *argv[])
1125 {
View the current line:
11(gdb) frame
2#2 0x00007ff677a31511 in f1 () at gdb.cpp:21
321 f2();
Return to the top stack frame:
11(gdb) f 0
2#0 f3 () at gdb.cpp:6
36 int f3_var = 30;
Run until the current function returns:
61(gdb) finish
2Run till exit from #0 f3 () at gdb.cpp:6
330[New Thread 23204.0x2738]
4
5f2 () at gdb.cpp:15
615 }