GDB Debugger for C Under Linux

The debugger is a special software used to analyze the behavior of another program in order to identify and eliminate any errors. In computer science we speak of “bug“, a word from which it derives debugging which literally means “eliminate mistakes.” The debugger allows you to do the following:

  • execute a program at a time
  • inspect and modify the value of variables
  • to stop the program when it comes to certain lines of source code
  • control the flow of the program, and then order in which the functions are called
  • parse a file “core

The errors found can be divided into two categories:

  • those for which the program does not run as expected
  • those that block the program, sending him crashing

Rather than placing the output code between the lines to find the source of the error you prefer to use a debugger. In this article we will cover the GNU debugger available for almost all major Unix and Linux systems, this is the GDB (GNU Debugger).
To illustrate the operation of the GNU debugger, we will use a small C program that does nothing but add the value of two variables and print the resulting value, call our file test.c.

#Include <stdio.h>

void main ()
{
   int x, y, t;

   x = 12;
   y = 46;

   t = x + y;

   printf ("The value of the sum %d plus %d is %d\n", x, y, t);
}

Now compile the program with the compiler that we have available, gcc, which we discussed in a previous article. For the debugger to run, you must compile with the-g compiler in such a way as to have the symbols and other useful information in the executable, which in fact will be slightly larger.

gcc-g-o test test.c

To start the debugger with our sample program to analyze give the command:

gdb test

At this point you enter the debugger shell where we can enter commands, the numerous series of commands can be listed with help, specifying the topic below to view. At the prompt, you can use the cursor keys to scroll through the history of all the commands entered, the tab key to have available the auto-complete function when you type the name of a command, variable or function. Pressing the Enter key on the blank line will cause the repetition of the previous command. The program is executed with the run command, followed by the arguments that we want to go, but in this way the program will be executed without disruption giving us the opportunity to analyze the values ​​of variables at certain points of the source. To run it step by step you need to enter a breakpoint, or a particular point in the program that, when met by gdb, pauses program execution allowing you to re-enter commands from the command line. A breakpoint can be specified using the breakpoint, break, or simply using the letter “b” followed by the line number of the program or the name of a function. We set a breakpoint on the main function.

(Gdb) break main

At this point we can launch the program

(Gdb) run test

the debugger stops execution of the program just entered the function specified by us showing the arguments and the next line of the program which will be executed. At this point, the debugger is stopped waiting for our command, which can be:

  • n, next – to advance to the next line of the program without going into sub-functions
  • s, step – just like next, but entering the sub-functions
  • p, print – to view or assign the content of a variable
  • l, list – to view the source code of the program
  • finish – to complete the execution of the program until the end of the current function
  • c, continue – to continue the execution of the program until the next breakpoint
  • quit – to exit the debugger

then we give the next command and advance one line of code. We can see the value of each variable with print and the name of the variable.
To see the list of all breakpoints inserted using the command info break, and to delete a breakpoint using the delete command followed by the number of breakpoints. Another command that comes in handy is the display, which displays the value of variables as we go into the debug the application. Now we can exit the debugger with quit.

This entry was posted in Linux. Bookmark the permalink.