C Debugging: Compiler Warning Flags (-Wall) & GDB Debugger Masterclass
Welcome to Phase 20 (Chapter 55): C Debugging โ Compiler Warning Flags (-Wall) & GDB Debugger Masterclass! Professional C development relies heavily on compiler warnings and interactive debuggers. In this guide, you will master GCC warning flags and GDB (GNU Debugger) execution commands.
| Flag | Purpose |
|---|---|
-Wall | Enables all common warning checks (uninitialized vars, missing returns). |
-Wextra | Enables additional strict checks (unused params, signed/unsigned compare). |
-Werror | Treats all compiler warnings as hard compilation errors! |
-g3 -O0 | Includes full debug symbol tables and disables optimization for GDB. |
Q1: How do you inspect stack frame state during a crash in GDB?
Run backtrace (or bt) to print the stack trace, then frame N to inspect local variables at frame N.
Q2: Why must compiler optimizations (-O2) be disabled for debugging?
Optimizations reorder lines and inline variables, making stepping through source lines in GDB jump around unpredictably.
Q3: What is a watchpoint in GDB?
A hardware-assisted breakpoint set with watch var that pauses execution whenever the value of `var` changes.
Q4: How do you pass command-line arguments inside GDB?
Use run arg1 arg2 or set arguments beforehand with set args arg1 arg2.
Q5: How do you view memory raw bytes at a pointer in GDB?
Use the examine command: x/10xb ptr prints 10 bytes in hexadecimal starting at `ptr`.