C++ Debugging Basics | GDB and LLDB — Breakpoints and Watchpoints in Minutes
이 글의 핵심
GDB/LLDB essentials: -g -O0 builds, break, watch, next/step, bt, thread apply all bt, and production core-file analysis.
Introduction: Limits of printf debugging
“I added 100 prints and still can’t find it”
Segfaults with confusing logs, buffering, and slow rebuild cycles—debuggers stop at the line, show variables, stack, and memory live.
Breakpoint: stop at a line. Watchpoint: stop when memory changes. Backtrace: see call chain.
Requirements: build with -g (symbols). -O0 often easiest for inspecting variables.
Environment: GDB on Linux/WSL; LLDB on macOS (Xcode CLI tools).
After reading:
- Core GDB/LLDB commands
- Breakpoints including conditions
- Watchpoints for corruption hunts
- Inspect vars/stack; multi-threaded deadlock introspection
Table of contents
- Scenarios
- Starting the debugger
- Breakpoints
- Watchpoints
- Stepping
- Inspecting variables
- Examples
- Common errors
- Best practices
- Production patterns
1. Scenarios
- Segfault → run under debugger →
btat crash - Rare iteration → conditional breakpoint
break main.cpp:10 if i == 500 - Mystery mutation →
watch variable - Infinite loop → Ctrl+C →
bt, inspect loop vars - Deadlock →
thread apply all backtrace - Production → core dump +
gdb ./app core
flowchart TD
A[Bug] --> B{Type?}
B -->|Crash| C[GDB run → bt]
B -->|Conditional| D[Conditional breakpoint]
B -->|Corruption| E[Watchpoint]
B -->|Hang| F[Ctrl+C → bt]
B -->|Deadlock| G[thread apply all bt]
B -->|Prod| H[Core file]
2. Starting
g++ -g -O0 main.cpp -o myapp
gdb ./myapp
(gdb) run
LLDB:
lldb ./myapp
(lldb) run
3. Breakpoints
GDB:
break main
break file.cpp:15
break file.cpp:20 if i == 50
info breakpoints
delete 1
LLDB:
breakpoint set --name main
breakpoint set -f file.cpp -l 15
breakpoint set -f file.cpp -l 20 -c 'i == 50'
4. Watchpoints
GDB (often set after break main / run when variable exists):
watch global_counter
watch *ptr
LLDB:
watchpoint set variable global_counter
5. Stepping
| Command | GDB | LLDB |
|---|---|---|
| Next line (no step into) | next / n | next |
| Step into | step / s | step |
| Until function returns | finish | finish |
| Continue | continue / c | continue |
6. Inspecting
print x,print *p,print arr[0]@10backtrace/bt,frame N,info locals,info argsx/(examine memory) in GDB
7. Examples
Array bounds off-by-one
for (int i = 0; i <= size; ++i) { // should be i < size
arr[i] = i * 2;
}
At SIGSEGV: bt, print i, print size.
Null pointer
print head → 0x0.
Deadlock
thread apply all backtrace — see each thread blocked on locks.
8. Common errors
- No symbols → missing
-gor stripped binary optimized out→ compile-O0or usevolatilesparingly for debug- Hardware watchpoint limit → reduce or use software watchpoints (slower)
9. Best practices
- Debug builds:
-g -O0 - On crash:
bt fullfirst - Use
.gdbinitforset print pretty on
10. Production patterns
ulimit -c unlimited
./myapp # crash
gdb ./myapp core
(gdb) bt full
gdbserver for remote attach (freeze-aware—use during maintenance windows).
Split debug info: objcopy --only-keep-debug + --add-gnu-debuglink for smaller release binaries with separate symbols.
GDB vs LLDB quick map
| Task | GDB | LLDB |
|---|---|---|
| Run | run | run |
| Break | break main | breakpoint set -n main |
| Step | next / step | same |
| Stack | bt | bt |
| Watch | watch var | watchpoint set variable var |
Summary
| Tool | Platform |
|---|---|
| GDB | Linux, typical for servers |
| LLDB | macOS, also Linux |
| VS | Windows GUI debugging |
Principles: prefer debugger over printf for control flow; bt on crashes; watch for mystery writes; thread bt for concurrency.
Next: Sanitizers #16-2
Previous: Compile-time optimization #15-3
FAQ
When is this useful?
A. Any non-trivial C++ bug—especially crashes, concurrency, and state you can’t print cleanly.
Read first?
A. Series index and build basics.
Related posts
- Sanitizers
- Logging and assertions
- Segfault debugging
Keywords
C++ debugging, GDB, LLDB, breakpoint, watchpoint, backtrace, segfault, core dump
Practical tips
Debugging
- Warnings first; minimal repro
Performance
- Profile before optimizing
Related posts
- GDB/LLDB deep dive
- GDB basics
- LLDB basics