diff mbox series

[RFC,v4,01/15] GDB, gdbserver: Convert regcache_register_size function to method

Message ID 20241102025635.586759-2-thiago.bauermann@linaro.org
State New
Headers show
Series gdbserver improvements for AArch64 SVE support | expand

Commit Message

Thiago Jung Bauermann Nov. 2, 2024, 2:56 a.m. UTC
The regcache_register_size function has one implementation in GDB, and
one in gdbserver.  Both of them have a gdb::checked_static_cast to their
corresponding regcache class.  This can be avoided by defining a
pure virtual register_size method in the
reg_buffer_common class, which is then implemented by the reg_buffer
class in GDB, and by the regcache class in gdbserver.

Calls to the register_size () function from methods of classes in the
reg_buffer_common hierarchy need to be changed to calls to the newly
defined method, otherwise the compiler complains that a matching method
cannot be found.
---
 gdb/i387-tdep.c              |  4 ++--
 gdb/nat/aarch64-hw-point.c   |  2 +-
 gdb/regcache-dump.c          |  2 +-
 gdb/regcache.c               | 18 ++++++++----------
 gdb/regcache.h               |  3 +++
 gdbserver/regcache.cc        |  5 ++---
 gdbserver/regcache.h         |  3 +++
 gdbsupport/common-regcache.h | 16 +++++-----------
 8 files changed, 25 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/gdb/i387-tdep.c b/gdb/i387-tdep.c
index 3bda88807943..92f898caf770 100644
--- a/gdb/i387-tdep.c
+++ b/gdb/i387-tdep.c
@@ -1565,7 +1565,7 @@  i387_collect_xsave (const struct regcache *regcache, int regnum,
 					byte_order, I387_FCTRL_INIT_VAL);
 	      else
 		memset (FXSAVE_ADDR (tdep, regs, i), 0,
-			regcache_register_size (regcache, i));
+			regcache->register_size (i));
 	    }
 	}
     }
@@ -1888,7 +1888,7 @@  i387_collect_xsave (const struct regcache *regcache, int regnum,
 	    int regsize;
 
 	    regcache->raw_collect (i, raw);
-	    regsize = regcache_register_size (regcache, i);
+	    regsize = regcache->register_size (i);
 	    p = FXSAVE_ADDR (tdep, regs, i);
 	    if (memcmp (raw, p, regsize))
 	      {
diff --git a/gdb/nat/aarch64-hw-point.c b/gdb/nat/aarch64-hw-point.c
index 6acee0fb814c..8ab91fe85142 100644
--- a/gdb/nat/aarch64-hw-point.c
+++ b/gdb/nat/aarch64-hw-point.c
@@ -166,7 +166,7 @@  aarch64_point_is_aligned (ptid_t ptid, int is_watchpoint, CORE_ADDR addr,
       /* Set alignment to 2 only if the current process is 32-bit,
 	 since thumb instruction can be 2-byte aligned.  Otherwise, set
 	 alignment to AARCH64_HBP_ALIGNMENT.  */
-      if (regcache_register_size (regcache, 0) == 8)
+      if (regcache->register_size (0) == 8)
 	alignment = AARCH64_HBP_ALIGNMENT;
       else
 	alignment = 2;
diff --git a/gdb/regcache-dump.c b/gdb/regcache-dump.c
index 3e6880535235..0a851e3ed771 100644
--- a/gdb/regcache-dump.c
+++ b/gdb/regcache-dump.c
@@ -110,7 +110,7 @@  class register_dump_reg_buffer : public register_dump, reg_buffer
       {
 	if (regnum < gdbarch_num_regs (m_gdbarch) || m_has_pseudo)
 	  {
-	    auto size = register_size (m_gdbarch, regnum);
+	    auto size = register_size (regnum);
 
 	    if (size == 0)
 	      return;
diff --git a/gdb/regcache.c b/gdb/regcache.c
index f04354d822f9..6e0c730d0d59 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -179,10 +179,9 @@  register_size (struct gdbarch *gdbarch, int regnum)
 /* See gdbsupport/common-regcache.h.  */
 
 int
-regcache_register_size (const reg_buffer_common *regcache, int n)
+reg_buffer::register_size (int regnum) const
 {
-  return register_size
-    (gdb::checked_static_cast<const struct regcache *> (regcache)->arch (), n);
+  return ::register_size (this->arch (), regnum);
 }
 
 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
@@ -939,7 +938,7 @@  register_status
 readable_regcache::read_part (int regnum, int offset,
 			      gdb::array_view<gdb_byte> dst, bool is_raw)
 {
-  int reg_size = register_size (arch (), regnum);
+  int reg_size = register_size (regnum);
 
   gdb_assert (offset >= 0);
   gdb_assert (offset + dst.size () <= reg_size);
@@ -983,7 +982,7 @@  void
 reg_buffer::raw_collect_part (int regnum, int offset,
 			      gdb::array_view<gdb_byte> dst) const
 {
-  int reg_size = register_size (arch (), regnum);
+  int reg_size = register_size (regnum);
 
   gdb_assert (offset >= 0);
   gdb_assert (offset + dst.size () <= reg_size);
@@ -1013,7 +1012,7 @@  register_status
 regcache::write_part (int regnum, int offset,
 		      gdb::array_view<const gdb_byte> src, bool is_raw)
 {
-  int reg_size = register_size (arch (), regnum);
+  int reg_size = register_size (regnum);
 
   gdb_assert (offset >= 0);
   gdb_assert (offset + src.size () <= reg_size);
@@ -1065,7 +1064,7 @@  void
 reg_buffer::raw_supply_part (int regnum, int offset,
 			     gdb::array_view<const gdb_byte> src)
 {
-  int reg_size = register_size (arch (), regnum);
+  int reg_size = register_size (regnum);
 
   gdb_assert (offset >= 0);
   gdb_assert (offset + src.size () <= reg_size);
@@ -1226,8 +1225,7 @@  regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
 				    const gdb_byte *in_buf, gdb_byte *out_buf,
 				    int slot_size, int offs) const
 {
-  struct gdbarch *gdbarch = arch ();
-  int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
+  int reg_size = std::min (register_size (regnum), slot_size);
 
   /* Use part versions and reg_size to prevent possible buffer overflows when
      accessing the regcache.  */
@@ -1244,7 +1242,7 @@  regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
   else if (in_buf != nullptr)
     {
       /* Zero-extend the register value if the slot is smaller than the register.  */
-      if (slot_size < register_size (gdbarch, regnum))
+      if (slot_size < register_size (regnum))
 	out_regcache->raw_supply_zeroed (regnum);
       out_regcache->raw_supply_part (regnum, 0,
 				     gdb::make_array_view (in_buf + offs,
diff --git a/gdb/regcache.h b/gdb/regcache.h
index 2f4b7d94c693..65e9f7bb79da 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -256,6 +256,9 @@  class reg_buffer : public reg_buffer_common
   /* See gdbsupport/common-regcache.h.  */
   bool raw_compare (int regnum, const void *buf, int offset) const override;
 
+  /* See gdbsupport/common-regcache.h.  */
+  int register_size (int regnum) const override;
+
 protected:
   /* Assert on the range of REGNUM.  */
   void assert_regnum (int regnum) const;
diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index 1bb71d103288..6a1526246867 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -308,10 +308,9 @@  register_size (const struct target_desc *tdesc, int n)
 /* See gdbsupport/common-regcache.h.  */
 
 int
-regcache_register_size (const reg_buffer_common *regcache, int n)
+regcache::register_size (int regnum) const
 {
-  return register_size
-    (gdb::checked_static_cast<const struct regcache *> (regcache)->tdesc, n);
+  return ::register_size (tdesc, regnum);
 }
 
 static gdb::array_view<gdb_byte>
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index 1752c3979d39..df0feca102e0 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -49,6 +49,9 @@  struct regcache : public reg_buffer_common
   /* See gdbsupport/common-regcache.h.  */
   enum register_status get_register_status (int regnum) const override;
 
+  /* See gdbsupport/common-regcache.h.  */
+  int register_size (int regnum) const override;
+
   /* See gdbsupport/common-regcache.h.  */
   void raw_supply (int regnum, gdb::array_view<const gdb_byte> src) override;
 
diff --git a/gdbsupport/common-regcache.h b/gdbsupport/common-regcache.h
index f8704c16939a..4594999346fd 100644
--- a/gdbsupport/common-regcache.h
+++ b/gdbsupport/common-regcache.h
@@ -48,11 +48,6 @@  enum register_status : signed char
 
 extern reg_buffer_common *get_thread_regcache_for_ptid (ptid_t ptid);
 
-/* Return the size of register numbered N in REGCACHE.  This function
-   must be provided by the client.  */
-
-extern int regcache_register_size (const reg_buffer_common *regcache, int n);
-
 /* Read the PC register.  This function must be provided by the
    client.  */
 
@@ -78,6 +73,9 @@  struct reg_buffer_common
      buffer.  */
   virtual register_status get_register_status (int regnum) const = 0;
 
+  /* Return the size of register numbered REGNUM in this buffer.  */
+  virtual int register_size (int regnum) const = 0;
+
   /* Supply register REGNUM, whose contents are stored in SRC, to this register
      buffer.  */
   virtual void raw_supply (int regnum, gdb::array_view<const gdb_byte> src)
@@ -91,9 +89,7 @@  struct reg_buffer_common
 
   void raw_supply (int regnum, const gdb_byte *src)
   {
-    raw_supply (regnum,
-		gdb::make_array_view (src,
-				      regcache_register_size (this, regnum)));
+    raw_supply (regnum, gdb::make_array_view (src, register_size (regnum)));
   }
 
   /* Collect register REGNUM from this register buffer and store its contents in
@@ -109,9 +105,7 @@  struct reg_buffer_common
 
   void raw_collect (int regnum, gdb_byte *dst)
   {
-    raw_collect (regnum,
-		 gdb::make_array_view (dst,
-				       regcache_register_size (this, regnum)));
+    raw_collect (regnum, gdb::make_array_view (dst, register_size (regnum)));
   }
 
   /* Compare the contents of the register stored in the regcache (ignoring the