From patchwork Sun Apr 28 16:58:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 32444 Received: (qmail 68044 invoked by alias); 28 Apr 2019 16:58:19 -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 67995 invoked by uid 89); 28 Apr 2019 16:58:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-17.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=theory, situations, Counts, Currently 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; Sun, 28 Apr 2019 16:58:17 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 44A06117218 for ; Sun, 28 Apr 2019 12:58:16 -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 HiaOARjsBER8 for ; Sun, 28 Apr 2019 12:58:16 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 359CD116F4B for ; Sun, 28 Apr 2019 12:58:16 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4233) id 3069D549; Sun, 28 Apr 2019 12:58:16 -0400 (EDT) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [RFA v2 2/2][master only] gdb/windows-nat.c: Get rid of main_thread_id global Date: Sun, 28 Apr 2019 12:58:11 -0400 Message-Id: <1556470691-146942-3-git-send-email-brobecker@adacore.com> In-Reply-To: <1556470691-146942-1-git-send-email-brobecker@adacore.com> References: <1555453982-77808-1-git-send-email-brobecker@adacore.com> <1556470691-146942-1-git-send-email-brobecker@adacore.com> This global is meant to point to the "main" thread of execution of the program we are debugging. It is set when attaching to a process or when receiving a CREATE_PROCESS_DEBUG_EVENT event. The theory at the time was that this was also going to be the thread receiving the EXIT_PROCESS_DEBUG_EVENT event. Unfortunately, we have discovered since then that this is actually not guaranteed. What this means in practice is that there is moderate risk that main_thread_id refers to a thread which no longer exists. This global is used in 3 situations: - OUTPUT_DEBUG_STRING_EVENT - LOAD_DLL_DEBUG_EVENT - UNLOAD_DLL_DEBUG_EVENT It's not clear why we would need to use the main_thread_id in those cases instead of using the thread ID provided by the kernel events itself. So this patch implements this approach, which then allows us to delete the main_thread_id global. gdb/testsuite: * windows-nat.c (main_thread_id): Delete. (handle_output_debug_string): Replace main_thread_id by current_event.dwThreadId. (fake_create_process): Likewise. (get_windows_debug_event) : Do not set main_thread_id. : Replace main_thread_id by current_event.dwThreadId. : Likewise. --- gdb/windows-nat.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 9f3242c..ae4e3d5 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -237,7 +237,6 @@ static DEBUG_EVENT current_event; /* The current debug event from WaitForDebugEvent */ static HANDLE current_process_handle; /* Currently executing process */ static windows_thread_info *current_thread; /* Info on currently selected thread */ -static DWORD main_thread_id; /* Thread ID of the main thread */ /* Counts of things. */ static int exception_count = 0; @@ -1030,7 +1029,7 @@ handle_output_debug_string (struct target_waitstatus *ourstatus) ourstatus->kind = TARGET_WAITKIND_STOPPED; retval = strtoul (p, &p, 0); if (!retval) - retval = main_thread_id; + retval = current_event.dwThreadId; else if ((x = (LPCVOID) (uintptr_t) strtoull (p, NULL, 0)) && ReadProcessMemory (current_process_handle, x, &saved_context, @@ -1406,14 +1405,13 @@ fake_create_process (void) (unsigned) GetLastError ()); /* We can not debug anything in that case. */ } - main_thread_id = current_event.dwThreadId; current_thread = windows_add_thread (ptid_t (current_event.dwProcessId, 0, current_event.dwThreadId), current_event.u.CreateThread.hThread, current_event.u.CreateThread.lpThreadLocalBase, true /* main_thread_p */); - return main_thread_id; + return current_event.dwThreadId; } void @@ -1611,7 +1609,6 @@ get_windows_debug_event (struct target_ops *ops, break; current_process_handle = current_event.u.CreateProcessInfo.hProcess; - main_thread_id = current_event.dwThreadId; /* Add the main thread. */ th = windows_add_thread (ptid_t (current_event.dwProcessId, 0, @@ -1656,7 +1653,7 @@ get_windows_debug_event (struct target_ops *ops, catch_errors (handle_load_dll); ourstatus->kind = TARGET_WAITKIND_LOADED; ourstatus->value.integer = 0; - thread_id = main_thread_id; + thread_id = current_event.dwThreadId; break; case UNLOAD_DLL_DEBUG_EVENT: @@ -1669,7 +1666,7 @@ get_windows_debug_event (struct target_ops *ops, catch_errors (handle_unload_dll); ourstatus->kind = TARGET_WAITKIND_LOADED; ourstatus->value.integer = 0; - thread_id = main_thread_id; + thread_id = current_event.dwThreadId; break; case EXCEPTION_DEBUG_EVENT: