๐Ÿงช C Debugging, Testing & Best Practices
Estimated reading: 4 minutes 280 views

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

CommandPurpose
gdb program_nameStart GDB with your compiled program
break <line> or bSet a breakpoint at a specific line or function
run or rStart program execution within GDB
next or nExecute the next line (without entering functions)
step or sStep into a function call
continue or cResume execution until the next breakpoint
print var or p varPrint the value of a variable
backtrace or btShow call stack trace
quitExit 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:

  1. Compile with debug info: gcc -g program.c -o program
  2. Start GDB: gdb ./program
  3. Run the program inside GDB: (gdb) run
  4. GDB will stop at the fault and display the location of the error: Program received signal SIGSEGV, Segmentation fault.
  5. View stack trace: (gdb) backtrace
  6. Inspect variable: (gdb) print ptr

Real-World Use Cases

ScenarioWhy GDB Helps
Segmentation faultsPinpoints exact crash line and call stack
Variable tracingShows real-time variable state at any point
Memory corruptionHelps detect invalid reads/writes
Loop diagnosticsTrace loop conditions and logic paths
Debugging core dumpsInspect 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, and backtrace to navigate runtime behavior.
  • Compile with -g to 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 :
Share

๐Ÿž C Debugging with GDB

Or Copy Link

CONTENTS
Scroll to Top