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