1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdbsupport/environ.h"
29 #include "gdbthread.h"
36 #include <sys/types.h>
39 #include "observable.h"
41 #include "cli/cli-decode.h"
42 #include "cli/cli-option.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46 #include "tid-parse.h"
48 #include "gdbsupport/gdb_optional.h"
49 #include "inline-frame.h"
52 /* Definition of struct thread_info exported to gdbthread.h. */
54 /* Prototypes for local functions. */
56 static int highest_thread_num;
58 /* The current/selected thread. */
59 static thread_info *current_thread_;
61 /* Returns true if THR is the current thread. */
64 is_current_thread (const thread_info *thr)
66 return thr == current_thread_;
70 inferior_thread (void)
72 gdb_assert (current_thread_ != nullptr);
73 return current_thread_;
76 /* Delete the breakpoint pointed at by BP_P, if there's one. */
79 delete_thread_breakpoint (struct breakpoint **bp_p)
83 delete_breakpoint (*bp_p);
89 delete_step_resume_breakpoint (struct thread_info *tp)
92 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
96 delete_exception_resume_breakpoint (struct thread_info *tp)
99 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
102 /* See gdbthread.h. */
105 delete_single_step_breakpoints (struct thread_info *tp)
108 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
111 /* Delete the breakpoint pointed at by BP_P at the next stop, if
115 delete_at_next_stop (struct breakpoint **bp)
119 (*bp)->disposition = disp_del_at_next_stop;
124 /* See gdbthread.h. */
127 thread_has_single_step_breakpoints_set (struct thread_info *tp)
129 return tp->control.single_step_breakpoints != NULL;
132 /* See gdbthread.h. */
135 thread_has_single_step_breakpoint_here (struct thread_info *tp,
136 const address_space *aspace,
139 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
141 return (ss_bps != NULL
142 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
145 /* See gdbthread.h. */
148 thread_cancel_execution_command (struct thread_info *thr)
150 if (thr->thread_fsm != NULL)
152 thr->thread_fsm->clean_up (thr);
153 delete thr->thread_fsm;
154 thr->thread_fsm = NULL;
159 clear_thread_inferior_resources (struct thread_info *tp)
161 /* NOTE: this will take care of any left-over step_resume breakpoints,
162 but not any user-specified thread-specific breakpoints. We can not
163 delete the breakpoint straight-off, because the inferior might not
164 be stopped at the moment. */
165 delete_at_next_stop (&tp->control.step_resume_breakpoint);
166 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
167 delete_at_next_stop (&tp->control.single_step_breakpoints);
169 delete_longjmp_breakpoint_at_next_stop (tp->global_num);
171 bpstat_clear (&tp->control.stop_bpstat);
173 btrace_teardown (tp);
175 thread_cancel_execution_command (tp);
177 clear_inline_frame_state (tp);
180 /* Set the TP's state as exited. */
183 set_thread_exited (thread_info *tp, bool silent)
185 /* Dead threads don't need to step-over. Remove from chain. */
186 if (tp->step_over_next != NULL)
187 global_thread_step_over_chain_remove (tp);
189 if (tp->state != THREAD_EXITED)
191 gdb::observers::thread_exit.notify (tp, silent);
193 /* Tag it as exited. */
194 tp->state = THREAD_EXITED;
196 /* Clear breakpoints, etc. associated with this thread. */
197 clear_thread_inferior_resources (tp);
202 init_thread_list (void)
204 highest_thread_num = 0;
206 for (thread_info *tp : all_threads_safe ())
208 inferior *inf = tp->inf;
210 if (tp->deletable ())
213 set_thread_exited (tp, 1);
215 inf->thread_list = NULL;
219 /* Allocate a new thread of inferior INF with target id PTID and add
220 it to the thread list. */
222 static struct thread_info *
223 new_thread (struct inferior *inf, ptid_t ptid)
225 thread_info *tp = new thread_info (inf, ptid);
227 if (inf->thread_list == NULL)
228 inf->thread_list = tp;
231 struct thread_info *last;
233 for (last = inf->thread_list; last->next != NULL; last = last->next)
234 gdb_assert (ptid != last->ptid
235 || last->state == THREAD_EXITED);
237 gdb_assert (ptid != last->ptid
238 || last->state == THREAD_EXITED);
247 add_thread_silent (process_stratum_target *targ, ptid_t ptid)
249 gdb_assert (targ != nullptr);
251 inferior *inf = find_inferior_ptid (targ, ptid);
253 /* We may have an old thread with the same id in the thread list.
254 If we do, it must be dead, otherwise we wouldn't be adding a new
255 thread with the same id. The OS is reusing this id --- delete
256 the old thread, and create a new one. */
257 thread_info *tp = find_thread_ptid (inf, ptid);
261 tp = new_thread (inf, ptid);
262 gdb::observers::new_thread.notify (tp);
268 add_thread_with_info (process_stratum_target *targ, ptid_t ptid,
269 private_thread_info *priv)
271 thread_info *result = add_thread_silent (targ, ptid);
273 result->priv.reset (priv);
275 if (print_thread_events)
276 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid).c_str ());
278 annotate_new_thread ();
283 add_thread (process_stratum_target *targ, ptid_t ptid)
285 return add_thread_with_info (targ, ptid, NULL);
288 private_thread_info::~private_thread_info () = default;
290 thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
291 : ptid (ptid_), inf (inf_)
293 gdb_assert (inf_ != NULL);
295 this->global_num = ++highest_thread_num;
296 this->per_inf_num = ++inf_->highest_thread_num;
298 /* Nothing to follow yet. */
299 memset (&this->pending_follow, 0, sizeof (this->pending_follow));
300 this->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
301 this->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
304 thread_info::~thread_info ()
309 /* See gdbthread.h. */
312 thread_info::deletable () const
314 /* If this is the current thread, or there's code out there that
315 relies on it existing (refcount > 0) we can't delete yet. */
316 return refcount () == 0 && !is_current_thread (this);
319 /* Add TP to the end of the step-over chain LIST_P. */
322 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
324 gdb_assert (tp->step_over_next == NULL);
325 gdb_assert (tp->step_over_prev == NULL);
330 tp->step_over_prev = tp->step_over_next = tp;
334 struct thread_info *head = *list_p;
335 struct thread_info *tail = head->step_over_prev;
337 tp->step_over_prev = tail;
338 tp->step_over_next = head;
339 head->step_over_prev = tp;
340 tail->step_over_next = tp;
344 /* See gdbthread.h. */
347 thread_step_over_chain_remove (thread_info **list_p, thread_info *tp)
349 gdb_assert (tp->step_over_next != NULL);
350 gdb_assert (tp->step_over_prev != NULL);
354 if (tp == tp->step_over_next)
357 *list_p = tp->step_over_next;
360 tp->step_over_prev->step_over_next = tp->step_over_next;
361 tp->step_over_next->step_over_prev = tp->step_over_prev;
362 tp->step_over_prev = tp->step_over_next = NULL;
365 /* See gdbthread.h. */
368 thread_step_over_chain_next (thread_info *chain_head, thread_info *tp)
370 thread_info *next = tp->step_over_next;
372 return next == chain_head ? NULL : next;
375 /* See gdbthread.h. */
378 global_thread_step_over_chain_next (struct thread_info *tp)
380 return thread_step_over_chain_next (global_thread_step_over_chain_head, tp);
383 /* See gdbthread.h. */
386 thread_is_in_step_over_chain (struct thread_info *tp)
388 return (tp->step_over_next != NULL);
391 /* See gdbthread.h. */
394 thread_step_over_chain_length (thread_info *tp)
399 gdb_assert (thread_is_in_step_over_chain (tp));
403 for (thread_info *iter = tp->step_over_next;
405 iter = iter->step_over_next)
411 /* See gdbthread.h. */
414 global_thread_step_over_chain_enqueue (struct thread_info *tp)
416 infrun_debug_printf ("enqueueing thread %s in global step over chain",
417 target_pid_to_str (tp->ptid).c_str ());
419 step_over_chain_enqueue (&global_thread_step_over_chain_head, tp);
422 /* See gdbthread.h. */
425 global_thread_step_over_chain_enqueue_chain (thread_info *chain_head)
427 gdb_assert (chain_head->step_over_next != nullptr);
428 gdb_assert (chain_head->step_over_prev != nullptr);
430 if (global_thread_step_over_chain_head == nullptr)
431 global_thread_step_over_chain_head = chain_head;
434 thread_info *global_last = global_thread_step_over_chain_head->step_over_prev;
435 thread_info *chain_last = chain_head->step_over_prev;
437 chain_last->step_over_next = global_thread_step_over_chain_head;
438 global_last->step_over_next = chain_head;
439 global_thread_step_over_chain_head->step_over_prev = chain_last;
440 chain_head->step_over_prev = global_last;
444 /* See gdbthread.h. */
447 global_thread_step_over_chain_remove (struct thread_info *tp)
449 infrun_debug_printf ("removing thread %s from global step over chain",
450 target_pid_to_str (tp->ptid).c_str ());
452 thread_step_over_chain_remove (&global_thread_step_over_chain_head, tp);
455 /* Delete the thread referenced by THR. If SILENT, don't notify
456 the observer of this exit.
458 THR must not be NULL or a failed assertion will be raised. */
461 delete_thread_1 (thread_info *thr, bool silent)
463 gdb_assert (thr != nullptr);
465 struct thread_info *tp, *tpprev = NULL;
467 for (tp = thr->inf->thread_list; tp; tpprev = tp, tp = tp->next)
474 set_thread_exited (tp, silent);
476 if (!tp->deletable ())
478 /* Will be really deleted some other time. */
483 tpprev->next = tp->next;
485 tp->inf->thread_list = tp->next;
490 /* See gdbthread.h. */
493 delete_thread (thread_info *thread)
495 delete_thread_1 (thread, false /* not silent */);
499 delete_thread_silent (thread_info *thread)
501 delete_thread_1 (thread, true /* silent */);
505 find_thread_global_id (int global_id)
507 for (thread_info *tp : all_threads ())
508 if (tp->global_num == global_id)
514 static struct thread_info *
515 find_thread_id (struct inferior *inf, int thr_num)
517 for (thread_info *tp : inf->threads ())
518 if (tp->per_inf_num == thr_num)
524 /* See gdbthread.h. */
527 find_thread_ptid (process_stratum_target *targ, ptid_t ptid)
529 inferior *inf = find_inferior_ptid (targ, ptid);
532 return find_thread_ptid (inf, ptid);
535 /* See gdbthread.h. */
538 find_thread_ptid (inferior *inf, ptid_t ptid)
540 gdb_assert (inf != nullptr);
542 for (thread_info *tp : inf->non_exited_threads ())
543 if (tp->ptid == ptid)
549 /* See gdbthread.h. */
552 find_thread_by_handle (gdb::array_view<const gdb_byte> handle,
553 struct inferior *inf)
555 return target_thread_handle_to_thread_info (handle.data (),
561 * Thread iterator function.
563 * Calls a callback function once for each thread, so long as
564 * the callback function returns false. If the callback function
565 * returns true, the iteration will end and the current thread
566 * will be returned. This can be useful for implementing a
567 * search for a thread with arbitrary attributes, or for applying
568 * some operation to every thread.
570 * FIXME: some of the existing functionality, such as
571 * "Thread apply all", might be rewritten using this functionality.
575 iterate_over_threads (int (*callback) (struct thread_info *, void *),
578 for (thread_info *tp : all_threads_safe ())
579 if ((*callback) (tp, data))
585 /* See gdbthread.h. */
590 for (thread_info *tp ATTRIBUTE_UNUSED : all_threads ())
596 thread_count (process_stratum_target *proc_target)
598 auto rng = all_threads (proc_target);
599 return std::distance (rng.begin (), rng.end ());
602 /* Return the number of non-exited threads in the thread list. */
605 live_threads_count (void)
607 auto rng = all_non_exited_threads ();
608 return std::distance (rng.begin (), rng.end ());
612 valid_global_thread_id (int global_id)
614 for (thread_info *tp : all_threads ())
615 if (tp->global_num == global_id)
622 in_thread_list (process_stratum_target *targ, ptid_t ptid)
624 return find_thread_ptid (targ, ptid) != nullptr;
627 /* Finds the first thread of the inferior. */
630 first_thread_of_inferior (inferior *inf)
632 return inf->thread_list;
636 any_thread_of_inferior (inferior *inf)
638 gdb_assert (inf->pid != 0);
640 /* Prefer the current thread, if there's one. */
641 if (inf == current_inferior () && inferior_ptid != null_ptid)
642 return inferior_thread ();
644 for (thread_info *tp : inf->non_exited_threads ())
651 any_live_thread_of_inferior (inferior *inf)
653 struct thread_info *curr_tp = NULL;
654 struct thread_info *tp_executing = NULL;
656 gdb_assert (inf != NULL && inf->pid != 0);
658 /* Prefer the current thread if it's not executing. */
659 if (inferior_ptid != null_ptid && current_inferior () == inf)
661 /* If the current thread is dead, forget it. If it's not
662 executing, use it. Otherwise, still choose it (below), but
663 only if no other non-executing thread is found. */
664 curr_tp = inferior_thread ();
665 if (curr_tp->state == THREAD_EXITED)
667 else if (!curr_tp->executing)
671 for (thread_info *tp : inf->non_exited_threads ())
679 /* If both the current thread and all live threads are executing,
680 prefer the current thread. */
684 /* Otherwise, just return an executing thread, if any. */
688 /* Return true if TP is an active thread. */
690 thread_alive (thread_info *tp)
692 if (tp->state == THREAD_EXITED)
695 /* Ensure we're looking at the right target stack. */
696 gdb_assert (tp->inf == current_inferior ());
698 return target_thread_alive (tp->ptid);
701 /* Switch to thread TP if it is alive. Returns true if successfully
702 switched, false otherwise. */
705 switch_to_thread_if_alive (thread_info *thr)
707 scoped_restore_current_thread restore_thread;
709 /* Switch inferior first, so that we're looking at the right target
711 switch_to_inferior_no_thread (thr->inf);
713 if (thread_alive (thr))
715 switch_to_thread (thr);
716 restore_thread.dont_restore ();
723 /* See gdbthreads.h. */
728 scoped_restore_current_thread restore_thread;
730 for (thread_info *tp : all_threads_safe ())
732 switch_to_inferior_no_thread (tp->inf);
734 if (!thread_alive (tp))
739 /* See gdbthreads.h. */
742 delete_exited_threads (void)
744 for (thread_info *tp : all_threads_safe ())
745 if (tp->state == THREAD_EXITED)
749 /* Return true value if stack temporaries are enabled for the thread
753 thread_stack_temporaries_enabled_p (thread_info *tp)
758 return tp->stack_temporaries_enabled;
761 /* Push V on to the stack temporaries of the thread with id PTID. */
764 push_thread_stack_temporary (thread_info *tp, struct value *v)
766 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
767 tp->stack_temporaries.push_back (v);
770 /* Return true if VAL is among the stack temporaries of the thread
771 TP. Return false otherwise. */
774 value_in_thread_stack_temporaries (struct value *val, thread_info *tp)
776 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
777 for (value *v : tp->stack_temporaries)
784 /* Return the last of the stack temporaries for thread with id PTID.
785 Return NULL if there are no stack temporaries for the thread. */
788 get_last_thread_stack_temporary (thread_info *tp)
790 struct value *lastval = NULL;
792 gdb_assert (tp != NULL);
793 if (!tp->stack_temporaries.empty ())
794 lastval = tp->stack_temporaries.back ();
800 thread_change_ptid (process_stratum_target *targ,
801 ptid_t old_ptid, ptid_t new_ptid)
803 struct inferior *inf;
804 struct thread_info *tp;
806 /* It can happen that what we knew as the target inferior id
807 changes. E.g, target remote may only discover the remote process
808 pid after adding the inferior to GDB's list. */
809 inf = find_inferior_ptid (targ, old_ptid);
810 inf->pid = new_ptid.pid ();
812 tp = find_thread_ptid (inf, old_ptid);
815 gdb::observers::thread_ptid_changed.notify (targ, old_ptid, new_ptid);
818 /* See gdbthread.h. */
821 set_resumed (process_stratum_target *targ, ptid_t ptid, bool resumed)
823 for (thread_info *tp : all_non_exited_threads (targ, ptid))
824 tp->resumed = resumed;
827 /* Helper for set_running, that marks one thread either running or
831 set_running_thread (struct thread_info *tp, bool running)
833 bool started = false;
835 if (running && tp->state == THREAD_STOPPED)
837 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
841 /* If the thread is now marked stopped, remove it from
842 the step-over queue, so that we don't try to resume
843 it until the user wants it to. */
844 if (tp->step_over_next != NULL)
845 global_thread_step_over_chain_remove (tp);
851 /* See gdbthread.h. */
854 thread_info::set_running (bool running)
856 if (set_running_thread (this, running))
857 gdb::observers::target_resumed.notify (this->ptid);
861 set_running (process_stratum_target *targ, ptid_t ptid, bool running)
863 /* We try not to notify the observer if no thread has actually
864 changed the running state -- merely to reduce the number of
865 messages to the MI frontend. A frontend is supposed to handle
866 multiple *running notifications just fine. */
867 bool any_started = false;
869 for (thread_info *tp : all_non_exited_threads (targ, ptid))
870 if (set_running_thread (tp, running))
874 gdb::observers::target_resumed.notify (ptid);
878 /* Helper for set_executing. Set's the thread's 'executing' field
879 from EXECUTING, and if EXECUTING is true also clears the thread's
883 set_executing_thread (thread_info *thr, bool executing)
885 thr->executing = executing;
887 thr->suspend.stop_pc = ~(CORE_ADDR) 0;
891 set_executing (process_stratum_target *targ, ptid_t ptid, bool executing)
893 for (thread_info *tp : all_non_exited_threads (targ, ptid))
894 set_executing_thread (tp, executing);
896 /* It only takes one running thread to spawn more threads. */
898 targ->threads_executing = true;
899 /* Only clear the flag if the caller is telling us everything is
901 else if (minus_one_ptid == ptid)
902 targ->threads_executing = false;
905 /* See gdbthread.h. */
908 threads_are_executing (process_stratum_target *target)
910 return target->threads_executing;
914 set_stop_requested (process_stratum_target *targ, ptid_t ptid, bool stop)
916 for (thread_info *tp : all_non_exited_threads (targ, ptid))
917 tp->stop_requested = stop;
919 /* Call the stop requested observer so other components of GDB can
920 react to this request. */
922 gdb::observers::thread_stop_requested.notify (ptid);
926 finish_thread_state (process_stratum_target *targ, ptid_t ptid)
928 bool any_started = false;
930 for (thread_info *tp : all_non_exited_threads (targ, ptid))
931 if (set_running_thread (tp, tp->executing))
935 gdb::observers::target_resumed.notify (ptid);
938 /* See gdbthread.h. */
941 validate_registers_access (void)
943 /* No selected thread, no registers. */
944 if (inferior_ptid == null_ptid)
945 error (_("No thread selected."));
947 thread_info *tp = inferior_thread ();
949 /* Don't try to read from a dead thread. */
950 if (tp->state == THREAD_EXITED)
951 error (_("The current thread has terminated"));
953 /* ... or from a spinning thread. FIXME: This isn't actually fully
954 correct. It'll allow an user-requested access (e.g., "print $pc"
955 at the prompt) when a thread is not executing for some internal
956 reason, but is marked running from the user's perspective. E.g.,
957 the thread is waiting for its turn in the step-over queue. */
959 error (_("Selected thread is running."));
962 /* See gdbthread.h. */
965 can_access_registers_thread (thread_info *thread)
967 /* No thread, no registers. */
971 /* Don't try to read from a dead thread. */
972 if (thread->state == THREAD_EXITED)
975 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
976 if (thread->executing)
983 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
985 return (pc >= thread->control.step_range_start
986 && pc < thread->control.step_range_end);
989 /* Helper for print_thread_info. Returns true if THR should be
990 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
991 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
992 is true if REQUESTED_THREADS is list of global IDs, false if a list
993 of per-inferior thread ids. If PID is not -1, only print THR if it
994 is a thread from the process PID. Otherwise, threads from all
995 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
996 and PID is not -1, then the thread is printed if it belongs to the
997 specified process. Otherwise, an error is raised. */
1000 should_print_thread (const char *requested_threads, int default_inf_num,
1001 int global_ids, int pid, struct thread_info *thr)
1003 if (requested_threads != NULL && *requested_threads != '\0')
1008 in_list = number_is_in_list (requested_threads, thr->global_num);
1010 in_list = tid_is_in_list (requested_threads, default_inf_num,
1011 thr->inf->num, thr->per_inf_num);
1016 if (pid != -1 && thr->ptid.pid () != pid)
1018 if (requested_threads != NULL && *requested_threads != '\0')
1019 error (_("Requested thread not found in requested process"));
1023 if (thr->state == THREAD_EXITED)
1029 /* Return the string to display in "info threads"'s "Target Id"
1033 thread_target_id_str (thread_info *tp)
1035 std::string target_id = target_pid_to_str (tp->ptid);
1036 const char *extra_info = target_extra_thread_info (tp);
1037 const char *name = tp->name != nullptr ? tp->name : target_thread_name (tp);
1039 if (extra_info != nullptr && name != nullptr)
1040 return string_printf ("%s \"%s\" (%s)", target_id.c_str (), name,
1042 else if (extra_info != nullptr)
1043 return string_printf ("%s (%s)", target_id.c_str (), extra_info);
1044 else if (name != nullptr)
1045 return string_printf ("%s \"%s\"", target_id.c_str (), name);
1050 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1051 whether REQUESTED_THREADS is a list of global or per-inferior
1055 print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
1056 int global_ids, int pid,
1057 int show_global_ids)
1059 int default_inf_num = current_inferior ()->num;
1061 update_thread_list ();
1063 /* Whether we saw any thread. */
1064 bool any_thread = false;
1065 /* Whether the current thread is exited. */
1066 bool current_exited = false;
1068 thread_info *current_thread = (inferior_ptid != null_ptid
1069 ? inferior_thread () : NULL);
1072 /* For backward compatibility, we make a list for MI. A table is
1073 preferable for the CLI, though, because it shows table
1075 gdb::optional<ui_out_emit_list> list_emitter;
1076 gdb::optional<ui_out_emit_table> table_emitter;
1078 /* We'll be switching threads temporarily below. */
1079 scoped_restore_current_thread restore_thread;
1081 if (uiout->is_mi_like_p ())
1082 list_emitter.emplace (uiout, "threads");
1086 /* The width of the "Target Id" column. Grown below to
1087 accommodate the largest entry. */
1088 size_t target_id_col_width = 17;
1090 for (thread_info *tp : all_threads ())
1092 if (!should_print_thread (requested_threads, default_inf_num,
1093 global_ids, pid, tp))
1096 if (!uiout->is_mi_like_p ())
1098 /* Switch inferiors so we're looking at the right
1100 switch_to_inferior_no_thread (tp->inf);
1103 = std::max (target_id_col_width,
1104 thread_target_id_str (tp).size ());
1112 if (requested_threads == NULL || *requested_threads == '\0')
1113 uiout->message (_("No threads.\n"));
1115 uiout->message (_("No threads match '%s'.\n"),
1120 table_emitter.emplace (uiout, show_global_ids ? 5 : 4,
1121 n_threads, "threads");
1123 uiout->table_header (1, ui_left, "current", "");
1124 uiout->table_header (4, ui_left, "id-in-tg", "Id");
1125 if (show_global_ids)
1126 uiout->table_header (4, ui_left, "id", "GId");
1127 uiout->table_header (target_id_col_width, ui_left,
1128 "target-id", "Target Id");
1129 uiout->table_header (1, ui_left, "frame", "Frame");
1130 uiout->table_body ();
1133 for (inferior *inf : all_inferiors ())
1134 for (thread_info *tp : inf->threads ())
1139 if (tp == current_thread && tp->state == THREAD_EXITED)
1140 current_exited = true;
1142 if (!should_print_thread (requested_threads, default_inf_num,
1143 global_ids, pid, tp))
1146 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1148 if (!uiout->is_mi_like_p ())
1150 if (tp == current_thread)
1151 uiout->field_string ("current", "*");
1153 uiout->field_skip ("current");
1155 uiout->field_string ("id-in-tg", print_thread_id (tp));
1158 if (show_global_ids || uiout->is_mi_like_p ())
1159 uiout->field_signed ("id", tp->global_num);
1161 /* Switch to the thread (and inferior / target). */
1162 switch_to_thread (tp);
1164 /* For the CLI, we stuff everything into the target-id field.
1165 This is a gross hack to make the output come out looking
1166 correct. The underlying problem here is that ui-out has no
1167 way to specify that a field's space allocation should be
1168 shared by several fields. For MI, we do the right thing
1171 if (uiout->is_mi_like_p ())
1173 uiout->field_string ("target-id", target_pid_to_str (tp->ptid));
1175 const char *extra_info = target_extra_thread_info (tp);
1176 if (extra_info != nullptr)
1177 uiout->field_string ("details", extra_info);
1179 const char *name = (tp->name != nullptr
1181 : target_thread_name (tp));
1183 uiout->field_string ("name", name);
1187 uiout->field_string ("target-id",
1188 thread_target_id_str (tp).c_str ());
1191 if (tp->state == THREAD_RUNNING)
1192 uiout->text ("(running)\n");
1195 /* The switch above put us at the top of the stack (leaf
1197 print_stack_frame (get_selected_frame (NULL),
1198 /* For MI output, print frame level. */
1199 uiout->is_mi_like_p (),
1203 if (uiout->is_mi_like_p ())
1205 const char *state = "stopped";
1207 if (tp->state == THREAD_RUNNING)
1209 uiout->field_string ("state", state);
1212 core = target_core_of_thread (tp->ptid);
1213 if (uiout->is_mi_like_p () && core != -1)
1214 uiout->field_signed ("core", core);
1217 /* This end scope restores the current thread and the frame
1218 selected before the "info threads" command, and it finishes the
1219 ui-out list or table. */
1222 if (pid == -1 && requested_threads == NULL)
1224 if (uiout->is_mi_like_p () && inferior_ptid != null_ptid)
1225 uiout->field_signed ("current-thread-id", current_thread->global_num);
1227 if (inferior_ptid != null_ptid && current_exited)
1228 uiout->message ("\n\
1229 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1230 print_thread_id (inferior_thread ()));
1231 else if (any_thread && inferior_ptid == null_ptid)
1232 uiout->message ("\n\
1233 No selected thread. See `help thread'.\n");
1237 /* See gdbthread.h. */
1240 print_thread_info (struct ui_out *uiout, const char *requested_threads,
1243 print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
1246 /* The options for the "info threads" command. */
1248 struct info_threads_opts
1251 bool show_global_ids = false;
1254 static const gdb::option::option_def info_threads_option_defs[] = {
1256 gdb::option::flag_option_def<info_threads_opts> {
1258 [] (info_threads_opts *opts) { return &opts->show_global_ids; },
1259 N_("Show global thread IDs."),
1264 /* Create an option_def_group for the "info threads" options, with
1265 IT_OPTS as context. */
1267 static inline gdb::option::option_def_group
1268 make_info_threads_options_def_group (info_threads_opts *it_opts)
1270 return {{info_threads_option_defs}, it_opts};
1273 /* Implementation of the "info threads" command.
1275 Note: this has the drawback that it _really_ switches
1276 threads, which frees the frame cache. A no-side
1277 effects info-threads command would be nicer. */
1280 info_threads_command (const char *arg, int from_tty)
1282 info_threads_opts it_opts;
1284 auto grp = make_info_threads_options_def_group (&it_opts);
1285 gdb::option::process_options
1286 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1288 print_thread_info_1 (current_uiout, arg, 0, -1, it_opts.show_global_ids);
1291 /* Completer for the "info threads" command. */
1294 info_threads_command_completer (struct cmd_list_element *ignore,
1295 completion_tracker &tracker,
1296 const char *text, const char *word_ignored)
1298 const auto grp = make_info_threads_options_def_group (nullptr);
1300 if (gdb::option::complete_options
1301 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1304 /* Convenience to let the user know what the option can accept. */
1307 gdb::option::complete_on_all_options (tracker, grp);
1308 /* Keep this "ID" in sync with what "help info threads"
1310 tracker.add_completion (make_unique_xstrdup ("ID"));
1314 /* See gdbthread.h. */
1317 switch_to_thread_no_regs (struct thread_info *thread)
1319 struct inferior *inf = thread->inf;
1321 set_current_program_space (inf->pspace);
1322 set_current_inferior (inf);
1324 current_thread_ = thread;
1325 inferior_ptid = current_thread_->ptid;
1328 /* See gdbthread.h. */
1331 switch_to_no_thread ()
1333 if (current_thread_ == nullptr)
1336 current_thread_ = nullptr;
1337 inferior_ptid = null_ptid;
1338 reinit_frame_cache ();
1341 /* See gdbthread.h. */
1344 switch_to_thread (thread_info *thr)
1346 gdb_assert (thr != NULL);
1348 if (is_current_thread (thr))
1351 switch_to_thread_no_regs (thr);
1353 reinit_frame_cache ();
1356 /* See gdbsupport/common-gdbthread.h. */
1359 switch_to_thread (process_stratum_target *proc_target, ptid_t ptid)
1361 thread_info *thr = find_thread_ptid (proc_target, ptid);
1362 switch_to_thread (thr);
1368 scoped_restore_current_thread::restore ()
1370 /* If an entry of thread_info was previously selected, it won't be
1371 deleted because we've increased its refcount. The thread represented
1372 by this thread_info entry may have already exited (due to normal exit,
1373 detach, etc), so the thread_info.state is THREAD_EXITED. */
1374 if (m_thread != NULL
1375 /* If the previously selected thread belonged to a process that has
1376 in the mean time exited (or killed, detached, etc.), then don't revert
1377 back to it, but instead simply drop back to no thread selected. */
1379 switch_to_thread (m_thread.get ());
1381 switch_to_inferior_no_thread (m_inf.get ());
1383 /* The running state of the originally selected thread may have
1384 changed, so we have to recheck it here. */
1385 if (inferior_ptid != null_ptid
1387 && m_thread->state == THREAD_STOPPED
1388 && target_has_registers ()
1389 && target_has_stack ()
1390 && target_has_memory ())
1391 restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
1393 set_language (m_lang);
1396 scoped_restore_current_thread::~scoped_restore_current_thread ()
1398 if (!m_dont_restore)
1402 scoped_restore_current_thread::scoped_restore_current_thread ()
1404 m_inf = inferior_ref::new_reference (current_inferior ());
1406 m_lang = current_language->la_language;
1408 if (inferior_ptid != null_ptid)
1410 m_thread = thread_info_ref::new_reference (inferior_thread ());
1412 m_was_stopped = m_thread->state == THREAD_STOPPED;
1413 save_selected_frame (&m_selected_frame_id, &m_selected_frame_level);
1417 /* See gdbthread.h. */
1420 show_thread_that_caused_stop (void)
1422 return highest_thread_num > 1;
1425 /* See gdbthread.h. */
1428 show_inferior_qualified_tids (void)
1430 return (inferior_list->next != NULL || inferior_list->num != 1);
1433 /* See gdbthread.h. */
1436 print_thread_id (struct thread_info *thr)
1438 char *s = get_print_cell ();
1440 if (show_inferior_qualified_tids ())
1441 xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num);
1443 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num);
1447 /* Sort an array of struct thread_info pointers by thread ID (first by
1448 inferior number, and then by per-inferior thread number). Sorts in
1452 tp_array_compar_ascending (const thread_info_ref &a, const thread_info_ref &b)
1454 if (a->inf->num != b->inf->num)
1455 return a->inf->num < b->inf->num;
1457 return (a->per_inf_num < b->per_inf_num);
1460 /* Sort an array of struct thread_info pointers by thread ID (first by
1461 inferior number, and then by per-inferior thread number). Sorts in
1462 descending order. */
1465 tp_array_compar_descending (const thread_info_ref &a, const thread_info_ref &b)
1467 if (a->inf->num != b->inf->num)
1468 return a->inf->num > b->inf->num;
1470 return (a->per_inf_num > b->per_inf_num);
1473 /* Assuming that THR is the current thread, execute CMD.
1474 FLAGS.QUIET controls the printing of the thread information.
1475 FLAGS.CONT and FLAGS.SILENT control how to handle errors. Can throw an
1476 exception if !FLAGS.SILENT and !FLAGS.CONT and CMD fails. */
1479 thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
1480 const qcs_flags &flags)
1482 gdb_assert (is_current_thread (thr));
1484 /* The thread header is computed before running the command since
1485 the command can change the inferior, which is not permitted
1486 by thread_target_id_str. */
1487 std::string thr_header =
1488 string_printf (_("\nThread %s (%s):\n"), print_thread_id (thr),
1489 thread_target_id_str (thr).c_str ());
1493 std::string cmd_result = execute_command_to_string
1494 (cmd, from_tty, gdb_stdout->term_out ());
1495 if (!flags.silent || cmd_result.length () > 0)
1498 printf_filtered ("%s", thr_header.c_str ());
1499 printf_filtered ("%s", cmd_result.c_str ());
1502 catch (const gdb_exception_error &ex)
1507 printf_filtered ("%s", thr_header.c_str ());
1509 printf_filtered ("%s\n", ex.what ());
1516 /* Option definition of "thread apply"'s "-ascending" option. */
1518 static const gdb::option::flag_option_def<> ascending_option_def = {
1521 Call COMMAND for all threads in ascending order.\n\
1522 The default is descending order."),
1525 /* The qcs command line flags for the "thread apply" commands. Keep
1526 this in sync with the "frame apply" commands. */
1528 using qcs_flag_option_def
1529 = gdb::option::flag_option_def<qcs_flags>;
1531 static const gdb::option::option_def thr_qcs_flags_option_defs[] = {
1532 qcs_flag_option_def {
1533 "q", [] (qcs_flags *opt) { return &opt->quiet; },
1534 N_("Disables printing the thread information."),
1537 qcs_flag_option_def {
1538 "c", [] (qcs_flags *opt) { return &opt->cont; },
1539 N_("Print any error raised by COMMAND and continue."),
1542 qcs_flag_option_def {
1543 "s", [] (qcs_flags *opt) { return &opt->silent; },
1544 N_("Silently ignore any errors or empty output produced by COMMAND."),
1548 /* Create an option_def_group for the "thread apply all" options, with
1549 ASCENDING and FLAGS as context. */
1551 static inline std::array<gdb::option::option_def_group, 2>
1552 make_thread_apply_all_options_def_group (bool *ascending,
1556 { {ascending_option_def.def ()}, ascending},
1557 { {thr_qcs_flags_option_defs}, flags },
1561 /* Create an option_def_group for the "thread apply" options, with
1562 FLAGS as context. */
1564 static inline gdb::option::option_def_group
1565 make_thread_apply_options_def_group (qcs_flags *flags)
1567 return {{thr_qcs_flags_option_defs}, flags};
1570 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1571 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1572 of two numbers separated by a hyphen. Examples:
1574 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1575 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1576 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1579 thread_apply_all_command (const char *cmd, int from_tty)
1581 bool ascending = false;
1584 auto group = make_thread_apply_all_options_def_group (&ascending,
1586 gdb::option::process_options
1587 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1589 validate_flags_qcs ("thread apply all", &flags);
1591 if (cmd == NULL || *cmd == '\000')
1592 error (_("Please specify a command at the end of 'thread apply all'"));
1594 update_thread_list ();
1596 int tc = live_threads_count ();
1599 /* Save a copy of the thread list and increment each thread's
1600 refcount while executing the command in the context of each
1601 thread, in case the command is one that wipes threads. E.g.,
1602 detach, kill, disconnect, etc., or even normally continuing
1603 over an inferior or thread exit. */
1604 std::vector<thread_info_ref> thr_list_cpy;
1605 thr_list_cpy.reserve (tc);
1607 for (thread_info *tp : all_non_exited_threads ())
1608 thr_list_cpy.push_back (thread_info_ref::new_reference (tp));
1609 gdb_assert (thr_list_cpy.size () == tc);
1611 auto *sorter = (ascending
1612 ? tp_array_compar_ascending
1613 : tp_array_compar_descending);
1614 std::sort (thr_list_cpy.begin (), thr_list_cpy.end (), sorter);
1616 scoped_restore_current_thread restore_thread;
1618 for (thread_info_ref &thr : thr_list_cpy)
1619 if (switch_to_thread_if_alive (thr.get ()))
1620 thr_try_catch_cmd (thr.get (), cmd, from_tty, flags);
1624 /* Completer for "thread apply [ID list]". */
1627 thread_apply_command_completer (cmd_list_element *ignore,
1628 completion_tracker &tracker,
1629 const char *text, const char * /*word*/)
1631 /* Don't leave this to complete_options because there's an early
1633 tracker.set_use_custom_word_point (true);
1635 tid_range_parser parser;
1636 parser.init (text, current_inferior ()->num);
1640 while (!parser.finished ())
1642 int inf_num, thr_start, thr_end;
1644 if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
1647 if (parser.in_star_range () || parser.in_thread_range ())
1648 parser.skip_range ();
1651 catch (const gdb_exception_error &ex)
1653 /* get_tid_range throws if it parses a negative number, for
1654 example. But a seemingly negative number may be the start of
1655 an option instead. */
1658 const char *cmd = parser.cur_tok ();
1662 /* No thread ID list yet. */
1666 /* Check if we're past a valid thread ID list already. */
1667 if (parser.finished ()
1668 && cmd > text && !isspace (cmd[-1]))
1671 /* We're past the thread ID list, advance word point. */
1672 tracker.advance_custom_word_point_by (cmd - text);
1675 const auto group = make_thread_apply_options_def_group (nullptr);
1676 if (gdb::option::complete_options
1677 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
1680 complete_nested_command_line (tracker, text);
1683 /* Completer for "thread apply all". */
1686 thread_apply_all_command_completer (cmd_list_element *ignore,
1687 completion_tracker &tracker,
1688 const char *text, const char *word)
1690 const auto group = make_thread_apply_all_options_def_group (nullptr,
1692 if (gdb::option::complete_options
1693 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
1696 complete_nested_command_line (tracker, text);
1699 /* Implementation of the "thread apply" command. */
1702 thread_apply_command (const char *tidlist, int from_tty)
1705 const char *cmd = NULL;
1706 tid_range_parser parser;
1708 if (tidlist == NULL || *tidlist == '\000')
1709 error (_("Please specify a thread ID list"));
1711 parser.init (tidlist, current_inferior ()->num);
1712 while (!parser.finished ())
1714 int inf_num, thr_start, thr_end;
1716 if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
1720 cmd = parser.cur_tok ();
1722 auto group = make_thread_apply_options_def_group (&flags);
1723 gdb::option::process_options
1724 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1726 validate_flags_qcs ("thread apply", &flags);
1729 error (_("Please specify a command following the thread ID list"));
1731 if (tidlist == cmd || isdigit (cmd[0]))
1732 invalid_thread_id_error (cmd);
1734 scoped_restore_current_thread restore_thread;
1736 parser.init (tidlist, current_inferior ()->num);
1737 while (!parser.finished ())
1739 struct thread_info *tp = NULL;
1740 struct inferior *inf;
1741 int inf_num, thr_num;
1743 parser.get_tid (&inf_num, &thr_num);
1744 inf = find_inferior_id (inf_num);
1746 tp = find_thread_id (inf, thr_num);
1748 if (parser.in_star_range ())
1752 warning (_("Unknown inferior %d"), inf_num);
1753 parser.skip_range ();
1757 /* No use looking for threads past the highest thread number
1758 the inferior ever had. */
1759 if (thr_num >= inf->highest_thread_num)
1760 parser.skip_range ();
1762 /* Be quiet about unknown threads numbers. */
1769 if (show_inferior_qualified_tids () || parser.tid_is_qualified ())
1770 warning (_("Unknown thread %d.%d"), inf_num, thr_num);
1772 warning (_("Unknown thread %d"), thr_num);
1776 if (!switch_to_thread_if_alive (tp))
1778 warning (_("Thread %s has terminated."), print_thread_id (tp));
1782 thr_try_catch_cmd (tp, cmd, from_tty, flags);
1787 /* Implementation of the "taas" command. */
1790 taas_command (const char *cmd, int from_tty)
1792 if (cmd == NULL || *cmd == '\0')
1793 error (_("Please specify a command to apply on all threads"));
1794 std::string expanded = std::string ("thread apply all -s ") + cmd;
1795 execute_command (expanded.c_str (), from_tty);
1798 /* Implementation of the "tfaas" command. */
1801 tfaas_command (const char *cmd, int from_tty)
1803 if (cmd == NULL || *cmd == '\0')
1804 error (_("Please specify a command to apply on all frames of all threads"));
1805 std::string expanded
1806 = std::string ("thread apply all -s -- frame apply all -s ") + cmd;
1807 execute_command (expanded.c_str (), from_tty);
1810 /* Switch to the specified thread, or print the current thread. */
1813 thread_command (const char *tidstr, int from_tty)
1817 if (inferior_ptid == null_ptid)
1818 error (_("No thread selected"));
1820 if (target_has_stack ())
1822 struct thread_info *tp = inferior_thread ();
1824 if (tp->state == THREAD_EXITED)
1825 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1826 print_thread_id (tp),
1827 target_pid_to_str (inferior_ptid).c_str ());
1829 printf_filtered (_("[Current thread is %s (%s)]\n"),
1830 print_thread_id (tp),
1831 target_pid_to_str (inferior_ptid).c_str ());
1834 error (_("No stack."));
1838 ptid_t previous_ptid = inferior_ptid;
1840 thread_select (tidstr, parse_thread_id (tidstr, NULL));
1842 /* Print if the thread has not changed, otherwise an event will
1844 if (inferior_ptid == previous_ptid)
1846 print_selected_thread_frame (current_uiout,
1847 USER_SELECTED_THREAD
1848 | USER_SELECTED_FRAME);
1852 gdb::observers::user_selected_context_changed.notify
1853 (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
1858 /* Implementation of `thread name'. */
1861 thread_name_command (const char *arg, int from_tty)
1863 struct thread_info *info;
1865 if (inferior_ptid == null_ptid)
1866 error (_("No thread selected"));
1868 arg = skip_spaces (arg);
1870 info = inferior_thread ();
1872 info->name = arg ? xstrdup (arg) : NULL;
1875 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1878 thread_find_command (const char *arg, int from_tty)
1881 unsigned long match = 0;
1883 if (arg == NULL || *arg == '\0')
1884 error (_("Command requires an argument."));
1886 tmp = re_comp (arg);
1888 error (_("Invalid regexp (%s): %s"), tmp, arg);
1890 /* We're going to be switching threads. */
1891 scoped_restore_current_thread restore_thread;
1893 update_thread_list ();
1895 for (thread_info *tp : all_threads ())
1897 switch_to_inferior_no_thread (tp->inf);
1899 if (tp->name != NULL && re_exec (tp->name))
1901 printf_filtered (_("Thread %s has name '%s'\n"),
1902 print_thread_id (tp), tp->name);
1906 tmp = target_thread_name (tp);
1907 if (tmp != NULL && re_exec (tmp))
1909 printf_filtered (_("Thread %s has target name '%s'\n"),
1910 print_thread_id (tp), tmp);
1914 std::string name = target_pid_to_str (tp->ptid);
1915 if (!name.empty () && re_exec (name.c_str ()))
1917 printf_filtered (_("Thread %s has target id '%s'\n"),
1918 print_thread_id (tp), name.c_str ());
1922 tmp = target_extra_thread_info (tp);
1923 if (tmp != NULL && re_exec (tmp))
1925 printf_filtered (_("Thread %s has extra info '%s'\n"),
1926 print_thread_id (tp), tmp);
1931 printf_filtered (_("No threads match '%s'\n"), arg);
1934 /* Print notices when new threads are attached and detached. */
1935 bool print_thread_events = true;
1937 show_print_thread_events (struct ui_file *file, int from_tty,
1938 struct cmd_list_element *c, const char *value)
1940 fprintf_filtered (file,
1941 _("Printing of thread events is %s.\n"),
1945 /* See gdbthread.h. */
1948 thread_select (const char *tidstr, thread_info *tp)
1950 if (!switch_to_thread_if_alive (tp))
1951 error (_("Thread ID %s has terminated."), tidstr);
1953 annotate_thread_changed ();
1955 /* Since the current thread may have changed, see if there is any
1956 exited thread we can now delete. */
1957 delete_exited_threads ();
1960 /* Print thread and frame switch command response. */
1963 print_selected_thread_frame (struct ui_out *uiout,
1964 user_selected_what selection)
1966 struct thread_info *tp = inferior_thread ();
1968 if (selection & USER_SELECTED_THREAD)
1970 if (uiout->is_mi_like_p ())
1972 uiout->field_signed ("new-thread-id",
1973 inferior_thread ()->global_num);
1977 uiout->text ("[Switching to thread ");
1978 uiout->field_string ("new-thread-id", print_thread_id (tp));
1980 uiout->text (target_pid_to_str (inferior_ptid).c_str ());
1985 if (tp->state == THREAD_RUNNING)
1987 if (selection & USER_SELECTED_THREAD)
1988 uiout->text ("(running)\n");
1990 else if (selection & USER_SELECTED_FRAME)
1992 if (selection & USER_SELECTED_THREAD)
1995 if (has_stack_frames ())
1996 print_stack_frame_to_uiout (uiout, get_selected_frame (NULL),
2001 /* Update the 'threads_executing' global based on the threads we know
2002 about right now. This is used by infrun to tell whether we should
2003 pull events out of the current target. */
2006 update_threads_executing (void)
2008 process_stratum_target *targ = current_inferior ()->process_target ();
2013 targ->threads_executing = false;
2015 for (inferior *inf : all_non_exited_inferiors (targ))
2017 if (!inf->has_execution ())
2020 /* If the process has no threads, then it must be we have a
2021 process-exit event pending. */
2022 if (inf->thread_list == NULL)
2024 targ->threads_executing = true;
2028 for (thread_info *tp : inf->non_exited_threads ())
2032 targ->threads_executing = true;
2040 update_thread_list (void)
2042 target_update_thread_list ();
2043 update_threads_executing ();
2046 /* Return a new value for the selected thread's id. Return a value of
2047 0 if no thread is selected. If GLOBAL is true, return the thread's
2048 global number. Otherwise return the per-inferior number. */
2050 static struct value *
2051 thread_num_make_value_helper (struct gdbarch *gdbarch, int global)
2055 if (inferior_ptid == null_ptid)
2059 thread_info *tp = inferior_thread ();
2061 int_val = tp->global_num;
2063 int_val = tp->per_inf_num;
2066 return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val);
2069 /* Return a new value for the selected thread's per-inferior thread
2070 number. Return a value of 0 if no thread is selected, or no
2073 static struct value *
2074 thread_id_per_inf_num_make_value (struct gdbarch *gdbarch,
2075 struct internalvar *var,
2078 return thread_num_make_value_helper (gdbarch, 0);
2081 /* Return a new value for the selected thread's global id. Return a
2082 value of 0 if no thread is selected, or no threads exist. */
2084 static struct value *
2085 global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2088 return thread_num_make_value_helper (gdbarch, 1);
2091 /* Commands with a prefix of `thread'. */
2092 struct cmd_list_element *thread_cmd_list = NULL;
2094 /* Implementation of `thread' variable. */
2096 static const struct internalvar_funcs thread_funcs =
2098 thread_id_per_inf_num_make_value,
2103 /* Implementation of `gthread' variable. */
2105 static const struct internalvar_funcs gthread_funcs =
2107 global_thread_id_make_value,
2112 void _initialize_thread ();
2114 _initialize_thread ()
2116 static struct cmd_list_element *thread_apply_list = NULL;
2117 cmd_list_element *c;
2119 const auto info_threads_opts = make_info_threads_options_def_group (nullptr);
2121 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2123 static std::string info_threads_help
2124 = gdb::option::build_help (_("\
2125 Display currently known threads.\n\
2126 Usage: info threads [OPTION]... [ID]...\n\
2127 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2128 Otherwise, all threads are displayed.\n\
2134 c = add_info ("threads", info_threads_command, info_threads_help.c_str ());
2135 set_cmd_completer_handle_brkchars (c, info_threads_command_completer);
2137 add_prefix_cmd ("thread", class_run, thread_command, _("\
2138 Use this command to switch between threads.\n\
2139 The new thread ID must be currently known."),
2140 &thread_cmd_list, "thread ", 1, &cmdlist);
2142 #define THREAD_APPLY_OPTION_HELP "\
2143 Prints per-inferior thread number and target system's thread id\n\
2144 followed by COMMAND output.\n\
2146 By default, an error raised during the execution of COMMAND\n\
2147 aborts \"thread apply\".\n\
2152 const auto thread_apply_opts = make_thread_apply_options_def_group (nullptr);
2154 static std::string thread_apply_help = gdb::option::build_help (_("\
2155 Apply a command to a list of threads.\n\
2156 Usage: thread apply ID... [OPTION]... COMMAND\n\
2157 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2158 THREAD_APPLY_OPTION_HELP),
2161 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
2162 thread_apply_help.c_str (),
2163 &thread_apply_list, "thread apply ", 1,
2165 set_cmd_completer_handle_brkchars (c, thread_apply_command_completer);
2167 const auto thread_apply_all_opts
2168 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2170 static std::string thread_apply_all_help = gdb::option::build_help (_("\
2171 Apply a command to all threads.\n\
2173 Usage: thread apply all [OPTION]... COMMAND\n"
2174 THREAD_APPLY_OPTION_HELP),
2175 thread_apply_all_opts);
2177 c = add_cmd ("all", class_run, thread_apply_all_command,
2178 thread_apply_all_help.c_str (),
2179 &thread_apply_list);
2180 set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer);
2182 c = add_com ("taas", class_run, taas_command, _("\
2183 Apply a command to all threads (ignoring errors and empty output).\n\
2184 Usage: taas [OPTION]... COMMAND\n\
2185 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2186 See \"help thread apply all\" for available options."));
2187 set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer);
2189 c = add_com ("tfaas", class_run, tfaas_command, _("\
2190 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2191 Usage: tfaas [OPTION]... COMMAND\n\
2192 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2193 See \"help frame apply all\" for available options."));
2194 set_cmd_completer_handle_brkchars (c, frame_apply_all_cmd_completer);
2196 add_cmd ("name", class_run, thread_name_command,
2197 _("Set the current thread's name.\n\
2198 Usage: thread name [NAME]\n\
2199 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2201 add_cmd ("find", class_run, thread_find_command, _("\
2202 Find threads that match a regular expression.\n\
2203 Usage: thread find REGEXP\n\
2204 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2207 add_com_alias ("t", "thread", class_run, 1);
2209 add_setshow_boolean_cmd ("thread-events", no_class,
2210 &print_thread_events, _("\
2211 Set printing of thread events (such as thread start and exit)."), _("\
2212 Show printing of thread events (such as thread start and exit)."), NULL,
2214 show_print_thread_events,
2215 &setprintlist, &showprintlist);
2217 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
2218 create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL);