This is the mail archive of the
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
May be this kind of feature could be useful in archer ?
def find_class_offset(init, src, dst):
if str(src) == str(dst):
return [True, init]
for field in src.fields():
result = find_class_offset(init - field.bitpos / 8,
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 ==
src = cast_type.target()
src = cast_type
dst = value.type.target()
result = find_class_offset(0, src, dst)
if not result:
raise Exception('Invalid dynamic cast')
addr = int(str(value), 16)
addr += result
eval_str = '(\'%s\'*)0x%X' % (src.tag, addr)
-- CODE --
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 <firstname.lastname@example.org>:
> >>>>>> "Matthieu" == Matthieu VIAL <email@example.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