GDB Cheat Sheet and Quick Start Guide

If you’ve ever worked with C or C++, you’ve likely heard of GDB (GNU Debugger). GDB is a powerful tool that allows you to see what your program is doing at any given time—or why it crashed.

In this guide, we’ll walk through the basics of using GDB, with clear examples, helpful output, and a free GDB Cheat Sheet you can download at the end!


What is GDB?

GDB lets you:

  • Run your program step-by-step.
  • Set breakpoints to pause execution.
  • Inspect variables, memory, and the call stack.
  • Find bugs and understand crashes.

It’s an essential tool for any C or C++ developer.


Getting Started with GDB

Compiling with Debug Information

Before you can debug, compile your program with -g to include debug info.

gcc -g myprogram.c -o myprogram

Starting GDB

gdb ./myprogram

You’ll see a prompt like:

GNU gdb (GDB) 10.2
Type "help" for more information.
(gdb)

Running Your Program

(gdb) run

This executes your program from the start.


Core GDB Commands

Breakpoints

Set a breakpoint to pause execution at a specific function or line.

(gdb) break main
(gdb) break 25  # Line 25

Inspecting Variables

View variable values.

(gdb) print x
$1 = 42

Stepping Through Code

Step into functions or move line-by-line.

(gdb) step    # Step into functions
(gdb) next    # Step over functions

Listing Source Code

View the surrounding source code.

(gdb) list

Continuing Execution

After hitting a breakpoint, continue running.

(gdb) continue

Viewing the Call Stack

See the function call history.

(gdb) backtrace

Example output:

#0  my_function (x=5) at myprogram.c:12
#1  main () at myprogram.c:25

Exiting GDB

(gdb) quit

Practical Debugging Example

Here’s a simple buggy program that causes memory issues:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *buffer = malloc(5);
    strcpy(buffer, "hello");  // Buffer overflow (off-by-one)
    free(buffer);
    printf("Buffer: %s\n", buffer);  // Use after free
    return 0;
}

Steps to debug:

  1. Compile with -g:
    gcc -g bug.c -o bug
    
  2. Start GDB:
    gdb ./bug
    
  3. Run the program:
    (gdb) run
    

Modern behavior: You may see harmless-looking output like (null) or even no crash at all. Modern systems (like recent glibc and heap hardening) often make use-after-free and buffer overflows less obvious without extra tools.

In GDB: Use breakpoints and inspection tools to catch issues earlier:

  1. Set a breakpoint before free(buffer):
    (gdb) break bug.c:8
    (gdb) run
    
  2. Inspect the buffer:
    (gdb) print buffer
    (gdb) x/s buffer
    
  3. Advance over the free, and re-inspect the buffer:
    (gdb) next
    (gdb) x/s buffer
    

You’ll likely see the correct content before free(), but after freeing, accessing it is undefined behavior—a serious bug even if it doesn’t immediately crash!

Bonus Tip: Use sanitizers during compilation to catch these bugs:

gcc -g -fsanitize=address bug.c -o bug

This will detect use-after-free and buffer overflows at runtime.


A Quick Anecdote

When I was a lab assistant back in college, students would often struggle with bugs like these—use-after-free, off-by-one, segmentation faults—and feel stuck.

I made it a personal challenge: teach at least one student per lab how to use GDB. Once they learned just a few basic commands, they could debug themselves instead of waiting for help.

Most students said it felt like “seeing inside the matrix”—and once you get the hang of it, it really does!


Download the GDB Cheat Sheet

Want a handy reference you can keep nearby?

It covers:

  • Starting and running programs
  • Setting breakpoints and watchpoints
  • Navigating the call stack
  • Inspecting variables and memory
  • Useful advanced commands

Final Thoughts

GDB can seem intimidating at first, but with practice, it becomes one of the most powerful tools in your toolkit. Start with the basics, keep the cheat sheet nearby, and dive into debugging with confidence!

Happy debugging!