What is GDB?
GDB stands for GNU Debugger. It is a powerful tool for debugging the programs of various programming languages like C, Fortran, Go, C++, etc. It runs on the Unix-like operating systems. This concept was written in 1986 by Richard Stallman. It is actually a free software, which is released under the GNU GPL. This debugger is written in a C language.
Following are the various languages supported by the GDB:
GDB uses the simple CLI (Command Line Interface), so that users or programmers can understand it easily. It allows users to inspect what the program or code is doing at a specific point during its execution.
It also enables the users/programmers to stop a program at a certain point and print the value of a specific variable at that point. It also operates on the binary files produced by the process of compilation.
Installation of GDB
Firstly, we have to check that the GNU debugger is already installed on a Unix or Linux operating system or not. For this, we have to run the following command in the terminal:
If GDB is already installed, then the above command will show the options which are available with GDB. If this tool is not installed, then we have to install the GDB on our system.
We can easily install the GDB on our Linux machine using the following two different ways or process:
1. Using the pre-built gdb binaries:
In this process, we have to run the following two commands on our ubuntu terminal. The following first command is used to update the dependencies:
After the successful execution of above command, run the following command which installs the GDB:
2. Using the compilation process of the downloaded source code
In this process, we have to follow the given steps for downloading the GDB:
1. In this step, we have to download the source code by using the /gnu/gdb/ site in the command.
2. Now, we have to extract the downloaded source code:
3. After extracting, we have to configure and compile the source code by running the following three commands one by one:
4. After the completion of the above commands. Now, we have to install the GDB using the command which is given below:
The above command installs the binaries of a GDB /usr/local/bin and libraries of GDB in /usr/local/lib by default.
After the successful execution of the above command, we have successfully compiled and installed the GNU Debugger in our Linux system.
After this, we can easily see the version of GDB by typing the following command and verify that it is successfully installed or not:
GDB is a command-line tool. So, we have to learn about the commands of GDB before learn about how to use the GDB.
GDB provides the various commands, which helps users for debugging the code or programs. The following table shows the list of command with their description:
How to Use GNU Debugger
Till now, we have studied what is GDB and what are its commands, now we have to learn about the example and how to use it.
In this section, we learn how to debug a program using the GNU debugger.
Now, we have to follow the steps on how to run a program with GDB:
Step 1: Firstly, we have to create a program of C or C++ in the text editor. So, we create the following code for finding a factorial of a number. And, we save this code with the .cextension.
Step 2: Now, we have to compile and build the above program.c in terminal with the help of debugging symbols.
In the above command, we use the -g option, which is used for compiling the program in debugging mode.
Step 3: Now, we have to run the program with the GDB, so type the following command in the terminal after the above command:
The above command gives the following console of GDB of the current program.
In this console, we have to use the GDB commands for analyzing, and debugging the program.
Step 4: And, at last after debugging, exit the GDB by typing the quitorq command on the prompt of GDB.
In this section, we will learn about how to use the commands of GDB with the example of the C programming language:
This example finds the factorial of a given number. In this program, there is an error for the debugging purpose.
Now, check the output of the program by typing the following two commands in the terminal. The first command is for compiling the program:
And, the second command is to run the program:
After typing the above command, we will see the following output:
Enter the number: 6The factorial of 6 is 23592240
In this output, the factorial of 6 is wrong, so we have to again compile the above program for debugging by using the -g option in the command:
Now, launch the GDB debugger for debugging the problem by typing the following command. In this command, a.out is a file which is produced after the compilation.
The above command gives the following console, which allows us to run the commands of GDB for debugging:
Now, we use the following steps to debug the above program:
Step 1: Firstly, we have to set a breakpoint in this factorial program by using the following syntax:
After placing the breakpoint in c program, the debugger will stop at it, and gives us a prompt for debugging. So, before executing the program from starting, we have to place the given breakpoint in the program:
This command gives the following statement:
Step 2: Now, we have to execute the c program from starting in gdb debugger by using the following syntax:
we can easily run the program from starting using the above command. We can also provide the command line arguments to the code by passing the arguments in the above command. But, our program does not need the command line arguments, so we use only run:
After the typing of the above command, it gives the following prompt for debugging and execute the program until the first breakpoint:
After typing 6, it will give the following prompt:
Step 3: Now, we have to print the values of a variable which are used in this program by using the following syntax:
Type the following commands for displaying the value of variables of the above program on the console:
In the above values, the value of variable j is the garbage, so due to this the output of the factorial program is wrong.
Therefore, firstly we have to initialize the value of variable jas1 and execute the above program again.