On 06/05/2018 09:49 PM, Philippe Waroquiers wrote:
> Enhance 'thread apply' command to also accepts FLAGS... arguments.
>
> Some examples usages for this new argument:
> thread apply all -s frame apply all -s p some_local_var_somewhere
> Prints the thread id, frame location and some_local_var_somewhere
> value in frames of threads that have such local var.
>
> To make the life of the user easier, the most typical use cases
> have shortcuts :
> taas : shortcut for 'thread apply all -s'
> tfaas : shortcut for 'thread apply all -s frame apply all -s"
>
> An example usage :
> tfaas p some_local_var_somewhere
> same as the longer:
> 'thread apply all -s frame apply all -s p some_local_var_somewhere'
Same comments on -q/-s and [FLAGS...]/[OPTION...] etc. also apply here.
> separated list of numbers, or ranges, or the keyword `all'. Ranges consist
> of two numbers separated by a hyphen. Examples:
> @@ -1592,6 +1631,10 @@ tp_array_compar (const thread_info *a, const thread_info *b)
> static void
> thread_apply_all_command (const char *cmd, int from_tty)
> {
> + int print_what_v = 1; /* Print thread id/thread/lwp. */
> + bool cont;
> + bool silent;
> +
> tp_array_compar_ascending = false;
> if (cmd != NULL
> && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
> @@ -1600,8 +1643,13 @@ thread_apply_all_command (const char *cmd, int from_tty)
> tp_array_compar_ascending = true;
> }
>
> + if (cmd != NULL)
> + check_for_flags_vqcs ("thread apply all", &cmd,
> + &print_what_v, 1,
> + &cont, &silent);
So here we see a case where there's really no good
reason this shouldn't work:
(gdb) thread apply all -q -ascending -c p 1
thread apply all only accepts flags vqcs given individually
>
> +
> +/* Implementation of the "taas" command. */
> +
> +static void
> +taas_command (const char *cmd, int from_tty)
> +{
> + std::string expanded = std::string ("thread apply all -s ") + std::string (cmd);
> + execute_command (expanded.c_str (), from_tty);
> +}
> +
> +/* Implementation of the "tfaas" command. */
> +
> +static void
> +tfaas_command (const char *cmd, int from_tty)
> +{
> + std::string expanded = std::string ("thread apply all -s frame apply all -s ") + std::string (cmd);
> + execute_command (expanded.c_str (), from_tty);
Overly long lines above.
Thanks,
Pedro Alves
@@ -1581,6 +1581,45 @@ tp_array_compar (const thread_info *a, const thread_info *b)
return (a->per_inf_num > b->per_inf_num);
}
+/* Switch to thread THR and executes CMD.
+ PRINT_WHAT_V verbosity controls the printing of the thread information.
+ CONT and SILENT control how to handle errors. */
+
+static void
+thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
+ int print_what_v,
+ bool cont, bool silent)
+{
+ switch_to_thread (thr->ptid);
+ TRY
+ {
+ std::string cmd_result = execute_command_to_string (cmd, from_tty);
+ if (!silent || cmd_result.length () > 0)
+ {
+ if (print_what_v > 0)
+ printf_filtered (_("\nThread %s (%s):\n"),
+ print_thread_id (thr),
+ target_pid_to_str (inferior_ptid));
+ printf_filtered ("%s", cmd_result.c_str ());
+ }
+ }
+ CATCH (ex, RETURN_MASK_ERROR)
+ {
+ if (!silent)
+ {
+ if (print_what_v > 0)
+ printf_filtered (_("\nThread %s (%s):\n"),
+ print_thread_id (thr),
+ target_pid_to_str (inferior_ptid));
+ if (cont)
+ printf_filtered ("%s\n", ex.message);
+ else
+ throw_exception (ex);
+ }
+ }
+ END_CATCH;
+}
+
/* Apply a GDB command to a list of threads. List syntax is a whitespace
separated list of numbers, or ranges, or the keyword `all'. Ranges consist
of two numbers separated by a hyphen. Examples:
@@ -1592,6 +1631,10 @@ tp_array_compar (const thread_info *a, const thread_info *b)
static void
thread_apply_all_command (const char *cmd, int from_tty)
{
+ int print_what_v = 1; /* Print thread id/thread/lwp. */
+ bool cont;
+ bool silent;
+
tp_array_compar_ascending = false;
if (cmd != NULL
&& check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
@@ -1600,8 +1643,13 @@ thread_apply_all_command (const char *cmd, int from_tty)
tp_array_compar_ascending = true;
}
+ if (cmd != NULL)
+ check_for_flags_vqcs ("thread apply all", &cmd,
+ &print_what_v, 1,
+ &cont, &silent);
+
if (cmd == NULL || *cmd == '\000')
- error (_("Please specify a command following the thread ID list"));
+ error (_("Please specify a command at the end of 'thread apply all'"));
update_thread_list ();
@@ -1637,14 +1685,9 @@ thread_apply_all_command (const char *cmd, int from_tty)
for (thread_info *thr : thr_list_cpy)
if (thread_alive (thr))
- {
- switch_to_thread (thr->ptid);
- printf_filtered (_("\nThread %s (%s):\n"),
- print_thread_id (thr),
- target_pid_to_str (inferior_ptid));
-
- execute_command (cmd, from_tty);
- }
+ thr_try_catch_cmd (thr, cmd, from_tty,
+ print_what_v,
+ cont, silent);
}
}
@@ -1653,7 +1696,11 @@ thread_apply_all_command (const char *cmd, int from_tty)
static void
thread_apply_command (const char *tidlist, int from_tty)
{
+ int print_what_v = 1; /* Print thread id/thread/lwp. */
+ bool cont;
+ bool silent;
const char *cmd = NULL;
+ const char *cmd_or_flags;
tid_range_parser parser;
if (tidlist == NULL || *tidlist == '\000')
@@ -1671,6 +1718,12 @@ thread_apply_command (const char *tidlist, int from_tty)
}
}
+ cmd_or_flags = cmd;
+ if (cmd != NULL)
+ check_for_flags_vqcs ("thread apply", &cmd,
+ &print_what_v, 1,
+ &cont, &silent);
+
if (cmd == NULL)
error (_("Please specify a command following the thread ID list"));
@@ -1680,7 +1733,7 @@ thread_apply_command (const char *tidlist, int from_tty)
scoped_restore_current_thread restore_thread;
parser.init (tidlist, current_inferior ()->num);
- while (!parser.finished () && parser.cur_tok () < cmd)
+ while (!parser.finished () && parser.cur_tok () < cmd_or_flags)
{
struct thread_info *tp = NULL;
struct inferior *inf;
@@ -1725,14 +1778,31 @@ thread_apply_command (const char *tidlist, int from_tty)
continue;
}
- switch_to_thread (tp->ptid);
-
- printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp),
- target_pid_to_str (inferior_ptid));
- execute_command (cmd, from_tty);
+ thr_try_catch_cmd (tp, cmd, from_tty,
+ print_what_v,
+ cont, silent);
}
}
+
+/* Implementation of the "taas" command. */
+
+static void
+taas_command (const char *cmd, int from_tty)
+{
+ std::string expanded = std::string ("thread apply all -s ") + std::string (cmd);
+ execute_command (expanded.c_str (), from_tty);
+}
+
+/* Implementation of the "tfaas" command. */
+
+static void
+tfaas_command (const char *cmd, int from_tty)
+{
+ std::string expanded = std::string ("thread apply all -s frame apply all -s ") + std::string (cmd);
+ execute_command (expanded.c_str (), from_tty);
+}
+
/* Switch to the specified thread, or print the current thread. */
void
@@ -2027,22 +2097,44 @@ Use this command to switch between threads.\n\
The new thread ID must be currently known."),
&thread_cmd_list, "thread ", 1, &cmdlist);
+#define THREAD_APPLY_FLAGS_HELP "\
+FLAGS are -v (increase verbosity), -q (decrease verbosity)\n\
+ -c (continue), -s (silent).\n\
+Verbosity (default 1) controls what to print for a thread:\n\
+ 0 : no thread info is printed\n\
+ 1 : print per-inferior thread number and target system's thread id\n\
+By default, if a COMMAND raises an error, thread apply is aborted.\n\
+Flag -c indicates to print the error and continue.\n\
+Flag -s indicates to silently ignore a COMMAND that raises an error\n\
+or produces no output."
+
add_prefix_cmd ("apply", class_run, thread_apply_command,
_("Apply a command to a list of threads.\n\
-Usage: thread apply ID... COMMAND\n\
-ID is a space-separated list of IDs of threads to apply COMMAND on."),
+Usage: thread apply ID... [FLAGS...] COMMAND\n\
+ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
+THREAD_APPLY_FLAGS_HELP),
&thread_apply_list, "thread apply ", 1, &thread_cmd_list);
add_cmd ("all", class_run, thread_apply_all_command,
_("\
Apply a command to all threads.\n\
\n\
-Usage: thread apply all [-ascending] COMMAND\n\
+Usage: thread apply all [-ascending] [FLAGS...] COMMAND\n\
-ascending: Call COMMAND for all threads in ascending order.\n\
- The default is descending order.\
-"),
+ The default is descending order.\n"
+THREAD_APPLY_FLAGS_HELP),
&thread_apply_list);
+ add_com ("taas", class_run, taas_command, _("\
+Apply a command to all threads (ignoring errors and empty output).\n\
+Usage: taas COMMAND\n\
+shortcut for 'thread apply all -s COMMAND'"));
+
+ add_com ("tfaas", class_run, tfaas_command, _("\
+Apply a command to all frames of all threads (ignoring errors and empty output).\n\
+Usage: tfaas COMMAND\n\
+shortcut for 'thread apply all -s frame apply all -s COMMAND'"));
+
add_cmd ("name", class_run, thread_name_command,
_("Set the current thread's name.\n\
Usage: thread name [NAME]\n\