[rfc] 'thread tid' command

Michael Snyder
2011-02-11T22:12:47Z

[-- Attachment #1: Type: text/plain, Size: 683 bytes --]

This is purposefully rough, 'cause I hope for discussion.

This is a new command to help manage large thread lists.
I started with the idea "I want to find out which thread
has target id 12345", then extended it to also handle the
new concept of thread names (which thread has name 'foo'),
and extra info as well (which thread has extra info that
includes the string "xyz").

The syntax (I'm open to renaming etc.):

thread tid [NAME | TARGET_ID | EXTRA_INFO]

The output:

Thread %d has name '%s' or
Thread %d has target id '%s' or
Thread %d has extra info '%s'

The user can then use the given thread id as input to the
thread command, info threads, etc.

Comments?

[-- Attachment #2: tid.txt --]
[-- Type: text/plain, Size: 1966 bytes --]

Index: thread.c
===================================================================
RCS file: /cvs/src/src/gdb/thread.c,v
retrieving revision 1.131
diff -u -p -u -p -r1.131 thread.c
--- thread.c 19 Jan 2011 17:21:36 -0000 1.131
+++ thread.c 11 Feb 2011 22:03:31 -0000
@@ -1313,6 +1323,34 @@ thread_name_command (char *arg, int from
info->name = arg ? xstrdup (arg) : NULL;
}

+/* Show thread ids with a name, target pid, or extra info matching ARG. */
+
+static void
+tid_command (char *arg, int from_tty)
+{
+ struct thread_info *tp;
+ char *tmp;
+
+ if (arg == NULL || *arg == '\0')
+ error (_("Command requires an argument."));
+
+ update_thread_list ();
+
+ for (tp = thread_list; tp; tp = tp->next)
+ {
+ if (tp->name != NULL && strcmp (arg, tp->name) == 0)
+ printf_filtered ("Thread %d has name '%s'\n", tp->num, tp->name);
+
+ tmp = target_pid_to_str (tp->ptid);
+ if (tmp != NULL && strstr (tmp, arg) != 0)
+ printf_filtered ("Thread %d has target id '%s'\n", tp->num, tmp);
+
+ tmp = target_extra_thread_info (tp);
+ if (tmp != NULL && strstr (tmp, arg) != 0)
+ printf_filtered ("Thread %d has extra info '%s'\n", tp->num, tmp);
+ }
+}
+
/* Print notices when new threads are attached and detached. */
int print_thread_events = 1;
static void
@@ -1418,11 +1460,15 @@ The new thread ID must be currently know
add_cmd ("all", class_run, thread_apply_all_command,
_("Apply a command to all threads."), &thread_apply_list);

- add_cmd ("name", class_run, thread_name_command,
+ add_cmd ("name", no_class, thread_name_command,
_("Set the current thread's name.\n\
Usage: thread name [NAME]\n\
If NAME is not given, then any existing name is removed."), &thread_cmd_list);

+ add_cmd ("tid", no_class, tid_command, _("\
+Show thread ids with a name, target pid, or extra info matching ARG."),
+ &thread_cmd_list);
+
if (!xdb_commands)
add_com_alias ("t", "thread", class_run, 1);