[PATCH v2 09/11] gdbserver: define and use regcache::reset

Tankut Baris Aktemur tankut.baris.aktemur@intel.com
Mon Dec 30 10:49:50 GMT 2024


Define a `reset` method for a regcache and use it for code
simplification.  This patch allows further simplification in the next
patch.

The reset method fills the register data with zeroes.  For the use in
get_thread_regcache, this is added behavior, making the patch not a
pure refactoring, and may look like extra overhead.  However, it is
better to avoid having arbitrary values left in the data buffer.
Hence, it is considered a behavioral improvement.
---
 gdbserver/regcache.cc | 30 ++++++++++++++++--------------
 gdbserver/regcache.h  |  4 ++++
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index 03e975dc00154d01798765273534ca9cdcdc7eb7..7e1a722b0f80b9e5641bddfbbe95cbcf2b228df8 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -52,8 +52,7 @@ get_thread_regcache (thread_info *thread, bool fetch)
 
       switch_to_thread (thread);
       /* Invalidate all registers, to prevent stale left-overs.  */
-      memset (regcache->register_status, REG_UNKNOWN,
-	      regcache->tdesc->reg_defs.size ());
+      regcache->reset (REG_UNKNOWN);
       fetch_inferior_registers (regcache, -1);
       regcache->registers_fetched = true;
     }
@@ -138,12 +137,11 @@ regcache::regcache (const target_desc *tdesc)
      garbage.  */
   this->tdesc = tdesc;
   this->registers
-    = (unsigned char *) xcalloc (1, tdesc->registers_size);
+    = (unsigned char *) xmalloc (tdesc->registers_size);
   this->registers_owned = true;
   this->register_status
     = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
-  memset ((void *) this->register_status, REG_UNKNOWN,
-	  tdesc->reg_defs.size ());
+  reset (REG_UNKNOWN);
 }
 
 regcache::~regcache ()
@@ -155,6 +153,18 @@ regcache::~regcache ()
 
 #endif
 
+void
+regcache::reset (enum register_status status)
+{
+  /* Zero-initialize the register cache, in case there are registers
+     the target never fetches.  This way they'll read as zero instead
+     of garbage.  */
+  memset (this->registers, 0, this->tdesc->registers_size);
+#ifndef IN_PROCESS_AGENT
+  memset (this->register_status, status, this->tdesc->reg_defs.size ());
+#endif
+}
+
 void
 regcache::copy_from (regcache *src)
 {
@@ -370,15 +380,7 @@ supply_regblock (struct regcache *regcache, const void *buf)
 #endif
     }
   else
-    {
-      const struct target_desc *tdesc = regcache->tdesc;
-
-      memset (regcache->registers, 0, tdesc->registers_size);
-#ifndef IN_PROCESS_AGENT
-      for (int i = 0; i < tdesc->reg_defs.size (); i++)
-	regcache->set_register_status (i, REG_UNAVAILABLE);
-#endif
-    }
+    regcache->reset (REG_UNAVAILABLE);
 }
 
 #ifndef IN_PROCESS_AGENT
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index f3e99de443b884e13b02c8d858ea9e6ee668700f..b5a0b85982515dd545c6f79688ba7a4fd177b3d6 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -56,6 +56,10 @@ struct regcache : public reg_buffer_common
 
   DISABLE_COPY_AND_ASSIGN (regcache);
 
+  /* Clear the register values to all zeros and set the register
+     statuses to STATUS.  */
+  void reset (enum register_status status);
+
   /* See gdbsupport/common-regcache.h.  */
   enum register_status get_register_status (int regnum) const override;
 

-- 
2.34.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


More information about the Gdb-patches mailing list