3.2.2. Attaching GDB to a process
To examine a system process, attach the GNU Debugger (GDB) to the process.
Prerequisites
Procedure
Start a program with GDB.
Launch a program using GDB:
$ gdb programReplace program with a file name or path to the program.
GDBis ready to start execution of the program. You can configure breakpoints and thegdbenvironment before beginning the execution of the process with theruncommand. For more information on setting breakpoints, see Using GDB breakpoints to stop execution at defined code locations.- Attach GDB to an already running process.
Find the process ID (pid) with the
pscommand:$ ps -C program -o pid hpidReplace program with a file name or path to the program.
Attach GDB to this process:
$ gdb -p pidReplace pid with an actual process ID number from the
psoutput.
Attach an active GDB session to a running program:
Use the
shellGDB command to run thepscommand and find the program’s process ID (pid):(gdb) shell ps -C program -o pid hpidReplace program with a file name or path to the program.
Use the
attachcommand to attach GDB to the program:(gdb) attach pidReplace pid by an actual process ID number from the
psoutput.注意In some cases, GDB might not be able to find the corresponding executable file. Use the
filecommand to specify the path:(gdb) file path/to/program