[1/2] gdb: fix assertion when interrupting an empty remote target

Message ID 20260805085919.218925-2-stephan.rohr@intel.com
State New
Headers
Series gdb: improve interrupting an empty remote target |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Stephan Rohr Aug. 5, 2026, 8:59 a.m. UTC
  From: "Rohr, Stephan" <stephan.rohr@intel.com>

GDB asserts when sending an interrupt to an empty remote target, e.g.
use stdio to connect to a remote target in non-stop mode:

  'gdb -ex "set non-stop on" -ex "target extended-remote | gdbserver
   --multi --once -'

Interrupting the target triggers an assertion in 'find_inferior_pid'.
Fix by checking for a valid thread in 'interrupt_target_1' for 'non-stop'
mode.
---
 gdb/infcmd.c                                  |  5 ++-
 .../interrupt-empty-remote-target.exp         | 40 +++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.server/interrupt-empty-remote-target.exp
  

Patch

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 3e943123519..512ecde4879 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -3133,7 +3133,10 @@  interrupt_target_1 (bool all_threads)
 	    }
 	}
       else
-	stop_current_target_threads_ns (inferior_ptid);
+	{
+	  ensure_valid_thread ();
+	  stop_current_target_threads_ns (inferior_ptid);
+	}
     }
   else
     target_interrupt ();
diff --git a/gdb/testsuite/gdb.server/interrupt-empty-remote-target.exp b/gdb/testsuite/gdb.server/interrupt-empty-remote-target.exp
new file mode 100644
index 00000000000..34437a17d90
--- /dev/null
+++ b/gdb/testsuite/gdb.server/interrupt-empty-remote-target.exp
@@ -0,0 +1,40 @@ 
+# Copyright 2024 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/>.
+
+# Tests that GDB does not assert if interrupting an empty remote target
+# in non-stop mode.
+
+load_lib gdbserver-support.exp
+
+require allow_gdbserver_tests
+
+standard_testfile server.c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+clean_restart ${testfile}
+
+# Make sure we're disconnected, in case we're testing with an
+# extended-remote board, therefore already connected.
+gdb_test "disconnect" ".*"
+
+gdbserver_start_extended "--once"
+
+gdb_test_no_output "set non-stop on"
+
+gdb_test "interrupt" \
+    "Cannot execute this command without a live selected thread."