Debugging with GDB (September 2007)
66 Debugging with GDB
used in expressions. Artificial arrays most often appear in expressions via the value history
(see Section 8.8 [Value history], page 74), after printing one out.
Another way to create an artificial array is to use a cast. This re-interprets a value as if
it were an array. The value need not be in memory:
(gdb) p/x (short[2])0x12345678
$1 = {0x1234, 0x5678}
As a convenience, if you leave the array length out (as in ‘(type [])value ’), GDB
calculates the size to fill the value (as ‘sizeof(value)/sizeof(type )’:
(gdb) p/x (short[])0x12345678
$2 = {0x1234, 0x5678}
Sometimes the artificial array mechanism is not quite enough; in moderately c omplex
data structures, the elements of interest may not actually be adjacent—for example, if you
are interested in the values of pointers in an array. One useful work-around in this situation
is to use a convenience variable (See (see Section 8.9 [Conve nience variables], page 75))
as a counter in an expression that prints the first interesting value, and then repeat that
expression via
h
RET
i
. For instance, suppose you have an array dtab of pointers to structures,
and you are interested in the values of a field fv in each structure. Here is an example of
what you might type:
set $i = 0
p dtab[$i++]->fv
h
RET
i
h
RET
i
...
8.4 Output formats
By default, GDB prints a value according to its data type. Sometimes this is not what
you want. For example, you might want to print a number in hex, or a pointer in decimal.
Or you might want to view data in memory at a certain address as a character string or as
an instruction. To do these things, specify an output format when you print a value.
The simplest use of output formats is to say how to print a value already computed.
This is done by starting the arguments of the print command with a slash and a format
letter. The format letters supported are:
x Regard the bits of the value as an integer, and print the integer in hexadecimal.
d Print as integer in signed decimal.
u Print as integer in unsigned decimal.
o Print as integer in octal.
t Print as integer in binary. The letter ‘t’ stands for “two”.
1
1
‘b’ cannot be used because these format letters are also used with the x command, where ‘b’ stands for
“byte”; see Section 8.5 [Examining memory], page 67.