[2026] C++ Debugging Basics | GDB and LLDB — Breakpoints and Watchpoints in Minutes

[2026] C++ Debugging Basics | GDB and LLDB — Breakpoints and Watchpoints in Minutes

이 글의 핵심

Stop printf debugging: GDB and LLDB breakpoints, conditional breaks, watchpoints, backtraces, stepping, multithread deadlock analysis, and core dumps—practical C++ debugger guide.

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

  1. Scenarios
  2. Starting the debugger
  3. Breakpoints
  4. Watchpoints
  5. Stepping
  6. Inspecting variables
  7. Examples
  8. Common errors
  9. Best practices
  10. Production patterns

1. Scenarios

  • Segfault → run under debugger → bt at crash
  • Rare iterationconditional breakpoint break main.cpp:10 if i == 500
  • Mystery mutationwatch variable
  • Infinite loop → Ctrl+C → bt, inspect loop vars
  • Deadlockthread apply all backtrace
  • Productioncore dump + gdb ./app core 아래 코드는 mermaid를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
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: 아래 코드는 text를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

CommandGDBLLDB
Next line (no step into)next / nnext
Step intostep / sstep
Until function returnsfinishfinish
Continuecontinue / ccontinue

6. Inspecting

  • print x, *print p, print arr[0]@10
  • backtrace / bt, frame N, info locals, info args
  • x/ (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 head0x0.

Deadlock

thread apply all backtrace — see each thread blocked on locks.

8. Common errors

  • No symbols → missing -g or stripped binary
  • optimized out → compile -O0 or use volatile sparingly for debug
  • Hardware watchpoint limit → reduce or use software watchpoints (slower)

9. Best practices

  • Debug builds: -g -O0
  • On crash: bt full first
  • Use .gdbinit for set print pretty on

10. Production patterns

다음은 간단한 bash 코드 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

ulimit -c unlimited
./myapp   # crash
gdb ./myapp core
(gdb) bt full

GDB vs LLDB quick map

TaskGDBLLDB
Runrunrun
Breakbreak mainbreakpoint set -n main
Stepnext / stepsame
Stackbtbt
Watchwatch varwatchpoint set variable var

Summary

ToolPlatform
GDBLinux, typical for servers
LLDBmacOS, also Linux
VSWindows 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.

Keywords

C++ debugging, GDB, LLDB, breakpoint, watchpoint, backtrace, segfault, core dump

Practical tips

Debugging

  • Warnings first; minimal repro

Performance

  • Profile before optimizing

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3