Debugging Optimized Builds
Purpose
Guide agents through debugging code compiled with optimization: choosing the right debug-friendly optimization level, reading inlined frames, diagnosing "value optimized out", using split-DWARF for faster debug builds, and applying GDB techniques specific to optimized code.
Triggers
- "GDB says 'value optimized out' — what does that mean?"
- "How do I debug a release build?"
- "How do I see inlined function frames in GDB?"
- "What's the difference between -O0 and -Og for debugging?"
- "How do I use RelWithDebInfo with CMake?"
- "Breakpoints in optimized code land on wrong lines"
Workflow
1. Choose the right build configuration
-Og: GCC's "debug-friendly optimization" — enables optimizations that don't interfere with debugging. Variables stay in registers where GDB can see them. Line numbers stay accurate. Best balance for development.
2. "Value optimized out" — causes and workarounds
This means the compiler decided the variable's value doesn't need to be stored at this point — it might be:
- Kept only in a register (not the one GDB is looking at)
- Folded into a constant by constant propagation
- Eliminated because it's not used after this point
- Replaced by a later optimized value
Workarounds:
3. Reading inlined frames in GDB
With optimization, frequently-called small functions get inlined. GDB shows these as extra frames:
4. Line number discrepancies
Optimizers reorder instructions, so the "current line" in GDB may jump around:
5. GDB scheduler-locking for optimized multithreaded code
With optimization, threads may race in unexpected ways when stepping:
6. split-DWARF — faster debug builds
Split DWARF offloads debug info to .dwo files, reducing linker input:
7. Useful GDB commands for optimized builds
Related skills
- Use
skills/debuggers/gdbfor full GDB session management - Use
skills/debuggers/dwarf-debug-formatfor DWARF debug info details - Use
skills/debuggers/core-dumpsfor post-mortem debugging of optimized crashes - Use
skills/compilers/gccfor-Og,-g, and debug flag selection


