+ update_thread_list ();
+ current_ptid = inferior_ptid;
+
+ {
+ /* For backward compatibility, we make a list for MI. A table is
+ preferable for the CLI, though, because it shows table
+ headers. */
+ gdb::optional<ui_out_emit_list> list_emitter;
+ gdb::optional<ui_out_emit_table> table_emitter;
+
+ if (uiout->is_mi_like_p ())
+ list_emitter.emplace (uiout, "threads");
+ else
+ {
+ int n_threads = 0;
+
+ for (tp = thread_list; tp; tp = tp->next)
+ {
+ if (!should_print_thread (requested_threads, default_inf_num,
+ global_ids, pid, tp))
+ continue;
+
+ ++n_threads;
+ }
+
+ if (n_threads == 0)
+ {
+ if (requested_threads == NULL || *requested_threads == '\0')
+ uiout->message (_("No threads.\n"));
+ else
+ uiout->message (_("No threads match '%s'.\n"),
+ requested_threads);
+ return;
+ }
+
+ table_emitter.emplace (uiout, 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)
+ uiout->table_header (4, ui_left, "id", "GId");
+ uiout->table_header (17, ui_left, "target-id", "Target Id");
+ uiout->table_header (1, ui_left, "frame", "Frame");
+ uiout->table_body ();
+ }
+
+ /* We'll be switching threads temporarily. */
+ scoped_restore_current_thread restore_thread;
+
+ ALL_THREADS_BY_INFERIOR (inf, tp)
+ {
+ int core;
+
+ if (!should_print_thread (requested_threads, default_inf_num,
+ global_ids, pid, tp))
+ continue;
+
+ ui_out_emit_tuple tuple_emitter (uiout, NULL);
+
+ if (!uiout->is_mi_like_p ())
+ {
+ if (tp->ptid == current_ptid)
+ uiout->field_string ("current", "*");
+ else
+ uiout->field_skip ("current");
+
+ uiout->field_string ("id-in-tg", print_thread_id (tp));
+ }
+
+ if (show_global_ids || uiout->is_mi_like_p ())
+ uiout->field_int ("id", tp->global_num);
+
+ /* For the CLI, we stuff everything into the target-id field.
+ This is a gross hack to make the output come out looking
+ correct. The underlying problem here is that ui-out has no
+ way to specify that a field's space allocation should be
+ shared by several fields. For MI, we do the right thing
+ instead. */
+
+ target_id = target_pid_to_str (tp->ptid);
+ extra_info = target_extra_thread_info (tp);
+ name = tp->name ? tp->name : target_thread_name (tp);
+
+ if (uiout->is_mi_like_p ())
+ {
+ uiout->field_string ("target-id", target_id);
+ if (extra_info)
+ uiout->field_string ("details", extra_info);
+ if (name)
+ uiout->field_string ("name", name);
+ }
+ else
+ {
+ std::string contents;
+
+ if (extra_info && name)
+ contents = string_printf ("%s \"%s\" (%s)", target_id,
+ name, extra_info);
+ else if (extra_info)
+ contents = string_printf ("%s (%s)", target_id, extra_info);
+ else if (name)
+ contents = string_printf ("%s \"%s\"", target_id, name);
+ else
+ contents = target_id;
+
+ uiout->field_string ("target-id", contents.c_str ());
+ }
+
+ if (tp->state == THREAD_RUNNING)
+ uiout->text ("(running)\n");
+ else
+ {
+ /* The switch below puts us at the top of the stack (leaf
+ frame). */
+ switch_to_thread (tp->ptid);
+ print_stack_frame (get_selected_frame (NULL),
+ /* For MI output, print frame level. */
+ uiout->is_mi_like_p (),
+ LOCATION, 0);
+ }
+
+ if (uiout->is_mi_like_p ())
+ {
+ const char *state = "stopped";
+
+ if (tp->state == THREAD_RUNNING)
+ state = "running";
+ uiout->field_string ("state", state);
+ }
+
+ core = target_core_of_thread (tp->ptid);
+ if (uiout->is_mi_like_p () && core != -1)
+ uiout->field_int ("core", core);
+ }
+
+ /* This end scope restores the current thread and the frame
+ selected before the "info threads" command, and it finishes the
+ ui-out list or table. */
+ }
+
+ if (pid == -1 && requested_threads == NULL)
+ {
+ if (uiout->is_mi_like_p ()
+ && inferior_ptid != null_ptid)
+ {
+ int num = ptid_to_global_thread_id (inferior_ptid);
+
+ gdb_assert (num != 0);
+ uiout->field_int ("current-thread-id", num);
+ }
+
+ if (inferior_ptid != null_ptid && is_exited (inferior_ptid))
+ uiout->message ("\n\
+The current thread <Thread ID %s> has terminated. See `help thread'.\n",
+ print_thread_id (inferior_thread ()));
+ else if (thread_list != NULL && inferior_ptid == null_ptid)
+ uiout->message ("\n\
+No selected thread. See `help thread'.\n");
+ }