๐ C Debugging with GDB โ A Developer’s Guide to Runtime Troubleshooting
๐งฒ Introduction โ Why GDB Matters in C Programming
When writing C programs, bugs such as segmentation faults, unexpected output, or memory corruption are inevitable. Thatโs where GDB (GNU Debugger) comes in. GDB is a powerful command-line debugging tool that helps developers inspect, monitor, and control the execution of C programs.
๐ฏ In this guide, youโll learn:
- What GDB is and how it works
- How to set breakpoints and step through code
- How to inspect variables and memory
- Use cases for crash diagnosis and runtime analysis
๐งช What Is GDB?
GDB stands for GNU Debugger, a tool used to debug C and C++ programs. It allows you to:
- Pause execution at specific points (breakpoints)
- Step through your code line by line
- View variable values and memory states
- Investigate segmentation faults or crashes
- Analyze core dumps
GDB is usually used with GCC-compiled programs and runs on Linux, Unix, and Windows (via MinGW or WSL).
โ๏ธ GDB Core Commands & Features
| Command | Purpose |
|---|---|
gdb program_name | Start GDB with your compiled program |
break <line> or b | Set a breakpoint at a specific line or function |
run or r | Start program execution within GDB |
next or n | Execute the next line (without entering functions) |
step or s | Step into a function call |
continue or c | Resume execution until the next breakpoint |
print var or p var | Print the value of a variable |
backtrace or bt | Show call stack trace |
quit | Exit GDB |
๐ป Example โ Debugging a Segmentation Fault
#include <stdio.h>
int main() {
int *ptr = NULL;
*ptr = 5; // Intentional segfault
return 0;
}
๐ Steps to Debug with GDB:
- Compile with debug info:
gcc -g program.c -o program - Start GDB:
gdb ./program - Run the program inside GDB:
(gdb) run - GDB will stop at the fault and display the location of the error:
Program received signal SIGSEGV, Segmentation fault. - View stack trace:
(gdb) backtrace - Inspect variable:
(gdb) print ptr
๐ Real-World Use Cases
| Scenario | Why GDB Helps |
|---|---|
| Segmentation faults | Pinpoints exact crash line and call stack |
| Variable tracing | Shows real-time variable state at any point |
| Memory corruption | Helps detect invalid reads/writes |
| Loop diagnostics | Trace loop conditions and logic paths |
| Debugging core dumps | Inspect post-crash memory states (gdb prog core) |
๐ก Best Practices & Tips
๐ก Always compile with -g for debug symbols:
gcc -g mycode.c -o mycode
๐ก Use break main or break <function> to stop at meaningful places early.
โ ๏ธ Avoid using next blindly inside loopsโuse watch or condition for deeper control.
๐ Use GDB scripts to automate repetitive debugging steps.
๐ก You can debug core dumps by running:
gdb ./myprogram core
๐ Summary โ Recap & Next Steps
GDB is an essential tool in every C programmer’s toolbox. It empowers you to identify, trace, and fix runtime errors with precision and control.
๐ Key Takeaways:
- Use
break,step,next,print, andbacktraceto navigate runtime behavior. - Compile with
-gto include debug symbols. - Analyze core dumps for post-crash investigations.
- Use GDB for real-time and post-mortem debugging.
โ๏ธ Real-World Relevance:
Widely used in embedded development, systems programming, Linux kernel modules, and performance tuning.
โ Frequently Asked Questions (FAQ)
โ What is GDB used for?
โ GDB is used to pause and analyze the state of a C program during execution to debug errors like segmentation faults, incorrect logic, or crashes.
โ How do I compile a C program for debugging?
โ
Use the -g flag with GCC:
gcc -g mycode.c -o mycode
โ Whatโs the difference between step and next?
โ
step enters into functions, while next executes the function without stepping into it.
โ Can I use GDB with code compiled in release mode?
โ
Technically yes, but without -g, symbol names and line numbers won’t be availableโmaking debugging difficult.
โ How do I debug a core dump with GDB?
โ Run:
gdb ./program core
Then use bt to see the stack trace.
Share Now :
