What is GNU Debugger?

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:

How GDB Debugs?

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.

Points to Note

GDB - Installation

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.

GDB - Debugging Symbols

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

GDB offers a big list of commands, however the following commands are the ones used most frequently:

GDB - Debugging Programs

Getting Started: Starting and Stopping