help with pretty printing

somersetgraham graham.labdon@avalonsciences.com
Fri Jun 1 09:07:00 GMT 2012


I am writing some gdb pretty printers for Qt classes to be used in the
Eclipse IDE, but cannot seem to handle pointers to data structures very well
(I am very new to Python)

For example I want to pretty print the QFile structure which I have achieved
with following code –

class QFilePrinter:
    def __init__(self, val):
        self.val = val
        data = self.val['d_ptr']['d'].dereference()

    def children(self):
        names = [1,2]
        data = self.val['d_ptr']['d']
        for i in names:
            if i==1:
                ptype = gdb.lookup_type("QFilePrivate").pointer()
                yield "fileName",data.cast(ptype).dereference()["fileName"]
            if i == 2:
                exp = "((class QFile*)%s)->exists()" % (self.val.address)
                r = callClassMethod(self.val, "exists","")
                yield "exists",r
                    
    def to_string (self):
        data = self.val['d_ptr']['d']
        ptype = gdb.lookup_type("QFilePrivate").pointer()
        return "%s %s" %
("fileName",data.cast(ptype).dereference()["fileName"])

    def display_hint (self):
        return 'string'

I have followed some examples that I found online  for building a lookup
table functions to be called – 

def lookup_function (val):
    #print "Look-up and return a pretty-printer that can print val."

    # Get the type.
    type = val.type;
    print type
    # If it points to a reference, get the reference.
    if type.code == gdb.TYPE_CODE_REF:
        print "deref"
        type = type.target ()

    print type
    # Get the unqualified type, stripped of typedefs.
    type = type.unqualified ().strip_typedefs ()
    print type
       
    # Get the type name.
    typename = type.tag
    print typename
    #print typename
    if typename == None:
        print "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.search (typename):
            print "found"
            return pretty_printers_dict[function] (val)
                    
    # Cannot find a pretty printer.  Return None.
    print "not found"
    return None
#
def build_dictionary ():
    pretty_printers_dict[re.compile ('^QFile$')] = lambda
val:QFilePrinter(val)
    pretty_printers_dict[re.compile ('^QFile *$')] = lambda
val:QFilePrinter(val)

pretty_printers_dict = {}

build_dictionary ()

When I debug my code containing a pointer to a QFile object my pretty
printer is not invoked, but if I type ‘print *f’ (f is a pointer to a QFile
object) the pretty printer is invoked. Also I add ‘*f’ in the Expressions
window in Eclipse the printer is invoked.

Is it possible to get my pretty printer to work with pointers in all cases?


-- 
View this message in context: http://old.nabble.com/help-with-pretty-printing-tp33943560p33943560.html
Sent from the Sourceware - gdb list mailing list archive at Nabble.com.



More information about the Gdb mailing list