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

* Re: help with pretty printing
  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-07  7:42 ` Joachim Protze
  2 siblings, 0 replies; 8+ messages in thread
From: Niko Sams @ 2012-06-02  5:28 UTC (permalink / raw)
  To: somersetgraham; +Cc: gdb

On Fri, Jun 1, 2012 at 11:07 AM, somersetgraham
<graham.labdon@avalonsciences.com> wrote:
>
> 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)
No answer to your question, but did you consider contributing your
pretty printers to kdevelop?

cheers,
Niko

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

* Re: help with pretty printing
  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-07  7:42 ` Joachim Protze
  2 siblings, 1 reply; 8+ messages in thread
From: xgsa @ 2012-06-02 10:29 UTC (permalink / raw)
  To: somersetgraham; +Cc: gdb

You could try the Qt pretty from here: 
https://projects.kde.org/projects/extragear/kdevelop/kdevelop/repository/revisions/4.1/show/debuggers/gdb/printers

If they work properly you could compare their implementation with yours 
or just use them.

Anton.

-------- Original message --------
> 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?
>
>


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

* Re: help with pretty printing
  2012-06-02 10:29 ` xgsa
@ 2012-06-02 11:10   ` Niko Sams
  2012-06-02 11:24     ` xgsa
  0 siblings, 1 reply; 8+ messages in thread
From: Niko Sams @ 2012-06-02 11:10 UTC (permalink / raw)
  To: xgsa; +Cc: somersetgraham, gdb

On Sat, Jun 2, 2012 at 12:28 PM, xgsa <xgsa@yandex.ru> wrote:
> You could try the Qt pretty from here:
> https://projects.kde.org/projects/extragear/kdevelop/kdevelop/repository/revisions/4.1/show/debuggers/gdb/printers
>
> If they work properly you could compare their implementation with yours or
> just use them.
I've written those :D
I'm just asking graham if he want's to add his QFilePrinter there (and
possibly others)

Niko

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

* Re: help with pretty printing
  2012-06-02 11:10   ` Niko Sams
@ 2012-06-02 11:24     ` xgsa
  0 siblings, 0 replies; 8+ messages in thread
From: xgsa @ 2012-06-02 11:24 UTC (permalink / raw)
  To: Niko Sams; +Cc: somersetgraham, gdb

> On Sat, Jun 2, 2012 at 12:28 PM, xgsa<xgsa@yandex.ru>  wrote:
>> You could try the Qt pretty from here:
>> https://projects.kde.org/projects/extragear/kdevelop/kdevelop/repository/revisions/4.1/show/debuggers/gdb/printers
>>
>> If they work properly you could compare their implementation with yours or
>> just use them.
> I've written those :D

Ok, so taking the opportunity I want to thank you. I use them and they 
are cool! :)

> I'm just asking graham if he want's to add his QFilePrinter there (and
> possibly others)

I thought it is supported yet. Yes, it would be great to add it.


Anton.

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

* Re: help with pretty printing
  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-07  7:42 ` Joachim Protze
  2012-06-07 18:27   ` Paul_Koning
  2 siblings, 1 reply; 8+ messages in thread
From: Joachim Protze @ 2012-06-07  7:42 UTC (permalink / raw)
  To: somersetgraham; +Cc: gdb

[-- Attachment #1: Type: text/plain, Size: 583 bytes --]

On 01.06.2012 11:07, somersetgraham wrote:
> def build_dictionary ():
>     pretty_printers_dict[re.compile ('^QFile$')] = lambda
> val:QFilePrinter(val)
>     pretty_printers_dict[re.compile ('^QFile *$')] = lambda
> val:QFilePrinter(val)
you may try something like the following to match both cases:

pretty_printers_dict[re.compile('^QFile ( \*)?$')] = lambda
val:QFilePrinter(val)


The asterisk is a special character in regular expressions and has to be escaped to match an asterisk. Your regexp matches QFile with any count of spaces as postfix.

- Joachim



[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5306 bytes --]

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

* Re: help with pretty printing
  2012-06-07  7:42 ` Joachim Protze
@ 2012-06-07 18:27   ` Paul_Koning
  2012-06-25 11:05     ` Joachim Protze
  0 siblings, 1 reply; 8+ messages in thread
From: Paul_Koning @ 2012-06-07 18:27 UTC (permalink / raw)
  To: joachim.protze; +Cc: graham.labdon, gdb


On Jun 7, 2012, at 3:42 AM, Joachim Protze wrote:

> On 01.06.2012 11:07, somersetgraham wrote:
>> def build_dictionary ():
>>    pretty_printers_dict[re.compile ('^QFile$')] = lambda
>> val:QFilePrinter(val)
>>    pretty_printers_dict[re.compile ('^QFile *$')] = lambda
>> val:QFilePrinter(val)
> you may try something like the following to match both cases:
> 
> pretty_printers_dict[re.compile('^QFile ( \*)?$')] = lambda
> val:QFilePrinter(val)
> 
> 
> The asterisk is a special character in regular expressions and has to be escaped to match an asterisk. Your regexp matches QFile with any count of spaces as postfix.
> 
> - Joachim

Yes, but since you're dealing with a regular (not raw) string here, the \ needs to be doubled, otherwise it is treated as a string character escape instead of a backslash character inside the string.  So one of these will match space followed by asterisk:

pretty_printers_dict[re.compile('^QFile ( \\*)?$')] = lambda val:QFilePrinter(val)
pretty_printers_dict[re.compile(r'^QFile ( \*)?$')] = lambda val:QFilePrinter(val)

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

* Re: help with pretty printing
  2012-06-07 18:27   ` Paul_Koning
@ 2012-06-25 11:05     ` Joachim Protze
  0 siblings, 0 replies; 8+ messages in thread
From: Joachim Protze @ 2012-06-25 11:05 UTC (permalink / raw)
  To: Paul_Koning; +Cc: graham.labdon, gdb

[-- Attachment #1: Type: text/plain, Size: 1319 bytes --]

Hi,
On 07.06.2012 20:26, Paul_Koning@Dell.com wrote:
> Yes, but since you're dealing with a regular (not raw) string here, the \ needs to be doubled, otherwise it is treated as a string character escape instead of a backslash character inside the string.  So one of these will match space followed by asterisk:
your post let me wonder, why my code worked for me and I asked the
python interpreter for his opinion:

>>> '^QFile (
\*)?$'                                                                                                                       

'^QFile (
\\*)?$'                                                                                                                          

>>> '^QFile (
\\*)?$'                                                                                                                      

'^QFile (
\\*)?$'                                                                                                                          

>>> r'^QFile (
\*)?$'                                                                                                                      

'^QFile ( \\*)?$'

Equal output for all versions of these strings and for python versions
2.4, 2.6, 2.7 and 3.2 (the versions I found on severals machines).

- Joachim



[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5319 bytes --]

^ 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).