您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

371 行
16KB

  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>Inferiors Connections and Programs (Debugging with GDB)</title>
  17. <meta name="description" content="Inferiors Connections and Programs (Debugging with GDB)">
  18. <meta name="keywords" content="Inferiors Connections and Programs (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="Threads.html#Threads" rel="next" title="Threads">
  27. <link href="Kill-Process.html#Kill-Process" rel="prev" title="Kill Process">
  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="Inferiors-Connections-and-Programs"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Threads.html#Threads" accesskey="n" rel="next">Threads</a>, Previous: <a href="Kill-Process.html#Kill-Process" accesskey="p" rel="prev">Kill Process</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-Multiple-Inferiors-Connections-and-Programs"></a>
  64. <h3 class="section">4.9 Debugging Multiple Inferiors Connections and Programs</h3>
  65. <p><small>GDB</small> lets you run and debug multiple programs in a single
  66. session. In addition, <small>GDB</small> on some systems may let you run
  67. several programs simultaneously (otherwise you have to exit from one
  68. before starting another). On some systems <small>GDB</small> may even let
  69. you debug several programs simultaneously on different remote systems.
  70. In the most general case, you can have multiple threads of execution
  71. in each of multiple processes, launched from multiple executables,
  72. running on different machines.
  73. </p>
  74. <a name="index-inferior"></a>
  75. <p><small>GDB</small> represents the state of each program execution with an
  76. object called an <em>inferior</em>. An inferior typically corresponds to
  77. a process, but is more general and applies also to targets that do not
  78. have processes. Inferiors may be created before a process runs, and
  79. may be retained after a process exits. Inferiors have unique
  80. identifiers that are different from process ids. Usually each
  81. inferior will also have its own distinct address space, although some
  82. embedded targets may have several inferiors running in different parts
  83. of a single address space. Each inferior may in turn have multiple
  84. threads running in it.
  85. </p>
  86. <p>To find out what inferiors exist at any moment, use <code>info&nbsp;inferiors</code><!-- /@w -->:
  87. </p>
  88. <dl compact="compact">
  89. <dd><a name="index-info-inferiors-_005b-id_2026-_005d"></a>
  90. </dd>
  91. <dt><code>info inferiors</code></dt>
  92. <dd><p>Print a list of all inferiors currently being managed by <small>GDB</small>.
  93. By default all inferiors are printed, but the argument <var>id</var>&hellip;
  94. &ndash; a space separated list of inferior numbers &ndash; can be used to limit
  95. the display to just the requested inferiors.
  96. </p>
  97. <p><small>GDB</small> displays for each inferior (in this order):
  98. </p>
  99. <ol>
  100. <li> the inferior number assigned by <small>GDB</small>
  101. </li><li> the target system&rsquo;s inferior identifier
  102. </li><li> the target connection the inferior is bound to, including the unique
  103. connection number assigned by <small>GDB</small>, and the protocol used by
  104. the connection.
  105. </li><li> the name of the executable the inferior is running.
  106. </li></ol>
  107. <p>An asterisk &lsquo;<samp>*</samp>&rsquo; preceding the <small>GDB</small> inferior number
  108. indicates the current inferior.
  109. </p>
  110. <p>For example,
  111. </p></dd>
  112. </dl>
  113. <div class="smallexample">
  114. <pre class="smallexample">(gdb) info inferiors
  115. Num Description Connection Executable
  116. * 1 process 3401 1 (native) goodbye
  117. 2 process 2307 2 (extended-remote host:10000) hello
  118. </pre></div>
  119. <p>To find out what open target connections exist at any moment, use
  120. <code>info&nbsp;connections</code><!-- /@w -->:
  121. </p>
  122. <dl compact="compact">
  123. <dd><a name="index-info-connections-_005b-id_2026-_005d"></a>
  124. </dd>
  125. <dt><code>info connections</code></dt>
  126. <dd><p>Print a list of all open target connections currently being managed by
  127. <small>GDB</small>. By default all connections are printed, but the
  128. argument <var>id</var>&hellip; &ndash; a space separated list of connections
  129. numbers &ndash; can be used to limit the display to just the requested
  130. connections.
  131. </p>
  132. <p><small>GDB</small> displays for each connection (in this order):
  133. </p>
  134. <ol>
  135. <li> the connection number assigned by <small>GDB</small>.
  136. </li><li> the protocol used by the connection.
  137. </li><li> a textual description of the protocol used by the connection.
  138. </li></ol>
  139. <p>An asterisk &lsquo;<samp>*</samp>&rsquo; preceding the connection number indicates the
  140. connection of the current inferior.
  141. </p>
  142. <p>For example,
  143. </p></dd>
  144. </dl>
  145. <div class="smallexample">
  146. <pre class="smallexample">(gdb) info connections
  147. Num What Description
  148. * 1 extended-remote host:10000 Extended remote serial target in gdb-specific protocol
  149. 2 native Native process
  150. 3 core Local core dump file
  151. </pre></div>
  152. <p>To switch focus between inferiors, use the <code>inferior</code> command:
  153. </p>
  154. <dl compact="compact">
  155. <dd><a name="index-inferior-infno"></a>
  156. </dd>
  157. <dt><code>inferior <var>infno</var></code></dt>
  158. <dd><p>Make inferior number <var>infno</var> the current inferior. The argument
  159. <var>infno</var> is the inferior number assigned by <small>GDB</small>, as shown
  160. in the first field of the &lsquo;<samp>info inferiors</samp>&rsquo; display.
  161. </p></dd>
  162. </dl>
  163. <a name="index-_0024_005finferior_002c-convenience-variable"></a>
  164. <p>The debugger convenience variable &lsquo;<samp>$_inferior</samp>&rsquo; contains the
  165. number of the current inferior. You may find this useful in writing
  166. breakpoint conditional expressions, command scripts, and so forth.
  167. See <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>, for general
  168. information on convenience variables.
  169. </p>
  170. <p>You can get multiple executables into a debugging session via the
  171. <code>add-inferior</code> and <code><span class="nolinebreak">clone-inferior</span></code><!-- /@w --> commands. On some
  172. systems <small>GDB</small> can add inferiors to the debug session
  173. automatically by following calls to <code>fork</code> and <code>exec</code>. To
  174. remove inferiors from the debugging session use the
  175. <code><span class="nolinebreak">remove-inferiors</span></code><!-- /@w --> command.
  176. </p>
  177. <dl compact="compact">
  178. <dd><a name="index-add_002dinferior"></a>
  179. </dd>
  180. <dt><code>add-inferior [ -copies <var>n</var> ] [ -exec <var>executable</var> ] [-no-connection ]</code></dt>
  181. <dd><p>Adds <var>n</var> inferiors to be run using <var>executable</var> as the
  182. executable; <var>n</var> defaults to 1. If no executable is specified,
  183. the inferiors begins empty, with no program. You can still assign or
  184. change the program assigned to the inferior at any time by using the
  185. <code>file</code> command with the executable name as its argument.
  186. </p>
  187. <p>By default, the new inferior begins connected to the same target
  188. connection as the current inferior. For example, if the current
  189. inferior was connected to <code>gdbserver</code> with <code>target remote</code>,
  190. then the new inferior will be connected to the same <code>gdbserver</code>
  191. instance. The &lsquo;<samp>-no-connection</samp>&rsquo; option starts the new inferior
  192. with no connection yet. You can then for example use the <code>target
  193. remote</code> command to connect to some other <code>gdbserver</code> instance,
  194. use <code>run</code> to spawn a local program, etc.
  195. </p>
  196. <a name="index-clone_002dinferior"></a>
  197. </dd>
  198. <dt><code>clone-inferior [ -copies <var>n</var> ] [ <var>infno</var> ]</code></dt>
  199. <dd><p>Adds <var>n</var> inferiors ready to execute the same program as inferior
  200. <var>infno</var>; <var>n</var> defaults to 1, and <var>infno</var> defaults to the
  201. number of the current inferior. This is a convenient command when you
  202. want to run another instance of the inferior you are debugging.
  203. </p>
  204. <div class="smallexample">
  205. <pre class="smallexample">(gdb) info inferiors
  206. Num Description Connection Executable
  207. * 1 process 29964 1 (native) helloworld
  208. (gdb) clone-inferior
  209. Added inferior 2.
  210. 1 inferiors added.
  211. (gdb) info inferiors
  212. Num Description Connection Executable
  213. * 1 process 29964 1 (native) helloworld
  214. 2 &lt;null&gt; 1 (native) helloworld
  215. </pre></div>
  216. <p>You can now simply switch focus to inferior 2 and run it.
  217. </p>
  218. <a name="index-remove_002dinferiors"></a>
  219. </dd>
  220. <dt><code>remove-inferiors <var>infno</var>&hellip;</code></dt>
  221. <dd><p>Removes the inferior or inferiors <var>infno</var>&hellip;. It is not
  222. possible to remove an inferior that is running with this command. For
  223. those, use the <code>kill</code> or <code>detach</code> command first.
  224. </p>
  225. </dd>
  226. </dl>
  227. <p>To quit debugging one of the running inferiors that is not the current
  228. inferior, you can either detach from it by using the <code>detach&nbsp;inferior</code><!-- /@w --> command (allowing it to run independently), or kill it
  229. using the <code>kill&nbsp;inferiors</code><!-- /@w --> command:
  230. </p>
  231. <dl compact="compact">
  232. <dd><a name="index-detach-inferiors-infno_2026"></a>
  233. </dd>
  234. <dt><code>detach inferior <var>infno</var>&hellip;</code></dt>
  235. <dd><p>Detach from the inferior or inferiors identified by <small>GDB</small>
  236. inferior number(s) <var>infno</var>&hellip;. Note that the inferior&rsquo;s entry
  237. still stays on the list of inferiors shown by <code>info inferiors</code>,
  238. but its Description will show &lsquo;<samp>&lt;null&gt;</samp>&rsquo;.
  239. </p>
  240. <a name="index-kill-inferiors-infno_2026"></a>
  241. </dd>
  242. <dt><code>kill inferiors <var>infno</var>&hellip;</code></dt>
  243. <dd><p>Kill the inferior or inferiors identified by <small>GDB</small> inferior
  244. number(s) <var>infno</var>&hellip;. Note that the inferior&rsquo;s entry still
  245. stays on the list of inferiors shown by <code>info inferiors</code>, but its
  246. Description will show &lsquo;<samp>&lt;null&gt;</samp>&rsquo;.
  247. </p></dd>
  248. </dl>
  249. <p>After the successful completion of a command such as <code>detach</code>,
  250. <code>detach inferiors</code>, <code>kill</code> or <code>kill inferiors</code>, or after
  251. a normal process exit, the inferior is still valid and listed with
  252. <code>info inferiors</code>, ready to be restarted.
  253. </p>
  254. <p>To be notified when inferiors are started or exit under <small>GDB</small>&rsquo;s
  255. control use <code>set&nbsp;print&nbsp;<span class="nolinebreak">inferior-events</span></code><!-- /@w -->:
  256. </p>
  257. <dl compact="compact">
  258. <dd><a name="index-set-print-inferior_002devents"></a>
  259. <a name="index-print-messages-on-inferior-start-and-exit"></a>
  260. </dd>
  261. <dt><code>set print inferior-events</code></dt>
  262. <dt><code>set print inferior-events on</code></dt>
  263. <dt><code>set print inferior-events off</code></dt>
  264. <dd><p>The <code>set print inferior-events</code> command allows you to enable or
  265. disable printing of messages when <small>GDB</small> notices that new
  266. inferiors have started or that inferiors have exited or have been
  267. detached. By default, these messages will not be printed.
  268. </p>
  269. <a name="index-show-print-inferior_002devents"></a>
  270. </dd>
  271. <dt><code>show print inferior-events</code></dt>
  272. <dd><p>Show whether messages will be printed when <small>GDB</small> detects that
  273. inferiors have started, exited or have been detached.
  274. </p></dd>
  275. </dl>
  276. <p>Many commands will work the same with multiple programs as with a
  277. single program: e.g., <code>print myglobal</code> will simply display the
  278. value of <code>myglobal</code> in the current inferior.
  279. </p>
  280. <p>Occasionally, when debugging <small>GDB</small> itself, it may be useful to
  281. get more info about the relationship of inferiors, programs, address
  282. spaces in a debug session. You can do that with the <code>maint&nbsp;info&nbsp;<span class="nolinebreak">program-spaces</span></code><!-- /@w --> command.
  283. </p>
  284. <dl compact="compact">
  285. <dd><a name="index-maint-info-program_002dspaces"></a>
  286. </dd>
  287. <dt><code>maint info program-spaces</code></dt>
  288. <dd><p>Print a list of all program spaces currently being managed by
  289. <small>GDB</small>.
  290. </p>
  291. <p><small>GDB</small> displays for each program space (in this order):
  292. </p>
  293. <ol>
  294. <li> the program space number assigned by <small>GDB</small>
  295. </li><li> the name of the executable loaded into the program space, with e.g.,
  296. the <code>file</code> command.
  297. </li></ol>
  298. <p>An asterisk &lsquo;<samp>*</samp>&rsquo; preceding the <small>GDB</small> program space number
  299. indicates the current program space.
  300. </p>
  301. <p>In addition, below each program space line, <small>GDB</small> prints extra
  302. information that isn&rsquo;t suitable to display in tabular form. For
  303. example, the list of inferiors bound to the program space.
  304. </p>
  305. <div class="smallexample">
  306. <pre class="smallexample">(gdb) maint info program-spaces
  307. Id Executable
  308. * 1 hello
  309. 2 goodbye
  310. Bound inferiors: ID 1 (process 21561)
  311. </pre></div>
  312. <p>Here we can see that no inferior is running the program <code>hello</code>,
  313. while <code>process 21561</code> is running the program <code>goodbye</code>. On
  314. some targets, it is possible that multiple inferiors are bound to the
  315. same program space. The most common example is that of debugging both
  316. the parent and child processes of a <code>vfork</code> call. For example,
  317. </p>
  318. <div class="smallexample">
  319. <pre class="smallexample">(gdb) maint info program-spaces
  320. Id Executable
  321. * 1 vfork-test
  322. Bound inferiors: ID 2 (process 18050), ID 1 (process 18045)
  323. </pre></div>
  324. <p>Here, both inferior 2 and inferior 1 are running in the same program
  325. space as a result of inferior 1 having executed a <code>vfork</code> call.
  326. </p></dd>
  327. </dl>
  328. <hr>
  329. <div class="header">
  330. <p>
  331. Next: <a href="Threads.html#Threads" accesskey="n" rel="next">Threads</a>, Previous: <a href="Kill-Process.html#Kill-Process" accesskey="p" rel="prev">Kill Process</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>
  332. </div>
  333. </body>
  334. </html>