Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: return the object's address for dynamic_cast<void *>
@ 2026-11-05 11:08 Claudiu Zissulescu
  2026-11-05 16:36 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Claudiu Zissulescu @ 2026-11-05 11:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro, Lancelot.Six, Claudiu Zissulescu-Ianculescu

From: Claudiu Zissulescu-Ianculescu <claudiu.zissulescu-ianculescu@amd.com>

value_dynamic_cast computes the address of the most derived object, but
for a cast to "void *" it then returns value_at_lazy (type, addr): the
"void *" stored at that address, i.e. the first word of the object.  For
a polymorphic class that is the vtable pointer:

  (gdb) print lrp
  $1 = (LR *) 0x416020
  (gdb) print dynamic_cast<void *> (rp)
  $2 = (void *) 0x4012f0 <vtable for LR+16>

The cast to "void *" must give the address itself.  Use
value_from_pointer, as value_addr does to make a pointer from an
address.

gdb.cp/casts.exp only checked dynamic_cast<void *> on a null pointer,
which returns early.  Add checks through the most derived class and
through both of its bases, one of which is at a non-zero offset.

Tested on x86_64-linux (GCC): gdb.cp/casts.exp 70 PASS / 3 FAIL before
(the new checks), 73 PASS after.
No regressions in gdb.cp/casts.exp, debug-expr.exp,
rvalue-ref-casts.exp, rtti.exp, typeid.exp, class2.exp, virtbase.exp,
derivation.exp and gdb.python/py-value.exp (Value.dynamic_cast uses
the same function): 615 PASS, 0 FAIL.

This is a complementary issue to the Bugzilla's gdb/29011 bug.
---
 gdb/testsuite/gdb.cp/casts.exp | 6 ++++++
 gdb/valops.c                   | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp
index 67246ac7b5c..1291af33be6 100644
--- a/gdb/testsuite/gdb.cp/casts.exp
+++ b/gdb/testsuite/gdb.cp/casts.exp
@@ -213,6 +213,12 @@ gdb_test "print &dynamic_cast<VirtualRight &> (*vlr) == vr" " = true"
 gdb_test "print &dynamic_cast<VirtualRight &> (*vl) == vr" " = true"
 gdb_test "print &dynamic_cast<VirtualRight &> (*vr) == vr" " = true"
 
+# dynamic_cast<void *> gives the address of the most derived object,
+# also through a base at a non-zero offset.
+gdb_test "print dynamic_cast<void *> (vlr) == vlr" " = true"
+gdb_test "print dynamic_cast<void *> (vl) == vlr" " = true"
+gdb_test "print dynamic_cast<void *> (vr) == vlr" " = true"
+
 if {[prepare_for_testing "failed to prepare" ${testfile}03 $srcfile2 \
 			 {debug c++ additional_flags=-std=c++03}]} {
     return
diff --git a/gdb/valops.c b/gdb/valops.c
index 55f891c7e72..52d0bb98198 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -945,7 +945,7 @@ value_dynamic_cast (struct type *type, struct value *arg)
      most-derived object.  */
   if (resolved_type->code () == TYPE_CODE_PTR
       && resolved_type->target_type ()->code () == TYPE_CODE_VOID)
-    return value_at_lazy (type, addr);
+    return value_from_pointer (type, addr);
 
   tem = value_at (resolved_type->target_type (), addr);
   type = (is_ref
-- 
2.56.0


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

* Re: [PATCH] gdb: return the object's address for dynamic_cast<void *>
  2026-11-05 11:08 [PATCH] gdb: return the object's address for dynamic_cast<void *> Claudiu Zissulescu
@ 2026-11-05 16:36 ` Tom Tromey
  2026-11-06 12:01   ` Zissulescu-Ianculescu, Claudiu
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2026-11-05 16:36 UTC (permalink / raw)
  To: Claudiu Zissulescu; +Cc: gdb-patches, pedro, Lancelot.Six

>>>>> Claudiu Zissulescu <claudiu.zissulescu-ianculescu@amd.com> writes:

> From: Claudiu Zissulescu-Ianculescu <claudiu.zissulescu-ianculescu@amd.com>
> value_dynamic_cast computes the address of the most derived object, but
> for a cast to "void *" it then returns value_at_lazy (type, addr): the
> "void *" stored at that address, i.e. the first word of the object.  For
> a polymorphic class that is the vtable pointer:

Thanks for the patch.  This is ok.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

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

* Re: [PATCH] gdb: return the object's address for dynamic_cast<void *>
  2026-11-05 16:36 ` Tom Tromey
@ 2026-11-06 12:01   ` Zissulescu-Ianculescu, Claudiu
  0 siblings, 0 replies; 3+ messages in thread
From: Zissulescu-Ianculescu, Claudiu @ 2026-11-06 12:01 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, pedro, Six, Lancelot

[-- Attachment #1: Type: text/plain, Size: 1261 bytes --]

AMD General

Patch pushed.
Thank you for your review,
Claudiu
________________________________
From: Tom Tromey <tom@tromey.com>
Sent: Thursday, November 5, 2026 6:36 PM
To: Zissulescu-Ianculescu, Claudiu <Claudiu.Zissulescu-Ianculescu@amd.com>
Cc: gdb-patches@sourceware.org <gdb-patches@sourceware.org>; pedro@palves.net <pedro@palves.net>; Six, Lancelot <Lancelot.Six@amd.com>
Subject: Re: [PATCH] gdb: return the object's address for dynamic_cast<void *>

[You don't often get email from tom@tromey.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.


>>>>> Claudiu Zissulescu <claudiu.zissulescu-ianculescu@amd.com> writes:

> From: Claudiu Zissulescu-Ianculescu <claudiu.zissulescu-ianculescu@amd.com>
> value_dynamic_cast computes the address of the most derived object, but
> for a cast to "void *" it then returns value_at_lazy (type, addr): the
> "void *" stored at that address, i.e. the first word of the object.  For
> a polymorphic class that is the vtable pointer:

Thanks for the patch.  This is ok.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

[-- Attachment #2: Type: text/html, Size: 2765 bytes --]

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

end of thread, other threads:[~2026-11-06 12:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 11:08 [PATCH] gdb: return the object's address for dynamic_cast<void *> Claudiu Zissulescu
2026-11-05 16:36 ` Tom Tromey
2026-11-06 12:01   ` Zissulescu-Ianculescu, Claudiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox