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

lionaneesh's Avatar author of How to get started with GNU Project Debugger [GDB]?
This is an article on How to get started with GNU Project Debugger [GDB]? in Unix.
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

int main()
        // 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 <> 
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 :-
(gdb) list 
1	#include<stdio.h> 
3	int main() 
4	{ 
5		// some variable declarations just to demonstrate 
6		int i = 1337; 
7		char string[] = "Hello Wassup!!\n"; 
9		printf("%s\n",string); 
10	} 
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

(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 :-
((gdb) run 
Starting program: /home/aneesh/articles/debugMe 

Breakpoint 1, main () at debugMe.c:4 
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

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

Here n is the no of times to step..
(gdb) 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 
9		printf("%s\n",string);
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

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

So :-

The memory map would look like

0x12345678 'l'
0x12345679 'i'
0x12345680 'o'
0x12345681 'n'
0x12345682 'a'
0x12345683 'n'
0x12345684 'e'
0x12345685 'e'
0x12345686 's'
…..... and so on

So ,

print *string
the statement is similar to :-

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
(gdb) continue


Hello Wassup!!

Program exited with code 020.

Quiting :-
(gdb) quit 

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

I hope this tutorial is enough to get you started working in GDB!!
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Thanks Shabbir.. For accepting my article...
I hope its better...
I gave a example in this...
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Yes a lot more.
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Originally Posted by shabbir View Post
Yes a lot more.