DEV Community

Cover image for GDB Debugger for C and C++ – Complete Guide
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

GDB Debugger for C and C++ – Complete Guide

Introduction

Debugging is a critical skill for any systems programmer working with C or C++. Unlike higher-level languages, C and C++ give you direct access to memory and low-level operations, which makes debugging both powerful and complex. The GNU Debugger (GDB) is one of the most widely used tools for debugging C and C++ programs.


What is GDB?

GDB (GNU Debugger) is a command-line debugging tool used to inspect and control the execution of programs. It allows developers to:

  • Pause execution (breakpoints)
  • Step through code line-by-line
  • Inspect variables and memory
  • Modify program state at runtime
  • Analyze crashes and core dumps

GDB works by interfacing with compiled binaries that include debugging symbols.


Installing GDB

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install gdb
Enter fullscreen mode Exit fullscreen mode

macOS (with Homebrew)

brew install gdb
Enter fullscreen mode Exit fullscreen mode

Windows

Use MinGW or WSL:

  • Install WSL and Ubuntu
  • Then install GDB inside WSL

Compiling with Debug Symbols

To use GDB effectively, you must compile your program with debug information using the -g flag:

gcc -g program.c -o program
g++ -g program.cpp -o program
Enter fullscreen mode Exit fullscreen mode

Optional flags:

  • -O0 → disable optimization (recommended for debugging)
  • -Wall → show warnings

Example:

gcc -g -O0 -Wall main.c -o main
Enter fullscreen mode Exit fullscreen mode

Starting GDB

gdb ./program
Enter fullscreen mode Exit fullscreen mode

Once inside GDB:

(gdb)
Enter fullscreen mode Exit fullscreen mode

Running the Program

run
Enter fullscreen mode Exit fullscreen mode

With arguments:

run arg1 arg2
Enter fullscreen mode Exit fullscreen mode

Breakpoints

Set breakpoint at function

break main
Enter fullscreen mode Exit fullscreen mode

Set breakpoint at line

break 10
Enter fullscreen mode Exit fullscreen mode

Set breakpoint in file

break file.c:25
Enter fullscreen mode Exit fullscreen mode

List breakpoints

info breakpoints
Enter fullscreen mode Exit fullscreen mode

Delete breakpoint

delete 1
Enter fullscreen mode Exit fullscreen mode

Stepping Through Code

Step into (enter functions)

step
Enter fullscreen mode Exit fullscreen mode

Step over (skip function internals)

next
Enter fullscreen mode Exit fullscreen mode

Continue execution

continue
Enter fullscreen mode Exit fullscreen mode

Finish current function

finish
Enter fullscreen mode Exit fullscreen mode

Inspecting Variables

Print variable

print x
Enter fullscreen mode Exit fullscreen mode

Print pointer value

print *ptr
Enter fullscreen mode Exit fullscreen mode

Display automatically

display x
Enter fullscreen mode Exit fullscreen mode

Show all local variables

info locals
Enter fullscreen mode Exit fullscreen mode

Working with Memory

Examine memory

x/4x ptr
Enter fullscreen mode Exit fullscreen mode

Format explanation:

  • 4 → number of units
  • x → hexadecimal format

Examples:

x/10d ptr   # decimal
x/5c ptr    # characters
Enter fullscreen mode Exit fullscreen mode

Call Stack (Backtrace)

When a crash occurs:

backtrace
Enter fullscreen mode Exit fullscreen mode

This shows the function call chain.

Navigate stack:

frame 0
frame 1
Enter fullscreen mode Exit fullscreen mode

Watchpoints

Watchpoints track variable changes.

watch x
Enter fullscreen mode Exit fullscreen mode

Breaks when x changes.


Conditional Breakpoints

break 20 if x == 5
Enter fullscreen mode Exit fullscreen mode

Modifying Variables

set variable x = 10
Enter fullscreen mode Exit fullscreen mode

Debugging Segmentation Faults

Compile with -g, then run:

gdb ./program
run
Enter fullscreen mode Exit fullscreen mode

When it crashes:

backtrace
Enter fullscreen mode Exit fullscreen mode

Check variables:

print ptr
Enter fullscreen mode Exit fullscreen mode

Common causes:

  • Null pointer dereference
  • Out-of-bounds array access
  • Use-after-free

Core Dumps

Enable core dumps:

ulimit -c unlimited
Enter fullscreen mode Exit fullscreen mode

Run program → crash generates core file.

Analyze:

gdb program core
Enter fullscreen mode Exit fullscreen mode

Debugging Multi-file Projects

Compile all files with -g:

gcc -g main.c utils.c -o app
Enter fullscreen mode Exit fullscreen mode

Then debug normally.


Working with C++ in GDB

GDB supports:

  • Classes
  • Objects
  • Templates
  • STL containers (with limitations)

Print object:

print obj
Enter fullscreen mode Exit fullscreen mode

Pretty printing (for STL):

set print pretty on
Enter fullscreen mode Exit fullscreen mode

Advanced Features

1. TUI Mode (Text UI)

gdb -tui ./program
Enter fullscreen mode Exit fullscreen mode

2. Layout Commands

layout src
layout asm
Enter fullscreen mode Exit fullscreen mode

3. Disassembly

disassemble main
Enter fullscreen mode Exit fullscreen mode

4. Reverse Debugging (if supported)

reverse-continue
Enter fullscreen mode Exit fullscreen mode

Useful GDB Shortcuts

Command Description
r run
c continue
n next
s step
bt backtrace
p print

Example Debugging Session

Code (buggy)

#include <stdio.h>

int main() {
    int *ptr = NULL;
    printf("%d\n", *ptr);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Steps

gcc -g main.c -o main
gdb ./main
break main
run
next
print ptr
Enter fullscreen mode Exit fullscreen mode

You will see ptr = 0x0 → null pointer → crash.


Best Practices

  • Always compile with -g
  • Disable optimization (-O0) while debugging
  • Use meaningful variable names
  • Reproduce bugs consistently
  • Use watchpoints for tricky bugs
  • Learn keyboard shortcuts

Conclusion

GDB is an essential tool for any C/C++ developer. Mastering it allows you to deeply understand program execution, memory behavior, and runtime issues. While it may feel complex initially, consistent practice will make debugging faster and more efficient.

If you are serious about systems programming, learning GDB is not optional—it is fundamental.

Top comments (0)