This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
Re: How to cast a pointer to a base class with multiple inheritance
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
> >