GDB - Java Debugger

GDB Tutorial

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:

  • Ada
  • Assembly
  • C
  • Fortran
  • Go
  • OpenCL
  • Modula-2
  • C++
  • D
  • Pascal
  • Rust

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 http://ftp.gnu.org/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 Commands

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:

S. No.Name of a CommandDescription
1.AttachThis command attaches the GNU Debugger with the process which is running.
2.backtraceThis command displays the call stack for the thread which is selected currently.
3.backtrace fullThis command displays the value of local variables.
4.b funThis command sets a breakpoint at the starting of function.
5.b NThis command sets a breakpoint at Nth line number of a file which is executing currently.
6.b mainThis command sets a breakpoint at the starting of a program.
7.bThis command sets a breakpoint at the current line of a file which is executing.
8.b +NThis command sets a breakpoint after the N lines from the current line of a file.
9.BtThis command displays the stack trace.
10.CThis 'command' allows the GDB to run the program till the next breakpoint or error.
11.ClearThis command deletes the breakpoint at a particular location.
12.checkpointThis command helps in creating a new checkpoint.
13.dThis command deletes or removes all the breakpoint from the program. This command is different from clear command.
14.d NThis command deletes/removes the breakpoint at Nth line number.
15.d 1 2This command deletes or removes the breakpoint 1, and 2 from the program.
16.delete checkpointThis command deletes or removes the checkpoint which is created previously.
17.detachThis command detaches the GNU Debugger from the process.
18.disableThis command disables all the breakpoints or particular breakpoints of a program.
19.downThis command only selects the frame which is one level down.
20.down NThis command selects those frame or frames which are N level down.
21.enableThis command enables all the breakpoints or particular breakpoints of a program.
22.enable onceThis command enables the breakpoint and after the first hit, it is disabled automatically.
23.enable deleteThis command enables the breakpoint and after the first hit, it is deleted automatically.
24.finishThis command continues the execution to the end of the function.
25.frameThis command selects the stack frame. It also displays the stack frame which is selected currently.
26.gdbThis command starts the GNU debugger. It starts with no files of debugging.
27.gdb --helpThis command describes the various options of a command line.
28.gdb --versionThis command prints the version of GDB and some information about GDB.
29.helpThis command displays the list of classes of commands.
30.help <cmd>This command describes the command about how to use it.
31.iThis command provides the list of all the info command.
32.info addressThis command displays the address of a specified symbol.
33.info bThis command displays the list of all the breakpoints.
34.info breakpointsThis command displays the information about breakpoints.
35.info checkpointsThis command provides the list of checkpoints which are created.
36.info functionsThis command shows the list of function of the program.
37.info registersThis command displays the content/list of registers which are in use.
38.info watchThis command displays the defined watchpoints.
39.info sourceThis command displays the information of a file which is used currently.
40.Info sourcesThis command provides the list of all source files which are in use.
41.info symbolThis command prints the symbol name which resides at the particular address.
42.Info linesThis command shows the starting and ending addresses of the specified source line in a code.
43.info argsThis command shows the argument values of the function of the frame which is used currently.
44.info localsThis command shows the information about the local variables.
45.info frameThis command shows the advanced information about the stack frame.
46.info break <breakpoint-number>This command prints the information about the particular breakpoint.
47.info vectorThis command shows the information of the vector registers.
48.info variablesThis command shows the list of static or global variables which are present in the debugged program or code.
49.killThis command stops the execution of the program.
50.printThis command displays the value of a specified expression.
51.p varThis command prints or displays the value of variable 'var' which is used currently.
52.p/x variableThis command prints the value of an integer variable in the hexadecimal format.
53.p/d variableThis command prints the value of the variable as a signed integer.
54.p/o variableThis command prints the value of an integer variable in the octal format.
55.p/a variableThis command prints the value of a variable as a hex address.
56.q or quitThis command exits the GNU debugger.
57.run or rThis command runs the program from the beginning to the end or to the next breakpoint.
58.sThis command executes the next line or statement of a program.
59.s NThis command executes the next Nth line or statement of a program.
60.select-frameThis command is used to select a stack frame.
61.show argsThis command displays the list of arguments.
62.set argsThis command specifies the list of argument list which is empty.
63.set var = vThis command assigns the value 'v' to the variable 'var'.
64.set args arglistThis command specifies the default arguments for the debugged program.
65.show envThis command displays all the environment variables.
66.show env 'var'This command displays the value of 'var', which is an environment variable.
67.stepThis command executes the next line of the code.
68.step NThis command executes the next Nth line of a code.
69.tbreakThis command is similar to the break command, but it is a temporary breakpoint. The breakpoint will be automatically deleted after the one hit.
70.upThis command only selects the frame which is one level up.
71.up NThis command selects those frame or frames which are N level up.
72.undisplayThis command removes the automatically displayed expression, which is set by the display command.
73.unset env varThis command removes the variable 'var' from the environment.
74.whereThis command displays the number of a current line and the function in which you are.
75.x Address ExprThis 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.

GDB Example

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:

GDB Tutorial

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.