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

* [Bug python/29011] Python Value.dynamic_cast does not work as expected
  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 ` ssbssa at sourceware dot org
  2022-03-31 17:09 ` simark at simark dot ca
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ssbssa at sourceware dot org @ 2022-03-31 15:38 UTC (permalink / raw)
  To: gdb-prs

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

Hannes Domani <ssbssa at sourceware dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ssbssa at sourceware dot org

--- Comment #1 from Hannes Domani <ssbssa at sourceware dot org> ---
You also get the same result without Python:

(gdb) p p
$1 = (Derived *) 0x5d20c0
(gdb) p pb
$2 = (Base *) 0x5d20c0
(gdb) p pb2
$3 = (Base2 *) 0x5d20d0
(gdb) p dynamic_cast<Derived*>(p)
$4 = (Derived *) 0x13f4e8800 <vtable for Derived+16>
(gdb) p dynamic_cast<Derived*>(pb)
$5 = (Derived *) 0x13f4e8800 <vtable for Derived+16>
(gdb) p dynamic_cast<Derived*>(pb2)
$6 = (Derived *) 0x13f4e8800 <vtable for Derived+16>
(gdb) p dynamic_cast<Base*>(p)
$7 = (Base *) 0x5d20c0
(gdb) p dynamic_cast<Base*>(pb)
$8 = (Base *) 0x13f4e8800 <vtable for Derived+16>
(gdb) p dynamic_cast<Base*>(pb2)
$9 = (Base *) 0x5d20c0
(gdb) p dynamic_cast<Base2*>(p)
$10 = (Base2 *) 0x5d20d0
(gdb) p dynamic_cast<Base2*>(pb)
$11 = (Base2 *) 0x5d20d0
(gdb) p dynamic_cast<Base2*>(pb2)
$12 = (Base2 *) 0x13f4e8800 <vtable for Derived+16>

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

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

* [Bug python/29011] Python Value.dynamic_cast does not work as expected
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: simark at simark dot ca @ 2022-03-31 17:09 UTC (permalink / raw)
  To: gdb-prs

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

Simon Marchi <simark at simark dot ca> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simark at simark dot ca

--- Comment #2 from Simon Marchi <simark at simark dot ca> ---
Looks similar to https://sourceware.org/bugzilla/show_bug.cgi?id=28907

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

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

* [Bug python/29011] Python Value.dynamic_cast does not work as expected
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: tromey at sourceware dot org @ 2022-04-02 16:00 UTC (permalink / raw)
  To: gdb-prs

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

Tom Tromey <tromey at sourceware dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tromey at sourceware dot org

--- Comment #3 from Tom Tromey <tromey at sourceware dot org> ---
(In reply to Simon Marchi from comment #2)
> Looks similar to https://sourceware.org/bugzilla/show_bug.cgi?id=28907

That one is about casts without RTTI, which is done differently.

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

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

* [Bug c++/29011] Python Value.dynamic_cast does not work as expected
  2022-03-30 23:55 [Bug python/29011] New: Python Value.dynamic_cast does not work as expected evan_l00 at qq dot com
                   ` (2 preceding siblings ...)
  2022-04-02 16:00 ` tromey at sourceware dot org
@ 2023-07-31 14:45 ` 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
  5 siblings, 0 replies; 7+ messages in thread
From: tromey at sourceware dot org @ 2023-07-31 14:45 UTC (permalink / raw)
  To: gdb-prs

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

Tom Tromey <tromey at sourceware dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|python                      |c++

--- Comment #4 from Tom Tromey <tromey at sourceware dot org> ---
Recategorizing since it happens without Python

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

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

* [Bug c++/29011] Python Value.dynamic_cast does not work as expected
  2022-03-30 23:55 [Bug python/29011] New: Python Value.dynamic_cast does not work as expected evan_l00 at qq dot com
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-12-11 14:53 UTC (permalink / raw)
  To: gdb-prs

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

--- Comment #5 from Sourceware Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Hannes Domani <ssbssa@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0238b543f3c294fc8512021a40b708e8ddc72bb2

commit 0238b543f3c294fc8512021a40b708e8ddc72bb2
Author: Hannes Domani <ssbssa@yahoo.de>
Date:   Tue Mar 29 20:05:06 2022 +0200

    Fix dynamic_cast

    PR29011 notes that dynamic_cast does not work correctly if
    classes with virtual methods are involved, some of the results
    wrongly point into the vtable of the derived class:
    ```
    (gdb) p vlr
    $1 = (VirtualLeftRight *) 0x162240
    (gdb) p vl
    $2 = (VirtualLeft *) 0x162240
    (gdb) p vr
    $3 = (VirtualRight *) 0x162250
    (gdb) p dynamic_cast<VirtualLeftRight*>(vlr)
    $4 = (VirtualLeftRight *) 0x13fab89b0 <vtable for VirtualLeftRight+16>
    (gdb) p dynamic_cast<VirtualLeftRight*>(vl)
    $5 = (VirtualLeftRight *) 0x13fab89b0 <vtable for VirtualLeftRight+16>
    (gdb) p dynamic_cast<VirtualLeftRight*>(vr)
    $6 = (VirtualLeftRight *) 0x13fab89b0 <vtable for VirtualLeftRight+16>
    (gdb) p dynamic_cast<VirtualLeft*>(vlr)
    $7 = (VirtualLeft *) 0x162240
    (gdb) p dynamic_cast<VirtualLeft*>(vl)
    $8 = (VirtualLeft *) 0x13fab89b0 <vtable for VirtualLeftRight+16>
    (gdb) p dynamic_cast<VirtualLeft*>(vr)
    $9 = (VirtualLeft *) 0x162240
    (gdb) p dynamic_cast<VirtualRight*>(vlr)
    $10 = (VirtualRight *) 0x162250
    (gdb) p dynamic_cast<VirtualRight*>(vl)
    $11 = (VirtualRight *) 0x162250
    (gdb) p dynamic_cast<VirtualRight*>(vr)
    $12 = (VirtualRight *) 0x13fab89b0 <vtable for VirtualLeftRight+16>
    ```

    For the cases where the dynamic_cast type is the same as the
    original type, it used the ARG value for the result, which in
    case of pointer types was already the dereferenced value.

    And the TEM value at the value address was created with the
    pointer/reference type, not the actual class type.

    With these fixed, the dynamic_cast results make more sense:
    ```
    (gdb) p vlr
    $1 = (VirtualLeftRight *) 0x692240
    (gdb) p vl
    $2 = (VirtualLeft *) 0x692240
    (gdb) p vr
    $3 = (VirtualRight *) 0x692250
    (gdb) p dynamic_cast<VirtualLeftRight*>(vlr)
    $4 = (VirtualLeftRight *) 0x692240
    (gdb) p dynamic_cast<VirtualLeftRight*>(vl)
    $5 = (VirtualLeftRight *) 0x692240
    (gdb) p dynamic_cast<VirtualLeftRight*>(vr)
    $6 = (VirtualLeftRight *) 0x692240
    (gdb) p dynamic_cast<VirtualLeft*>(vlr)
    $7 = (VirtualLeft *) 0x692240
    (gdb) p dynamic_cast<VirtualLeft*>(vl)
    $8 = (VirtualLeft *) 0x692240
    (gdb) p dynamic_cast<VirtualLeft*>(vr)
    $9 = (VirtualLeft *) 0x692240
    (gdb) p dynamic_cast<VirtualRight*>(vlr)
    $10 = (VirtualRight *) 0x692250
    (gdb) p dynamic_cast<VirtualRight*>(vl)
    $11 = (VirtualRight *) 0x692250
    (gdb) p dynamic_cast<VirtualRight*>(vr)
    $12 = (VirtualRight *) 0x692250
    ```

    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29011
    Approved-By: Tom Tromey <tom@tromey.com>

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

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

* [Bug c++/29011] Python Value.dynamic_cast does not work as expected
  2022-03-30 23:55 [Bug python/29011] New: Python Value.dynamic_cast does not work as expected evan_l00 at qq dot com
                   ` (4 preceding siblings ...)
  2023-12-11 14:53 ` cvs-commit at gcc dot gnu.org
@ 2023-12-11 14:55 ` ssbssa at sourceware dot org
  5 siblings, 0 replies; 7+ messages in thread
From: ssbssa at sourceware dot org @ 2023-12-11 14:55 UTC (permalink / raw)
  To: gdb-prs

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

Hannes Domani <ssbssa at sourceware dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
   Target Milestone|---                         |15.1
         Resolution|---                         |FIXED

--- Comment #6 from Hannes Domani <ssbssa at sourceware dot org> ---
Fixed.

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