Debugging Fundamentals
Debugging C is an art. Let’s master the tools that make you dangerous.GDB Essentials
Starting GDB
Basic Commands
Advanced GDB
GDB Init File
Core Dumps
Enabling Core Dumps
Analyzing Core Dumps
Triggering Core Dumps
Memory Debugging
Valgrind
Understanding Valgrind Output
AddressSanitizer
Common Memory Errors
Debugging Patterns
Printf Debugging (But Better)
Assertions
Memory Debugging Wrapper
Tracing System Calls
ltrace (Library Calls)
Debugging Crashes Systematically
1
Reproduce Consistently
Find the minimal input/steps to trigger the crash. Randomness = harder debugging.
2
Get the Stack Trace
Run in GDB or analyze core dump. Know exactly where it crashed.
3
Examine State
Check variable values, pointer validity, array bounds at crash site.
4
Find the Root Cause
The crash site is often not the bug. Trace backwards—when did state become invalid?
5
Verify Fix
Run the same reproduction steps. Then run your test suite.
Exercises
1
GDB Mastery
Write a program with a linked list. Use GDB to traverse the list, print node values, and find a bug you intentionally introduce.
2
Core Dump Analysis
Write a program that crashes. Generate a core dump and analyze it to find the crash location.
3
Memory Bug Hunt
Write a program with at least 3 different memory bugs. Use Valgrind and AddressSanitizer to find them all.
4
Debug Wrapper
Extend the debug_malloc wrapper to detect double-frees and use-after-free.
Next Up
Pointers Deep Dive
Master the heart of C programming