Share Windows thread-suspend and -resume code
authorTom Tromey <tromey@adacore.com>
Wed, 8 Apr 2020 20:33:35 +0000 (14:33 -0600)
committerTom Tromey <tromey@adacore.com>
Wed, 8 Apr 2020 20:47:58 +0000 (14:47 -0600)
This adds "suspend" and "resume" methods to windows_thread_info, and
changes gdb and gdbserver to share this code.

gdb/ChangeLog
2020-04-08  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.

gdbserver/ChangeLog
2020-04-08  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
gdb/configure.nat
gdb/nat/windows-nat.c [new file with mode: 0644]
gdb/nat/windows-nat.h
gdb/windows-nat.c
gdbserver/ChangeLog
gdbserver/configure.srv
gdbserver/win32-low.cc

index c9885942a719e31c2e814d11fde3c6ad2f341188..bf1ef5b9d8c940149e9c14c45188c4fe5f1a5408 100644 (file)
@@ -1,3 +1,12 @@
+2020-04-08  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.
+
 2020-04-08  Tom Tromey  <tromey@adacore.com>
 
        * windows-nat.c (windows_add_thread, windows_delete_thread)
index 83ffdb80486d9f993e1e5e081a84dfcae5f11daf..6ea25834954c53dd24e063f0f1589eab48069ddb 100644 (file)
@@ -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/nat/windows-nat.c b/gdb/nat/windows-nat.c
new file mode 100644 (file)
index 0000000..a98ff42
--- /dev/null
@@ -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;
+}
index 543de895e77a8e5297d56a2502e932c3cd439634..695f801c58fbb7d74521277be2294bc512d1c43d 100644 (file)
@@ -34,6 +34,12 @@ 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;
 
index d2e900c2387aa16c0bf48a4bd8389b7a781ea01d..da3579942f1e17d1aba4b06494577f726a4d5921 100644 (file)
@@ -416,27 +416,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;
@@ -1515,9 +1495,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,
index 25b57685d2284e599bdf7e1fc0b222114467cbaa..29d176071ca25283ff316dc0db121d62fbcc40f1 100644 (file)
@@ -1,3 +1,10 @@
+2020-04-08  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.
+
 2020-04-08  Tom Tromey  <tromey@adacore.com>
 
        * win32-i386-low.c (update_debug_registers)
index ecdd63a310afa032dbfc56a570871567e4f51a42..7acf229fbefaf0fc779127d41806bb806a51c872 100644 (file)
@@ -74,7 +74,7 @@ case "${gdbserver_host}" 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=""
@@ -99,6 +99,7 @@ case "${gdbserver_host}" in
   i[34567]86-*-cygwin*)        srv_regobj=""
                        srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o"
                        srv_tgtobj="${srv_tgtobj} win32-i386-low.o"
+                       srv_tgtobj="${srv_tgtobj} nat/windows-nat.o"
                        srv_tgtobj="${srv_tgtobj} arch/i386.o"
                        ;;
   i[34567]86-*-linux*) srv_tgtobj="${srv_tgtobj} arch/i386.o"
@@ -126,6 +127,7 @@ case "${gdbserver_host}" in
                        srv_regobj=""
                        srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o"
                        srv_tgtobj="${srv_tgtobj} win32-i386-low.o"
+                       srv_tgtobj="${srv_tgtobj} nat/windows-nat.o"
                        srv_tgtobj="${srv_tgtobj} arch/i386.o"
                        srv_tgtobj="${srv_tgtobj} wincecompat.o"
                        # hostio_last_error implementation is in win32-low.c
@@ -136,6 +138,7 @@ case "${gdbserver_host}" in
   i[34567]86-*-mingw*) srv_regobj=""
                        srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o"
                        srv_tgtobj="${srv_tgtobj} win32-i386-low.o"
+                       srv_tgtobj="${srv_tgtobj} nat/windows-nat.o"
                        srv_tgtobj="${srv_tgtobj} arch/i386.o"
                        srv_mingw=yes
                        ;;
@@ -393,12 +396,14 @@ case "${gdbserver_host}" in
   x86_64-*-mingw*)     srv_regobj=""
                        srv_tgtobj="x86-low.o nat/x86-dregs.o i387-fp.o"
                        srv_tgtobj="${srv_tgtobj} win32-low.o win32-i386-low.o"
+                       srv_tgtobj="${srv_tgtobj} nat/windows-nat.o"
                        srv_tgtobj="${srv_tgtobj} arch/amd64.o"
                        srv_mingw=yes
                        ;;
   x86_64-*-cygwin*)    srv_regobj=""
                        srv_tgtobj="x86-low.o nat/x86-dregs.o i387-fp.o"
                        srv_tgtobj="${srv_tgtobj} win32-low.o win32-i386-low.o"
+                       srv_tgtobj="${srv_tgtobj} nat/windows-nat.o"
                        srv_tgtobj="${srv_tgtobj} arch/amd64.o"
                        ;;
 
index 1284ed177cb5497fe80ed45ec1735f46da2c6727..7cad640878f89dca1b0392c781b514f1e7ba4129 100644 (file)
@@ -171,18 +171,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);
     }
 }
@@ -435,13 +424,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 ();
        }
     }
 }
@@ -1348,17 +1331,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