Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Zoran Zaric via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Subject: [PATCH 31/43] Add to_gdb_value method to DWARF entry class
Date: Mon,  1 Mar 2021 14:46:08 +0000	[thread overview]
Message-ID: <20210301144620.103016-32-Zoran.Zaric@amd.com> (raw)
In-Reply-To: <20210301144620.103016-1-Zoran.Zaric@amd.com>

The result of the DWARF expression evaluation is expected to be in a
format of a struct value object. This means that a new to_gdb_value
method is needed for all classes that derive from dwarf_entry class.

In the case of the dwarf_value class, it is usefull to have an even
more simplified version of this method (convert_to_gdb_value) that
does not require the evaluation frame context or any subobj
information. This method will be used even more in the following patch.

gdb/ChangeLog:

        * dwarf2/expr.c (dwarf_entry::to_gdb_value): New method.
        (dwarf_location::to_gdb_value): New method.
        (dwarf_value::convert_to_gdb_value): New method.
        (dwarf_value::to_gdb_value): New method.
        (dwarf_undefined::to_gdb_value): New method.
        (dwarf_memory::to_gdb_value): New method.
        (dwarf_register::to_gdb_value): New method.
        (dwarf_implicit::to_gdb_value): New method.
        (dwarf_implicit_pointer::to_gdb_value): New method.
        (dwarf_composite::to_gdb_value): New method.
---
 gdb/dwarf2/expr.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 239 insertions(+)

diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index ec78a698e52..a2bdee95725 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -425,6 +425,16 @@ class dwarf_entry : public std::enable_shared_from_this<dwarf_entry>
      desired type of the returned DWARF value if it already
      doesnt have one.  */
   virtual std::shared_ptr<dwarf_value> to_value (struct type *type) = 0;
+
+  /* Convert DWARF entry to the matching struct value representation
+     of the given TYPE type in a given FRAME. SUBOBJ_TYPE information
+     if specified, will be used for more precise description of the
+     source variable type information.  Where SUBOBJ_OFFSET defines an
+     offset into the DWARF entry contents.  */
+  virtual struct value *to_gdb_value (struct frame_info *frame,
+				      struct type *type,
+				      struct type *subobj_type,
+				      LONGEST subobj_offset) const = 0;
 };
 
 dwarf_entry::~dwarf_entry () = default;
@@ -744,6 +754,12 @@ class dwarf_value : public dwarf_entry
     return unpack_long (m_type, m_contents.get ());
   }
 
+  /* Convert DWARF value to the matching struct value representation
+     of the given TYPE type.  Where offset defines an offset into the
+     DWARF value contents.  */
+  struct value *convert_to_gdb_value (struct type *type,
+				      LONGEST offset = 0) const;
+
   /* Convert DWARF value into a DWARF memory location description.
      ARCH defines an architecture of the location described.  */
   std::shared_ptr<dwarf_location> to_location (struct gdbarch *arch) override;
@@ -756,6 +772,10 @@ class dwarf_value : public dwarf_entry
     return std::dynamic_pointer_cast<dwarf_value> (shared_from_this ());
   }
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* Value contents as a stream of bytes in target byte order.  */
   gdb::unique_xmalloc_ptr<gdb_byte> m_contents;
@@ -764,6 +784,20 @@ class dwarf_value : public dwarf_entry
   struct type *m_type;
 };
 
+struct value *
+dwarf_value::convert_to_gdb_value (struct type *type, LONGEST offset) const
+{
+  size_t type_len = TYPE_LENGTH (type);
+
+  if (offset + type_len > TYPE_LENGTH (m_type))
+    invalid_synthetic_pointer ();
+
+  struct value *retval = allocate_value (type);
+  memcpy (value_contents_raw (retval),
+	  m_contents.get () + offset, type_len);
+  return retval;
+}
+
 std::shared_ptr<dwarf_location>
 dwarf_value::to_location (struct gdbarch *arch)
 {
@@ -779,6 +813,17 @@ dwarf_value::to_location (struct gdbarch *arch)
   return std::dynamic_pointer_cast<dwarf_location> (memory);
 }
 
+struct value *
+dwarf_value::to_gdb_value (struct frame_info *frame, struct type *type,
+			   struct type *subobj_type,
+			   LONGEST subobj_offset) const
+{
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  return convert_to_gdb_value (subobj_type, subobj_offset);
+}
+
 /* Undefined location description entry.  This is a special location
    description type that describes the location description that is
    not known.  */
@@ -807,6 +852,20 @@ class dwarf_undefined : public dwarf_location
     *unavailable = 0;
     *optimized = 1;
   }
+
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override
+  {
+    struct value *retval = allocate_value (subobj_type);
+
+    if (subobj_type == nullptr)
+      subobj_type = type;
+
+    mark_value_bytes_optimized_out (retval, subobj_offset,
+				    TYPE_LENGTH (subobj_type));
+    return retval;
+  }
 };
 
 class dwarf_memory : public dwarf_location
@@ -839,6 +898,10 @@ class dwarf_memory : public dwarf_location
     (struct frame_info *frame, const struct property_addr_info *addr_info,
      struct type *type, size_t size = 0) const override;
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* True if the location belongs to a stack memory region.  */
   bool m_stack;
@@ -1012,6 +1075,27 @@ dwarf_memory::deref (struct frame_info *frame,
   return std::make_shared<dwarf_value> (read_buf.data (), type);
 }
 
+struct value *
+dwarf_memory::to_gdb_value (struct frame_info *frame, struct type *type,
+			    struct type *subobj_type,
+			    LONGEST subobj_offset) const
+{
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  struct type *ptr_type = builtin_type (m_arch)->builtin_data_ptr;
+  CORE_ADDR address = m_offset;
+
+  if (subobj_type->code () == TYPE_CODE_FUNC
+      || subobj_type->code () == TYPE_CODE_METHOD)
+    ptr_type = builtin_type (m_arch)->builtin_func_ptr;
+
+  address = value_as_address (value_from_pointer (ptr_type, address));
+  struct value *retval = value_at_lazy (subobj_type, address + subobj_offset);
+  set_value_stack (retval, m_stack);
+  return retval;
+}
+
 /* Register location description entry.  */
 
 class dwarf_register : public dwarf_location
@@ -1032,6 +1116,10 @@ class dwarf_register : public dwarf_location
 	      size_t location_bit_limit, bool big_endian,
 	      int *optimized, int *unavailable) const override;
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* DWARF register number.  */
   unsigned int m_regnum;
@@ -1123,6 +1211,50 @@ dwarf_register::write (struct frame_info *frame, const gdb_byte *buf,
 		     temp_buf, optimized, unavailable);
 }
 
+struct value *
+dwarf_register::to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const
+{
+  int gdb_regnum = dwarf_reg_to_regnum_or_error (m_arch, m_regnum);
+
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  if (frame == NULL)
+    internal_error (__FILE__, __LINE__, _("invalid frame information"));
+
+  /* Construct the value.  */
+  struct value *retval
+    = gdbarch_value_from_register (m_arch, type,
+				   gdb_regnum, get_frame_id (frame));
+  LONGEST retval_offset = value_offset (retval);
+
+  if (type_byte_order (type) == BFD_ENDIAN_BIG
+      && TYPE_LENGTH (type) + m_offset < retval_offset)
+    /* Big-endian, and we want less than full size.  */
+    set_value_offset (retval, retval_offset - m_offset);
+  else
+    set_value_offset (retval, retval_offset + m_offset);
+
+  /* Get the data.  */
+  read_frame_register_value (retval, frame);
+
+  if (value_optimized_out (retval))
+    {
+      /* This means the register has undefined value / was not saved.
+	 As we're computing the location of some variable etc. in the
+	 program, not a value for inspecting a register ($pc, $sp, etc.),
+	 return a generic optimized out value instead, so that we show
+	 <optimized out> instead of <not saved>.  */
+      struct value *temp = allocate_value (subobj_type);
+      value_contents_copy (temp, 0, retval, 0, TYPE_LENGTH (subobj_type));
+      retval = temp;
+    }
+
+  return retval;
+}
+
 /* Implicit location description entry.  Describes a location
    description not found on the target but instead saved in a
    gdb-allocated buffer.  */
@@ -1155,6 +1287,10 @@ class dwarf_implicit : public dwarf_location
     *unavailable = 0;
   }
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* Implicit location contents as a stream of bytes in target byte-order.  */
   gdb::unique_xmalloc_ptr<gdb_byte> m_contents;
@@ -1205,6 +1341,36 @@ dwarf_implicit::read (struct frame_info *frame, gdb_byte *buf,
 		total_bits_to_skip, bit_size, big_endian);
 }
 
+struct value *
+dwarf_implicit::to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const
+{
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  size_t subtype_len = TYPE_LENGTH (subobj_type);
+  size_t type_len = TYPE_LENGTH (type);
+
+  /* To be compatible with expected error output of the existing
+     tests, the invalid synthetic pointer is not reported for
+     DW_OP_implicit_value operation.  */
+  if (subobj_offset + subtype_len > type_len
+      && m_byte_order != BFD_ENDIAN_UNKNOWN)
+    invalid_synthetic_pointer ();
+
+  struct value *retval = allocate_value (subobj_type);
+
+  /* The given offset is relative to the actual object.  */
+  if (m_byte_order == BFD_ENDIAN_BIG)
+    subobj_offset += m_size - type_len;
+
+  memcpy ((void *)value_contents_raw (retval),
+	  (void *)(m_contents.get () + subobj_offset), subtype_len);
+
+  return retval;
+}
+
 /* Implicit pointer location description entry.  */
 
 class dwarf_implicit_pointer : public dwarf_location
@@ -1260,6 +1426,10 @@ class dwarf_implicit_pointer : public dwarf_location
 				       LONGEST bit_offset = 0,
 				       int bit_length = 0) const override;
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* Per object file data of the implicit pointer.  */
   dwarf2_per_objfile *m_per_objfile;
@@ -1323,6 +1493,32 @@ dwarf_implicit_pointer::indirect_implicit_ptr (struct frame_info *frame,
 				     m_per_cu, m_per_objfile, frame, type);
 }
 
+struct value *
+dwarf_implicit_pointer::to_gdb_value (struct frame_info *frame,
+				      struct type *type,
+				      struct type *subobj_type,
+				      LONGEST subobj_offset) const
+{
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  /* Complain if the expression is larger than the size of the
+     outer type.  */
+  if (m_addr_size > HOST_CHAR_BIT * TYPE_LENGTH (type))
+    invalid_synthetic_pointer ();
+
+  computed_closure *closure
+    = new computed_closure (std::make_shared<dwarf_implicit_pointer> (*this),
+			    get_frame_id (frame));
+  closure->incref ();
+
+  struct value *retval
+    = allocate_computed_value (subobj_type, &closure_value_funcs, closure);
+  set_value_offset (retval, subobj_offset);
+
+  return retval;
+}
+
 /* Composite location description entry.  */
 
 class dwarf_composite : public dwarf_location
@@ -1366,6 +1562,10 @@ class dwarf_composite : public dwarf_location
 				       LONGEST bit_offset = 0,
 				       int bit_length = 0) const override;
 
+  struct value *to_gdb_value (struct frame_info *frame, struct type *type,
+			      struct type *subobj_type,
+			      LONGEST subobj_offset) const override;
+
 private:
   /* Composite piece that contains a piece location
      description and it's size.  */
@@ -1636,6 +1836,45 @@ dwarf_composite::indirect_implicit_ptr (struct frame_info *frame,
   return nullptr;
 }
 
+struct value *
+dwarf_composite::to_gdb_value (struct frame_info *frame, struct type *type,
+			       struct type *subobj_type,
+			       LONGEST subobj_offset) const
+{
+  size_t pieces_num = m_pieces.size ();
+  ULONGEST bit_size = 0;
+
+  if (subobj_type == nullptr)
+    subobj_type = type;
+
+  for (unsigned int i = 0; i < pieces_num; i++)
+    bit_size += m_pieces[i].m_size;
+
+  /* Complain if the expression is larger than the size of the
+     outer type.  */
+  if (bit_size > HOST_CHAR_BIT * TYPE_LENGTH (type))
+    invalid_synthetic_pointer ();
+
+  computed_closure *closure;
+
+  /* If compilation unit information is not available
+     we are in a CFI context.  */
+  if (m_per_cu == NULL)
+    closure = new computed_closure (std::make_shared<dwarf_composite> (*this),
+				    frame);
+  else
+    closure = new computed_closure (std::make_shared<dwarf_composite> (*this),
+				    get_frame_id (frame));
+
+  closure->incref ();
+
+  struct value *retval
+    = allocate_computed_value (subobj_type, &closure_value_funcs, closure);
+  set_value_offset (retval, subobj_offset);
+
+  return retval;
+}
+
 static void *
 copy_value_closure (const struct value *v)
 {
-- 
2.17.1


  parent reply	other threads:[~2021-03-01 14:47 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-01 14:45 [PATCH 00/43 V2] Allow location description on the DWARF stack Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 01/43] Replace the symbol needs evaluator with a parser Zoran Zaric via Gdb-patches
2021-04-27  1:20   ` Simon Marchi via Gdb-patches
2021-04-28 10:17     ` Zoran Zaric via Gdb-patches
2021-04-28 14:08       ` Simon Marchi via Gdb-patches
2021-04-28 15:02         ` Zoran Zaric via Gdb-patches
2021-04-28 15:31         ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 02/43] Cleanup of the dwarf_expr_context constructor Zoran Zaric via Gdb-patches
2021-04-27  1:23   ` Simon Marchi via Gdb-patches
2021-04-28 10:19     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 03/43] Move frame context info to dwarf_expr_context Zoran Zaric via Gdb-patches
2021-04-27  2:19   ` Simon Marchi via Gdb-patches
2021-04-28 10:51     ` Zoran Zaric via Gdb-patches
2021-04-28 14:14       ` Simon Marchi via Gdb-patches
2021-04-28 15:55         ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 04/43] Remove get_frame_cfa from dwarf_expr_context Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 05/43] Move compilation unit info to dwarf_expr_context Zoran Zaric via Gdb-patches
2021-04-27  2:58   ` Simon Marchi via Gdb-patches
2021-04-28 11:28     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 06/43] Move dwarf_call " Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 07/43] Move get_object_address " Zoran Zaric via Gdb-patches
2021-04-27  3:12   ` Simon Marchi via Gdb-patches
2021-04-28 11:34     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 08/43] Move read_mem " Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 09/43] Move push_dwarf_reg_entry_value to expr.c Zoran Zaric via Gdb-patches
2021-04-27  3:56   ` Simon Marchi via Gdb-patches
2021-04-28 11:36     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 10/43] Inline get_reg_value method of dwarf_expr_context Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 11/43] Remove empty frame and full evaluators Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 12/43] Merge evaluate_for_locexpr_baton evaluator Zoran Zaric via Gdb-patches
2021-04-28  1:33   ` Simon Marchi via Gdb-patches
2021-04-28 11:39     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 13/43] Move piece_closure and its support to expr.c Zoran Zaric via Gdb-patches
2021-04-28  1:56   ` Simon Marchi via Gdb-patches
2021-04-28 11:40     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 14/43] Make value_copy also copy the stack data member Zoran Zaric via Gdb-patches
2021-04-28  2:01   ` Simon Marchi via Gdb-patches
2021-04-28 11:43     ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 15/43] Make DWARF evaluator return a single struct value Zoran Zaric via Gdb-patches
2021-04-28  2:21   ` Simon Marchi via Gdb-patches
2021-04-28 11:47     ` Zoran Zaric via Gdb-patches
2021-04-28 14:24       ` Simon Marchi via Gdb-patches
2021-03-01 14:45 ` [PATCH 16/43] Simplify dwarf_expr_context class interface Zoran Zaric via Gdb-patches
2021-04-28  2:45   ` Simon Marchi via Gdb-patches
2021-04-28 13:15     ` Zoran Zaric via Gdb-patches
2021-04-28 14:41       ` Simon Marchi via Gdb-patches
2021-04-28 15:39         ` Zoran Zaric via Gdb-patches
2021-04-28 19:19           ` Simon Marchi via Gdb-patches
2021-04-29 15:49       ` Simon Marchi via Gdb-patches
2021-04-29 15:55         ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 17/43] Add as_lval argument to expression evaluator Zoran Zaric via Gdb-patches
2021-04-28  3:04   ` Simon Marchi via Gdb-patches
2021-04-28 13:16     ` Zoran Zaric via Gdb-patches
2021-04-28  3:30   ` Simon Marchi via Gdb-patches
2021-03-01 14:45 ` [PATCH 18/43] Add new register access interface to expr.c Zoran Zaric via Gdb-patches
2021-03-08 23:52   ` Lancelot SIX via Gdb-patches
2021-04-28  3:25   ` Simon Marchi via Gdb-patches
2021-04-28 13:29     ` Zoran Zaric via Gdb-patches
2021-04-28 14:48       ` Simon Marchi via Gdb-patches
2021-04-28 15:42         ` Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 19/43] Add new memory " Zoran Zaric via Gdb-patches
2021-04-30 21:24   ` Simon Marchi via Gdb-patches
2021-03-01 14:45 ` [PATCH 20/43] Add new classes that model DWARF stack element Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 21/43] Add to_location method to DWARF entry classes Zoran Zaric via Gdb-patches
2021-03-01 14:45 ` [PATCH 22/43] Add to_value " Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 23/43] Add read method to location description classes Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 24/43] Add write " Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 25/43] Add deref " Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 26/43] Add read_from_gdb_value method to dwarf_location Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 27/43] Add write_to_gdb_value " Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 28/43] Add is_implicit_ptr_at " Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 29/43] Add indirect_implicit_ptr to dwarf_location class Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 30/43] Add new computed struct value callback interface Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` Zoran Zaric via Gdb-patches [this message]
2021-03-01 14:46 ` [PATCH 32/43] Change DWARF stack to use new dwarf_entry classes Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 33/43] Remove old computed struct value callbacks Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 34/43] Comments cleanup between expr.h and expr.c Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 35/43] Remove dwarf_expr_context from expr.h interface Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 36/43] Move read_addr_from_reg function to frame.c Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 37/43] Add frame info check to DW_OP_reg operations Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 38/43] Remove DWARF expression composition check Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 39/43] Change back the symbol needs to use the evaluator Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 40/43] Add support for any location description in CFI Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 41/43] Add DWARF operations for byte and bit offset Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 42/43] Add support for DW_OP_LLVM_undefined operation Zoran Zaric via Gdb-patches
2021-03-01 14:46 ` [PATCH 43/43] Add support for nested composite locations Zoran Zaric via Gdb-patches

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=20210301144620.103016-32-Zoran.Zaric@amd.com \
    --to=gdb-patches@sourceware.org \
    --cc=Zoran.Zaric@amd.com \
    /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