2025-04-27 10:37:43 +08:00

435 lines
21 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2022 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." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Data</title>
<meta name="description" content="Debugging with GDB: Data">
<meta name="keywords" content="Debugging with GDB: Data">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Expressions.html#Expressions" rel="next" title="Expressions">
<link href="Disable-Reading-Source.html#Disable-Reading-Source" rel="previous" title="Disable Reading Source">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Data"></a>
<div class="header">
<p>
Next: <a href="Optimized-Code.html#Optimized-Code" accesskey="n" rel="next">Optimized Code</a>, Previous: <a href="Source.html#Source" accesskey="p" rel="previous">Source</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
</div>
<hr>
<a name="Examining-Data"></a>
<h2 class="chapter">10 Examining Data</h2>
<a name="index-printing-data"></a>
<a name="index-examining-data"></a>
<a name="index-print"></a>
<a name="index-inspect"></a>
<p>The usual way to examine data in your program is with the <code>print</code>
command (abbreviated <code>p</code>), or its synonym <code>inspect</code>. It
evaluates and prints the value of an expression of the language your
program is written in (see <a href="Languages.html#Languages">Using <small>GDB</small> with
Different Languages</a>). It may also print the expression using a
Python-based pretty-printer (see <a href="Pretty-Printing.html#Pretty-Printing">Pretty Printing</a>).
</p>
<dl compact="compact">
<dt><code>print [[<var>options</var>] --] <var>expr</var></code></dt>
<dt><code>print [[<var>options</var>] --] /<var>f</var> <var>expr</var></code></dt>
<dd><p><var>expr</var> is an expression (in the source language). By default the
value of <var>expr</var> is printed in a format appropriate to its data type;
you can choose a different format by specifying &lsquo;<samp>/<var>f</var></samp>&rsquo;, where
<var>f</var> is a letter specifying the format; see <a href="Output-Formats.html#Output-Formats">Output
Formats</a>.
</p>
<a name="print-options"></a><p>The <code>print</code> command supports a number of options that allow
overriding relevant global print settings as set by <code>set print</code>
subcommands:
</p>
<dl compact="compact">
<dt><code>-address [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of addresses.
Related setting: <a href="Print-Settings.html#set-print-address">set print address</a>.
</p>
</dd>
<dt><code>-array [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Pretty formatting of arrays.
Related setting: <a href="Print-Settings.html#set-print-array">set print array</a>.
</p>
</dd>
<dt><code>-array-indexes [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of array indexes.
Related setting: <a href="Print-Settings.html#set-print-array_002dindexes">set print array-indexes</a>.
</p>
</dd>
<dt><code>-elements <var>number-of-elements</var>|<code>unlimited</code></code></dt>
<dd><p>Set limit on string chars or array elements to print. The value
<code>unlimited</code> causes there to be no limit. Related setting:
<a href="Print-Settings.html#set-print-elements">set print elements</a>.
</p>
</dd>
<dt><code>-max-depth <var>depth</var>|<code>unlimited</code></code></dt>
<dd><p>Set the threshold after which nested structures are replaced with
ellipsis. Related setting: <a href="Print-Settings.html#set-print-max_002ddepth">set print max-depth</a>.
</p>
</dd>
<dt><code>-memory-tag-violations [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of additional information about memory tag violations.
See <a href="Print-Settings.html#set-print-memory_002dtag_002dviolations">set print memory-tag-violations</a>.
</p>
</dd>
<dt><code>-null-stop [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of char arrays to stop at first null char. Related
setting: <a href="Print-Settings.html#set-print-null_002dstop">set print null-stop</a>.
</p>
</dd>
<dt><code>-object [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing C<tt>++</tt> virtual function tables. Related setting:
<a href="Print-Settings.html#set-print-object">set print object</a>.
</p>
</dd>
<dt><code>-pretty [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set pretty formatting of structures. Related setting: <a href="Print-Settings.html#set-print-pretty">set print pretty</a>.
</p>
</dd>
<dt><code>-raw-values [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set whether to print values in raw form, bypassing any
pretty-printers for that value. Related setting: <a href="Print-Settings.html#set-print-raw_002dvalues">set print raw-values</a>.
</p>
</dd>
<dt><code>-repeats <var>number-of-repeats</var>|<code>unlimited</code></code></dt>
<dd><p>Set threshold for repeated print elements. <code>unlimited</code> causes
all elements to be individually printed. Related setting: <a href="Print-Settings.html#set-print-repeats">set print repeats</a>.
</p>
</dd>
<dt><code>-static-members [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing C<tt>++</tt> static members. Related setting: <a href="Print-Settings.html#set-print-static_002dmembers">set print static-members</a>.
</p>
</dd>
<dt><code>-symbol [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of symbol names when printing pointers. Related setting:
<a href="Print-Settings.html#set-print-symbol">set print symbol</a>.
</p>
</dd>
<dt><code>-union [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of unions interior to structures. Related setting:
<a href="Print-Settings.html#set-print-union">set print union</a>.
</p>
</dd>
<dt><code>-vtbl [<code>on</code>|<code>off</code>]</code></dt>
<dd><p>Set printing of C++ virtual function tables. Related setting:
<a href="Print-Settings.html#set-print-vtbl">set print vtbl</a>.
</p></dd>
</dl>
<p>Because the <code>print</code> command accepts arbitrary expressions which
may look like options (including abbreviations), if you specify any
command option, then you must use a double dash (<code>--</code>) to mark
the end of option processing.
</p>
<p>For example, this prints the value of the <code>-p</code> expression:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print -p
</pre></div>
<p>While this repeats the last value in the value history (see below)
with the <code>-pretty</code> option in effect:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print -p --
</pre></div>
<p>Here is an example including both on option and an expression:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print -pretty -- *myptr
$1 = {
next = 0x0,
flags = {
sweet = 1,
sour = 1
},
meat = 0x54 &quot;Pork&quot;
}
</pre></div>
</dd>
<dt><code>print [<var>options</var>]</code></dt>
<dt><code>print [<var>options</var>] /<var>f</var></code></dt>
<dd><a name="index-reprint-the-last-value"></a>
<p>If you omit <var>expr</var>, <small>GDB</small> displays the last value again (from the
<em>value history</em>; see <a href="Value-History.html#Value-History">Value History</a>). This allows you to
conveniently inspect the same value in an alternative format.
</p></dd>
</dl>
<p>If the architecture supports memory tagging, the <code>print</code> command will
display pointer/memory tag mismatches if what is being printed is a pointer
or reference type. See <a href="Memory-Tagging.html#Memory-Tagging">Memory Tagging</a>.
</p>
<p>A more low-level way of examining data is with the <code>x</code> command.
It examines data in memory at a specified address and prints it in a
specified format. See <a href="Memory.html#Memory">Examining Memory</a>.
</p>
<p>If you are interested in information about types, or about how the
fields of a struct or a class are declared, use the <code>ptype <var>exp</var></code>
command rather than <code>print</code>. See <a href="Symbols.html#Symbols">Examining the Symbol
Table</a>.
</p>
<a name="index-exploring-hierarchical-data-structures"></a>
<a name="index-explore"></a>
<p>Another way of examining values of expressions and type information is
through the Python extension command <code>explore</code> (available only if
the <small>GDB</small> build is configured with <code>--with-python</code>). It
offers an interactive way to start at the highest level (or, the most
abstract level) of the data type of an expression (or, the data type
itself) and explore all the way down to leaf scalar values/fields
embedded in the higher level data types.
</p>
<dl compact="compact">
<dt><code>explore <var>arg</var></code></dt>
<dd><p><var>arg</var> is either an expression (in the source language), or a type
visible in the current context of the program being debugged.
</p></dd>
</dl>
<p>The working of the <code>explore</code> command can be illustrated with an
example. If a data type <code>struct ComplexStruct</code> is defined in your
C program as
</p>
<div class="smallexample">
<pre class="smallexample">struct SimpleStruct
{
int i;
double d;
};
struct ComplexStruct
{
struct SimpleStruct *ss_p;
int arr[10];
};
</pre></div>
<p>followed by variable declarations as
</p>
<div class="smallexample">
<pre class="smallexample">struct SimpleStruct ss = { 10, 1.11 };
struct ComplexStruct cs = { &amp;ss, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
</pre></div>
<p>then, the value of the variable <code>cs</code> can be explored using the
<code>explore</code> command as follows.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) explore cs
The value of `cs' is a struct/class of type `struct ComplexStruct' with
the following fields:
ss_p = &lt;Enter 0 to explore this field of type `struct SimpleStruct *'&gt;
arr = &lt;Enter 1 to explore this field of type `int [10]'&gt;
Enter the field number of choice:
</pre></div>
<p>Since the fields of <code>cs</code> are not scalar values, you are being
prompted to chose the field you want to explore. Let&rsquo;s say you choose
the field <code>ss_p</code> by entering <code>0</code>. Then, since this field is a
pointer, you will be asked if it is pointing to a single value. From
the declaration of <code>cs</code> above, it is indeed pointing to a single
value, hence you enter <code>y</code>. If you enter <code>n</code>, then you will
be asked if it were pointing to an array of values, in which case this
field will be explored as if it were an array.
</p>
<div class="smallexample">
<pre class="smallexample">`cs.ss_p' is a pointer to a value of type `struct SimpleStruct'
Continue exploring it as a pointer to a single value [y/n]: y
The value of `*(cs.ss_p)' is a struct/class of type `struct
SimpleStruct' with the following fields:
i = 10 .. (Value of type `int')
d = 1.1100000000000001 .. (Value of type `double')
Press enter to return to parent value:
</pre></div>
<p>If the field <code>arr</code> of <code>cs</code> was chosen for exploration by
entering <code>1</code> earlier, then since it is as array, you will be
prompted to enter the index of the element in the array that you want
to explore.
</p>
<div class="smallexample">
<pre class="smallexample">`cs.arr' is an array of `int'.
Enter the index of the element you want to explore in `cs.arr': 5
`(cs.arr)[5]' is a scalar value of type `int'.
(cs.arr)[5] = 4
Press enter to return to parent value:
</pre></div>
<p>In general, at any stage of exploration, you can go deeper towards the
leaf values by responding to the prompts appropriately, or hit the
return key to return to the enclosing data structure (the <i>higher</i>
level data structure).
</p>
<p>Similar to exploring values, you can use the <code>explore</code> command to
explore types. Instead of specifying a value (which is typically a
variable name or an expression valid in the current context of the
program being debugged), you specify a type name. If you consider the
same example as above, your can explore the type
<code>struct ComplexStruct</code> by passing the argument
<code>struct ComplexStruct</code> to the <code>explore</code> command.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) explore struct ComplexStruct
</pre></div>
<p>By responding to the prompts appropriately in the subsequent interactive
session, you can explore the type <code>struct ComplexStruct</code> in a
manner similar to how the value <code>cs</code> was explored in the above
example.
</p>
<p>The <code>explore</code> command also has two sub-commands,
<code>explore value</code> and <code>explore type</code>. The former sub-command is
a way to explicitly specify that value exploration of the argument is
being invoked, while the latter is a way to explicitly specify that type
exploration of the argument is being invoked.
</p>
<dl compact="compact">
<dt><code>explore value <var>expr</var></code></dt>
<dd><a name="index-explore-value"></a>
<p>This sub-command of <code>explore</code> explores the value of the
expression <var>expr</var> (if <var>expr</var> is an expression valid in the
current context of the program being debugged). The behavior of this
command is identical to that of the behavior of the <code>explore</code>
command being passed the argument <var>expr</var>.
</p>
</dd>
<dt><code>explore type <var>arg</var></code></dt>
<dd><a name="index-explore-type"></a>
<p>This sub-command of <code>explore</code> explores the type of <var>arg</var> (if
<var>arg</var> is a type visible in the current context of program being
debugged), or the type of the value/expression <var>arg</var> (if <var>arg</var>
is an expression valid in the current context of the program being
debugged). If <var>arg</var> is a type, then the behavior of this command is
identical to that of the <code>explore</code> command being passed the
argument <var>arg</var>. If <var>arg</var> is an expression, then the behavior of
this command will be identical to that of the <code>explore</code> command
being passed the type of <var>arg</var> as the argument.
</p></dd>
</dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="Expressions.html#Expressions" accesskey="1">Expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Expressions
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Ambiguous-Expressions.html#Ambiguous-Expressions" accesskey="2">Ambiguous Expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Ambiguous Expressions
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Variables.html#Variables" accesskey="3">Variables</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Program variables
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Arrays.html#Arrays" accesskey="4">Arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Artificial arrays
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Output-Formats.html#Output-Formats" accesskey="5">Output Formats</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Output formats
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Memory.html#Memory" accesskey="6">Memory</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Examining memory
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Memory-Tagging.html#Memory-Tagging" accesskey="7">Memory Tagging</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Memory Tagging
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Auto-Display.html#Auto-Display" accesskey="8">Auto Display</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Automatic display
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Print-Settings.html#Print-Settings" accesskey="9">Print Settings</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Print settings
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Pretty-Printing.html#Pretty-Printing">Pretty Printing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Python pretty printing
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Value-History.html#Value-History">Value History</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Value history
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Convenience variables
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Convenience-Funs.html#Convenience-Funs">Convenience Funs</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Convenience functions
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Registers.html#Registers">Registers</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Registers
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Floating-Point-Hardware.html#Floating-Point-Hardware">Floating Point Hardware</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Floating point hardware
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Vector-Unit.html#Vector-Unit">Vector Unit</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Vector Unit
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="OS-Information.html#OS-Information">OS Information</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Auxiliary data provided by operating system
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Memory-Region-Attributes.html#Memory-Region-Attributes">Memory Region Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Memory region attributes
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Dump_002fRestore-Files.html#Dump_002fRestore-Files">Dump/Restore Files</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Copy between memory and a file
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Core-File-Generation.html#Core-File-Generation">Core File Generation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Cause a program dump its core
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Character-Sets.html#Character-Sets">Character Sets</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Debugging programs that use a different
character set than GDB does
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Caching-Target-Data.html#Caching-Target-Data">Caching Target Data</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Data caching for targets
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Searching-Memory.html#Searching-Memory">Searching Memory</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Searching memory for a sequence of bytes
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Value-Sizes.html#Value-Sizes">Value Sizes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Managing memory allocated for values
</td></tr>
</table>
<hr>
<div class="header">
<p>
Next: <a href="Optimized-Code.html#Optimized-Code" accesskey="n" rel="next">Optimized Code</a>, Previous: <a href="Source.html#Source" accesskey="p" rel="previous">Source</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
</div>
</body>
</html>