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.
$ gcc -g -o test test.c
Once we compiled, we can begin debugging by invoking gdb with the name of the compiled file.
$ gdb test
GNU gdb 5.3-debian
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-linux"...
(gdb)
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.
(gdb) l
1 #include <stdio.h>
2
3 int main() {
4
5 int v = 0;
6 int i;
7
8 for (i = 0; i < 5; i++) {
9 v += i;
10 }
(gdb)
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.
(gdb) r
Starting program: /ramdisk/home/knoppix/test
Resultado: 10
Program exited normally.
(gdb)
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.
(gdb) b 9
Breakpoint 1 at 0x804834e: file test.c, line 9.
(gdb)
Now we have a break point on line 9, if we now run the program it will stop at that line.
(gdb) r
Starting program: /pymelab/home/gdb/test
Breakpoint 1, main () at test.c:9
9 v += i;
(gdb)
To continue the execution of the program, we need to use the command _continue or _c
(gdb) c
Continuing.
Breakpoint 1, main () at test.c:9
9 v += i;
(gdb) c
Continuing.
Breakpoint 1, main () at test.c:9
9 v += i;
(gdb) c
Continuing.
Breakpoint 1, main () at test.c:9
9 v += i;
(gdb) c
Continuing.
Breakpoint 1, main () at test.c:9
9 v += i;
(gdb) c
Continuing.
Resultado: 10
Program exited normally.
(gdb)
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
(gdb) r
Starting program: /ramdisk/home/knoppix/test
Breakpoint 2, main () at test.c:9
9 v += i;
(gdb) n
8 for (i = 0; i < 5; i++) {
(gdb) n
Breakpoint 2, main () at test.c:9
9 v += i;
(gdb)
At any time we can see the value of a variable with the command (p) rint followed by the name of the variable.
(gdb) p v
$1 = 1
(gdb) c
Continuing. Breakpoint 2, main () at test.c:9
9 v += i;
(gdb) c
Continuing.
Breakpoint 2, main () at test.c:9
9 v += i;
(gdb) p v
$2 = 6
(gdb)
Finally, to exit the debugger we use the command (q) uit.