Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

423 lines
21KB

  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>Data (Debugging with GDB)</title>
  17. <meta name="description" content="Data (Debugging with GDB)">
  18. <meta name="keywords" content="Data (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="index.html#Top" rel="up" title="Top">
  26. <link href="Expressions.html#Expressions" rel="next" title="Expressions">
  27. <link href="Machine-Code.html#Machine-Code" rel="prev" title="Machine Code">
  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="Data"></a>
  58. <div class="header">
  59. <p>
  60. 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> &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="Examining-Data"></a>
  64. <h2 class="chapter">10 Examining Data</h2>
  65. <a name="index-printing-data"></a>
  66. <a name="index-examining-data"></a>
  67. <a name="index-print"></a>
  68. <a name="index-inspect"></a>
  69. <p>The usual way to examine data in your program is with the <code>print</code>
  70. command (abbreviated <code>p</code>), or its synonym <code>inspect</code>. It
  71. evaluates and prints the value of an expression of the language your
  72. program is written in (see <a href="Languages.html#Languages">Using <small>GDB</small> with
  73. Different Languages</a>). It may also print the expression using a
  74. Python-based pretty-printer (see <a href="Pretty-Printing.html#Pretty-Printing">Pretty Printing</a>).
  75. </p>
  76. <dl compact="compact">
  77. <dt><code>print [[<var>options</var>] --] <var>expr</var></code></dt>
  78. <dt><code>print [[<var>options</var>] --] /<var>f</var> <var>expr</var></code></dt>
  79. <dd><p><var>expr</var> is an expression (in the source language). By default the
  80. value of <var>expr</var> is printed in a format appropriate to its data type;
  81. you can choose a different format by specifying &lsquo;<samp>/<var>f</var></samp>&rsquo;, where
  82. <var>f</var> is a letter specifying the format; see <a href="Output-Formats.html#Output-Formats">Output
  83. Formats</a>.
  84. </p>
  85. <a name="print-options"></a><p>The <code>print</code> command supports a number of options that allow
  86. overriding relevant global print settings as set by <code>set print</code>
  87. subcommands:
  88. </p>
  89. <dl compact="compact">
  90. <dt><code>-address [<code>on</code>|<code>off</code>]</code></dt>
  91. <dd><p>Set printing of addresses.
  92. Related setting: <a href="Print-Settings.html#set-print-address">set print address</a>.
  93. </p>
  94. </dd>
  95. <dt><code>-array [<code>on</code>|<code>off</code>]</code></dt>
  96. <dd><p>Pretty formatting of arrays.
  97. Related setting: <a href="Print-Settings.html#set-print-array">set print array</a>.
  98. </p>
  99. </dd>
  100. <dt><code>-array-indexes [<code>on</code>|<code>off</code>]</code></dt>
  101. <dd><p>Set printing of array indexes.
  102. Related setting: <a href="Print-Settings.html#set-print-array_002dindexes">set print array-indexes</a>.
  103. </p>
  104. </dd>
  105. <dt><code>-elements <var>number-of-elements</var>|<code>unlimited</code></code></dt>
  106. <dd><p>Set limit on string chars or array elements to print. The value
  107. <code>unlimited</code> causes there to be no limit. Related setting:
  108. <a href="Print-Settings.html#set-print-elements">set print elements</a>.
  109. </p>
  110. </dd>
  111. <dt><code>-max-depth <var>depth</var>|<code>unlimited</code></code></dt>
  112. <dd><p>Set the threshold after which nested structures are replaced with
  113. ellipsis. Related setting: <a href="Print-Settings.html#set-print-max_002ddepth">set print max-depth</a>.
  114. </p>
  115. </dd>
  116. <dt><code>-null-stop [<code>on</code>|<code>off</code>]</code></dt>
  117. <dd><p>Set printing of char arrays to stop at first null char. Related
  118. setting: <a href="Print-Settings.html#set-print-null_002dstop">set print null-stop</a>.
  119. </p>
  120. </dd>
  121. <dt><code>-object [<code>on</code>|<code>off</code>]</code></dt>
  122. <dd><p>Set printing C<tt>++</tt> virtual function tables. Related setting:
  123. <a href="Print-Settings.html#set-print-object">set print object</a>.
  124. </p>
  125. </dd>
  126. <dt><code>-pretty [<code>on</code>|<code>off</code>]</code></dt>
  127. <dd><p>Set pretty formatting of structures. Related setting: <a href="Print-Settings.html#set-print-pretty">set print pretty</a>.
  128. </p>
  129. </dd>
  130. <dt><code>-raw-values [<code>on</code>|<code>off</code>]</code></dt>
  131. <dd><p>Set whether to print values in raw form, bypassing any
  132. pretty-printers for that value. Related setting: <a href="Print-Settings.html#set-print-raw_002dvalues">set print raw-values</a>.
  133. </p>
  134. </dd>
  135. <dt><code>-repeats <var>number-of-repeats</var>|<code>unlimited</code></code></dt>
  136. <dd><p>Set threshold for repeated print elements. <code>unlimited</code> causes
  137. all elements to be individually printed. Related setting: <a href="Print-Settings.html#set-print-repeats">set print repeats</a>.
  138. </p>
  139. </dd>
  140. <dt><code>-static-members [<code>on</code>|<code>off</code>]</code></dt>
  141. <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>.
  142. </p>
  143. </dd>
  144. <dt><code>-symbol [<code>on</code>|<code>off</code>]</code></dt>
  145. <dd><p>Set printing of symbol names when printing pointers. Related setting:
  146. <a href="Print-Settings.html#set-print-symbol">set print symbol</a>.
  147. </p>
  148. </dd>
  149. <dt><code>-union [<code>on</code>|<code>off</code>]</code></dt>
  150. <dd><p>Set printing of unions interior to structures. Related setting:
  151. <a href="Print-Settings.html#set-print-union">set print union</a>.
  152. </p>
  153. </dd>
  154. <dt><code>-vtbl [<code>on</code>|<code>off</code>]</code></dt>
  155. <dd><p>Set printing of C++ virtual function tables. Related setting:
  156. <a href="Print-Settings.html#set-print-vtbl">set print vtbl</a>.
  157. </p></dd>
  158. </dl>
  159. <p>Because the <code>print</code> command accepts arbitrary expressions which
  160. may look like options (including abbreviations), if you specify any
  161. command option, then you must use a double dash (<code>--</code>) to mark
  162. the end of option processing.
  163. </p>
  164. <p>For example, this prints the value of the <code>-p</code> expression:
  165. </p>
  166. <div class="smallexample">
  167. <pre class="smallexample">(gdb) print -p
  168. </pre></div>
  169. <p>While this repeats the last value in the value history (see below)
  170. with the <code>-pretty</code> option in effect:
  171. </p>
  172. <div class="smallexample">
  173. <pre class="smallexample">(gdb) print -p --
  174. </pre></div>
  175. <p>Here is an example including both on option and an expression:
  176. </p>
  177. <div class="smallexample">
  178. <pre class="smallexample">(gdb) print -pretty -- *myptr
  179. $1 = {
  180. next = 0x0,
  181. flags = {
  182. sweet = 1,
  183. sour = 1
  184. },
  185. meat = 0x54 &quot;Pork&quot;
  186. }
  187. </pre></div>
  188. </dd>
  189. <dt><code>print [<var>options</var>]</code></dt>
  190. <dt><code>print [<var>options</var>] /<var>f</var></code></dt>
  191. <dd><a name="index-reprint-the-last-value"></a>
  192. <p>If you omit <var>expr</var>, <small>GDB</small> displays the last value again (from the
  193. <em>value history</em>; see <a href="Value-History.html#Value-History">Value History</a>). This allows you to
  194. conveniently inspect the same value in an alternative format.
  195. </p></dd>
  196. </dl>
  197. <p>A more low-level way of examining data is with the <code>x</code> command.
  198. It examines data in memory at a specified address and prints it in a
  199. specified format. See <a href="Memory.html#Memory">Examining Memory</a>.
  200. </p>
  201. <p>If you are interested in information about types, or about how the
  202. fields of a struct or a class are declared, use the <code>ptype <var>exp</var></code>
  203. command rather than <code>print</code>. See <a href="Symbols.html#Symbols">Examining the Symbol
  204. Table</a>.
  205. </p>
  206. <a name="index-exploring-hierarchical-data-structures"></a>
  207. <a name="index-explore"></a>
  208. <p>Another way of examining values of expressions and type information is
  209. through the Python extension command <code>explore</code> (available only if
  210. the <small>GDB</small> build is configured with <code>--with-python</code>). It
  211. offers an interactive way to start at the highest level (or, the most
  212. abstract level) of the data type of an expression (or, the data type
  213. itself) and explore all the way down to leaf scalar values/fields
  214. embedded in the higher level data types.
  215. </p>
  216. <dl compact="compact">
  217. <dt><code>explore <var>arg</var></code></dt>
  218. <dd><p><var>arg</var> is either an expression (in the source language), or a type
  219. visible in the current context of the program being debugged.
  220. </p></dd>
  221. </dl>
  222. <p>The working of the <code>explore</code> command can be illustrated with an
  223. example. If a data type <code>struct ComplexStruct</code> is defined in your
  224. C program as
  225. </p>
  226. <div class="smallexample">
  227. <pre class="smallexample">struct SimpleStruct
  228. {
  229. int i;
  230. double d;
  231. };
  232. struct ComplexStruct
  233. {
  234. struct SimpleStruct *ss_p;
  235. int arr[10];
  236. };
  237. </pre></div>
  238. <p>followed by variable declarations as
  239. </p>
  240. <div class="smallexample">
  241. <pre class="smallexample">struct SimpleStruct ss = { 10, 1.11 };
  242. struct ComplexStruct cs = { &amp;ss, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
  243. </pre></div>
  244. <p>then, the value of the variable <code>cs</code> can be explored using the
  245. <code>explore</code> command as follows.
  246. </p>
  247. <div class="smallexample">
  248. <pre class="smallexample">(gdb) explore cs
  249. The value of `cs' is a struct/class of type `struct ComplexStruct' with
  250. the following fields:
  251. ss_p = &lt;Enter 0 to explore this field of type `struct SimpleStruct *'&gt;
  252. arr = &lt;Enter 1 to explore this field of type `int [10]'&gt;
  253. Enter the field number of choice:
  254. </pre></div>
  255. <p>Since the fields of <code>cs</code> are not scalar values, you are being
  256. prompted to chose the field you want to explore. Let&rsquo;s say you choose
  257. the field <code>ss_p</code> by entering <code>0</code>. Then, since this field is a
  258. pointer, you will be asked if it is pointing to a single value. From
  259. the declaration of <code>cs</code> above, it is indeed pointing to a single
  260. value, hence you enter <code>y</code>. If you enter <code>n</code>, then you will
  261. be asked if it were pointing to an array of values, in which case this
  262. field will be explored as if it were an array.
  263. </p>
  264. <div class="smallexample">
  265. <pre class="smallexample">`cs.ss_p' is a pointer to a value of type `struct SimpleStruct'
  266. Continue exploring it as a pointer to a single value [y/n]: y
  267. The value of `*(cs.ss_p)' is a struct/class of type `struct
  268. SimpleStruct' with the following fields:
  269. i = 10 .. (Value of type `int')
  270. d = 1.1100000000000001 .. (Value of type `double')
  271. Press enter to return to parent value:
  272. </pre></div>
  273. <p>If the field <code>arr</code> of <code>cs</code> was chosen for exploration by
  274. entering <code>1</code> earlier, then since it is as array, you will be
  275. prompted to enter the index of the element in the array that you want
  276. to explore.
  277. </p>
  278. <div class="smallexample">
  279. <pre class="smallexample">`cs.arr' is an array of `int'.
  280. Enter the index of the element you want to explore in `cs.arr': 5
  281. `(cs.arr)[5]' is a scalar value of type `int'.
  282. (cs.arr)[5] = 4
  283. Press enter to return to parent value:
  284. </pre></div>
  285. <p>In general, at any stage of exploration, you can go deeper towards the
  286. leaf values by responding to the prompts appropriately, or hit the
  287. return key to return to the enclosing data structure (the <i>higher</i>
  288. level data structure).
  289. </p>
  290. <p>Similar to exploring values, you can use the <code>explore</code> command to
  291. explore types. Instead of specifying a value (which is typically a
  292. variable name or an expression valid in the current context of the
  293. program being debugged), you specify a type name. If you consider the
  294. same example as above, your can explore the type
  295. <code>struct ComplexStruct</code> by passing the argument
  296. <code>struct ComplexStruct</code> to the <code>explore</code> command.
  297. </p>
  298. <div class="smallexample">
  299. <pre class="smallexample">(gdb) explore struct ComplexStruct
  300. </pre></div>
  301. <p>By responding to the prompts appropriately in the subsequent interactive
  302. session, you can explore the type <code>struct ComplexStruct</code> in a
  303. manner similar to how the value <code>cs</code> was explored in the above
  304. example.
  305. </p>
  306. <p>The <code>explore</code> command also has two sub-commands,
  307. <code>explore value</code> and <code>explore type</code>. The former sub-command is
  308. a way to explicitly specify that value exploration of the argument is
  309. being invoked, while the latter is a way to explicitly specify that type
  310. exploration of the argument is being invoked.
  311. </p>
  312. <dl compact="compact">
  313. <dt><code>explore value <var>expr</var></code></dt>
  314. <dd><a name="index-explore-value"></a>
  315. <p>This sub-command of <code>explore</code> explores the value of the
  316. expression <var>expr</var> (if <var>expr</var> is an expression valid in the
  317. current context of the program being debugged). The behavior of this
  318. command is identical to that of the behavior of the <code>explore</code>
  319. command being passed the argument <var>expr</var>.
  320. </p>
  321. </dd>
  322. <dt><code>explore type <var>arg</var></code></dt>
  323. <dd><a name="index-explore-type"></a>
  324. <p>This sub-command of <code>explore</code> explores the type of <var>arg</var> (if
  325. <var>arg</var> is a type visible in the current context of program being
  326. debugged), or the type of the value/expression <var>arg</var> (if <var>arg</var>
  327. is an expression valid in the current context of the program being
  328. debugged). If <var>arg</var> is a type, then the behavior of this command is
  329. identical to that of the <code>explore</code> command being passed the
  330. argument <var>arg</var>. If <var>arg</var> is an expression, then the behavior of
  331. this command will be identical to that of the <code>explore</code> command
  332. being passed the type of <var>arg</var> as the argument.
  333. </p></dd>
  334. </dl>
  335. <table class="menu" border="0" cellspacing="0">
  336. <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
  337. </td></tr>
  338. <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
  339. </td></tr>
  340. <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
  341. </td></tr>
  342. <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
  343. </td></tr>
  344. <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
  345. </td></tr>
  346. <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
  347. </td></tr>
  348. <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
  349. </td></tr>
  350. <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
  351. </td></tr>
  352. <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
  353. </td></tr>
  354. <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
  355. </td></tr>
  356. <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
  357. </td></tr>
  358. <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
  359. </td></tr>
  360. <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
  361. </td></tr>
  362. <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
  363. </td></tr>
  364. <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
  365. </td></tr>
  366. <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
  367. </td></tr>
  368. <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
  369. </td></tr>
  370. <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
  371. </td></tr>
  372. <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
  373. </td></tr>
  374. <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
  375. character set than GDB does
  376. </td></tr>
  377. <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
  378. </td></tr>
  379. <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
  380. </td></tr>
  381. <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
  382. </td></tr>
  383. </table>
  384. <hr>
  385. <div class="header">
  386. <p>
  387. 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> &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>
  388. </div>
  389. </body>
  390. </html>