4.7 Debugging an Already-running Process
- This command attaches to a running process—one that was started
outside gdb. (
info filesshows your active targets.) The command takes as argument a process ID. The usual way to find out the process-id of a Unix process is with the
psutility, or with the jobs -l shell command.
attachdoes not repeat if you press <RET> a second time after executing the command.
attach, your program must be running in an environment
which supports processes; for example,
attach does not work for
programs on bare-board targets that lack an operating system. You must
also have permission to send the process a signal.
When you use
attach, the debugger finds the program running in
the process first by looking in the current working directory, then (if
the program is not found) by using the source file search path
(see Specifying Source Directories). You can also use
file command to load the program. See Commands to Specify Files.
The first thing gdb does after arranging to debug the specified
process is to stop it. You can examine and modify an attached process
with all the gdb commands that are ordinarily available when
you start processes with
run. You can insert breakpoints; you
can step and continue; you can modify storage. If you would rather the
process continue running, you may use the
continue command after
attaching gdb to the process.
- When you have finished debugging the attached process, you can use the
detachcommand to release it from gdb control. Detaching the process continues its execution. After the
detachcommand, that process and gdb become completely independent once more, and you are ready to
attachanother process or start one with
detachdoes not repeat if you press <RET> again after executing the command.
If you exit gdb while you have an attached process, you detach
that process. If you use the
run command, you kill that process.
By default, gdb asks for confirmation if you try to do either of these
things; you can control whether or not you need to confirm by using the
set confirm command (see Optional Warnings and Messages).