From patchwork Mon Oct 14 16:15:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 34949 Received: (qmail 124220 invoked by alias); 14 Oct 2019 16:16:18 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 123577 invoked by uid 89); 14 Oct 2019 16:15:49 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 14 Oct 2019 16:15:47 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id C20375604E; Mon, 14 Oct 2019 12:15:25 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id shqFEOHR3cGB; Mon, 14 Oct 2019 12:15:25 -0400 (EDT) Received: from murgatroyd.Home (174-29-53-230.hlrn.qwest.net [174.29.53.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 6CD8F56043; Mon, 14 Oct 2019 12:15:25 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 8/9] Share Windows thread-suspend and -resume code Date: Mon, 14 Oct 2019 10:15:19 -0600 Message-Id: <20191014161520.13150-9-tromey@adacore.com> In-Reply-To: <20191014161520.13150-1-tromey@adacore.com> References: <20191014161520.13150-1-tromey@adacore.com> MIME-Version: 1.0 This adds "suspend" and "resume" methods to windows_thread_info, and changes gdb and gdbserver to share this code. 2019-10-14 Tom Tromey * windows-nat.c (thread_rec): Use windows_thread_info::suspend. (windows_continue): Use windows_continue::resume. * nat/windows-nat.h (struct windows_thread_info) : 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 * 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 . */ + +#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 + /* 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,