### 4.7 Debugging an Already-running Process ¶

`attach process-id`

  This command attaches to a running process—one that was started outside GDB. (`info files` shows 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 `ps` utility, or with the ‘`jobs -l`’ shell command.

  `attach` does not repeat if you press `RET` a second time after executing the command.

To use `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 the `file` command to load the program. See Commands to Specify Files.

If the debugger can determine that the executable file running in the process it is attaching to does not match the current exec-file loaded by GDB, the option `exec-file-mismatch` specifies how to handle the mismatch. GDB tries to compare the files by comparing their build IDs (see build ID), if available.

`set exec-file-mismatch ‘`ask|warn|off`’` ¶

  Whether to detect mismatch between the current executable file loaded by GDB and the executable file used to start the process. If ‘`ask`’, the default, display a warning and ask the user whether to load the process executable file; if ‘`warn`’, just display a warning; if ‘`off`’, don’t attempt to detect a mismatch. If the user confirms loading the process executable file, then its symbols will be loaded as well.

`show exec-file-mismatch` ¶

  Show the current value of `exec-file-mismatch`.

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.

`detach` ¶

  When you have finished debugging the attached process, you can use the `detach` command to release it from GDB control. Detaching the process continues its execution. After the `detach` command, that process and GDB become completely independent once more, and you are ready to `attach` another process or start one with `run`. `detach` does 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).