01 / 25

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.

C++

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?

✕ Without Debugger
You add cout statements everywhere, recompile, re-run, and hope the output reveals the bug.
✓ With Debugger
You set a breakpoint, run once, and see every variable's value at the exact line where things go wrong.

Popular C++ Debuggers

GDB
GNU Debugger: the standard command-line debugger on Linux. Works with g++.
LLDB
LLVM Debugger: default on macOS. Works with clang++. Similar commands to GDB.
IDE Debugger
Visual Studio, CLion, VS Code: graphical interface that wraps GDB or LLDB behind the scenes.

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.

$ g++ -g -o myprogram main.cpp ^ debug flag: includes symbol table

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.

1 int x = 10 ; 2 int y = 0 ; 3 y = x / 2 ; ← breakpoint: pauses here 4 cout << y ; 5 return 0 ; →

Stepping Through Code

Once paused at a breakpoint, you advance execution using stepping commands. Each command moves through your code at a different granularity.

1
Step Over (next): executes the current line completely, then pauses at the next line. If the line calls a function, the entire function runs without pausing inside it.
2
Step Into (step): if the current line calls a function, the debugger enters that function and pauses at its first line.
3
Step Out (finish): runs the rest of the current function and pauses after returning to the caller.
4
Continue (continue): resumes full-speed execution until the next breakpoint or the program ends.

Step Over vs Step Into

main() int a = 5; int b = square(a); cout << b; square(int n) int result = n * n; return result; Step Over (skips inside) Step Into (enters function) next / Step Over: line 2 → line 3 step / Step Into: line 2 → inside square()

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.

(gdb) print x $1 = 10 (gdb) print y $2 = 0 ← y hasn't been assigned yet (gdb) next ← execute y = x / 2; (gdb) print y $3 = 5 ← now y has the result

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

1
Compile with -g: include debug symbols so the debugger can map binary to source code.
2
Launch the debugger: type gdb ./myprogram (or press the Debug button in your IDE).
3
Set a breakpoint: pick a line near where you think the bug occurs. Use break filename:line.
4
Run the program: type run. Execution stops at your breakpoint.
5
Inspect and step: use print to check values, next or step to advance. Repeat until you find the bug.
6
Fix and re-test: edit the code, recompile with -g, and debug again to confirm the fix.

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.

1#include <iostream> 2using namespace std; 3 4int main() { 5 int a = 2, b = 3; 6 double avg = (a + b) / 2; // bug here? 7 cout << avg << endl; 8 return 0; 9}

Debugging the Average Bug

$ g++ -g -o avg avg.cpp $ gdb ./avg (gdb) break 6 Breakpoint 1 at 0x... : file avg.cpp, line 6. (gdb) run Breakpoint 1, main() at avg.cpp:6 (gdb) print a + b $1 = 5 ← sum is correct (gdb) print (a + b) / 2 $2 = 2 ← integer division! (gdb) print (a + b) / 2.0 $3 = 2.5 ← fix: use 2.0

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.

1for (int i = 1; i < 5; i++) { 2 cout << i << " "; 3}
(gdb) break 2 (gdb) run Breakpoint 1, main() at loop.cpp:2 (gdb) print i $1 = 1 (gdb) continue $2 = 2 (gdb) continue $3 = 3 (gdb) continue $4 = 4 ← loop ends: i < 5 is false when i = 5

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.

1int arr[3] = {10, 20, 30}; 2cout << arr[5]; // out of bounds!
(gdb) run Program received signal SIGSEGV 0x... in main() at crash.cpp:2 (gdb) print arr[5] $1 = -1297834 ← garbage: beyond array (gdb) backtrace #0 main() at crash.cpp:2

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?"

#0 compute() at math.cpp:12 ← crash #1 process() at app.cpp:45 #2 main() at app.cpp:10 Top Bottom main() called process(), which called compute(): crash at #0

Common Mistakes

✕ Forgetting -g
Compiling without debug symbols: the debugger shows raw addresses instead of variable names and line numbers.
✕ Breakpoint on Wrong Line
Setting a breakpoint after the buggy code. The error already happened, so variables show the wrong state.
✕ Step Over When You Should Step Into
Using "next" on a function call when the bug is inside that function. You skip right past the problem.
✕ Optimized Build
Compiling with -O2 or -O3 rearranges code. Variables may be "optimized out" and lines execute out of order.

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

1
Always compile with -g to include debug symbols in your executable.
2
Breakpoints pause execution at a line so you can inspect state before that line runs.
3
Step Over skips into functions; Step Into enters them. Choose based on where you suspect the bug.
4
print and backtrace are your most powerful inspection tools.
5
GDB, LLDB, and IDE debuggers share the same core concepts: learn one, use any.

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

What is the most likely cause of both problems?