The normal process for developing computer programs goes something like this: write some code, compile the code, run the program. If the program does not work as expected, then you go back to the code to look for errors (bugs) and repeat the cycle again.

Depending on the complexity of the program and the nature of the bugs, there are times when you can do with some additional help in tracking down the errors. This is what a “debugger” does. It allows you to examine a computer program while it is running. You can see the values of the different variables, you can examine the contents of memory and you can halt the program at a specified point and step through the code one line at a time.

The primary debugger on Linux is the GNU debugger (gdb). It might already be installed on your system (or a slimmed down version called gdb-minimal), but to be sure type the following command in a terminal:

sudo apt-get install gdb

To use the debugger, you need to tell the compiler to include debug information in the binary. Create a file called “hello10.c” using nano:

nano hello10.c

Copy and paste in the following code:

#include <stdio.h>

main()
{
        int i;
        for(i=0;i<10;i++)
                printf("%d",i);
}

The “-g” flag tells the compiler to produce debug information, so to compile the program use:

gcc -g -o hello10 hello10.c

To start debugging the program type:

gdb hello10

At this point if you just start running the program (using the “run” command), then the program will execute and finish before you get a chance to do anything. To stop that, you need to create a “breakpoint” which will halt the program at a specified point. The easiest way to do this is to tell the debugger to stop in the function “main()”:

break main

Now start the program:

run

gdb-break-run

The debugger has stopped at the first executable line of code, e.g. the “for” loop. To step to the next line, type “next” or “n” for short. Keep using “next” to repeat around the loop a couple of times:

gdb-break-next

To inspect the value of a variable use the “print” command. In our example program, we can examine the contents of the variable “i”:

print i

Repeat around the loop and few more times and see how “i” changes:

next

next

next

next

print i

gdb-next-print-i

In the example above “i” has reached the value of 4. The loop will continue while “i” is less than 10. You can change the value of a variable using “set var.” Type the following in gdb to set “i” to 10.

set var i = 10

print i

next

gdb-set-var

You may need to do another “next” (depending on where the program was halted when you set “i” to 10), but when the “for” loop line is next executed, the loop will exit, because “i” is no longer less than 10.

The “next” command doesn’t drill down into functions but rather the function is executed, and the debugger stops again at the next line after the function. If you want to step into a function, use the “step” command, or “s” for short.

Another way to debug your program is to set a watch on a variable. What this does is halt the program whenever the variable changes. Restart the program again by typing “run.” Since the program is already running, the debugger will ask if you want to start it again from the beginning.

The program will stop in main (as we didn’t remove the break point). Now set a watch on “i”:

watch i

continue

The “continue” command starts the program running again until the next break point condition. In this case it will run again until the variable “i” changes (due to the watch).

gdb-watch-i

To stop debugging, just use the “quit” command.

If you want to learn some more about gdb, then a good place to start is the GDB Documentation. If you have any trouble with the examples given above, please use the comments section below and we will see if we can help.