GNU Debugger(GDB)
Introduction
A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise. GDB allows you to run the program up to a certain point, then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.
Errors like segmentation faults may be easier to find with the help of GDB.
GDB allows you to:-
- Pause and continue its execution
- Set "break points" or conditions where the execution pauses so you can look at its state (the value of the variables at that point).
- View and "watch" variable values
- Step through the program line-by-line (or instruction by instruction)
Installation
Docker Based Setup
If you followed the Docker-based setup, GDB should have already been included in the docker image you built and will be available in the container. You can proceed to the section on using GDB.
Manual Setup
If you followed the manual setup process, you might have to install GDB. Before you do, check whether it is already present in your system by running the following command.
gdb -help
If you have already installed GDB, then it will display all the available options within your GDB, else if the terminal says "command not found", then you can proceed with the installation process.
- Ubuntu / Debian
- Fedora / Red Hat
- Arch Linux
sudo apt update
sudo apt-get install -y gdb
sudo yum install gdb
sudo pacman -Sy gdb
Now you can confirm the installation of GDB by executing the command gdb -help
again.
Using GDB
You have to tell your compiler to compile your code with symbolic debugging information included. Here's how to do it with gcc, using the -g flag.
g++ -g nitcbase.cpp -o nitcbase
The Makefile provided with NITCbase supports compiling in debug mode by running it as
make mode=debug
This will create the nitcbase-debug
executable.
Once you've done that, you should be able to debug your program in the debugger.
Your text editor/IDE might already come with debug functionality built-in. You can find below config for various text editors and IDEs to take advantage of their frontends.
- VSCode
- Terminal
- Emacs
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug nitcbase",
"cwd": "${workspaceFolder}",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/nitcbase-debug", // Binary to exec
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build debug nitcbase",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
{
"tasks": [
{
"type": "shell",
"label": "Build debug nitcbase",
"command": "/usr/bin/make",
"args": ["mode=debug"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The "Debug nitcbase" task can be launched from the "Run and Debug" menu.
If you prefer to keep it within the terminal itself, you can use gdb with the -tui
flag.
gdb -tui nitcbase-debug
# nitcbase-debug is the executable here
looking for contributions
There's also the possibility of using the gdb prompt (that is, the "dumb terminal mode") which might be harder to use than the earlier mentioned options.
You could find online documentation for how to use the debugger in your editor of choice, or just jump right into it and figure it out as you go.
If you feel you need a general overview of the usage of GDB, Beej's Quick Guide to GDB will be helpful.
Using GDB in a Docker Container
This section explains how to use GDB from within a docker container. If you followed the Docker-based setup, it is recommended to keep reading.
For working with the VSCode frontend for GDB, you will need to attach to the nitcbase container and then launch the debugger from within the container. You might be prompted to install the C/C++ VSCode extension the first time you run the debugger.
For terminal-based usage, gdb can be accessed from a shell in the container.