← 返回命令列表

Linux command

gdb 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Debug program

gdb [program]

Debug with core

gdb [program] [core]

Attach to process

gdb -p [pid]

Run with arguments

gdb --args [program] [arg1] [arg2]

Execute commands

gdb -x [commands.gdb] [program]

说明

gdb (GNU Debugger) is the standard debugger for C, C++, and other compiled languages on Unix-like systems. It provides comprehensive facilities for inspecting program state, setting breakpoints, stepping through code, examining variables, and analyzing memory contents during execution. The debugger operates by controlling program execution, allowing developers to pause at specific points, examine the call stack, modify variable values, and trace execution flow. It can debug running processes by attaching to them, analyze core dumps from crashed programs, and even perform remote debugging across network connections via gdbserver. gdb supports scripting through its command language and Python integration, enabling automated debugging sessions and custom commands. The Text User Interface (TUI) mode provides a split-screen view of source code, assembly, registers, and command prompt. Debugging symbols (compiled with -g flag) are essential for meaningful source-level debugging, though gdb can still work with stripped binaries at the assembly level. Common workflows include setting breakpoints at functions or line numbers, examining variables with print and display commands, stepping through code with next/step/continue, and analyzing crashes through backtrace. The tool is indispensable for diagnosing segmentation faults, memory corruption, race conditions, and complex logic errors in native applications.

参数

-p _PID_
Attach to running process.
--args
Pass arguments to program.
-x _FILE_
Execute GDB commands from file.
-q, --quiet
Suppress startup messages.
--tui
Enable text user interface.
--help
Display help information.

FAQ

What is the gdb command used for?

gdb (GNU Debugger) is the standard debugger for C, C++, and other compiled languages on Unix-like systems. It provides comprehensive facilities for inspecting program state, setting breakpoints, stepping through code, examining variables, and analyzing memory contents during execution. The debugger operates by controlling program execution, allowing developers to pause at specific points, examine the call stack, modify variable values, and trace execution flow. It can debug running processes by attaching to them, analyze core dumps from crashed programs, and even perform remote debugging across network connections via gdbserver. gdb supports scripting through its command language and Python integration, enabling automated debugging sessions and custom commands. The Text User Interface (TUI) mode provides a split-screen view of source code, assembly, registers, and command prompt. Debugging symbols (compiled with -g flag) are essential for meaningful source-level debugging, though gdb can still work with stripped binaries at the assembly level. Common workflows include setting breakpoints at functions or line numbers, examining variables with print and display commands, stepping through code with next/step/continue, and analyzing crashes through backtrace. The tool is indispensable for diagnosing segmentation faults, memory corruption, race conditions, and complex logic errors in native applications.

How do I run a basic gdb example?

Run `gdb [program]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -p _PID_ do in gdb?

Attach to running process.