A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise.
GNU Debugger, which is also called gdb, is the most popular debugger for UNIX systems to debug C and C++ programs.
GNU Debugger helps you in getting information about the following:
GDB allows you to run the program up to a certain point, then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.
GDB uses a simple command line interface.
Before you go for installation, check if you already have gdb installed on your Unix system by issuing the following command:
$gdb -help
If GDB is installed, then it will display all the available options within your GDB. If GDB is not installed, then proceed for a fresh installation.
You can install GDB on your system by following the simple steps discussed below.
step 1: Make sure you have the prerequisites for installing gdb:
step 2: Download the gdb source distribution from ftp.gnu.org/gnu/gdb. (We used gdb-6.6.tar.gz for these instructions.) Place the distribution files in your build directory.
step 3: In your build directory, decompress gdb-6.6.tar.gz and extract the source files from the archive. Once the files have finished extracting, change your working directory to the gdb-6.6 directory that was automatically created in your build directory.
$ build> gzip -d gdb-6.6.tar.gz $ build> tar xfv gdb-6.6.tar $ build> cd gdb-6.6
step 4: Run the configure script to configure the source tree for your platform.
$ gdb-6.6> .⁄configure
step 5: Build gdb using the make utility.
$ gdb-6.6> make
step 6: Login as root and install gdb using the following command.
$ gdb-6.6> make install
step 7: If required, disk space can be reclaimed by deleting the gdb build directory and the archive file after the installation is complete.
$ gdb-6.6> cd .. $ build> rm -r gdb-6.6 $ build> rm gdb-6.6.tar
You now have gdb installed on your system and it is ready to use.
A Debugging Symbol Table maps instructions in the compiled binary program to their corresponding variable, function, or line in the source code. This mapping could be something like:
Symbol tables may be embedded into the program or stored as a separate file. So if you plan to debug your program, then it is required to create a symbol table which will have the required information to debug the program.
We can infer the following facts about symbol tables:
To let GDB be able to read all that information line by line from the symbol table, we need to compile it a bit differently. Normally we compile our programs as:
gcc hello.cc -o hello
Instead of doing this, we need to compile with the -g flag as shown below:
gcc -g hello.cc -o hello
GDB offers a big list of commands, however the following commands are the ones used most frequently:
Stepping lets you trace the path of your program, and zero in on the code that is crashing or returning invalid input.
Breakpoints play an important role in debugging. They pause (break) a program when it reaches a certain point. You can examine and change variables and resume execution. This is helpful when some input failure occurs, or inputs are to be tested.
Viewing and changing variables at runtime is a critical part of debugging. Try providing invalid inputs to functions or running other test cases to find the root cause of problems. Typically, you will view/set variables when the program is paused.
A stack is a list of the current function calls - it shows you where you are in the program. A frame stores the details of a single function call, such as the arguments.
Signals are messages thrown after certain events, such as a timer or error. GDB may pause when it encounters a signal; you may wish to ignore them instead.
Go through the following examples to understand the procedure of debugging a program and core dumped.
Both the programs are written in C++ and generate core dump due to different reasons. After going through these two examples, you should be in a position to debug your C or C++ programs generating core dumps.
After going through this tutorial, you must have gained a good understanding of debugging a C or C++ program using GNU Debugger. Now it should be very easy for you to learn the functionality of other debuggers because they are very similar to GDB. It is highly recommended that you go through other debuggers as well to become familiar with their features.
There are quite a few good debuggers available in the market:
You can get a comprehensive detail about GNU Debugger from the following link: Debugging with GDB