GDB is GNU Project Debugger its basically is a debugger and allows you to see inside the program flow, control structure, working, code and we will see how we can do that.
We will we using a basic debugMe.c program throughout this tutorial
helloworld.c
Compile the file with -ggdb flag with gcc
[/code]
To start GDB open your shell :-
enter gdb (followed by the name of the program)
And you should get some out put like the following :-
GDB displays its prompt and now we are ready to enter GDB commands..
Lets first list(print the source) the program :-
Now I would like your will and explain a little about breakpoints..
What are Breakpoints
There are 2 types of Breakpoints :-
Breakpoints are the instructions that pause the control flow of the program and gives the control to the debugger...We all know that the main executable is nothing but some opcodes(hex numbers) The debugger adds a opcode before a function or in the middle of the function (as desired) ..
Now that we know something about the Breakpoints lets use it in our program :-
Lets put a breakpoint at very beginning of the program..And trace what's happening
We successfully added a breakpoint and now lets run the program rest the GDB output is quite self-explanatory
The syntax of run is as follows :-
So lets use this to run our program :-
As we run the program we reach the very first breakpoint of our program...
The out put by GDB is pretty self-explanatory..
Lets first examine our local variables :-
It can be done with 'info locals'
The variables contains garbage values this is because we have not initialized our variables till..
Now as we hit our breakpoint the program pauses lets step through our program now..
Note : if you are debugging large programs with lots and lots of functions you can use 'next' command but as we are using only a small program with only 10 lines of ….We can use step
Using 'step' command
Syntax :-
Note : Step's syntax is exactly similar to next's syntax
Here n is the no of times to step..
Note : The step command displays the instruction to be stepped upon...Its not already been stepped upon
Lets step 2 instructions down
Now that we have declared all our variables lets check our local variables again..
As expected we get the values of these variables as we specified in the source...
We can also use 'print' command to print the value of our variable and the memory address pointed by it
Syntax :-
Lets use it now
As expected we get the contents of the string..
Now its kind off surprising … we get only 'H'
The reason to this is that GDB gets the memory address of the string and displays the contents of that memory address...
For eg :-
Suppose there is a string:-
Hello[] = “lionaneesh”
Now lets suppose that this string sits in a memory address :-
So :-
The memory map would look like
…..... and so on
So ,
the statement is similar to :-
Which prints 'l'
Now that we have examined many of our variables and much of our program lets
Continue the execution flow and exit
Quiting :-
After this you should get your shell prompt back!!!
I hope this tutorial is enough to get you started working in GDB!!
How to work with GDB
We will we using a basic debugMe.c program throughout this tutorial
helloworld.c
Code:
#include<stdio.h>
int main()
{
// some variable declarations just to demonstrate
int i = 1337;
char string[] = "Hello Wassup!!\n";
printf("%s\n",string);
}
Code:
aneesh@aneesh-laptop:~/articles$ gcc debugMe.c -o debugMe -ggdb
To start GDB open your shell :-
enter gdb (followed by the name of the program)
And you should get some out put like the following :-
Code:
aneesh@aneesh-laptop:~/articles$ gdb debugMe GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i486-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/aneesh/articles/debugMe...done. (gdb)
Lets first list(print the source) the program :-
Code:
(gdb) list
1 #include<stdio.h>
2
3 int main()
4 {
5 // some variable declarations just to demonstrate
6 int i = 1337;
7 char string[] = "Hello Wassup!!\n";
8
9 printf("%s\n",string);
10 }
(gdb)
What are Breakpoints
There are 2 types of Breakpoints :-
- Hardware Breakpoints
- Software Breakpoints
Breakpoints are the instructions that pause the control flow of the program and gives the control to the debugger...We all know that the main executable is nothing but some opcodes(hex numbers) The debugger adds a opcode before a function or in the middle of the function (as desired) ..
Now that we know something about the Breakpoints lets use it in our program :-
Lets put a breakpoint at very beginning of the program..And trace what's happening
Code:
(gdb) break main Breakpoint 1 at 0x804844d: file debugMe.c, line 4.
The syntax of run is as follows :-
Code:
run (arguments to the program)
Code:
((gdb) run
Starting program: /home/aneesh/articles/debugMe
Breakpoint 1, main () at debugMe.c:4
4 {
(gdb)
The out put by GDB is pretty self-explanatory..
Lets first examine our local variables :-
It can be done with 'info locals'
Code:
(gdb) info locals i = 134513856 string = "X\364\377\277\245\324\025\000\060\340\021\000˄\004\b" (gdb)
Now as we hit our breakpoint the program pauses lets step through our program now..
Note : if you are debugging large programs with lots and lots of functions you can use 'next' command but as we are using only a small program with only 10 lines of ….We can use step
Using 'step' command
Syntax :-
Code:
step [n]
Here n is the no of times to step..
Code:
(gdb) step 6 int i = 1337; (gdb)
Lets step 2 instructions down
Code:
(gdb) step 2
9 printf("%s\n",string);
Code:
(gdb) info locals i = 1337 string = "Hello Wassup!!\n"
We can also use 'print' command to print the value of our variable and the memory address pointed by it
Syntax :-
Code:
print I // prints the contents on variable I print *I // prints the data in the memory address pointed to it
Code:
(gdb) print string $4 = "Hello Wassup!!\n"
Code:
(gdb) print *string $5 = 72 'H' (gdb)
The reason to this is that GDB gets the memory address of the string and displays the contents of that memory address...
For eg :-
Suppose there is a string:-
Hello[] = “lionaneesh”
Now lets suppose that this string sits in a memory address :-
Code:
0x12345678
The memory map would look like
Code:
0x12345678 'l' 0x12345679 'i' 0x12345680 'o' 0x12345681 'n' 0x12345682 'a' 0x12345683 'n' 0x12345684 'e' 0x12345685 'e' 0x12345686 's'
So ,
Code:
print *string
Code:
print 0x12345678
Now that we have examined many of our variables and much of our program lets
Continue the execution flow and exit
Code:
(gdb) continue Continuing. Hello Wassup!! Program exited with code 020. (gdb)
Code:
(gdb) quit aneesh@aneesh-laptop:~/articles$
I hope this tutorial is enough to get you started working in GDB!!


