An example output is provided (see Pretty Printing).
A pretty-printer is just an object that holds a value and implements a specific interface, defined here.
gdb will call this method on a pretty-printer to compute the children of the pretty-printer's value.
This method must return an object conforming to the Python iterator protocol. Each item returned by the iterator must be a tuple holding two elements. The first element is the “name” of the child; the second element is the child's value. The value can be any Python object which is convertible to a gdb value.
This method is optional. If it does not exist, gdb will act as though the value has no children.
The CLI may call this method and use its result to change the formatting of a value. The result will also be supplied to an MI consumer as a ‘displayhint’ attribute of the variable being printed.
This method is optional. If it does exist, this method must return a string.
Some display hints are predefined by gdb:
- Indicate that the object being printed is “array-like”. The CLI uses this to respect parameters such as
set print elementsand
set print array.
- Indicate that the object being printed is “map-like”, and that the children of this value can be assumed to alternate between keys and values.
- Indicate that the object being printed is “string-like”. If the printer's
to_stringmethod returns a Python string of some kind, then gdb will call its internal language-specific string-printing function to format the string. For the CLI this means adding quotation marks, possibly escaping some characters, respecting
set print elements, and the like.
gdb will call this method to display the string representation of the value passed to the object's constructor.
When printing from the CLI, if the
to_stringmethod exists, then gdb will prepend its result to the values returned by
children. Exactly how this formatting is done is dependent on the display hint, and may change as more hints are added. Also, depending on the print settings (see Print Settings), the CLI may print just the result of
to_stringin a stack trace, omitting the result of
If this method returns a string, it is printed verbatim.
Otherwise, if this method returns an instance of
gdb.Value, then gdb prints this value. This may result in a call to another pretty-printer.
If instead the method returns a Python value which is convertible to a
gdb.Value, then gdb performs the conversion and prints the resulting value. Again, this may result in a call to another pretty-printer. Python scalars (integers, floats, and booleans) and strings are convertible to
gdb.Value; other types are not.
Finally, if this method returns
Nonethen no further operations are peformed in this method and nothing is printed.
If the result is not one of these types, an exception is raised.
gdb provides a function which can be used to look up the
default pretty-printer for a