hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
 
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
 
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual.  Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Forks</title>
 
<meta name="description" content="Debugging with GDB: Forks">
<meta name="keywords" content="Debugging with GDB: Forks">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Running.html#Running" rel="up" title="Running">
<link href="Checkpoint_002fRestart.html#Checkpoint_002fRestart" rel="next" title="Checkpoint/Restart">
<link href="Threads.html#Threads" rel="previous" title="Threads">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
 
 
</head>
 
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Forks"></a>
<div class="header">
<p>
Next: <a href="Checkpoint_002fRestart.html#Checkpoint_002fRestart" accesskey="n" rel="next">Checkpoint/Restart</a>, Previous: <a href="Threads.html#Threads" accesskey="p" rel="previous">Threads</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>
</div>
<hr>
<a name="Debugging-Forks"></a>
<h3 class="section">4.11 Debugging Forks</h3>
 
<a name="index-fork_002c-debugging-programs-which-call"></a>
<a name="index-multiple-processes"></a>
<a name="index-processes_002c-multiple"></a>
<p>On most systems, <small>GDB</small> has no special support for debugging
programs which create additional processes using the <code>fork</code>
function.  When a program forks, <small>GDB</small> will continue to debug the
parent process and the child process will run unimpeded.  If you have
set a breakpoint in any code which the child then executes, the child
will get a <code>SIGTRAP</code> signal which (unless it catches the signal)
will cause it to terminate.
</p>
<p>However, if you want to debug the child process there is a workaround
which isn&rsquo;t too painful.  Put a call to <code>sleep</code> in the code which
the child process executes after the fork.  It may be useful to sleep
only if a certain environment variable is set, or a certain file exists,
so that the delay need not occur when you don&rsquo;t want to run <small>GDB</small>
on the child.  While the child is sleeping, use the <code>ps</code> program to
get its process ID.  Then tell <small>GDB</small> (a new invocation of
<small>GDB</small> if you are also debugging the parent process) to attach to
the child process (see <a href="Attach.html#Attach">Attach</a>).  From that point on you can debug
the child process just like any other process which you attached to.
</p>
<p>On some systems, <small>GDB</small> provides support for debugging programs
that create additional processes using the <code>fork</code> or <code>vfork</code>
functions.  On <small>GNU</small>/Linux platforms, this feature is supported
with kernel version 2.5.46 and later.
</p>
<p>The fork debugging commands are supported in native mode and when
connected to <code>gdbserver</code> in either <code>target remote</code> mode or
<code>target extended-remote</code> mode.
</p>
<p>By default, when a program forks, <small>GDB</small> will continue to debug
the parent process and the child process will run unimpeded.
</p>
<p>If you want to follow the child process instead of the parent process,
use the command <code>set&nbsp;<span class="nolinebreak">follow-fork-mode</span></code><!-- /@w -->.
</p>
<dl compact="compact">
<dd><a name="index-set-follow_002dfork_002dmode"></a>
</dd>
<dt><code>set follow-fork-mode <var>mode</var></code></dt>
<dd><p>Set the debugger response to a program call of <code>fork</code> or
<code>vfork</code>.  A call to <code>fork</code> or <code>vfork</code> creates a new
process.  The <var>mode</var> argument can be:
</p>
<dl compact="compact">
<dt><code>parent</code></dt>
<dd><p>The original process is debugged after a fork.  The child process runs
unimpeded.  This is the default.
</p>
</dd>
<dt><code>child</code></dt>
<dd><p>The new process is debugged after a fork.  The parent process runs
unimpeded.
</p>
</dd>
</dl>
 
<a name="index-show-follow_002dfork_002dmode"></a>
</dd>
<dt><code>show follow-fork-mode</code></dt>
<dd><p>Display the current debugger response to a <code>fork</code> or <code>vfork</code> call.
</p></dd>
</dl>
 
<a name="index-debugging-multiple-processes"></a>
<p>On Linux, if you want to debug both the parent and child processes, use the
command <code>set&nbsp;<span class="nolinebreak">detach-on-fork</span></code><!-- /@w -->.
</p>
<dl compact="compact">
<dd><a name="index-set-detach_002don_002dfork"></a>
</dd>
<dt><code>set detach-on-fork <var>mode</var></code></dt>
<dd><p>Tells gdb whether to detach one of the processes after a fork, or
retain debugger control over them both.
</p>
<dl compact="compact">
<dt><code>on</code></dt>
<dd><p>The child process (or parent process, depending on the value of
<code>follow-fork-mode</code>) will be detached and allowed to run 
independently.  This is the default.
</p>
</dd>
<dt><code>off</code></dt>
<dd><p>Both processes will be held under the control of <small>GDB</small>.
One process (child or parent, depending on the value of 
<code>follow-fork-mode</code>) is debugged as usual, while the other
is held suspended.  
</p>
</dd>
</dl>
 
<a name="index-show-detach_002don_002dfork"></a>
</dd>
<dt><code>show detach-on-fork</code></dt>
<dd><p>Show whether detach-on-fork mode is on/off.
</p></dd>
</dl>
 
<p>If you choose to set &lsquo;<samp>detach-on-fork</samp>&rsquo; mode off, then <small>GDB</small>
will retain control of all forked processes (including nested forks).
You can list the forked processes under the control of <small>GDB</small> by
using the <code>info&nbsp;inferiors</code><!-- /@w --> command, and switch from one fork
to another by using the <code>inferior</code> command (see <a href="Inferiors-Connections-and-Programs.html#Inferiors-Connections-and-Programs">Debugging Multiple Inferiors Connections and Programs</a>).
</p>
<p>To quit debugging one of the forked processes, you can either detach
from it by using the <code>detach&nbsp;inferiors</code><!-- /@w --> command (allowing it
to run independently), or kill it using the <code>kill&nbsp;inferiors</code><!-- /@w -->
command.  See <a href="Inferiors-Connections-and-Programs.html#Inferiors-Connections-and-Programs">Debugging
Multiple Inferiors Connections and Programs</a>.
</p>
<p>If you ask to debug a child process and a <code>vfork</code> is followed by an
<code>exec</code>, <small>GDB</small> executes the new target up to the first
breakpoint in the new target.  If you have a breakpoint set on
<code>main</code> in your original program, the breakpoint will also be set on
the child process&rsquo;s <code>main</code>.
</p>
<p>On some systems, when a child process is spawned by <code>vfork</code>, you
cannot debug the child or parent until an <code>exec</code> call completes.
</p>
<p>If you issue a <code>run</code> command to <small>GDB</small> after an <code>exec</code>
call executes, the new target restarts.  To restart the parent
process, use the <code>file</code> command with the parent executable name
as its argument.  By default, after an <code>exec</code> call executes,
<small>GDB</small> discards the symbols of the previous executable image.
You can change this behaviour with the <code>set&nbsp;<span class="nolinebreak">follow-exec-mode</span></code><!-- /@w -->
command.
</p>
<dl compact="compact">
<dd><a name="index-set-follow_002dexec_002dmode"></a>
</dd>
<dt><code>set follow-exec-mode <var>mode</var></code></dt>
<dd>
<p>Set debugger response to a program call of <code>exec</code>.  An
<code>exec</code> call replaces the program image of a process.
</p>
<p><code>follow-exec-mode</code> can be:
</p>
<dl compact="compact">
<dt><code>new</code></dt>
<dd><p><small>GDB</small> creates a new inferior and rebinds the process to this
new inferior.  The program the process was running before the
<code>exec</code> call can be restarted afterwards by restarting the
original inferior.
</p>
<p>For example:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) info inferiors
(gdb) info inferior
  Id   Description   Executable
* 1    &lt;null&gt;        prog1
(gdb) run
process 12020 is executing new program: prog2
Program exited normally.
(gdb) info inferiors
  Id   Description   Executable
  1    &lt;null&gt;        prog1
* 2    &lt;null&gt;        prog2
</pre></div>
 
</dd>
<dt><code>same</code></dt>
<dd><p><small>GDB</small> keeps the process bound to the same inferior.  The new
executable image replaces the previous executable loaded in the
inferior.  Restarting the inferior after the <code>exec</code> call, with
e.g., the <code>run</code> command, restarts the executable the process was
running after the <code>exec</code> call.  This is the default mode.
</p>
<p>For example:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) info inferiors
  Id   Description   Executable
* 1    &lt;null&gt;        prog1
(gdb) run
process 12020 is executing new program: prog2
Program exited normally.
(gdb) info inferiors
  Id   Description   Executable
* 1    &lt;null&gt;        prog2
</pre></div>
 
</dd>
</dl>
</dd>
</dl>
 
<p><code>follow-exec-mode</code> is supported in native mode and
<code>target extended-remote</code> mode.
</p>
<p>You can use the <code>catch</code> command to make <small>GDB</small> stop whenever
a <code>fork</code>, <code>vfork</code>, or <code>exec</code> call is made.  See <a href="Set-Catchpoints.html#Set-Catchpoints">Setting Catchpoints</a>.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Checkpoint_002fRestart.html#Checkpoint_002fRestart" accesskey="n" rel="next">Checkpoint/Restart</a>, Previous: <a href="Threads.html#Threads" accesskey="p" rel="previous">Threads</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>
</div>
 
 
 
</body>
</html>