Debugging with GDB (September 2007)

Chapter 8: Examining Data 75
or discarded (for example with the file or symbol-file commands). When the symbol
table changes, the value history is discarded, since the values may contain pointers back to
the types defined in the symb ol table.
The values printed are given history numbers by which you can refer to them. These
are a range of integers starting with one. print shows you the history number assigned to
a value by printing $num = before the value; here num is the history number.
To refer to any previous value, use $ followed by the history number of the value. The
way print labe ls its output is designed to remind you of this. Just $ refers to the most
recent value in the history, and $$ refers to the value before that. $$n refers to the nth
value from the end; $$2 is the value just prior to $$, $$1 is equivalent to $$, and $$0 is
equivalent to $.
For example, suppos e you have just printed a pointer to a structure and want to see the
contents of the structure. It s uffice s to type
p *$
If you have a chain of structures where the component next points to the next one, you
can print the contents of the next one with this:
p *$.next
You can print successive links in the chain by repeating this command using the
h
RET
i
key.
Note that the history records values, not expressions. If the value of x is 4 and you type
these commands:
print x
set x=5
then the value recorded in the value history by the print command remains 4 even though
the value of x has changed.
show values
Print the last ten values in the value history, with their item numb e rs. This is
like p $$9 repeated ten times, except that show values does not change the
history.
show values n
Print ten history values centered on history item number n.
show values +
Print ten history values following the values last printed. If no more values are
available, show values + produces no display.
Pressing
h
RET
i
to repeat show values n has exactly the same effect as show values +’.
8.9 Convenience variables
GDB provides convenience variables that you can use within GDB to hold on to a value
and refer to it later. These variables exist entirely within GDB. They are not part of your
program, and setting a convenience variable has no direct effect on further execution of your
program. That is why you can use them freely.