@@ -56,6 +56,11 @@ info sharedlibrary
command are now for the full memory range allocated to the shared
library.
+info threads [-gid] [-stopped] [ID]...
+ This command now takes an optional flag, '-stopped', that causes only
+ the stopped threads to be printed. The flag can be useful to get a
+ reduced list when there is a large number of unstopped threads.
+
* Python API
** New class gdb.Color for dealing with colors.
@@ -3807,7 +3807,7 @@ Thread 1 "main" received signal SIGINT, Interrupt.
@table @code
@anchor{info_threads}
@kindex info threads
-@item info threads @r{[}-gid@r{]} @r{[}@var{thread-id-list}@r{]}
+@item info threads @r{[}-gid@r{]} @r{[}-stopped@r{]} @r{[}@var{thread-id-list}@r{]}
Display information about one or more threads. With no arguments
displays information about all threads. You can specify the list of
@@ -3857,6 +3857,10 @@ If you're debugging multiple inferiors, @value{GDBN} displays thread
IDs using the qualified @var{inferior-num}.@var{thread-num} format.
Otherwise, only @var{thread-num} is shown.
+If you specify the @samp{-stopped} option, @value{GDBN} displays the
+stopped threads only. This can be helpful to reduce the output list
+if there is a large number of unstopped threads.
+
If you specify the @samp{-gid} option, @value{GDBN} displays a column
indicating each thread's global thread ID:
@@ -509,12 +509,21 @@ proc_with_prefix test-thread-apply {} {
proc_with_prefix test-info-threads {} {
test_gdb_complete_multiple "info threads " "" "" {
"-gid"
+ "-stopped"
"ID"
}
+ test_gdb_complete_multiple "info threads " "-" "" {
+ "-gid"
+ "-stopped"
+ }
+
test_gdb_complete_unique \
- "info threads -" \
+ "info threads -g" \
"info threads -gid"
+ test_gdb_complete_unique \
+ "info threads -s" \
+ "info threads -stopped"
# "ID" isn't really something the user can type.
test_gdb_complete_none "info threads I"
new file mode 100644
@@ -0,0 +1,78 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2022-2025 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 <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#define NUM 4
+
+volatile int should_spin = 1;
+
+static void
+something ()
+{
+}
+
+static void
+spin ()
+{
+ while (should_spin)
+ usleep (1);
+}
+
+static void *
+work (void *arg)
+{
+ int id = *((int *) arg);
+
+ /* Sleep a bit to give the other threads a chance to run. */
+ usleep (1);
+
+ if (id % 2 == 0)
+ something (); /* break-here */
+ else
+ spin ();
+
+ pthread_exit (NULL);
+}
+
+int
+main ()
+{
+ /* Ensure we stop if GDB crashes and DejaGNU fails to kill us. */
+ alarm (10);
+
+ pthread_t threads[NUM];
+ void *thread_result;
+ int ids[NUM];
+
+ for (int i = 0; i < NUM; i++)
+ {
+ ids[i] = i + 2;
+ pthread_create (&threads[i], NULL, work, &(ids[i]));
+ }
+
+ sleep (10);
+ should_spin = 0;
+
+ for (int i = 0; i < NUM; i++)
+ pthread_join(threads[i], &thread_result);
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,107 @@
+# Copyright (C) 2022-2025 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 for the '-stopped' flag of the "info threads" command.
+
+standard_testfile
+
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
+ executable debug] != "" } {
+ return -1
+}
+
+save_vars { GDBFLAGS } {
+ append GDBFLAGS " -ex \"set non-stop on\""
+ clean_restart $binfile
+}
+
+gdb_breakpoint "something"
+gdb_run_cmd
+
+# Two threads hit the bp.
+set fill "\[^\r\n\]+"
+set num_hits 0
+gdb_test_multiple "" "hit the breakpoint" -lbl {
+ -re "\r\nThread ${fill} hit Breakpoint 1${fill}" {
+ incr num_hits
+ if {$num_hits < 2} {
+ exp_continue
+ }
+ }
+ -re "\r\n$gdb_prompt " {
+ exp_continue
+ }
+}
+gdb_assert {$num_hits == 2} "two threads hit the bp"
+
+# We are in non-stop mode.
+# Send a simple command to resync the command prompt.
+gdb_test "p 42" " = 42"
+
+# Count the number of running/stopped threads reported
+# by the "info threads" command. We also capture thread ids
+# for additional tests.
+set running_tid "invalid"
+set stopped_tid "invalid"
+
+foreach flag {"" "-stopped"} {
+ set num_running 0
+ set num_stopped 0
+ gdb_test_multiple "info threads $flag" "info threads $flag" {
+ -re "Id${fill}Target Id${fill}Frame${fill}" {
+ exp_continue
+ }
+ -re "^\r\n. (${decimal})${fill}Thread ${fill}.running." {
+ incr num_running
+ set running_tid $expect_out(1,string)
+ exp_continue
+ }
+ -re "^\r\n. (${decimal})${fill}Thread ${fill}something ${fill}${srcfile}:${decimal}" {
+ incr num_stopped
+ set stopped_tid $expect_out(1,string)
+ exp_continue
+ }
+ -re "^\r\n$gdb_prompt $" {
+ gdb_assert {$num_stopped == 2} "$gdb_test_name: num stopped"
+ if {$flag eq ""} {
+ gdb_assert {$num_running == 3} "$gdb_test_name: num running"
+ } else {
+ gdb_assert {$num_running == 0} "$gdb_test_name: num running"
+ }
+ }
+ }
+}
+
+gdb_assert {$running_tid != "invalid"} "found a running thread"
+gdb_assert {$stopped_tid != "invalid"} "found a stopped thread"
+
+# Test specifying thread ids.
+gdb_test "info threads -stopped $running_tid" \
+ "No stopped threads match '$running_tid'\." \
+ "info thread -stopped for a running thread"
+
+set fill "\[^\r\n\]+"
+set ws "\[ \t\]+"
+gdb_test "info threads -stopped $stopped_tid" \
+ [multi_line \
+ "${ws}Id${ws}Target Id${ws}Frame${ws}" \
+ "${ws}${stopped_tid}${ws}Thread ${fill} something ${fill}"] \
+ "info thread -stopped for a stopped thread"
+
+gdb_test "info threads -stopped $running_tid $stopped_tid" \
+ [multi_line \
+ "${ws}Id${ws}Target Id${ws}Frame${ws}" \
+ "${ws}${stopped_tid}${ws}Thread ${fill} something ${fill}"] \
+ "info thread -stopped for a running and a stopped thread"
@@ -1044,6 +1044,8 @@ struct info_threads_opts
{
/* For "-gid". */
bool show_global_ids = false;
+ /* For "-stopped". */
+ bool show_stopped_threads = false;
};
static const gdb::option::option_def info_threads_option_defs[] = {
@@ -1053,6 +1055,11 @@ static const gdb::option::option_def info_threads_option_defs[] = {
[] (info_threads_opts *opts) { return &opts->show_global_ids; },
N_("Show global thread IDs."),
},
+ gdb::option::flag_option_def<info_threads_opts> {
+ "stopped",
+ [] (info_threads_opts *opts) { return &opts->show_stopped_threads; },
+ N_("Show stopped threads only."),
+ },
};
@@ -1095,6 +1102,11 @@ should_print_thread (const char *requested_threads, int default_inf_num,
if (thr->state == THREAD_EXITED)
return false;
+ /* Skip a running thread if the user wants stopped threads only. */
+ bool is_stopped = (thr->state == THREAD_STOPPED);
+ if (opts.show_stopped_threads && !is_stopped)
+ return false;
+
return true;
}
@@ -1286,7 +1298,8 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
if (requested_threads == NULL || *requested_threads == '\0')
uiout->message (_("No threads.\n"));
else
- uiout->message (_("No threads match '%s'.\n"),
+ uiout->message (_("No %sthreads match '%s'.\n"),
+ (opts.show_stopped_threads ? "stopped " : ""),
requested_threads);
return;
}
@@ -1342,7 +1355,7 @@ void
print_thread_info (struct ui_out *uiout, const char *requested_threads,
int pid)
{
- info_threads_opts opts {false};
+ info_threads_opts opts {false, false};
print_thread_info_1 (uiout, requested_threads, 1, pid, opts);
}