Linux gdb command

Linux 命令大全Linux Command Reference


GDB (GNU Debugger) is the most commonly used program debugging tool on Linux systems. It can help developers:

  • Trace program execution flow
  • Set breakpoints to pause program execution
  • View and modify variable values
  • Analyze the causes of program crashes
  • Inspect the function call stack

GDB supports multiple programming languages, including C, C++, Objective-C, Fortran, Ada, etc., and is an indispensable debugging tool for Linux developers.


Install gdb

On most Linux distributions, gdb can be easily installed through the package manager:

Examples

# Ubuntu/Debian
sudo apt-get install gdb

# CentOS/RHEL
sudo yum install gdb

# Fedora
sudo dnf install gdb

# Arch Linux
sudo pacman -S gdb

After installation, you can verify whether the installation was successful with the following command:

gdb --version

Compile a debuggable program

To debug a program with gdb, you need to add the-goption during compilation to generate debugging information:

gcc -g program.c -o program

-gThis option embeds source code information in the executable file, enabling gdb to map machine instructions to source code.


Basic gdb commands

Start and exit gdb

Examples

# Start gdb and load the program
gdb ./program

# Start gdb and attach to a running process
gdb -p PID

# Exit gdb
(gdb) quit
# Or shorthand
(gdb) q

Run the program

Examples

# Run the program
(gdb) run
# Or shorthand
(gdb) r

# Run with arguments
(gdb) run arg1 arg2

Breakpoint management

Examples

# Set a breakpoint at the specified line
(gdb) break 10
# Or shorthand
(gdb) b 10

# Set a breakpoint at the function entry
(gdb) break main
(gdb) break function_name

# View all breakpoints
(gdb) info breakpoints

# Delete breakpoints
(gdb) delete 1  # Delete breakpoint number 1
(gdb) delete    # Delete all breakpoints

Program execution control

Examples

# Continue execution until the next breakpoint
(gdb) continue
# Or shorthand
(gdb) c

# Single-step execute (step into functions)
(gdb) step
# Or shorthand
(gdb) s

# Single-step execute (step over functions)
(gdb) next
# Or shorthand
(gdb) n

# Finish executing the current function and return
(gdb) finish

View code

Examples

# View code near the current line
(gdb) list
# Or shorthand
(gdb) l

# View code near the specified line
(gdb) list 15

# View the code of the specified function
(gdb) list main

Inspect variables and memory

Examples

# Print variable value
(gdb) print variable_name
# Or shorthand
(gdb) p variable_name

# Modify variable value
(gdb) print variable_name = new_value

# View variable type
(gdb) ptype variable_name

# View memory contents
(gdb) x/10xw &variable  # View 10 words in hexadecimal

Call stack analysis

Examples

# View the call stack
(gdb) backtrace
# Or shorthand
(gdb) bt

# Switch to the specified stack frame
(gdb) frame 2
# Or shorthand
(gdb) f 2

Advanced debugging techniques

Conditional breakpoints

Examples

# Trigger breakpoint when i equals 5
(gdb) break 10 if i == 5

Watchpoints

Examples

# Pause when the variable is modified
(gdb) watch variable_name

# Pause when the variable is read
(gdb) rwatch variable_name

# Pause when the variable is read or modified
(gdb) awatch variable_name

Multi-threaded debugging

Examples

# View all threads
(gdb) info threads

# Switch to the specified thread
(gdb) thread 2

# Only allow the current thread to execute
(gdb) set scheduler-locking on

Debugging core dump files

Examples

# Load the core dump file
gdb ./program core

# View the call stack at the time of the crash
(gdb) bt

gdb graphical interface

gdb also supports a graphical interface mode, which can be started using the-tuioption:

gdb -tui ./program

Or switch to it during use:

Examples

(gdb) layout src  # Display the source code window
(gdb) layout asm  # Display the assembly window
(gdb) layout regs # Display the register window
(gdb) layout split # Display source code and assembly simultaneously

Practical gdb configuration

You can add common configurations to the~/.gdbinitfile:

Examples

# Enable pretty printing
set print pretty on

# Set history size
set history save on
set history size 1000

# Custom commands
define printarray
    set $i = 0
    while $i < $arg0
        printf "array[%d] = %dn", $i, $arg1[$i]
        set $i = $i + 1
    end
end

Troubleshooting common issues

Cannot see source code when debugging

  1. Make sure the-goption is used during compilation
  2. Check whether the source code is in the same location as at compile time
  3. Use thedirectorycommand to add the source code path:
(gdb) directory /path/to/source

Debugging optimized code

Optimization may change the execution order of the code, making debugging difficult. It is recommended to disable optimization when debugging:

gcc -O0 -g program.c -o program

Summary

gdb is a powerful debugging tool on Linux. Mastering it can significantly improve debugging efficiency. This article introduced the basic usage and common commands of gdb. Readers are advised to consolidate this knowledge through actual debugging practice. As you gain experience, you will find that gdb can help you solve various complex debugging problems.


Linux 命令大全Linux Command Reference

Other extensions