[PATCH 6/9] gdb: implement address_from_register using value_from_register

Simon Marchi
2023-12-21T19:18:14Z

As explained in the comment removed by the previous commit "gdb: pass
non-nullptr frame to gdbarch_value_from_register in
address_from_register", address_from_register copies some implementation
bits from value_from_register:

/* This routine may be called during early unwinding, at a time
where the ID of FRAME is not yet known. Calling value_from_register
would therefore abort in get_frame_id. However, since we only need
a temporary value that is never used as lvalue, we actually do not
really need to set its VALUE_NEXT_FRAME_ID. Therefore, we re-implement
the core of value_from_register, but use the null_frame_id. */

This is no longer relevant, since we now create a value with a valid next
frame id, so change address_from_register to use value_from_register.

Change-Id: I189bd96f28735ed9f47750ffd73764c459ec6f43
---
gdb/findvar.c | 41 ++++-------------------------------------
1 file changed, 4 insertions(+), 37 deletions(-)

diff --git a/gdb/findvar.c b/gdb/findvar.c
index 838d850e821d..fa014d60291d 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -867,40 +867,10 @@ value_from_register (struct type *type, int regnum, frame_info_ptr frame)
CORE_ADDR
address_from_register (int regnum, frame_info_ptr frame)
{
- struct gdbarch *gdbarch = get_frame_arch (frame);
- struct type *type = builtin_type (gdbarch)->builtin_data_ptr;
- CORE_ADDR result;
- int regnum_max_excl = gdbarch_num_cooked_regs (gdbarch);
-
- if (regnum < 0 || regnum >= regnum_max_excl)
- error (_("Invalid register #%d, expecting 0 <= # < %d"), regnum,
- regnum_max_excl);
+ type *type = builtin_type (get_frame_arch (frame))->builtin_data_ptr;
+ value_ref_ptr v = release_value (value_from_register (type, regnum, frame));

- /* Some targets require a special conversion routine even for plain
- pointer types. Avoid constructing a value object in those cases. */
- if (gdbarch_convert_register_p (gdbarch, regnum, type))
- {
- gdb_byte *buf = (gdb_byte *) alloca (type->length ());
- int optim, unavail, ok;
-
- ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
- buf, &optim, &unavail);
- if (!ok)
- {
- /* This function is used while computing a location expression.
- Complain about the value being optimized out, rather than
- letting value_as_address complain about some random register
- the expression depends on not being saved. */
- error_value_optimized_out ();
- }
-
- return unpack_long (type, buf);
- }
-
- value *value = gdbarch_value_from_register (gdbarch, type, regnum, frame);
- read_frame_register_value (value);
-
- if (value->optimized_out ())
+ if (v->optimized_out ())
{
/* This function is used while computing a location expression.
Complain about the value being optimized out, rather than
@@ -909,10 +879,7 @@ address_from_register (int regnum, frame_info_ptr frame)
error_value_optimized_out ();
}

- result = value_as_address (value);
- release_value (value);
-
- return result;
+ return value_as_address (v.get ());
}

#if GDB_SELF_TEST
--
2.43.0