GDB Debugger

3 min read
🐞 Introduction to GDB (GNU Debugger)
1. What is GDB?
GDB (GNU Debugger) is a powerful tool that allows you to:
Run your program step by step
Pause at specific lines or functions (breakpoints)
Inspect variables and memory in real-time
Trace the call stack when something goes wrong
In short, GDB helps you understand how your program is running under the hood and catch bugs more effectively.
2. Hands-on: Debugging example.cpp
with GDB
0) Source
// example.cpp
#include <iostream>
using namespace std;
int add(int a, int b) {
int c = a + b;
return c;
}
int main() {
int x = 5, y = 10;
int result = add(x, y);
cout << "Result: " << result << endl;
return 0;
}
1) Build with debug symbols
g++ -g example.cpp -o example
-g
adds line/variable info for GDB.
2) Start GDB and set a breakpoint
gdb ./example
(gdb) break add # Stop at the start of add()
Breakpoint 1 at 0x...: file example.cpp, line 5.
3) Run until the breakpoint hits
(gdb) run
Starting program: /path/to/example
Breakpoint 1, add (a=5, b=10) at example.cpp:5
5 int c = a + b;
4) Inspect variables
(gdb) print a
$1 = 5
(gdb) print b
$2 = 10
5) Step over and check the new value
(gdb) next # executes 'int c = a + b;'
6 return c;
(gdb) print c
$3 = 15
6) See where we came from (call stack)
(gdb) backtrace
#0 add (a=5, b=10) at example.cpp:6
#1 0x... in main() at example.cpp:12
7) Finish the function and continue
(gdb) finish # run until add() returns
Run till exit from #0 add (a=5, b=10) at example.cpp:6
0x... in main() at example.cpp:12
(gdb) continue
Result: 15
[Inferior 1 (process ...) exited normally]
8) Quit GDB
(gdb) quit
3. Basic GDB Commands
Starting GDB
gdb ./example
Running the Program
(gdb) run
(gdb) run arg1 arg2 # Run with arguments
Breakpoints
(gdb) break main # Stop at main()
(gdb) break 15 # Stop at line 15
(gdb) delete # Remove all breakpoints
(gdb) info breakpoints # Show current breakpoints
Controlling Execution
(gdb) next # Execute next line (skip into functions)
(gdb) step # Step into a function
(gdb) continue # Run until the next breakpoint
(gdb) finish # Run until the current function returns
Inspecting Variables
(gdb) print x # Print value of variable x
(gdb) print arr[3] # Print array element
(gdb) display x # Automatically show x after each step
(gdb) watch x # Pause when x changes
Program State
(gdb) backtrace # Show call stack
(gdb) info locals # Show local variables
(gdb) info args # Show function arguments
Exiting GDB
(gdb) quit
4. Why Use GDB?
Find logic errors that print statements can’t show
Debug segmentation faults by checking where the crash happened
Inspect call stacks in recursive or complex programs
Essential foundation for advanced debugging tools (like
cuda-gdb
for GPU code)
0
Subscribe to my newsletter
Read articles from 박서경 directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
