public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 07/11] Add support for variable field offsets
Date: Wed,  8 Apr 2020 11:54:48 -0600	[thread overview]
Message-ID: <20200408175452.30637-8-tromey@adacore.com> (raw)
In-Reply-To: <20200408175452.30637-1-tromey@adacore.com>

In Ada, a field can have a variable offset.  This patch adds support
for this case to gdb, using the existing dynamic type resolution code.

Doing just this, though, would break C++ virtual base handling.

It turns out that virtual base handling only worked by the ugliest of
hacks.  In particular, the DWARF reader would call decode_locdesc for
a virtual base location.  Here's an example of such an expression from
gdb's m-static test case:

    <241>   DW_AT_data_member_location: 6 byte block: 12 6 48 1c 6 22 	(DW_OP_dup; DW_OP_deref; DW_OP_lit24; DW_OP_minus; DW_OP_deref; DW_OP_plus)

When examining this, decode_locdesc would treat DW_OP_deref as a no-op
and compute some answer (here, -24).  This would be stored as the
offset.

Later, in gnu-v3-abi.c, the real offset would be computed by digging
around in the vtable.

This patch cleans up this area.  In particular, it now evaluates the
location expression on demand.

Note there is a new FIXME in gnu-v3-abi.c.  I think some of the
callers are incorrect here, and have only worked because this member
is unused.  I will file a bug for this.  I didn't fix this problem in
this series because I felt it was already too complex.

gdb/ChangeLog
2020-04-08  Tom Tromey  <tromey@adacore.com>

	* dwarf2/read.c (handle_data_member_location): New overload.
	(dwarf2_add_field): Use it.
	(decode_locdesc): Add "computed" parameter.  Update comment.
	* gdbtypes.c (is_dynamic_type_internal): Also look for
	FIELD_LOC_KIND_DWARF_BLOCK.
	(resolve_dynamic_struct): Handle FIELD_LOC_KIND_DWARF_BLOCK.
	* gdbtypes.c (is_dynamic_type_internal): Add special case for C++
	virtual base classes.
	* gnu-v3-abi.c (gnuv3_baseclass_offset): Handle
	FIELD_LOC_KIND_DWARF_BLOCK.

gdb/testsuite/ChangeLog
2020-04-08  Tom Tromey  <tromey@adacore.com>

	* gdb.ada/variant.exp: Add dynamic field offset tests.
	* gdb.ada/variant/pck.ads (Nested_And_Variable): New type.
	* gdb.ada/variant/pkg.adb: Add new variables.
---
 gdb/ChangeLog                         |  13 +++
 gdb/dwarf2/read.c                     | 145 ++++++++++++++++++--------
 gdb/gdbtypes.c                        |  39 ++++++-
 gdb/gnu-v3-abi.c                      |  26 +++++
 gdb/testsuite/ChangeLog               |   6 ++
 gdb/testsuite/gdb.ada/variant.exp     |   6 ++
 gdb/testsuite/gdb.ada/variant/pck.ads |  17 +++
 gdb/testsuite/gdb.ada/variant/pkg.adb |  11 ++
 8 files changed, 218 insertions(+), 45 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 31f732d29a3..9d78527b56e 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1435,7 +1435,8 @@ static const char *namespace_name (struct die_info *die,
 
 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
 
-static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
+static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
+				 bool * = nullptr);
 
 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
 						       struct dwarf2_cu *);
@@ -14169,6 +14170,53 @@ handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
   return 0;
 }
 
+/* Look for DW_AT_data_member_location and store the results in FIELD.  */
+
+static void
+handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
+			     struct field *field)
+{
+  struct attribute *attr;
+
+  attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
+  if (attr != NULL)
+    {
+      if (attr->form_is_constant ())
+	{
+	  LONGEST offset = attr->constant_value (0);
+	  SET_FIELD_BITPOS (*field, offset * bits_per_byte);
+	}
+      else if (attr->form_is_section_offset ())
+	dwarf2_complex_location_expr_complaint ();
+      else if (attr->form_is_block ())
+	{
+	  bool handled;
+	  CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
+	  if (handled)
+	    SET_FIELD_BITPOS (*field, offset * bits_per_byte);
+	  else
+	    {
+	      struct objfile *objfile
+		= cu->per_cu->dwarf2_per_objfile->objfile;
+	      struct dwarf2_locexpr_baton *dlbaton
+		= XOBNEW (&objfile->objfile_obstack,
+			  struct dwarf2_locexpr_baton);
+	      dlbaton->data = DW_BLOCK (attr)->data;
+	      dlbaton->size = DW_BLOCK (attr)->size;
+	      /* When using this baton, we want to compute the address
+		 of the field, not the value.  This is why
+		 is_reference is set to false here.  */
+	      dlbaton->is_reference = false;
+	      dlbaton->per_cu = cu->per_cu;
+
+	      SET_FIELD_DWARF_BLOCK (*field, dlbaton);
+	    }
+	}
+      else
+	dwarf2_complex_location_expr_complaint ();
+    }
+}
+
 /* Add an aggregate field to the field list.  */
 
 static void
@@ -14213,8 +14261,6 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
 
   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
     {
-      LONGEST offset;
-
       /* Data member other than a C++ static data member.  */
 
       /* Get type of field.  */
@@ -14234,8 +14280,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
 	}
 
       /* Get bit offset of field.  */
-      if (handle_data_member_location (die, cu, &offset))
-	SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
+      handle_data_member_location (die, cu, fp);
       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
       if (attr != nullptr)
 	{
@@ -14344,11 +14389,8 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
     }
   else if (die->tag == DW_TAG_inheritance)
     {
-      LONGEST offset;
-
       /* C++ base class field.  */
-      if (handle_data_member_location (die, cu, &offset))
-	SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
+      handle_data_member_location (die, cu, fp);
       FIELD_BITSIZE (*fp) = 0;
       FIELD_TYPE (*fp) = die_type (die, cu);
       FIELD_NAME (*fp) = TYPE_NAME (fp->type);
@@ -22612,27 +22654,13 @@ read_signatured_type (struct signatured_type *sig_type)
 
 /* Decode simple location descriptions.
    Given a pointer to a dwarf block that defines a location, compute
-   the location and return the value.
-
-   NOTE drow/2003-11-18: This function is called in two situations
-   now: for the address of static or global variables (partial symbols
-   only) and for offsets into structures which are expected to be
-   (more or less) constant.  The partial symbol case should go away,
-   and only the constant case should remain.  That will let this
-   function complain more accurately.  A few special modes are allowed
-   without complaint for global variables (for instance, global
-   register values and thread-local values).
-
-   A location description containing no operations indicates that the
-   object is optimized out.  The return value is 0 for that case.
-   FIXME drow/2003-11-16: No callers check for this case any more; soon all
-   callers will only want a very basic result and this can become a
-   complaint.
-
-   Note that stack[0] is unused except as a default error return.  */
+   the location and return the value.  If COMPUTED is non-null, it is
+   set to true to indicate that decoding was successful, and false
+   otherwise.  If COMPUTED is null, then this function may emit a
+   complaint.  */
 
 static CORE_ADDR
-decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
+decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
 {
   struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
   size_t i;
@@ -22643,6 +22671,9 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
   unsigned int bytes_read, unsnd;
   gdb_byte op;
 
+  if (computed != nullptr)
+    *computed = false;
+
   i = 0;
   stacki = 0;
   stack[stacki] = 0;
@@ -22722,7 +22753,12 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 	case DW_OP_reg31:
 	  stack[++stacki] = op - DW_OP_reg0;
 	  if (i < size)
-	    dwarf2_complex_location_expr_complaint ();
+	    {
+	      if (computed == nullptr)
+		dwarf2_complex_location_expr_complaint ();
+	      else
+		return 0;
+	    }
 	  break;
 
 	case DW_OP_regx:
@@ -22730,7 +22766,12 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 	  i += bytes_read;
 	  stack[++stacki] = unsnd;
 	  if (i < size)
-	    dwarf2_complex_location_expr_complaint ();
+	    {
+	      if (computed == nullptr)
+		dwarf2_complex_location_expr_complaint ();
+	      else
+		return 0;
+	    }
 	  break;
 
 	case DW_OP_addr:
@@ -22812,7 +22853,12 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 	     global symbols, although the variable's address will be bogus
 	     in the psymtab.  */
 	  if (i < size)
-	    dwarf2_complex_location_expr_complaint ();
+	    {
+	      if (computed == nullptr)
+		dwarf2_complex_location_expr_complaint ();
+	      else
+		return 0;
+	    }
 	  break;
 
         case DW_OP_GNU_push_tls_address:
@@ -22826,11 +22872,18 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 	     non-zero to not look as a variable garbage collected by linker
 	     which have DW_OP_addr 0.  */
 	  if (i < size)
-	    dwarf2_complex_location_expr_complaint ();
+	    {
+	      if (computed == nullptr)
+		dwarf2_complex_location_expr_complaint ();
+	      else
+		return 0;
+	    }
 	  stack[stacki]++;
           break;
 
 	case DW_OP_GNU_uninit:
+	  if (computed != nullptr)
+	    return 0;
 	  break;
 
 	case DW_OP_addrx:
@@ -22842,16 +22895,17 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
 	  break;
 
 	default:
-	  {
-	    const char *name = get_DW_OP_name (op);
+	  if (computed == nullptr)
+	    {
+	      const char *name = get_DW_OP_name (op);
 
-	    if (name)
-	      complaint (_("unsupported stack op: '%s'"),
-			 name);
-	    else
-	      complaint (_("unsupported stack op: '%02x'"),
-			 op);
-	  }
+	      if (name)
+		complaint (_("unsupported stack op: '%s'"),
+			   name);
+	      else
+		complaint (_("unsupported stack op: '%02x'"),
+			   op);
+	    }
 
 	  return (stack[stacki]);
 	}
@@ -22860,16 +22914,21 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          outside of the allocated space.  Also enforce minimum>0.  */
       if (stacki >= ARRAY_SIZE (stack) - 1)
 	{
-	  complaint (_("location description stack overflow"));
+	  if (computed == nullptr)
+	    complaint (_("location description stack overflow"));
 	  return 0;
 	}
 
       if (stacki <= 0)
 	{
-	  complaint (_("location description stack underflow"));
+	  if (computed == nullptr)
+	    complaint (_("location description stack underflow"));
 	  return 0;
 	}
     }
+
+  if (computed != nullptr)
+    *computed = true;
   return (stack[stacki]);
 }
 
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 4491c1832f1..01d838381ba 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -2015,10 +2015,27 @@ is_dynamic_type_internal (struct type *type, int top_level)
       {
 	int i;
 
+	bool is_cplus = HAVE_CPLUS_STRUCT (type);
+
 	for (i = 0; i < TYPE_NFIELDS (type); ++i)
-	  if (!field_is_static (&TYPE_FIELD (type, i))
-	      && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
+	  {
+	    /* Static fields can be ignored here.  */
+	    if (field_is_static (&TYPE_FIELD (type, i)))
+	      continue;
+	    /* If the field has dynamic type, then so does TYPE.  */
+	    if (is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0))
+	      return 1;
+	    /* If the field is at a fixed offset, then it is not
+	       dynamic.  */
+	    if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_DWARF_BLOCK)
+	      continue;
+	    /* Do not consider C++ virtual base types to be dynamic
+	       due to the field's offset being dynamic; these are
+	       handled via other means.  */
+	    if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
+	      continue;
 	    return 1;
+	  }
       }
       break;
     }
@@ -2430,6 +2447,24 @@ resolve_dynamic_struct (struct type *type,
       if (field_is_static (&TYPE_FIELD (resolved_type, i)))
 	continue;
 
+      if (TYPE_FIELD_LOC_KIND (resolved_type, i) == FIELD_LOC_KIND_DWARF_BLOCK)
+	{
+	  struct dwarf2_property_baton baton;
+	  baton.property_type
+	    = lookup_pointer_type (TYPE_FIELD_TYPE (resolved_type, i));
+	  baton.locexpr = *TYPE_FIELD_DWARF_BLOCK (resolved_type, i);
+
+	  struct dynamic_prop prop;
+	  prop.kind = PROP_LOCEXPR;
+	  prop.data.baton = &baton;
+
+	  CORE_ADDR addr;
+	  if (dwarf2_evaluate_property (&prop, nullptr, addr_stack, &addr,
+					true))
+	    SET_FIELD_BITPOS (TYPE_FIELD (resolved_type, i),
+			      TARGET_CHAR_BIT * (addr - addr_stack->addr));
+	}
+
       /* As we know this field is not a static field, the field's
 	 field_loc_kind should be FIELD_LOC_KIND_BITPOS.  Verify
 	 this is the case, but only trigger a simple error rather
diff --git a/gdb/gnu-v3-abi.c b/gdb/gnu-v3-abi.c
index 89574ec9ed5..70558437f9c 100644
--- a/gdb/gnu-v3-abi.c
+++ b/gdb/gnu-v3-abi.c
@@ -30,6 +30,7 @@
 #include "typeprint.h"
 #include <algorithm>
 #include "cli/cli-style.h"
+#include "dwarf2/loc.h"
 
 static struct cp_abi_ops gnu_v3_abi_ops;
 
@@ -461,6 +462,31 @@ gnuv3_baseclass_offset (struct type *type, int index,
   if (!BASETYPE_VIA_VIRTUAL (type, index))
     return TYPE_BASECLASS_BITPOS (type, index) / 8;
 
+  /* If we have a DWARF expression for the offset, evaluate it.  */
+  if (TYPE_FIELD_LOC_KIND (type, index) == FIELD_LOC_KIND_DWARF_BLOCK)
+    {
+      struct dwarf2_property_baton baton;
+      baton.property_type
+	= lookup_pointer_type (TYPE_FIELD_TYPE (type, index));
+      baton.locexpr = *TYPE_FIELD_DWARF_BLOCK (type, index);
+
+      struct dynamic_prop prop;
+      prop.kind = PROP_LOCEXPR;
+      prop.data.baton = &baton;
+
+      struct property_addr_info addr_stack;
+      addr_stack.type = type;
+      /* Note that we don't set "valaddr" here.  Doing so causes
+	 regressions.  FIXME.  */
+      addr_stack.addr = address + embedded_offset;
+      addr_stack.next = nullptr;
+
+      CORE_ADDR result;
+      if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
+				    true))
+	return (int) (result - addr_stack.addr);
+    }
+
   /* To access a virtual base, we need to use the vbase offset stored in
      our vtable.  Recent GCC versions provide this information.  If it isn't
      available, we could get what we needed from RTTI, or from drawing the
diff --git a/gdb/testsuite/gdb.ada/variant.exp b/gdb/testsuite/gdb.ada/variant.exp
index b68bf60b192..490956a2666 100644
--- a/gdb/testsuite/gdb.ada/variant.exp
+++ b/gdb/testsuite/gdb.ada/variant.exp
@@ -37,4 +37,10 @@ foreach_with_prefix scenario {none all minimal} {
 
     gdb_test "print st1" " = \\(i => -4, one => 1, x => 2\\)"
     gdb_test "print st2" " = \\(i => 99, one => 1, y => 77\\)"
+
+    gdb_test "print nav1" " = \\(one => 0, two => 93, str => \"\"\\)"
+    gdb_test "print nav2" \
+	" = \\(one => 3, two => 0, str => \"zzz\", onevalue => 33, str2 => \"\"\\)"
+    gdb_test "print nav3" \
+	" = \\(one => 3, two => 7, str => \"zzz\", onevalue => 33, str2 => \"qqqqqqq\", twovalue => 88\\)"
 }
diff --git a/gdb/testsuite/gdb.ada/variant/pck.ads b/gdb/testsuite/gdb.ada/variant/pck.ads
index 41b6efd4da8..3895b9c48eb 100644
--- a/gdb/testsuite/gdb.ada/variant/pck.ads
+++ b/gdb/testsuite/gdb.ada/variant/pck.ads
@@ -34,4 +34,21 @@ package Pck is
 	   Y : Integer;
       end case;
    end record;
+
+   type Nested_And_Variable (One, Two: Integer) is record
+       Str : String (1 .. One);
+       case One is
+          when 0 =>
+	     null;
+          when others =>
+	     OneValue : Integer;
+             Str2 : String (1 .. Two);
+             case Two is
+	        when 0 =>
+		   null;
+		when others =>
+		   TwoValue : Integer;
+             end case;
+       end case;
+   end record;
 end Pck;
diff --git a/gdb/testsuite/gdb.ada/variant/pkg.adb b/gdb/testsuite/gdb.ada/variant/pkg.adb
index 0cc38f5b253..91cf080ed1d 100644
--- a/gdb/testsuite/gdb.ada/variant/pkg.adb
+++ b/gdb/testsuite/gdb.ada/variant/pkg.adb
@@ -22,6 +22,17 @@ procedure Pkg is
    ST1 : constant Second_Type := (I => -4, One => 1, X => 2);
    ST2 : constant Second_Type := (I => 99, One => 1, Y => 77);
 
+   NAV1 : constant Nested_And_Variable := (One => 0, Two => 93,
+                                           Str => (others => 'z'));
+   NAV2 : constant Nested_And_Variable := (One => 3, OneValue => 33,
+                                           Str => (others => 'z'),
+                                           Str2 => (others => 'q'),
+                                           Two => 0);
+   NAV3 : constant Nested_And_Variable := (One => 3, OneValue => 33,
+                                           Str => (others => 'z'),
+                                           Str2 => (others => 'q'),
+                                           Two => 7, TwoValue => 88);
+
 begin
    R := (C => 'd');
    Q := (C => Character'First, X_First => 27);
-- 
2.21.1


  parent reply	other threads:[~2020-04-08 17:54 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-08 17:54 [PATCH 0/11] Variant part support, plus more Tom Tromey
2020-04-08 17:54 ` [PATCH 01/11] Rename "variant" to "ppc_variant" Tom Tromey
2020-04-08 17:54 ` [PATCH 02/11] Add new variant part code Tom Tromey
2020-04-08 17:54 ` [PATCH 03/11] Allow DWARF expression to push the initial address Tom Tromey
2020-04-08 17:54 ` [PATCH 04/11] Prefer existing data when evaluating DWARF expression Tom Tromey
2020-04-08 17:54 ` [PATCH 05/11] Rewrite the existing variant part code Tom Tromey
2020-06-04  8:13   ` Tom de Vries
2020-06-15 17:30     ` Tom Tromey
2020-06-17 16:11       ` Tom de Vries
2020-06-17 16:45         ` Tom Tromey
     [not found]           ` <af2905c9-739a-be03-6925-2109e308540a@suse.de>
2020-06-19 20:51             ` Tom Tromey
2020-04-08 17:54 ` [PATCH 06/11] Add support for dynamic type lengths Tom Tromey
2020-04-08 17:54 ` Tom Tromey [this message]
2020-04-08 17:54 ` [PATCH 08/11] Update Ada ptype support for dynamic types Tom Tromey
2020-04-08 17:54 ` [PATCH 09/11] Add tests for Ada changes Tom Tromey
2020-04-08 17:54 ` [PATCH 10/11] Add Python support for dynamic types Tom Tromey
2020-04-08 18:59   ` Eli Zaretskii
2020-04-09  0:20   ` Christian Biesinger
2020-04-09 18:44     ` Tom Tromey
2020-04-10 17:44       ` Christian Biesinger
2020-04-24 19:42         ` Tom Tromey
2020-04-24 20:35         ` Tom Tromey
2020-04-25  8:43           ` Matt Rice
2020-04-08 17:54 ` [PATCH 11/11] Update test cases that work with minimal encodings Tom Tromey
2020-04-28  6:38   ` Tom de Vries
2020-04-28 15:35     ` Tom Tromey
2020-04-30  6:45       ` Tom de Vries
2020-05-11 20:31       ` [committed][gdb/testsuite] Change kfail into xfail in gdb.ada/packed_tagged.exp Tom de Vries
2020-04-24 19:39 ` [PATCH 0/11] Variant part support, plus more Tom Tromey

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=20200408175452.30637-8-tromey@adacore.com \
    --to=tromey@adacore.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).