Variables.html 12 KB

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