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