Using a
Debugger
Learn to step through C++ code, set breakpoints, and inspect variables using GDB, LLDB, or your IDE to find and fix bugs efficiently.
What Is a Debugger?
A debugger is a tool that lets you pause a running program at any point, inspect the values of variables, and step through code one line at a time. Instead of guessing where a bug lives, you can observe your program's behavior directly.
Why Use a Debugger?
Popular C++ Debuggers
The Debug Build: -g Flag
Before you can debug, you must compile with debug symbols. These embed source code information (line numbers, variable names) into your executable so the debugger can map machine instructions back to your C++ code.
Core Concept: Breakpoints
A breakpoint is a marker you place on a specific line. When the program reaches that line, execution pauses and control returns to you. You can then inspect the program's state before continuing.
Stepping Through Code
Once paused at a breakpoint, you advance execution using stepping commands. Each command moves through your code at a different granularity.
Step Over vs Step Into
Inspecting Variables
While paused, you can print or watch any variable's current value. This is the core power of a debugger: seeing exactly what your program knows at any moment.
Essential GDB Commands
| Command | Shortcut | Action |
|---|---|---|
| break main.cpp:5 | b main.cpp:5 | Set breakpoint at line 5 |
| run | r | Start the program |
| next | n | Step over one line |
| step | s | Step into a function |
| finish | fin | Step out of current function |
| continue | c | Resume until next breakpoint |
| print x | p x | Show value of x |
| backtrace | bt | Show call stack |
| quit | q | Exit GDB |
Debugging Workflow
Example 1: Finding a Division Bug
This program is supposed to compute the average of two integers, but it outputs 2 instead of 2.5 for inputs 2 and 3. Let's debug it.
Debugging the Average Bug
Example 2: An Off-by-One Loop
This code should print numbers 1 through 5, but only prints 1 through 4. We set a breakpoint inside the loop and watch the loop variable.
Example 3: Diagnosing a Segfault
A segmentation fault crashes your program instantly with no helpful message. The debugger catches the exact line and lets you inspect the cause.
The Call Stack
The call stack (or backtrace) shows the chain of function calls that led to the current point. It answers: "how did I get here?"
Common Mistakes
GDB vs IDE Debugger
| Feature | GDB / LLDB | IDE Debugger |
|---|---|---|
| Interface | Command line (text) | Graphical (click) |
| Set Breakpoint | break file:line | Click the gutter margin |
| Inspect Variables | print varname | Hover or Watch panel |
| Step Over | next (n) | Step Over button / F10 |
| Learning Curve | Steeper (memorize commands) | Gentler (visual cues) |
| Availability | Any terminal (SSH, remote) | Requires IDE installed |
IDE Keyboard Shortcuts
| Action | Visual Studio | VS Code | CLion |
|---|---|---|---|
| Start Debugging | F5 | F5 | Shift+F9 |
| Toggle Breakpoint | F9 | F9 | Ctrl+F8 |
| Step Over | F10 | F10 | F8 |
| Step Into | F11 | F11 | F7 |
| Step Out | Shift+F11 | Shift+F11 | Shift+F8 |
| Continue | F5 | F5 | F9 |
Key Takeaways
Question 1 of 5
Which compiler flag must you include so that GDB can display variable names and source lines?
Question 2 of 5
You are paused at a line that calls calculateTotal(). You suspect the bug is inside that function. Which command should you use?
Question 3 of 5
When the debugger pauses at a breakpoint on line 10, which statement is true about line 10?
Question 4 of 5
Your program crashes with a segmentation fault. After running it in GDB, which command shows the chain of function calls that led to the crash?
Question 5 of 5
A student compiles their program with: g++ -O2 -o myapp main.cpp
They launch GDB and try to inspect a variable, but GDB reports: "value optimized out". The student then tries setting a breakpoint, but lines seem to execute out of order.
What is the most likely cause of both problems?