Debugging with GDB & Sanitizers in C
Debug builds compile with -g (symbols) and -O0 (no reordering) so GDB maps crashes to source lines, prints variables, and steps execution. The core loop is tiny: break on a line, run, inspect (print x, backtrace), step (next over, step into), continue.
Sanitizers find what debuggers can't see: -fsanitize=address catches overflows, use-after-free, and leaks; -fsanitize=undefined catches signed overflow and misaligned access. Run test suites under both regularly — silent corruption becomes loud, located errors.
Build debuggable
gcc -g -O0 app.c — symbols for names/lines, no optimization moving code from under the debugger.
Break, run, inspect
break main, run, print x, backtrace, next/step, continue — six GDB commands covering nearly every session.
AddressSanitizer for memory
-fsanitize=address aborts on the exact overflowing line with a full report — overflows, use-after-free, leaks.
UBSan for undefined behavior
-fsanitize=undefined flags signed overflow and misaligned access — bugs that otherwise behave impossibly.
$ gcc -g -O0 app.c -o app$ gdb ./app(gdb) break main(gdb) run(gdb) print x$ gcc -fsanitize=address app.c && ./a.outRecommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.