This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFA] Add optional argument to "info threads" command
On Monday 24 November 2008 21:53:25, Michael Snyder wrote:
> > Isn't this the same reasoning behind having thread_apply_command
> > and thread_apply_all implementations, instead of having one call
> > into the other?
>
> OK -- are you suggesting to abstract print_thread_info out
> into two separate functions?
That, or make print_thread_info itself take a range, something
like the attached. If MI wants to be able to specify a range,
we could make print_thread_info itself that a char* and do the
range parsing there.
There's still always both a prune_threads and target_find_new_threads
call, but with that I can live.
We *could* be smarter about that too. Only prune threads in
the passed range iff we're specifying a range (all otherwise); and,
only find new threads if any of the range ends is higher
than the highest id known.
--
Pedro Alves
---
gdb/thread.c | 135 ++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 87 insertions(+), 48 deletions(-)
Index: src/gdb/thread.c
===================================================================
--- src.orig/gdb/thread.c 2008-11-24 22:08:50.000000000 +0000
+++ src/gdb/thread.c 2008-11-24 23:05:22.000000000 +0000
@@ -631,19 +631,22 @@ set_stop_requested (ptid_t ptid, int sto
observer_notify_thread_stop_requested (ptid);
}
-/* Prints the list of threads and their details on UIOUT.
- This is a version of 'info_thread_command' suitable for
- use from MI.
- If REQUESTED_THREAD is not -1, it's the GDB id of the thread
- that should be printed. Otherwise, all threads are
- printed.
- If PID is not -1, only print threads from the process PID.
- Otherwise, threads from all attached PIDs are printed.
- If both REQUESTED_THREAD and PID are not -1, then the thread
- is printed if it belongs to the specified process. Otherwise,
- an error is raised. */
-void
-print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
+/* Prints the list of threads whose id falls in the range specified by
+ RANGE_START and RANGE_END (inclusive) and their details on UIOUT.
+
+ If RANGE_START is -1, all threads are printed.
+
+ If PID is not -1, only print threads from the process PID (target
+ id, not GDB inferior number).
+
+ Otherwise, threads from all attached PIDs are printed. If both
+ RANGE_START and PID are not -1, and RANGE_START equal RANGE_END,
+ then the RANGE_START thread is printed if it belongs to the
+ specified process; otherwise, an error is raised. */
+
+static void
+print_thread_info_1 (struct ui_out *uiout,
+ int range_start, int range_end, int pid)
{
struct thread_info *tp;
ptid_t current_ptid;
@@ -663,12 +666,9 @@ print_thread_info (struct ui_out *uiout,
{
struct cleanup *chain2;
- if (requested_thread != -1 && tp->num != requested_thread)
- continue;
-
if (pid != -1 && PIDGET (tp->ptid) != pid)
{
- if (requested_thread != -1)
+ if (range_start != -1 && range_start == range_end)
error (_("Requested thread not found in requested process"));
continue;
}
@@ -676,6 +676,10 @@ print_thread_info (struct ui_out *uiout,
if (ptid_equal (tp->ptid, current_ptid))
current_thread = tp->num;
+ if (range_start != -1
+ && (tp->num < range_start || range_end < tp->num))
+ continue;
+
if (tp->state_ == THREAD_EXITED)
continue;
@@ -700,7 +704,12 @@ print_thread_info (struct ui_out *uiout,
ui_out_text (uiout, " ");
if (tp->state_ == THREAD_RUNNING)
- ui_out_text (uiout, "(running)\n");
+ {
+ ui_out_text (uiout, "(running)\n");
+
+ if (ui_out_is_mi_like_p (uiout))
+ ui_out_field_string (uiout, "state", "running");
+ }
else
{
/* The switch below puts us at the top of the stack (leaf
@@ -710,14 +719,9 @@ print_thread_info (struct ui_out *uiout,
/* For MI output, print frame level. */
ui_out_is_mi_like_p (uiout),
LOCATION);
- }
- if (ui_out_is_mi_like_p (uiout))
- {
- char *state = "stopped";
- if (tp->state_ == THREAD_RUNNING)
- state = "running";
- ui_out_field_string (uiout, "state", state);
+ if (ui_out_is_mi_like_p (uiout))
+ ui_out_field_string (uiout, "state", "stopped");
}
do_cleanups (chain2);
@@ -727,7 +731,7 @@ print_thread_info (struct ui_out *uiout,
the "info threads" command. */
do_cleanups (old_chain);
- if (pid == -1 && requested_thread == -1 )
+ if (pid == -1 && range_start == -1)
{
gdb_assert (current_thread != -1
|| !thread_list);
@@ -741,6 +745,56 @@ The current thread <Thread ID %d> has te
}
}
+/* Prints the list of threads and their details on UIOUT. This is a
+ version of 'info_thread_command' suitable for use from MI.
+
+ If REQUESTED_THREAD is not -1, it's the GDB id of the thread that
+ should be printed. Otherwise, all threads are printed.
+
+ If PID is not -1, only print threads from the process PID.
+ Otherwise, threads from all attached PIDs are printed. If both
+ REQUESTED_THREAD and PID are not -1, then the thread is printed if
+ it belongs to the specified process. Otherwise, an error is
+ raised. */
+
+void
+print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
+{
+ print_thread_info_1 (uiout, requested_thread, requested_thread, pid);
+}
+
+/* Parse a thread id or thread id range specified in TIDLIST, and
+ store it into START and END. */
+
+static char *
+parse_thread_id_or_range (char *tidlist, int *start, int *end)
+{
+ char *p;
+
+ *start = strtol (tidlist, &p, 10);
+ if (p == tidlist)
+ error (_("Error parsing %s"), tidlist);
+ tidlist = p;
+
+ while (*tidlist == ' ' || *tidlist == '\t')
+ tidlist++;
+
+ if (*tidlist == '-') /* Got a range of IDs? */
+ {
+ tidlist++; /* Skip the - */
+ *end = strtol (tidlist, &p, 10);
+ if (p == tidlist)
+ error (_("Error parsing %s"), tidlist);
+ tidlist = p;
+
+ while (*tidlist == ' ' || *tidlist == '\t')
+ tidlist++;
+ }
+ else
+ *end = *start;
+
+ return tidlist;
+}
/* Print information about currently known threads
@@ -752,7 +806,12 @@ The current thread <Thread ID %d> has te
static void
info_threads_command (char *arg, int from_tty)
{
- print_thread_info (uiout, -1, -1);
+ int start = -1, end = -1;
+
+ if (arg && *arg)
+ parse_thread_range (arg, &start, &end);
+
+ print_thread_info_1 (uiout, start, end, -1);
}
/* Switch from one thread to another. */
@@ -971,27 +1030,7 @@ thread_apply_command (char *tidlist, int
struct thread_info *tp;
int start, end;
- start = strtol (tidlist, &p, 10);
- if (p == tidlist)
- error (_("Error parsing %s"), tidlist);
- tidlist = p;
-
- while (*tidlist == ' ' || *tidlist == '\t')
- tidlist++;
-
- if (*tidlist == '-') /* Got a range of IDs? */
- {
- tidlist++; /* Skip the - */
- end = strtol (tidlist, &p, 10);
- if (p == tidlist)
- error (_("Error parsing %s"), tidlist);
- tidlist = p;
-
- while (*tidlist == ' ' || *tidlist == '\t')
- tidlist++;
- }
- else
- end = start;
+ tidlist = parse_thread_range (tidlist, &start, &end);
make_cleanup_restore_current_thread ();