public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* help with pretty printing
@ 2012-06-01  9:07 somersetgraham
  2012-06-02  5:28 ` Niko Sams
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: somersetgraham @ 2012-06-01  9:07 UTC (permalink / raw)
  To: gdb


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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-06-25 11:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-01  9:07 help with pretty printing somersetgraham
2012-06-02  5:28 ` Niko Sams
2012-06-02 10:29 ` xgsa
2012-06-02 11:10   ` Niko Sams
2012-06-02 11:24     ` xgsa
2012-06-07  7:42 ` Joachim Protze
2012-06-07 18:27   ` Paul_Koning
2012-06-25 11:05     ` Joachim Protze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).