[binutils-gdb] Windows gdb: Eliminate reload_context

Pedro Alves via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5f226ea6ab18538540e6394d63cb7ac0abdd59d8

commit 5f226ea6ab18538540e6394d63cb7ac0abdd59d8
Author: Pedro Alves <[email protected]>
Date:   Tue Apr 30 15:33:58 2024 +0100

    Windows gdb: Eliminate reload_context
    
    We don't need reload_context, because we can get the same information
    out of th->context.ContextFlags.  If ContextFlags is zero, then we
    need to fetch the context out of the inferior thread.  This is what
    gdbserver does too.
    
    Approved-By: Tom Tromey <[email protected]>
    Change-Id: Ied566037c81383414c46c77713bdd1aec6377b23

Diff:
---
 gdb/aarch64-windows-nat.c | 18 ++++++++++++++++--
 gdb/nat/windows-nat.h     |  4 ----
 gdb/windows-nat.c         | 16 ++++------------
 gdb/windows-nat.h         |  3 +++
 gdb/x86-windows-nat.c     | 21 ++++++++++++++++++---
 5 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/gdb/aarch64-windows-nat.c b/gdb/aarch64-windows-nat.c
index 4ae2c758f15..c375bbf2ddd 100644
--- a/gdb/aarch64-windows-nat.c
+++ b/gdb/aarch64-windows-nat.c
@@ -29,6 +29,8 @@ using namespace windows_nat;
 struct aarch64_windows_per_inferior : public windows_per_inferior
 {
   aarch64_debug_reg_state dr_state;
+
+  void invalidate_thread_context (windows_thread_info *th) override;
 };
 
 struct aarch64_windows_nat_target final
@@ -181,8 +183,20 @@ aarch64_windows_nat_target::fill_thread_context (windows_thread_info *th)
 {
   CONTEXT *context = &th->context;
 
-  context->ContextFlags = WindowsContext<decltype(context)>::all;
-  CHECK (get_thread_context (th->h, context));
+  if (context->ContextFlags == 0)
+    {
+      context->ContextFlags = WindowsContext<decltype(context)>::all;
+      CHECK (get_thread_context (th->h, context));
+    }
+}
+
+/* See windows-nat.h.  */
+
+void
+aarch64_windows_per_inferior::invalidate_thread_context (windows_thread_info *th)
+{
+  CONTEXT *context = &th->context;
+  context->ContextFlags = 0;
 }
 
 /* See windows-nat.h.  */
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 97c8c6cc43d..24ef6cc3f9c 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -90,10 +90,6 @@ struct windows_thread_info
      the thread.  */
   bool debug_registers_changed = false;
 
-  /* Nonzero if CONTEXT is invalidated and must be re-read from the
-     inferior thread.  */
-  bool reload_context = false;
-
   /* True if this thread is currently stopped at a software
      breakpoint.  This is used to offset the PC when needed.  */
   bool stopped_at_software_breakpoint = false;
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index ff23791e5a8..e8df5e053e6 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -287,11 +287,11 @@ windows_per_inferior::thread_rec (ptid_t ptid,
 	case INVALIDATE_CONTEXT:
 	  if (ptid.lwp () != current_event.dwThreadId)
 	    th->suspend ();
-	  th->reload_context = true;
+	  invalidate_thread_context (th);
 	  break;
 	case DONT_SUSPEND:
-	  th->reload_context = true;
 	  th->suspended = -1;
+	  invalidate_thread_context (th);
 	  break;
 	}
     }
@@ -400,12 +400,7 @@ windows_nat_target::fetch_registers (struct regcache *regcache, int r)
   if (th == NULL)
     return;
 
-  if (th->reload_context)
-    {
-      fill_thread_context (th);
-
-      th->reload_context = false;
-    }
+  fill_thread_context (th);
 
   if (r < 0)
     for (r = 0; r < gdbarch_num_regs (regcache->arch()); r++)
@@ -938,10 +933,7 @@ windows_nat_target::get_windows_debug_event
       *ourstatus = stop->status;
 
       ptid_t ptid (windows_process->current_event.dwProcessId, thread_id);
-      windows_thread_info *th
-	= windows_process->thread_rec (ptid, INVALIDATE_CONTEXT);
-      th->reload_context = true;
-
+      windows_process->thread_rec (ptid, INVALIDATE_CONTEXT);
       return ptid;
     }
 
diff --git a/gdb/windows-nat.h b/gdb/windows-nat.h
index a686c5b6679..aa90dfd37d3 100644
--- a/gdb/windows-nat.h
+++ b/gdb/windows-nat.h
@@ -55,6 +55,9 @@ struct windows_per_inferior : public windows_nat::windows_process_info
   void handle_unload_dll () override;
   bool handle_access_violation (const EXCEPTION_RECORD *rec) override;
 
+  /* Invalidate the thread context.  */
+  virtual void invalidate_thread_context (windows_thread_info *th) = 0;
+
   int windows_initialization_done = 0;
 
   std::vector<std::unique_ptr<windows_thread_info>> thread_list;
diff --git a/gdb/x86-windows-nat.c b/gdb/x86-windows-nat.c
index 5a82a2e0e36..d76b89b186a 100644
--- a/gdb/x86-windows-nat.c
+++ b/gdb/x86-windows-nat.c
@@ -47,6 +47,8 @@ struct x86_windows_per_inferior : public windows_per_inferior
   /* The function to use in order to determine whether a register is
      a segment register or not.  */
   segment_register_p_ftype *segment_register_p = nullptr;
+
+  void invalidate_thread_context (windows_thread_info *th) override;
 };
 
 struct x86_windows_nat_target final : public x86_nat_target<windows_nat_target>
@@ -107,8 +109,22 @@ x86_windows_nat_target::fill_thread_context (windows_thread_info *th)
 {
   x86_windows_process.with_context (th, [&] (auto *context)
     {
-      context->ContextFlags = WindowsContext<decltype(context)>::all;
-      CHECK (get_thread_context (th->h, context));
+      if (context->ContextFlags == 0)
+	{
+	  context->ContextFlags = WindowsContext<decltype(context)>::all;
+	  CHECK (get_thread_context (th->h, context));
+	}
+    });
+}
+
+/* See nat/windows-nat.h.  */
+
+void
+x86_windows_per_inferior::invalidate_thread_context (windows_thread_info *th)
+{
+  x86_windows_process.with_context (th, [&] (auto *context)
+    {
+      context->ContextFlags = 0;
     });
 }
 
@@ -170,7 +186,6 @@ x86_windows_nat_target::fetch_one_register (struct regcache *regcache,
 					    windows_thread_info *th, int r)
 {
   gdb_assert (r >= 0);
-  gdb_assert (!th->reload_context);
 
   char *context_ptr = x86_windows_process.with_context (th, [] (auto *context)
     {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.