Debugging with GDB Manual HP WDB v6.3 (5900-2180, August 2012)
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 “Compiling for debugging” (page 31).
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 used in expressions.
Artificial arrays most often appear in expressions via the value history (see “Value history”
(page 72)), 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 complex 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 “Convenience variables” (page 73)) as a counter in an expression that prints the
first interesting value, and then repeat that expression via RET. 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
<RET>
<RET>
...
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.
8.3 Artificial arrays 65