Context: In Mesa graphics drivers implemented in C, there are these Mesa core types (struct gl_texture_object, struct gl_texture_image), which each driver creates "subclasses" of by embedding that struct in their own struct as: struct intel_texture_image { struct gl_texture_image base; struct intel_mipmap_tree *mt; } When debugging Mesa, we often have a pointer to a base type like struct gl_texture_image, so to print the information we always want to see we have to p *(struct intel_context *)tex_image. What I want: I'd like to be able to implement a python pretty-printer autoloaded based on my driver's object file that can make a decision about what type to print one of these base types as (either by looking at vtbl-like information in some objects, or just by knowing that if you've got i965_dri.so loaded, every struct gl_texture_image is a struct intel_texture_image). However, I'm told that there isn't a way to do that today: <tromey> so, we don't have a value-replacement facility <tromey> that might be a nice addition <xdje> replace the value where? <tromey> he basically wants something like 'set print object on', that works for his particular C case <tromey> based on knowledge of his program <tromey> without writing a full printer for the enclosing struct
I thought maybe this could be done by returning the derived object in the printer's to_string method. However, there is no way to circumvent the resulting recursion in this case. So, I think we need some new facility here.
I think there are ways to avoid the recursion. It's not a real replacement for a value-replacement facility, but this works for me (based on py-prettyprint.py in the testsuite): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.c: struct base { int a; }; struct sub { struct base base; int b; }; struct base b = { 1 }; struct sub s = { { 1 }, 2}; int main (int argc, char *argv) { return 0; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ baseprinter.py: import re import gdb class pp_base: def __init__(self, val): self.val = val ptrtype = gdb.lookup_type('struct sub') self.castval = val.address.cast(ptrtype.pointer()).dereference () def to_string(self): disable_lookup_function () r = str(self.castval) enable_lookup_function () return r def lookup_function (val): "Look-up and return a pretty-printer that can print val." # Get the type. type = val.type # If it points to a reference, get the reference. if type.code == gdb.TYPE_CODE_REF: type = type.target () # Get the unqualified type, stripped of typedefs. type = type.unqualified ().strip_typedefs () # Get the type name. typename = type.tag if typename == None: return None # Iterate over local dictionary of types to determine # if a printer is registered for that type. Return an # instantiation of the printer if found. for function in pretty_printers_dict: if function.match (typename): return pretty_printers_dict[function] (val) # Cannot find a pretty printer. Return None. return None def disable_lookup_function (): lookup_function.enabled = False def enable_lookup_function (): lookup_function.enabled = True def register_pretty_printers (): pretty_printers_dict[re.compile ('^struct base$')] = pp_base pretty_printers_dict[re.compile ('^base$')] = pp_base pretty_printers_dict = {} register_pretty_printers () gdb.pretty_printers.append (lookup_function) $ gdb -nx -q ~/printer -ex "set python print-stack on" -ex "python execfile ('/home/pedro/printer.py')" Reading symbols from /home/pedro/printer...done. (gdb) p b $1 = {base = {a = 1}, b = 1} (gdb) p /r b $2 = {a = 1} (gdb) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ That is, disable the printer lookup function of the baseclass when you want to print it raw.