From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id tXGvOLnJAmrQtzIAWB0awg (envelope-from ) for ; Tue, 12 May 2026 02:33:29 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Dx575XnX; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CFE371E0C3; Tue, 12 May 2026 02:33:29 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED 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 188BF1E093 for ; Tue, 12 May 2026 02:33:28 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 846B64BB24CC for ; Tue, 12 May 2026 06:33:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 846B64BB24CC Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Dx575XnX Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by sourceware.org (Postfix) with ESMTPS id C94994BA799E for ; Tue, 12 May 2026 06:32:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C94994BA799E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C94994BA799E Authentication-Results: sourceware.org; arc=none smtp.remote-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778567569; cv=none; b=pIqNr6cCRpeqtqS+tFCg5wsDhKRcsvov3NTqwmeerjeGaAa+ikIbH71nG5smcBzc14zdvcvooBaBspa+o3z29mHgMhusAp9GA7keI+ay2VXYcibqgA9vjsRB5RK9Q/Nj8Kbn/ugBW6P7p+rDVpniGjc3UNDqb8ZC8lD7G3EOBRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778567569; c=relaxed/simple; bh=l6vMkPcrIYIjfVQZizFqjqm6Gn8tDNSVGT2L7D54s3M=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=K1gdHQK2DWKr7UXf8axPqIpz05/7ocYmYoORRBXl/XhL0DuGYlmgGZcF697GtKGBAzi8E6Uytvn7fl0fBSw45+CYS0Sv1b8Jv86MZ/HbsbOw4C19VDrruqmS5HG8KEwdrXw33vVdyHMvKbRkYtyc/HQ2xLLII5XVWgi/itQs0zk= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Dx575XnX DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C94994BA799E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1778567569; x=1810103569; h=from:to:subject:date:message-id:mime-version: content-transfer-encoding; bh=l6vMkPcrIYIjfVQZizFqjqm6Gn8tDNSVGT2L7D54s3M=; b=Dx575XnXqkW+Dxvejc3nvvnsifR2dH9lSSn30x/Sm2lnKY2Wmrb/C+b+ P2YqIBxRB3mWGje4aD/+9fzSHF9Ny4oevzQI/tEESSQQ8W9WLJoq+jzKe WzRYM4UkWMMupZlxPZMSf7swwaVFd8uUrVKvMcn+Kyn7b2oRyurj9LSBJ 0hzjDHnDPDCrFc3X/hiuw2kYzIsID4qsYTczHjVqKkT4QJKfgQrxiu9ue 9eypUCT1l6Cf5vOvRkmus8gBoFgXuHnfi447xV0Q+CK7RrgYrgwSSzwqO 8OMssHo9ZEm3+1X1bDl7bSGfjHgIslD3iLf3OmhBxQHVioRXPx28ipk+D A==; X-CSE-ConnectionGUID: ZKMPFbMMRBSCWS6a1RILvw== X-CSE-MsgGUID: jY+ogXkvQXuHQXp2i72yWQ== X-IronPort-AV: E=McAfee;i="6800,10657,11783"; a="102133521" X-IronPort-AV: E=Sophos;i="6.23,230,1770624000"; d="scan'208";a="102133521" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2026 23:32:48 -0700 X-CSE-ConnectionGUID: rsfwKfKHSiOIDhjNdNtUJQ== X-CSE-MsgGUID: cAXPJpdiRyGmvsjRyHBfTg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,230,1770624000"; d="scan'208";a="261170828" Received: from gkldtt-dev-004.igk.intel.com (HELO localhost) ([10.123.221.202]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2026 23:32:47 -0700 From: Markus Metzger To: gdb-patches@sourceware.org Subject: [PATCH] gdb: use all_threads() in for_each_thread() Date: Tue, 12 May 2026 06:32:44 +0000 Message-Id: <20260512063244.2790197-1-markus.t.metzger@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 In for_each_thread(), we use all_threads_safe(), which allows deleting the thread. This it not needed in the cases where it is currently used and it causes the iterator to get corrupted when threads are deleted in a nested all_threads_safe() iteration. One such scenario is as follows: - remote debugging in non-stop mode - one thread has hit a breakpoint - one thread exited but GDB does not know about it, yet - resume with 'continue -a' - the continue command uses for_each_thread() to proceed threads individually - the stopped thread starts an inline step-over (displaced stepping disabled) - update_thread_list() learns about the exit and deletes the thread - the safe iterator's m_next still points to the deleted thread --- gdb/gdbthread.h | 9 ++++++++- gdb/thread.c | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index b3052b28d1a..1bdbf621982 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -796,7 +796,14 @@ void thread_change_ptid (process_stratum_target *targ, using for_each_thread_callback_ftype = gdb::function_view; -/* Call CALLBACK once for each known thread. */ +/* Call CALLBACK once for each known thread. + + CALLBACK must not delete the thread. To delete threads, use: + + for (thread_info &t : all_threads_safe ()) + if (some_condition ()) + delete &t; +*/ extern void for_each_thread (for_each_thread_callback_ftype callback); diff --git a/gdb/thread.c b/gdb/thread.c index 4f62ca280cd..110e58a70aa 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -605,7 +605,7 @@ find_thread_by_handle (gdb::array_view handle, void for_each_thread (for_each_thread_callback_ftype callback) { - for (thread_info &tp : all_threads_safe ()) + for (thread_info &tp : all_threads ()) callback (&tp); } -- 2.34.1 Intel Deutschland GmbH Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany Tel: +49 89 991 430, www.intel.de Managing Directors: Harry Demas, Jeffrey Schneiderman, Yin Chong Sorrell Chairperson of the Supervisory Board: Nicole Lau Registered Seat: Munich Commercial Register: Amtsgericht Muenchen HRB 186928