public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/9] share some Windows code between gdb and gdbserver
@ 2019-10-14 16:15 Tom Tromey
  2019-10-14 16:15 ` [PATCH 7/9] Make windows_thread_info::name a unique_xmalloc_ptr Tom Tromey
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:15 UTC (permalink / raw)
  To: gdb-patches

While working on PR gdb/22992, I found that there is a lot of very
similar code in the Windows native code in gdb and gdbserver.  (Pedro
said that the gdbserver port was essentially copied from
windows-nat.c.)

This series starts sharing a bit of code between the two.  I tested
each patch using the AdaCore internal test suite.

There is still a lot more code that could be shared.  Some aspects of
that look a bit tricky, but meanwhile I thought this was a reasonable
place to start.

Tom


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 7/9] Make windows_thread_info::name a unique_xmalloc_ptr
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
@ 2019-10-14 16:15 ` Tom Tromey
  2019-10-14 16:16 ` [PATCH 1/9] Remove the "next" field from windows_thread_info Tom Tromey
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:15 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes windows_thread_info::name to be a unique_xmalloc_ptr,
removing some manual memory management.

2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (handle_exception)
	(windows_nat_target::thread_name): Update.
	* nat/windows-nat.h (windows_thread_info): Remove destructor.
	<name>: Now unique_xmalloc_ptr.
---
 gdb/ChangeLog         | 7 +++++++
 gdb/nat/windows-nat.h | 7 +------
 gdb/windows-nat.c     | 5 ++---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 0cfc0716f26..06c6486c0c0 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -30,11 +30,6 @@ struct windows_thread_info
   {
   }
 
-  ~windows_thread_info ()
-  {
-    xfree (name);
-  }
-
   DISABLE_COPY_AND_ASSIGN (windows_thread_info);
 
   /* The Win32 thread identifier.  */
@@ -68,7 +63,7 @@ struct windows_thread_info
   bool reload_context = false;
 
   /* The name of the thread, allocated by xmalloc.  */
-  char *name = nullptr;
+  gdb::unique_xmalloc_ptr<char> name;
 };
 
 #endif
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index b38ff402cb5..3a526e504e4 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1261,8 +1261,7 @@ handle_exception (struct target_waitstatus *ourstatus)
 	      if (thread_name_len > 0)
 		{
 		  thread_name.get ()[thread_name_len - 1] = '\0';
-		  xfree (named_thread->name);
-		  named_thread->name = thread_name.release ();
+		  named_thread->name = std::move (thread_name);
 		}
 	    }
 	  ourstatus->value.sig = GDB_SIGNAL_TRAP;
@@ -3000,7 +2999,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.tid (), 0)->name;
+  return thread_rec (thr->ptid.tid (), 0)->name.get ();
 }
 
 
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/9] Rename win32_thread_info to windows_thread_info
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (2 preceding siblings ...)
  2019-10-14 16:16 ` [PATCH 8/9] Share Windows thread-suspend and -resume code Tom Tromey
@ 2019-10-14 16:16 ` Tom Tromey
  2019-10-14 16:16 ` [PATCH 6/9] Change two windows_thread_info members to "bool" Tom Tromey
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This renames win32_thread_info to windows_thread_info in gdbserver.
This renaming helps make it possible to share some code between gdb
and gdbserver.

gdb/gdbserver/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* win32-low.h (struct windows_thread_info): Rename from
	win32_thread_info.  Remove typedef.
	(struct win32_target_ops, win32_require_context): Update.
	* win32-low.c (win32_get_thread_context)
	(win32_set_thread_context, win32_prepare_to_resume)
	(win32_require_context, thread_rec, child_add_thread)
	(delete_thread_info, continue_one_thread)
	(child_fetch_inferior_registers, child_store_inferior_registers)
	(win32_resume, suspend_one_thread, win32_get_tib_address):
	Update.
	* win32-i386-low.c (update_debug_registers)
	(win32_get_current_dr, i386_get_thread_context)
	(i386_prepare_to_resume, i386_thread_added, i386_single_step)
	(i386_fetch_inferior_register, i386_store_inferior_register):
	Update.
	* win32-arm-low.c (arm_get_thread_context)
	(arm_fetch_inferior_register, arm_store_inferior_register):
	Update.
---
 gdb/gdbserver/ChangeLog        | 21 +++++++++++++++++++++
 gdb/gdbserver/win32-arm-low.c  |  6 +++---
 gdb/gdbserver/win32-i386-low.c | 18 +++++++++---------
 gdb/gdbserver/win32-low.c      | 32 ++++++++++++++++----------------
 gdb/gdbserver/win32-low.h      | 18 +++++++++---------
 5 files changed, 58 insertions(+), 37 deletions(-)

diff --git a/gdb/gdbserver/win32-arm-low.c b/gdb/gdbserver/win32-arm-low.c
index 2d7dfde35cd..c465aedda88 100644
--- a/gdb/gdbserver/win32-arm-low.c
+++ b/gdb/gdbserver/win32-arm-low.c
@@ -27,7 +27,7 @@ void init_registers_arm (void);
 extern const struct target_desc *tdesc_arm;
 
 static void
-arm_get_thread_context (win32_thread_info *th)
+arm_get_thread_context (windows_thread_info *th)
 {
   th->context.ContextFlags = \
     CONTEXT_FULL | \
@@ -88,7 +88,7 @@ regptr (CONTEXT* c, int r)
 /* Fetch register from gdbserver regcache data.  */
 static void
 arm_fetch_inferior_register (struct regcache *regcache,
-			     win32_thread_info *th, int r)
+			     windows_thread_info *th, int r)
 {
   char *context_offset = regptr (&th->context, r);
   supply_register (regcache, r, context_offset);
@@ -97,7 +97,7 @@ arm_fetch_inferior_register (struct regcache *regcache,
 /* Store a new register value into the thread context of TH.  */
 static void
 arm_store_inferior_register (struct regcache *regcache,
-			     win32_thread_info *th, int r)
+			     windows_thread_info *th, int r)
 {
   collect_register (regcache, r, regptr (&th->context, r));
 }
diff --git a/gdb/gdbserver/win32-i386-low.c b/gdb/gdbserver/win32-i386-low.c
index 399520c11dc..3fc0cf1cd03 100644
--- a/gdb/gdbserver/win32-i386-low.c
+++ b/gdb/gdbserver/win32-i386-low.c
@@ -40,7 +40,7 @@ static struct x86_debug_reg_state debug_reg_state;
 static void
 update_debug_registers (thread_info *thread)
 {
-  win32_thread_info *th = (win32_thread_info *) thread_target_data (thread);
+  windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
 
   /* The actual update is done later just before resuming the lwp,
      we just mark that the registers need updating.  */
@@ -73,8 +73,8 @@ x86_dr_low_set_control (unsigned long control)
 static DWORD64
 win32_get_current_dr (int dr)
 {
-  win32_thread_info *th
-    = (win32_thread_info *) thread_target_data (current_thread);
+  windows_thread_info *th
+    = (windows_thread_info *) thread_target_data (current_thread);
 
   win32_require_context (th);
 
@@ -210,7 +210,7 @@ i386_initial_stuff (void)
 }
 
 static void
-i386_get_thread_context (win32_thread_info *th)
+i386_get_thread_context (windows_thread_info *th)
 {
   /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if
      the system doesn't support extended registers.  */
@@ -237,7 +237,7 @@ i386_get_thread_context (win32_thread_info *th)
 }
 
 static void
-i386_prepare_to_resume (win32_thread_info *th)
+i386_prepare_to_resume (windows_thread_info *th)
 {
   if (th->debug_registers_changed)
     {
@@ -258,13 +258,13 @@ i386_prepare_to_resume (win32_thread_info *th)
 }
 
 static void
-i386_thread_added (win32_thread_info *th)
+i386_thread_added (windows_thread_info *th)
 {
   th->debug_registers_changed = 1;
 }
 
 static void
-i386_single_step (win32_thread_info *th)
+i386_single_step (windows_thread_info *th)
 {
   th->context.EFlags |= FLAG_TRACE_BIT;
 }
@@ -398,7 +398,7 @@ static const int mappings[] =
 /* Fetch register from gdbserver regcache data.  */
 static void
 i386_fetch_inferior_register (struct regcache *regcache,
-			      win32_thread_info *th, int r)
+			      windows_thread_info *th, int r)
 {
   char *context_offset = (char *) &th->context + mappings[r];
 
@@ -420,7 +420,7 @@ i386_fetch_inferior_register (struct regcache *regcache,
 /* Store a new register value into the thread context of TH.  */
 static void
 i386_store_inferior_register (struct regcache *regcache,
-			      win32_thread_info *th, int r)
+			      windows_thread_info *th, int r)
 {
   char *context_offset = (char *) &th->context + mappings[r];
   collect_register (regcache, r, context_offset);
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 7088ba4dd17..7120b1d4dd1 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -126,7 +126,7 @@ debug_event_ptid (DEBUG_EVENT *event)
 /* Get the thread context of the thread associated with TH.  */
 
 static void
-win32_get_thread_context (win32_thread_info *th)
+win32_get_thread_context (windows_thread_info *th)
 {
   memset (&th->context, 0, sizeof (CONTEXT));
   (*the_low_target.get_thread_context) (th);
@@ -138,7 +138,7 @@ win32_get_thread_context (win32_thread_info *th)
 /* Set the thread context of the thread associated with TH.  */
 
 static void
-win32_set_thread_context (win32_thread_info *th)
+win32_set_thread_context (windows_thread_info *th)
 {
 #ifdef _WIN32_WCE
   /* Calling SuspendThread on a thread that is running kernel code
@@ -159,7 +159,7 @@ win32_set_thread_context (win32_thread_info *th)
 /* Set the thread context of the thread associated with TH.  */
 
 static void
-win32_prepare_to_resume (win32_thread_info *th)
+win32_prepare_to_resume (windows_thread_info *th)
 {
   if (the_low_target.prepare_to_resume != NULL)
     (*the_low_target.prepare_to_resume) (th);
@@ -168,7 +168,7 @@ win32_prepare_to_resume (win32_thread_info *th)
 /* See win32-low.h.  */
 
 void
-win32_require_context (win32_thread_info *th)
+win32_require_context (windows_thread_info *th)
 {
   if (th->context.ContextFlags == 0)
     {
@@ -190,30 +190,30 @@ win32_require_context (win32_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 win32_thread_info *
+static windows_thread_info *
 thread_rec (ptid_t ptid, int get_context)
 {
   thread_info *thread = find_thread_ptid (ptid);
   if (thread == NULL)
     return NULL;
 
-  win32_thread_info *th = (win32_thread_info *) thread_target_data (thread);
+  windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
   if (get_context)
     win32_require_context (th);
   return th;
 }
 
 /* Add a thread to the thread list.  */
-static win32_thread_info *
+static windows_thread_info *
 child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
 {
-  win32_thread_info *th;
+  windows_thread_info *th;
   ptid_t ptid = ptid_t (pid, tid, 0);
 
   if ((th = thread_rec (ptid, FALSE)))
     return th;
 
-  th = XCNEW (win32_thread_info);
+  th = XCNEW (windows_thread_info);
   th->tid = tid;
   th->h = h;
   th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
@@ -230,7 +230,7 @@ child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
 static void
 delete_thread_info (thread_info *thread)
 {
-  win32_thread_info *th = (win32_thread_info *) thread_target_data (thread);
+  windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
 
   remove_thread (thread);
   CloseHandle (th->h);
@@ -425,7 +425,7 @@ do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
 static void
 continue_one_thread (thread_info *thread, int thread_id)
 {
-  win32_thread_info *th = (win32_thread_info *) thread_target_data (thread);
+  windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
 
   if (thread_id == -1 || thread_id == th->tid)
     {
@@ -474,7 +474,7 @@ static void
 child_fetch_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
-  win32_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
+  windows_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
   if (r == -1 || r > NUM_REGS)
     child_fetch_inferior_registers (regcache, NUM_REGS);
   else
@@ -488,7 +488,7 @@ static void
 child_store_inferior_registers (struct regcache *regcache, int r)
 {
   int regno;
-  win32_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
+  windows_thread_info *th = thread_rec (current_thread_ptid (), TRUE);
   if (r == -1 || r == 0 || r > NUM_REGS)
     child_store_inferior_registers (regcache, NUM_REGS);
   else
@@ -900,7 +900,7 @@ win32_resume (struct thread_resume *resume_info, size_t n)
   DWORD tid;
   enum gdb_signal sig;
   int step;
-  win32_thread_info *th;
+  windows_thread_info *th;
   DWORD continue_status = DBG_CONTINUE;
   ptid_t ptid;
 
@@ -1335,7 +1335,7 @@ handle_exception (struct target_waitstatus *ourstatus)
 static void
 suspend_one_thread (thread_info *thread)
 {
-  win32_thread_info *th = (win32_thread_info *) thread_target_data (thread);
+  windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
 
   if (!th->suspended)
     {
@@ -1759,7 +1759,7 @@ wince_hostio_last_error (char *buf)
 static int
 win32_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
 {
-  win32_thread_info *th;
+  windows_thread_info *th;
   th = thread_rec (ptid, 0);
   if (th == NULL)
     return 0;
diff --git a/gdb/gdbserver/win32-low.h b/gdb/gdbserver/win32-low.h
index 815c1b04669..192e177aa71 100644
--- a/gdb/gdbserver/win32-low.h
+++ b/gdb/gdbserver/win32-low.h
@@ -29,7 +29,7 @@ extern const struct target_desc *win32_tdesc;
 
 /* Thread information structure used to track extra information about
    each thread.  */
-typedef struct win32_thread_info
+struct windows_thread_info
 {
   /* The Win32 thread identifier.  */
   DWORD tid;
@@ -54,7 +54,7 @@ typedef struct win32_thread_info
   /* Whether debug registers changed since we last set CONTEXT back to
      the thread.  */
   int debug_registers_changed;
-} win32_thread_info;
+};
 
 struct win32_target_ops
 {
@@ -68,23 +68,23 @@ struct win32_target_ops
   void (*initial_stuff) (void);
 
   /* Fetch the context from the inferior.  */
-  void (*get_thread_context) (win32_thread_info *th);
+  void (*get_thread_context) (windows_thread_info *th);
 
   /* Called just before resuming the thread.  */
-  void (*prepare_to_resume) (win32_thread_info *th);
+  void (*prepare_to_resume) (windows_thread_info *th);
 
   /* Called when a thread was added.  */
-  void (*thread_added) (win32_thread_info *th);
+  void (*thread_added) (windows_thread_info *th);
 
   /* Fetch register from gdbserver regcache data.  */
   void (*fetch_inferior_register) (struct regcache *regcache,
-				   win32_thread_info *th, int r);
+				   windows_thread_info *th, int r);
 
   /* Store a new register value into the thread context of TH.  */
   void (*store_inferior_register) (struct regcache *regcache,
-				   win32_thread_info *th, int r);
+				   windows_thread_info *th, int r);
 
-  void (*single_step) (win32_thread_info *th);
+  void (*single_step) (windows_thread_info *th);
 
   const unsigned char *breakpoint;
   int breakpoint_len;
@@ -102,7 +102,7 @@ struct win32_target_ops
 extern struct win32_target_ops the_low_target;
 
 /* Retrieve the context for this thread, if not already retrieved.  */
-extern void win32_require_context (win32_thread_info *th);
+extern void win32_require_context (windows_thread_info *th);
 
 /* Map the Windows error number in ERROR to a locale-dependent error
    message string and return a pointer to it.  Typically, the values
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 8/9] Share Windows thread-suspend and -resume code
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
  2019-10-14 16:15 ` [PATCH 7/9] Make windows_thread_info::name a unique_xmalloc_ptr Tom Tromey
  2019-10-14 16:16 ` [PATCH 1/9] Remove the "next" field from windows_thread_info Tom Tromey
@ 2019-10-14 16:16 ` Tom Tromey
  2019-10-14 16:16 ` [PATCH 2/9] Rename win32_thread_info to windows_thread_info Tom Tromey
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This adds "suspend" and "resume" methods to windows_thread_info, and
changes gdb and gdbserver to share this code.

2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (thread_rec): Use windows_thread_info::suspend.
	(windows_continue): Use windows_continue::resume.
	* nat/windows-nat.h (struct windows_thread_info) <suspend,
	resume>: Declare new methods.
	* nat/windows-nat.c: New file.
	* configure.nat (NATDEPFILES): Add nat/windows-nat.o when needed.

gdb/gdbserver/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* win32-low.c (win32_require_context, suspend_one_thread): Use
	windows_thread_info::suspend.
	(continue_one_thread): Use windows_thread_info::resume.
	* configure.srv (srv_tgtobj): Add windows-nat.o when needed.
---
 gdb/ChangeLog               |  9 ++++++
 gdb/configure.nat           |  4 +--
 gdb/gdbserver/ChangeLog     |  7 +++++
 gdb/gdbserver/configure.srv | 12 ++++----
 gdb/gdbserver/win32-low.c   | 33 ++------------------
 gdb/nat/windows-nat.c       | 60 +++++++++++++++++++++++++++++++++++++
 gdb/nat/windows-nat.h       |  9 ++++++
 gdb/windows-nat.c           | 26 ++--------------
 8 files changed, 98 insertions(+), 62 deletions(-)
 create mode 100644 gdb/nat/windows-nat.c

diff --git a/gdb/configure.nat b/gdb/configure.nat
index 77a2ee80839..15390e3309c 100644
--- a/gdb/configure.nat
+++ b/gdb/configure.nat
@@ -75,10 +75,10 @@ case ${gdb_host} in
 	NATDEPFILES='fork-child.o nat/fork-inferior.o inf-ptrace.o'
 	;;
     cygwin*)
-	NATDEPFILES='x86-nat.o nat/x86-dregs.o windows-nat.o'
+	NATDEPFILES='x86-nat.o nat/x86-dregs.o windows-nat.o nat/windows-nat.o'
 	;;
     mingw*)
-	NATDEPFILES='x86-nat.o nat/x86-dregs.o windows-nat.o'
+	NATDEPFILES='x86-nat.o nat/x86-dregs.o windows-nat.o nat/windows-nat.o'
 	;;
     aix)
 	NATDEPFILES='nat/fork-inferior.o fork-child.o inf-ptrace.o'
diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
index 1a4ab8e3361..404ed118061 100644
--- a/gdb/gdbserver/configure.srv
+++ b/gdb/gdbserver/configure.srv
@@ -63,7 +63,7 @@ case "${target}" in
 			srv_linux_thread_db=yes
 			;;
   arm*-*-mingw32ce*)	srv_regobj=reg-arm.o
-			srv_tgtobj="win32-low.o win32-arm-low.o"
+			srv_tgtobj="win32-low.o windows-nat.o win32-arm-low.o"
 			srv_tgtobj="${srv_tgtobj} wincecompat.o"
 			# hostio_last_error implementation is in win32-low.c
 			srv_hostio_err_objs=""
@@ -86,7 +86,7 @@ case "${target}" in
 			srv_linux_thread_db=yes
 			;;
   i[34567]86-*-cygwin*)	srv_regobj=""
-			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o win32-i386-low.o"
+			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o windows-nat.o win32-i386-low.o"
 			srv_tgtobj="${srv_tgtobj} arch/i386.o"
 			;;
   i[34567]86-*-linux*)	srv_tgtobj="${srv_tgtobj} arch/i386.o"
@@ -108,7 +108,7 @@ case "${target}" in
 			;;
   i[34567]86-*-mingw32ce*)
 			srv_regobj=""
-			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o win32-i386-low.o"
+			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o windows-nat.o win32-i386-low.o"
 			srv_tgtobj="${srv_tgtobj} arch/i386.o"
 			srv_tgtobj="${srv_tgtobj} wincecompat.o"
 			# hostio_last_error implementation is in win32-low.c
@@ -117,7 +117,7 @@ case "${target}" in
 			srv_mingwce=yes
 			;;
   i[34567]86-*-mingw*)	srv_regobj=""
-			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o win32-i386-low.o"
+			srv_tgtobj="x86-low.o x86-dregs.o win32-low.o windows-nat.o win32-i386-low.o"
 			srv_tgtobj="${srv_tgtobj} arch/i386.o"
 			srv_mingw=yes
 			;;
@@ -364,12 +364,12 @@ case "${target}" in
 			ipa_obj="${ipa_obj} arch/amd64-ipa.o"
 			;;
   x86_64-*-mingw*)	srv_regobj=""
-			srv_tgtobj="x86-low.o x86-dregs.o i387-fp.o win32-low.o win32-i386-low.o"
+			srv_tgtobj="x86-low.o x86-dregs.o i387-fp.o win32-low.o windows-nat.o win32-i386-low.o"
 			srv_tgtobj="${srv_tgtobj} arch/amd64.o"
 			srv_mingw=yes
 			;;
   x86_64-*-cygwin*)	srv_regobj=""
-			srv_tgtobj="x86-low.o x86-dregs.o i387-fp.o win32-low.o win32-i386-low.o"
+			srv_tgtobj="x86-low.o x86-dregs.o i387-fp.o win32-low.o windows-nat.o win32-i386-low.o"
 			srv_tgtobj="${srv_tgtobj} arch/amd64.o"
 			;;
 
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index b478d970b5f..7ccf7c18ffc 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -172,18 +172,7 @@ win32_require_context (windows_thread_info *th)
 {
   if (th->context.ContextFlags == 0)
     {
-      if (!th->suspended)
-	{
-	  if (SuspendThread (th->h) == (DWORD) -1)
-	    {
-	      DWORD err = GetLastError ();
-	      OUTMSG (("warning: SuspendThread failed in thread_rec, "
-		       "(error %d): %s\n", (int) err, strwinerror (err)));
-	    }
-	  else
-	    th->suspended = 1;
-	}
-
+      th->suspend ();
       win32_get_thread_context (th);
     }
 }
@@ -436,13 +425,7 @@ continue_one_thread (thread_info *thread, int thread_id)
 	      th->context.ContextFlags = 0;
 	    }
 
-	  if (ResumeThread (th->h) == (DWORD) -1)
-	    {
-	      DWORD err = GetLastError ();
-	      OUTMSG (("warning: ResumeThread failed in continue_one_thread, "
-		       "(error %d): %s\n", (int) err, strwinerror (err)));
-	    }
-	  th->suspended = 0;
+	  th->resume ();
 	}
     }
 }
@@ -1334,17 +1317,7 @@ suspend_one_thread (thread_info *thread)
 {
   windows_thread_info *th = (windows_thread_info *) thread_target_data (thread);
 
-  if (!th->suspended)
-    {
-      if (SuspendThread (th->h) == (DWORD) -1)
-	{
-	  DWORD err = GetLastError ();
-	  OUTMSG (("warning: SuspendThread failed in suspend_one_thread, "
-		   "(error %d): %s\n", (int) err, strwinerror (err)));
-	}
-      else
-	th->suspended = 1;
-    }
+  th->suspend ();
 }
 
 static void
diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
new file mode 100644
index 00000000000..a98ff421e6f
--- /dev/null
+++ b/gdb/nat/windows-nat.c
@@ -0,0 +1,60 @@
+/* Internal interfaces for the Windows code
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "gdbsupport/common-defs.h"
+#include "nat/windows-nat.h"
+
+void
+windows_thread_info::suspend ()
+{
+  if (suspended != 0)
+    return;
+
+  if (SuspendThread (h) == (DWORD) -1)
+    {
+      DWORD err = GetLastError ();
+
+      /* We get Access Denied (5) when trying to suspend
+	 threads that Windows started on behalf of the
+	 debuggee, usually when those threads are just
+	 about to exit.
+	 We can get Invalid Handle (6) if the main thread
+	 has exited.  */
+      if (err != ERROR_INVALID_HANDLE && err != ERROR_ACCESS_DENIED)
+	warning (_("SuspendThread (tid=0x%x) failed. (winerr %u)"),
+		 (unsigned) tid, (unsigned) err);
+      suspended = -1;
+    }
+  else
+    suspended = 1;
+}
+
+void
+windows_thread_info::resume ()
+{
+  if (suspended > 0)
+    {
+      if (ResumeThread (h) == (DWORD) -1)
+	{
+	  DWORD err = GetLastError ();
+	  warning (_("warning: ResumeThread (tid=0x%x) failed. (winerr %u)"),
+		   (unsigned) tid, (unsigned) err);
+	}
+    }
+  suspended = 0;
+}
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 06c6486c0c0..b51279b65c9 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -19,6 +19,8 @@
 #ifndef NAT_WINDOWS_NAT_H
 #define NAT_WINDOWS_NAT_H
 
+#include <windows.h>
+
 /* Thread information structure used to track extra information about
    each thread.  */
 struct windows_thread_info
@@ -32,6 +34,13 @@ struct windows_thread_info
 
   DISABLE_COPY_AND_ASSIGN (windows_thread_info);
 
+  /* Ensure that this thread has been suspended.  */
+  void suspend ();
+
+  /* Resume the thread if it has been suspended.  */
+  void resume ();
+
+
   /* The Win32 thread identifier.  */
   DWORD tid;
 
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 3a526e504e4..e6a97f8b8c3 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -377,27 +377,7 @@ thread_rec (DWORD id, int get_context)
 	if (!th->suspended && get_context)
 	  {
 	    if (get_context > 0 && id != current_event.dwThreadId)
-	      {
-		if (SuspendThread (th->h) == (DWORD) -1)
-		  {
-		    DWORD err = GetLastError ();
-
-		    /* We get Access Denied (5) when trying to suspend
-		       threads that Windows started on behalf of the
-		       debuggee, usually when those threads are just
-		       about to exit.
-		       We can get Invalid Handle (6) if the main thread
-		       has exited.  */
-		    if (err != ERROR_INVALID_HANDLE
-			&& err != ERROR_ACCESS_DENIED)
-		      warning (_("SuspendThread (tid=0x%x) failed."
-				 " (winerr %u)"),
-			       (unsigned) id, (unsigned) err);
-		    th->suspended = -1;
-		  }
-		else
-		  th->suspended = 1;
-	      }
+	      th->suspend ();
 	    else if (get_context < 0)
 	      th->suspended = -1;
 	    th->reload_context = true;
@@ -1329,9 +1309,7 @@ windows_continue (DWORD continue_status, int id, int killed)
 	      }
 	    th->context.ContextFlags = 0;
 	  }
-	if (th->suspended > 0)
-	  (void) ResumeThread (th->h);
-	th->suspended = 0;
+	th->resume ();
       }
 
   res = ContinueDebugEvent (current_event.dwProcessId,
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 6/9] Change two windows_thread_info members to "bool"
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (3 preceding siblings ...)
  2019-10-14 16:16 ` [PATCH 2/9] Rename win32_thread_info to windows_thread_info Tom Tromey
@ 2019-10-14 16:16 ` Tom Tromey
  2019-10-14 16:16 ` [PATCH 3/9] Rename windows_thread_info::id to "tid" Tom Tromey
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes a couple of fields of windows_thread_info to have type
"bool".  It also updates the comment of another field, to clarify the
possible values it can hold.

2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (thread_rec)
	(windows_nat_target::fetch_registers): Update.
	* nat/windows-nat.h (struct windows_thread_info) <suspended>:
	Update comment.
	<debug_registers_changed, reload_context>: Now bool.

gdb/gdbserver/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* win32-i386-low.c (update_debug_registers)
	(i386_prepare_to_resume, i386_thread_added): Update.
---
 gdb/ChangeLog                  | 8 ++++++++
 gdb/gdbserver/ChangeLog        | 5 +++++
 gdb/gdbserver/win32-i386-low.c | 6 +++---
 gdb/nat/windows-nat.h          | 8 +++++---
 gdb/windows-nat.c              | 4 ++--
 5 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/gdb/gdbserver/win32-i386-low.c b/gdb/gdbserver/win32-i386-low.c
index 3fc0cf1cd03..b834b160d9a 100644
--- a/gdb/gdbserver/win32-i386-low.c
+++ b/gdb/gdbserver/win32-i386-low.c
@@ -44,7 +44,7 @@ update_debug_registers (thread_info *thread)
 
   /* The actual update is done later just before resuming the lwp,
      we just mark that the registers need updating.  */
-  th->debug_registers_changed = 1;
+  th->debug_registers_changed = true;
 }
 
 /* Update the inferior's debug register REGNUM from STATE.  */
@@ -253,14 +253,14 @@ i386_prepare_to_resume (windows_thread_info *th)
 	 FIXME: should we set dr6 also ?? */
       th->context.Dr7 = dr->dr_control_mirror;
 
-      th->debug_registers_changed = 0;
+      th->debug_registers_changed = false;
     }
 }
 
 static void
 i386_thread_added (windows_thread_info *th)
 {
-  th->debug_registers_changed = 1;
+  th->debug_registers_changed = true;
 }
 
 static void
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 2fe2a2f0e88..0cfc0716f26 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -46,7 +46,9 @@ struct windows_thread_info
   /* Thread Information Block address.  */
   CORE_ADDR thread_local_base;
 
-  /* Non zero if SuspendThread was called on this thread.  */
+  /* This keeps track of whether SuspendThread was called on this
+     thread.  -1 means there was a failure, 1 means it was called, and
+     0 means it was not.  */
   int suspended = 0;
 
 #ifdef _WIN32_WCE
@@ -59,11 +61,11 @@ struct windows_thread_info
 
   /* Whether debug registers changed since we last set CONTEXT back to
      the thread.  */
-  int debug_registers_changed = 0;
+  bool debug_registers_changed = false;
 
   /* Nonzero if CONTEXT is invalidated and must be re-read from the
      inferior thread.  */
-  int reload_context = 0;
+  bool reload_context = false;
 
   /* The name of the thread, allocated by xmalloc.  */
   char *name = nullptr;
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 210bcfb6f7f..b38ff402cb5 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -400,7 +400,7 @@ thread_rec (DWORD id, int get_context)
 	      }
 	    else if (get_context < 0)
 	      th->suspended = -1;
-	    th->reload_context = 1;
+	    th->reload_context = true;
 	  }
 	return th;
       }
@@ -604,7 +604,7 @@ windows_nat_target::fetch_registers (struct regcache *regcache, int r)
 	      dr[7] = th->context.Dr7;
 	    }
 	}
-      th->reload_context = 0;
+      th->reload_context = false;
     }
 
   if (r < 0)
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/9] Remove the "next" field from windows_thread_info
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
  2019-10-14 16:15 ` [PATCH 7/9] Make windows_thread_info::name a unique_xmalloc_ptr Tom Tromey
@ 2019-10-14 16:16 ` Tom Tromey
  2019-10-14 16:16 ` [PATCH 8/9] Share Windows thread-suspend and -resume code Tom Tromey
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes windows_thread_info to remove the "next" field, replacing
the linked list of threads with a vector.  This is a prerequisite to
sharing is structure with gdbserver, which manages threads
differently.

gdb/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (struct windows_thread_info): Remove typedef.
	(thread_head): Remove.
	(thread_list): New global.
	(thread_rec, windows_add_thread, windows_init_thread_list)
	(windows_delete_thread, windows_continue): Update.
---
 gdb/ChangeLog     |  8 ++++++++
 gdb/windows-nat.c | 52 +++++++++++++++++++----------------------------
 2 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 607b2e8cb97..d3413a3e319 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -43,6 +43,7 @@
 #include <cygwin/version.h>
 #endif
 #include <algorithm>
+#include <vector>
 
 #include "filenames.h"
 #include "symfile.h"
@@ -214,9 +215,8 @@ static enum gdb_signal last_sig = GDB_SIGNAL_0;
 
 /* Thread information structure used to track information that is
    not available in gdb's thread structure.  */
-typedef struct windows_thread_info_struct
+struct windows_thread_info
   {
-    struct windows_thread_info_struct *next;
     DWORD id;
     HANDLE h;
     CORE_ADDR thread_local_base;
@@ -224,10 +224,9 @@ typedef struct windows_thread_info_struct
     int suspended;
     int reload_context;
     CONTEXT context;
-  }
-windows_thread_info;
+  };
 
-static windows_thread_info thread_head;
+static std::vector<windows_thread_info *> thread_list;
 
 /* The process and thread handles for the above context.  */
 
@@ -384,9 +383,7 @@ check (BOOL ok, const char *file, int line)
 static windows_thread_info *
 thread_rec (DWORD id, int get_context)
 {
-  windows_thread_info *th;
-
-  for (th = &thread_head; (th = th->next) != NULL;)
+  for (windows_thread_info *th : thread_list)
     if (th->id == id)
       {
 	if (!th->suspended && get_context)
@@ -448,8 +445,7 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
   th->id = id;
   th->h = h;
   th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
-  th->next = thread_head.next;
-  thread_head.next = th;
+  thread_list.push_back (th);
 
   /* Add this new thread to the list of threads.
 
@@ -484,17 +480,13 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
 static void
 windows_init_thread_list (void)
 {
-  windows_thread_info *th = &thread_head;
-
   DEBUG_EVENTS (("gdb: windows_init_thread_list\n"));
   init_thread_list ();
-  while (th->next != NULL)
-    {
-      windows_thread_info *here = th->next;
-      th->next = here->next;
-      xfree (here);
-    }
-  thread_head.next = NULL;
+
+  for (windows_thread_info *here : thread_list)
+    xfree (here);
+
+  thread_list.clear ();
 }
 
 /* Delete a thread from the list of threads.
@@ -507,7 +499,6 @@ windows_init_thread_list (void)
 static void
 windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p)
 {
-  windows_thread_info *th;
   DWORD id;
 
   gdb_assert (ptid.tid () != 0);
@@ -530,17 +521,17 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p)
 
   delete_thread (find_thread_ptid (ptid));
 
-  for (th = &thread_head;
-       th->next != NULL && th->next->id != id;
-       th = th->next)
-    continue;
+  auto iter = std::find_if (thread_list.begin (), thread_list.end (),
+			    [=] (windows_thread_info *th)
+			    {
+			      return th->id == id;
+			    });
 
-  if (th->next != NULL)
+  if (iter != thread_list.end ())
     {
-      windows_thread_info *here = th->next;
-      th->next = here->next;
-      xfree (here->name);
-      xfree (here);
+      xfree ((*iter)->name);
+      xfree (*iter);
+      thread_list.erase (iter);
     }
 }
 
@@ -1319,7 +1310,6 @@ handle_exception (struct target_waitstatus *ourstatus)
 static BOOL
 windows_continue (DWORD continue_status, int id, int killed)
 {
-  windows_thread_info *th;
   BOOL res;
 
   DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=0x%x, %s);\n",
@@ -1328,7 +1318,7 @@ windows_continue (DWORD continue_status, int id, int killed)
 		  continue_status == DBG_CONTINUE ?
 		  "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED"));
 
-  for (th = &thread_head; (th = th->next) != NULL;)
+  for (windows_thread_info *th : thread_list)
     if ((id == -1 || id == (int) th->id)
 	&& th->suspended)
       {
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 3/9] Rename windows_thread_info::id to "tid"
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (4 preceding siblings ...)
  2019-10-14 16:16 ` [PATCH 6/9] Change two windows_thread_info members to "bool" Tom Tromey
@ 2019-10-14 16:16 ` Tom Tromey
  2019-10-14 16:22 ` [PATCH 5/9] Use new and delete for windows_thread_info Tom Tromey
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes the name of a field in windows_thread_info, bringing gdb
and gdbserver closer into sync.

gdb/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (struct windows_thread_info) <tid>: Rename from "id".
	(thread_rec, windows_add_thread, windows_delete_thread)
	(windows_continue): Update.
---
 gdb/ChangeLog     |  6 ++++++
 gdb/windows-nat.c | 10 +++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index d3413a3e319..7911afe80a2 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -217,7 +217,7 @@ static enum gdb_signal last_sig = GDB_SIGNAL_0;
    not available in gdb's thread structure.  */
 struct windows_thread_info
   {
-    DWORD id;
+    DWORD tid;
     HANDLE h;
     CORE_ADDR thread_local_base;
     char *name;
@@ -384,7 +384,7 @@ static windows_thread_info *
 thread_rec (DWORD id, int get_context)
 {
   for (windows_thread_info *th : thread_list)
-    if (th->id == id)
+    if (th->tid == id)
       {
 	if (!th->suspended && get_context)
 	  {
@@ -442,7 +442,7 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
     return th;
 
   th = XCNEW (windows_thread_info);
-  th->id = id;
+  th->tid = id;
   th->h = h;
   th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
   thread_list.push_back (th);
@@ -524,7 +524,7 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p)
   auto iter = std::find_if (thread_list.begin (), thread_list.end (),
 			    [=] (windows_thread_info *th)
 			    {
-			      return th->id == id;
+			      return th->tid == id;
 			    });
 
   if (iter != thread_list.end ())
@@ -1319,7 +1319,7 @@ windows_continue (DWORD continue_status, int id, int killed)
 		  "DBG_CONTINUE" : "DBG_EXCEPTION_NOT_HANDLED"));
 
   for (windows_thread_info *th : thread_list)
-    if ((id == -1 || id == (int) th->id)
+    if ((id == -1 || id == (int) th->tid)
 	&& th->suspended)
       {
 	if (debug_registers_changed)
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 4/9] Share windows_thread_info between gdb and gdbserver
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (7 preceding siblings ...)
  2019-10-14 16:22 ` [PATCH 9/9] Change type of argument to windows-nat.c:thread_rec Tom Tromey
@ 2019-10-14 16:22 ` Tom Tromey
  2019-10-29 16:51 ` [PATCH 0/9] share some Windows code " Tom Tromey
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This introduces a new file, nat/windows-nat.h, which holds the
definition of windows_thread_info.  This is now shared between gdb and
gdbserver.

Note that the two implementations different slightly.  gdb had a
couple of fields ("name" and "reload_context") that gdbserver did not;
while gdbserver had one field ("base_context") that gdb did not, plus
better comments.  The new file preserves all the fields, and the
comments.

gdb/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (struct windows_thread_info): Remove.
	* nat/windows-nat.h: New file.

gdb/gdbserver/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* win32-low.h (struct windows_thread_info): Remove.
---
 gdb/ChangeLog             |  5 ++++
 gdb/gdbserver/ChangeLog   |  4 +++
 gdb/gdbserver/win32-low.h | 30 +-------------------
 gdb/nat/windows-nat.h     | 58 +++++++++++++++++++++++++++++++++++++++
 gdb/windows-nat.c         | 14 +---------
 5 files changed, 69 insertions(+), 42 deletions(-)
 create mode 100644 gdb/nat/windows-nat.h

diff --git a/gdb/gdbserver/win32-low.h b/gdb/gdbserver/win32-low.h
index 192e177aa71..342411d3956 100644
--- a/gdb/gdbserver/win32-low.h
+++ b/gdb/gdbserver/win32-low.h
@@ -20,6 +20,7 @@
 #define GDBSERVER_WIN32_LOW_H
 
 #include <windows.h>
+#include "nat/windows-nat.h"
 
 struct target_desc;
 
@@ -27,35 +28,6 @@ struct target_desc;
    Windows ports support neither bi-arch nor multi-process.  */
 extern const struct target_desc *win32_tdesc;
 
-/* Thread information structure used to track extra information about
-   each thread.  */
-struct windows_thread_info
-{
-  /* The Win32 thread identifier.  */
-  DWORD tid;
-
-  /* The handle to the thread.  */
-  HANDLE h;
-
-  /* Thread Information Block address.  */
-  CORE_ADDR thread_local_base;
-
-  /* Non zero if SuspendThread was called on this thread.  */
-  int suspended;
-
-#ifdef _WIN32_WCE
-  /* The context as retrieved right after suspending the thread. */
-  CONTEXT base_context;
-#endif
-
-  /* The context of the thread, including any manipulations.  */
-  CONTEXT context;
-
-  /* Whether debug registers changed since we last set CONTEXT back to
-     the thread.  */
-  int debug_registers_changed;
-};
-
 struct win32_target_ops
 {
   /* Architecture-specific setup.  */
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
new file mode 100644
index 00000000000..30fce27de2f
--- /dev/null
+++ b/gdb/nat/windows-nat.h
@@ -0,0 +1,58 @@
+/* Internal interfaces for the Windows code
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef NAT_WINDOWS_NAT_H
+#define NAT_WINDOWS_NAT_H
+
+/* Thread information structure used to track extra information about
+   each thread.  */
+struct windows_thread_info
+{
+  /* The Win32 thread identifier.  */
+  DWORD tid;
+
+  /* The handle to the thread.  */
+  HANDLE h;
+
+  /* Thread Information Block address.  */
+  CORE_ADDR thread_local_base;
+
+  /* Non zero if SuspendThread was called on this thread.  */
+  int suspended;
+
+#ifdef _WIN32_WCE
+  /* The context as retrieved right after suspending the thread. */
+  CONTEXT base_context;
+#endif
+
+  /* The context of the thread, including any manipulations.  */
+  CONTEXT context;
+
+  /* Whether debug registers changed since we last set CONTEXT back to
+     the thread.  */
+  int debug_registers_changed;
+
+  /* Nonzero if CONTEXT is invalidated and must be re-read from the
+     inferior thread.  */
+  int reload_context;
+
+  /* The name of the thread, allocated by xmalloc.  */
+  char *name;
+};
+
+#endif
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 7911afe80a2..010d6565330 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -69,6 +69,7 @@
 #include "inf-child.h"
 #include "gdbsupport/gdb_tilde_expand.h"
 #include "gdbsupport/pathstuff.h"
+#include "nat/windows-nat.h"
 
 #define AdjustTokenPrivileges		dyn_AdjustTokenPrivileges
 #define DebugActiveProcessStop		dyn_DebugActiveProcessStop
@@ -213,19 +214,6 @@ static unsigned long cygwin_get_dr7 (void);
 static enum gdb_signal last_sig = GDB_SIGNAL_0;
 /* Set if a signal was received from the debugged process.  */
 
-/* Thread information structure used to track information that is
-   not available in gdb's thread structure.  */
-struct windows_thread_info
-  {
-    DWORD tid;
-    HANDLE h;
-    CORE_ADDR thread_local_base;
-    char *name;
-    int suspended;
-    int reload_context;
-    CONTEXT context;
-  };
-
 static std::vector<windows_thread_info *> thread_list;
 
 /* The process and thread handles for the above context.  */
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 9/9] Change type of argument to windows-nat.c:thread_rec
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (6 preceding siblings ...)
  2019-10-14 16:22 ` [PATCH 5/9] Use new and delete for windows_thread_info Tom Tromey
@ 2019-10-14 16:22 ` Tom Tromey
  2019-10-14 16:22 ` [PATCH 4/9] Share windows_thread_info between gdb and gdbserver Tom Tromey
  2019-10-29 16:51 ` [PATCH 0/9] share some Windows code " Tom Tromey
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

windows-nat.c:thread_rec accepts an integer parameter whose
interpretation depends on whether it is less than, equal to, or
greater than zero.  I found this confusing at times, so this patch
replaces it with an enum instead.

2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (enum thread_disposition_type): New.
	(thread_rec): Replace "get_context" parameter with "disposition";
	change type.
	(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.
---
 gdb/ChangeLog     | 12 +++++++++
 gdb/windows-nat.c | 63 ++++++++++++++++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index e6a97f8b8c3..f038772d0e9 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -365,22 +365,44 @@ check (BOOL ok, const char *file, int line)
 		     (unsigned) GetLastError ());
 }
 
-/* Find a thread record given a thread id.  If GET_CONTEXT is not 0,
-   then also retrieve the context for this thread.  If GET_CONTEXT is
-   negative, then don't suspend the thread.  */
+/* 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.  */
 static windows_thread_info *
-thread_rec (DWORD id, int get_context)
+thread_rec (DWORD id, enum thread_disposition_type disposition)
 {
   for (windows_thread_info *th : thread_list)
     if (th->tid == id)
       {
-	if (!th->suspended && get_context)
+	if (!th->suspended)
 	  {
-	    if (get_context > 0 && id != current_event.dwThreadId)
-	      th->suspend ();
-	    else if (get_context < 0)
-	      th->suspended = -1;
-	    th->reload_context = true;
+	    switch (disposition)
+	      {
+	      case DONT_INVALIDATE_CONTEXT:
+		/* Nothing.  */
+		break;
+	      case INVALIDATE_CONTEXT:
+		if (id != current_event.dwThreadId)
+		  th->suspend ();
+		th->reload_context = true;
+		break;
+	      case DONT_SUSPEND:
+		th->reload_context = true;
+		th->suspended = -1;
+		break;
+	      }
 	  }
 	return th;
       }
@@ -406,7 +428,7 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
 
   id = ptid.tid ();
 
-  if ((th = thread_rec (id, FALSE)))
+  if ((th = thread_rec (id, DONT_INVALIDATE_CONTEXT)))
     return th;
 
   th = new windows_thread_info (id, h, (CORE_ADDR) (uintptr_t) tlb);
@@ -546,7 +568,7 @@ void
 windows_nat_target::fetch_registers (struct regcache *regcache, int r)
 {
   DWORD pid = regcache->ptid ().tid ();
-  windows_thread_info *th = thread_rec (pid, TRUE);
+  windows_thread_info *th = thread_rec (pid, INVALIDATE_CONTEXT);
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -616,7 +638,7 @@ void
 windows_nat_target::store_registers (struct regcache *regcache, int r)
 {
   DWORD pid = regcache->ptid ().tid ();
-  windows_thread_info *th = thread_rec (pid, TRUE);
+  windows_thread_info *th = thread_rec (pid, INVALIDATE_CONTEXT);
 
   /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
@@ -1114,7 +1136,7 @@ handle_exception (struct target_waitstatus *ourstatus)
   ourstatus->kind = TARGET_WAITKIND_STOPPED;
 
   /* Record the context of the current thread.  */
-  thread_rec (current_event.dwThreadId, -1);
+  thread_rec (current_event.dwThreadId, DONT_SUSPEND);
 
   switch (code)
     {
@@ -1230,7 +1252,7 @@ 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, 0);
+	  named_thread = thread_rec (named_thread_id, DONT_INVALIDATE_CONTEXT);
 	  if (named_thread != NULL)
 	    {
 	      int thread_name_len;
@@ -1402,7 +1424,7 @@ windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
 	       ptid.pid (), ptid.tid (), step, sig));
 
   /* Get context for currently selected thread.  */
-  th = thread_rec (inferior_ptid.tid (), FALSE);
+  th = thread_rec (inferior_ptid.tid (), DONT_INVALIDATE_CONTEXT);
   if (th)
     {
       if (step)
@@ -1656,7 +1678,7 @@ get_windows_debug_event (struct target_ops *ops,
       inferior_ptid = ptid_t (current_event.dwProcessId, 0, thread_id);
       current_thread = th;
       if (!current_thread)
-	current_thread = thread_rec (thread_id, TRUE);
+	current_thread = thread_rec (thread_id, INVALIDATE_CONTEXT);
     }
 
 out:
@@ -2956,7 +2978,7 @@ windows_nat_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
 {
   windows_thread_info *th;
 
-  th = thread_rec (ptid.tid (), 0);
+  th = thread_rec (ptid.tid (), DONT_INVALIDATE_CONTEXT);
   if (th == NULL)
     return false;
 
@@ -2977,7 +2999,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.tid (), 0)->name.get ();
+  return thread_rec (thr->ptid.tid (), DONT_INVALIDATE_CONTEXT)->name.get ();
 }
 
 
@@ -3140,7 +3162,8 @@ windows_nat_target::thread_alive (ptid_t ptid)
   gdb_assert (ptid.tid () != 0);
   tid = ptid.tid ();
 
-  return WaitForSingleObject (thread_rec (tid, FALSE)->h, 0) != WAIT_OBJECT_0;
+  return (WaitForSingleObject (thread_rec (tid, DONT_INVALIDATE_CONTEXT)->h, 0)
+	  != WAIT_OBJECT_0);
 }
 
 void
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 5/9] Use new and delete for windows_thread_info
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (5 preceding siblings ...)
  2019-10-14 16:16 ` [PATCH 3/9] Rename windows_thread_info::id to "tid" Tom Tromey
@ 2019-10-14 16:22 ` Tom Tromey
  2019-10-14 16:22 ` [PATCH 9/9] Change type of argument to windows-nat.c:thread_rec Tom Tromey
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-14 16:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This adds a constructor, destructor, and member initializers to
windows_thread_info, and changes gdb and gdbserver to use new and
delete.

2019-10-14  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (windows_add_thread): Use new.
	(windows_init_thread_list, windows_delete_thread): Use delete.
	(get_windows_debug_event): Update.
	* nat/windows-nat.h (struct windows_thread_info): Add constructor,
	destructor, and initializers.

gdb/gdbserver/ChangeLog
2019-10-14  Tom Tromey  <tromey@adacore.com>

	* win32-low.c (child_add_thread): Use new.
	(delete_thread_info): Use delete.
---
 gdb/ChangeLog             |  8 ++++++++
 gdb/gdbserver/ChangeLog   |  5 +++++
 gdb/gdbserver/win32-low.c |  7 ++-----
 gdb/nat/windows-nat.h     | 26 ++++++++++++++++++++------
 gdb/windows-nat.c         | 12 ++++--------
 5 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 7120b1d4dd1..b478d970b5f 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -213,10 +213,7 @@ child_add_thread (DWORD pid, DWORD tid, HANDLE h, void *tlb)
   if ((th = thread_rec (ptid, FALSE)))
     return th;
 
-  th = XCNEW (windows_thread_info);
-  th->tid = tid;
-  th->h = h;
-  th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
+  th = new windows_thread_info (tid, h, (CORE_ADDR) (uintptr_t) tlb);
 
   add_thread (ptid, th);
 
@@ -234,7 +231,7 @@ delete_thread_info (thread_info *thread)
 
   remove_thread (thread);
   CloseHandle (th->h);
-  free (th);
+  delete th;
 }
 
 /* Delete a thread from the list of threads.  */
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 30fce27de2f..2fe2a2f0e88 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -23,6 +23,20 @@
    each thread.  */
 struct windows_thread_info
 {
+  windows_thread_info (DWORD tid_, HANDLE h_, CORE_ADDR tlb)
+    : tid (tid_),
+      h (h_),
+      thread_local_base (tlb)
+  {
+  }
+
+  ~windows_thread_info ()
+  {
+    xfree (name);
+  }
+
+  DISABLE_COPY_AND_ASSIGN (windows_thread_info);
+
   /* The Win32 thread identifier.  */
   DWORD tid;
 
@@ -33,26 +47,26 @@ struct windows_thread_info
   CORE_ADDR thread_local_base;
 
   /* Non zero if SuspendThread was called on this thread.  */
-  int suspended;
+  int suspended = 0;
 
 #ifdef _WIN32_WCE
   /* The context as retrieved right after suspending the thread. */
-  CONTEXT base_context;
+  CONTEXT base_context {};
 #endif
 
   /* The context of the thread, including any manipulations.  */
-  CONTEXT context;
+  CONTEXT context {};
 
   /* Whether debug registers changed since we last set CONTEXT back to
      the thread.  */
-  int debug_registers_changed;
+  int debug_registers_changed = 0;
 
   /* Nonzero if CONTEXT is invalidated and must be re-read from the
      inferior thread.  */
-  int reload_context;
+  int reload_context = 0;
 
   /* The name of the thread, allocated by xmalloc.  */
-  char *name;
+  char *name = nullptr;
 };
 
 #endif
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 010d6565330..210bcfb6f7f 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -429,10 +429,7 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p)
   if ((th = thread_rec (id, FALSE)))
     return th;
 
-  th = XCNEW (windows_thread_info);
-  th->tid = id;
-  th->h = h;
-  th->thread_local_base = (CORE_ADDR) (uintptr_t) tlb;
+  th = new windows_thread_info (id, h, (CORE_ADDR) (uintptr_t) tlb);
   thread_list.push_back (th);
 
   /* Add this new thread to the list of threads.
@@ -472,7 +469,7 @@ windows_init_thread_list (void)
   init_thread_list ();
 
   for (windows_thread_info *here : thread_list)
-    xfree (here);
+    delete here;
 
   thread_list.clear ();
 }
@@ -517,8 +514,7 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p)
 
   if (iter != thread_list.end ())
     {
-      xfree ((*iter)->name);
-      xfree (*iter);
+      delete *iter;
       thread_list.erase (iter);
     }
 }
@@ -1501,7 +1497,7 @@ get_windows_debug_event (struct target_ops *ops,
   BOOL debug_event;
   DWORD continue_status, event_code;
   windows_thread_info *th;
-  static windows_thread_info dummy_thread_info;
+  static windows_thread_info dummy_thread_info (0, 0, 0);
   DWORD thread_id = 0;
 
   last_sig = GDB_SIGNAL_0;
-- 
2.20.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/9] share some Windows code between gdb and gdbserver
  2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
                   ` (8 preceding siblings ...)
  2019-10-14 16:22 ` [PATCH 4/9] Share windows_thread_info between gdb and gdbserver Tom Tromey
@ 2019-10-29 16:51 ` Tom Tromey
  9 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2019-10-29 16:51 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

Tom> While working on PR gdb/22992, I found that there is a lot of very
Tom> similar code in the Windows native code in gdb and gdbserver.  (Pedro
Tom> said that the gdbserver port was essentially copied from
Tom> windows-nat.c.)

Tom> This series starts sharing a bit of code between the two.  I tested
Tom> each patch using the AdaCore internal test suite.

Tom> There is still a lot more code that could be shared.  Some aspects of
Tom> that look a bit tricky, but meanwhile I thought this was a reasonable
Tom> place to start.

I based my fesfix for PR gdb/22992 on top of this series, and in the
process some changes were needed -- so this is obsolete now.  I'll send
the new series through gerrit once I'm done testing it.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-10-29 16:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-14 16:15 [PATCH 0/9] share some Windows code between gdb and gdbserver Tom Tromey
2019-10-14 16:15 ` [PATCH 7/9] Make windows_thread_info::name a unique_xmalloc_ptr Tom Tromey
2019-10-14 16:16 ` [PATCH 1/9] Remove the "next" field from windows_thread_info Tom Tromey
2019-10-14 16:16 ` [PATCH 8/9] Share Windows thread-suspend and -resume code Tom Tromey
2019-10-14 16:16 ` [PATCH 2/9] Rename win32_thread_info to windows_thread_info Tom Tromey
2019-10-14 16:16 ` [PATCH 6/9] Change two windows_thread_info members to "bool" Tom Tromey
2019-10-14 16:16 ` [PATCH 3/9] Rename windows_thread_info::id to "tid" Tom Tromey
2019-10-14 16:22 ` [PATCH 5/9] Use new and delete for windows_thread_info Tom Tromey
2019-10-14 16:22 ` [PATCH 9/9] Change type of argument to windows-nat.c:thread_rec Tom Tromey
2019-10-14 16:22 ` [PATCH 4/9] Share windows_thread_info between gdb and gdbserver Tom Tromey
2019-10-29 16:51 ` [PATCH 0/9] share some Windows code " Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).