3.2.2. Attaching GDB to a process


To examine a system process, attach the GNU Debugger (GDB) to the process.

Procedure

  • Start a program with GDB.

    1. Launch a program using GDB:

      $ gdb program

      Replace program with a file name or path to the program.

      GDB is ready to start execution of the program. You can configure breakpoints and the gdb environment before beginning the execution of the process with the run command. For more information on setting breakpoints, see Using GDB breakpoints to stop execution at defined code locations.

    2. Attach GDB to an already running process.
    3. Find the process ID (pid) with the ps command:

      $ ps -C program -o pid h
       pid

      Replace program with a file name or path to the program.

    4. Attach GDB to this process:

      $ gdb -p pid

      Replace pid with an actual process ID number from the ps output.

  • Attach an active GDB session to a running program:

    1. Use the shell GDB command to run the ps command and find the program’s process ID (pid):

      (gdb) shell ps -C program -o pid h
       pid

      Replace program with a file name or path to the program.

    2. Use the attach command to attach GDB to the program:

      (gdb) attach pid

      Replace pid by an actual process ID number from the ps output.

      注意

      In some cases, GDB might not be able to find the corresponding executable file. Use the file command to specify the path:

      (gdb) file path/to/program
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部