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 06/11] Add support for dynamic type lengths
Date: Wed,  8 Apr 2020 11:54:47 -0600	[thread overview]
Message-ID: <20200408175452.30637-7-tromey@adacore.com> (raw)
In-Reply-To: <20200408175452.30637-1-tromey@adacore.com>

In Ada, a type with variant parts can have a variable length.  This
patch adds support for this to gdb, by integrating the length
computation into the dynamic type resolution code.

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

	* dwarf2/read.c (read_structure_type): Handle dynamic length.
	* gdbtypes.c (is_dynamic_type_internal): Check
	TYPE_HAS_DYNAMIC_LENGTH.
	(resolve_dynamic_type_internal): Use TYPE_DYNAMIC_LENGTH.
	* gdbtypes.h (TYPE_HAS_DYNAMIC_LENGTH, TYPE_DYNAMIC_LENGTH):
	New macros.
	(enum dynamic_prop_node_kind) <DYN_PROP_BYTE_SIZE>: New
	constant.

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

	* gdb.ada/variant.exp: New file
	* gdb.ada/variant/pkg.adb: New file
	* gdb.ada/variant/pck.adb: New file
---
 gdb/ChangeLog                         | 11 ++++++++
 gdb/dwarf2/read.c                     | 12 +++-----
 gdb/gdbtypes.c                        | 20 +++++++++++++-
 gdb/gdbtypes.h                        |  9 ++++++
 gdb/testsuite/ChangeLog               |  6 ++++
 gdb/testsuite/gdb.ada/variant.exp     | 40 +++++++++++++++++++++++++++
 gdb/testsuite/gdb.ada/variant/pck.ads | 37 +++++++++++++++++++++++++
 gdb/testsuite/gdb.ada/variant/pkg.adb | 30 ++++++++++++++++++++
 8 files changed, 156 insertions(+), 9 deletions(-)
 create mode 100644 gdb/testsuite/gdb.ada/variant.exp
 create mode 100644 gdb/testsuite/gdb.ada/variant/pck.ads
 create mode 100644 gdb/testsuite/gdb.ada/variant/pkg.adb

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index c0bced60384..31f732d29a3 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15257,14 +15257,10 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
         TYPE_LENGTH (type) = DW_UNSND (attr);
       else
 	{
-	  /* For the moment, dynamic type sizes are not supported
-	     by GDB's struct type.  The actual size is determined
-	     on-demand when resolving the type of a given object,
-	     so set the type's length to zero for now.  Otherwise,
-	     we record an expression as the length, and that expression
-	     could lead to a very large value, which could eventually
-	     lead to us trying to allocate that much memory when creating
-	     a value of that type.  */
+	  struct dynamic_prop prop;
+	  if (attr_to_dynamic_prop (attr, die, cu, &prop,
+				    cu->per_cu->addr_type ()))
+	    add_dyn_prop (DYN_PROP_BYTE_SIZE, prop, type);
           TYPE_LENGTH (type) = 0;
 	}
     }
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 9d0ebaa159e..4491c1832f1 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1975,6 +1975,9 @@ is_dynamic_type_internal (struct type *type, int top_level)
   if (prop != nullptr && prop->kind != PROP_TYPE)
     return 1;
 
+  if (TYPE_HAS_DYNAMIC_LENGTH (type))
+    return 1;
+
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_RANGE:
@@ -2491,13 +2494,19 @@ resolve_dynamic_type_internal (struct type *type,
 			       int top_level)
 {
   struct type *real_type = check_typedef (type);
-  struct type *resolved_type = type;
+  struct type *resolved_type = nullptr;
   struct dynamic_prop *prop;
   CORE_ADDR value;
 
   if (!is_dynamic_type_internal (real_type, top_level))
     return type;
 
+  gdb::optional<CORE_ADDR> type_length;
+  prop = TYPE_DYNAMIC_LENGTH (type);
+  if (prop != NULL
+      && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
+    type_length = value;
+
   if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
     {
       resolved_type = copy_type (type);
@@ -2553,6 +2562,15 @@ resolve_dynamic_type_internal (struct type *type,
 	}
     }
 
+  if (resolved_type == nullptr)
+    return type;
+
+  if (type_length.has_value ())
+    {
+      TYPE_LENGTH (resolved_type) = *type_length;
+      remove_dyn_prop (DYN_PROP_BYTE_SIZE, resolved_type);
+    }
+
   /* Resolve data_location attribute.  */
   prop = TYPE_DATA_LOCATION (resolved_type);
   if (prop != NULL
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 134515845f2..87b1bca3a22 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -355,6 +355,10 @@ DEF_ENUM_FLAGS_TYPE (enum type_instance_flag_value, type_instance_flags);
 #define TYPE_HAS_VARIANT_PARTS(t) \
   (get_dyn_prop (DYN_PROP_VARIANT_PARTS, t) != nullptr)
 
+/* * True if this type has a dynamic length.  */
+#define TYPE_HAS_DYNAMIC_LENGTH(t) \
+  (get_dyn_prop (DYN_PROP_BYTE_SIZE, t) != nullptr)
+
 /* * Instruction-space delimited type.  This is for Harvard architectures
    which have separate instruction and data address spaces (and perhaps
    others).
@@ -552,6 +556,9 @@ enum dynamic_prop_node_kind
 
   /* A property holding variant parts.  */
   DYN_PROP_VARIANT_PARTS,
+
+  /* A property holding the size of the type.  */
+  DYN_PROP_BYTE_SIZE,
 };
 
 /* * List for dynamic type attributes.  */
@@ -1445,6 +1452,8 @@ extern bool set_type_align (struct type *, ULONGEST);
   TYPE_DATA_LOCATION (thistype)->data.const_val
 #define TYPE_DATA_LOCATION_KIND(thistype) \
   TYPE_DATA_LOCATION (thistype)->kind
+#define TYPE_DYNAMIC_LENGTH(thistype) \
+  get_dyn_prop (DYN_PROP_BYTE_SIZE, thistype)
 
 /* Property accessors for the type allocated/associated.  */
 #define TYPE_ALLOCATED_PROP(thistype) \
diff --git a/gdb/testsuite/gdb.ada/variant.exp b/gdb/testsuite/gdb.ada/variant.exp
new file mode 100644
index 00000000000..b68bf60b192
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/variant.exp
@@ -0,0 +1,40 @@
+# Copyright 2020 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/>.
+
+load_lib "ada.exp"
+
+standard_ada_testfile pkg
+
+foreach_with_prefix scenario {none all minimal} {
+    set flags {debug}
+    if {$scenario != "none"} {
+	lappend flags additional_flags=-fgnat-encodings=$scenario
+    }
+
+    if {[gdb_compile_ada "${srcfile}" "${binfile}" executable $flags] != ""} {
+	return -1
+    }
+
+    clean_restart ${testfile}
+
+    set bp_location [gdb_get_line_number "STOP" ${testdir}/pkg.adb]
+    runto "pkg.adb:$bp_location"
+
+    gdb_test "print r" "= \\(c => 100 'd'\\)"
+    gdb_test "print q" " = \\(c => 0 '\\\[\"00\"\\\]', x_first => 27\\)"
+
+    gdb_test "print st1" " = \\(i => -4, one => 1, x => 2\\)"
+    gdb_test "print st2" " = \\(i => 99, one => 1, y => 77\\)"
+}
diff --git a/gdb/testsuite/gdb.ada/variant/pck.ads b/gdb/testsuite/gdb.ada/variant/pck.ads
new file mode 100644
index 00000000000..41b6efd4da8
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/variant/pck.ads
@@ -0,0 +1,37 @@
+--  Copyright 2020 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/>.
+
+package Pck is
+
+   type Rec_Type (C : Character := 'd') is record
+      case C is
+         when Character'First     => X_First : Integer;
+         when Character'Val (127) => X_127   : Integer;
+         when Character'Val (128) => X_128   : Integer;
+         when Character'Last      => X_Last  : Integer;
+         when others              => null;
+      end case;
+   end record;
+
+   type Second_Type (I : Integer) is record
+      One: Integer;
+      case I is
+         when -5 .. 5 =>
+	   X : Integer;
+         when others =>
+	   Y : Integer;
+      end case;
+   end record;
+end Pck;
diff --git a/gdb/testsuite/gdb.ada/variant/pkg.adb b/gdb/testsuite/gdb.ada/variant/pkg.adb
new file mode 100644
index 00000000000..0cc38f5b253
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/variant/pkg.adb
@@ -0,0 +1,30 @@
+--  Copyright 2020 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/>.
+
+with Pck; use Pck;
+
+procedure Pkg is
+
+   R, Q : Rec_Type;
+
+   ST1 : constant Second_Type := (I => -4, One => 1, X => 2);
+   ST2 : constant Second_Type := (I => 99, One => 1, Y => 77);
+
+begin
+   R := (C => 'd');
+   Q := (C => Character'First, X_First => 27);
+
+   null; -- STOP
+end Pkg;
-- 
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 ` Tom Tromey [this message]
2020-04-08 17:54 ` [PATCH 07/11] Add support for variable field offsets Tom Tromey
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-7-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).