This guide will help you learn how to use GDB (GNU Debugger) to debug C++ programs by setting breakpoints and examining values.
Table of Contents
- Introduction to GDB
- Compiling with Debugging Information
- Starting GDB
- Setting Breakpoints
- Running the Program
- Examining Values
- Stepping Through Code
- Practical Examples
- Common GDB Commands
Introduction to GDB
GDB (GNU Debugger) is a powerful tool that allows developers to:
- Run programs in a controlled environment
- Stop execution at specific points (breakpoints)
- Examine what’s happening during program execution
- Change variables to test different scenarios
Compiling with Debugging Information
Before you can effectively use GDB, you need to compile your program with debugging information:
g++ -g -o program_name source_file.cpp
The -g
flag adds debugging information that GDB needs.
Starting GDB
Start GDB with your compiled program:
gdb ./program_name
You’ll see the GDB prompt: (gdb)
Setting Breakpoints
Breakpoints pause execution at specific locations. There are several ways to set breakpoints:
- By function name:
(gdb) break printValue
- By file and line number:
(gdb) break debug1.cpp:5
- By line number in the current file:
(gdb) break 5
To list all breakpoints:
(gdb) info breakpoints
To delete a breakpoint:
(gdb) delete 1 # Delete breakpoint #1
Running the Program
Start your program:
(gdb) run
The program will run until it hits a breakpoint, encounters an error, or completes execution.
Examining Values
Once the program is paused at a breakpoint, you can examine values:
- Print a variable’s value:
(gdb) print value # Print the variable 'value'
- Print an expression:
(gdb) print value + 10
- Display a variable’s value (updates automatically):
(gdb) display value
Stepping Through Code
Navigate through your program:
- Step to the next line (will step into functions):
(gdb) step
- Next line (will not step into functions):
(gdb) next
- Continue execution until the next breakpoint:
(gdb) continue
- Finish the current function:
(gdb) finish
Practical Examples
Let’s use our example files to demonstrate GDB usage.
Example 1: Debug the First Program
Using debug1.cpp
:
#include <iostream>
void printValue(int value)
{
std::cout << value << '\n';
}
int main()
{
printValue(5);
return 0;
}
Debugging session:
$ g++ -g -o debug1 debug1.cpp
$ gdb ./debug1
(gdb) break main
(gdb) run
# Program stops at main()
(gdb) step
# Steps into printValue(5)
(gdb) print value
$1 = 5
(gdb) continue
# Program finishes
Example 2: Debug with Multiple Function Calls
Using debug2.cpp
:
#include <iostream>
void printValue(int value)
{
std::cout << value << '\n';
}
int main()
{
printValue(5);
printValue(6);
printValue(7);
return 0;
}
Debugging session:
$ g++ -g -o debug2 debug2.cpp
$ gdb ./debug2
(gdb) break printValue
(gdb) run
# Program stops at first call to printValue
(gdb) print value
$1 = 5
(gdb) continue
# Program stops at second call to printValue
(gdb) print value
$2 = 6
(gdb) continue
# Program stops at third call to printValue
(gdb) print value
$3 = 7
(gdb) continue
# Program finishes
Common GDB Commands
Command | Description |
---|---|
help |
Get help on GDB commands |
break or b |
Set a breakpoint |
run or r |
Start program execution |
continue or c |
Continue execution until next breakpoint |
next or n |
Execute next line without stepping into functions |
step or s |
Execute next line, stepping into functions |
print or p |
Print variable value |
display |
Set automatic display of a variable |
info locals |
Show all local variables |
info breakpoints |
List all breakpoints |
delete or d |
Delete breakpoints |
quit or q |
Exit GDB |
backtrace or bt |
Show call stack |
finish |
Run until the current function returns |
watch |
Set watchpoint on variable |
Final Tips
- Use
up
anddown
arrow keys to navigate command history - Use
ctrl+L
to clear the screen - Try
layout src
to see source code alongside GDB commands - Use tab completion for commands and variable names
- Add
-Wall
when compiling to see all warnings