[PATCH v3 05/28] Add to_value method to DWARF entry classes

Zoran Zaric zoran.zaric@amd.com
Thu Oct 14 09:32:12 GMT 2021


From: Zoran Zaric <Zoran.Zaric@amd.com>

Following the idea from the last patch this patch is adding another
conversion method from any DWARF entry object into a DWARF value
object.

Currently, we only know how to convert from a memory location
description into a value, but it is resonable to expect a set of target
hooks that would let the target decide on how to do other conversions
in the future.

gdb/ChangeLog:

        * dwarf2/expr.c (dwarf_location::to_value): New method.
        (dwarf_memory::to_value): New method.
        (ill_formed_expression): New function.
---
 gdb/dwarf2/expr.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index 5d443edfd9d..56f17f52756 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -100,6 +100,14 @@ bits_to_bytes (ULONGEST start, ULONGEST n_bits)
   return (start % HOST_CHAR_BIT + n_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
 }
 
+/* Throw an exception about the invalid DWARF expression.  */
+
+static void ATTRIBUTE_NORETURN
+ill_formed_expression ()
+{
+  error (_("Ill-formed DWARF expression"));
+}
+
 /* See expr.h.  */
 
 CORE_ADDR
@@ -283,6 +291,7 @@ write_to_memory (CORE_ADDR address, const gdb_byte *buffer,
 
 class dwarf_location;
 class dwarf_memory;
+class dwarf_value;
 
 /* Base class that describes entries found on a DWARF expression
    evaluation stack.  */
@@ -330,6 +339,16 @@ class dwarf_location : public dwarf_entry
     m_initialised = initialised;
   };
 
+  /* Convert DWARF entry into a DWARF value.  TYPE defines a desired type of
+     the returned DWARF value if it doesn't already have one.
+
+     If the conversion from that location description kind to a value is not
+     supported, throw an error.  */
+  virtual std::unique_ptr<dwarf_value> to_value (struct type *type) const
+  {
+    ill_formed_expression ();
+  }
+
 protected:
   /* Architecture of the location.  */
   gdbarch *m_arch;
@@ -395,6 +414,8 @@ class dwarf_value : public dwarf_entry
   struct type *m_type;
 };
 
+using dwarf_value_up = std::unique_ptr<dwarf_value>;
+
 /* Undefined location description entry.  This is a special location
    description type that describes the location description that is
    not known.  */
@@ -419,6 +440,8 @@ class dwarf_memory final : public dwarf_location
     m_stack = stack;
   };
 
+  dwarf_value_up to_value (struct type *type) const override;
+
 private:
   /* True if the location belongs to a stack memory region.  */
   bool m_stack;
@@ -439,6 +462,12 @@ dwarf_value::to_location (struct gdbarch *arch) const
   return make_unique<dwarf_memory> (arch, offset);
 }
 
+dwarf_value_up
+dwarf_memory::to_value (struct type *type) const
+{
+  return make_unique<dwarf_value> (m_offset, type);
+}
+
 /* Register location description entry.  */
 
 class dwarf_register final : public dwarf_location
-- 
2.17.1



More information about the Gdb-patches mailing list