Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   How to get started with GNU Project Debugger [GDB]? (http://www.go4expert.com/articles/started-gnu-project-debugger-gdb-t24718/)

lionaneesh 24Jan2011 21:27

How to get started with GNU Project Debugger [GDB]?
 
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

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);
}

Compile the file with -ggdb flag with gcc
Code:

aneesh@aneesh-laptop:~/articles$ gcc debugMe.c -o debugMe -ggdb
[/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 :-

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)

GDB displays its prompt and now we are ready to enter GDB commands..

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)

Now I would like your will and explain a little about breakpoints..

What are Breakpoints

There are 2 types of Breakpoints :-
  1. Hardware Breakpoints
  2. 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

Code:

(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 :-

Code:

run (arguments to the program)
So lets use this to run our program :-
Code:

((gdb) run
Starting program: /home/aneesh/articles/debugMe

Breakpoint 1, main () at debugMe.c:4
4        {
(gdb)

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'

Code:

(gdb) info locals
i = 134513856
string = "X\364\377\277\245\324\025\000\060\340\021\000˄\004\b"
(gdb)

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 :-
Code:

step [n]
Note : Step's syntax is exactly similar to next's syntax

Here n is the no of times to step..
Code:

(gdb) step
6                int i = 1337;
(gdb)

Note : The step command displays the instruction to be stepped upon...Its not already been stepped upon

Lets step 2 instructions down

Code:

(gdb) step 2
9                printf("%s\n",string);

Now that we have declared all our variables lets check our local variables again..
Code:

(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

Syntax :-
Code:

print I // prints the contents on variable I
print *I // prints the data in the memory address pointed to it

Lets use it now
Code:

(gdb) print string
$4 = "Hello Wassup!!\n"

As expected we get the contents of the string..
Code:

(gdb) print *string
$5 = 72 'H'
(gdb)

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 :-

Code:

0x12345678
So :-

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'

…..... and so on

So ,

Code:

print *string
the statement is similar to :-

Code:

print 0x12345678
Which prints 'l'

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)

Quiting :-
Code:

(gdb) quit

aneesh@aneesh-laptop:~/articles$

After this you should get your shell prompt back!!!

I hope this tutorial is enough to get you started working in GDB!!

lionaneesh 25Jan2011 15:27

Re: How to get started with GNU Project Debugger [GDB]?
 
Thanks Shabbir.. For accepting my article...
I hope its better...
I gave a example in this...

shabbir 25Jan2011 15:41

Re: How to get started with GNU Project Debugger [GDB]?
 
Yes a lot more.

lionaneesh 25Jan2011 19:22

Re: How to get started with GNU Project Debugger [GDB]?
 
Quote:

Originally Posted by shabbir (Post 78237)
Yes a lot more.

Thanks.....


All times are GMT +5.5. The time now is 18:22.