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.
How to work with GDB
We will we using a basic debugMe.c program throughout this tutorial
// some variable declarations just to demonstrate
int i = 1337;
char string = "Hello Wassup!!\n";
Compile the file with -ggdb flag with gcc
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 :-
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:
Reading symbols from /home/aneesh/articles/debugMe...done.
GDB displays its prompt and now we are ready to enter GDB commands..
Lets first list(print the source) the program :-
3 int main()
5 // some variable declarations just to demonstrate
6 int i = 1337;
7 char string = "Hello Wassup!!\n";
Now I would like your will and explain a little about breakpoints..
What are Breakpoints
There are 2 types of Breakpoints :-
- Hardware Breakpoints
- Software Breakpoints
For the scope of this tutorial I am only talking about 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
(gdb) break main
Breakpoint 1 at 0x804844d: file debugMe.c, line 4.
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 :-
run (arguments to the program)
So lets use this to run our program :-
Starting program: /home/aneesh/articles/debugMe
Breakpoint 1, main () at debugMe.c:4
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'
(gdb) info locals
i = 134513856
string = "X\364\377\277\245\324\025\000\060\340\021\000˄\004\b"
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
Note : Step's syntax is exactly similar to next's syntax
Here n is the no of times to step..
6 int i = 1337;
Note : The step command displays the instruction to be stepped upon...Its not already been stepped upon
Lets step 2 instructions down
(gdb) step 2
Now that we have declared all our variables lets check our local variables again..
(gdb) info locals
i = 1337
string = "Hello Wassup!!\n"
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
print I // prints the contents on variable I
print *I // prints the data in the memory address pointed to it
Lets use it now
(gdb) print string
$4 = "Hello Wassup!!\n"
As expected we get the contents of the string..
(gdb) print *string
$5 = 72 'H'
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 :-
The memory map would look like
…..... and so on
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
Program exited with code 020.
After this you should get your shell prompt back!!!
I hope this tutorial is enough to get you started working in GDB!!