Determining type of an object at debug time.

Daniel Berlin dan@cgsoftware.com
Mon Mar 13 17:02:00 GMT 2000


<SNIP>
> So if you only have a void*, you could do some guess-work to find
out
> what the dynamic type is. You could scan the fields of the object to
> find something that looks like a vtable pointer. On most systems, you
> can tell whether it is a vtable pointer if it has an external symbol.
> In case they are stripped, it should live in the text segment, and so
> on.
>

Finding the vpointer is easy, we always know where it is.
Given that, i realized i could do just what you described a few hours ago 
during class, and implemented it.
> I feel that it would pay-off to try to be smart, as long as nothings
> breaks in case you guess wrong.
> 
> I think this would be a very important feature, so let me know if you
> have further questions.
> 

Well, then, i have some good news.
I have it working.
Even with multiple inheritance (It figures out the offset properly)
It'll determine the type, and print it (IE print foo2), as if it was that
type, but just as in C++, you won't be able to directly access the
members without explicitly casting it to the derived type.
Although it's a little odd to see:
31              dan=new daniel();
(gdb) p dan
$6 = (daniel *) 0x8051030
(gdb) n
33              dan=new bob();
(gdb)
34              dan=new george2();
(gdb) p dan
$7 = (bob *) 0x8051040
(gdb) n
35 }
(gdb) p dan
$8 = (george2 *) 0x8050064
(gdb)
(notice dan keeps changing type. ALl those classes are inherited from dan,
george2 is multiple inherited from another class and a subclass of dan,
just for corner case sake)
If you want to see what printing those pointers looks like, i just sent
the output to the gdb mailing list, so i don't clutter up this one.

Thanks for all your help,
Dan
>
Regards, > Martin
> 



More information about the Gcc mailing list