I'm debugging a C++ program with GDB.
I have a pointer to an object of certain class. The pointer is declared to be of some super class which is extended by several sub-classes.
There is no fields in the object to specify the precise class type of this object but some virtual functions (e.g. bool is_xxx()) are defined to tell the class type at runtime.
Is there some way to tell the precise class type of an object in GDB without calling these virtual functions. Calling such functions in GDB may generate confusing result when the program is multi-threaded.
GDB 7.11
As of GDB 7.11, GCC 5.3.1, Ubuntu 16.04, doing just:
on something compiled with:
may be enough as it already shows:
where
MyDerived1
is the current derived class we are looking for.But if you do in addition:
the output is even clearer and looks like:
This also affects other commands like:
which shows:
instead of:
In this case, there was no indication of the derived type without
set print object on
.whatis
is similarly affected:Test program:
Use
ptype
. If you use it by itself, you get the declared type of the pointer:To get the actual type of the object pointed to, set the "print object" variable:
On my system ptype or whatis also only show the obvious.
But printing the first entry of the vtable helped me:
Here the pObject pointed to a QMessageBox which is derived from QObject. This only works if vtable-entry points to a method that is overridden by the derived class.
See also: Print C++ vtables using GDB
Edit: Printing only the pointer to the vtable works more reliable (though the output uses the mangled name and is not so readable):
You do not need to call the virtual functions, you can just see the address of the virtual function or vtable. Another way is to use RTTI