245 lines
12 KiB
HTML
245 lines
12 KiB
HTML
<html lang="en">
|
|
<head>
|
|
<title>Inferiors and Programs - Debugging with GDB</title>
|
|
<meta http-equiv="Content-Type" content="text/html">
|
|
<meta name="description" content="Debugging with GDB">
|
|
<meta name="generator" content="makeinfo 4.13">
|
|
<link title="Top" rel="start" href="index.html#Top">
|
|
<link rel="up" href="Running.html#Running" title="Running">
|
|
<link rel="prev" href="Kill-Process.html#Kill-Process" title="Kill Process">
|
|
<link rel="next" href="Threads.html#Threads" title="Threads">
|
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
|
<!--
|
|
Copyright (C) 1988-2019 Free Software Foundation, Inc.
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
any later version published by the Free Software Foundation; with the
|
|
Invariant Sections being ``Free Software'' and ``Free Software Needs
|
|
Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,''
|
|
and with the Back-Cover Texts as in (a) below.
|
|
|
|
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
|
|
this GNU Manual. Buying copies from GNU Press supports the FSF in
|
|
developing GNU and promoting software freedom.''
|
|
-->
|
|
<meta http-equiv="Content-Style-Type" content="text/css">
|
|
<style type="text/css"><!--
|
|
pre.display { font-family:inherit }
|
|
pre.format { font-family:inherit }
|
|
pre.smalldisplay { font-family:inherit; font-size:smaller }
|
|
pre.smallformat { font-family:inherit; font-size:smaller }
|
|
pre.smallexample { font-size:smaller }
|
|
pre.smalllisp { font-size:smaller }
|
|
span.sc { font-variant:small-caps }
|
|
span.roman { font-family:serif; font-weight:normal; }
|
|
span.sansserif { font-family:sans-serif; font-weight:normal; }
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
<div class="node">
|
|
<a name="Inferiors-and-Programs"></a>
|
|
<p>
|
|
Next: <a rel="next" accesskey="n" href="Threads.html#Threads">Threads</a>,
|
|
Previous: <a rel="previous" accesskey="p" href="Kill-Process.html#Kill-Process">Kill Process</a>,
|
|
Up: <a rel="up" accesskey="u" href="Running.html#Running">Running</a>
|
|
<hr>
|
|
</div>
|
|
|
|
<h3 class="section">4.9 Debugging Multiple Inferiors and Programs</h3>
|
|
|
|
<p><span class="sc">gdb</span> lets you run and debug multiple programs in a single
|
|
session. In addition, <span class="sc">gdb</span> on some systems may let you run
|
|
several programs simultaneously (otherwise you have to exit from one
|
|
before starting another). In the most general case, you can have
|
|
multiple threads of execution in each of multiple processes, launched
|
|
from multiple executables.
|
|
|
|
<p><a name="index-inferior-157"></a><span class="sc">gdb</span> represents the state of each program execution with an
|
|
object called an <dfn>inferior</dfn>. An inferior typically corresponds to
|
|
a process, but is more general and applies also to targets that do not
|
|
have processes. Inferiors may be created before a process runs, and
|
|
may be retained after a process exits. Inferiors have unique
|
|
identifiers that are different from process ids. Usually each
|
|
inferior will also have its own distinct address space, although some
|
|
embedded targets may have several inferiors running in different parts
|
|
of a single address space. Each inferior may in turn have multiple
|
|
threads running in it.
|
|
|
|
<p>To find out what inferiors exist at any moment, use <code>info inferiors</code><!-- /@w -->:
|
|
|
|
|
|
<a name="index-info-inferiors-_005b-_0040var_007bid_007d_0040dots_007b_007d-_005d-158"></a>
|
|
<dl><dt><code>info inferiors</code><dd>Print a list of all inferiors currently being managed by <span class="sc">gdb</span>.
|
|
By default all inferiors are printed, but the argument <var>id</var><small class="dots">...</small>
|
|
– a space separated list of inferior numbers – can be used to limit
|
|
the display to just the requested inferiors.
|
|
|
|
<p><span class="sc">gdb</span> displays for each inferior (in this order):
|
|
|
|
<ol type=1 start=1>
|
|
<li>the inferior number assigned by <span class="sc">gdb</span>
|
|
|
|
<li>the target system's inferior identifier
|
|
|
|
<li>the name of the executable the inferior is running.
|
|
|
|
</ol>
|
|
|
|
<p class="noindent">An asterisk ‘<samp><span class="samp">*</span></samp>’ preceding the <span class="sc">gdb</span> inferior number
|
|
indicates the current inferior.
|
|
|
|
<p>For example,
|
|
</dl>
|
|
<!-- end table here to get a little more width for example -->
|
|
|
|
<pre class="smallexample"> (gdb) info inferiors
|
|
Num Description Executable
|
|
2 process 2307 hello
|
|
* 1 process 3401 goodbye
|
|
</pre>
|
|
<p>To switch focus between inferiors, use the <code>inferior</code> command:
|
|
|
|
|
|
<a name="index-inferior-_0040var_007binfno_007d-159"></a>
|
|
<dl><dt><code>inferior </code><var>infno</var><dd>Make inferior number <var>infno</var> the current inferior. The argument
|
|
<var>infno</var> is the inferior number assigned by <span class="sc">gdb</span>, as shown
|
|
in the first field of the ‘<samp><span class="samp">info inferiors</span></samp>’ display.
|
|
</dl>
|
|
|
|
<p><a name="index-g_t_0024_005finferior_0040r_007b_002c-convenience-variable_007d-160"></a>The debugger convenience variable ‘<samp><span class="samp">$_inferior</span></samp>’ contains the
|
|
number of the current inferior. You may find this useful in writing
|
|
breakpoint conditional expressions, command scripts, and so forth.
|
|
See <a href="Convenience-Vars.html#Convenience-Vars">Convenience Variables</a>, for general
|
|
information on convenience variables.
|
|
|
|
<p>You can get multiple executables into a debugging session via the
|
|
<code>add-inferior</code> and <code>clone-inferior</code><!-- /@w --> commands. On some
|
|
systems <span class="sc">gdb</span> can add inferiors to the debug session
|
|
automatically by following calls to <code>fork</code> and <code>exec</code>. To
|
|
remove inferiors from the debugging session use the
|
|
<code>remove-inferiors</code><!-- /@w --> command.
|
|
|
|
|
|
<a name="index-add_002dinferior-161"></a>
|
|
<dl><dt><code>add-inferior [ -copies </code><var>n</var><code> ] [ -exec </code><var>executable</var><code> ]</code><dd>Adds <var>n</var> inferiors to be run using <var>executable</var> as the
|
|
executable; <var>n</var> defaults to 1. If no executable is specified,
|
|
the inferiors begins empty, with no program. You can still assign or
|
|
change the program assigned to the inferior at any time by using the
|
|
<code>file</code> command with the executable name as its argument.
|
|
|
|
<p><a name="index-clone_002dinferior-162"></a><br><dt><code>clone-inferior [ -copies </code><var>n</var><code> ] [ </code><var>infno</var><code> ]</code><dd>Adds <var>n</var> inferiors ready to execute the same program as inferior
|
|
<var>infno</var>; <var>n</var> defaults to 1, and <var>infno</var> defaults to the
|
|
number of the current inferior. This is a convenient command when you
|
|
want to run another instance of the inferior you are debugging.
|
|
|
|
<pre class="smallexample"> (gdb) info inferiors
|
|
Num Description Executable
|
|
* 1 process 29964 helloworld
|
|
(gdb) clone-inferior
|
|
Added inferior 2.
|
|
1 inferiors added.
|
|
(gdb) info inferiors
|
|
Num Description Executable
|
|
2 <null> helloworld
|
|
* 1 process 29964 helloworld
|
|
</pre>
|
|
<p>You can now simply switch focus to inferior 2 and run it.
|
|
|
|
<p><a name="index-remove_002dinferiors-163"></a><br><dt><code>remove-inferiors </code><var>infno</var><code>...</code><dd>Removes the inferior or inferiors <var>infno</var><small class="dots">...</small>. It is not
|
|
possible to remove an inferior that is running with this command. For
|
|
those, use the <code>kill</code> or <code>detach</code> command first.
|
|
|
|
</dl>
|
|
|
|
<p>To quit debugging one of the running inferiors that is not the current
|
|
inferior, you can either detach from it by using the <code>detach inferior</code><!-- /@w --> command (allowing it to run independently), or kill it
|
|
using the <code>kill inferiors</code><!-- /@w --> command:
|
|
|
|
|
|
<a name="index-detach-inferiors-_0040var_007binfno_007d_0040dots_007b_007d-164"></a>
|
|
<dl><dt><code>detach inferior </code><var>infno</var><code>...</code><dd>Detach from the inferior or inferiors identified by <span class="sc">gdb</span>
|
|
inferior number(s) <var>infno</var><small class="dots">...</small>. Note that the inferior's entry
|
|
still stays on the list of inferiors shown by <code>info inferiors</code>,
|
|
but its Description will show ‘<samp><span class="samp"><null></span></samp>’.
|
|
|
|
<p><a name="index-kill-inferiors-_0040var_007binfno_007d_0040dots_007b_007d-165"></a><br><dt><code>kill inferiors </code><var>infno</var><code>...</code><dd>Kill the inferior or inferiors identified by <span class="sc">gdb</span> inferior
|
|
number(s) <var>infno</var><small class="dots">...</small>. Note that the inferior's entry still
|
|
stays on the list of inferiors shown by <code>info inferiors</code>, but its
|
|
Description will show ‘<samp><span class="samp"><null></span></samp>’.
|
|
</dl>
|
|
|
|
<p>After the successful completion of a command such as <code>detach</code>,
|
|
<code>detach inferiors</code>, <code>kill</code> or <code>kill inferiors</code>, or after
|
|
a normal process exit, the inferior is still valid and listed with
|
|
<code>info inferiors</code>, ready to be restarted.
|
|
|
|
<p>To be notified when inferiors are started or exit under <span class="sc">gdb</span>'s
|
|
control use <code>set print inferior-events</code><!-- /@w -->:
|
|
|
|
|
|
<a name="index-set-print-inferior_002devents-166"></a>
|
|
<a name="index-print-messages-on-inferior-start-and-exit-167"></a>
|
|
<dl><dt><code>set print inferior-events</code><dt><code>set print inferior-events on</code><dt><code>set print inferior-events off</code><dd>The <code>set print inferior-events</code> command allows you to enable or
|
|
disable printing of messages when <span class="sc">gdb</span> notices that new
|
|
inferiors have started or that inferiors have exited or have been
|
|
detached. By default, these messages will not be printed.
|
|
|
|
<p><a name="index-show-print-inferior_002devents-168"></a><br><dt><code>show print inferior-events</code><dd>Show whether messages will be printed when <span class="sc">gdb</span> detects that
|
|
inferiors have started, exited or have been detached.
|
|
</dl>
|
|
|
|
<p>Many commands will work the same with multiple programs as with a
|
|
single program: e.g., <code>print myglobal</code> will simply display the
|
|
value of <code>myglobal</code> in the current inferior.
|
|
|
|
<p>Occasionaly, when debugging <span class="sc">gdb</span> itself, it may be useful to
|
|
get more info about the relationship of inferiors, programs, address
|
|
spaces in a debug session. You can do that with the <code>maint info program-spaces</code><!-- /@w --> command.
|
|
|
|
|
|
<a name="index-maint-info-program_002dspaces-169"></a>
|
|
<dl><dt><code>maint info program-spaces</code><dd>Print a list of all program spaces currently being managed by
|
|
<span class="sc">gdb</span>.
|
|
|
|
<p><span class="sc">gdb</span> displays for each program space (in this order):
|
|
|
|
<ol type=1 start=1>
|
|
<li>the program space number assigned by <span class="sc">gdb</span>
|
|
|
|
<li>the name of the executable loaded into the program space, with e.g.,
|
|
the <code>file</code> command.
|
|
|
|
</ol>
|
|
|
|
<p class="noindent">An asterisk ‘<samp><span class="samp">*</span></samp>’ preceding the <span class="sc">gdb</span> program space number
|
|
indicates the current program space.
|
|
|
|
<p>In addition, below each program space line, <span class="sc">gdb</span> prints extra
|
|
information that isn't suitable to display in tabular form. For
|
|
example, the list of inferiors bound to the program space.
|
|
|
|
<pre class="smallexample"> (gdb) maint info program-spaces
|
|
Id Executable
|
|
* 1 hello
|
|
2 goodbye
|
|
Bound inferiors: ID 1 (process 21561)
|
|
</pre>
|
|
<p>Here we can see that no inferior is running the program <code>hello</code>,
|
|
while <code>process 21561</code> is running the program <code>goodbye</code>. On
|
|
some targets, it is possible that multiple inferiors are bound to the
|
|
same program space. The most common example is that of debugging both
|
|
the parent and child processes of a <code>vfork</code> call. For example,
|
|
|
|
<pre class="smallexample"> (gdb) maint info program-spaces
|
|
Id Executable
|
|
* 1 vfork-test
|
|
Bound inferiors: ID 2 (process 18050), ID 1 (process 18045)
|
|
</pre>
|
|
<p>Here, both inferior 2 and inferior 1 are running in the same program
|
|
space as a result of inferior 1 having executed a <code>vfork</code> call.
|
|
</dl>
|
|
|
|
</body></html>
|
|
|