public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug python/29011] New: Python Value.dynamic_cast does not work as expected
@ 2022-03-30 23:55 evan_l00 at qq dot com
  2022-03-31 15:38 ` [Bug python/29011] " ssbssa at sourceware dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: evan_l00 at qq dot com @ 2022-03-30 23:55 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=29011

            Bug ID: 29011
           Summary: Python Value.dynamic_cast does not work as expected
           Product: gdb
           Version: 11.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: python
          Assignee: unassigned at sourceware dot org
          Reporter: evan_l00 at qq dot com
  Target Milestone: ---

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

I have asked on the stackoverflow in
https://stackoverflow.com/questions/71644182/gdb-python-api-dynamic-cast-not-work-as-expected,
but received a comment that says this might be a bug in the gdb. So I open this
ticket.

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)?

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-12-11 14:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-30 23:55 [Bug python/29011] New: Python Value.dynamic_cast does not work as expected evan_l00 at qq dot com
2022-03-31 15:38 ` [Bug python/29011] " ssbssa at sourceware dot org
2022-03-31 17:09 ` simark at simark dot ca
2022-04-02 16:00 ` tromey at sourceware dot org
2023-07-31 14:45 ` [Bug c++/29011] " tromey at sourceware dot org
2023-12-11 14:53 ` cvs-commit at gcc dot gnu.org
2023-12-11 14:55 ` ssbssa at sourceware dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).