1<html lang="en"> 2<head> 3<title>Forks - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Running.html#Running" title="Running"> 9<link rel="prev" href="Threads.html#Threads" title="Threads"> 10<link rel="next" href="Checkpoint_002fRestart.html#Checkpoint_002fRestart" title="Checkpoint/Restart"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Free Software'' and ``Free Software Needs 19Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 20and with the Back-Cover Texts as in (a) below. 21 22(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 23this GNU Manual. Buying copies from GNU Press supports the FSF in 24developing GNU and promoting software freedom.'' 25--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="Forks"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="Checkpoint_002fRestart.html#Checkpoint_002fRestart">Checkpoint/Restart</a>, 45Previous: <a rel="previous" accesskey="p" href="Threads.html#Threads">Threads</a>, 46Up: <a rel="up" accesskey="u" href="Running.html#Running">Running</a> 47<hr> 48</div> 49 50<h3 class="section">4.11 Debugging Forks</h3> 51 52<p><a name="index-fork_002c-debugging-programs-which-call-186"></a><a name="index-multiple-processes-187"></a><a name="index-processes_002c-multiple-188"></a>On most systems, <span class="sc">gdb</span> has no special support for debugging 53programs which create additional processes using the <code>fork</code> 54function. When a program forks, <span class="sc">gdb</span> will continue to debug the 55parent process and the child process will run unimpeded. If you have 56set a breakpoint in any code which the child then executes, the child 57will get a <code>SIGTRAP</code> signal which (unless it catches the signal) 58will cause it to terminate. 59 60 <p>However, if you want to debug the child process there is a workaround 61which isn't too painful. Put a call to <code>sleep</code> in the code which 62the child process executes after the fork. It may be useful to sleep 63only if a certain environment variable is set, or a certain file exists, 64so that the delay need not occur when you don't want to run <span class="sc">gdb</span> 65on the child. While the child is sleeping, use the <code>ps</code> program to 66get its process ID. Then tell <span class="sc">gdb</span> (a new invocation of 67<span class="sc">gdb</span> if you are also debugging the parent process) to attach to 68the child process (see <a href="Attach.html#Attach">Attach</a>). From that point on you can debug 69the child process just like any other process which you attached to. 70 71 <p>On some systems, <span class="sc">gdb</span> provides support for debugging programs that 72create additional processes using the <code>fork</code> or <code>vfork</code> functions. 73Currently, the only platforms with this feature are HP-UX (11.x and later 74only?) and <span class="sc">gnu</span>/Linux (kernel version 2.5.60 and later). 75 76 <p>By default, when a program forks, <span class="sc">gdb</span> will continue to debug 77the parent process and the child process will run unimpeded. 78 79 <p>If you want to follow the child process instead of the parent process, 80use the command <code>set follow-fork-mode</code><!-- /@w -->. 81 82 83<a name="index-set-follow_002dfork_002dmode-189"></a> 84<dl><dt><code>set follow-fork-mode </code><var>mode</var><dd>Set the debugger response to a program call of <code>fork</code> or 85<code>vfork</code>. A call to <code>fork</code> or <code>vfork</code> creates a new 86process. The <var>mode</var> argument can be: 87 88 <dl> 89<dt><code>parent</code><dd>The original process is debugged after a fork. The child process runs 90unimpeded. This is the default. 91 92 <br><dt><code>child</code><dd>The new process is debugged after a fork. The parent process runs 93unimpeded. 94 95 </dl> 96 97 <p><a name="index-show-follow_002dfork_002dmode-190"></a><br><dt><code>show follow-fork-mode</code><dd>Display the current debugger response to a <code>fork</code> or <code>vfork</code> call. 98</dl> 99 100 <p><a name="index-debugging-multiple-processes-191"></a>On Linux, if you want to debug both the parent and child processes, use the 101command <code>set detach-on-fork</code><!-- /@w -->. 102 103 104<a name="index-set-detach_002don_002dfork-192"></a> 105<dl><dt><code>set detach-on-fork </code><var>mode</var><dd>Tells gdb whether to detach one of the processes after a fork, or 106retain debugger control over them both. 107 108 <dl> 109<dt><code>on</code><dd>The child process (or parent process, depending on the value of 110<code>follow-fork-mode</code>) will be detached and allowed to run 111independently. This is the default. 112 113 <br><dt><code>off</code><dd>Both processes will be held under the control of <span class="sc">gdb</span>. 114One process (child or parent, depending on the value of 115<code>follow-fork-mode</code>) is debugged as usual, while the other 116is held suspended. 117 118 </dl> 119 120 <p><a name="index-show-detach_002don_002dfork-193"></a><br><dt><code>show detach-on-fork</code><dd>Show whether detach-on-fork mode is on/off. 121</dl> 122 123 <p>If you choose to set ‘<samp><span class="samp">detach-on-fork</span></samp>’ mode off, then <span class="sc">gdb</span> 124will retain control of all forked processes (including nested forks). 125You can list the forked processes under the control of <span class="sc">gdb</span> by 126using the <code>info inferiors</code><!-- /@w --> command, and switch from one fork 127to another by using the <code>inferior</code> command (see <a href="Inferiors-and-Programs.html#Inferiors-and-Programs">Debugging Multiple Inferiors and Programs</a>). 128 129 <p>To quit debugging one of the forked processes, you can either detach 130from it by using the <code>detach inferiors</code><!-- /@w --> command (allowing it 131to run independently), or kill it using the <code>kill inferiors</code><!-- /@w --> 132command. See <a href="Inferiors-and-Programs.html#Inferiors-and-Programs">Debugging Multiple Inferiors and Programs</a>. 133 134 <p>If you ask to debug a child process and a <code>vfork</code> is followed by an 135<code>exec</code>, <span class="sc">gdb</span> executes the new target up to the first 136breakpoint in the new target. If you have a breakpoint set on 137<code>main</code> in your original program, the breakpoint will also be set on 138the child process's <code>main</code>. 139 140 <p>On some systems, when a child process is spawned by <code>vfork</code>, you 141cannot debug the child or parent until an <code>exec</code> call completes. 142 143 <p>If you issue a <code>run</code> command to <span class="sc">gdb</span> after an <code>exec</code> 144call executes, the new target restarts. To restart the parent 145process, use the <code>file</code> command with the parent executable name 146as its argument. By default, after an <code>exec</code> call executes, 147<span class="sc">gdb</span> discards the symbols of the previous executable image. 148You can change this behaviour with the <code>set follow-exec-mode</code><!-- /@w --> 149command. 150 151 152<a name="index-set-follow_002dexec_002dmode-194"></a> 153<dl><dt><code>set follow-exec-mode </code><var>mode</var><dd> 154Set debugger response to a program call of <code>exec</code>. An 155<code>exec</code> call replaces the program image of a process. 156 157 <p><code>follow-exec-mode</code> can be: 158 159 <dl> 160<dt><code>new</code><dd><span class="sc">gdb</span> creates a new inferior and rebinds the process to this 161new inferior. The program the process was running before the 162<code>exec</code> call can be restarted afterwards by restarting the 163original inferior. 164 165 <p>For example: 166 167 <pre class="smallexample"> (gdb) info inferiors 168 (gdb) info inferior 169 Id Description Executable 170 * 1 <null> prog1 171 (gdb) run 172 process 12020 is executing new program: prog2 173 Program exited normally. 174 (gdb) info inferiors 175 Id Description Executable 176 * 2 <null> prog2 177 1 <null> prog1 178</pre> 179 <br><dt><code>same</code><dd><span class="sc">gdb</span> keeps the process bound to the same inferior. The new 180executable image replaces the previous executable loaded in the 181inferior. Restarting the inferior after the <code>exec</code> call, with 182e.g., the <code>run</code> command, restarts the executable the process was 183running after the <code>exec</code> call. This is the default mode. 184 185 <p>For example: 186 187 <pre class="smallexample"> (gdb) info inferiors 188 Id Description Executable 189 * 1 <null> prog1 190 (gdb) run 191 process 12020 is executing new program: prog2 192 Program exited normally. 193 (gdb) info inferiors 194 Id Description Executable 195 * 1 <null> prog2 196</pre> 197 </dl> 198 </dl> 199 200 <p>You can use the <code>catch</code> command to make <span class="sc">gdb</span> stop whenever 201a <code>fork</code>, <code>vfork</code>, or <code>exec</code> call is made. See <a href="Set-Catchpoints.html#Set-Catchpoints">Setting Catchpoints</a>. 202 203 </body></html> 204 205