[v3,14/29] Share thread_rec between gdb and gdbserver

Message ID 20200313190855.28662-15-tromey@adacore.com
State New
Headers show
Series
  • Windows code sharing + bug fix
Related show

Commit Message

Tom Tromey March 13, 2020, 7:08 p.m.
This changes gdb and gdbserver to use the same calling convention for
the "thread_rec" helper function.  Fully merging these is difficult
due to differences in how threads are managed by the enclosing
applications; but sharing a declaration makes it possible for future
shared code to call this method.

gdb/ChangeLog
2020-03-13  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (enum thread_disposition_type): Move to
	nat/windows-nat.h.
	(windows_nat::thread_rec): Rename from thread_rec.  No longer
	static.
	(windows_add_thread, windows_nat_target::fetch_registers)
	(windows_nat_target::store_registers, handle_exception)
	(windows_nat_target::resume, get_windows_debug_event)
	(windows_nat_target::get_tib_address)
	(windows_nat_target::thread_name)
	(windows_nat_target::thread_alive): Update.
	* nat/windows-nat.h (enum thread_disposition_type): Move from
	windows-nat.c.
	(thread_rec): Declare.

gdbserver/ChangeLog
2020-03-13  Tom Tromey  <tromey@adacore.com>

	* win32-low.c (windows_nat::thread_rec): Rename from thread_rec.
	No longer static.  Change parameters.
	(child_add_thread, child_fetch_inferior_registers)
	(child_store_inferior_registers, win32_resume)
	(win32_get_tib_address): Update.
---
 gdb/ChangeLog          | 16 +++++++++++
 gdb/nat/windows-nat.h  | 21 +++++++++++++++
 gdb/windows-nat.c      | 61 +++++++++++++++---------------------------
 gdbserver/ChangeLog    |  8 ++++++
 gdbserver/win32-low.cc | 22 ++++++++-------
 5 files changed, 79 insertions(+), 49 deletions(-)

-- 
2.21.1

Patch

diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index df283646753..e63ef753c92 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -90,6 +90,27 @@  struct windows_thread_info
   gdb::unique_xmalloc_ptr<char> name;
 };
 
+
+/* Possible values to pass to 'thread_rec'.  */
+enum thread_disposition_type
+{
+  /* Do not invalidate the thread's context, and do not suspend the
+     thread.  */
+  DONT_INVALIDATE_CONTEXT,
+  /* Invalidate the context, but do not suspend the thread.  */
+  DONT_SUSPEND,
+  /* Invalidate the context and suspend the thread.  */
+  INVALIDATE_CONTEXT
+};
+
+/* Find a thread record given a thread id.  THREAD_DISPOSITION
+   controls whether the thread is suspended, and whether the context
+   is invalidated.
+
+   This function must be supplied by the embedding application.  */
+extern windows_thread_info *thread_rec (ptid_t ptid,
+					thread_disposition_type disposition);
+
 }
 
 #endif
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index b05a27c7cac..1192b9abac4 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -456,26 +456,13 @@  check (BOOL ok, const char *file, int line)
 		     (unsigned) GetLastError ());
 }
 
-/* Possible values to pass to 'thread_rec'.  */
-enum thread_disposition_type
-{
-  /* Do not invalidate the thread's context, and do not suspend the
-     thread.  */
-  DONT_INVALIDATE_CONTEXT,
-  /* Invalidate the context, but do not suspend the thread.  */
-  DONT_SUSPEND,
-  /* Invalidate the context and suspend the thread.  */
-  INVALIDATE_CONTEXT
-};
+/* See nat/windows-nat.h.  */
 
-/* Find a thread record given a thread id.  THREAD_DISPOSITION
-   controls whether the thread is suspended, and whether the context
-   is invalidated.  */
-static windows_thread_info *
-thread_rec (DWORD id, enum thread_disposition_type disposition)
+windows_thread_info *
+windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition)
 {
   for (windows_thread_info *th : thread_list)
-    if (th->tid == id)
+    if (th->tid == ptid.lwp ())
       {
 	if (!th->suspended)
 	  {
@@ -485,7 +472,7 @@  thread_rec (DWORD id, enum thread_disposition_type disposition)
 		/* Nothing.  */
 		break;
 	      case INVALIDATE_CONTEXT:
-		if (id != current_event.dwThreadId)
+		if (ptid.lwp () != current_event.dwThreadId)
 		  th->suspend ();
 		th->reload_context = true;
 		break;
@@ -513,13 +500,10 @@  static windows_thread_info *
 windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
 {
   windows_thread_info *th;
-  DWORD id;
 
   gdb_assert (ptid.lwp () != 0);
 
-  id = ptid.lwp ();
-
-  if ((th = thread_rec (id, DONT_INVALIDATE_CONTEXT)))
+  if ((th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
     return th;
 
   CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb;
@@ -529,7 +513,7 @@  windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
   if (wow64_process)
     base += 0x2000;
 #endif
-  th = new windows_thread_info (id, h, base);
+  th = new windows_thread_info (ptid.lwp (), h, base);
   thread_list.push_back (th);
 
   /* Add this new thread to the list of threads.
@@ -716,8 +700,7 @@  windows_fetch_one_register (struct regcache *regcache,
 void
 windows_nat_target::fetch_registers (struct regcache *regcache, int r)
 {
-  DWORD tid = regcache->ptid ().lwp ();
-  windows_thread_info *th = thread_rec (tid, INVALIDATE_CONTEXT);
+  windows_thread_info *th = thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -812,8 +795,7 @@  windows_store_one_register (const struct regcache *regcache,
 void
 windows_nat_target::store_registers (struct regcache *regcache, int r)
 {
-  DWORD tid = regcache->ptid ().lwp ();
-  windows_thread_info *th = thread_rec (tid, INVALIDATE_CONTEXT);
+  windows_thread_info *th = thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -1353,7 +1335,8 @@  handle_exception (struct target_waitstatus *ourstatus)
   ourstatus->kind = TARGET_WAITKIND_STOPPED;
 
   /* Record the context of the current thread.  */
-  thread_rec (current_event.dwThreadId, DONT_SUSPEND);
+  thread_rec (ptid_t (current_event.dwProcessId, current_event.dwThreadId, 0),
+	      DONT_SUSPEND);
 
   switch (code)
     {
@@ -1483,7 +1466,9 @@  handle_exception (struct target_waitstatus *ourstatus)
 	  if (named_thread_id == (DWORD) -1)
 	    named_thread_id = current_event.dwThreadId;
 
-	  named_thread = thread_rec (named_thread_id, DONT_INVALIDATE_CONTEXT);
+	  named_thread = thread_rec (ptid_t (current_event.dwProcessId,
+					     named_thread_id, 0),
+				     DONT_INVALIDATE_CONTEXT);
 	  if (named_thread != NULL)
 	    {
 	      int thread_name_len;
@@ -1714,7 +1699,7 @@  windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
 	       ptid.pid (), (unsigned) ptid.lwp (), step, sig));
 
   /* Get context for currently selected thread.  */
-  th = thread_rec (inferior_ptid.lwp (), DONT_INVALIDATE_CONTEXT);
+  th = thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT);
   if (th)
     {
 #ifdef __x86_64__
@@ -1847,7 +1832,7 @@  windows_nat_target::get_windows_debug_event (int pid,
 	  current_event = iter->event;
 
 	  inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
-	  current_thread = thread_rec (thread_id, INVALIDATE_CONTEXT);
+	  current_thread = thread_rec (inferior_ptid, INVALIDATE_CONTEXT);
 	  current_thread->reload_context = 1;
 
 	  DEBUG_EVENTS (("get_windows_debug_event - "
@@ -2060,7 +2045,8 @@  windows_nat_target::get_windows_debug_event (int pid,
 	      == EXCEPTION_BREAKPOINT)
 	  && windows_initialization_done)
 	{
-	  th = thread_rec (thread_id, INVALIDATE_CONTEXT);
+	  ptid_t ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
+	  th = thread_rec (ptid, INVALIDATE_CONTEXT);
 	  th->stopped_at_software_breakpoint = true;
 	}
       pending_stops.push_back ({thread_id, *ourstatus, current_event});
@@ -2072,7 +2058,7 @@  windows_nat_target::get_windows_debug_event (int pid,
       inferior_ptid = ptid_t (current_event.dwProcessId, thread_id, 0);
       current_thread = th;
       if (!current_thread)
-	current_thread = thread_rec (thread_id, INVALIDATE_CONTEXT);
+	current_thread = thread_rec (inferior_ptid, INVALIDATE_CONTEXT);
     }
 
 out:
@@ -3505,7 +3491,7 @@  windows_nat_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
 {
   windows_thread_info *th;
 
-  th = thread_rec (ptid.lwp (), DONT_INVALIDATE_CONTEXT);
+  th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
   if (th == NULL)
     return false;
 
@@ -3526,7 +3512,7 @@  windows_nat_target::get_ada_task_ptid (long lwp, long thread)
 const char *
 windows_nat_target::thread_name (struct thread_info *thr)
 {
-  return thread_rec (thr->ptid.lwp (), DONT_INVALIDATE_CONTEXT)->name.get ();
+  return thread_rec (thr->ptid, DONT_INVALIDATE_CONTEXT)->name.get ();
 }
 
 
@@ -3685,12 +3671,9 @@  cygwin_get_dr7 (void)
 bool
 windows_nat_target::thread_alive (ptid_t ptid)
 {
-  int tid;
-
   gdb_assert (ptid.lwp () != 0);
-  tid = ptid.lwp ();
 
-  return (WaitForSingleObject (thread_rec (tid, DONT_INVALIDATE_CONTEXT)->h, 0)
+  return (WaitForSingleObject (thread_rec (ptid, DONT_INVALIDATE_CONTEXT)->h, 0)
 	  != WAIT_OBJECT_0);
 }
 
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 8f8b6cedd28..1e86b3b5926 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -178,17 +178,17 @@  win32_require_context (windows_thread_info *th)
     }
 }
 
-/* Find a thread record given a thread id.  If GET_CONTEXT is set then
-   also retrieve the context for this thread.  */
-static windows_thread_info *
-thread_rec (ptid_t ptid, int get_context)
+/* See nat/windows-nat.h.  */
+
+windows_thread_info *
+windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition)
 {
   thread_info *thread = find_thread_ptid (ptid);
   if (thread == NULL)
     return NULL;
 
   windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
-  if (get_context)
+  if (disposition != DONT_INVALIDATE_CONTEXT)
     win32_require_context (th);
   return th;
 }
@@ -200,7 +200,7 @@  child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
   windows_thread_info *th;
   ptid_t ptid = ptid_t (pid, tid, 0);
 
-  if ((th = thread_rec (ptid, FALSE)))
+  if ((th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
     return th;
 
   th = new windows_thread_info (tid, h, (CORE_ADDR) (uintptr_t) tlb);
@@ -454,7 +454,8 @@  static void
 child_fetch_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
-  windows_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
+  windows_thread_info *th = thread_rec (current_thread_ptid (),
+					INVALIDATE_CONTEXT);
   if (r == -1 || r > NUM_REGS)
     child_fetch_inferior_registers (regcache, NUM_REGS);
   else
@@ -468,7 +469,8 @@  static void
 child_store_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
-  windows_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
+  windows_thread_info *th = thread_rec (current_thread_ptid (),
+					INVALIDATE_CONTEXT);
   if (r == -1 || r == 0 || r > NUM_REGS)
     child_store_inferior_registers (regcache, NUM_REGS);
   else
@@ -937,7 +939,7 @@  win32_process_target::resume (thread_resume *resume_info, size_t n)
 
   /* Get context for the currently selected thread.  */
   ptid = debug_event_ptid (&current_event);
-  th = thread_rec (ptid, FALSE);
+  th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
   if (th)
     {
       win32_prepare_to_resume (th);
@@ -1807,7 +1809,7 @@  int
 win32_process_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
 {
   windows_thread_info *th;
-  th = thread_rec (ptid, 0);
+  th = thread_rec (ptid, DONT_INVALIDATE_CONTEXT);
   if (th == NULL)
     return 0;
   if (addr != NULL)