From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8JyuI1ExK2ioNSoAWB0awg (envelope-from ) for ; Mon, 19 May 2025 09:25:37 -0400 Received: by simark.ca (Postfix, from userid 112) id 8FB661E11C; Mon, 19 May 2025 09:25:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-9.0 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 6A8B11E102 for ; Mon, 19 May 2025 09:25:36 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC6D0385828E for ; Mon, 19 May 2025 13:25:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC6D0385828E Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by sourceware.org (Postfix) with ESMTPS id 0BAB53858D3C for ; Mon, 19 May 2025 13:23:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0BAB53858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0BAB53858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1747661012; cv=none; b=QkOSXZwOEXhDYMFY48cIztG/94xToCFhKyDeHHENWKqvka7e10EiX+Q/4n+KgKGCLhrA2egYAFsJPCLM33FvgvtTMOlLYbzo4bW/fUtMKZ70g6cVOC1Gqx9vMBgUwInriyCxu0fWZeetspJyTprOEZBoLzHducmPwW69ohDyumA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1747661012; c=relaxed/simple; bh=i5vxF1ZvvPGx0HrUl+Q/Mvrw6h+jblE2yWvGgrQSTAo=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=J6+F5dvOoYIXGqk9yMZf4twNiKxj9IOCeBRCUmWSJZD7opCTdBQ9sZUztDyhNNDLxFtQHHZ54XpB6+xXL4x/zBjzvg2WKv6v8O6ZoPTjG+DNJv7YfDYY284DlEGOubhSwmi2yMZsyzpE1puMLoSzSqb8jaoBn+USYqGK8SX3BBs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BAB53858D3C Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-3a375888197so349108f8f.0 for ; Mon, 19 May 2025 06:23:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747661010; x=1748265810; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SoQYOv56PzjkcNtv5WWL/cOXk3uMC9du0/fOfr64p/M=; b=kow6SfZllloLHf4OkUwI1tnM566/+YueezXYLPa8KP0gs85Ym00t0g7A0LVHlWLSqd 1Z919UnDUGrlblNPVSUatNAX4pG7OixVfbZb+oUoiLux4km5sZth4VmgfWwcpgGIPqq9 mxnhQc6YSfZtdfp9UEyTC6URlBxnDTxnPoJGz5WteZv4zaRtuMa1FrAw0dJZKIDcm04s V0ZUdCggxvENZWGmpdEn2X/OjUIptnDQktF7Z9V5W2e7Suzccwa4iUUD9oQ8PX6o4VLm DmNkFX3wO/okkfN7gH4CvhiWIPlGrp5pWT31i4Gtmv40TfDKOKSYfGmu9X8NZeMnfVmR FN9w== X-Gm-Message-State: AOJu0YxLztdy+IHfQko0TeKPQbbPiTOg5ic06ApEzp2/CB60/0Wj89Y2 wJtvbwYCnuGJ/J3Hi+3RpK1yRCwh4v7YOX4wXou/8g1reF3Kf+NjjsQSlFNDXmlo X-Gm-Gg: ASbGncu6OUkVCLoiQR1p/EHnC3YeVC2LnlQ3v4k+SRYWQgj8xO5hFrtd9ZWabL1deq+ NOh55NGEMMpw5dMUfERk8V8QBS3McvKOQPe5TzZjPr/HO/u10m8D/I01rNf5uSGi7yOcT7CsQGk qfix8qOJFmWBTNfBkbO3vjP2Jxz1J409/QdbC/rGNe2p/XsR4Pd02FFia1x1eqTh9x4N9gwgdmI 7YS9uwC4o9g/pzWIM98YR292XG+Opd01blZnLlFAMEIo1SoebmvVsXv+qOnPjMaFT7mfMiOhBnj WZAZKWNgzBnEfPDLJbgqcC8nwsK1r5hqeQ+oeNVbNtavdVCH0OyzKh+r6IXWsQ== X-Google-Smtp-Source: AGHT+IFXHCKDZ2etx7FdBrKlxyTMZx0PpqnFMBimq8+SXZXea9LJNPJ4qTEj5WSeXS1mdNwb+cBfEg== X-Received: by 2002:a05:6000:3108:b0:3a0:b565:f1ea with SMTP id ffacd0b85a97d-3a35c834fbdmr9445256f8f.5.1747661010336; Mon, 19 May 2025 06:23:30 -0700 (PDT) Received: from localhost ([2001:8a0:4fe9:b400:8d90:6f0d:36bf:32df]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-3a362b4e2e1sm11473330f8f.96.2025.05.19.06.23.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 May 2025 06:23:29 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH v2 07/47] Windows gdb: Eliminate global current_process.dr[8] global Date: Mon, 19 May 2025 14:22:28 +0100 Message-ID: <20250519132308.3553663-8-pedro@palves.net> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250519132308.3553663-1-pedro@palves.net> References: <20250519132308.3553663-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org current_process.dr needs to be per-thread for non-stop. Actually, it doesn't even need to exist at all. We have x86_debug_reg_state recording intent, and then the cygwin_get_dr/cygwin_get_dr6/cygwin_get_dr7 functions are registered as x86_dr_low_type vector functions, so they should return the current value in the inferior's registers. See this comment in x86-dregs.c: ~~~ /* In non-stop/async, threads can be running while we change the global dr_mirror (and friends). Say, we set a watchpoint, and let threads resume. Now, say you delete the watchpoint, or add/remove watchpoints such that dr_mirror changes while threads are running. On targets that support non-stop, inserting/deleting watchpoints updates the global dr_mirror only. It does not update the real thread's debug registers; that's only done prior to resume. Instead, if threads are running when the mirror changes, a temporary and transparent stop on all threads is forced so they can get their copy of the debug registers updated on re-resume. Now, say, a thread hit a watchpoint before having been updated with the new dr_mirror contents, and we haven't yet handled the corresponding SIGTRAP. If we trusted dr_mirror below, we'd mistake the real trapped address (from the last time we had updated debug registers in the thread) with whatever was currently in dr_mirror. So to fix this, dr_mirror always represents intention, what we _want_ threads to have in debug registers. To get at the address and cause of the trap, we need to read the state the thread still has in its debug registers. In sum, always get the current debug register values the current thread has, instead of trusting the global mirror. If the thread was running when we last changed watchpoints, the mirror no longer represents what was set in this thread's debug registers. */ ~~~ This patch makes the Windows native target follow that model as well. I don't understand why would windows_nat_target::resume want to call SetThreadContext itself. That duplicates things as it is currently worrying about setting the debug registers as well. windows_continue also does that, and windows_nat_target::resume always calls it. So this patch simplifies windows_nat_target::resume too. Tromey pointed out that gdb/2388 mentioned in the code being removed was moved to https://sourceware.org/bugzilla/show_bug.cgi?id=9493 in the bugzilla migration. I tried the reproducer mentioned there, and it still works correctly. Change-Id: Id762d0faa7d5e788402f2ff5adad5352447a7526 --- gdb/nat/windows-nat.h | 1 + gdb/windows-nat.c | 92 ++++++++++++++++++------------------------ gdbserver/win32-low.cc | 1 + 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h index 2f50742c810..806bd352705 100644 --- a/gdb/nat/windows-nat.h +++ b/gdb/nat/windows-nat.h @@ -150,6 +150,7 @@ struct windows_process_info { /* The process handle */ HANDLE handle = 0; + DWORD process_id = 0; DWORD main_thread_id = 0; enum gdb_signal last_sig = GDB_SIGNAL_0; diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 092cb699a4a..fbe36102e2e 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -100,8 +100,6 @@ struct windows_per_inferior : public windows_process_info void handle_unload_dll () override; bool handle_access_violation (const EXCEPTION_RECORD *rec) override; - uintptr_t dr[8] {}; - int windows_initialization_done = 0; std::vector> thread_list; @@ -721,18 +719,6 @@ windows_nat_target::fetch_registers (struct regcache *regcache, int r) { context->ContextFlags = WindowsContext::all; CHECK (get_thread_context (th->h, context)); - /* Copy dr values from that thread. - But only if there were not modified since last stop. - PR gdb/2388 */ - if (!th->debug_registers_changed) - { - windows_process.dr[0] = context->Dr0; - windows_process.dr[1] = context->Dr1; - windows_process.dr[2] = context->Dr2; - windows_process.dr[3] = context->Dr3; - windows_process.dr[6] = context->Dr6; - windows_process.dr[7] = context->Dr7; - } }); th->reload_context = false; @@ -1224,18 +1210,21 @@ windows_nat_target::windows_continue (DWORD continue_status, int id, for (auto &th : windows_process.thread_list) if (id == -1 || id == (int) th->tid) { + struct x86_debug_reg_state *state + = x86_debug_reg_state (windows_process.process_id); + windows_process.with_context (th.get (), [&] (auto *context) { if (th->debug_registers_changed) { context->ContextFlags |= WindowsContext::debug; - context->Dr0 = windows_process.dr[0]; - context->Dr1 = windows_process.dr[1]; - context->Dr2 = windows_process.dr[2]; - context->Dr3 = windows_process.dr[3]; + context->Dr0 = state->dr_mirror[0]; + context->Dr1 = state->dr_mirror[1]; + context->Dr2 = state->dr_mirror[2]; + context->Dr3 = state->dr_mirror[3]; context->Dr6 = DR6_CLEAR_VALUE; - context->Dr7 = windows_process.dr[7]; + context->Dr7 = state->dr_control_mirror; th->debug_registers_changed = false; } if (context->ContextFlags) @@ -1374,22 +1363,6 @@ windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig) fetch_registers (regcache, gdbarch_ps_regnum (gdbarch)); context->EFlags |= FLAG_TRACE_BIT; } - - if (context->ContextFlags) - { - if (th->debug_registers_changed) - { - context->Dr0 = windows_process.dr[0]; - context->Dr1 = windows_process.dr[1]; - context->Dr2 = windows_process.dr[2]; - context->Dr3 = windows_process.dr[3]; - context->Dr6 = DR6_CLEAR_VALUE; - context->Dr7 = windows_process.dr[7]; - th->debug_registers_changed = false; - } - CHECK (set_thread_context (th->h, context)); - context->ContextFlags = 0; - } }); } @@ -1770,19 +1743,15 @@ windows_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, void windows_nat_target::do_initial_windows_stuff (DWORD pid, bool attaching) { - int i; struct inferior *inf; windows_process.last_sig = GDB_SIGNAL_0; windows_process.open_process_used = 0; - for (i = 0; - i < sizeof (windows_process.dr) / sizeof (windows_process.dr[0]); - i++) - windows_process.dr[i] = 0; #ifdef __CYGWIN__ windows_process.cygwin_load_start = 0; windows_process.cygwin_load_end = 0; #endif + windows_process.process_id = pid; memset (&windows_process.current_event, 0, sizeof (windows_process.current_event)); inf = current_inferior (); @@ -3205,7 +3174,6 @@ cygwin_set_dr (int i, CORE_ADDR addr) { if (i < 0 || i > 3) internal_error (_("Invalid register %d in cygwin_set_dr.\n"), i); - windows_process.dr[i] = addr; for (auto &th : windows_process.thread_list) th->debug_registers_changed = true; @@ -3217,8 +3185,6 @@ cygwin_set_dr (int i, CORE_ADDR addr) static void cygwin_set_dr7 (unsigned long val) { - windows_process.dr[7] = (CORE_ADDR) val; - for (auto &th : windows_process.thread_list) th->debug_registers_changed = true; } @@ -3228,26 +3194,48 @@ cygwin_set_dr7 (unsigned long val) static CORE_ADDR cygwin_get_dr (int i) { - return windows_process.dr[i]; + windows_thread_info *th + = windows_process.thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT); + + return windows_process.with_context (th, [&] (auto *context) -> CORE_ADDR + { + gdb_assert (context->ContextFlags != 0); + switch (i) + { + case 0: + return context->Dr0; + case 1: + return context->Dr1; + case 2: + return context->Dr2; + case 3: + return context->Dr3; + case 6: + return context->Dr6; + case 7: + return context->Dr7; + }; + + gdb_assert_not_reached ("invalid x86 dr register number: %d", i); + }); } -/* Get the value of the DR6 debug status register from the inferior. - Here we just return the value stored in dr[6] - by the last call to thread_rec for current_event.dwThreadId id. */ +/* Get the value of the DR6 debug status register from the + inferior. */ + static unsigned long cygwin_get_dr6 (void) { - return (unsigned long) windows_process.dr[6]; + return cygwin_get_dr (6); } -/* Get the value of the DR7 debug status register from the inferior. - Here we just return the value stored in dr[7] by the last call to - thread_rec for current_event.dwThreadId id. */ +/* Get the value of the DR7 debug status register from the + inferior. */ static unsigned long cygwin_get_dr7 (void) { - return (unsigned long) windows_process.dr[7]; + return cygwin_get_dr (7); } /* Determine if the thread referenced by "ptid" is alive diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc index 89831de9d43..61f124337a5 100644 --- a/gdbserver/win32-low.cc +++ b/gdbserver/win32-low.cc @@ -296,6 +296,7 @@ do_initial_child_stuff (HANDLE proch, DWORD pid, int attached) windows_process.last_sig = GDB_SIGNAL_0; windows_process.handle = proch; + windows_process.process_id = pid; windows_process.main_thread_id = 0; windows_process.soft_interrupt_requested = 0; -- 2.49.0