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
Key Facts
- GDB allows you to inspect variables, memory, and registers.
- You can set breakpoints to pause program execution at specific lines.
- GDB enables stepping through code line by line or function by function.
- It can analyze core dumps to understand program crashes.
- GDB supports various programming languages, including C, C++, and Fortran.
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_programOnce compiled, you can start GDB by running it with your program's executable:
gdb ./my_programThis 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
run(orr): Starts the execution of your program.start: Starts your program and stops at the first line of themainfunction.continue(orc): Resumes execution after a breakpoint or interruption.next(orn): Executes the current line and stops at the next line in the current function. It steps over function calls.step(ors): Executes the current line and stops at the next executable line, stepping into function calls.finish: Continues execution until the current function returns, then stops.quit(orq): Exits GDB.
Breakpoints
Breakpoints are essential for pausing program execution at specific points to inspect its state. You can set breakpoints in several ways:
break(orb): Sets a breakpoint at a specific line number in the current file.break(orb): Sets a breakpoint at the beginning of a function.info breakpoints: Lists all active breakpoints.delete(ord): Removes a breakpoint.disable: Temporarily disables a breakpoint.enable: Re-enables a disabled breakpoint.
Inspecting Data
Once your program is paused, you can examine the state of variables and memory:
print(orp): Displays the value of a variable.print $: Displays the value of a CPU register (e.g.,print $eax).x/: Examines memory at a given address. The format can specify how to display the memory (e.g.,x/10xw 0x12345678to view 10 words in hexadecimal starting from the address).info locals: Displays the values of local variables in the current stack frame.info args: Displays the arguments passed to the current function.
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.
backtrace(orbt): Displays the call stack, showing the sequence of function calls.frame(orf): Switches the context to a different stack frame, allowing you to inspect variables and code from that point in the call history.up: Moves one frame up the call stack.down: Moves one frame down the call stack.
Advanced Features
- Watchpoints: Break execution when a variable's value changes. Use
watch. - Core Dumps: Analyze the state of a program after it has crashed by loading the core dump file into GDB (e.g.,
gdb ./my_program core). - Attaching to Running Processes: Debug a program that is already running using
gdb attach. - Reverse Debugging: Some versions of GDB (like rr) support reverse debugging, allowing you to step backward through execution.
Tips for Effective Debugging with GDB
- Compile with
-g: Always include the-gflag during compilation to ensure debugging symbols are generated. - Start Simple: Begin by setting breakpoints at the beginning of functions or key sections of your code.
- Inspect Regularly: Use
printandinfo localsfrequently to check variable values. - Understand the Stack: Use
backtraceandframeto navigate the call stack and understand program flow. - Read GDB Documentation: The GDB manual is extensive and provides detailed information on all its features. You can access it within GDB by typing
helporhelp.
Mastering GDB takes practice, but its power and flexibility make it an indispensable tool for diagnosing and resolving bugs in complex software systems.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- GDB Manual - GNU ProjectGFDL-1.3-or-later
- GDB Online DocumentationGFDL-1.3-or-later
- GDB - WikipediaCC-BY-SA-3.0
Missing an answer?
Suggest a question and we'll generate an answer for it.