'gdb python API dynamic cast not work as expected

The document in this link says that Value.dynamic_cast works like dynamic cast in C++, but in the following experiement, it shows that the Value.cast works like dynamic cast in C++, and Value.dynamic_cast gives some value I don't understand.

This is the sample code


class Base {
public:
  virtual void p() {}
  int i =3;
};
class Base2 {
public:
  virtual void f() {}
  int i2 =4;
};
class Derived: public Base, public Base2{
  public:
  void p() override{}
  int t =4;
};
int main() {
   auto* p = new Derived();
   Base2* pb2 = p;
   Base* pb = p;
   return 0;
}

Run the code, and print the values of p, pb, and pb2

(gdb) p p
$1 = (Derived *) 0x613c20
(gdb) p pb // 
$2 = (Base *) 0x613c20
(gdb) p pb2
$3 = (Base2 *) 0x613c30

Then use python script to print the value as follows, the Value.dynamic_cast will give

(gdb) py print(ppb2.dynamic_cast(ppb2.dynamic_type))
0x400808 <vtable for Derived+16>

And the Value.cast gives the correct derived pointer,

(gdb) py print(ppb2.cast(ppb2.dynamic_type))
0x613c20

pp, ppb, and ppb2 are the corresponding values but in Python env, got from the following code.

(gdb) py pp = gdb.parse_and_eval('p')
(gdb) py print(pp)
0x613c20
(gdb) py ppb2 = gdb.parse_and_eval('pb2')
(gdb) py print(ppb2)
0x613c30
(gdb) py print(ppb2.cast(ppb2.dynamic_type))
0x613c20
(gdb) py print(ppb2.dynamic_cast(ppb2.dynamic_type))
0x400808 <vtable for Derived+16>

Why Value.dynamic_cast will give 0x400808 <vtable for Derived+16> instead of 0x613c20 (the derived pointer)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source