binutils-gdb/gdb/dwarf2
Simon Marchi b3245ceff0 gdb: read pseudo register through frame
Change gdbarch_pseudo_register_read_value to take a frame instead of a
regcache.  The frame (and formerly the regcache) is used to read raw
registers needed to make up the pseudo register value.  The problem with
using the regcache is that it always provides raw register values for
the current frame (frame 0).

Let's say the user wants to read the ebx register on amd64.  ebx is a pseudo
register, obtained by reading the bottom half (bottom 4 bytes) of the
rbx register, which is a raw register.  If the currently selected frame
is frame 0, it works fine:

    (gdb) frame 0
    #0  break_here_asm () at /home/smarchi/src/binutils-gdb/gdb/testsuite/gdb.arch/amd64-pseudo-unwind-asm.S:36
    36      in /home/smarchi/src/binutils-gdb/gdb/testsuite/gdb.arch/amd64-pseudo-unwind-asm.S
    (gdb) p/x $ebx
    $1 = 0x24252627
    (gdb) p/x $rbx
    $2 = 0x2021222324252627

But if the user is looking at another frame, and the raw register behind
the pseudo register has been saved at some point in the call stack, then
we get a wrong answer:

    (gdb) frame 1
    #1  0x000055555555517d in caller () at /home/smarchi/src/binutils-gdb/gdb/testsuite/gdb.arch/amd64-pseudo-unwind-asm.S:56
    56      in /home/smarchi/src/binutils-gdb/gdb/testsuite/gdb.arch/amd64-pseudo-unwind-asm.S
    (gdb) p/x $ebx
    $3 = 0x24252627
    (gdb) p/x $rbx
    $4 = 0x1011121314151617

Here, the value of ebx was computed using the value of rbx in frame 0
(through the regcache), it should have been computed using the value of
rbx in frame 1.

In other to make this work properly, make the following changes:

 - Make dwarf2_frame_prev_register return nullptr if it doesn't know how
   to unwind a register and that register is a pseudo register.
   Previously, it returned `frame_unwind_got_register`, meaning, in our
   example, "the value of ebx in frame 1 is the same as the value of ebx
   in frame 0", which is obviously false.  Return nullptr as a way to
   say "I don't know".

 - In frame_unwind_register_value, when prev_register (for instance
   dwarf2_frame_prev_register) returns nullptr, and we are trying to
   read a pseudo register, try to get the register value through
   gdbarch_pseudo_register_read_value or gdbarch_pseudo_register_read.
   If using gdbarch_pseudo_register_read, the behavior is known to be
   broken.  Implementations should be migrated to use
   gdbarch_pseudo_register_read_value to fix that.

 - Change gdbarch_pseudo_register_read_value to take a frame_info
   instead of a regcache, update implementations (aarch64, amd64, i386).
   In i386-tdep.c, I made a copy of i386_mmx_regnum_to_fp_regnum that
   uses a frame instead of a regcache.  The version using the regcache
   is still used by i386_pseudo_register_write.  It will get removed in
   a subsequent patch.

 - Add some helpers in value.{c,h} to implement the common cases of
   pseudo registers: taking part of a raw register and concatenating
   multiple raw registers.

 - Update readable_regcache::{cooked_read,cooked_read_value} to pass the
   current frame to gdbarch_pseudo_register_read_value.  Passing the
   current frame will give the same behavior as before: for frame 0, raw
   registers will be read from the current thread's regcache.

Notes:

 - I do not plan on changing gdbarch_pseudo_register_read to receive a
   frame instead of a regcache. That method is considered deprecated.
   Instead, we should be working on migrating implementations to use
   gdbarch_pseudo_register_read_value instead.

 - In frame_unwind_register_value, we still ask the unwinder to try to
   unwind pseudo register values.  It's apparently possible for the
   debug info to provide information about [1] pseudo registers, so we
   want to try that first, before falling back to computing them
   ourselves.

[1] https://inbox.sourceware.org/gdb-patches/20180528174715.A954AD804AD@oc3748833570.ibm.com/

Change-Id: Id6ef1c64e19090a183dec050e4034d8c2394e7ca
Reviewed-by: John Baldwin <jhb@FreeBSD.org>
2023-12-14 16:04:49 +00:00
..
abbrev-cache.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
abbrev-cache.h gdb: move call site types to call-site.h 2023-01-20 14:48:57 -05:00
abbrev.c [gdb/symtab] Add optimized out static var to cooked index 2023-07-21 08:25:25 +02:00
abbrev.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
ada-imported.c Handle Ada Pragma Import and Pragma Export 2023-05-12 13:25:28 -06:00
aranges.c Move read_addrmap_from_aranges to new file 2023-10-29 10:35:29 -06:00
aranges.h Move read_addrmap_from_aranges to new file 2023-10-29 10:35:29 -06:00
attribute.c Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
attribute.h gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
call-site.h Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
comp-unit-head.c Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
comp-unit-head.h Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
cooked-index.c Always use expand_symtabs_matching in ada-lang.c 2023-12-06 10:14:24 -07:00
cooked-index.h gdb: Use std::string_view instead of gdb::string_view 2023-11-21 11:52:36 +00:00
cu.c [gdb/symtab] Work around gas PR28629 2023-11-01 00:33:12 +01:00
cu.h gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
die.c Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
die.h gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
dwz.c Pre-read DWZ file in DWARF reader 2023-11-05 12:32:34 -07:00
dwz.h [gdb/symtab] Error out for .debug_types section in dwz file 2023-09-20 16:05:55 +02:00
expr.c gdb: make get_frame_register_bytes take the next frame 2023-12-14 16:04:49 +00:00
expr.h Change value::m_initialized to bool 2023-02-15 15:07:07 -07:00
file-and-dir.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
frame-tailcall.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
frame-tailcall.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
frame.c gdb: read pseudo register through frame 2023-12-14 16:04:49 +00:00
frame.h gdb: dwarf2 generic implementation for caching function data 2023-01-25 21:04:40 +01:00
index-cache.c [gdb/symtab] Fix data race on bfd::{cacheable,format} 2023-08-04 15:02:43 +02:00
index-cache.h gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
index-common.c gdb: Use std::string_view instead of gdb::string_view 2023-11-21 11:52:36 +00:00
index-common.h gdb: Use std::string_view instead of gdb::string_view 2023-11-21 11:52:36 +00:00
index-write.c gdb: improve error reporting for 'save gdb-index' 2023-12-13 08:54:06 +00:00
index-write.h Only use the per-BFD object to write a DWARF index 2023-02-24 11:46:53 -07:00
leb.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
leb.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
line-header.c gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
line-header.h [gdb] Fix typos 2023-06-03 22:43:57 +02:00
loc.c Use gdb::byte_vector in agent_expr 2023-06-20 11:00:19 -06:00
loc.h Use unrelocated_addr in the DWARF reader 2023-06-05 09:59:18 -06:00
macro.c Use C++17 [[fallthrough]] attribute 2023-11-29 14:29:43 -07:00
macro.h gdb: Replace gdb::optional with std::optional 2023-11-21 11:52:35 +00:00
mapped-index.h Write the DWARF index in the background 2023-02-24 11:46:53 -07:00
public.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
read-debug-names.c Remove quick_symbol_functions::expand_matching_symbols 2023-12-06 10:14:25 -07:00
read-debug-names.h gdb/dwarf2: split .debug_names reading code to own file 2023-02-15 15:12:06 -05:00
read-gdb-index.c Remove quick_symbol_functions::expand_matching_symbols 2023-12-06 10:14:25 -07:00
read-gdb-index.h gdb/dwarf2: split .gdb_index reading code to own file 2023-02-15 15:12:01 -05:00
read.c Remove quick_symbol_functions::expand_matching_symbols 2023-12-06 10:14:25 -07:00
read.h [gdb/symtab] Redo "Fix assert in set_length" 2023-12-06 10:29:17 +01:00
sect-names.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
section.c Rename bfd_bread and bfd_bwrite 2023-08-09 08:48:09 +09:30
section.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
stringify.c Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
stringify.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
tag.h Update copyright year range in header of all files managed by GDB 2023-01-01 17:01:16 +04:00
types.h gdb: update some copyright years (2022 -> 2023) 2023-03-01 20:54:56 -05:00