* [PATCH 1/3] gdb: pass info_threads_opts to print_thread_info_1
2023-04-05 9:19 [PATCH 0/3] Option to show stopped threads only Tankut Baris Aktemur
@ 2023-04-05 9:20 ` Tankut Baris Aktemur
2023-04-05 9:20 ` [PATCH 2/3] gdb, doc: add the missing '-gid' option to 'info threads' Tankut Baris Aktemur
2023-04-05 9:20 ` [PATCH 3/3] gdb: add a '-stopped' option to "info threads" Tankut Baris Aktemur
2 siblings, 0 replies; 11+ messages in thread
From: Tankut Baris Aktemur @ 2023-04-05 9:20 UTC (permalink / raw)
To: gdb-patches
The "info threads" command tracks its options in a struct named
'info_threads_opts', which currently has only one option. Pass the
whole options object to helper functions, instead of passing
the option value individually. This is a refactoring to make adding
more options easier.
---
gdb/thread.c | 61 +++++++++++++++++++++++++++-------------------------
1 file changed, 32 insertions(+), 29 deletions(-)
diff --git a/gdb/thread.c b/gdb/thread.c
index 4d97ed3f2d1..57b3f7611e2 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -951,6 +951,24 @@ pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
&& pc < thread->control.step_range_end);
}
+/* The options for the "info threads" command. */
+
+struct info_threads_opts
+{
+ /* For "-gid". */
+ bool show_global_ids = false;
+};
+
+static const gdb::option::option_def info_threads_option_defs[] = {
+
+ gdb::option::flag_option_def<info_threads_opts> {
+ "gid",
+ [] (info_threads_opts *opts) { return &opts->show_global_ids; },
+ N_("Show global thread IDs."),
+ },
+
+};
+
/* Helper for print_thread_info. Returns true if THR should be
printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
NULL, only print THR if its ID is included in the list. GLOBAL_IDS
@@ -959,11 +977,13 @@ pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
is a thread from the process PID. Otherwise, threads from all
attached PIDs are printed. If both REQUESTED_THREADS is not NULL
and PID is not -1, then the thread is printed if it belongs to the
- specified process. Otherwise, an error is raised. */
+ specified process. Otherwise, an error is raised. OPTS is the
+ options of the "info threads" command. */
static bool
should_print_thread (const char *requested_threads, int default_inf_num,
- int global_ids, int pid, struct thread_info *thr)
+ int global_ids, int pid, thread_info *thr,
+ info_threads_opts opts)
{
if (requested_threads != NULL && *requested_threads != '\0')
{
@@ -1014,12 +1034,12 @@ thread_target_id_str (thread_info *tp)
/* Like print_thread_info, but in addition, GLOBAL_IDS indicates
whether REQUESTED_THREADS is a list of global or per-inferior
- thread ids. */
+ thread ids. OPTS is the options of the "info threads" command. */
static void
print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
int global_ids, int pid,
- int show_global_ids)
+ info_threads_opts opts)
{
int default_inf_num = current_inferior ()->num;
@@ -1055,7 +1075,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
for (thread_info *tp : all_threads ())
{
if (!should_print_thread (requested_threads, default_inf_num,
- global_ids, pid, tp))
+ global_ids, pid, tp, opts))
continue;
/* Switch inferiors so we're looking at the right
@@ -1079,12 +1099,12 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
return;
}
- table_emitter.emplace (uiout, show_global_ids ? 5 : 4,
+ table_emitter.emplace (uiout, opts.show_global_ids ? 5 : 4,
n_threads, "threads");
uiout->table_header (1, ui_left, "current", "");
uiout->table_header (4, ui_left, "id-in-tg", "Id");
- if (show_global_ids)
+ if (opts.show_global_ids)
uiout->table_header (4, ui_left, "id", "GId");
uiout->table_header (target_id_col_width, ui_left,
"target-id", "Target Id");
@@ -1102,7 +1122,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
current_exited = true;
if (!should_print_thread (requested_threads, default_inf_num,
- global_ids, pid, tp))
+ global_ids, pid, tp, opts))
continue;
ui_out_emit_tuple tuple_emitter (uiout, NULL);
@@ -1117,7 +1137,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
uiout->field_string ("id-in-tg", print_thread_id (tp));
}
- if (show_global_ids || uiout->is_mi_like_p ())
+ if (opts.show_global_ids || uiout->is_mi_like_p ())
uiout->field_signed ("id", tp->global_num);
/* Switch to the thread (and inferior / target). */
@@ -1199,27 +1219,10 @@ void
print_thread_info (struct ui_out *uiout, const char *requested_threads,
int pid)
{
- print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
+ info_threads_opts opts {false};
+ print_thread_info_1 (uiout, requested_threads, 1, pid, opts);
}
-/* The options for the "info threads" command. */
-
-struct info_threads_opts
-{
- /* For "-gid". */
- bool show_global_ids = false;
-};
-
-static const gdb::option::option_def info_threads_option_defs[] = {
-
- gdb::option::flag_option_def<info_threads_opts> {
- "gid",
- [] (info_threads_opts *opts) { return &opts->show_global_ids; },
- N_("Show global thread IDs."),
- },
-
-};
-
/* Create an option_def_group for the "info threads" options, with
IT_OPTS as context. */
@@ -1244,7 +1247,7 @@ info_threads_command (const char *arg, int from_tty)
gdb::option::process_options
(&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
- print_thread_info_1 (current_uiout, arg, 0, -1, it_opts.show_global_ids);
+ print_thread_info_1 (current_uiout, arg, 0, -1, it_opts);
}
/* Completer for the "info threads" command. */
--
2.25.1
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 3/3] gdb: add a '-stopped' option to "info threads"
2023-04-05 9:19 [PATCH 0/3] Option to show stopped threads only Tankut Baris Aktemur
2023-04-05 9:20 ` [PATCH 1/3] gdb: pass info_threads_opts to print_thread_info_1 Tankut Baris Aktemur
2023-04-05 9:20 ` [PATCH 2/3] gdb, doc: add the missing '-gid' option to 'info threads' Tankut Baris Aktemur
@ 2023-04-05 9:20 ` Tankut Baris Aktemur
2023-04-05 10:00 ` Eli Zaretskii
2 siblings, 1 reply; 11+ messages in thread
From: Tankut Baris Aktemur @ 2023-04-05 9:20 UTC (permalink / raw)
To: gdb-patches
Add a '-stopped' option to the "info threads" command to print stopped
threads only and skip the running ones. This is a convenience flag to
filter out the running threads in programs that have many threads.
Suppose we have an application with 5 threads, 2 of which have hit a
breakpoint. The "info threads" command in the non-stop mode gives:
(gdb) info threads
Id Target Id Frame
* 1 Thread 0x7ffff7d99740 (running)
2 Thread 0x7ffff7d98700 something () at file.c:30
3 Thread 0x7ffff7597700 (running)
4 Thread 0x7ffff6d96700 something () at file.c:30
5 Thread 0x7ffff6595700 (running)
(gdb)
Using the "-stopped" flag, we get
(gdb) info threads -stopped
Id Target Id Frame
2 Thread 0x7ffff7d98700 something () at file.c:30
4 Thread 0x7ffff6d96700 something () at file.c:30
(gdb)
When combined with a thread ID, the behavior is as follows:
(gdb) info threads 3
Id Target Id Frame
3 Thread 0x7ffff7597700 (running)
(gdb) info threads -stopped 3
No stopped threads match '3'.
(gdb)
Regression-tested on X86_64 Linux.
---
gdb/NEWS | 7 ++
gdb/doc/gdb.texinfo | 6 +-
gdb/testsuite/gdb.base/options.exp | 11 ++-
.../gdb.threads/info-threads-stopped.c | 78 +++++++++++++++++++
.../gdb.threads/info-threads-stopped.exp | 75 ++++++++++++++++++
gdb/thread.c | 17 +++-
6 files changed, 190 insertions(+), 4 deletions(-)
create mode 100644 gdb/testsuite/gdb.threads/info-threads-stopped.c
create mode 100644 gdb/testsuite/gdb.threads/info-threads-stopped.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index 10a1a70fa52..33b104fda72 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -96,6 +96,13 @@ info main
$2 = 1
(gdb) break func if $_shell("some command") == 0
+* Changed commands
+
+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.
+
* MI changes
** mi now reports 'no-history' as a stop reason when hitting the end of the
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index ed14888b77b..02f6a9eb49a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3678,7 +3678,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
@@ -3728,6 +3728,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:
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 6d90615710e..58c85567a4e 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -492,12 +492,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"
diff --git a/gdb/testsuite/gdb.threads/info-threads-stopped.c b/gdb/testsuite/gdb.threads/info-threads-stopped.c
new file mode 100644
index 00000000000..42ca8cf0b69
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/info-threads-stopped.c
@@ -0,0 +1,78 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2022-2023 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;
+}
diff --git a/gdb/testsuite/gdb.threads/info-threads-stopped.exp b/gdb/testsuite/gdb.threads/info-threads-stopped.exp
new file mode 100644
index 00000000000..5643a513aff
--- /dev/null
+++ b/gdb/testsuite/gdb.threads/info-threads-stopped.exp
@@ -0,0 +1,75 @@
+# Copyright (C) 2022-2023 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"
+
+# Count the number of running/stopped threads reported
+# by the "info threads" command.
+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
+ exp_continue
+ }
+ -re "^\r\n. ${decimal}${fill}Thread ${fill}something ${fill}" {
+ incr num_stopped
+ 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"
+ }
+ }
+ }
+}
diff --git a/gdb/thread.c b/gdb/thread.c
index 57b3f7611e2..8886bd45674 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -957,6 +957,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[] = {
@@ -966,6 +968,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."),
+ },
};
@@ -1008,6 +1015,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;
}
@@ -1094,7 +1106,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;
}
@@ -1219,7 +1232,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);
}
--
2.25.1
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 11+ messages in thread