public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug gdb/13465] New: typecasting in gdb python pretty-printer
@ 2011-12-01 19:53 eric at anholt dot net
  2011-12-01 20:26 ` [Bug gdb/13465] " tromey at redhat dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: eric at anholt dot net @ 2011-12-01 19:53 UTC (permalink / raw)
  To: gdb-prs

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

             Bug #: 13465
           Summary: typecasting in gdb python pretty-printer
           Product: gdb
           Version: 7.3
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: gdb
        AssignedTo: unassigned@sourceware.org
        ReportedBy: eric@anholt.net
    Classification: Unclassified


Context:

In Mesa graphics drivers implemented in C, there are these Mesa core types
(struct gl_texture_object, struct gl_texture_image), which each driver creates
"subclasses" of by embedding that struct in their own struct as:

struct intel_texture_image {
  struct gl_texture_image base;
  struct intel_mipmap_tree *mt;
}

When debugging Mesa, we often have a pointer to a base type like struct
gl_texture_image, so to print the information we always want to see we have to
p *(struct intel_context *)tex_image.

What I want:

I'd like to be able to implement a python pretty-printer autoloaded based on my
driver's object file that can make a decision about what type to print one of
these base types as (either by looking at vtbl-like information in some
objects, or just by knowing that if you've got i965_dri.so loaded, every struct
gl_texture_image is a struct intel_texture_image).  However, I'm told that
there isn't a way to do that today:

<tromey> so, we don't have a value-replacement facility
<tromey> that might be a nice addition
<xdje> replace the value where?
<tromey> he basically wants something like 'set print object on', that works
for his particular C case
<tromey> based on knowledge of his program
<tromey> without writing a full printer for the enclosing struct

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


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

* [Bug gdb/13465] typecasting in gdb python pretty-printer
  2011-12-01 19:53 [Bug gdb/13465] New: typecasting in gdb python pretty-printer eric at anholt dot net
@ 2011-12-01 20:26 ` tromey at redhat dot com
  2011-12-02 12:14 ` pedro at codesourcery dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: tromey at redhat dot com @ 2011-12-01 20:26 UTC (permalink / raw)
  To: gdb-prs

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

Tom Tromey <tromey at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tromey at redhat dot com

--- Comment #1 from Tom Tromey <tromey at redhat dot com> 2011-12-01 20:24:51 UTC ---
I thought maybe this could be done by returning the
derived object in the printer's to_string method.
However, there is no way to circumvent the resulting
recursion in this case.
So, I think we need some new facility here.

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


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

* [Bug gdb/13465] typecasting in gdb python pretty-printer
  2011-12-01 19:53 [Bug gdb/13465] New: typecasting in gdb python pretty-printer 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
  2011-12-14 16:29 ` dje at google dot com
  2012-02-07 16:01 ` [Bug python/13465] " tromey at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pedro at codesourcery dot com @ 2011-12-02 12:14 UTC (permalink / raw)
  To: gdb-prs

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.


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

* [Bug gdb/13465] typecasting in gdb python pretty-printer
  2011-12-01 19:53 [Bug gdb/13465] New: typecasting in gdb python pretty-printer 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
@ 2011-12-14 16:29 ` dje at google dot com
  2012-02-07 16:01 ` [Bug python/13465] " tromey at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: dje at google dot com @ 2011-12-14 16:29 UTC (permalink / raw)
  To: gdb-prs

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

dje at google dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dje at google dot com

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


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

* [Bug python/13465] typecasting in gdb python pretty-printer
  2011-12-01 19:53 [Bug gdb/13465] New: typecasting in gdb python pretty-printer eric at anholt dot net
                   ` (2 preceding siblings ...)
  2011-12-14 16:29 ` dje at google dot com
@ 2012-02-07 16:01 ` tromey at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: tromey at redhat dot com @ 2012-02-07 16:01 UTC (permalink / raw)
  To: gdb-prs

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

Tom Tromey <tromey at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|gdb                         |python

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


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

end of thread, other threads:[~2012-02-07 16:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-01 19:53 [Bug gdb/13465] New: typecasting in gdb python pretty-printer 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
2011-12-14 16:29 ` dje at google dot com
2012-02-07 16:01 ` [Bug python/13465] " tromey at redhat dot com

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