gdb command in Linux with examples
GDB, the acronym for GNU Debugger, is a powerful debugging tool used to analyze and debug programs written in languages like C, C++, Ada, and Fortran. It allows developers to inspect the behavior of their programs, step through code, set breakpoints, and examine variable values in real-time. GDB is an essential tool for debugging complex code, providing insights into runtime behavior that are invaluable for troubleshooting and optimization.
Basic Syntax of GDB
gdb [-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core] [-x cmds] [-d dir] [prog[core|procID]]
Example:

The program to be debugged should be compiled with ‘-g’ option. The below given C++ file that is saved as ‘gfg.cpp’. We are going to use this file in this article.
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int findSquare(int a)
{
return a * a;
}
int main(int n, char** args)
{
for (int i = 1; i < n; i++)
{
int a = atoi(args[i]);
cout << findSquare(a) << endl;
}
return 0;
}
Preparing a Program for Debugging with GDB
Compile the above C++ program using the command:
g++ -g -o gfg gfg.cpp
To start the debugger of the above ‘gfg’ executable file, enter the command ‘gdb gfg’. It opens the gdb console of the current program, after printing the version information.
Key Commands in GDB
1. run [args]
This command runs the current executable file. In the below image, the program was executed twice, one with the command line argument 10 and another with the command line argument 1, and their corresponding outputs were printed.
2. Quit GDB (quit or q):
To quit the gdb console, either quit or q can be used.
3. Get Help (‘help’)
It launches the manual of gdb along with all list of classes of individual commands.
4. Setting Breakpoints (break)
The command break [function name] helps to pause the program during execution when it starts to execute the function. It helps to debug the program at that point. Multiple breakpoints can be inserted by executing the command wherever necessary. ‘b findSquare’ command makes the gfg executable pause when the debugger starts to execute the findSquare function.
b
break [function name]
break [file name]:[line number]
break [line number]
break *[address]
break ***any of the above arguments*** if [condition]
b ***any of the above arguments***

5. Continue Execution (‘continue’ or ‘c’)
This command helps to resume the current executable after it is paused by the breakpoint. It executes the program until it encounters any breakpoint or runs time error or the end of the program. If there is an integer in the argument(repeat count), it will consider it as the continue repeat count and will execute continue command “repeat count” number of times.
continue [repeat count]
c [repeat count]

6. Step Over (‘next’ or ‘n’)
This command helps to execute the next instruction after it encounters the breakpoint. Whenever it encounters the above command, it executes the next instruction of the executable by printing the line in execution.
7. Delete Breakpoints (‘delete’)
This command helps to deletes the breakpoints and checkpoints. If the delete command is executed without any arguments, it deletes all the breakpoints without modifying any of the checkpoints. Similarly, if the checkpoint of the parent process is deleted, all the child checkpoints are automatically deleted.
d
delete
delete [breakpoint number 1] [breakpoint number 2] ...
delete checkpoint [checkpoint number 1] [checkpoint number 2] ...

8. Clear Breakpoints (‘clear’)
This command deletes the breakpoint which is at a particular function with the name ‘FUNCTION_NAME’. If the argument is a number, then it deletes the breakpoint that lies in that particular line.
clear [line number]
clear [FUNCTION_NAME]

9. disable [breakpoint number 1] [breakpoint number 2] …. (disable and enable)
Instead of deleting or clearing the breakpoints, they can be disabled and can be enabled whenever they are necessary.
10. enable [breakpoint number 1] [breakpoint number 2] ….
To enable the disabled breakpoints, this command is used.
11. Inspect Breakpoints (‘info breakpoints’)
When the info breakpoints in invoked, the breakpoint number, type, display, status, address, the location will be displayed. If the breakpoint number is specified, only the information about that particular breakpoint will be displayed. Similarly, when the info checkpoints are invoked, the checkpoint number, the process id, program counter, file name, and line number are displayed.
info breakpoints [breakpoint number 1] [breakpoint number 2] ...
info checkpoints [checkpoint number 1] [checkpoint number 2] ...

12. Checkpoints and Process Management (checkpoint and restart)
These command creates a new process and keep that process in the suspended mode and prints the created process’s process id. For example, in the above execution, the breakpoint is kept at function findSquare and the program was executed with the arguments “1 10 100”. When the function is called initially with a = 1, the breakpoint happens. Now we create a checkpoint and hence gdb returns a process id(4272), keeps it in the suspended mode and resumes the original thread once the continue command is invoked. Now the breakpoint happens with a = 10 and another checkpoint(pid = 4278) is created. From the info checkpoint information, the asterisk mentions the process that will run if the gdb encounters a continue. To resume a specific process, restart command is used with the argument that specifies the serial number of the process. If all the process are finished executing, the info checkpoint command returns nothing.
13. Set and Show Arguments (set args [arg1] [arg2] …)
This command creates the argument list and it passes the specified arguments as the command line arguments whenever the run command without any argument is invoked. If the run command is executed with arguments after set args, the arguments are updated. Whenever the run command is ran without the arguments, the arguments are set by default.
14. show args
The show args prints the default arguments that will passed if the run command is executed. If either set args or run command is executed with the arguments, the default arguments will get updated, and can be viewed using the above show args command.
15. display [/format specifier] [expression] and undisplay [display id1] [display id2] …
These command enables automatic displaying of expressions each time whenever the execution encounters a breakpoint or the n command. The undisplay command is used to remove display expressions. Valid format specifiers are as follows:
o - octal
x - hexadecimal
d - decimal
u - unsigned decimal
t - binary
f - floating point
a - address
c - char
s - string
i - instruction

16. Print Expressions (‘print’)
This command prints the value of a given expression. The display command prints all the previously displayed values whenever it encounters a breakpoint or the next command, whereas the print command saves all the previously displayed values and prints whenever it is called.
print [Expression]
print $[Previous value number]
print {[Type]}[Address]
print [First element]@[Element count]
print /[Format] [Expression]

17. Load Executables (‘file’)
gdb console can be opened using the command gdb command. To debug the executables from the console, file [executable filename] command is used.
Conclusion
GDB is an indispensable tool for developers working with compiled languages like C and C++. Its robust set of debugging features makes it possible to pinpoint and resolve bugs, analyze program behavior, and optimize performance. By understanding and mastering GDB commands and options, you can greatly enhance your debugging skills, leading to more efficient and effective development.