What is GDB in C language

How to Debug C Programs in Linux with gdb

No matter how skilled you are as a programmer, all software you develop cannot be completely bug-free. Therefore, identifying errors and correcting them is one of the most important tasks in the software development cycle. While there are many ways to identify bugs (testing, code self-checking, and more), there is special software - called debuggers - that can help you understand exactly what the problem is so that you can easily fix it.

If you are a C / C ++ programmer or develop software using the Fortran and Modula-2 programming languages, you will be pleased to know that there is an excellent debugger called GDB that you can use to easily troubleshoot your code for bugs and other problems can investigate. In this article, we're going to discuss the basics of GDB, including some of the useful features / options it offers.

But before we go any further, it's worth noting that all of the instructions, as well as the examples presented in this article, have been tested on Ubuntu 14.04LTS. The example code used in the tutorial is written in the C language; the command line shell we use is bash (version 4.3.11); and the GDB version we are using is 7.7.1.

GDB Debugger Basics

In layman's terms, GDB allows you to peek inside a program while the program is running, something that will help you identify the exact problem. We'll discuss using the GDB debugger with a working example in the next section, but before that we're going to cover some basic points that will help you later.

First, in order to successfully use debuggers like GDB, you must compile your program so that the compiler also produces debugging information that the debuggers need. For example, in the case of the gcc compiler, which we will use to compile the sample C program later in this tutorial, you will need to use the command line option while compiling your code -G use.

To see what the gcc compiler manual page says about this command line option, continue here.

The next step is to make sure you have GDB installed on your system. If that's not the case and you are on a Debian based system like Ubuntu, you can simply install the tool with the following command:

sudo apt-get install gdb

To install on a different distro, go here.

Once you've compiled your program for debugging and have GDB on your system, you can run your program in debug mode with the following command:

gdb [prog-executable-name]

While this will initiate the GDB debugger, your executable will not start at this point. This is when you can define your debugging-related settings. For example, you can define a breakpoint that tells GDB to pause program execution at a specific line number or function.

To actually start your program you need to run the following gdb command:


It's worth noting that you can specify here if your program needs some command line arguments to be passed to it. For example you can

run [arguments]

The GDB has many useful commands that are useful when debugging. We'll discuss some of these in the example in the next section.

Example of using GDB

Now we have a basic idea about GDB and how to use it. So let's take an example and apply the knowledge there. Here is a sample code:


int main ()
int out = 0, tot = 0, cnt = 0;
int val [] = {5, 54, 76, 91, 35, 27, 45, 15, 99, 0};

while (cnt <10)
out = val [cnt];
tot = tot + 0xffffffff / out;
cnt ++;

printf ("\ n Total = [% d] \ n", dead);
return 0;

So basically this code takes every value contained in the 'val' array, assigns it to the 'out' integer and then calculates 'tot' by adding up the previous value of the variable and the result of '0xffffffff / out' .

The problem here is that when the code runs, the code produces the following error:

$ ./gdb-test Floating point exception (core dumped)

So, to debug the code, the first step would be to compile the program with -g. Here is the command:

gcc -g -wall gdb-test.c -o gdb-test

Next, we'll run GDB and let it know which executable we want to debug. Here is the command for it:

gdb ./gdb-test

Well, the error I get is "Floating Point Exception," and as most of you may already know, it is caused by n% x when x is 0. So with that in mind, I set a breakpoint on line number 11 where the division takes place. This was done in the following way:

(gdb) break 11

Notice that "(gdb)" is the debugger prompt, I just wrote the 'break' command.

Well I asked GDB to start running the program:


So when the breakpoint was hit for the first time, the GDB showed the following in the output:

Breakpoint 1, main () at gdb-test.c: 11 11 tot = tot + 0xffffffff / out; (gdb)

As you can see in the output above, the debugger showed the line where the breakpoint was set. Now let's print the current value of 'out'. This can be done in the following ways:

(gdb) print out$1 = 5 (gdb)

As you can see, the value '5' was printed, so everything is fine at the moment. I asked the debugger to continue executing the program until the next breakpoint, which can be done with the 'c' command.


I kept going until I saw that the value of 'out' was zero.

... ... ... Breakpoint 1, main () at gdb-test.c: 11 11 tot = tot + 0xffffffff / out; (gdb) print out $ 2 = 99 (gdb) c Continuing.

Breakpoint 1, main () at gdb-test.c: 11
11 tot = tot + 0xffffffff / out;
(gdb) print out
$3 = 0

Now, to confirm that this is the exact problem, this time I used GDB's ’s’ (or ’step’) command instead of the “c”. The reason for this is that I only wanted to run line 11, where program execution is currently paused, to see if a crash occurs at that point.

This is what happened:

(gdb) s

Program received signal SIGFPE, Arithmetic exception.
0x080484aa in main () at gdb-test.c: 11
11 tot = tot + 0xffffffff / out;

Yes, as confirmed by the edition highlighted above, the exception was thrown here. The final confirmation came when I tried the command ’s’ again:

(gdb) s

Program terminated with signal SIGFPE, arithmetic exception.
The program no longer exists.

So this way you can debug your programs with GDB.


We have only scratched the surface here, because the GDB offers a lot of functions that can be explored and used by the users. Go through the GDB man page to learn more about the tool and try to use it whenever you want to debug something in your code. There is some learning curve involved in the debugger, but it's worth the hard work.