+static bool
+tp_array_compar_ascending (const thread_info *a, const thread_info *b)
+{
+ if (a->inf->num != b->inf->num)
+ return a->inf->num < b->inf->num;
+
+ return (a->per_inf_num < b->per_inf_num);
+}
+
+/* Sort an array of struct thread_info pointers by thread ID (first by
+ inferior number, and then by per-inferior thread number). Sorts in
+ descending order. */
+
+static bool
+tp_array_compar_descending (const thread_info *a, const thread_info *b)
+{
+ if (a->inf->num != b->inf->num)
+ return a->inf->num > b->inf->num;
+
+ return (a->per_inf_num > b->per_inf_num);
+}
+
+/* Switch to thread THR and execute CMD.
+ FLAGS.QUIET controls the printing of the thread information.
+ FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
+
+static void
+thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
+ const qcs_flags &flags)
+{
+ switch_to_thread (thr);
+
+ /* The thread header is computed before running the command since
+ the command can change the inferior, which is not permitted
+ by thread_target_id_str. */
+ std::string thr_header =
+ string_printf (_("\nThread %s (%s):\n"), print_thread_id (thr),
+ thread_target_id_str (thr).c_str ());
+
+ try
+ {
+ std::string cmd_result = execute_command_to_string
+ (cmd, from_tty, gdb_stdout->term_out ());
+ if (!flags.silent || cmd_result.length () > 0)
+ {
+ if (!flags.quiet)
+ printf_filtered ("%s", thr_header.c_str ());
+ printf_filtered ("%s", cmd_result.c_str ());
+ }
+ }
+ catch (const gdb_exception_error &ex)
+ {
+ if (!flags.silent)
+ {
+ if (!flags.quiet)
+ printf_filtered ("%s", thr_header.c_str ());
+ if (flags.cont)
+ printf_filtered ("%s\n", ex.what ());
+ else
+ throw;
+ }
+ }
+}
+
+/* Option definition of "thread apply"'s "-ascending" option. */
+
+static const gdb::option::flag_option_def<> ascending_option_def = {
+ "ascending",
+ N_("\
+Call COMMAND for all threads in ascending order.\n\
+The default is descending order."),
+};
+
+/* The qcs command line flags for the "thread apply" commands. Keep
+ this in sync with the "frame apply" commands. */
+
+using qcs_flag_option_def
+ = gdb::option::flag_option_def<qcs_flags>;
+
+static const gdb::option::option_def thr_qcs_flags_option_defs[] = {
+ qcs_flag_option_def {
+ "q", [] (qcs_flags *opt) { return &opt->quiet; },
+ N_("Disables printing the thread information."),
+ },
+
+ qcs_flag_option_def {
+ "c", [] (qcs_flags *opt) { return &opt->cont; },
+ N_("Print any error raised by COMMAND and continue."),
+ },
+
+ qcs_flag_option_def {
+ "s", [] (qcs_flags *opt) { return &opt->silent; },
+ N_("Silently ignore any errors or empty output produced by COMMAND."),
+ },
+};
+
+/* Create an option_def_group for the "thread apply all" options, with
+ ASCENDING and FLAGS as context. */