1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Attach</title>
18
19<meta name="description" content="Debugging with GDB: Attach">
20<meta name="keywords" content="Debugging with GDB: Attach">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Running.html#Running" rel="up" title="Running">
29<link href="Kill-Process.html#Kill-Process" rel="next" title="Kill Process">
30<link href="Input_002fOutput.html#Input_002fOutput" rel="previous" title="Input/Output">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Attach"></a>
65<div class="header">
66<p>
67Next: <a href="Kill-Process.html#Kill-Process" accesskey="n" rel="next">Kill Process</a>, Previous: <a href="Input_002fOutput.html#Input_002fOutput" accesskey="p" rel="previous">Input/Output</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
68</div>
69<hr>
70<a name="Debugging-an-Already_002dRunning-Process"></a>
71<h3 class="section">4.7 Debugging an Already-Running Process</h3>
72<a name="index-attach-1"></a>
73<a name="index-attach"></a>
74
75<dl compact="compact">
76<dt><code>attach <var>process-id</var></code></dt>
77<dd><p>This command attaches to a running process&mdash;one that was started
78outside <small>GDB</small>.  (<code>info files</code> shows your active
79targets.)  The command takes as argument a process ID.  The usual way to
80find out the <var>process-id</var> of a Unix process is with the <code>ps</code> utility,
81or with the &lsquo;<samp>jobs -l</samp>&rsquo; shell command.
82</p>
83<p><code>attach</code> does not repeat if you press <tt class="key">RET</tt> a second time after
84executing the command.
85</p></dd>
86</dl>
87
88<p>To use <code>attach</code>, your program must be running in an environment
89which supports processes; for example, <code>attach</code> does not work for
90programs on bare-board targets that lack an operating system.  You must
91also have permission to send the process a signal.
92</p>
93<p>When you use <code>attach</code>, the debugger finds the program running in
94the process first by looking in the current working directory, then (if
95the program is not found) by using the source file search path
96(see <a href="Source-Path.html#Source-Path">Specifying Source Directories</a>).  You can also use
97the <code>file</code> command to load the program.  See <a href="Files.html#Files">Commands to
98Specify Files</a>.
99</p>
100<a name="set-exec_002dfile_002dmismatch"></a><p>If the debugger can determine that the executable file running in the
101process it is attaching to does not match the current exec-file loaded
102by <small>GDB</small>, the option <code>exec-file-mismatch</code> specifies how to
103handle the mismatch.  <small>GDB</small> tries to compare the files by
104comparing their build IDs (see <a href="Separate-Debug-Files.html#build-ID">build ID</a>), if available.
105</p>
106<dl compact="compact">
107<dd><a name="index-exec_002dfile_002dmismatch"></a>
108<a name="index-set-exec_002dfile_002dmismatch"></a>
109</dd>
110<dt><code>set exec-file-mismatch &lsquo;<samp>ask|warn|off</samp>&rsquo;</code></dt>
111<dd>
112<p>Whether to detect mismatch between the current executable file loaded
113by <small>GDB</small> and the executable file used to start the process.  If
114&lsquo;<samp>ask</samp>&rsquo;, the default, display a warning and ask the user whether to
115load the process executable file; if &lsquo;<samp>warn</samp>&rsquo;, just display a
116warning; if &lsquo;<samp>off</samp>&rsquo;, don&rsquo;t attempt to detect a mismatch.
117If the user confirms loading the process executable file, then its symbols
118will be loaded as well.
119</p>
120<a name="index-show-exec_002dfile_002dmismatch"></a>
121</dd>
122<dt><code>show exec-file-mismatch</code></dt>
123<dd><p>Show the current value of <code>exec-file-mismatch</code>.
124</p>
125</dd>
126</dl>
127
128<p>The first thing <small>GDB</small> does after arranging to debug the specified
129process is to stop it.  You can examine and modify an attached process
130with all the <small>GDB</small> commands that are ordinarily available when
131you start processes with <code>run</code>.  You can insert breakpoints; you
132can step and continue; you can modify storage.  If you would rather the
133process continue running, you may use the <code>continue</code> command after
134attaching <small>GDB</small> to the process.
135</p>
136<dl compact="compact">
137<dd><a name="index-detach"></a>
138</dd>
139<dt><code>detach</code></dt>
140<dd><p>When you have finished debugging the attached process, you can use the
141<code>detach</code> command to release it from <small>GDB</small> control.  Detaching
142the process continues its execution.  After the <code>detach</code> command,
143that process and <small>GDB</small> become completely independent once more, and you
144are ready to <code>attach</code> another process or start one with <code>run</code>.
145<code>detach</code> does not repeat if you press <tt class="key">RET</tt> again after
146executing the command.
147</p></dd>
148</dl>
149
150<p>If you exit <small>GDB</small> while you have an attached process, you detach
151that process.  If you use the <code>run</code> command, you kill that process.
152By default, <small>GDB</small> asks for confirmation if you try to do either of these
153things; you can control whether or not you need to confirm by using the
154<code>set confirm</code> command (see <a href="Messages_002fWarnings.html#Messages_002fWarnings">Optional Warnings and
155Messages</a>).
156</p>
157<hr>
158<div class="header">
159<p>
160Next: <a href="Kill-Process.html#Kill-Process" accesskey="n" rel="next">Kill Process</a>, Previous: <a href="Input_002fOutput.html#Input_002fOutput" accesskey="p" rel="previous">Input/Output</a>, Up: <a href="Running.html#Running" accesskey="u" rel="up">Running</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
161</div>
162
163
164
165</body>
166</html>
167