You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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>Variables (Debugging with GDB)</title>
  17. <meta name="description" content="Variables (Debugging with GDB)">
  18. <meta name="keywords" content="Variables (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="Data.html#Data" rel="up" title="Data">
  26. <link href="Arrays.html#Arrays" rel="next" title="Arrays">
  27. <link href="Ambiguous-Expressions.html#Ambiguous-Expressions" rel="prev" title="Ambiguous Expressions">
  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="Variables"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Arrays.html#Arrays" accesskey="n" rel="next">Arrays</a>, Previous: <a href="Ambiguous-Expressions.html#Ambiguous-Expressions" accesskey="p" rel="prev">Ambiguous Expressions</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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="Program-Variables"></a>
  64. <h3 class="section">10.3 Program Variables</h3>
  65. <p>The most common kind of expression to use is the name of a variable
  66. in your program.
  67. </p>
  68. <p>Variables in expressions are understood in the selected stack frame
  69. (see <a href="Selection.html#Selection">Selecting a Frame</a>); they must be either:
  70. </p>
  71. <ul>
  72. <li> global (or file-static)
  73. </li></ul>
  74. <p>or
  75. </p>
  76. <ul>
  77. <li> visible according to the scope rules of the
  78. programming language from the point of execution in that frame
  79. </li></ul>
  80. <p>This means that in the function
  81. </p>
  82. <div class="smallexample">
  83. <pre class="smallexample">foo (a)
  84. int a;
  85. {
  86. bar (a);
  87. {
  88. int b = test ();
  89. bar (b);
  90. }
  91. }
  92. </pre></div>
  93. <p>you can examine and use the variable <code>a</code> whenever your program is
  94. executing within the function <code>foo</code>, but you can only use or
  95. examine the variable <code>b</code> while your program is executing inside
  96. the block where <code>b</code> is declared.
  97. </p>
  98. <a name="index-variable-name-conflict"></a>
  99. <p>There is an exception: you can refer to a variable or function whose
  100. scope is a single source file even if the current execution point is not
  101. in this file. But it is possible to have more than one such variable or
  102. function with the same name (in different source files). If that
  103. happens, referring to that name has unpredictable effects. If you wish,
  104. you can specify a static variable in a particular function or file by
  105. using the colon-colon (<code>::</code>) notation:
  106. </p>
  107. <a name="index-colon_002dcolon_002c-context-for-variables_002ffunctions"></a>
  108. <a name="index-_003a_003a_002c-context-for-variables_002ffunctions"></a>
  109. <div class="smallexample">
  110. <pre class="smallexample"><var>file</var>::<var>variable</var>
  111. <var>function</var>::<var>variable</var>
  112. </pre></div>
  113. <p>Here <var>file</var> or <var>function</var> is the name of the context for the
  114. static <var>variable</var>. In the case of file names, you can use quotes to
  115. make sure <small>GDB</small> parses the file name as a single word&mdash;for example,
  116. to print a global value of <code>x</code> defined in <samp>f2.c</samp>:
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample">(gdb) p 'f2.c'::x
  120. </pre></div>
  121. <p>The <code>::</code> notation is normally used for referring to
  122. static variables, since you typically disambiguate uses of local variables
  123. in functions by selecting the appropriate frame and using the
  124. simple name of the variable. However, you may also use this notation
  125. to refer to local variables in frames enclosing the selected frame:
  126. </p>
  127. <div class="smallexample">
  128. <pre class="smallexample">void
  129. foo (int a)
  130. {
  131. if (a &lt; 10)
  132. bar (a);
  133. else
  134. process (a); /* Stop here */
  135. }
  136. int
  137. bar (int a)
  138. {
  139. foo (a + 5);
  140. }
  141. </pre></div>
  142. <p>For example, if there is a breakpoint at the commented line,
  143. here is what you might see
  144. when the program stops after executing the call <code>bar(0)</code>:
  145. </p>
  146. <div class="smallexample">
  147. <pre class="smallexample">(gdb) p a
  148. $1 = 10
  149. (gdb) p bar::a
  150. $2 = 5
  151. (gdb) up 2
  152. #2 0x080483d0 in foo (a=5) at foobar.c:12
  153. (gdb) p a
  154. $3 = 5
  155. (gdb) p bar::a
  156. $4 = 0
  157. </pre></div>
  158. <a name="index-C_002b_002b-scope-resolution"></a>
  159. <p>These uses of &lsquo;<samp>::</samp>&rsquo; are very rarely in conflict with the very
  160. similar use of the same notation in C<tt>++</tt>. When they are in
  161. conflict, the C<tt>++</tt> meaning takes precedence; however, this can be
  162. overridden by quoting the file or function name with single quotes.
  163. </p>
  164. <p>For example, suppose the program is stopped in a method of a class
  165. that has a field named <code>includefile</code>, and there is also an
  166. include file named <samp>includefile</samp> that defines a variable,
  167. <code>some_global</code>.
  168. </p>
  169. <div class="smallexample">
  170. <pre class="smallexample">(gdb) p includefile
  171. $1 = 23
  172. (gdb) p includefile::some_global
  173. A syntax error in expression, near `'.
  174. (gdb) p 'includefile'::some_global
  175. $2 = 27
  176. </pre></div>
  177. <a name="index-wrong-values"></a>
  178. <a name="index-variable-values_002c-wrong"></a>
  179. <a name="index-function-entry_002fexit_002c-wrong-values-of-variables"></a>
  180. <a name="index-optimized-code_002c-wrong-values-of-variables"></a>
  181. <blockquote>
  182. <p><em>Warning:</em> Occasionally, a local variable may appear to have the
  183. wrong value at certain points in a function&mdash;just after entry to a new
  184. scope, and just before exit.
  185. </p></blockquote>
  186. <p>You may see this problem when you are stepping by machine instructions.
  187. This is because, on most machines, it takes more than one instruction to
  188. set up a stack frame (including local variable definitions); if you are
  189. stepping by machine instructions, variables may appear to have the wrong
  190. values until the stack frame is completely built. On exit, it usually
  191. also takes more than one machine instruction to destroy a stack frame;
  192. after you begin stepping through that group of instructions, local
  193. variable definitions may be gone.
  194. </p>
  195. <p>This may also happen when the compiler does significant optimizations.
  196. To be sure of always seeing accurate values, turn off all optimization
  197. when compiling.
  198. </p>
  199. <a name="index-_0060_0060No-symbol-_0022foo_0022-in-current-context_0027_0027"></a>
  200. <p>Another possible effect of compiler optimizations is to optimize
  201. unused variables out of existence, or assign variables to registers (as
  202. opposed to memory addresses). Depending on the support for such cases
  203. offered by the debug info format used by the compiler, <small>GDB</small>
  204. might not be able to display values for such local variables. If that
  205. happens, <small>GDB</small> will print a message like this:
  206. </p>
  207. <div class="smallexample">
  208. <pre class="smallexample">No symbol &quot;foo&quot; in current context.
  209. </pre></div>
  210. <p>To solve such problems, either recompile without optimizations, or use a
  211. different debug info format, if the compiler supports several such
  212. formats. See <a href="Compilation.html#Compilation">Compilation</a>, for more information on choosing compiler
  213. options. See <a href="C.html#C">C and C<tt>++</tt></a>, for more information about debug
  214. info formats that are best suited to C<tt>++</tt> programs.
  215. </p>
  216. <p>If you ask to print an object whose contents are unknown to
  217. <small>GDB</small>, e.g., because its data type is not completely specified
  218. by the debug information, <small>GDB</small> will say &lsquo;<samp>&lt;incomplete
  219. type&gt;</samp>&rsquo;. See <a href="Symbols.html#Symbols">incomplete type</a>, for more about this.
  220. </p>
  221. <a name="index-no-debug-info-variables"></a>
  222. <p>If you try to examine or use the value of a (global) variable for
  223. which <small>GDB</small> has no type information, e.g., because the program
  224. includes no debug information, <small>GDB</small> displays an error message.
  225. See <a href="Symbols.html#Symbols">unknown type</a>, for more about unknown types. If you
  226. cast the variable to its declared type, <small>GDB</small> gets the
  227. variable&rsquo;s value using the cast-to type as the variable&rsquo;s type. For
  228. example, in a C program:
  229. </p>
  230. <div class="smallexample">
  231. <pre class="smallexample"> (gdb) p var
  232. 'var' has unknown type; cast it to its declared type
  233. (gdb) p (float) var
  234. $1 = 3.14
  235. </pre></div>
  236. <p>If you append <kbd>@entry</kbd> string to a function parameter name you get its
  237. value at the time the function got called. If the value is not available an
  238. error message is printed. Entry values are available only with some compilers.
  239. Entry values are normally also printed at the function parameter list according
  240. to <a href="Print-Settings.html#set-print-entry_002dvalues">set print entry-values</a>.
  241. </p>
  242. <div class="smallexample">
  243. <pre class="smallexample">Breakpoint 1, d (i=30) at gdb.base/entry-value.c:29
  244. 29 i++;
  245. (gdb) next
  246. 30 e (i);
  247. (gdb) print i
  248. $1 = 31
  249. (gdb) print i@entry
  250. $2 = 30
  251. </pre></div>
  252. <p>Strings are identified as arrays of <code>char</code> values without specified
  253. signedness. Arrays of either <code>signed char</code> or <code>unsigned char</code> get
  254. printed as arrays of 1 byte sized integers. <code>-fsigned-char</code> or
  255. <code>-funsigned-char</code> <small>GCC</small> options have no effect as <small>GDB</small>
  256. defines literal string type <code>&quot;char&quot;</code> as <code>char</code> without a sign.
  257. For program code
  258. </p>
  259. <div class="smallexample">
  260. <pre class="smallexample">char var0[] = &quot;A&quot;;
  261. signed char var1[] = &quot;A&quot;;
  262. </pre></div>
  263. <p>You get during debugging
  264. </p><div class="smallexample">
  265. <pre class="smallexample">(gdb) print var0
  266. $1 = &quot;A&quot;
  267. (gdb) print var1
  268. $2 = {65 'A', 0 '\0'}
  269. </pre></div>
  270. <hr>
  271. <div class="header">
  272. <p>
  273. Next: <a href="Arrays.html#Arrays" accesskey="n" rel="next">Arrays</a>, Previous: <a href="Ambiguous-Expressions.html#Ambiguous-Expressions" accesskey="p" rel="prev">Ambiguous Expressions</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</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>
  274. </div>
  275. </body>
  276. </html>