What Is GDB Debugger?
The GDB Debugger, also known as the GNU Project Debugger, is a powerful and versatile debugging tool for software developers. It plays a crucial role in identifying and resolving issues in programs written in various programming languages such as C, C++, Objective-C, Rust, Go, and many others. GDB’s primary function is to help programmers analyze their code during execution or after a crash.
GDB provides an interactive environment that allows engineers to monitor the internal state of a program while it’s running or after it has terminated unexpectedly. This enables them to diagnose potential problems in their codebase. For engineers working on embedded systems or IoT products, tools like GDB are essential for ensuring high quality software, as well as identifying and remediating security issues in their code.
Visit the open source project for GDB Debugger.
In this article:
GDB Key Features
Inspection and Modification of Variables
GDB Debugger lets you inspect and modify variables during runtime. This allows developers to monitor how different variables change throughout program execution without having to stop or restart the application. Moreover, they can also alter variable values on-the-fly if needed for testing purposes.
GDB Backtrace
Understanding an application’s call stack is crucial for debugging, as it reveals which functions were called leading up to an error or crash. GDB’s backtrace feature provides developers with valuable insights into function calls made within their programs at any given point in time.
The output displays the function call stack, with each line representing a function call, its corresponding arguments, file name, and line number.
GDB Disassembly
Embedded system engineers working on low-level code or debugging assembly language programs can benefit from GDB’s powerful disassembly feature. This tool allows developers to view the machine code instructions generated by their program’s source code. By examining these instructions directly, it becomes easier to identify incorrect compiler optimizations or other errors.
GDB Core Dump Analysis
When an application crashes unexpectedly due to segmentation faults or other fatal errors, it often generates a core dump—a snapshot of the program’s memory state at that moment. Analyzing this core dump can provide invaluable information about the cause of the issue within your application. GDB’s core dump analysis feature enables developers to load and inspect these memory snapshots, simplifying the process of determining the root cause of a crash.
GDB Breakpoints
Breakpoints are a fundamental debugging tool that allows developers to pause program execution at specific points in their code. This feature helps them investigate variable changes during execution or examine call stacks more closely. GDB’s breakpoints functionality offers various breakpoint types, such as line number-based, function-based, conditional, and watchpoint (memory access).
GDB Installation Guide
There are two methods to install GDB on your Linux machine.
Install Pre-built GDB Binaries from Verified Distribution Resources
To install GDB on a Debian-based Linux distribution (e.g., Ubuntu, Mint), use the following commands:
sudo apt-get update sudo apt-get install gdb
Download GDB Source Code, Compile, and Install
Follow these steps to compile GDB from scratch and install it:
Download source code
Download the source code of the latest release from http://ftp.gnu.org/gnu/gdb/
wget "http://ftp.gnu.org/gnu/gdb/gdb-7.11.tar.gz"
Extract the source code
tar -xvzf gdb-7.11.tar.gz
Configure and compile
cd gdb-7.11 ./configure make
This step might take some time. Once completed, you can find the GDB binary at gdb-7.11/gdb/gdb.
Install GDB
make install
By default, this will install GDB binaries in /usr/local/bin
and libraries in /usr/local/lib
.
That’s it! You have successfully compiled and installed GDB.
After installing GDB, you can print the GDB version to test whether it was installed correctly.
gdb --version
GDB Tutorial: Debugging a Simple C++ Application
To break down how GDB works, here is an example of a simple debugging flow of an C++ application.
For the sake of this tutorial we’ll be working with the following example code:
#include int inc(int a) { return a + 1; } int main(int argc, char** argv) { for (int i = 1; i < 5; i++) { int a = 1; a = inc(a); std::cout << "Value of a: " << a << std::endl; } return argc; }
1. Compilation and Loading
First, compile the example with debugging symbols using the -g
or -ggdb
flags:
g++ -ggdb target.cpp -o target
Then, load the target in GDB:
gdb target
2. Exploring Source Code
To view the source code, use the list
command:
(gdb) list 1,12
3. Setting Breakpoints
Set a breakpoint at the inc
function:
(gdb) break inc (gdb) run
You can delete breakpoints using the delete
command.
4. Examining Information
To view the call stack, use the backtrace
command:
(gdb) backtrace
To view local variables, use the info locals
command.
To evaluate and print expressions, use the print
command:
(gdb) print a (gdb) print a+1
5. Restarting
To stop the current run, use the kill
command:
(gdb) kill
To start debugging with a temporary breakpoint at the beginning, use the start
command:
(gdb) start
6. Stepping Through Code
To resume after a stop, use the continue
, step
, and next
commands:
(gdb) continue (gdb) step (gdb) next
7. Using Checkpoints
You can save the state of a debugging session at a given point using the checkpoint
command:
(gdb) checkpoint
To restore a checkpoint, use the restart
command:
(gdb) restart 1
To view information about checkpoints, use the info checkpoints
command:
(gdb) info checkpoints