1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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/>. */
29 #include "gdbthread.h"
37 #include <sys/types.h>
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46 #include "tid-parse.h"
49 /* Definition of struct thread_info exported to gdbthread.h. */
51 /* Prototypes for exported functions. */
53 void _initialize_thread (void);
55 /* Prototypes for local functions. */
57 struct thread_info *thread_list = NULL;
58 static int highest_thread_num;
60 /* True if any thread is, or may be executing. We need to track this
61 separately because until we fully sync the thread list, we won't
62 know whether the target is fully stopped, even if we see stop
63 events for all known threads, because any of those threads may have
64 spawned new threads we haven't heard of yet. */
65 static int threads_executing;
67 static void thread_apply_all_command (char *, int);
68 static int thread_alive (struct thread_info *);
69 static void info_threads_command (char *, int);
70 static void thread_apply_command (char *, int);
72 /* RAII type used to increase / decrease the refcount of each thread
73 in a given list of threads. */
75 class scoped_inc_dec_ref
78 explicit scoped_inc_dec_ref (const std::vector<thread_info *> &thrds)
81 for (thread_info *thr : m_thrds)
85 ~scoped_inc_dec_ref ()
87 for (thread_info *thr : m_thrds)
92 const std::vector<thread_info *> &m_thrds;
97 inferior_thread (void)
99 struct thread_info *tp = find_thread_ptid (inferior_ptid);
104 /* Delete the breakpoint pointed at by BP_P, if there's one. */
107 delete_thread_breakpoint (struct breakpoint **bp_p)
111 delete_breakpoint (*bp_p);
117 delete_step_resume_breakpoint (struct thread_info *tp)
120 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
124 delete_exception_resume_breakpoint (struct thread_info *tp)
127 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
130 /* See gdbthread.h. */
133 delete_single_step_breakpoints (struct thread_info *tp)
136 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
139 /* Delete the breakpoint pointed at by BP_P at the next stop, if
143 delete_at_next_stop (struct breakpoint **bp)
147 (*bp)->disposition = disp_del_at_next_stop;
152 /* See gdbthread.h. */
155 thread_has_single_step_breakpoints_set (struct thread_info *tp)
157 return tp->control.single_step_breakpoints != NULL;
160 /* See gdbthread.h. */
163 thread_has_single_step_breakpoint_here (struct thread_info *tp,
164 struct address_space *aspace,
167 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
169 return (ss_bps != NULL
170 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
173 /* See gdbthread.h. */
176 thread_cancel_execution_command (struct thread_info *thr)
178 if (thr->thread_fsm != NULL)
180 thread_fsm_clean_up (thr->thread_fsm, thr);
181 thread_fsm_delete (thr->thread_fsm);
182 thr->thread_fsm = NULL;
187 clear_thread_inferior_resources (struct thread_info *tp)
189 /* NOTE: this will take care of any left-over step_resume breakpoints,
190 but not any user-specified thread-specific breakpoints. We can not
191 delete the breakpoint straight-off, because the inferior might not
192 be stopped at the moment. */
193 delete_at_next_stop (&tp->control.step_resume_breakpoint);
194 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
195 delete_at_next_stop (&tp->control.single_step_breakpoints);
197 delete_longjmp_breakpoint_at_next_stop (tp->global_num);
199 bpstat_clear (&tp->control.stop_bpstat);
201 btrace_teardown (tp);
203 thread_cancel_execution_command (tp);
206 /* Set the TP's state as exited. */
209 set_thread_exited (thread_info *tp, int silent)
211 /* Dead threads don't need to step-over. Remove from queue. */
212 if (tp->step_over_next != NULL)
213 thread_step_over_chain_remove (tp);
215 if (tp->state != THREAD_EXITED)
217 observer_notify_thread_exit (tp, silent);
219 /* Tag it as exited. */
220 tp->state = THREAD_EXITED;
222 /* Clear breakpoints, etc. associated with this thread. */
223 clear_thread_inferior_resources (tp);
228 init_thread_list (void)
230 struct thread_info *tp, *tpnext;
232 highest_thread_num = 0;
237 for (tp = thread_list; tp; tp = tpnext)
240 if (tp->deletable ())
243 set_thread_exited (tp, 1);
247 threads_executing = 0;
250 /* Allocate a new thread of inferior INF with target id PTID and add
251 it to the thread list. */
253 static struct thread_info *
254 new_thread (struct inferior *inf, ptid_t ptid)
256 thread_info *tp = new thread_info (inf, ptid);
258 if (thread_list == NULL)
262 struct thread_info *last;
264 for (last = thread_list; last->next != NULL; last = last->next)
273 add_thread_silent (ptid_t ptid)
275 struct thread_info *tp;
276 struct inferior *inf = find_inferior_ptid (ptid);
277 gdb_assert (inf != NULL);
279 tp = find_thread_ptid (ptid);
281 /* Found an old thread with the same id. It has to be dead,
282 otherwise we wouldn't be adding a new thread with the same id.
283 The OS is reusing this id --- delete it, and recreate a new
286 /* In addition to deleting the thread, if this is the current
287 thread, then we need to take care that delete_thread doesn't
288 really delete the thread if it is inferior_ptid. Create a
289 new template thread in the list with an invalid ptid, switch
290 to it, delete the original thread, reset the new thread's
291 ptid, and switch to it. */
293 if (inferior_ptid == ptid)
295 tp = new_thread (inf, null_ptid);
297 /* Make switch_to_thread not read from the thread. */
298 tp->state = THREAD_EXITED;
299 switch_to_thread (null_ptid);
301 /* Now we can delete it. */
302 delete_thread (ptid);
304 /* Now reset its ptid, and reswitch inferior_ptid to it. */
306 tp->state = THREAD_STOPPED;
307 switch_to_thread (ptid);
309 observer_notify_new_thread (tp);
315 /* Just go ahead and delete it. */
316 delete_thread (ptid);
319 tp = new_thread (inf, ptid);
320 observer_notify_new_thread (tp);
326 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
328 struct thread_info *result = add_thread_silent (ptid);
332 if (print_thread_events)
333 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
335 annotate_new_thread ();
340 add_thread (ptid_t ptid)
342 return add_thread_with_info (ptid, NULL);
345 thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
346 : ptid (ptid_), inf (inf_)
348 gdb_assert (inf_ != NULL);
350 this->global_num = ++highest_thread_num;
351 this->per_inf_num = ++inf_->highest_thread_num;
353 /* Nothing to follow yet. */
354 memset (&this->pending_follow, 0, sizeof (this->pending_follow));
355 this->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
356 this->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
359 thread_info::~thread_info ()
363 if (this->private_dtor)
364 this->private_dtor (this->priv);
372 /* Add TP to the end of the step-over chain LIST_P. */
375 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
377 gdb_assert (tp->step_over_next == NULL);
378 gdb_assert (tp->step_over_prev == NULL);
383 tp->step_over_prev = tp->step_over_next = tp;
387 struct thread_info *head = *list_p;
388 struct thread_info *tail = head->step_over_prev;
390 tp->step_over_prev = tail;
391 tp->step_over_next = head;
392 head->step_over_prev = tp;
393 tail->step_over_next = tp;
397 /* Remove TP from step-over chain LIST_P. */
400 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
402 gdb_assert (tp->step_over_next != NULL);
403 gdb_assert (tp->step_over_prev != NULL);
407 if (tp == tp->step_over_next)
410 *list_p = tp->step_over_next;
413 tp->step_over_prev->step_over_next = tp->step_over_next;
414 tp->step_over_next->step_over_prev = tp->step_over_prev;
415 tp->step_over_prev = tp->step_over_next = NULL;
418 /* See gdbthread.h. */
421 thread_step_over_chain_next (struct thread_info *tp)
423 struct thread_info *next = tp->step_over_next;
425 return (next == step_over_queue_head ? NULL : next);
428 /* See gdbthread.h. */
431 thread_is_in_step_over_chain (struct thread_info *tp)
433 return (tp->step_over_next != NULL);
436 /* See gdbthread.h. */
439 thread_step_over_chain_enqueue (struct thread_info *tp)
441 step_over_chain_enqueue (&step_over_queue_head, tp);
444 /* See gdbthread.h. */
447 thread_step_over_chain_remove (struct thread_info *tp)
449 step_over_chain_remove (&step_over_queue_head, tp);
452 /* Delete thread PTID. If SILENT, don't notify the observer of this
455 delete_thread_1 (ptid_t ptid, int silent)
457 struct thread_info *tp, *tpprev;
461 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
462 if (tp->ptid == ptid)
468 set_thread_exited (tp, silent);
470 if (!tp->deletable ())
472 /* Will be really deleted some other time. */
477 tpprev->next = tp->next;
479 thread_list = tp->next;
484 /* Delete thread PTID and notify of thread exit. If this is
485 inferior_ptid, don't actually delete it, but tag it as exited and
486 do the notification. If PTID is the user selected thread, clear
489 delete_thread (ptid_t ptid)
491 delete_thread_1 (ptid, 0 /* not silent */);
495 delete_thread_silent (ptid_t ptid)
497 delete_thread_1 (ptid, 1 /* silent */);
501 find_thread_global_id (int global_id)
503 struct thread_info *tp;
505 for (tp = thread_list; tp; tp = tp->next)
506 if (tp->global_num == global_id)
512 static struct thread_info *
513 find_thread_id (struct inferior *inf, int thr_num)
515 struct thread_info *tp;
517 for (tp = thread_list; tp; tp = tp->next)
518 if (tp->inf == inf && tp->per_inf_num == thr_num)
524 /* Find a thread_info by matching PTID. */
526 find_thread_ptid (ptid_t ptid)
528 struct thread_info *tp;
530 for (tp = thread_list; tp; tp = tp->next)
531 if (tp->ptid == ptid)
538 * Thread iterator function.
540 * Calls a callback function once for each thread, so long as
541 * the callback function returns false. If the callback function
542 * returns true, the iteration will end and the current thread
543 * will be returned. This can be useful for implementing a
544 * search for a thread with arbitrary attributes, or for applying
545 * some operation to every thread.
547 * FIXME: some of the existing functionality, such as
548 * "Thread apply all", might be rewritten using this functionality.
552 iterate_over_threads (int (*callback) (struct thread_info *, void *),
555 struct thread_info *tp, *next;
557 for (tp = thread_list; tp; tp = next)
560 if ((*callback) (tp, data))
571 struct thread_info *tp;
573 for (tp = thread_list; tp; tp = tp->next)
579 /* Return the number of non-exited threads in the thread list. */
582 live_threads_count (void)
585 struct thread_info *tp;
587 ALL_NON_EXITED_THREADS (tp)
594 valid_global_thread_id (int global_id)
596 struct thread_info *tp;
598 for (tp = thread_list; tp; tp = tp->next)
599 if (tp->global_num == global_id)
606 ptid_to_global_thread_id (ptid_t ptid)
608 struct thread_info *tp;
610 for (tp = thread_list; tp; tp = tp->next)
611 if (tp->ptid == ptid)
612 return tp->global_num;
618 global_thread_id_to_ptid (int global_id)
620 struct thread_info *thread = find_thread_global_id (global_id);
625 return minus_one_ptid;
629 in_thread_list (ptid_t ptid)
631 struct thread_info *tp;
633 for (tp = thread_list; tp; tp = tp->next)
634 if (tp->ptid == ptid)
637 return 0; /* Never heard of 'im. */
640 /* Finds the first thread of the inferior given by PID. If PID is -1,
641 return the first thread in the list. */
644 first_thread_of_process (int pid)
646 struct thread_info *tp, *ret = NULL;
648 for (tp = thread_list; tp; tp = tp->next)
649 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
650 if (ret == NULL || tp->global_num < ret->global_num)
657 any_thread_of_process (int pid)
659 struct thread_info *tp;
661 gdb_assert (pid != 0);
663 /* Prefer the current thread. */
664 if (ptid_get_pid (inferior_ptid) == pid)
665 return inferior_thread ();
667 ALL_NON_EXITED_THREADS (tp)
668 if (ptid_get_pid (tp->ptid) == pid)
675 any_live_thread_of_process (int pid)
677 struct thread_info *curr_tp = NULL;
678 struct thread_info *tp;
679 struct thread_info *tp_executing = NULL;
681 gdb_assert (pid != 0);
683 /* Prefer the current thread if it's not executing. */
684 if (ptid_get_pid (inferior_ptid) == pid)
686 /* If the current thread is dead, forget it. If it's not
687 executing, use it. Otherwise, still choose it (below), but
688 only if no other non-executing thread is found. */
689 curr_tp = inferior_thread ();
690 if (curr_tp->state == THREAD_EXITED)
692 else if (!curr_tp->executing)
696 ALL_NON_EXITED_THREADS (tp)
697 if (ptid_get_pid (tp->ptid) == pid)
705 /* If both the current thread and all live threads are executing,
706 prefer the current thread. */
710 /* Otherwise, just return an executing thread, if any. */
714 /* Print a list of thread ids currently known, and the total number of
715 threads. To be used from within catch_errors. */
717 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
719 struct thread_info *tp;
721 struct cleanup *cleanup_chain;
722 int current_thread = -1;
724 update_thread_list ();
726 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
728 for (tp = thread_list; tp; tp = tp->next)
730 if (tp->state == THREAD_EXITED)
733 if (tp->ptid == inferior_ptid)
734 current_thread = tp->global_num;
737 uiout->field_int ("thread-id", tp->global_num);
740 do_cleanups (cleanup_chain);
742 if (current_thread != -1)
743 uiout->field_int ("current-thread-id", current_thread);
744 uiout->field_int ("number-of-threads", num);
748 /* Official gdblib interface function to get a list of thread ids and
751 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
753 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
754 error_message, RETURN_MASK_ALL) < 0)
759 /* Return true if TP is an active thread. */
761 thread_alive (struct thread_info *tp)
763 if (tp->state == THREAD_EXITED)
765 if (!target_thread_alive (tp->ptid))
770 /* See gdbthreads.h. */
775 struct thread_info *tp, *tmp;
777 ALL_THREADS_SAFE (tp, tmp)
779 if (!thread_alive (tp))
780 delete_thread (tp->ptid);
784 /* See gdbthreads.h. */
787 delete_exited_threads (void)
789 struct thread_info *tp, *tmp;
791 ALL_THREADS_SAFE (tp, tmp)
793 if (tp->state == THREAD_EXITED)
794 delete_thread (tp->ptid);
798 /* Disable storing stack temporaries for the thread whose id is
802 disable_thread_stack_temporaries (void *data)
804 ptid_t *pd = (ptid_t *) data;
805 struct thread_info *tp = find_thread_ptid (*pd);
809 tp->stack_temporaries_enabled = 0;
810 VEC_free (value_ptr, tp->stack_temporaries);
816 /* Enable storing stack temporaries for thread with id PTID and return a
817 cleanup which can disable and clear the stack temporaries. */
820 enable_thread_stack_temporaries (ptid_t ptid)
822 struct thread_info *tp = find_thread_ptid (ptid);
826 gdb_assert (tp != NULL);
828 tp->stack_temporaries_enabled = 1;
829 tp->stack_temporaries = NULL;
830 data = XNEW (ptid_t);
832 c = make_cleanup (disable_thread_stack_temporaries, data);
837 /* Return non-zero value if stack temporaies are enabled for the thread
841 thread_stack_temporaries_enabled_p (ptid_t ptid)
843 struct thread_info *tp = find_thread_ptid (ptid);
848 return tp->stack_temporaries_enabled;
851 /* Push V on to the stack temporaries of the thread with id PTID. */
854 push_thread_stack_temporary (ptid_t ptid, struct value *v)
856 struct thread_info *tp = find_thread_ptid (ptid);
858 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
859 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
862 /* Return 1 if VAL is among the stack temporaries of the thread
863 with id PTID. Return 0 otherwise. */
866 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
868 struct thread_info *tp = find_thread_ptid (ptid);
870 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
871 if (!VEC_empty (value_ptr, tp->stack_temporaries))
876 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
884 /* Return the last of the stack temporaries for thread with id PTID.
885 Return NULL if there are no stack temporaries for the thread. */
888 get_last_thread_stack_temporary (ptid_t ptid)
890 struct value *lastval = NULL;
891 struct thread_info *tp = find_thread_ptid (ptid);
893 gdb_assert (tp != NULL);
894 if (!VEC_empty (value_ptr, tp->stack_temporaries))
895 lastval = VEC_last (value_ptr, tp->stack_temporaries);
901 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
903 struct inferior *inf;
904 struct thread_info *tp;
906 /* It can happen that what we knew as the target inferior id
907 changes. E.g, target remote may only discover the remote process
908 pid after adding the inferior to GDB's list. */
909 inf = find_inferior_ptid (old_ptid);
910 inf->pid = ptid_get_pid (new_ptid);
912 tp = find_thread_ptid (old_ptid);
915 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
918 /* See gdbthread.h. */
921 set_resumed (ptid_t ptid, int resumed)
923 struct thread_info *tp;
924 int all = ptid == minus_one_ptid;
926 if (all || ptid_is_pid (ptid))
928 for (tp = thread_list; tp; tp = tp->next)
929 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
930 tp->resumed = resumed;
934 tp = find_thread_ptid (ptid);
935 gdb_assert (tp != NULL);
936 tp->resumed = resumed;
940 /* Helper for set_running, that marks one thread either running or
944 set_running_thread (struct thread_info *tp, int running)
948 if (running && tp->state == THREAD_STOPPED)
950 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
954 /* If the thread is now marked stopped, remove it from
955 the step-over queue, so that we don't try to resume
956 it until the user wants it to. */
957 if (tp->step_over_next != NULL)
958 thread_step_over_chain_remove (tp);
965 set_running (ptid_t ptid, int running)
967 struct thread_info *tp;
968 int all = ptid == minus_one_ptid;
971 /* We try not to notify the observer if no thread has actually changed
972 the running state -- merely to reduce the number of messages to
973 frontend. Frontend is supposed to handle multiple *running just fine. */
974 if (all || ptid_is_pid (ptid))
976 for (tp = thread_list; tp; tp = tp->next)
977 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
979 if (tp->state == THREAD_EXITED)
982 if (set_running_thread (tp, running))
988 tp = find_thread_ptid (ptid);
989 gdb_assert (tp != NULL);
990 gdb_assert (tp->state != THREAD_EXITED);
991 if (set_running_thread (tp, running))
995 observer_notify_target_resumed (ptid);
999 is_thread_state (ptid_t ptid, enum thread_state state)
1001 struct thread_info *tp;
1003 tp = find_thread_ptid (ptid);
1005 return tp->state == state;
1009 is_stopped (ptid_t ptid)
1011 return is_thread_state (ptid, THREAD_STOPPED);
1015 is_exited (ptid_t ptid)
1017 return is_thread_state (ptid, THREAD_EXITED);
1021 is_running (ptid_t ptid)
1023 return is_thread_state (ptid, THREAD_RUNNING);
1027 is_executing (ptid_t ptid)
1029 struct thread_info *tp;
1031 tp = find_thread_ptid (ptid);
1033 return tp->executing;
1037 set_executing (ptid_t ptid, int executing)
1039 struct thread_info *tp;
1040 int all = ptid == minus_one_ptid;
1042 if (all || ptid_is_pid (ptid))
1044 for (tp = thread_list; tp; tp = tp->next)
1045 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1046 tp->executing = executing;
1050 tp = find_thread_ptid (ptid);
1052 tp->executing = executing;
1055 /* It only takes one running thread to spawn more threads.*/
1057 threads_executing = 1;
1058 /* Only clear the flag if the caller is telling us everything is
1060 else if (minus_one_ptid == ptid)
1061 threads_executing = 0;
1064 /* See gdbthread.h. */
1067 threads_are_executing (void)
1069 return threads_executing;
1073 set_stop_requested (ptid_t ptid, int stop)
1075 struct thread_info *tp;
1076 int all = ptid == minus_one_ptid;
1078 if (all || ptid_is_pid (ptid))
1080 for (tp = thread_list; tp; tp = tp->next)
1081 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1082 tp->stop_requested = stop;
1086 tp = find_thread_ptid (ptid);
1088 tp->stop_requested = stop;
1091 /* Call the stop requested observer so other components of GDB can
1092 react to this request. */
1094 observer_notify_thread_stop_requested (ptid);
1098 finish_thread_state (ptid_t ptid)
1100 struct thread_info *tp;
1102 int any_started = 0;
1104 all = ptid == minus_one_ptid;
1106 if (all || ptid_is_pid (ptid))
1108 for (tp = thread_list; tp; tp = tp->next)
1110 if (tp->state == THREAD_EXITED)
1112 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1114 if (set_running_thread (tp, tp->executing))
1121 tp = find_thread_ptid (ptid);
1123 if (tp->state != THREAD_EXITED)
1125 if (set_running_thread (tp, tp->executing))
1131 observer_notify_target_resumed (ptid);
1135 finish_thread_state_cleanup (void *arg)
1137 ptid_t *ptid_p = (ptid_t *) arg;
1141 finish_thread_state (*ptid_p);
1144 /* See gdbthread.h. */
1147 validate_registers_access (void)
1149 /* No selected thread, no registers. */
1150 if (inferior_ptid == null_ptid)
1151 error (_("No thread selected."));
1153 /* Don't try to read from a dead thread. */
1154 if (is_exited (inferior_ptid))
1155 error (_("The current thread has terminated"));
1157 /* ... or from a spinning thread. FIXME: This isn't actually fully
1158 correct. It'll allow an user-requested access (e.g., "print $pc"
1159 at the prompt) when a thread is not executing for some internal
1160 reason, but is marked running from the user's perspective. E.g.,
1161 the thread is waiting for its turn in the step-over queue. */
1162 if (is_executing (inferior_ptid))
1163 error (_("Selected thread is running."));
1166 /* See gdbthread.h. */
1169 can_access_registers_ptid (ptid_t ptid)
1171 /* No thread, no registers. */
1172 if (ptid == null_ptid)
1175 /* Don't try to read from a dead thread. */
1176 if (is_exited (ptid))
1179 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1180 if (is_executing (ptid))
1187 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1189 return (pc >= thread->control.step_range_start
1190 && pc < thread->control.step_range_end);
1193 /* Helper for print_thread_info. Returns true if THR should be
1194 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1195 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1196 is true if REQUESTED_THREADS is list of global IDs, false if a list
1197 of per-inferior thread ids. If PID is not -1, only print THR if it
1198 is a thread from the process PID. Otherwise, threads from all
1199 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1200 and PID is not -1, then the thread is printed if it belongs to the
1201 specified process. Otherwise, an error is raised. */
1204 should_print_thread (const char *requested_threads, int default_inf_num,
1205 int global_ids, int pid, struct thread_info *thr)
1207 if (requested_threads != NULL && *requested_threads != '\0')
1212 in_list = number_is_in_list (requested_threads, thr->global_num);
1214 in_list = tid_is_in_list (requested_threads, default_inf_num,
1215 thr->inf->num, thr->per_inf_num);
1220 if (pid != -1 && ptid_get_pid (thr->ptid) != pid)
1222 if (requested_threads != NULL && *requested_threads != '\0')
1223 error (_("Requested thread not found in requested process"));
1227 if (thr->state == THREAD_EXITED)
1233 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1234 whether REQUESTED_THREADS is a list of global or per-inferior
1238 print_thread_info_1 (struct ui_out *uiout, char *requested_threads,
1239 int global_ids, int pid,
1240 int show_global_ids)
1242 struct thread_info *tp;
1243 ptid_t current_ptid;
1244 struct cleanup *old_chain;
1245 const char *extra_info, *name, *target_id;
1246 struct inferior *inf;
1247 int default_inf_num = current_inferior ()->num;
1249 update_thread_list ();
1250 current_ptid = inferior_ptid;
1252 /* We'll be switching threads temporarily. */
1253 old_chain = make_cleanup_restore_current_thread ();
1255 /* For backward compatibility, we make a list for MI. A table is
1256 preferable for the CLI, though, because it shows table
1258 if (uiout->is_mi_like_p ())
1259 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1264 for (tp = thread_list; tp; tp = tp->next)
1266 if (!should_print_thread (requested_threads, default_inf_num,
1267 global_ids, pid, tp))
1275 if (requested_threads == NULL || *requested_threads == '\0')
1276 uiout->message (_("No threads.\n"));
1278 uiout->message (_("No threads match '%s'.\n"),
1280 do_cleanups (old_chain);
1284 if (show_global_ids || uiout->is_mi_like_p ())
1285 make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads");
1287 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1289 uiout->table_header (1, ui_left, "current", "");
1291 if (!uiout->is_mi_like_p ())
1292 uiout->table_header (4, ui_left, "id-in-tg", "Id");
1293 if (show_global_ids || uiout->is_mi_like_p ())
1294 uiout->table_header (4, ui_left, "id", "GId");
1295 uiout->table_header (17, ui_left, "target-id", "Target Id");
1296 uiout->table_header (1, ui_left, "frame", "Frame");
1297 uiout->table_body ();
1300 ALL_THREADS_BY_INFERIOR (inf, tp)
1302 struct cleanup *chain2;
1305 if (!should_print_thread (requested_threads, default_inf_num,
1306 global_ids, pid, tp))
1309 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1311 if (!uiout->is_mi_like_p ())
1313 if (tp->ptid == current_ptid)
1314 uiout->field_string ("current", "*");
1316 uiout->field_skip ("current");
1319 if (!uiout->is_mi_like_p ())
1320 uiout->field_string ("id-in-tg", print_thread_id (tp));
1322 if (show_global_ids || uiout->is_mi_like_p ())
1323 uiout->field_int ("id", tp->global_num);
1325 /* For the CLI, we stuff everything into the target-id field.
1326 This is a gross hack to make the output come out looking
1327 correct. The underlying problem here is that ui-out has no
1328 way to specify that a field's space allocation should be
1329 shared by several fields. For MI, we do the right thing
1332 target_id = target_pid_to_str (tp->ptid);
1333 extra_info = target_extra_thread_info (tp);
1334 name = tp->name ? tp->name : target_thread_name (tp);
1336 if (uiout->is_mi_like_p ())
1338 uiout->field_string ("target-id", target_id);
1340 uiout->field_string ("details", extra_info);
1342 uiout->field_string ("name", name);
1346 struct cleanup *str_cleanup;
1349 if (extra_info && name)
1350 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1352 else if (extra_info)
1353 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1355 contents = xstrprintf ("%s \"%s\"", target_id, name);
1357 contents = xstrdup (target_id);
1358 str_cleanup = make_cleanup (xfree, contents);
1360 uiout->field_string ("target-id", contents);
1361 do_cleanups (str_cleanup);
1364 if (tp->state == THREAD_RUNNING)
1365 uiout->text ("(running)\n");
1368 /* The switch below puts us at the top of the stack (leaf
1370 switch_to_thread (tp->ptid);
1371 print_stack_frame (get_selected_frame (NULL),
1372 /* For MI output, print frame level. */
1373 uiout->is_mi_like_p (),
1377 if (uiout->is_mi_like_p ())
1379 const char *state = "stopped";
1381 if (tp->state == THREAD_RUNNING)
1383 uiout->field_string ("state", state);
1386 core = target_core_of_thread (tp->ptid);
1387 if (uiout->is_mi_like_p () && core != -1)
1388 uiout->field_int ("core", core);
1390 do_cleanups (chain2);
1393 /* Restores the current thread and the frame selected before
1394 the "info threads" command. */
1395 do_cleanups (old_chain);
1397 if (pid == -1 && requested_threads == NULL)
1399 if (uiout->is_mi_like_p ()
1400 && inferior_ptid != null_ptid)
1402 int num = ptid_to_global_thread_id (inferior_ptid);
1404 gdb_assert (num != 0);
1405 uiout->field_int ("current-thread-id", num);
1408 if (inferior_ptid != null_ptid && is_exited (inferior_ptid))
1409 uiout->message ("\n\
1410 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1411 print_thread_id (inferior_thread ()));
1412 else if (thread_list != NULL && inferior_ptid == null_ptid)
1413 uiout->message ("\n\
1414 No selected thread. See `help thread'.\n");
1418 /* See gdbthread.h. */
1421 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1423 print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
1426 /* Implementation of the "info threads" command.
1428 Note: this has the drawback that it _really_ switches
1429 threads, which frees the frame cache. A no-side
1430 effects info-threads command would be nicer. */
1433 info_threads_command (char *arg, int from_tty)
1435 int show_global_ids = 0;
1438 && check_for_argument (&arg, "-gid", sizeof ("-gid") - 1))
1440 arg = skip_spaces (arg);
1441 show_global_ids = 1;
1444 print_thread_info_1 (current_uiout, arg, 0, -1, show_global_ids);
1447 /* See gdbthread.h. */
1450 switch_to_thread_no_regs (struct thread_info *thread)
1452 struct inferior *inf = thread->inf;
1454 set_current_program_space (inf->pspace);
1455 set_current_inferior (inf);
1457 inferior_ptid = thread->ptid;
1458 stop_pc = ~(CORE_ADDR) 0;
1461 /* Switch to no thread selected. */
1464 switch_to_no_thread ()
1466 if (inferior_ptid == null_ptid)
1469 inferior_ptid = null_ptid;
1470 reinit_frame_cache ();
1471 stop_pc = ~(CORE_ADDR) 0;
1474 /* Switch from one thread to another. */
1477 switch_to_thread (thread_info *thr)
1479 gdb_assert (thr != NULL);
1481 if (inferior_ptid == thr->ptid)
1484 switch_to_thread_no_regs (thr);
1486 reinit_frame_cache ();
1488 /* We don't check for is_stopped, because we're called at times
1489 while in the TARGET_RUNNING state, e.g., while handling an
1491 if (thr->state != THREAD_EXITED
1493 stop_pc = regcache_read_pc (get_thread_regcache (thr->ptid));
1496 /* See gdbthread.h. */
1499 switch_to_thread (ptid_t ptid)
1501 if (ptid == null_ptid)
1502 switch_to_no_thread ();
1504 switch_to_thread (find_thread_ptid (ptid));
1508 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1510 struct frame_info *frame = NULL;
1513 /* This means there was no selected frame. */
1514 if (frame_level == -1)
1516 select_frame (NULL);
1520 gdb_assert (frame_level >= 0);
1522 /* Restore by level first, check if the frame id is the same as
1523 expected. If that fails, try restoring by frame id. If that
1524 fails, nothing to do, just warn the user. */
1526 count = frame_level;
1527 frame = find_relative_frame (get_current_frame (), &count);
1530 /* The frame ids must match - either both valid or both outer_frame_id.
1531 The latter case is not failsafe, but since it's highly unlikely
1532 the search by level finds the wrong frame, it's 99.9(9)% of
1533 the time (for all practical purposes) safe. */
1534 && frame_id_eq (get_frame_id (frame), a_frame_id))
1536 /* Cool, all is fine. */
1537 select_frame (frame);
1541 frame = frame_find_by_id (a_frame_id);
1544 /* Cool, refound it. */
1545 select_frame (frame);
1549 /* Nothing else to do, the frame layout really changed. Select the
1550 innermost stack frame. */
1551 select_frame (get_current_frame ());
1553 /* Warn the user. */
1554 if (frame_level > 0 && !current_uiout->is_mi_like_p ())
1556 warning (_("Couldn't restore frame #%d in "
1557 "current thread. Bottom (innermost) frame selected:"),
1559 /* For MI, we should probably have a notification about
1560 current frame change. But this error is not very
1561 likely, so don't bother for now. */
1562 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1566 /* Data used by the cleanup installed by
1567 'make_cleanup_restore_current_thread'. */
1569 struct current_thread_cleanup
1571 thread_info *thread;
1572 struct frame_id selected_frame_id;
1573 int selected_frame_level;
1579 do_restore_current_thread_cleanup (void *arg)
1581 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1583 /* If an entry of thread_info was previously selected, it won't be
1584 deleted because we've increased its refcount. The thread represented
1585 by this thread_info entry may have already exited (due to normal exit,
1586 detach, etc), so the thread_info.state is THREAD_EXITED. */
1587 if (old->thread != NULL
1588 /* If the previously selected thread belonged to a process that has
1589 in the mean time exited (or killed, detached, etc.), then don't revert
1590 back to it, but instead simply drop back to no thread selected. */
1591 && old->inf->pid != 0)
1592 switch_to_thread (old->thread);
1595 switch_to_no_thread ();
1596 set_current_inferior (old->inf);
1599 /* The running state of the originally selected thread may have
1600 changed, so we have to recheck it here. */
1601 if (inferior_ptid != null_ptid
1603 && is_stopped (inferior_ptid)
1604 && target_has_registers
1606 && target_has_memory)
1607 restore_selected_frame (old->selected_frame_id,
1608 old->selected_frame_level);
1612 restore_current_thread_cleanup_dtor (void *arg)
1614 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1616 if (old->thread != NULL)
1617 old->thread->decref ();
1619 old->inf->decref ();
1624 make_cleanup_restore_current_thread (void)
1626 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1629 old->inf = current_inferior ();
1631 if (inferior_ptid != null_ptid)
1633 struct frame_info *frame;
1635 old->was_stopped = is_stopped (inferior_ptid);
1636 if (old->was_stopped
1637 && target_has_registers
1639 && target_has_memory)
1641 /* When processing internal events, there might not be a
1642 selected frame. If we naively call get_selected_frame
1643 here, then we can end up reading debuginfo for the
1644 current frame, but we don't generally need the debuginfo
1646 frame = get_selected_frame_if_set ();
1651 old->selected_frame_id = get_frame_id (frame);
1652 old->selected_frame_level = frame_relative_level (frame);
1654 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1661 old->inf->incref ();
1663 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1664 restore_current_thread_cleanup_dtor);
1667 /* See gdbthread.h. */
1670 show_thread_that_caused_stop (void)
1672 return highest_thread_num > 1;
1675 /* See gdbthread.h. */
1678 show_inferior_qualified_tids (void)
1680 return (inferior_list->next != NULL || inferior_list->num != 1);
1683 /* See gdbthread.h. */
1686 print_thread_id (struct thread_info *thr)
1688 char *s = get_print_cell ();
1690 if (show_inferior_qualified_tids ())
1691 xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num);
1693 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num);
1697 /* If true, tp_array_compar should sort in ascending order, otherwise
1698 in descending order. */
1700 static bool tp_array_compar_ascending;
1702 /* Sort an array for struct thread_info pointers by thread ID (first
1703 by inferior number, and then by per-inferior thread number). The
1704 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1707 tp_array_compar (const thread_info *a, const thread_info *b)
1709 if (a->inf->num != b->inf->num)
1711 if (tp_array_compar_ascending)
1712 return a->inf->num < b->inf->num;
1714 return a->inf->num > b->inf->num;
1717 if (tp_array_compar_ascending)
1718 return (a->per_inf_num < b->per_inf_num);
1720 return (a->per_inf_num > b->per_inf_num);
1723 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1724 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1725 of two numbers seperated by a hyphen. Examples:
1727 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1728 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1729 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1732 thread_apply_all_command (char *cmd, int from_tty)
1734 struct cleanup *old_chain;
1737 tp_array_compar_ascending = false;
1739 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1741 cmd = skip_spaces (cmd);
1742 tp_array_compar_ascending = true;
1745 if (cmd == NULL || *cmd == '\000')
1746 error (_("Please specify a command following the thread ID list"));
1748 update_thread_list ();
1750 old_chain = make_cleanup_restore_current_thread ();
1752 /* Save a copy of the command in case it is clobbered by
1754 saved_cmd = xstrdup (cmd);
1755 make_cleanup (xfree, saved_cmd);
1757 int tc = live_threads_count ();
1760 /* Save a copy of the thread list and increment each thread's
1761 refcount while executing the command in the context of each
1762 thread, in case the command is one that wipes threads. E.g.,
1763 detach, kill, disconnect, etc., or even normally continuing
1764 over an inferior or thread exit. */
1765 std::vector<thread_info *> thr_list_cpy;
1766 thr_list_cpy.reserve (tc);
1771 ALL_NON_EXITED_THREADS (tp)
1773 thr_list_cpy.push_back (tp);
1776 gdb_assert (thr_list_cpy.size () == tc);
1779 /* Increment the refcounts, and restore them back on scope
1781 scoped_inc_dec_ref inc_dec_ref (thr_list_cpy);
1783 std::sort (thr_list_cpy.begin (), thr_list_cpy.end (), tp_array_compar);
1785 for (thread_info *thr : thr_list_cpy)
1786 if (thread_alive (thr))
1788 switch_to_thread (thr->ptid);
1789 printf_filtered (_("\nThread %s (%s):\n"),
1790 print_thread_id (thr),
1791 target_pid_to_str (inferior_ptid));
1792 execute_command (cmd, from_tty);
1794 /* Restore exact command used previously. */
1795 strcpy (cmd, saved_cmd);
1799 do_cleanups (old_chain);
1802 /* Implementation of the "thread apply" command. */
1805 thread_apply_command (char *tidlist, int from_tty)
1808 struct cleanup *old_chain;
1810 tid_range_parser parser;
1812 if (tidlist == NULL || *tidlist == '\000')
1813 error (_("Please specify a thread ID list"));
1815 parser.init (tidlist, current_inferior ()->num);
1816 while (!parser.finished ())
1818 int inf_num, thr_start, thr_end;
1820 if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
1822 cmd = (char *) parser.cur_tok ();
1828 error (_("Please specify a command following the thread ID list"));
1830 if (tidlist == cmd || !isalpha (cmd[0]))
1831 invalid_thread_id_error (cmd);
1833 /* Save a copy of the command in case it is clobbered by
1835 saved_cmd = xstrdup (cmd);
1836 old_chain = make_cleanup (xfree, saved_cmd);
1838 make_cleanup_restore_current_thread ();
1840 parser.init (tidlist, current_inferior ()->num);
1841 while (!parser.finished () && parser.cur_tok () < cmd)
1843 struct thread_info *tp = NULL;
1844 struct inferior *inf;
1845 int inf_num, thr_num;
1847 parser.get_tid (&inf_num, &thr_num);
1848 inf = find_inferior_id (inf_num);
1850 tp = find_thread_id (inf, thr_num);
1852 if (parser.in_star_range ())
1856 warning (_("Unknown inferior %d"), inf_num);
1857 parser.skip_range ();
1861 /* No use looking for threads past the highest thread number
1862 the inferior ever had. */
1863 if (thr_num >= inf->highest_thread_num)
1864 parser.skip_range ();
1866 /* Be quiet about unknown threads numbers. */
1873 if (show_inferior_qualified_tids () || parser.tid_is_qualified ())
1874 warning (_("Unknown thread %d.%d"), inf_num, thr_num);
1876 warning (_("Unknown thread %d"), thr_num);
1880 if (!thread_alive (tp))
1882 warning (_("Thread %s has terminated."), print_thread_id (tp));
1886 switch_to_thread (tp->ptid);
1888 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp),
1889 target_pid_to_str (inferior_ptid));
1890 execute_command (cmd, from_tty);
1892 /* Restore exact command used previously. */
1893 strcpy (cmd, saved_cmd);
1896 do_cleanups (old_chain);
1899 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1900 if prefix of arg is `apply'. */
1903 thread_command (char *tidstr, int from_tty)
1907 if (inferior_ptid == null_ptid)
1908 error (_("No thread selected"));
1910 if (target_has_stack)
1912 struct thread_info *tp = inferior_thread ();
1914 if (is_exited (inferior_ptid))
1915 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1916 print_thread_id (tp),
1917 target_pid_to_str (inferior_ptid));
1919 printf_filtered (_("[Current thread is %s (%s)]\n"),
1920 print_thread_id (tp),
1921 target_pid_to_str (inferior_ptid));
1924 error (_("No stack."));
1928 ptid_t previous_ptid = inferior_ptid;
1931 result = gdb_thread_select (current_uiout, tidstr, NULL);
1933 /* If thread switch did not succeed don't notify or print. */
1934 if (result == GDB_RC_FAIL)
1937 /* Print if the thread has not changed, otherwise an event will
1939 if (inferior_ptid == previous_ptid)
1941 print_selected_thread_frame (current_uiout,
1942 USER_SELECTED_THREAD
1943 | USER_SELECTED_FRAME);
1947 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1948 | USER_SELECTED_FRAME);
1953 /* Implementation of `thread name'. */
1956 thread_name_command (char *arg, int from_tty)
1958 struct thread_info *info;
1960 if (inferior_ptid == null_ptid)
1961 error (_("No thread selected"));
1963 arg = skip_spaces (arg);
1965 info = inferior_thread ();
1967 info->name = arg ? xstrdup (arg) : NULL;
1970 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1973 thread_find_command (char *arg, int from_tty)
1975 struct thread_info *tp;
1977 unsigned long match = 0;
1979 if (arg == NULL || *arg == '\0')
1980 error (_("Command requires an argument."));
1982 tmp = re_comp (arg);
1984 error (_("Invalid regexp (%s): %s"), tmp, arg);
1986 update_thread_list ();
1987 for (tp = thread_list; tp; tp = tp->next)
1989 if (tp->name != NULL && re_exec (tp->name))
1991 printf_filtered (_("Thread %s has name '%s'\n"),
1992 print_thread_id (tp), tp->name);
1996 tmp = target_thread_name (tp);
1997 if (tmp != NULL && re_exec (tmp))
1999 printf_filtered (_("Thread %s has target name '%s'\n"),
2000 print_thread_id (tp), tmp);
2004 tmp = target_pid_to_str (tp->ptid);
2005 if (tmp != NULL && re_exec (tmp))
2007 printf_filtered (_("Thread %s has target id '%s'\n"),
2008 print_thread_id (tp), tmp);
2012 tmp = target_extra_thread_info (tp);
2013 if (tmp != NULL && re_exec (tmp))
2015 printf_filtered (_("Thread %s has extra info '%s'\n"),
2016 print_thread_id (tp), tmp);
2021 printf_filtered (_("No threads match '%s'\n"), arg);
2024 /* Print notices when new threads are attached and detached. */
2025 int print_thread_events = 1;
2027 show_print_thread_events (struct ui_file *file, int from_tty,
2028 struct cmd_list_element *c, const char *value)
2030 fprintf_filtered (file,
2031 _("Printing of thread events is %s.\n"),
2036 do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
2038 const char *tidstr = (const char *) tidstr_v;
2039 struct thread_info *tp;
2041 if (uiout->is_mi_like_p ())
2043 int num = value_as_long (parse_and_eval (tidstr));
2045 tp = find_thread_global_id (num);
2047 error (_("Thread ID %d not known."), num);
2051 tp = parse_thread_id (tidstr, NULL);
2052 gdb_assert (tp != NULL);
2055 if (!thread_alive (tp))
2056 error (_("Thread ID %s has terminated."), tidstr);
2058 switch_to_thread (tp->ptid);
2060 annotate_thread_changed ();
2062 /* Since the current thread may have changed, see if there is any
2063 exited thread we can now delete. */
2069 /* Print thread and frame switch command response. */
2072 print_selected_thread_frame (struct ui_out *uiout,
2073 user_selected_what selection)
2075 struct thread_info *tp = inferior_thread ();
2076 struct inferior *inf = current_inferior ();
2078 if (selection & USER_SELECTED_THREAD)
2080 if (uiout->is_mi_like_p ())
2082 uiout->field_int ("new-thread-id",
2083 inferior_thread ()->global_num);
2087 uiout->text ("[Switching to thread ");
2088 uiout->field_string ("new-thread-id", print_thread_id (tp));
2090 uiout->text (target_pid_to_str (inferior_ptid));
2095 if (tp->state == THREAD_RUNNING)
2097 if (selection & USER_SELECTED_THREAD)
2098 uiout->text ("(running)\n");
2100 else if (selection & USER_SELECTED_FRAME)
2102 if (selection & USER_SELECTED_THREAD)
2105 if (has_stack_frames ())
2106 print_stack_frame_to_uiout (uiout, get_selected_frame (NULL),
2112 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
2114 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
2115 error_message, RETURN_MASK_ALL) < 0)
2120 /* Update the 'threads_executing' global based on the threads we know
2124 update_threads_executing (void)
2126 struct thread_info *tp;
2128 threads_executing = 0;
2129 ALL_NON_EXITED_THREADS (tp)
2133 threads_executing = 1;
2140 update_thread_list (void)
2142 target_update_thread_list ();
2143 update_threads_executing ();
2146 /* Return a new value for the selected thread's id. Return a value of
2147 0 if no thread is selected. If GLOBAL is true, return the thread's
2148 global number. Otherwise return the per-inferior number. */
2150 static struct value *
2151 thread_num_make_value_helper (struct gdbarch *gdbarch, int global)
2153 struct thread_info *tp = find_thread_ptid (inferior_ptid);
2159 int_val = tp->global_num;
2161 int_val = tp->per_inf_num;
2163 return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val);
2166 /* Return a new value for the selected thread's per-inferior thread
2167 number. Return a value of 0 if no thread is selected, or no
2170 static struct value *
2171 thread_id_per_inf_num_make_value (struct gdbarch *gdbarch,
2172 struct internalvar *var,
2175 return thread_num_make_value_helper (gdbarch, 0);
2178 /* Return a new value for the selected thread's global id. Return a
2179 value of 0 if no thread is selected, or no threads exist. */
2181 static struct value *
2182 global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2185 return thread_num_make_value_helper (gdbarch, 1);
2188 /* Commands with a prefix of `thread'. */
2189 struct cmd_list_element *thread_cmd_list = NULL;
2191 /* Implementation of `thread' variable. */
2193 static const struct internalvar_funcs thread_funcs =
2195 thread_id_per_inf_num_make_value,
2200 /* Implementation of `gthread' variable. */
2202 static const struct internalvar_funcs gthread_funcs =
2204 global_thread_id_make_value,
2210 _initialize_thread (void)
2212 static struct cmd_list_element *thread_apply_list = NULL;
2214 add_info ("threads", info_threads_command,
2215 _("Display currently known threads.\n\
2216 Usage: info threads [-gid] [ID]...\n\
2217 -gid: Show global thread IDs.\n\
2218 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2219 Otherwise, all threads are displayed."));
2221 add_prefix_cmd ("thread", class_run, thread_command, _("\
2222 Use this command to switch between threads.\n\
2223 The new thread ID must be currently known."),
2224 &thread_cmd_list, "thread ", 1, &cmdlist);
2226 add_prefix_cmd ("apply", class_run, thread_apply_command,
2227 _("Apply a command to a list of threads."),
2228 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
2230 add_cmd ("all", class_run, thread_apply_all_command,
2232 Apply a command to all threads.\n\
2234 Usage: thread apply all [-ascending] <command>\n\
2235 -ascending: Call <command> for all threads in ascending order.\n\
2236 The default is descending order.\
2238 &thread_apply_list);
2240 add_cmd ("name", class_run, thread_name_command,
2241 _("Set the current thread's name.\n\
2242 Usage: thread name [NAME]\n\
2243 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2245 add_cmd ("find", class_run, thread_find_command, _("\
2246 Find threads that match a regular expression.\n\
2247 Usage: thread find REGEXP\n\
2248 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2251 add_com_alias ("t", "thread", class_run, 1);
2253 add_setshow_boolean_cmd ("thread-events", no_class,
2254 &print_thread_events, _("\
2255 Set printing of thread events (such as thread start and exit)."), _("\
2256 Show printing of thread events (such as thread start and exit)."), NULL,
2258 show_print_thread_events,
2259 &setprintlist, &showprintlist);
2261 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
2262 create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL);