| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <html> |
| <!-- Copyright (C) 1988-2015 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.2, 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="prev" 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="prev">Source</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<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>expr</var></code></dt> |
| <dt><code>print /<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 ‘<samp>/<var>f</var></samp>’, where |
| <var>f</var> is a letter specifying the format; see <a href="Output-Formats.html#Output-Formats">Output |
| Formats</a>. |
| </p> |
| </dd> |
| <dt><code>print</code></dt> |
| <dt><code>print /<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 = { &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 = <Enter 0 to explore this field of type `struct SimpleStruct *'> |
| arr = <Enter 1 to explore this field of type `int [10]'> |
| |
| 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’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">• <a href="Expressions.html#Expressions" accesskey="1">Expressions</a>:</td><td> </td><td align="left" valign="top">Expressions |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Ambiguous-Expressions.html#Ambiguous-Expressions" accesskey="2">Ambiguous Expressions</a>:</td><td> </td><td align="left" valign="top">Ambiguous Expressions |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Variables.html#Variables" accesskey="3">Variables</a>:</td><td> </td><td align="left" valign="top">Program variables |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Arrays.html#Arrays" accesskey="4">Arrays</a>:</td><td> </td><td align="left" valign="top">Artificial arrays |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Output-Formats.html#Output-Formats" accesskey="5">Output Formats</a>:</td><td> </td><td align="left" valign="top">Output formats |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Memory.html#Memory" accesskey="6">Memory</a>:</td><td> </td><td align="left" valign="top">Examining memory |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Auto-Display.html#Auto-Display" accesskey="7">Auto Display</a>:</td><td> </td><td align="left" valign="top">Automatic display |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Print-Settings.html#Print-Settings" accesskey="8">Print Settings</a>:</td><td> </td><td align="left" valign="top">Print settings |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Pretty-Printing.html#Pretty-Printing" accesskey="9">Pretty Printing</a>:</td><td> </td><td align="left" valign="top">Python pretty printing |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Value-History.html#Value-History">Value History</a>:</td><td> </td><td align="left" valign="top">Value history |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>:</td><td> </td><td align="left" valign="top">Convenience variables |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Convenience-Funs.html#Convenience-Funs">Convenience Funs</a>:</td><td> </td><td align="left" valign="top">Convenience functions |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Registers.html#Registers">Registers</a>:</td><td> </td><td align="left" valign="top">Registers |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Floating-Point-Hardware.html#Floating-Point-Hardware">Floating Point Hardware</a>:</td><td> </td><td align="left" valign="top">Floating point hardware |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Vector-Unit.html#Vector-Unit">Vector Unit</a>:</td><td> </td><td align="left" valign="top">Vector Unit |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="OS-Information.html#OS-Information">OS Information</a>:</td><td> </td><td align="left" valign="top">Auxiliary data provided by operating system |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Memory-Region-Attributes.html#Memory-Region-Attributes">Memory Region Attributes</a>:</td><td> </td><td align="left" valign="top">Memory region attributes |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Dump_002fRestore-Files.html#Dump_002fRestore-Files">Dump/Restore Files</a>:</td><td> </td><td align="left" valign="top">Copy between memory and a file |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Core-File-Generation.html#Core-File-Generation">Core File Generation</a>:</td><td> </td><td align="left" valign="top">Cause a program dump its core |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Character-Sets.html#Character-Sets">Character Sets</a>:</td><td> </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">• <a href="Caching-Target-Data.html#Caching-Target-Data">Caching Target Data</a>:</td><td> </td><td align="left" valign="top">Data caching for targets |
| </td></tr> |
| <tr><td align="left" valign="top">• <a href="Searching-Memory.html#Searching-Memory">Searching Memory</a>:</td><td> </td><td align="left" valign="top">Searching memory for a sequence of bytes |
| </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="prev">Source</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<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> |