How to gdb

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: GDB, the GNU Debugger, is a powerful command-line tool used to debug programs written in languages like C, C++, and Fortran. It allows you to examine what is happening inside your program while it executes or what it was doing at the moment it crashed.

Key Facts

What is GDB?

GDB, short for the GNU Debugger, is a free and open-source command-line debugging tool that allows programmers to understand and fix errors in their code. It is widely used for debugging programs written in languages such as C, C++, Objective-C, Fortran, and Assembly. GDB enables you to see what is going on inside your program while it executes or at the moment it has stopped. It's an essential tool for any developer working with compiled languages on Unix-like systems.

Why Use GDB?

Debugging is a critical part of the software development lifecycle. While many Integrated Development Environments (IDEs) offer graphical debuggers, GDB provides a robust and flexible alternative, especially for developers working in terminal environments, on remote servers, or when dealing with complex issues that graphical tools might obscure. It offers deep insight into program execution, memory state, and system interactions.

Getting Started with GDB

To use GDB, you first need to compile your program with debugging information. This is typically done by adding the -g flag to your compiler command. For example:

gcc -g my_program.c -o my_program

Once compiled, you can start GDB by running it with your program's executable:

gdb ./my_program

This will launch the GDB prompt, which usually looks like this: (gdb).

Common GDB Commands

Here are some of the most frequently used GDB commands:

Running and Controlling Execution

Breakpoints

Breakpoints are essential for pausing program execution at specific points to inspect its state. You can set breakpoints in several ways:

Inspecting Data

Once your program is paused, you can examine the state of variables and memory:

Stack and Backtraces

When your program is stopped, the call stack shows the sequence of function calls that led to the current point. This is crucial for understanding how you got there.

Advanced Features

Tips for Effective Debugging with GDB

Mastering GDB takes practice, but its power and flexibility make it an indispensable tool for diagnosing and resolving bugs in complex software systems.

Sources

  1. GDB Manual - GNU ProjectGFDL-1.3-or-later
  2. GDB Online DocumentationGFDL-1.3-or-later
  3. GDB - WikipediaCC-BY-SA-3.0

Missing an answer?

Suggest a question and we'll generate an answer for it.