You can print the values of one or more expressions or all local variables. You can also use the print command to evaluate complex expressions involving typecasts, pointer dereferences, multiple variables, constants, and any legal operators allowed by the language of the program you are debugging:
GDB Mode
print_command
: print [/format specifier][expression]
format specifier
: x | d | u | o | t | a | c | f
By default, GDB prints a value according to its data type. But user might want to print a number in hex, or a pointer in decimal. Or user might want to view data in memory at a certain address as a character string or as an instruction. In this case the user should specify an output format.
To specify a format, append a slash and a format letter to the print command. The format letters supported are:
DBX Mode
print_command
: print [ expression ,... ]
| printb [ expression ,... ]
| printd [ expression ,... ]
| printo [ expression ,... ]
| printx [ expression ,... ]
rescoped_expression
: filename ` qual_symbol
| ` qual_symbol
qual_symbol
: expression
| qual_symbol ` expression
Use the $hexints, $decints, or $octints variables to select a radix for the output of the print command. If you do not want to change the radix permanently, use the printx, printd, printo, and printb commands to print expressions in hexadecimal, decimal, octal, or binary base format, respectively.
Use the set output-radix xxx command to select a radix for the output of the print command, where xxx can be values: 8, 10 or 16.
For an array, the debugger prints every cell in the array if you do not specify a specific cell.
Consider the following declarations in a C++ program:
GDB Mode
(idb) list 59,+2
59 const unsigned int biggestCount = 10;
60 static Moon *biggestMoons[biggestCount];
(idb) list 59:2
59 const unsigned int biggestCount = 10;
60 static Moon *biggestMoons[biggestCount];
The following example uses the print command to display a non-string array:
GDB Mode
(idb) print biggestMoons
$4 = {0x8067998, 0x8067cb0, 0x80679f0, 0x80678e8, 0x8067730, 0x8067940, 0x8068020, 0x8067f18, 0x8067c58, 0x8067f70}
DBX Mode
(idb) print biggestMoons
[0] = 0x8067998,[1] = 0x8067cb0,[2] = 0x80679f0,[3] = 0x80678e8,[4] = 0x8067730,[5] = 0x8067940,[6] = 0x8068020,[7] = 0x8067f18,[8] = 0x8067c58,[9] = 0x8067f70
The following example shows how to print individual values of an array:
GDB Mode
(idb) print biggestMoons[3]
$7 = (Moon *) 0x80678e8
(idb) print *biggestMoons[3]
$8 = {<Planet> = {<HeavenlyBody> = {_name = 0x805a514 "Io", _innerNeighbor = 0x0, _outerNeighbor = 0x8067940, _firstSatellite = 0x0, _lastSatellite = 0x0}, <Orbit> = {_primary = 0x8067890, _distance = 422, _name = 0x8067918 "Jupiter 1"}}, _radius = 1815}
DBX Mode
(idb) print biggestMoons[3]
0x80678e8
(idb) print *biggestMoons[3]
class Moon {
_radius = 1815;
_name = 0x805a514="Io"; // class Planet::HeavenlyBody
_innerNeighbor = 0x0; // class Planet::HeavenlyBody
_outerNeighbor = 0x8067940; // class Planet::HeavenlyBody
_firstSatellite = 0x0; // class Planet::HeavenlyBody
_lastSatellite = 0x0; // class Planet::HeavenlyBody
_primary = 0x8067890; // class Planet::Orbit
_distance = 422; // class Planet::Orbit
_name = 0x8067918="Jupiter 1"; // class Planet::Orbit
}