[06/34] Windows gdb+gdbserver: Eliminate thread_rec(INVALIDATE_CONTEXT) calls

Message ID 20240507234233.371123-7-pedro@palves.net
State New
Headers
Series Windows non-stop mode |

Commit Message

Pedro Alves May 7, 2024, 11:42 p.m. UTC
  Replace thread_rec(INVALIDATE_CONTEXT) calls with find_thread, and
invalidate_context / suspend calls in the spots that might need those.

I don't know why does the INVALIDATE_CONTEXT implementation in GDB
avoid suspending the event thread:

	case INVALIDATE_CONTEXT:
	  if (ptid.lwp () != current_event.dwThreadId)
	    th->suspend ();

Checks for a global "current_event" get in the way of non-stop support
later in the series, as each thread will have its own "last debug
event".  Regardless, it should be fine to suspend the event thread.
As a data point, the GDBserver implementation always suspends.  So
this patch does not try to avoid suspending the event thread on the
native side either.

Change-Id: I8d2f0a749d23329956e62362a7007189902dddb5
---
 gdb/nat/windows-nat.h  |  2 --
 gdb/windows-nat.c      | 38 ++++++++++++++++++++++----------------
 gdbserver/win32-low.cc |  8 ++++----
 3 files changed, 26 insertions(+), 22 deletions(-)
  

Comments

Tom Tromey May 8, 2024, 3:08 p.m. UTC | #1
>>>>> "Pedro" == Pedro Alves <pedro@palves.net> writes:

Pedro> Replace thread_rec(INVALIDATE_CONTEXT) calls with find_thread, and
Pedro> invalidate_context / suspend calls in the spots that might need those.

This looks good to me.
Approved-By: Tom Tromey <tom@tromey.com>

Pedro> I don't know why does the INVALIDATE_CONTEXT implementation in GDB
Pedro> avoid suspending the event thread:

Pedro> 	case INVALIDATE_CONTEXT:
Pedro> 	  if (ptid.lwp () != current_event.dwThreadId)
Pedro>        th-> suspend ();

Me neither.  However, the suspend/resume code in gdb/windows-nat.c has
always seemed pretty suspect to me.  Like, I think in all-stop, there
was probably normally no need for any of those suspend calls -- only
when scheduler-locking was enabled.  At least, my understanding is that
on a stop, Windows is already suspending all threads.

Tom
  

Patch

diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 41921aa653b..0e2093ee06d 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -103,8 +103,6 @@  enum thread_disposition_type
 {
   /* Invalidate the context, but do not suspend the thread.  */
   DONT_SUSPEND,
-  /* Invalidate the context and suspend the thread.  */
-  INVALIDATE_CONTEXT
 };
 
 /* A single pending stop.  See "pending_stops" for more
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 3888ab6a02c..f53a45f657f 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -547,11 +547,6 @@  windows_per_inferior::thread_rec
     {
       switch (disposition)
 	{
-	case INVALIDATE_CONTEXT:
-	  if (ptid.lwp () != current_event.dwThreadId)
-	    th->suspend ();
-	  invalidate_context (th);
-	  break;
 	case DONT_SUSPEND:
 	  th->suspended = -1;
 	  invalidate_context (th);
@@ -724,8 +719,7 @@  windows_fetch_one_register (struct regcache *regcache,
 void
 windows_nat_target::fetch_registers (struct regcache *regcache, int r)
 {
-  windows_thread_info *th
-    = windows_process.thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
+  windows_thread_info *th = windows_process.find_thread (regcache->ptid ());
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -737,6 +731,7 @@  windows_nat_target::fetch_registers (struct regcache *regcache, int r)
     {
       if (th->wow64_context.ContextFlags == 0)
 	{
+	  th->suspend ();
 	  th->wow64_context.ContextFlags = CONTEXT_DEBUGGER_DR;
 	  CHECK (Wow64GetThreadContext (th->h, &th->wow64_context));
 	}
@@ -746,6 +741,7 @@  windows_nat_target::fetch_registers (struct regcache *regcache, int r)
     {
       if (th->context.ContextFlags == 0)
 	{
+	  th->suspend ();
 	  th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
 	  CHECK (GetThreadContext (th->h, &th->context));
 	}
@@ -770,11 +766,23 @@  windows_store_one_register (const struct regcache *regcache,
 {
   gdb_assert (r >= 0);
 
-  char *context_ptr = (char *) &th->context;
+  DWORD context_flags;
+  char *context_ptr;
+
 #ifdef __x86_64__
   if (windows_process.wow64_process)
-    context_ptr = (char *) &th->wow64_context;
+    {
+      context_flags = th->wow64_context.ContextFlags;
+      context_ptr = (char *) &th->wow64_context;
+    }
+  else
 #endif
+    {
+      context_flags = th->context.ContextFlags;
+      context_ptr = (char *) &th->context;
+    }
+
+  gdb_assert (context_flags != 0);
 
   struct gdbarch *gdbarch = regcache->arch ();
   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
@@ -807,8 +815,7 @@  windows_store_one_register (const struct regcache *regcache,
 void
 windows_nat_target::store_registers (struct regcache *regcache, int r)
 {
-  windows_thread_info *th
-    = windows_process.thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
+  windows_thread_info *th = windows_process.find_thread (regcache->ptid ());
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -1544,7 +1551,8 @@  windows_nat_target::get_windows_debug_event
       *ourstatus = stop->status;
 
       ptid_t ptid (windows_process.current_event.dwProcessId, thread_id);
-      windows_process.thread_rec (ptid, INVALIDATE_CONTEXT);
+      windows_thread_info *th = windows_process.find_thread (ptid);
+      windows_process.invalidate_context (th);
       return ptid;
     }
 
@@ -1765,8 +1773,7 @@  windows_nat_target::get_windows_debug_event
 	  && windows_process.windows_initialization_done)
 	{
 	  ptid_t ptid = ptid_t (current_event->dwProcessId, thread_id, 0);
-	  windows_thread_info *th
-	    = windows_process.thread_rec (ptid, INVALIDATE_CONTEXT);
+	  windows_thread_info *th = windows_process.find_thread (ptid);
 	  th->stopped_at_software_breakpoint = true;
 	  th->pc_adjusted = false;
 	}
@@ -1804,8 +1811,7 @@  windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 	  if (ourstatus->kind () != TARGET_WAITKIND_EXITED
 	      && ourstatus->kind () !=  TARGET_WAITKIND_SIGNALLED)
 	    {
-	      windows_thread_info *th
-		= windows_process.thread_rec (result, INVALIDATE_CONTEXT);
+	      windows_thread_info *th = windows_process.find_thread (result);
 
 	      if (th != nullptr)
 		{
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 5d3ad38cdbb..b693da583b0 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -463,8 +463,8 @@  child_fetch_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
   windows_thread_info *th
-    = windows_process.thread_rec (current_thread_ptid (),
-				  INVALIDATE_CONTEXT);
+    = windows_process.find_thread (current_thread_ptid ());
+  win32_require_context (th);
   if (r == -1 || r > NUM_REGS)
     child_fetch_inferior_registers (regcache, NUM_REGS);
   else
@@ -479,8 +479,8 @@  child_store_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
   windows_thread_info *th
-    = windows_process.thread_rec (current_thread_ptid (),
-				  INVALIDATE_CONTEXT);
+    = windows_process.find_thread (current_thread_ptid ());
+  win32_require_context (th);
   if (r == -1 || r == 0 || r > NUM_REGS)
     child_store_inferior_registers (regcache, NUM_REGS);
   else