* [PATCH] gdb/remote: tell the stub about "set non-stop" before run and attach
@ 2026-11-08 19:00 Josip Seketa
0 siblings, 0 replies; only message in thread
From: Josip Seketa @ 2026-11-08 19:00 UTC (permalink / raw)
To: gdb-patches; +Cc: Pedro Alves
GDB tells a remote stub its mode with QNonStop once, when connecting.
But "set non-stop" only refuses to change while the program is running,
so on an extended-remote connection with no program yet the user can
change it, and the stub is never told:
(gdb) target extended-remote :1234
(gdb) set non-stop on
(gdb) run
Unexpected vCont reply in non-stop mode: T05swbreak:;...
The two now disagree on how to talk to each other. The first stop is
rejected, and the program stays shown as running. The other direction,
"set non-stop off", hangs in run, and attach is affected the same way.
Remember what the stub was last told, and tell it again before run and
attach if that is no longer what GDB is in. A session that does not
change the setting sends the same packets as before.
Do not do that while another program runs on the same connection.
"set non-stop" only checks the current inferior, so it can be changed
with a second, empty inferior selected, and switching the stub would
change how it runs the first program too. That case stays as it was.
A stub that does not support QNonStop now gets the same error at run
and attach as at connect, "Non-stop mode requested, but remote does not
support non-stop", instead of running into the mismatch above.
Move the QNonStop code to a function, to use it for this.
---
gdb.server/non-stop-after-connect.exp is new. It does not cover a stub
without QNonStop: gdbserver always supports it, and GDB has no "set
remote" command to turn it off.
On native-extended-gdbserver this also fixes the "run to main" failures
of gdb.threads/break-while-running.exp, multi-create-ns-info-thr.exp,
non-stop-fair-events.exp and thread-execl.exp, 7 in all, which set
non-stop after the board connected.
Tested on x86_64 GNU/Linux, master at 819dfa7b532, against the same
commit without the patch, with an unpatched gdbserver, on gdb.server
and the four tests above. The new test fails 10 of 26 on master with
native-extended-gdbserver and with unix, and 5 of 14 with
native-gdbserver, and passes on all three with the patch; the four
tests go from 7 failures to none. No new failures, and with "maint set
target-non-stop on" the results are the same with and without the
patch.
I have no FSF copyright assignment. This is my own work, not my
employer's.
gdb/remote.c | 96 +++++++++++++------
.../gdb.server/non-stop-after-connect.exp | 93 ++++++++++++++++++
2 files changed, 160 insertions(+), 29 deletions(-)
create mode 100644 gdb/testsuite/gdb.server/non-stop-after-connect.exp
diff --git a/gdb/remote.c b/gdb/remote.c
index a728d57df6e..132840259b3 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -609,6 +609,10 @@ class remote_state
because we allow GDB commands while the target is running. */
bool waiting_for_stop_reply = false;
+ /* The mode the stub was last told about with QNonStop. "set
+ non-stop" can change GDB's side of this while connected. */
+ bool stub_non_stop = false;
+
/* The status of the stub support for the various vCont actions. */
vCont_action_support supports_vCont;
@@ -1307,6 +1311,14 @@ class remote_target : public process_stratum_target
ptid_t ptid);
static void open_1 (const char *name, int from_tty, int extended_p);
void start_remote (int from_tty, int extended_p);
+
+ /* Tell the stub to be in non-stop mode if ON, else all-stop. */
+ void set_stub_non_stop (bool on);
+
+ /* Call set_stub_non_stop if the stub's mode is not GDB's, and no
+ program is running on this connection. */
+ void update_stub_non_stop ();
+
void remote_detach_1 (struct inferior *inf, int from_tty);
char *append_resumption (char *p, char *endp,
@@ -5553,36 +5565,13 @@ remote_target::start_remote_1 (int from_tty, int extended_p)
if (gdbarch_has_global_solist (current_inferior ()->arch ()))
solib_add (NULL, from_tty, auto_solib_add);
- if (target_is_non_stop_p ())
- {
- if (m_features.packet_support (PACKET_QNonStop) != PACKET_ENABLE)
- error (_("Non-stop mode requested, but remote "
- "does not support non-stop"));
-
- putpkt ("QNonStop:1");
- getpkt (&rs->buf);
-
- if (!streq (rs->buf.data (), "OK"))
- error (_("Remote refused setting non-stop mode with: %s"),
- rs->buf.data ());
+ set_stub_non_stop (target_is_non_stop_p ());
- /* Find about threads and processes the stub is already
- controlling. We default to adding them in the running state.
- The '?' query below will then tell us about which threads are
- stopped. */
- this->update_thread_list ();
- }
- else if (m_features.packet_support (PACKET_QNonStop) == PACKET_ENABLE)
- {
- /* Don't assume that the stub can operate in all-stop mode.
- Request it explicitly. */
- putpkt ("QNonStop:0");
- getpkt (&rs->buf);
-
- if (!streq (rs->buf.data (), "OK"))
- error (_("Remote refused setting all-stop mode with: %s"),
- rs->buf.data ());
- }
+ /* Find about threads and processes the stub is already controlling.
+ We default to adding them in the running state. The '?' query
+ below will then tell us about which threads are stopped. */
+ if (target_is_non_stop_p ())
+ this->update_thread_list ();
/* Upload TSVs regardless of whether the target is running or not. The
remote stub, such as GDBserver, may have some predefined or builtin
@@ -5776,6 +5765,49 @@ remote_target::start_remote_1 (int from_tty, int extended_p)
return true;
}
+void
+remote_target::set_stub_non_stop (bool on)
+{
+ remote_state *rs = get_remote_state ();
+
+ if (m_features.packet_support (PACKET_QNonStop) != PACKET_ENABLE)
+ {
+ if (on)
+ error (_("Non-stop mode requested, but remote "
+ "does not support non-stop"));
+
+ /* Don't assume that the stub can operate in all-stop mode, but
+ if it has no other mode there is nothing to request. */
+ return;
+ }
+
+ putpkt (on ? "QNonStop:1" : "QNonStop:0");
+ getpkt (&rs->buf);
+
+ if (!streq (rs->buf.data (), "OK"))
+ error (on
+ ? _("Remote refused setting non-stop mode with: %s")
+ : _("Remote refused setting all-stop mode with: %s"),
+ rs->buf.data ());
+
+ rs->stub_non_stop = on;
+}
+
+void
+remote_target::update_stub_non_stop ()
+{
+ if (target_is_non_stop_p () == get_remote_state ()->stub_non_stop)
+ return;
+
+ /* "set non-stop" only checks the current inferior. Do not switch
+ the stub under a program that is already running on this
+ connection. */
+ if (number_of_live_inferiors (this) > 0)
+ return;
+
+ set_stub_non_stop (target_is_non_stop_p ());
+}
+
/* Start the remote connection and sync state. */
void
@@ -7002,6 +7034,9 @@ extended_remote_target::attach (const char *args, int from_tty)
if (m_features.packet_support (PACKET_vAttach) == PACKET_DISABLE)
error (_("This target does not support attaching to a process"));
+ /* "set non-stop" may have been used since connecting. */
+ update_stub_non_stop ();
+
target_announce_attach (from_tty, pid);
xsnprintf (rs->buf.data (), get_remote_packet_size (), "vAttach;%x", pid);
@@ -11451,6 +11486,9 @@ extended_remote_target::create_inferior (const char *exec_file,
std::string remote_exec_file
= get_exec_file_for_create_inferior (exec_file);
+ /* "set non-stop" may have been used since connecting. */
+ update_stub_non_stop ();
+
/* If running asynchronously, register the target file descriptor
with the event loop. */
if (target_can_async_p ())
diff --git a/gdb/testsuite/gdb.server/non-stop-after-connect.exp b/gdb/testsuite/gdb.server/non-stop-after-connect.exp
new file mode 100644
index 00000000000..d5384afcaa1
--- /dev/null
+++ b/gdb/testsuite/gdb.server/non-stop-after-connect.exp
@@ -0,0 +1,93 @@
+# 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/>.
+
+# Test changing "set non-stop" while connected with extended-remote, but
+# with no program yet. GDB has to tell the stub before it starts or
+# attaches to one, otherwise the two disagree on how to talk to each
+# other: in non-stop mode GDB rejects the stop reply of an all-stop stub,
+# and the program is left shown as running.
+
+load_lib gdbserver-support.exp
+
+standard_testfile ext-attach.c
+
+require allow_gdbserver_tests
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
+ return
+}
+
+# Connect with "set non-stop FROM", change that to TO, and then get a
+# program with HOW, which is "run" or "attach".
+proc run_test { how from to } {
+ save_vars { ::GDBFLAGS } {
+ # If GDB and GDBserver are both running locally, set the sysroot to
+ # avoid reading files via the remote protocol.
+ if { ![is_remote host] && ![is_remote target] } {
+ append ::GDBFLAGS " -ex \"set sysroot\""
+ }
+
+ clean_restart $::testfile
+ }
+
+ # Make sure we're disconnected, in case we're testing with an
+ # extended-remote board, therefore already connected.
+ gdb_test "disconnect" ".*"
+
+ gdb_test_no_output "set non-stop $from"
+
+ set target_exec [gdbserver_download_current_prog]
+ gdbserver_start_extended
+
+ gdb_test_no_output "set non-stop $to"
+
+ if { $how eq "run" } {
+ gdb_test_no_output "set remote exec-file $target_exec"
+
+ gdb_breakpoint main
+ gdb_test "run" "Breakpoint $::decimal.* main .*" "run to main"
+ } else {
+ set test_spawn_id [spawn_wait_for_attach $::binfile]
+ set testpid [spawn_id_get_pid $test_spawn_id]
+
+ gdb_test "attach $testpid" "Attaching to .* $testpid.*" "attach"
+ }
+
+ gdb_test_multiple "info threads" "thread is stopped" {
+ -re "\\(running\\)\r\n$::gdb_prompt $" {
+ fail $gdb_test_name
+ }
+ -re -wrap "\\* 1 .*" {
+ pass $gdb_test_name
+ }
+ }
+
+ gdb_test "kill" "\\\[Inferior $::decimal \\(.*\\) killed\\\]" "kill" \
+ "Kill the program being debugged\\? \\(y or n\\) $" "y"
+
+ if { $how eq "attach" } {
+ kill_wait_spawned_process $test_spawn_id
+ }
+}
+
+foreach_with_prefix how {run attach} {
+ if { $how eq "attach" && ![can_spawn_for_attach] } {
+ continue
+ }
+
+ foreach_with_prefix change {{off on} {on off}} {
+ run_test $how [lindex $change 0] [lindex $change 1]
+ }
+}
--
2.56.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-08 19:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-08 19:00 [PATCH] gdb/remote: tell the stub about "set non-stop" before run and attach Josip Seketa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox