Skip to content

gdb command in Linux

The gdb command in Linux is a powerful tool for debugging programs. It allows developers to inspect and manipulate the execution of their code, set breakpoints, and analyze memory usage. With gdb, users can trace the flow of their program, step through code line by line, and identify errors in their logic. This command also provides advanced features for analyzing and troubleshooting code effectively, making it an essential tool for software developers working on Linux systems.

Terminal window
gdb [options] [executable_file [core_file or process_id]]
OptionDescription
-c <file>Use a core dump file for debugging
-e <file>Use an executable file for debugging
-qDon’t print version message on startup
-x <file>Read and execute commands from a file
-pid <number>Attach to a running process by process ID
-s <file>Use a command script provided in a file
-batchRun in batch mode
-cd <directory>Run in the specified directory
-nInitiate without reading init file
ParameterDescription
executable_fileThe executable file to be debugged
core_fileThe core dump file for debugging (if applicable)
process_idThe Process ID of a running process to attach to
Terminal window
gdb ./my_program

Run a program called “my_program” within gdb to start the debugging session.

Terminal window
break main

Set a breakpoint at the beginning of the main function in the program being debugged.

Terminal window
list

Display the source code around the current point of execution within gdb.

Terminal window
p my_variable

Print the current value of a variable named “my_variable” during debugging.

Terminal window
next

Execute the next line of code within gdb, stepping over function calls.

Terminal window
step

Execute the next line of code within gdb, stepping into functions if applicable.

Terminal window
continue

Continue the execution of the program until the next breakpoint is encountered or the program completes.

Terminal window
x/10x 0x7fffffffeff8

Display the hexadecimal values of 10 memory locations starting from the specified address within gdb.

To run the gdb command in Linux, use the following syntax:

Terminal window
gdb <program_name>

To set breakpoints in gdb, use the following command:

Terminal window
break <line_number>

How do I start gdb with a specific program?

Section titled “How do I start gdb with a specific program?”

To start gdb with a specific program, use the following command:

Terminal window
gdb -exec <program_name>

To display the source code in gdb, use the following command:

Terminal window
list

To continue execution in gdb, use the following command:

Terminal window
continue

To examine variables in gdb, use the following command:

Terminal window
print <variable_name>

To step through code in gdb, use the following command:

Terminal window
step

To quit gdb, use the following command:

Terminal window
quit

To enable verbose mode in gdb, use the following command:

Terminal window
set verbose on
  • Debugging programs
  • Analyzing memory usage
  • Inspection and modification of program variables
  • Backtracing function calls
  • Catching and handling signals
  • Examining registers and memory contents
  • Setting breakpoints
  • Stepping through code
  • Viewing disassembled code