This is the same question as How to determine if an object is an instance of certain derived C++ class from a pointer to a base class in GDB? but to do the same from the gdb extension in Python.
Given the same example shown in an answer to that question:
#include <iostream>
class MyBase {
public:
virtual int myMethod() = 0;
};
class MyDerived1 : public MyBase {
public:
virtual int myMethod() { return 1; }
};
class MyDerived2 : public MyBase {
public:
virtual int myMethod() { return 2; }
};
int main() {
MyBase *myBase;
MyDerived1 myDerived1;
MyDerived2 myDerived2;
myBase = &myDerived1;
std::cout << myBase->myMethod() << std::endl;
myBase = &myDerived2;
std::cout << myBase->myMethod() << std::endl;
}
As mentioned in the response, I can do:
(gdb) b 24
Breakpoint 1 at 0x400a19: file derived.cc, line 24.
(gdb) run
Starting program: /home/ubuntu/c-test/derived/derived
1
Breakpoint 1, main () at derived.cc:24
24 myBase = &myDerived2;
(gdb) p *myBase
$1 = {_vptr.MyBase = 0x400c00 <vtable for MyDerived1+16>}
(gdb) set print object on
(gdb) p *myBase
$2 = (MyDerived1) {<MyBase> = {_vptr.MyBase = 0x400c00 <vtable for MyDerived1+16>}, <No data fields>}
So I can see that this is an instance of MyDerived1. But in Python, I get the following.
(gdb) python-interactive
>>> myBase = gdb.parse_and_eval("myBase")
>>> str(myBase.type)
'MyBase *'
How can I obtain the actual class in Python extension? The real objective here is to be able to cast the instance to the right derived class so I can pretty print the information specific to that derived class.