
gdb Command in Linux
gdb, short for GNU Debugger, is a command used in Linux to debug programs written in languages like C, C++ and Fortran. With gdb command, you can run your programs step-by-step, set breakpoints to pause execution and inspect variables to understand the program behavior at different stages. Doing this, you can identify and fix bugs effectively.
Additionally, gdb also supports remote debugging that allows you to debug programs running on different machines.
Table of Contents
Here is a comprehensive guide to the options available with the gdb command in linux −
Syntax of gdb Command
The basic syntax for using gdb in Linux is as follows −
gdb [options] [program] [core|processID]
Here,
- gdb is the command to start the GNU Debugger.
- [options] are various options to customize the debugging session.
- [program] is the name of the executable program you want to debug.
- [core|processID] is optional used to specify a core dump file or a process ID to attach gdb to a running process.
gdb Command Options
There are tons of options you can use with the gdb command, you can find them with the description given below −
Option | Description |
---|---|
--args | Passes arguments after the executable file to the interior process. |
--core=COREFILE | Analyzes the coredump in COREFILE. |
--exec=EXECFILE | Uses EXECFILE as executable. |
--pid=PID | Attaches to a running process with process ID PID. |
--directory=DIR | Searches for the source file within the specified directory DIR. |
--se=FILE | Utilizes the specified file as both a symbol and an executable file |
--symbols=SYMFILE | Loads symbols from the specified SYMFILE |
--readnow | Completely loads the symbol files upon initial access. |
--readnever | Avoids reading symbol files. |
--write | Enables writing to core and executable files. |
--command=FILE, -x | Runs GDB commands from the specified FILE. |
--init-command=FILE | Similar to -x, but executes commands prior to loading the inferior process. |
--eval-command=COMMAND | Executes a single GDB command, with the option to use it multiple times. |
init-eval-command | Like --eval-command, but before loading the inferior process. |
--nh | Dont read ~/.gbinit. |
--nx | Dont read any .gbinit files in any directory. |
--full-name | Outputs information used by the emacs-GDB interface. |
--interpreter= INTERP | Chooses a particular interpreter or user interface. |
--tty=TTY | Uses the specified TTY for the programs input and output during debugging. |
-w | Uses the GUI interface. |
--nw | Avoids using the GUI interface. |
--tui | Use a terminal interface. |
-q, --quiet, --silent | Dont print the version number on startup. |
--batch | Exits after processing options. |
--batch-silent | Similar to --batch, but it suppresses all standard output from GDB. |
--return-child-result | The GDB exit code will match the exit code of the child process. |
--configuration | Displays GDB configuration details and then exits. |
--help | Prints the help information and then exits. |
--version | Prints version information. |
Examples of gdb Command in Linux
Here are some examples of gdb command in Linux systems −
- Starting a Debugging Session
- Setting a Breakpoint
- Running the Program
- Inspect Variables
- Stepping Through Code
- Continue Execution
- Backtrace
- Quitting gdb
Starting a Debugging Session
One of the basic uses of gdb command is to start debugging a program on the system. You can make it possible by using the gdb command along with the program name you want to debug. Heres an example −
gdb program
The above command will start gdb with the executable program. It may ask from you whether to enable the debuginfod for this session, do it by entering y −

Once inside gdb, you can run your program using the run command.

Setting a Breakpoint
You can also set a breakpoint at a specific function or line number to pause the execution of your program. For example −
(gdb) break main
The above command will set a breakpoint at the beginning of the main function. When you run the program, it will stop at this point.

Running the Program
Once you set the breakpoints, you can run your program within gdb using −
(gdb) run
As soon as you hit the run command, it will start the execution of your program and will stop at any breakpoints you have set.

Inspecting Variables
You can also inspect the value of a variable at a certain point in your program by using the print command followed by the variable_name. For example −
(gdb) print variable_name
The above command will display the current value of variable_name.
Stepping Through Code
In case, you want to execute your program line-by-line to closely observe its behavior, you can run the step command −
(gdb) step

When you run the command, it will execute the next line of code and then pause. This will allow you to inspect the state of your program.
Continuing Execution
You can continue running your program until the next breakpoint or the end of the program by using the continue command −
(gdb) continue
This will resume the execution of your program.

Backtrace
If you want to see the call stack and understand the sequence of function calls leading to the current point, simply run the backtrace command −
(gdb) backtrace
Running the above command will show the call stack, which is useful for debugging.

Quitting gdb
Once you are done with your debugging session, you can exit gdb using the quit command −
(gdb) quit
This will exit the gdb session.

Thats how you can use the gdb command on your Linux system.
Conclusion
The gdb command is a powerful command used in Linux for debugging programs written in different programming languages (C, C++ and Fortran). It provides numerous options that help you customize the debugging process based on your needs.
In this tutorial, we have explored the syntax of the gdb command and various options you can use with it. In addition, a few examples are also provided to help you in mastering the command and enhance your debugging efficiency on various Linux systems.