Forks.html 12 KB

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