public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "pedro at codesourcery dot com" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug gdb/13465] typecasting in gdb python pretty-printer
Date: Fri, 02 Dec 2011 12:14:00 -0000	[thread overview]
Message-ID: <bug-13465-4717-N2ZArVRrZT@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-13465-4717@http.sourceware.org/bugzilla/>

http://sourceware.org/bugzilla/show_bug.cgi?id=13465

Pedro Alves <pedro at codesourcery dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pedro at codesourcery dot
                   |                            |com

--- Comment #2 from Pedro Alves <pedro at codesourcery dot com> 2011-12-02 12:13:32 UTC ---
I think there are ways to avoid the recursion.  It's not a real replacement for
a value-replacement facility, but this works for me (based on py-prettyprint.py
in the testsuite):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:

struct base
{
  int a;
};

struct sub
{
  struct base base;
  int b;
};

struct base b = { 1 };
struct sub s = { { 1 }, 2};

int
main (int argc, char *argv)
{
  return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
baseprinter.py:

import re
import gdb

class pp_base:
    def __init__(self, val):
        self.val = val
        ptrtype = gdb.lookup_type('struct sub')
        self.castval = val.address.cast(ptrtype.pointer()).dereference ()

    def to_string(self):
        disable_lookup_function ()
        r = str(self.castval)
        enable_lookup_function ()
        return r

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.
    for function in pretty_printers_dict:
        if function.match (typename):
            return pretty_printers_dict[function] (val)

    # Cannot find a pretty printer.  Return None.

    return None

def disable_lookup_function ():
    lookup_function.enabled = False

def enable_lookup_function ():
    lookup_function.enabled = True

def register_pretty_printers ():
    pretty_printers_dict[re.compile ('^struct base$')]   = pp_base
    pretty_printers_dict[re.compile ('^base$')]   = pp_base

pretty_printers_dict = {}

register_pretty_printers ()
gdb.pretty_printers.append (lookup_function)

$ gdb -nx -q ~/printer -ex "set python print-stack on" -ex "python execfile
('/home/pedro/printer.py')"
Reading symbols from /home/pedro/printer...done.
(gdb) p b
$1 = {base = {a = 1}, b = 1}
(gdb) p /r b
$2 = {a = 1}
(gdb) 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

That is, disable the printer lookup function of the baseclass when you want to
print it raw.

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


  parent reply	other threads:[~2011-12-02 12:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-01 19:53 [Bug gdb/13465] New: " eric at anholt dot net
2011-12-01 20:26 ` [Bug gdb/13465] " tromey at redhat dot com
2011-12-02 12:14 ` pedro at codesourcery dot com [this message]
2011-12-14 16:29 ` dje at google dot com
2012-02-07 16:01 ` [Bug python/13465] " tromey at redhat dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-13465-4717-N2ZArVRrZT@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).