public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* pretty print beginner question
@ 2011-05-03  4:51 Klaus Rudolph
  2011-05-03 13:36 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Klaus Rudolph @ 2011-05-03  4:51 UTC (permalink / raw)
  To: gdb

Hi again,

after I heared that gdb.printing is comming with gdb 7.3 and we using the last release 7.2 I tried other examples from the older manual, but without any result?

I have a file printer.py:
class StdStringPrinter(object):
    "Print a std::string"

def __init__(self, val):
    self.val = val 

def to_string(self):
    return self.val['_M_dataplus']['_M_p']

def display_hint(self):
    return 'string'


def str_lookup_function(val):
    lookup_tag = val.type.tag
    if lookup_tag == None:
        return None
    regex = re.compile("^std::basic_string<char,.*>$")
    if regex.match(lookup_tag):
        return StdStringPrinter(val)
    return None

def register_printers(objfile):
   objfile.pretty_printers.add(str_lookup_function)


source it from the gdb prompt: source printer.py

have a little c++ prog with a std::string s

if i do a "print s" the output is as without printer.py

Can anyone give a short complete and running example how to write and load and setup a first pretty printer for any kind of data...

It is hard to fill the gaps in the manual :-(

Thanks
 Klaus


-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

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

* Re: pretty print beginner question
  2011-05-03  4:51 pretty print beginner question Klaus Rudolph
@ 2011-05-03 13:36 ` Tom Tromey
  2011-05-03 14:19   ` pretty print beginner question <solved> Klaus Rudolph
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2011-05-03 13:36 UTC (permalink / raw)
  To: Klaus Rudolph; +Cc: gdb

>>>>> "Klaus" == Klaus Rudolph <lts-rudolph@gmx.de> writes:

Klaus> after I heared that gdb.printing is comming with gdb 7.3 and we
Klaus> using the last release 7.2 I tried other examples from the older
Klaus> manual, but without any result?
[...]
Klaus> if i do a "print s" the output is as without printer.py

We would need more information to diagnose the problem.

Could you email your complete gdb session?

Tom

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

* Re: pretty print beginner question <solved>
  2011-05-03 13:36 ` Tom Tromey
@ 2011-05-03 14:19   ` Klaus Rudolph
  0 siblings, 0 replies; 3+ messages in thread
From: Klaus Rudolph @ 2011-05-03 14:19 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb

After a few hours of hinting a got a simple result, puh! :-)

simple source the following file and all I need in my first steps is working now:

---- 
import gdb
import re
import pprint

class StdStringPrinter:
    "Print a std::basic_string of some kind"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        # Make sure &string works, too.
        type = self.val.type
        if type.code == gdb.TYPE_CODE_REF:
            type = type.target ()

        # Calculate the length of the string so that to_string returns
        # the string according to length, not according to first null
        # encountered.
        ptr = self.val ['_M_dataplus']['_M_p']
        realtype = type.unqualified ().strip_typedefs ()
        reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer ()
        header = ptr.cast(reptype) - 1
        len = header.dereference ()['_M_length']
        if hasattr(ptr, "lazy_string"):
            return ptr.lazy_string (length = len)
        return ptr.string (length = len)

    def display_hint (self):
        return 'string'



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.

    #print typename


    for function in pretty_printers_dict:
        if function.search (typename):
            return pretty_printers_dict[function] (val)
        
    # Cannot find a pretty printer.  Return None.
    return None

class DBZPrinter:
    "jj"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        type = self.val.type
        if type.code == gdb.TYPE_CODE_REF:
            type = type.target ()

        #print self.val['banknr']
        #print self.val['offset']

        template_type = self.val.type.template_argument(0)
        nn = gdb.parse_and_eval("basic_pointer");
        print "############################"
        print nn
        print "############################"

        ptr = self.val['offset'] + nn
        #dptr = ptr.cast(gdb.lookup_type('A').pointer())
        dptr = ptr.cast(template_type.pointer())

        #print dptr.dereference()
        #print "(A *) %s" % ( dptr );
        return '(%s *) %s' % ( template_type, dptr );

        #print dptr





    def display_hint (self):
        return 'dbz'

pretty_printers_dict = {}
pretty_printers_dict[re.compile('^std::basic_string<.*>$')] = lambda val: StdStringPrinter(val)
pretty_printers_dict[re.compile('^DBZ<.*>$')] = lambda val: DBZPrinter(val)
gdb.pretty_printers.append (lookup_function)


print "printers loaded"
----------------


As you can see we try to implement a special pointer type. The next step is to handle these pointer templates as normal pointers in gdb. Maybe there will come a lot more questions. Actually dereferencing them from ddd by double click is not possible. operator *() is present, but only 1 indirection is working. I will dig a bit deeper in it and will ask again.

Thanks!
 Klaus

-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

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

end of thread, other threads:[~2011-05-03 14:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-03  4:51 pretty print beginner question Klaus Rudolph
2011-05-03 13:36 ` Tom Tromey
2011-05-03 14:19   ` pretty print beginner question <solved> Klaus Rudolph

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