public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
From: Matthieu VIAL <matthieu.vial@orange-ftgroup.com>
To: Richard Ward <richard.j.ward1@googlemail.com>
Cc: archer@sourceware.org
Subject: Re: How to cast a pointer to a base class with multiple inheritance
Date: Tue, 04 Aug 2009 11:40:00 -0000	[thread overview]
Message-ID: <1249386015.2853.15.camel@pc1134.silicomp.fr> (raw)
In-Reply-To: <54ee47490908040141h6e2c94a0t7ecdca39172ff71e@mail.gmail.com>

Your explanations sound interesting because for the moment I store rtti
from g++ in my code (with mangling problem).

Then I use the bitpos attribute of gdb.Field to find the appropriate
class in the inheritance tree.

I wrote the following code to have dynamic upcast feature not available
with gdb.Value.cast
May be this kind of feature could be useful in archer ?


--CODE--

def find_class_offset(init, src, dst):
    if str(src) == str(dst):
        return [True, init]
    
    for field in src.fields():
        if field.is_base_class:
            result = find_class_offset(init - field.bitpos / 8,
field.type, dst)
            if result[0]:
                return result
    
    return [False, init]
    

def dynamic_upcast(cast_type, value):
    assert(value.type.code == gdb.TYPE_CODE_PTR)
    
    if (cast_type.code == gdb.TYPE_CODE_PTR) or (cast_type.code ==
gdb.TYPE_CODE_REF):
        src = cast_type.target()
    else:
        src = cast_type
    dst = value.type.target()
    
    result = find_class_offset(0, src, dst)
    
    if not result[0]:
        raise Exception('Invalid dynamic cast')
    else:
        addr = int(str(value), 16)
        addr += result[1]
        eval_str = '(\'%s\'*)0x%X' % (src.tag, addr)
        return gdb.parse_and_eval(eval_str)

-- CODE --


Matthieu



Le mardi 04 août 2009 à 09:41 +0100, Richard Ward a écrit :
> I have a patch I've been meaning to submit for a while, it adds
> gdb.Value.rtti_type which returns the run time type of an object (as a
> gdb.Type) using similar code to `set print object on'. The object can
> then be cast using gdb.Value.cast.
> I'm not at the right pc just now, but I'll try and mail it to this
> list later today.
> 
> Just in case it helps in the mean time, it is possible to look at the
> first 4 (8 on 64-bit) bytes of an object which has a vtable (which
> should be a pointer to the vtable), and run `addr2line -Cfe
> name-of-executable 0xBLAHBLAH' which should then say something like
> `0xBLAHBLAH: vtable for class FOO'. I think you can also find the
> vtable this way using nm or objdump, there may be a similar way to do
> this within gdb, but I wouldn't know how. Sorry if these instructions
> are rough/wrong, I'm at a non gnu/linux machine at the moment.
> 
> Richard Ward.
> 
> 
> 2009/8/3 Tom Tromey <tromey@redhat.com>:
> >>>>>> "Matthieu" == Matthieu VIAL <matthieu.vial@orange-ftgroup.com> writes:
> >
> > Matthieu> Sorry Value.cast seems to work but my problem is actually a bit more
> > Matthieu> complex.
> >
> > Ok.
> >
> > Matthieu> I need to compare pointers to the interface with pointers to
> > Matthieu> the base class. But these classes are not directly related so
> > Matthieu> Value.cast is useless. The solution is to up cast the base
> > Matthieu> class pointer to the final class and then down cast to the
> > Matthieu> interface. But i need run time type information for that. The
> > Matthieu> typeid operator doesn't seem to be available in archer.
> >
> > Nope.  The Python API just exposes things that already exist in gdb.
> > typeid is not implemented yet, there's a bug for it:
> >
> > http://sourceware.org/bugzilla/show_bug.cgi?id=9065
> >
> >
> > However, gdb does have "set print object on"... if this prints the right
> > thing for you, I think we could hook up the internal code to find the
> > "real object" as a Value method.
> >
> > Tom
> >

  reply	other threads:[~2009-08-04 11:40 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-31  7:21 Matthieu VIAL
2009-07-31 18:03 ` Tom Tromey
2009-08-03  9:20   ` Matthieu VIAL
2009-08-03 18:30     ` Tom Tromey
2009-08-04  8:42       ` Richard Ward
2009-08-04 11:40         ` Matthieu VIAL [this message]
2009-08-10 21:12         ` Tom Tromey

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=1249386015.2853.15.camel@pc1134.silicomp.fr \
    --to=matthieu.vial@orange-ftgroup.com \
    --cc=archer@sourceware.org \
    --cc=richard.j.ward1@googlemail.com \
    /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).