GDB provides a way for Python code to customize type display. This is mainly useful for substituting canonical typedef names for types.
A type printer is just a Python object conforming to a certain protocol. A simple base class implementing the protocol is provided; see gdb.types. A type printer must supply at least:
recognizemethod, as described below.
When displaying a type, say via the
ptype command, GDB
will compute a list of type recognizers. This is done by iterating
first over the per-objfile type printers (see Objfiles In Python),
followed by the per-progspace type printers (see Progspaces In Python), and finally the global type printers.
GDB will call the
instantiate method of each enabled
type printer. If this method returns
None, then the result is
ignored; otherwise, it is appended to the list of recognizers.
Then, when GDB is going to display a type name, it iterates
over the list of recognizers. For each one, it calls the recognition
function, stopping if the function returns a non-
The recognition function is defined as:
typeis not recognized, return
None. Otherwise, return a string which is to be printed as the name of
typeargument will be an instance of
gdb.Type(see Types In Python).
GDB uses this two-pass approach so that type printers can efficiently cache information without holding on to it too long. For example, it can be convenient to look up type information in a type printer and hold it for a recognizer’s lifetime; if a single pass were done then type printers would have to make use of the event system in order to avoid holding information that could become stale as the inferior changed.