Debugging with GDB (September 2007)

Chapter 8: Examining Data 63
8 Examining Data
The usual way to examine data in your program is with the print command (abbreviated
p), or its synonym inspect. It evaluates and prints the value of an expression of the
language your program is written in (see Chapter 9 [Using GDB with Different Languages],
page 79).
The following forms of print command are supp orted:
print expr
print /f expr
expr is an expression (in the source language). By default the value of expr is
printed in a format appropriate to its data type; you can choose a different for-
mat by specifying /f ’, where f is a letter specifying the format; see Section 8.4
[Output formats], page 66.
print
print /f If you omit expr, GDB displays the last value again (from the value history; see
Section 8.8 [Value history], page 74). This allows you to conveniently inspect
the same value in an alternative format.
A more low-level way of examining data is with the x command. It examines data in
memory at a specified address and prints it in a specified format. See Section 8.5 [Examining
memory], page 67.
If you are interested in information about types, or about how the fields of a struct
or a class are declared, use the ptype exp command rather than print. See Chapter 10
[Examining the Symb ol Table], page 93.
8.1 Expressions
print and many other GDB c ommands accept an expression and compute its value. Any
kind of constant, variable or operator defined by the programming language you are using
is valid in an expression in GDB. This includes conditional expressions, function calls, casts
and string constants. It unfortunately does not include symbols defined by preprocessor
#define commands.
GDB supports array constants in expressions input by the user. The syntax is {element,
element. . . }. For example, you can use the command print {1, 2, 3} to build up an array
in memory that calls malloc in the target program.
Because C is so widespread, most of the expressions shown in examples in this manual
are in C. See Chapter 9 [Using GDB with Different Languages], page 79, for information
on how to use expressions in other languages.
In this section, we discuss operators that you can use in GDB expressions regardless of
your programming language.
Casts are supported in all languages, not just in C, because it is so useful to cast a
numb e r into a pointer in order to examine a structure at that address in memory.
GDB supports these operators, in addition to those common to programming languages: