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.

gdb Syntax:

Terminal window
gdb [options] [executable_file [core_file or process_id]]

Linux gdb Options:

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

gdb Parameters:

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

How to use gdb command:

Run a Program in gdb

Terminal window
gdb ./my_program

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

Set a Breakpoint

Terminal window
break main

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

List Source Code

Terminal window
list

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

Display Variable Value

Terminal window
p my_variable

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

Step Through Code

Terminal window
next

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

Step Into Function

Terminal window
step

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

Continue Execution

Terminal window
continue

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

Examine Memory

Terminal window
x/10x 0x7fffffffeff8

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

How do I run gdb in Linux?

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

Terminal window
gdb <program_name>

How can I set breakpoints in gdb?

To set breakpoints in gdb, use the following command:

Terminal window
break <line_number>

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>

How can I display the source code in gdb?

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

Terminal window
list

How do I continue execution in gdb?

To continue execution in gdb, use the following command:

Terminal window
continue

How can I examine variables in gdb?

To examine variables in gdb, use the following command:

Terminal window
print <variable_name>

How do I step through code in gdb?

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

Terminal window
step

How can I quit gdb?

To quit gdb, use the following command:

Terminal window
quit

How do I enable verbose mode in gdb?

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

Terminal window
set verbose on

Applications of the gdb command

  • 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