You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
12KB

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