hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2021 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="Machine-Code.html#Machine-Code" rel="previous" title="Machine Code">
<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>-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>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="Auto-Display.html#Auto-Display" accesskey="7">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="8">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" accesskey="9">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>