import gdb.printing

# in python2 gdb.Value can only be converted to long(), python3 only has int()
try: a=long(1)
except: long=int

def PrettyPrinter(arg):

    name = getattr(arg, '__name__', arg)

    def PrettyPrinterWrapperWrapperWrapper(func):

        class PrettyPrinterWrapperWrapper:

            class PrettyPrinterWrapper:
                def __init__(self, prefix, val, cb):
                    self.prefix = prefix
                    self.val = val
                    self.cb = cb
                def to_string(self):
                    return self.prefix + self.cb(self.val)

            def __init__(self, name, cb):
                self.name = name
                self.enabled = True
                self.cb = cb

            def __call__(self, val):
                prefix = ''
                if val.type.code == gdb.TYPE_CODE_PTR:
                    prefix = '({}) {:#08x} '.format(str(val.type), long(val))
                    try: val = val.dereference()
                    except: return None
                valtype=val.type.unqualified()
                if valtype.name == self.name:
                    return self.PrettyPrinterWrapper(prefix, val, self.cb)
                if valtype.strip_typedefs().name == self.name:
                    return self.PrettyPrinterWrapper(prefix, val, self.cb)
                return None

        pp=PrettyPrinterWrapperWrapper(name, func)
        gdb.printing.register_pretty_printer(None, pp, True)
        return func

    if callable(arg):
        return PrettyPrinterWrapperWrapperWrapper(arg)

    return PrettyPrinterWrapperWrapperWrapper

@@ -1,8 +1,65 @@
+%22%22%22a helper for easy creation of gdb pretty-printers%22%22%22%0A%0A
 import g
@@ -207,16 +207,808 @@
 r(arg):%0A
+    %22%22%22@PrettyPrinter decorator.%0A%0A    With a @PrettyPrinter decorator one only needs to write a function%0A    that takes gdb.Value as an argument and returns a string to be%0A    shown by gdb.%0A%0A    Typical usage:%0A%0A        @PrettyPrinter%0A        def some_typename(val):%0A            %3Cconvert val to a string and return it%3E%0A%0A    This creates all necessary classes and register a pretty-printer%0A    for the type %22some_typename%22, be it a typedef'ed type name or%0A    the real underlying type with all typedef's resolved. It also%0A    creates a pretty-printer for a pointer to some_typename.%0A%0A    When a type name is not a valid Python identifier, one can use%0A%0A        @PrettyPrinter(%22real complex type name%22)%0A        def does_not_matter(val):%0A            %3Cconvert val to a string and return it%3E%0A    %22%22%22
 %0A    nam

document pretty_printer.py