From: Weimin Pan <weimin.pan@oracle.com>
To: gdb-patches@sourceware.org
Subject: [PATCH PR gdb/16841] virtual inheritance via typedef cannot find base
Date: Fri, 20 Jul 2018 23:50:00 -0000 [thread overview]
Message-ID: <1532128565-75923-1-git-send-email-weimin.pan@oracle.com> (raw)
Finding data member in virtual base class
This patch fixes the original problem - printing member in a virtual base,
using various expressions, do not yield the same value. Simple test case
below demonstrates the problem:
% cat t.cc
struct base { int i; };
typedef base tbase;
struct derived: virtual tbase { void func() { } };
int main() { derived().func(); }
% g++ -g t.cc
% gdb a.out
(gdb) break derived::func
(gdb) run
(gdb) p i
$1 = 0
(gdb) p base::i
$2 = 0
(gdb) p derived::base::i
$3 = 0
(gdb) p derived::i
$4 = 4196392
To fix the problem, the virtual-base offset, relative to its derived class,
needs to be fetched, via baseclass_offset(), and used in calculating the
address of its data member in value_struct_elt_for_reference().
Tested on amd64-linux-gnu. No regressions.
---
gdb/ChangeLog | 6 +++++
gdb/testsuite/ChangeLog | 6 +++++
gdb/testsuite/gdb.cp/virtbase2.cc | 28 ++++++++++++++++++++++++++
gdb/testsuite/gdb.cp/virtbase2.exp | 38 ++++++++++++++++++++++++++++++++++++
gdb/valops.c | 17 +++++++++++++++-
5 files changed, 94 insertions(+), 1 deletions(-)
create mode 100644 gdb/testsuite/gdb.cp/virtbase2.cc
create mode 100644 gdb/testsuite/gdb.cp/virtbase2.exp
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index c47c111..1531ff1 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-07-20 Weimin Pan <weimin.pan@oracle.com>
+
+ PR gdb/16841
+ * valops.c (value_struct_elt_for_reference): Calculate virtual
+ baseclass offset, relative to its derived class.
+
2018-06-29 Pedro Alves <palves@redhat.com>
* gdb/amd64-tdep.h (amd64_create_target_description): Add
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 93c849c..1577a54 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2018-07-20 Weimin Pan <weimin.pan@oracle.com>
+
+ PR gdb/16841
+ * gdb.cp/virtbase2.cc: New file.
+ * gdb.cp/virtbase2.exp: New file.
+
2018-06-29 Pedro Alves <palves@redhat.com>
* gdb.threads/names.exp: Adjust expected "info threads" output.
diff --git a/gdb/testsuite/gdb.cp/virtbase2.cc b/gdb/testsuite/gdb.cp/virtbase2.cc
new file mode 100644
index 0000000..33753b3
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/virtbase2.cc
@@ -0,0 +1,28 @@
+/* This test script is part of GDB, the GNU debugger.
+
+ Copyright 2018 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+struct left { int i; };
+struct right {int j;};
+struct derived: virtual left, virtual right
+{
+ void func() { }
+};
+
+int main()
+{
+ derived().func();
+}
diff --git a/gdb/testsuite/gdb.cp/virtbase2.exp b/gdb/testsuite/gdb.cp/virtbase2.exp
new file mode 100644
index 0000000..01dd298
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/virtbase2.exp
@@ -0,0 +1,38 @@
+# Copyright 2018 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Make sure printing virtual base class data member correctly (PR16841)
+
+if { [skip_cplus_tests] } { continue }
+
+standard_testfile .cc
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
+ return -1
+}
+
+if {![runto_main]} then {
+ perror "couldn't run to main"
+ continue
+}
+
+gdb_breakpoint "derived::func"
+gdb_continue_to_breakpoint "continue to derived::func"
+gdb_test "print j" " = 0" "j in base class right"
+gdb_test "print i" " = 0" "i in base class left"
+gdb_test "print derived::j" " = 0" "j in base class right"
+gdb_test "print derived::i" " = 0" "i in base class left"
+gdb_test "print derived::right::j" " = 0" "j in base class right"
+gdb_test "print derived::left::i" " = 0" "i in base class left"
diff --git a/gdb/valops.c b/gdb/valops.c
index 9bdbf22..f0a8118 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3385,6 +3385,7 @@ value_struct_elt_for_reference (struct type *domain, int offset,
struct value *ptr;
long mem_offset;
struct type *type, *tmp;
+ int index;
ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
type = check_typedef (value_type (ptr));
@@ -3392,7 +3393,21 @@ value_struct_elt_for_reference (struct type *domain, int offset,
&& TYPE_CODE (type) == TYPE_CODE_MEMBERPTR);
tmp = lookup_pointer_type (TYPE_SELF_TYPE (type));
v = value_cast_pointers (tmp, v, 1);
- mem_offset = value_as_long (ptr);
+ for (index = 0; index < TYPE_NFIELDS (domain); index++)
+ {
+ if (TYPE_FIELDS (domain)[index].type == curtype)
+ break;
+ }
+ if (index != TYPE_NFIELDS (domain)
+ && BASETYPE_VIA_VIRTUAL (domain, index))
+ {
+ const gdb_byte *adr = value_contents_for_printing (ptr);
+ mem_offset = baseclass_offset (domain, index, adr,
+ value_offset (ptr),
+ value_as_long (v), ptr);
+ }
+ else
+ mem_offset = value_as_long (ptr);
tmp = lookup_pointer_type (TYPE_TARGET_TYPE (type));
result = value_from_pointer (tmp,
value_as_long (v) + mem_offset);
--
1.7.1
next reply other threads:[~2018-07-20 23:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-20 23:50 Weimin Pan [this message]
2018-07-24 22:18 ` Simon Marchi
2018-07-24 23:26 ` Weimin Pan
2018-07-29 2:28 ` Tom Tromey
2018-07-30 17:58 ` Wei-min Pan
2018-07-31 15:42 ` Tom Tromey
-- strict thread matches above, loose matches on Subject: below --
2018-06-15 23:45 Weimin Pan
2018-06-17 1:06 ` Simon Marchi
2018-06-18 16:12 ` Wei-min Pan
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=1532128565-75923-1-git-send-email-weimin.pan@oracle.com \
--to=weimin.pan@oracle.com \
--cc=gdb-patches@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