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).

Prerequisites

  • Ubuntu 20.04 system
  • User with sudo privileges for renaming multiple files.

Note: The commands discussed in this article have been tested on Ubuntu 20.04 LTS (Focal Fossa).

Installing Packages

Install prerequisite GN packages for compiling and debugging. Run the following command in the terminal:

$ sudo apt install gcc gdb -y

Install GDB Debugger

C-Program example for debugging

Code

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;
}

Example program written in C

Compile C Program using GCC

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

Compile program

An executable file named bin will appear.

Execute the test program

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

Execute the test application

The output of the code will appear.

Debugging an Application on Linux

Initiate Debugger

Run the GDB utility using following command in the terminal:

$ gdb bin

Debug program using GDB

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

Debug output

Debug Breakpoints

Add Breakpoints

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

Add Breakpoint in GDB

Alternatively, a line number can be used to add a breakpoint as well.

(gdb) break 6

Add breakpoint by line number in GDB

Enter the run command and the program will stop at the breakpoint.

Debugging programs with breakpoints

Step through Breakpoints

Use the command continue to continue the execution of the program.

(gdb) continue

GDB continue command

There are two other commands for different purposes of continuing the execution of the program:

  • Step: steps through the next machine instruction.
  • Next: steps to through the next line of code.

Abbreviations of commands can also be used. Like abbreviation of continue command is c.

(gdb) c

shortcut for continue command

Information About Breakpoints

Information about breakpoints can be observed using info command of gdb. Run the following command the terminal:

(gdb) info breakpoints

Breakpoint info

The information about breakpoints will appear.

Note: The number on the left of the breakpoint is used to refer to it by other commands.

Delete Breakpoints

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

Deleting Btreakpoints in GDB

Now the breakpoint has been deleted and if run, the program will execute straight to the end.

Watch Variables

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

Show variable content

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

Watch value of a variable using GDB

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

GDB info breakpoints

Moreover, we do not need the breakpoint inserted earlier. It can be easily removed using the following command:

(gdb) delete 1

Delete the breakpoint

Now if continued, the code will view values whenever the variable has changed the value and show both old and new values.

(gdb) c

Continue debugging

Further iterations of the program can be observed as well, using the same command.

Watch iterations

Quit Debugger

Run the following command in the terminal to exit the debugger.

(gdb) quit

Quit debugging

This close gdb utility and the default command-line prompt will appear.

Conclusion

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.

How to Use GDB to Debug Programs in Ubuntu 20.04

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn