[PATCH 31/43] Add to_gdb_value method to DWARF entry class

Zoran Zaric via Gdb-patches
2021-03-01T14:47:55Z

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