From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id xb/qMqFF7mpxvyoAWB0awg (envelope-from ) for ; Fri, 06 Nov 2026 16:24:49 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KsPRG5w/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9E3331E043; Fri, 06 Nov 2026 16:24:49 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,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 6D2E41E043 for ; Fri, 06 Nov 2026 16:24:48 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A9104BA2E1E for ; Fri, 6 Nov 2026 21:24:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A9104BA2E1E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KsPRG5w/ Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 96DD04BA2E06 for ; Fri, 6 Nov 2026 21:24:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 96DD04BA2E06 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 96DD04BA2E06 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::331 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794000262; cv=none; b=IGRPuZuT1Pf/pMiHZ84iPhL+gfB8b4isVQIv5E4R84zCnZ/traP1N+I/59S3zQJIlazIc+sCOX4g39pz24jV4oRSD5gRzPq0Ep81ALOTyfpKhaEeEtp1ztN4TGY+8+lCK7c76cfnryXtDMCxNqhTWnsrn7fwFvjQox9yMcNt+rA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794000262; c=relaxed/simple; bh=Z6xqfeo3dUFtwezO/2a5gUaD6Hctl1aggKqb2WJR6SU=; h=DKIM-Signature:To:Date:Message-ID:MIME-Version:From:Subject; b=fo2ws+KtazL5SPmkepFbY6vfvBC8htsPaBfmzmLRQgYdj0HRvi6f196q2e0OnNLcTU2ql0A4YJwIbs/VugKQ93x9nmGGKm40KYnmQ8MAKrZ9x07l5icd8/Xz87X8P8IzHlXEsK8bv31uJ2A1D/rXvz2DWgx+TDShP2z/zB1tnKI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=KsPRG5w/ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 96DD04BA2E06 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-4a2167bd087so10245165e9.3 for ; Fri, 06 Nov 2026 13:24:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794000261; x=1794605061; darn=sourceware.org; h=subject:from:content-transfer-encoding:content-type:mime-version :message-id:date:cc:to:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JKJ4S5gWXLW8G34pMmJwb3xGnxcvLwmheDLT1vWTjLQ=; b=KsPRG5w/kQfWYdwvodn3r3m61FM4IDp+Tm9ZkViNjm76auAtemDR9LIP7iq+f5bYfU bem3XD1OsrB7fo2qAs/5xMR6XBixkVSoqZZlZMHdjRPOtr168gQgEl5cwMiooJUGJOhN AMfXuyNa3IsuAO8BYMT9417955I0wFuLu3OQCQ3ySwOIKoqqytwOUOcMf9JzDjSowgfh noVlctRdHQr+BaBC86Coy1asTuI779unuRvEer3RSHw9nMA8wCUJM8EQTDIIssFYYw7f RAPHK+iYl/bcv/KFFCTBGRxHvtig3MSS6nehi23inQS7HVFSrZy/INm0KNUXV+mDABxf q/nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794000261; x=1794605061; h=subject:from:content-transfer-encoding:content-type:mime-version :message-id:date:cc:to:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=JKJ4S5gWXLW8G34pMmJwb3xGnxcvLwmheDLT1vWTjLQ=; b=HBytS7NdFsvou2Dw/T0iiKXH4nh4ZgZng3hPgnKD4vjTbCEd78iPlV4gNOycuROcmi itO8R7mYlRaqjmWTtUSam8VBcaR0+jGPxdNOWFCxuLsiaoKuGNANZ8mJwU7nOnmJXKBN SwGBdkJ+9xbp41kwlmd8vmZP38gnxLa5EOq29tA4slJwd1BbtGh3qkfWRjpthJTasYaX DyiqRT3u8dyfrtcIjCAFbeSSlV0wziqIQK5B3mWmW6hTFhD07Lpua/rv/bwfxIoGA9pZ ljpsp4vfGW1tDf4PznhVYHao+xqGTnCYYtFmgIWaPvT64ZnzVvfU90zlcrAkcP7rxQiT rpag== X-Gm-Message-State: APuU4Z/G9ehblot3cp8dLf6OkdIkTPJqoo47b8j/esTCka4lxxysSr0f 2sYcMZC7HhKzwYrZV+z2spYR4cmpmXQgOuCuW6dG8hgZyB2rdE0lOTYaedFrpANDQws= X-Gm-Gg: AYBFou3uHdbbioTV21M4ZeRGc0VZ0nZKCf8I1bghKUoWMBVEyUKHD906CmdDBFO64yR lwmYZHGxPE7SNauiiSHLrqHOI7KXSi6DIRcPpVFHVvT48emq3XEdE+N4jU6u6JdNv2QakwGAnCE dqfGMewQrMgVw0qG2MC5if0by8XClR5iPgjsMZ80L/bCkVVyg7+g+h2tmxiN1e/S+FJOiMVb2eX 4Dwlq9NNM7Swi30W9EVmCpFJff7eXQcSqevoET2A+asdcPMLX1CFWkGcvZUCEyy/04T8uKvMsY3 cJpUFINXtFjfwOTXvn1PjSJTHyX5VmwS40u7BRVXnVMEB34tYN8tSnWWItseE1kCDg7S7ugiVn+ J0paMDdDS9wo594MGkGe4u9zNOy0ahEX9+WdjcAQZvRJMcgwn2li6hx89bYTX1IMWY+i/KXuF6N V5z7J2aiBJxaIH5zvJRH/OJyFQOkGL54VzOkzefGmB0a/2orNgyV3ENVXEiHkUJE70jWSi5zqwE iOqA5sEqr2JurXDrnp34aKZ7qnFLVXWkwIKRaGu06qwtNigTVGU1Ok/g17XxjPo0P9+VR1z5p7+ aFwv7t1T3jTlkW7YBPndxj14Y2fH3GTJ1dZ3+rGQrRJZ18eTJoV8 X-Received: by 2002:a05:600c:6095:b0:4a1:fb01:13 with SMTP id 5b1f17b1804b1-4b6df9bf214mr64518825e9.18.1794000261225; Fri, 06 Nov 2026 13:24:21 -0800 (PST) Received: from [127.0.1.1] ([187.40.41.197]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492d6d63accsm6020797f8f.17.2026.11.06.13.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 13:24:20 -0800 (PST) To: gdb-patches@sourceware.org Cc: Pedro Alves , Andrew Burgess Date: Fri, 06 Nov 2026 21:23:56 +0000 Message-ID: <179400023614.1839834.11238127260815544254@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Josip Seketa Subject: [PATCH] gdbserver: tag all threads as stopped after an all-stop vAttach 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 After vAttach in all-stop mode, only the current thread is tagged as wanted stopped. A new thread starts as resume_continue with an ignored last_status, the same state as a thread GDB told to run. So when a later vCont resumes only another thread and waitpid has no more events, resume_stopped_resumed_lwps resumes the attached threads, which GDB believes are stopped. The normal stop path tags all threads with gdb_wants_all_threads_stopped, "so we don't resume them implicitly without the client telling us to". Do the same in handle_v_attach. "gdbserver --attach" does not need this: GDB's first "?" packet tags them in handle_status, and at that point GDB may ask for non-stop mode. Seen with extended-remote: scheduler-locking does not hold for the first step after attach, and one of those threads can hit a breakpoint while GDB steps a thread of another process in-line. GDB then fails: infrun.c: internal-error: finish_step_over: Assertion `ecs->event_thread->control.trap_expected' failed. gdb.threads/detach-step-over.exp hits that under load. Add gdb.threads/attach-schedlock-stepi.exp, which needs neither load nor a second process. --- The new test, native-extended-gdbserver, 10 runs each, alternated, no load: master fails "other threads did not run" 10 of 10, this patch passes 10 of 10. gdb.threads/detach-step-over.exp under 12 busy loops, stock gdb and gdbserver, 10 rounds each, alternated, no logging: the assertion in 7 of 10 rounds on master, in 0 of 10 with this patch. Pinned to one core it almost never shows. Bisected with a counter thread and scheduler-locking, gdbserver built from each commit, today's GDB, 8 runs each: other threads ran in 0 of 8 at 3c537f7fdb1, and in 8 of 8 at 20ba1ce66d3 ("Linux: don't resume new LWPs until we've pulled all events", 2015), which added resume_stopped_resumed_lwps. The missing tag is older. Building those commits took small changes for current glibc headers. Rejected first: tagging in attach_inferior. "gdbserver --attach" shares that function and only learns about non-stop later, so GDB got a stop for every thread (gdb.server/attach-flag.exp caught it). Changing resume_stopped_resumed_lwps instead does not work: it cannot tell these threads from ones GDB told to run. Tested on x86-64 GNU/Linux: gdb.server, gdb.threads and gdb.multi on native-extended-gdbserver, default and with target-non-stop on, compared with master test by test. Differences were flaky tests, in both directions; each one was rerun or explained. Not tested on other architectures. I have no FSF copyright assignment. This is my own work, not my employer's. .../gdb.threads/attach-schedlock-stepi.c | 55 ++++++++++++++++++ .../gdb.threads/attach-schedlock-stepi.exp | 57 +++++++++++++++++++ gdbserver/server.cc | 10 +++- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.threads/attach-schedlock-stepi.c create mode 100644 gdb/testsuite/gdb.threads/attach-schedlock-stepi.exp diff --git a/gdb/testsuite/gdb.threads/attach-schedlock-stepi.c b/gdb/testsuite/gdb.threads/attach-schedlock-stepi.c new file mode 100644 index 00000000000..3791f7ec82d --- /dev/null +++ b/gdb/testsuite/gdb.threads/attach-schedlock-stepi.c @@ -0,0 +1,55 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 Free Software Foundation, Inc. + + 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 +#include + +#define NTHREADS 10 + +/* Incremented by the threads that the testcase never resumes. */ +volatile unsigned long counter; + +/* Incremented by the main thread, which the testcase steps. */ +volatile unsigned long tick; + +/* Set to 1 once all threads run. */ +volatile int started; + +static void * +spin (void *arg) +{ + for (;;) + counter++; + return arg; +} + +int +main (void) +{ + pthread_t thread; + int i; + + alarm (60); + + for (i = 0; i < NTHREADS; i++) + pthread_create (&thread, NULL, spin, NULL); + + started = 1; + + for (;;) + tick++; +} diff --git a/gdb/testsuite/gdb.threads/attach-schedlock-stepi.exp b/gdb/testsuite/gdb.threads/attach-schedlock-stepi.exp new file mode 100644 index 00000000000..228423c022d --- /dev/null +++ b/gdb/testsuite/gdb.threads/attach-schedlock-stepi.exp @@ -0,0 +1,57 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# 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 . + +# Attach to a running multi-threaded process, and step one thread with +# scheduler-locking on. No other thread may run. With gdbserver, +# threads that were never reported to GDB after vAttach used to be +# resumed behind GDB's back. + +require can_spawn_for_attach + +standard_testfile + +if { [build_executable "failed to prepare" $testfile $srcfile \ + {debug pthreads}] } { + return +} + +set test_spawn_id [spawn_wait_for_attach $binfile] +set testpid [spawn_id_get_pid $test_spawn_id] + +clean_restart $testfile + +gdb_test "attach $testpid" "Attaching to.*" "attach" + +# The threads may not have been started yet when we attached. Do not +# try again in that case: the test is only meaningful with them. +if { [get_integer_valueof "started" 0] == 0 } { + unsupported "attached too early" + kill_wait_spawned_process $test_spawn_id + return +} + +gdb_test_no_output "set scheduler-locking on" +gdb_test "thread 1" "Switching to thread 1.*" + +set pc_before [get_hexadecimal_valueof "\$pc" "unknown" "pc before stepi"] +set before [get_integer_valueof "counter" -1 "counter before stepi"] +gdb_test "stepi" ".*" "step the main thread" +set after [get_integer_valueof "counter" -2 "counter after stepi"] +set pc_after [get_hexadecimal_valueof "\$pc" "unknown" "pc after stepi"] + +gdb_assert { $pc_before != $pc_after } "the main thread stepped" +gdb_assert { $before == $after } "other threads did not run" + +kill_wait_spawned_process $test_spawn_id diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 74774fcde7c..90307c38b50 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -3377,7 +3377,15 @@ handle_v_attach (char *own_buf) write_ok (own_buf); } else - prepare_resume_reply (own_buf, cs.last_ptid, cs.last_status); + { + /* All-stop mode always stops all threads implicitly. Tag + them all as "want-stopped", not only the one + attach_inferior tagged, or they look like running threads + to resume_stopped_resumed_lwps, which would resume them + without the client telling us to. */ + gdb_wants_all_threads_stopped (); + prepare_resume_reply (own_buf, cs.last_ptid, cs.last_status); + } } else { -- 2.56.0