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 :
