[06/26] gdbserver: turn part of get_thread_regcache into regcache::fetch

Message ID d2bd6c9812d5565f7ce91501892680ba54ec4522.1677582744.git.tankut.baris.aktemur@intel.com
State New
Headers
Series gdbserver: refactor regcache and allow gradually populating |

Commit Message

Tankut Baris Aktemur Feb. 28, 2023, 11:28 a.m. UTC
  Extract out a piece of code in get_thread_regcache that fetches the
registers from the target, and turn it into a method of 'regcache'.
---
 gdbserver/regcache.cc | 23 +++++++++++++++--------
 gdbserver/regcache.h  |  3 +++
 2 files changed, 18 insertions(+), 8 deletions(-)
  

Patch

diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index 2a8dc17ed6a..89ecdfec6f3 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -48,19 +48,26 @@  get_thread_regcache (struct thread_info *thread, bool fetch)
       regcache->thread = thread;
     }
 
-  if (fetch && regcache->registers_valid == 0)
+  if (fetch)
+    regcache->fetch ();
+
+  return regcache;
+}
+
+void
+regcache::fetch ()
+{
+  if (registers_valid == 0)
     {
       scoped_restore_current_thread restore_thread;
+      gdb_assert (this->thread != nullptr);
+      switch_to_thread (this->thread);
 
-      switch_to_thread (thread);
       /* Invalidate all registers, to prevent stale left-overs.  */
-      memset (regcache->register_status, REG_UNAVAILABLE,
-	      regcache->tdesc->reg_defs.size ());
-      fetch_inferior_registers (regcache, -1);
-      regcache->registers_valid = 1;
+      memset (register_status, REG_UNAVAILABLE, tdesc->reg_defs.size ());
+      fetch_inferior_registers (this, -1);
+      registers_valid = 1;
     }
-
-  return regcache;
 }
 
 /* See gdbsupport/common-regcache.h.  */
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index 053ed08b20f..7eae6cb724a 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -67,6 +67,9 @@  struct regcache : public reg_buffer_common
 
   /* See gdbsupport/common-regcache.h.  */
   bool raw_compare (int regnum, const void *buf, int offset) const override;
+
+  /* Fetch the registers from the target, if not done already.  */
+  void fetch ();
 };
 
 void regcache_cpy (struct regcache *dst, struct regcache *src);