From: Josip Seketa <josip.seketa@gmail.com>
To: gdb-patches@sourceware.org
Cc: Pedro Alves <pedro@palves.net>, Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdbserver: tag all threads as stopped after an all-stop vAttach
Date: Fri, 06 Nov 2026 21:23:56 +0000 [thread overview]
Message-ID: <179400023614.1839834.11238127260815544254@gmail.com> (raw)
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 <http://www.gnu.org/licenses/>. */
+
+#include <pthread.h>
+#include <unistd.h>
+
+#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 <http://www.gnu.org/licenses/>.
+
+# 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
reply other threads:[~2026-11-06 21:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179400023614.1839834.11238127260815544254@gmail.com \
--to=josip.seketa@gmail.com \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=pedro@palves.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox