Exercise

This tutorial have the intended to be a brief and simple practical guide to the GDB debugger.

For this tutorial we are going to use a simple c code.

#include <stdio.h>

int main() {

 int v = 0;

   int i;

 for (i = 0; i < 5; i++) {

   v += i;

 }

 printf("Resultado: %i\n", v);

 return 0;

}

The first thing we have to do is compile this program with the "-g" option so that the necessary information for debugging is included.

Once we compiled, we can begin debugging by invoking gdb with the name of the compiled file.

The command gdb at the end of the console indicates that the debugger is ready to start processing. The first command that we will see is l. This command shows the source code of the program.

For executing the program, we are going to use the command run _or _r. We will see that the program execute till the end, because we don't add a break.

Now we need to set a break point. As its name suggests, a break point is a point where the execution of our program stops. To place a breaking point we use the command breakpoint _or _b followed by the line where we want to put it.

Now we have a break point on line 9, if we now run the program it will stop at that line.

To continue the execution of the program, we need to use the command _continue or _c

Since we have a break point in the loop and it goes 5 times, we had to introduce the command continue 5 times to execute the whole program. It is also possible to run the program line by line with the command next

At any time we can see the value of a variable with the command (p) rint followed by the name of the variable.

Finally, to exit the debugger we use the command (q) uit.

For more information we can use

Last updated

Was this helpful?