public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "evan_l00 at qq dot com" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug python/29011] New: Python Value.dynamic_cast does not work as expected
Date: Wed, 30 Mar 2022 23:55:09 +0000	[thread overview]
Message-ID: <bug-29011-4717@http.sourceware.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-03-30 23:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-30 23:55 evan_l00 at qq dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-29011-4717@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).