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:
||Name of a Command
||This command attaches the GNU Debugger with the process which is running.
||This command displays the call stack for the thread which is selected currently.
||This command displays the value of local variables.
||This command sets a breakpoint at the starting of function.
||This command sets a breakpoint at Nth line number of a file which is executing currently.
||This command sets a breakpoint at the starting of a program.
||This command sets a breakpoint at the current line of a file which is executing.
||This command sets a breakpoint after the N lines from the current line of a file.
||This command displays the stack trace.
||This 'command' allows the GDB to run the program till the next breakpoint or error.
||This command deletes the breakpoint at a particular location.
||This command helps in creating a new checkpoint.
||This command deletes or removes all the breakpoint from the program. This command is different from clear command.
||This command deletes/removes the breakpoint at Nth line number.
||d 1 2
||This command deletes or removes the breakpoint 1, and 2 from the program.
||This command deletes or removes the checkpoint which is created previously.
||This command detaches the GNU Debugger from the process.
||This command disables all the breakpoints or particular breakpoints of a program.
||This command only selects the frame which is one level down.
||This command selects those frame or frames which are N level down.
||This command enables all the breakpoints or particular breakpoints of a program.
||This command enables the breakpoint and after the first hit, it is disabled automatically.
||This command enables the breakpoint and after the first hit, it is deleted automatically.
||This command continues the execution to the end of the function.
||This command selects the stack frame. It also displays the stack frame which is selected currently.
||This command starts the GNU debugger. It starts with no files of debugging.
||This command describes the various options of a command line.
||This command prints the version of GDB and some information about GDB.
||This command displays the list of classes of commands.
||This command describes the command about how to use it.
||This command provides the list of all the info command.
||This command displays the address of a specified symbol.
||This command displays the list of all the breakpoints.
||This command displays the information about breakpoints.
||This command provides the list of checkpoints which are created.
||This command shows the list of function of the program.
||This command displays the content/list of registers which are in use.
||This command displays the defined watchpoints.
||This command displays the information of a file which is used currently.
||This command provides the list of all source files which are in use.
||This command prints the symbol name which resides at the particular address.
||This command shows the starting and ending addresses of the specified source line in a code.
||This command shows the argument values of the function of the frame which is used currently.
||This command shows the information about the local variables.
||This command shows the advanced information about the stack frame.
||info break <breakpoint-number>
||This command prints the information about the particular breakpoint.
||This command shows the information of the vector registers.
||This command shows the list of static or global variables which are present in the debugged program or code.
||This command stops the execution of the program.
||This command displays the value of a specified expression.
||This command prints or displays the value of variable 'var' which is used currently.
||This command prints the value of an integer variable in the hexadecimal format.
||This command prints the value of the variable as a signed integer.
||This command prints the value of an integer variable in the octal format.
||This command prints the value of a variable as a hex address.
||q or quit
||This command exits the GNU debugger.
||run or r
||This command runs the program from the beginning to the end or to the next breakpoint.
||This command executes the next line or statement of a program.
||This command executes the next Nth line or statement of a program.
||This command is used to select a stack frame.
||This command displays the list of arguments.
||This command specifies the list of argument list which is empty.
||set var = v
||This command assigns the value 'v' to the variable 'var'.
||set args arglist
||This command specifies the default arguments for the debugged program.
||This command displays all the environment variables.
||show env 'var'
||This command displays the value of 'var', which is an environment variable.
||This command executes the next line of the code.
||This command executes the next Nth line of a code.
||This command is similar to the break command, but it is a temporary breakpoint. The breakpoint will be automatically deleted after the one hit.
||This command only selects the frame which is one level up.
||This command selects those frame or frames which are N level up.
||This command removes the automatically displayed expression, which is set by the display command.
||unset env var
||This command removes the variable 'var' from the environment.
||This command displays the number of a current line and the function in which you are.
||x Address Expr
||This command displays the information of memory at an address which is given in command.
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 .c extension.
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 quit or q 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: 6
The 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 j as 1 and execute the above program again.