From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id FrpFOpdwNWoGkQ8AWB0awg (envelope-from ) for ; Fri, 19 Jun 2026 12:38:47 -0400 Received: by simark.ca (Postfix, from userid 112) id DF78E1E070; Fri, 19 Jun 2026 12:38:47 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (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 2FA4B1E070 for ; Fri, 19 Jun 2026 12:38:47 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F224B4BAE7FC for ; Fri, 19 Jun 2026 16:38:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F224B4BAE7FC Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by sourceware.org (Postfix) with ESMTPS id B0FD94BAE7E0 for ; Fri, 19 Jun 2026 16:38:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B0FD94BAE7E0 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 B0FD94BAE7E0 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1781887102; cv=none; b=YVm1lNKy8zfQnD0oBa2QROH+kLobsapGydzw0+VuJ23n05mmU3A65Hy4tRZDAck/ZEmB2lnoD2GezsgigfpRzLnhi7a7i+WX4uflHd83AJ1jH++WZNiCnZSPY03D35dssmaZVl6QsJAqNTVOx4WGbejgtZPk90ShJoCv7OHqqr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1781887102; c=relaxed/simple; bh=yYGu8vx/AiFc4a1FV0dFNBLBJH7i0UxzS2ZOrQQnt0Y=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=lGS9bh0TcuYRu4qK7l1BvEJyhZ+6sBwlQLEv9W+6h+PTvmXy8gLybcViJH/5WcOwNMLSY+10LX2z0660gLmEvojl8l5vEiZtx6jk4NCQDhnlbA1ilLDUplOJJEY22BLfPXVGnAzAysNLl1UI3zjGrD9LQ1YbDuG8FqWcUh8T7fw= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0FD94BAE7E0 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-490b4a8e28bso17217815e9.1 for ; Fri, 19 Jun 2026 09:38:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781887101; x=1782491901; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tBfqm19+eYdbASay9ccY/DbMjipb6bGCRjZ12EcFjhg=; b=CHn1/jAVJSvgdULH6nz6e4zVPe6VuscFg55K4q2ru8KhAXJ22IPbDtwWPt14y3BVyT 7scBJgI/Kj9euDjvYpX3cBsA8OjJMhLbLppix1W7WKwoAbGu215SA6C83SW6Rwz95qg2 UmGad1LEaX5/tgoLPfTSlqHsJE2D402upjI/qdlN//4X6h8ACxYbbsHXts+BUX9F3fyV gObVJZVRlbcJva0asQB//WYGv2gWy+S0GOqxdFWJ5YEpQAV8uY8rW1gkxtQd0mUxdU0v BUofBnniL8zRCl0LL6Edl1bxwa7Q0vajFy1vT//alhujir1JtzVI4I/nBVL9undUn3Yh MCHg== X-Gm-Message-State: AOJu0Yw1XACSvlbf2SYUV3nLMguPgEn8slsh1iIIq96LlcXAVBDvLa92 GhZwUP2PbkHj9sEtqyl/vszZKl680A/guy2r0elPua/celJIlNmthZwIm82sSw== X-Gm-Gg: AfdE7ckTJkfpGaVyeWw/xYaDIlZ4KdDhpiSQ5sAnh1hg2/JCi9kbJ0jjadKNOGq/UhW wNQcVnMozTLyCb9qacJC/bZGlZkJMIij3yaGgxpvrXgk8YBgLel6JGgjMN/RwUVlHSCdF4LfbIp cIZx56HnzYZod54Zg+hCgSHXh9fQtjZ3vN01762xzD2a6ti8tXmF4o/YrGJ2tBGPWU8ZwJyMt7J o4o6kQj25redyoI9p0WZyQeNy+cr+89JldYPI2ov898JH7/Ni2dI4ACEoCF/uIulxPG8yfWyBNR odkjfMtoZg2O0ZtIakp5SREny5fV9s/fkJFb4cGZbcLsO7CQ5az18TF2p46tCP02uUt11s0JKGz yyrpmcHITLhxBBf5PY7aHlhPEDHBQ+DeWZ4EONtZbcsr5XjU376bSn0HaxVNpHBWDn5OWTx4Q2w dfBfU7dsbRAr4= X-Received: by 2002:a05:600c:1c1c:b0:490:6237:5200 with SMTP id 5b1f17b1804b1-49240e40982mr80120045e9.10.1781887101310; Fri, 19 Jun 2026 09:38:21 -0700 (PDT) Received: from localhost ([2001:8a0:fae3:2600:fa79:b88e:8dae:a26f]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-49240eef2basm77166425e9.2.2026.06.19.09.38.20 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 19 Jun 2026 09:38:20 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH] gdb/Windows: Make windows-nat skip iterating GPU threads Date: Fri, 19 Jun 2026 17:38:13 +0100 Message-ID: <20260619163813.815335-1-pedro@palves.net> X-Mailer: git-send-email 2.54.0 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 When the AMD GPU dbgapi target is pushed on the stack, the GPU threads are put in the same inferior thread list as the CPU threads. all_windows_threads() returns an iterator that walks all threads that have an inferior whose process_target() returns the Windows target. So it ends up walking GPU threads as well, which is incorrect and leads to crashes inside gdb/windows-nat.c. E.g.: (gdb) interrupt -a Thread 1 received signal SIGSEGV, Segmentation fault. 0x00007ff6bf27ad9b in windows_nat_target::stop_one_thread (this=0x11b7850, th=0x0, stopping_kind=windows_nat::SK_EXTERNAL) at .../gdb/windows-nat.c:1984 ... (gdb) detach Thread 1 received signal SIGSEGV, Segmentation fault. 0x00007ff774ead9ee in windows_nat_target::detach (this=0x12a77c0, inf=0x1299460, from_tty=0) at .../gdb/windows-nat.c:3047 ... Fix this by: - Making as_windows_thread_info use dynamic_cast instead of gdb::checked_static_cast, so only CPU threads return non-NULL. - Making the windows threads iterator operator++ skip threads for which as_windows_thread_info() returns NULL. Same for all_windows_threads_range::begin(). - Adjusting a few spots that iterate over all threads instead of using all_windows_threads (because they need info from the thread_info object) to likewise skip non-Windows threads. No new test, as the existing gdb.rocm/ tests would catch this. We just can't run them yet due to a few more missing Windows pieces. Change-Id: If98caf405a7373d95d885dfd31ac491fe4b01c60 --- gdb/windows-nat.c | 38 ++++++++++++++++++++++++++++++++++---- gdb/windows-nat.h | 19 ++++++++++++------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 3399b8c3303..7cdee85c9f0 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -320,6 +320,31 @@ static const struct xlate_exception xlate[] = #endif /* 0 */ +void +all_windows_threads_iterator::advance () +{ + /* Skip threads owned by targets on the target stack other than + windows-nat. E.g., GPU threads. */ + do + { + ++m_base_iter; + } + while (m_base_iter != all_non_exited_threads_iterator {} + && as_windows_thread_info (&*m_base_iter) == nullptr); +} + +all_windows_threads_iterator +all_windows_threads_range::begin () const +{ + /* Find the first thread owned by the windows-nat target, if + any. */ + auto begin = m_base_range.begin (); + while (begin != m_base_range.end () + && as_windows_thread_info (&*begin) == nullptr) + ++begin; + return all_windows_threads_iterator (begin); +} + void check (BOOL ok, const char *file, int line) { @@ -1332,10 +1357,11 @@ windows_nat_target::stop_interrupt (ptid_t ptid, bool stop_on_first_match) { for (thread_info &thr : all_non_exited_threads (this)) { - if (!thr.ptid.matches (ptid)) + auto *w_th = as_windows_thread_info (&thr); + if (w_th == nullptr || !thr.ptid.matches (ptid)) continue; - if (stop_one_thread (as_windows_thread_info (&thr), SK_EXTERNAL)) + if (stop_one_thread (w_th, SK_EXTERNAL)) { if (stop_on_first_match) return; @@ -1420,7 +1446,8 @@ windows_nat_target::get_windows_debug_event continue; auto *th = as_windows_thread_info (&thread); - if (thread.internal_state () == THREAD_INT_RUNNING + if (th != nullptr + && thread.internal_state () == THREAD_INT_RUNNING && th->suspended && th->pending_status.kind () != TARGET_WAITKIND_IGNORE) { @@ -2308,9 +2335,12 @@ windows_nat_target::detach (inferior *inf, int from_tty) flag. */ for (thread_info &thr : inf->non_exited_threads ()) { + auto *w_th = as_windows_thread_info (&thr); + if (w_th == nullptr) + continue; + if (thr.internal_state () != THREAD_INT_RUNNING) { - windows_thread_info *w_th = windows_process->find_thread (thr.ptid); gdb_signal signo = get_detach_signal (this, thr.ptid); if (signo != w_th->last_sig diff --git a/gdb/windows-nat.h b/gdb/windows-nat.h index cbe5429fd84..8972e56feea 100644 --- a/gdb/windows-nat.h +++ b/gdb/windows-nat.h @@ -106,16 +106,19 @@ struct windows_private_thread_info : private_thread_info, windows_thread_info {} }; -/* Get the windows_thread_info object associated with THR. */ +/* If THR belongs to the windows-nat target, returns the + windows_thread_info object associated with it. Otherwise returns + NULL. */ static inline windows_thread_info * as_windows_thread_info (thread_info *thr) { /* Cast to windows_private_thread_info, which inherits from - private_thread_info, and is implicitly convertible to - windows_thread_info, the return type. */ + windows_thread_info, the return type. We use dynamic_cast, + because the inferior's thread list may have threads from other + targets on the target stack. */ private_thread_info *priv = thr->priv.get (); - return gdb::checked_static_cast (priv); + return dynamic_cast (priv); } struct windows_per_inferior : public windows_nat::windows_process_info @@ -440,7 +443,7 @@ class all_windows_threads_iterator all_windows_threads_iterator &operator++ () { - ++m_base_iter; + advance (); return *this; } @@ -451,6 +454,9 @@ class all_windows_threads_iterator { return !(*this == other); } private: + /* Advance to the next windows-nat thread. */ + void advance (); + all_non_exited_threads_iterator m_base_iter; }; @@ -463,8 +469,7 @@ class all_windows_threads_range : public all_non_exited_threads_range : m_base_range (base_range) {} - all_windows_threads_iterator begin () const - { return all_windows_threads_iterator (m_base_range.begin ()); } + all_windows_threads_iterator begin () const; all_windows_threads_iterator end () const { return all_windows_threads_iterator (m_base_range.end ()); } base-commit: fd3cf86c638b7fdc0ccb64e178ec43ccd7e9d3f9 -- 2.54.0