GNU Debugger (GDB) is an open-source debugger for GNU Systems. The debugger is portable and can be used for multiple languages as C/C++ and Fortran. It can be used for debugging programs by observing their states on specific breakpoints and even altering the flow or values for further execution. Usually, the GDB runs in command-line but several GUI has been developed for it as well.
In this article, we explore how to debug C programs using GDB in Ubuntu 20.04 LTS (Focal Fossa).
Note: The commands discussed in this article have been tested on Ubuntu 20.04 LTS (Focal Fossa).
Install prerequisite GN packages for compiling and debugging. Run the following command in the terminal:
$ sudo apt install gcc gdb -y
Before running, a program needs to be compiled. We are going to compile the following C code in the file main.c.
#include <stdio.h>
int main() {
for (int i=0; i<5; ++i) {
printf("Iterator: %d\n", i);
}
return 0;
} Usually, a C code is compiled in GCC using the following command:
$ gcc main.c -o bin
Another argument needs to be provided to include symbols in the binary. These symbols are used by GDB to track and debug the program. Run the following command in terminal to compile the C code:
$ gcc -g main.c -o bin
An executable file named bin will appear.
The binary file named bin can be executed like any other executable file on a command-line interface. Use the following command to run it in terminal:
$ ./bin
The output of the code will appear.
Run the GDB utility using following command in the terminal:
$ gdb bin
Press enter. The console for GDB terminal will appear. Enter the run command in this console to run the executable provided to the utility as an argument.
(gdb) run
Breakpoints can be added in several ways. We will be adding a breakpoint on the printf function in our code. Run the following command in terminal to add a breakpoint:
(gdb) break printf
Alternatively, a line number can be used to add a breakpoint as well.
(gdb) break 6
Enter the run command and the program will stop at the breakpoint.
Use the command continue to continue the execution of the program.
(gdb) continue
There are two other commands for different purposes of continuing the execution of the program:
Abbreviations of commands can also be used. Like abbreviation of continue command is c.
(gdb) c
Information about breakpoints can be observed using info command of gdb. Run the following command the terminal:
(gdb) info breakpoints
The information about breakpoints will appear.
Note: The number on the left of the breakpoint is used to refer to it by other commands.
A breakpoint can be deleted using the delete command and by referring to the breakpoint number observed in the output of the info utility.
(gdb) delete 1
Now the breakpoint has been deleted and if run, the program will execute straight to the end.
Variables can be watched using the watch utility. First, we need to enter the scope in which the variable exists. For this purpose, add a breakpoint first using the following command:
(gdb) break 6
Then run the code that hits this breakpoint.
(gdb) r
Now we are in the loop where the variable i exists.
The watch command will be used to observe the previous and new value of the variable i in the loop.
(gdb) watch i
Now the breakpoint generated by watch command will appear in the list of breakpoints as well. The list of breakpoints can be shown using the following command:
(gdb) info breakpoints
Moreover, we do not need the breakpoint inserted earlier. It can be easily removed using the following command:
(gdb) delete 1
Now if continued, the code will view values whenever the variable has changed the value and show both old and new values.
(gdb) c
Further iterations of the program can be observed as well, using the same command.
Run the following command in the terminal to exit the debugger.
(gdb) quit
This close gdb utility and the default command-line prompt will appear.
In this article, we explored how to run and break a program in GDB. Moreover, it was also configured to break itself when the value of a variable has changed. We hope you can easily debug your programs in GDB after following this article.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…