What is GDB and how it works?

What is GDB?

GDB, the GNU Debugger, allows you to see what’s going on ‘inside’ another program while it executes. Or you can say that, it back traces what program was doing at the moment it crashed. GDB is helpful in many ways. For example, you may have seen many open source softwares and it is difficult for another programmer to understand the code. GDB is the best option to understand the flow or code of a program. It can also be helpful in many other cases. For example you have developed a software, it is not showing any error but the output is not as expected. So, GDB can help you trace the values of all the variables to find the bug in your software.

GDB can do four main things to help you catch bugs in the act:

  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

GDB works as a debugger for many languages. Some of them are: Ada, C, C++, Objective C, Pascal. Those programs might be executing on the same machine as GDB(native) or on another machine (remote). GDB can run on most popular Microsoft Windows and UNIX variants.

How GDB works?

Follow the steps described below:

Installation:

As GDB works on Command Line Interface, run th following command in Linux Ubuntu Terminal to install GBD:

“sudo apt-get install gdb”

‘gdb’ is the package and it will be directly installed in your machine. Most UNIX variants already have GDB installed. If not installed, then you can install it by this package.

1- Adding Exception:

As described above, you have to add an exception to stop your program. There is no limit or any specification of adding an exception. You can add any exception which will give run time error. For example, dividing by zero or any pointer definition.

Exception should be added in source code at point where you want to break the program. Or from where you want to check the back trace of the program.

Lets suppose, I have a source code, and there is a line “count = c+b;” and I want to check the value of count. So, I will insert exception right after this line. For example,

int check(c, b){
int count;
count = c + b;
  int *a;                    //exception added
  a = 0;                      //exception added
  *a = 1;                    //exception added
return count;
}

2- Compiling:

After adding exception, compile the program and restart all the services associated with the file you make changes in. After compiling, you are required to run the program in debug mode, So that, GDB can run properly. The program will be compiled successfully, because the exception you added will give run time error not compile time error.

3- Running GDB:

After successful compilation, find the output file of your program and now you are required to run that output in GDB Debugging mode. Run the following command to run the program in GDB debugging mode:

“sudo gdb “

For example, the name of output file is ‘a.out’ run the command as follows:

“sudo gdb a.out”

capture

The screen shot is shown above for clearing concepts. after running this command you are entered int the GDB mode Command Line Interface. “done” in the end shows that shifting to GDB mode is successful.

Now run the command “run” in GDB mode. It will start running your program and when it hits the exception point, it will be crashed showing the exception.

1

Until now, I have described what is GDB and how to run GDB. Now, we have hit the exception point, so its the time to find the back trace and values of all variables in order to check the flow of program and find the bug.

Commands of GDB:

  1. backtrace/bt (displays full stack of all the frames and shows flow of program)
  2. frame/f ( displays the details or specific Frame Number)
  3. list (displays the portion of code where the code crashed. It shows 5 lines above the exception point and 5 lines below the exception point, depending on frame Number.)
  4. print/p (displays the value of variable. Sometimes, this value could be a pointer value. For displaying the value of pointer, use ‘print * ‘)
  5. quit (quiting GDB)

I hope this will be helpful for a lot of people who still have confusion! Please comment, if you know something else which I missed or stated incorrectly.

Thanks!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s