Debugging with GDB (February 2008)

Table Of Contents
Chapter 8: Examining Data 65
((gdb)) p ’f2.c’::x
This use of :: is very rarely in conflict with the very similar use of the same notation
in C++. GDB also supports use of the C++ scope resolution operator in GDB expressions.
Warning: Occasionally, a local variable may appear to have the wrong value
at certain points in a function just after entry to a new scope, and just before
exit.
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.
This may also happen when the compiler does significant optimizations. To be sure of
always seeing accurate values, turn off all optimization when compiling.
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, GDB
might not be able to display values for such local variables. If that happens, GDB will print
a message like this:
No symbol "foo" in current context.
To solve such problems, either recompile without optimizations, or use a different debug
info format, if the compiler supports several such formats. For example, GCC, the gnu
C/C++ compiler usually supports the -gstabs option. The -gstabs produces debug
information in a format that is superior to formats such as COFF. You may be able to use
DWARF-2 (‘-gdwarf-2’), which is also an effective form for debug info. See Section 4.1
[Compiling for Debugging], page 23.
8.3 Artificial arrays
It is often useful to print out several successive objects of the same type in memory; a
section of an array, or an array of dynamically determined size for which only a pointer
exists in the program.
You can do this by referring to a contiguous span of memory as an artificial array, using
the binary operator @’. The left operand of @ should be the first element of the desired
array and be an individual object. The right operand should be the desired length of the
array. The result is an array value whose elements are all of the type of the left argument.
The first element is actually the left argument; the second element comes from bytes of
memory immediately following those that hold the first element, and so on. Here is an
example. If a program says
int *array = (int *) malloc (len * sizeof (int));
you can print the contents of array with
p *array@len
The left operand of @ must reside in memory. Array values made with @ in this way
behave just like other arrays in terms of subscripting, and are coerced to pointers when