3 <title>Debugging with GDB
</title>
4 <meta http-equiv=
"Content-Type" content=
"text/html">
5 <meta name=
"description" content=
"Debugging with GDB">
6 <meta name=
"generator" content=
"makeinfo 4.3">
7 <link href=
"http://www.gnu.org/software/texinfo/" rel=
"generator-home">
12 Node:
<a name=
"Variables">Variables
</a>,
13 Next:
<a rel=
"next" accesskey=
"n" href=
"Arrays.html#Arrays">Arrays
</a>,
14 Previous:
<a rel=
"previous" accesskey=
"p" href=
"Expressions.html#Expressions">Expressions
</a>,
15 Up:
<a rel=
"up" accesskey=
"u" href=
"Data.html#Data">Data
</a>
19 <h3 class=
"section">Program variables
</h3>
21 <p>The most common kind of expression to use is the name of a variable
24 <p>Variables in expressions are understood in the selected stack frame
25 (see
<a href=
"Selection.html#Selection">Selecting a frame
</a>); they must be either:
28 <li>global (or file-static)
34 <li>visible according to the scope rules of the
35 programming language from the point of execution in that frame
38 <p>This means that in the function
40 <pre class=
"example"> foo (a)
51 <p>you can examine and use the variable
<code>a
</code> whenever your program is
52 executing within the function
<code>foo
</code>, but you can only use or
53 examine the variable
<code>b
</code> while your program is executing inside
54 the block where
<code>b
</code> is declared.
56 <p>There is an exception: you can refer to a variable or function whose
57 scope is a single source file even if the current execution point is not
58 in this file. But it is possible to have more than one such variable or
59 function with the same name (in different source files). If that
60 happens, referring to that name has unpredictable effects. If you wish,
61 you can specify a static variable in a particular function or file,
62 using the colon-colon notation:
64 <pre class=
"example"> <var>file
</var>::
<var>variable
</var>
65 <var>function
</var>::
<var>variable
</var>
68 <p>Here
<var>file
</var> or
<var>function
</var> is the name of the context for the
69 static
<var>variable
</var>. In the case of file names, you can use quotes to
70 make sure GDB parses the file name as a single word--for example,
71 to print a global value of
<code>x
</code> defined in
<code>f2.c
</code>:
73 <pre class=
"example"> (gdb) p 'f2.c'::x
76 <p>This use of
<code>::
</code> is very rarely in conflict with the very similar
77 use of the same notation in C
<tt>++
</tt>. GDB also supports use of the C
<tt>++
</tt>
78 scope resolution operator in GDB expressions.
81 <em>Warning:
</em> Occasionally, a local variable may appear to have the
82 wrong value at certain points in a function--just after entry to a new
83 scope, and just before exit.
85 You may see this problem when you are stepping by machine instructions.
86 This is because, on most machines, it takes more than one instruction to
87 set up a stack frame (including local variable definitions); if you are
88 stepping by machine instructions, variables may appear to have the wrong
89 values until the stack frame is completely built. On exit, it usually
90 also takes more than one machine instruction to destroy a stack frame;
91 after you begin stepping through that group of instructions, local
92 variable definitions may be gone.
94 <p>This may also happen when the compiler does significant optimizations.
95 To be sure of always seeing accurate values, turn off all optimization
98 <p>Another possible effect of compiler optimizations is to optimize
99 unused variables out of existence, or assign variables to registers (as
100 opposed to memory addresses). Depending on the support for such cases
101 offered by the debug info format used by the compiler, GDB
102 might not be able to display values for such local variables. If that
103 happens, GDB will print a message like this:
105 <pre class=
"example"> No symbol
"foo" in current context.
108 <p>To solve such problems, either recompile without optimizations, or use a
109 different debug info format, if the compiler supports several such
110 formats. For example, GCC, the
<small>GNU
</small> C/C
<tt>++
</tt> compiler usually
111 supports the
<code>-gstabs
</code> option.
<code>-gstabs
</code> produces debug info
112 in a format that is superior to formats such as COFF. You may be able
113 to use DWARF2 (
<code>-gdwarf-
2</code>), which is also an effective form for
114 debug info. See
<a href=
"../gcc.info/Debugging-Options.html#Debugging%20Options">Options for Debugging Your Program or
<small>GNU
</small> CC
</a>, for more