toolchain/share/doc/gdb/Variables.html

242 lines
11 KiB
HTML
Raw Permalink Normal View History

2024-01-10 05:24:32 +00:00
<html lang="en">
<head>
<title>Variables - Debugging with GDB</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Debugging with GDB">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Data.html#Data" title="Data">
<link rel="prev" href="Ambiguous-Expressions.html#Ambiguous-Expressions" title="Ambiguous Expressions">
<link rel="next" href="Arrays.html#Arrays" title="Arrays">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1988-2019 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software'' and ``Free Software Needs
Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,''
and with the Back-Cover Texts as in (a) below.
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom.''
-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Variables"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Arrays.html#Arrays">Arrays</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Ambiguous-Expressions.html#Ambiguous-Expressions">Ambiguous Expressions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Data.html#Data">Data</a>
<hr>
</div>
<h3 class="section">10.3 Program Variables</h3>
<p>The most common kind of expression to use is the name of a variable
in your program.
<p>Variables in expressions are understood in the selected stack frame
(see <a href="Selection.html#Selection">Selecting a Frame</a>); they must be either:
<ul>
<li>global (or file-static)
</ul>
<p class="noindent">or
<ul>
<li>visible according to the scope rules of the
programming language from the point of execution in that frame
</ul>
<p class="noindent">This means that in the function
<pre class="smallexample"> foo (a)
int a;
{
bar (a);
{
int b = test ();
bar (b);
}
}
</pre>
<p class="noindent">you can examine and use the variable <code>a</code> whenever your program is
executing within the function <code>foo</code>, but you can only use or
examine the variable <code>b</code> while your program is executing inside
the block where <code>b</code> is declared.
<p><a name="index-variable-name-conflict-636"></a>There is an exception: you can refer to a variable or function whose
scope is a single source file even if the current execution point is not
in this file. But it is possible to have more than one such variable or
function with the same name (in different source files). If that
happens, referring to that name has unpredictable effects. If you wish,
you can specify a static variable in a particular function or file by
using the colon-colon (<code>::</code>) notation:
<p><a name="index-colon_002dcolon_002c-context-for-variables_002ffunctions-637"></a><!-- info cannot cope with a :: index entry, but why deprive hard copy readers? -->
<a name="index-g_t_0040code_007b_003a_003a_007d_002c-context-for-variables_002ffunctions-638"></a>
<pre class="smallexample"> <var>file</var>::<var>variable</var>
<var>function</var>::<var>variable</var>
</pre>
<p class="noindent">Here <var>file</var> or <var>function</var> is the name of the context for the
static <var>variable</var>. In the case of file names, you can use quotes to
make sure <span class="sc">gdb</span> parses the file name as a single word&mdash;for example,
to print a global value of <code>x</code> defined in <samp><span class="file">f2.c</span></samp>:
<pre class="smallexample"> (gdb) p 'f2.c'::x
</pre>
<p>The <code>::</code> notation is normally used for referring to
static variables, since you typically disambiguate uses of local variables
in functions by selecting the appropriate frame and using the
simple name of the variable. However, you may also use this notation
to refer to local variables in frames enclosing the selected frame:
<pre class="smallexample"> void
foo (int a)
{
if (a &lt; 10)
bar (a);
else
process (a); /* Stop here */
}
int
bar (int a)
{
foo (a + 5);
}
</pre>
<p class="noindent">For example, if there is a breakpoint at the commented line,
here is what you might see
when the program stops after executing the call <code>bar(0)</code>:
<pre class="smallexample"> (gdb) p a
$1 = 10
(gdb) p bar::a
$2 = 5
(gdb) up 2
#2 0x080483d0 in foo (a=5) at foobar.c:12
(gdb) p a
$3 = 5
(gdb) p bar::a
$4 = 0
</pre>
<p><a name="index-C_0040t_007b_002b_002b_007d-scope-resolution-639"></a>These uses of &lsquo;<samp><span class="samp">::</span></samp>&rsquo; are very rarely in conflict with the very
similar use of the same notation in C<tt>++</tt>. When they are in
conflict, the C<tt>++</tt> meaning takes precedence; however, this can be
overridden by quoting the file or function name with single quotes.
<p>For example, suppose the program is stopped in a method of a class
that has a field named <code>includefile</code>, and there is also an
include file named <samp><span class="file">includefile</span></samp> that defines a variable,
<code>some_global</code>.
<pre class="smallexample"> (gdb) p includefile
$1 = 23
(gdb) p includefile::some_global
A syntax error in expression, near `'.
(gdb) p 'includefile'::some_global
$2 = 27
</pre>
<p><a name="index-wrong-values-640"></a><a name="index-variable-values_002c-wrong-641"></a><a name="index-function-entry_002fexit_002c-wrong-values-of-variables-642"></a><a name="index-optimized-code_002c-wrong-values-of-variables-643"></a><blockquote>
<em>Warning:</em> Occasionally, a local variable may appear to have the
wrong value at certain points in a function&mdash;just after entry to a new
scope, and just before exit.
</blockquote>
You may see this problem when you are stepping by machine instructions.
This is because, on most machines, it takes more than one instruction to
set up a stack frame (including local variable definitions); if you are
stepping by machine instructions, variables may appear to have the wrong
values until the stack frame is completely built. On exit, it usually
also takes more than one machine instruction to destroy a stack frame;
after you begin stepping through that group of instructions, local
variable definitions may be gone.
<p>This may also happen when the compiler does significant optimizations.
To be sure of always seeing accurate values, turn off all optimization
when compiling.
<p><a name="index-g_t_0060_0060No-symbol-_0022foo_0022-in-current-context_0027_0027-644"></a>Another possible effect of compiler optimizations is to optimize
unused variables out of existence, or assign variables to registers (as
opposed to memory addresses). Depending on the support for such cases
offered by the debug info format used by the compiler, <span class="sc">gdb</span>
might not be able to display values for such local variables. If that
happens, <span class="sc">gdb</span> will print a message like this:
<pre class="smallexample"> No symbol "foo" in current context.
</pre>
<p>To solve such problems, either recompile without optimizations, or use a
different debug info format, if the compiler supports several such
formats. See <a href="Compilation.html#Compilation">Compilation</a>, for more information on choosing compiler
options. See <a href="C.html#C">C and C<tt>++</tt></a>, for more information about debug
info formats that are best suited to C<tt>++</tt> programs.
<p>If you ask to print an object whose contents are unknown to
<span class="sc">gdb</span>, e.g., because its data type is not completely specified
by the debug information, <span class="sc">gdb</span> will say &lsquo;<samp><span class="samp">&lt;incomplete
type&gt;</span></samp>&rsquo;. See <a href="Symbols.html#Symbols">incomplete type</a>, for more about this.
<p><a name="index-no-debug-info-variables-645"></a>If you try to examine or use the value of a (global) variable for
which <span class="sc">gdb</span> has no type information, e.g., because the program
includes no debug information, <span class="sc">gdb</span> displays an error message.
See <a href="Symbols.html#Symbols">unknown type</a>, for more about unknown types. If you
cast the variable to its declared type, <span class="sc">gdb</span> gets the
variable's value using the cast-to type as the variable's type. For
example, in a C program:
<pre class="smallexample"> (gdb) p var
'var' has unknown type; cast it to its declared type
(gdb) p (float) var
$1 = 3.14
</pre>
<p>If you append <kbd>@entry</kbd> string to a function parameter name you get its
value at the time the function got called. If the value is not available an
error message is printed. Entry values are available only with some compilers.
Entry values are normally also printed at the function parameter list according
to <a href="set-print-entry_002dvalues.html#set-print-entry_002dvalues">set print entry-values</a>.
<pre class="smallexample"> Breakpoint 1, d (i=30) at gdb.base/entry-value.c:29
29 i++;
(gdb) next
30 e (i);
(gdb) print i
$1 = 31
(gdb) print i@entry
$2 = 30
</pre>
<p>Strings are identified as arrays of <code>char</code> values without specified
signedness. Arrays of either <code>signed char</code> or <code>unsigned char</code> get
printed as arrays of 1 byte sized integers. <code>-fsigned-char</code> or
<code>-funsigned-char</code> <span class="sc">gcc</span> options have no effect as <span class="sc">gdb</span>
defines literal string type <code>"char"</code> as <code>char</code> without a sign.
For program code
<pre class="smallexample"> char var0[] = "A";
signed char var1[] = "A";
</pre>
<p>You get during debugging
<pre class="smallexample"> (gdb) print var0
$1 = "A"
(gdb) print var1
$2 = {65 'A', 0 '\0'}
</pre>
</body></html>