1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2016 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"
48 /* Definition of struct thread_info exported to gdbthread.h. */
50 /* Prototypes for exported functions. */
52 void _initialize_thread (void);
54 /* Prototypes for local functions. */
56 struct thread_info *thread_list = NULL;
57 static int highest_thread_num;
59 /* True if any thread is, or may be executing. We need to track this
60 separately because until we fully sync the thread list, we won't
61 know whether the target is fully stopped, even if we see stop
62 events for all known threads, because any of those threads may have
63 spawned new threads we haven't heard of yet. */
64 static int threads_executing;
66 static void thread_apply_all_command (char *, int);
67 static int thread_alive (struct thread_info *);
68 static void info_threads_command (char *, int);
69 static void thread_apply_command (char *, int);
70 static void restore_current_thread (ptid_t);
72 /* Data to cleanup thread array. */
74 struct thread_array_cleanup
76 /* Array of thread pointers used to set
78 struct thread_info **tp_array;
80 /* Thread count in the array. */
86 inferior_thread (void)
88 struct thread_info *tp = find_thread_ptid (inferior_ptid);
93 /* Delete the breakpoint pointed at by BP_P, if there's one. */
96 delete_thread_breakpoint (struct breakpoint **bp_p)
100 delete_breakpoint (*bp_p);
106 delete_step_resume_breakpoint (struct thread_info *tp)
109 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
113 delete_exception_resume_breakpoint (struct thread_info *tp)
116 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
119 /* See gdbthread.h. */
122 delete_single_step_breakpoints (struct thread_info *tp)
125 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
128 /* Delete the breakpoint pointed at by BP_P at the next stop, if
132 delete_at_next_stop (struct breakpoint **bp)
136 (*bp)->disposition = disp_del_at_next_stop;
141 /* See gdbthread.h. */
144 thread_has_single_step_breakpoints_set (struct thread_info *tp)
146 return tp->control.single_step_breakpoints != NULL;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoint_here (struct thread_info *tp,
153 struct address_space *aspace,
156 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
158 return (ss_bps != NULL
159 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
162 /* See gdbthread.h. */
165 thread_cancel_execution_command (struct thread_info *thr)
167 if (thr->thread_fsm != NULL)
169 thread_fsm_clean_up (thr->thread_fsm);
170 thread_fsm_delete (thr->thread_fsm);
171 thr->thread_fsm = NULL;
176 clear_thread_inferior_resources (struct thread_info *tp)
178 /* NOTE: this will take care of any left-over step_resume breakpoints,
179 but not any user-specified thread-specific breakpoints. We can not
180 delete the breakpoint straight-off, because the inferior might not
181 be stopped at the moment. */
182 delete_at_next_stop (&tp->control.step_resume_breakpoint);
183 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
184 delete_at_next_stop (&tp->control.single_step_breakpoints);
186 delete_longjmp_breakpoint_at_next_stop (tp->global_num);
188 bpstat_clear (&tp->control.stop_bpstat);
190 btrace_teardown (tp);
192 thread_cancel_execution_command (tp);
196 free_thread (struct thread_info *tp)
200 if (tp->private_dtor)
201 tp->private_dtor (tp->priv);
211 init_thread_list (void)
213 struct thread_info *tp, *tpnext;
215 highest_thread_num = 0;
220 for (tp = thread_list; tp; tp = tpnext)
227 threads_executing = 0;
230 /* Allocate a new thread of inferior INF with target id PTID and add
231 it to the thread list. */
233 static struct thread_info *
234 new_thread (struct inferior *inf, ptid_t ptid)
236 struct thread_info *tp;
238 gdb_assert (inf != NULL);
240 tp = XCNEW (struct thread_info);
243 tp->global_num = ++highest_thread_num;
245 tp->per_inf_num = ++inf->highest_thread_num;
247 if (thread_list == NULL)
251 struct thread_info *last;
253 for (last = thread_list; last->next != NULL; last = last->next)
258 /* Nothing to follow yet. */
259 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
260 tp->state = THREAD_STOPPED;
261 tp->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
267 add_thread_silent (ptid_t ptid)
269 struct thread_info *tp;
270 struct inferior *inf = find_inferior_ptid (ptid);
271 gdb_assert (inf != NULL);
273 tp = find_thread_ptid (ptid);
275 /* Found an old thread with the same id. It has to be dead,
276 otherwise we wouldn't be adding a new thread with the same id.
277 The OS is reusing this id --- delete it, and recreate a new
280 /* In addition to deleting the thread, if this is the current
281 thread, then we need to take care that delete_thread doesn't
282 really delete the thread if it is inferior_ptid. Create a
283 new template thread in the list with an invalid ptid, switch
284 to it, delete the original thread, reset the new thread's
285 ptid, and switch to it. */
287 if (ptid_equal (inferior_ptid, ptid))
289 tp = new_thread (inf, null_ptid);
291 /* Make switch_to_thread not read from the thread. */
292 tp->state = THREAD_EXITED;
293 switch_to_thread (null_ptid);
295 /* Now we can delete it. */
296 delete_thread (ptid);
298 /* Now reset its ptid, and reswitch inferior_ptid to it. */
300 tp->state = THREAD_STOPPED;
301 switch_to_thread (ptid);
303 observer_notify_new_thread (tp);
309 /* Just go ahead and delete it. */
310 delete_thread (ptid);
313 tp = new_thread (inf, ptid);
314 observer_notify_new_thread (tp);
320 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
322 struct thread_info *result = add_thread_silent (ptid);
326 if (print_thread_events)
327 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
329 annotate_new_thread ();
334 add_thread (ptid_t ptid)
336 return add_thread_with_info (ptid, NULL);
339 /* Add TP to the end of the step-over chain LIST_P. */
342 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
344 gdb_assert (tp->step_over_next == NULL);
345 gdb_assert (tp->step_over_prev == NULL);
350 tp->step_over_prev = tp->step_over_next = tp;
354 struct thread_info *head = *list_p;
355 struct thread_info *tail = head->step_over_prev;
357 tp->step_over_prev = tail;
358 tp->step_over_next = head;
359 head->step_over_prev = tp;
360 tail->step_over_next = tp;
364 /* Remove TP from step-over chain LIST_P. */
367 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
369 gdb_assert (tp->step_over_next != NULL);
370 gdb_assert (tp->step_over_prev != NULL);
374 if (tp == tp->step_over_next)
377 *list_p = tp->step_over_next;
380 tp->step_over_prev->step_over_next = tp->step_over_next;
381 tp->step_over_next->step_over_prev = tp->step_over_prev;
382 tp->step_over_prev = tp->step_over_next = NULL;
385 /* See gdbthread.h. */
388 thread_step_over_chain_next (struct thread_info *tp)
390 struct thread_info *next = tp->step_over_next;
392 return (next == step_over_queue_head ? NULL : next);
395 /* See gdbthread.h. */
398 thread_is_in_step_over_chain (struct thread_info *tp)
400 return (tp->step_over_next != NULL);
403 /* See gdbthread.h. */
406 thread_step_over_chain_enqueue (struct thread_info *tp)
408 step_over_chain_enqueue (&step_over_queue_head, tp);
411 /* See gdbthread.h. */
414 thread_step_over_chain_remove (struct thread_info *tp)
416 step_over_chain_remove (&step_over_queue_head, tp);
419 /* Delete thread PTID. If SILENT, don't notify the observer of this
422 delete_thread_1 (ptid_t ptid, int silent)
424 struct thread_info *tp, *tpprev;
428 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
429 if (ptid_equal (tp->ptid, ptid))
435 /* Dead threads don't need to step-over. Remove from queue. */
436 if (tp->step_over_next != NULL)
437 thread_step_over_chain_remove (tp);
439 /* If this is the current thread, or there's code out there that
440 relies on it existing (refcount > 0) we can't delete yet. Mark
441 it as exited, and notify it. */
443 || ptid_equal (tp->ptid, inferior_ptid))
445 if (tp->state != THREAD_EXITED)
447 observer_notify_thread_exit (tp, silent);
449 /* Tag it as exited. */
450 tp->state = THREAD_EXITED;
452 /* Clear breakpoints, etc. associated with this thread. */
453 clear_thread_inferior_resources (tp);
456 /* Will be really deleted some other time. */
460 /* Notify thread exit, but only if we haven't already. */
461 if (tp->state != THREAD_EXITED)
462 observer_notify_thread_exit (tp, silent);
464 /* Tag it as exited. */
465 tp->state = THREAD_EXITED;
466 clear_thread_inferior_resources (tp);
469 tpprev->next = tp->next;
471 thread_list = tp->next;
476 /* Delete thread PTID and notify of thread exit. If this is
477 inferior_ptid, don't actually delete it, but tag it as exited and
478 do the notification. If PTID is the user selected thread, clear
481 delete_thread (ptid_t ptid)
483 delete_thread_1 (ptid, 0 /* not silent */);
487 delete_thread_silent (ptid_t ptid)
489 delete_thread_1 (ptid, 1 /* silent */);
493 find_thread_global_id (int global_id)
495 struct thread_info *tp;
497 for (tp = thread_list; tp; tp = tp->next)
498 if (tp->global_num == global_id)
504 static struct thread_info *
505 find_thread_id (struct inferior *inf, int thr_num)
507 struct thread_info *tp;
509 for (tp = thread_list; tp; tp = tp->next)
510 if (tp->inf == inf && tp->per_inf_num == thr_num)
516 /* Find a thread_info by matching PTID. */
518 find_thread_ptid (ptid_t ptid)
520 struct thread_info *tp;
522 for (tp = thread_list; tp; tp = tp->next)
523 if (ptid_equal (tp->ptid, ptid))
530 * Thread iterator function.
532 * Calls a callback function once for each thread, so long as
533 * the callback function returns false. If the callback function
534 * returns true, the iteration will end and the current thread
535 * will be returned. This can be useful for implementing a
536 * search for a thread with arbitrary attributes, or for applying
537 * some operation to every thread.
539 * FIXME: some of the existing functionality, such as
540 * "Thread apply all", might be rewritten using this functionality.
544 iterate_over_threads (int (*callback) (struct thread_info *, void *),
547 struct thread_info *tp, *next;
549 for (tp = thread_list; tp; tp = next)
552 if ((*callback) (tp, data))
563 struct thread_info *tp;
565 for (tp = thread_list; tp; tp = tp->next)
572 valid_global_thread_id (int global_id)
574 struct thread_info *tp;
576 for (tp = thread_list; tp; tp = tp->next)
577 if (tp->global_num == global_id)
584 ptid_to_global_thread_id (ptid_t ptid)
586 struct thread_info *tp;
588 for (tp = thread_list; tp; tp = tp->next)
589 if (ptid_equal (tp->ptid, ptid))
590 return tp->global_num;
596 global_thread_id_to_ptid (int global_id)
598 struct thread_info *thread = find_thread_global_id (global_id);
603 return minus_one_ptid;
607 in_thread_list (ptid_t ptid)
609 struct thread_info *tp;
611 for (tp = thread_list; tp; tp = tp->next)
612 if (ptid_equal (tp->ptid, ptid))
615 return 0; /* Never heard of 'im. */
618 /* Finds the first thread of the inferior given by PID. If PID is -1,
619 return the first thread in the list. */
622 first_thread_of_process (int pid)
624 struct thread_info *tp, *ret = NULL;
626 for (tp = thread_list; tp; tp = tp->next)
627 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
628 if (ret == NULL || tp->global_num < ret->global_num)
635 any_thread_of_process (int pid)
637 struct thread_info *tp;
639 gdb_assert (pid != 0);
641 /* Prefer the current thread. */
642 if (ptid_get_pid (inferior_ptid) == pid)
643 return inferior_thread ();
645 ALL_NON_EXITED_THREADS (tp)
646 if (ptid_get_pid (tp->ptid) == pid)
653 any_live_thread_of_process (int pid)
655 struct thread_info *curr_tp = NULL;
656 struct thread_info *tp;
657 struct thread_info *tp_executing = NULL;
659 gdb_assert (pid != 0);
661 /* Prefer the current thread if it's not executing. */
662 if (ptid_get_pid (inferior_ptid) == pid)
664 /* If the current thread is dead, forget it. If it's not
665 executing, use it. Otherwise, still choose it (below), but
666 only if no other non-executing thread is found. */
667 curr_tp = inferior_thread ();
668 if (curr_tp->state == THREAD_EXITED)
670 else if (!curr_tp->executing)
674 ALL_NON_EXITED_THREADS (tp)
675 if (ptid_get_pid (tp->ptid) == pid)
683 /* If both the current thread and all live threads are executing,
684 prefer the current thread. */
688 /* Otherwise, just return an executing thread, if any. */
692 /* Print a list of thread ids currently known, and the total number of
693 threads. To be used from within catch_errors. */
695 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
697 struct thread_info *tp;
699 struct cleanup *cleanup_chain;
700 int current_thread = -1;
702 update_thread_list ();
704 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
706 for (tp = thread_list; tp; tp = tp->next)
708 if (tp->state == THREAD_EXITED)
711 if (ptid_equal (tp->ptid, inferior_ptid))
712 current_thread = tp->global_num;
715 ui_out_field_int (uiout, "thread-id", tp->global_num);
718 do_cleanups (cleanup_chain);
720 if (current_thread != -1)
721 ui_out_field_int (uiout, "current-thread-id", current_thread);
722 ui_out_field_int (uiout, "number-of-threads", num);
726 /* Official gdblib interface function to get a list of thread ids and
729 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
731 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
732 error_message, RETURN_MASK_ALL) < 0)
737 /* Return true if TP is an active thread. */
739 thread_alive (struct thread_info *tp)
741 if (tp->state == THREAD_EXITED)
743 if (!target_thread_alive (tp->ptid))
748 /* See gdbthreads.h. */
753 struct thread_info *tp, *tmp;
755 ALL_THREADS_SAFE (tp, tmp)
757 if (!thread_alive (tp))
758 delete_thread (tp->ptid);
762 /* See gdbthreads.h. */
765 delete_exited_threads (void)
767 struct thread_info *tp, *tmp;
769 ALL_THREADS_SAFE (tp, tmp)
771 if (tp->state == THREAD_EXITED)
772 delete_thread (tp->ptid);
776 /* Disable storing stack temporaries for the thread whose id is
780 disable_thread_stack_temporaries (void *data)
782 ptid_t *pd = (ptid_t *) data;
783 struct thread_info *tp = find_thread_ptid (*pd);
787 tp->stack_temporaries_enabled = 0;
788 VEC_free (value_ptr, tp->stack_temporaries);
794 /* Enable storing stack temporaries for thread with id PTID and return a
795 cleanup which can disable and clear the stack temporaries. */
798 enable_thread_stack_temporaries (ptid_t ptid)
800 struct thread_info *tp = find_thread_ptid (ptid);
804 gdb_assert (tp != NULL);
806 tp->stack_temporaries_enabled = 1;
807 tp->stack_temporaries = NULL;
808 data = XNEW (ptid_t);
810 c = make_cleanup (disable_thread_stack_temporaries, data);
815 /* Return non-zero value if stack temporaies are enabled for the thread
819 thread_stack_temporaries_enabled_p (ptid_t ptid)
821 struct thread_info *tp = find_thread_ptid (ptid);
826 return tp->stack_temporaries_enabled;
829 /* Push V on to the stack temporaries of the thread with id PTID. */
832 push_thread_stack_temporary (ptid_t ptid, struct value *v)
834 struct thread_info *tp = find_thread_ptid (ptid);
836 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
837 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
840 /* Return 1 if VAL is among the stack temporaries of the thread
841 with id PTID. Return 0 otherwise. */
844 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
846 struct thread_info *tp = find_thread_ptid (ptid);
848 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
849 if (!VEC_empty (value_ptr, tp->stack_temporaries))
854 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
862 /* Return the last of the stack temporaries for thread with id PTID.
863 Return NULL if there are no stack temporaries for the thread. */
866 get_last_thread_stack_temporary (ptid_t ptid)
868 struct value *lastval = NULL;
869 struct thread_info *tp = find_thread_ptid (ptid);
871 gdb_assert (tp != NULL);
872 if (!VEC_empty (value_ptr, tp->stack_temporaries))
873 lastval = VEC_last (value_ptr, tp->stack_temporaries);
879 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
881 struct inferior *inf;
882 struct thread_info *tp;
884 /* It can happen that what we knew as the target inferior id
885 changes. E.g, target remote may only discover the remote process
886 pid after adding the inferior to GDB's list. */
887 inf = find_inferior_ptid (old_ptid);
888 inf->pid = ptid_get_pid (new_ptid);
890 tp = find_thread_ptid (old_ptid);
893 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
896 /* See gdbthread.h. */
899 set_resumed (ptid_t ptid, int resumed)
901 struct thread_info *tp;
902 int all = ptid_equal (ptid, minus_one_ptid);
904 if (all || ptid_is_pid (ptid))
906 for (tp = thread_list; tp; tp = tp->next)
907 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
908 tp->resumed = resumed;
912 tp = find_thread_ptid (ptid);
913 gdb_assert (tp != NULL);
914 tp->resumed = resumed;
918 /* Helper for set_running, that marks one thread either running or
922 set_running_thread (struct thread_info *tp, int running)
926 if (running && tp->state == THREAD_STOPPED)
928 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
932 /* If the thread is now marked stopped, remove it from
933 the step-over queue, so that we don't try to resume
934 it until the user wants it to. */
935 if (tp->step_over_next != NULL)
936 thread_step_over_chain_remove (tp);
943 set_running (ptid_t ptid, int running)
945 struct thread_info *tp;
946 int all = ptid_equal (ptid, minus_one_ptid);
949 /* We try not to notify the observer if no thread has actually changed
950 the running state -- merely to reduce the number of messages to
951 frontend. Frontend is supposed to handle multiple *running just fine. */
952 if (all || ptid_is_pid (ptid))
954 for (tp = thread_list; tp; tp = tp->next)
955 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
957 if (tp->state == THREAD_EXITED)
960 if (set_running_thread (tp, running))
966 tp = find_thread_ptid (ptid);
967 gdb_assert (tp != NULL);
968 gdb_assert (tp->state != THREAD_EXITED);
969 if (set_running_thread (tp, running))
973 observer_notify_target_resumed (ptid);
977 is_thread_state (ptid_t ptid, enum thread_state state)
979 struct thread_info *tp;
981 tp = find_thread_ptid (ptid);
983 return tp->state == state;
987 is_stopped (ptid_t ptid)
989 return is_thread_state (ptid, THREAD_STOPPED);
993 is_exited (ptid_t ptid)
995 return is_thread_state (ptid, THREAD_EXITED);
999 is_running (ptid_t ptid)
1001 return is_thread_state (ptid, THREAD_RUNNING);
1005 is_executing (ptid_t ptid)
1007 struct thread_info *tp;
1009 tp = find_thread_ptid (ptid);
1011 return tp->executing;
1015 set_executing (ptid_t ptid, int executing)
1017 struct thread_info *tp;
1018 int all = ptid_equal (ptid, minus_one_ptid);
1020 if (all || ptid_is_pid (ptid))
1022 for (tp = thread_list; tp; tp = tp->next)
1023 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1024 tp->executing = executing;
1028 tp = find_thread_ptid (ptid);
1030 tp->executing = executing;
1033 /* It only takes one running thread to spawn more threads.*/
1035 threads_executing = 1;
1036 /* Only clear the flag if the caller is telling us everything is
1038 else if (ptid_equal (minus_one_ptid, ptid))
1039 threads_executing = 0;
1042 /* See gdbthread.h. */
1045 threads_are_executing (void)
1047 return threads_executing;
1051 set_stop_requested (ptid_t ptid, int stop)
1053 struct thread_info *tp;
1054 int all = ptid_equal (ptid, minus_one_ptid);
1056 if (all || ptid_is_pid (ptid))
1058 for (tp = thread_list; tp; tp = tp->next)
1059 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1060 tp->stop_requested = stop;
1064 tp = find_thread_ptid (ptid);
1066 tp->stop_requested = stop;
1069 /* Call the stop requested observer so other components of GDB can
1070 react to this request. */
1072 observer_notify_thread_stop_requested (ptid);
1076 finish_thread_state (ptid_t ptid)
1078 struct thread_info *tp;
1080 int any_started = 0;
1082 all = ptid_equal (ptid, minus_one_ptid);
1084 if (all || ptid_is_pid (ptid))
1086 for (tp = thread_list; tp; tp = tp->next)
1088 if (tp->state == THREAD_EXITED)
1090 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1092 if (set_running_thread (tp, tp->executing))
1099 tp = find_thread_ptid (ptid);
1101 if (tp->state != THREAD_EXITED)
1103 if (set_running_thread (tp, tp->executing))
1109 observer_notify_target_resumed (ptid);
1113 finish_thread_state_cleanup (void *arg)
1115 ptid_t *ptid_p = (ptid_t *) arg;
1119 finish_thread_state (*ptid_p);
1122 /* See gdbthread.h. */
1125 validate_registers_access (void)
1127 /* No selected thread, no registers. */
1128 if (ptid_equal (inferior_ptid, null_ptid))
1129 error (_("No thread selected."));
1131 /* Don't try to read from a dead thread. */
1132 if (is_exited (inferior_ptid))
1133 error (_("The current thread has terminated"));
1135 /* ... or from a spinning thread. FIXME: This isn't actually fully
1136 correct. It'll allow an user-requested access (e.g., "print $pc"
1137 at the prompt) when a thread is not executing for some internal
1138 reason, but is marked running from the user's perspective. E.g.,
1139 the thread is waiting for its turn in the step-over queue. */
1140 if (is_executing (inferior_ptid))
1141 error (_("Selected thread is running."));
1145 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1147 return (pc >= thread->control.step_range_start
1148 && pc < thread->control.step_range_end);
1151 /* Helper for print_thread_info. Returns true if THR should be
1152 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1153 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1154 is true if REQUESTED_THREADS is list of global IDs, false if a list
1155 of per-inferior thread ids. If PID is not -1, only print THR if it
1156 is a thread from the process PID. Otherwise, threads from all
1157 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1158 and PID is not -1, then the thread is printed if it belongs to the
1159 specified process. Otherwise, an error is raised. */
1162 should_print_thread (const char *requested_threads, int default_inf_num,
1163 int global_ids, int pid, struct thread_info *thr)
1165 if (requested_threads != NULL && *requested_threads != '\0')
1170 in_list = number_is_in_list (requested_threads, thr->global_num);
1172 in_list = tid_is_in_list (requested_threads, default_inf_num,
1173 thr->inf->num, thr->per_inf_num);
1178 if (pid != -1 && ptid_get_pid (thr->ptid) != pid)
1180 if (requested_threads != NULL && *requested_threads != '\0')
1181 error (_("Requested thread not found in requested process"));
1185 if (thr->state == THREAD_EXITED)
1191 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1192 whether REQUESTED_THREADS is a list of global or per-inferior
1196 print_thread_info_1 (struct ui_out *uiout, char *requested_threads,
1197 int global_ids, int pid,
1198 int show_global_ids)
1200 struct thread_info *tp;
1201 ptid_t current_ptid;
1202 struct cleanup *old_chain;
1203 const char *extra_info, *name, *target_id;
1204 int current_thread = -1;
1205 struct inferior *inf;
1206 int default_inf_num = current_inferior ()->num;
1208 update_thread_list ();
1209 current_ptid = inferior_ptid;
1211 /* We'll be switching threads temporarily. */
1212 old_chain = make_cleanup_restore_current_thread ();
1214 /* For backward compatibility, we make a list for MI. A table is
1215 preferable for the CLI, though, because it shows table
1217 if (ui_out_is_mi_like_p (uiout))
1218 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1223 for (tp = thread_list; tp; tp = tp->next)
1225 if (!should_print_thread (requested_threads, default_inf_num,
1226 global_ids, pid, tp))
1234 if (requested_threads == NULL || *requested_threads == '\0')
1235 ui_out_message (uiout, 0, _("No threads.\n"));
1237 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1239 do_cleanups (old_chain);
1243 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1244 make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads");
1246 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1248 ui_out_table_header (uiout, 1, ui_left, "current", "");
1250 if (!ui_out_is_mi_like_p (uiout))
1251 ui_out_table_header (uiout, 4, ui_left, "id-in-tg", "Id");
1252 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1253 ui_out_table_header (uiout, 4, ui_left, "id", "GId");
1254 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1255 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1256 ui_out_table_body (uiout);
1259 ALL_THREADS_BY_INFERIOR (inf, tp)
1261 struct cleanup *chain2;
1264 if (ptid_equal (tp->ptid, current_ptid))
1265 current_thread = tp->global_num;
1267 if (!should_print_thread (requested_threads, default_inf_num,
1268 global_ids, pid, tp))
1271 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1273 if (ui_out_is_mi_like_p (uiout))
1275 /* Compatibility. */
1276 if (ptid_equal (tp->ptid, current_ptid))
1277 ui_out_text (uiout, "* ");
1279 ui_out_text (uiout, " ");
1283 if (ptid_equal (tp->ptid, current_ptid))
1284 ui_out_field_string (uiout, "current", "*");
1286 ui_out_field_skip (uiout, "current");
1289 if (!ui_out_is_mi_like_p (uiout))
1290 ui_out_field_string (uiout, "id-in-tg", print_thread_id (tp));
1292 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1293 ui_out_field_int (uiout, "id", tp->global_num);
1295 /* For the CLI, we stuff everything into the target-id field.
1296 This is a gross hack to make the output come out looking
1297 correct. The underlying problem here is that ui-out has no
1298 way to specify that a field's space allocation should be
1299 shared by several fields. For MI, we do the right thing
1302 target_id = target_pid_to_str (tp->ptid);
1303 extra_info = target_extra_thread_info (tp);
1304 name = tp->name ? tp->name : target_thread_name (tp);
1306 if (ui_out_is_mi_like_p (uiout))
1308 ui_out_field_string (uiout, "target-id", target_id);
1310 ui_out_field_string (uiout, "details", extra_info);
1312 ui_out_field_string (uiout, "name", name);
1316 struct cleanup *str_cleanup;
1319 if (extra_info && name)
1320 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1322 else if (extra_info)
1323 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1325 contents = xstrprintf ("%s \"%s\"", target_id, name);
1327 contents = xstrdup (target_id);
1328 str_cleanup = make_cleanup (xfree, contents);
1330 ui_out_field_string (uiout, "target-id", contents);
1331 do_cleanups (str_cleanup);
1334 if (tp->state == THREAD_RUNNING)
1335 ui_out_text (uiout, "(running)\n");
1338 /* The switch below puts us at the top of the stack (leaf
1340 switch_to_thread (tp->ptid);
1341 print_stack_frame (get_selected_frame (NULL),
1342 /* For MI output, print frame level. */
1343 ui_out_is_mi_like_p (uiout),
1347 if (ui_out_is_mi_like_p (uiout))
1349 char *state = "stopped";
1351 if (tp->state == THREAD_RUNNING)
1353 ui_out_field_string (uiout, "state", state);
1356 core = target_core_of_thread (tp->ptid);
1357 if (ui_out_is_mi_like_p (uiout) && core != -1)
1358 ui_out_field_int (uiout, "core", core);
1360 do_cleanups (chain2);
1363 /* Restores the current thread and the frame selected before
1364 the "info threads" command. */
1365 do_cleanups (old_chain);
1367 if (pid == -1 && requested_threads == NULL)
1369 if (ui_out_is_mi_like_p (uiout)
1370 && !ptid_equal (inferior_ptid, null_ptid))
1372 int num = ptid_to_global_thread_id (inferior_ptid);
1374 gdb_assert (num != 0);
1375 ui_out_field_int (uiout, "current-thread-id", num);
1378 if (!ptid_equal (inferior_ptid, null_ptid) && is_exited (inferior_ptid))
1379 ui_out_message (uiout, 0, "\n\
1380 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1381 print_thread_id (inferior_thread ()));
1382 else if (thread_list != NULL
1383 && ptid_equal (inferior_ptid, null_ptid))
1384 ui_out_message (uiout, 0, "\n\
1385 No selected thread. See `help thread'.\n");
1389 /* See gdbthread.h. */
1392 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1394 print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
1397 /* Implementation of the "info threads" command.
1399 Note: this has the drawback that it _really_ switches
1400 threads, which frees the frame cache. A no-side
1401 effects info-threads command would be nicer. */
1404 info_threads_command (char *arg, int from_tty)
1406 int show_global_ids = 0;
1409 && check_for_argument (&arg, "-gid", sizeof ("-gid") - 1))
1411 arg = skip_spaces (arg);
1412 show_global_ids = 1;
1415 print_thread_info_1 (current_uiout, arg, 0, -1, show_global_ids);
1418 /* See gdbthread.h. */
1421 switch_to_thread_no_regs (struct thread_info *thread)
1423 struct inferior *inf;
1425 inf = find_inferior_ptid (thread->ptid);
1426 gdb_assert (inf != NULL);
1427 set_current_program_space (inf->pspace);
1428 set_current_inferior (inf);
1430 inferior_ptid = thread->ptid;
1431 stop_pc = ~(CORE_ADDR) 0;
1434 /* Switch from one thread to another. */
1437 switch_to_thread (ptid_t ptid)
1439 /* Switch the program space as well, if we can infer it from the now
1440 current thread. Otherwise, it's up to the caller to select the
1442 if (!ptid_equal (ptid, null_ptid))
1444 struct inferior *inf;
1446 inf = find_inferior_ptid (ptid);
1447 gdb_assert (inf != NULL);
1448 set_current_program_space (inf->pspace);
1449 set_current_inferior (inf);
1452 if (ptid_equal (ptid, inferior_ptid))
1455 inferior_ptid = ptid;
1456 reinit_frame_cache ();
1458 /* We don't check for is_stopped, because we're called at times
1459 while in the TARGET_RUNNING state, e.g., while handling an
1461 if (!ptid_equal (inferior_ptid, null_ptid)
1462 && !is_exited (ptid)
1463 && !is_executing (ptid))
1464 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1466 stop_pc = ~(CORE_ADDR) 0;
1470 restore_current_thread (ptid_t ptid)
1472 switch_to_thread (ptid);
1476 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1478 struct frame_info *frame = NULL;
1481 /* This means there was no selected frame. */
1482 if (frame_level == -1)
1484 select_frame (NULL);
1488 gdb_assert (frame_level >= 0);
1490 /* Restore by level first, check if the frame id is the same as
1491 expected. If that fails, try restoring by frame id. If that
1492 fails, nothing to do, just warn the user. */
1494 count = frame_level;
1495 frame = find_relative_frame (get_current_frame (), &count);
1498 /* The frame ids must match - either both valid or both outer_frame_id.
1499 The latter case is not failsafe, but since it's highly unlikely
1500 the search by level finds the wrong frame, it's 99.9(9)% of
1501 the time (for all practical purposes) safe. */
1502 && frame_id_eq (get_frame_id (frame), a_frame_id))
1504 /* Cool, all is fine. */
1505 select_frame (frame);
1509 frame = frame_find_by_id (a_frame_id);
1512 /* Cool, refound it. */
1513 select_frame (frame);
1517 /* Nothing else to do, the frame layout really changed. Select the
1518 innermost stack frame. */
1519 select_frame (get_current_frame ());
1521 /* Warn the user. */
1522 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1524 warning (_("Couldn't restore frame #%d in "
1525 "current thread. Bottom (innermost) frame selected:"),
1527 /* For MI, we should probably have a notification about
1528 current frame change. But this error is not very
1529 likely, so don't bother for now. */
1530 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1534 /* Data used by the cleanup installed by
1535 'make_cleanup_restore_current_thread'. */
1537 struct current_thread_cleanup
1539 /* Next in list of currently installed 'struct
1540 current_thread_cleanup' cleanups. See
1541 'current_thread_cleanup_chain' below. */
1542 struct current_thread_cleanup *next;
1544 ptid_t inferior_ptid;
1545 struct frame_id selected_frame_id;
1546 int selected_frame_level;
1552 /* A chain of currently installed 'struct current_thread_cleanup'
1553 cleanups. Restoring the previously selected thread looks up the
1554 old thread in the thread list by ptid. If the thread changes ptid,
1555 we need to update the cleanup's thread structure so the look up
1557 static struct current_thread_cleanup *current_thread_cleanup_chain;
1559 /* A thread_ptid_changed observer. Update all currently installed
1560 current_thread_cleanup cleanups that want to switch back to
1561 OLD_PTID to switch back to NEW_PTID instead. */
1564 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1566 struct current_thread_cleanup *it;
1568 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1570 if (ptid_equal (it->inferior_ptid, old_ptid))
1571 it->inferior_ptid = new_ptid;
1576 do_restore_current_thread_cleanup (void *arg)
1578 struct thread_info *tp;
1579 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1581 tp = find_thread_ptid (old->inferior_ptid);
1583 /* If the previously selected thread belonged to a process that has
1584 in the mean time been deleted (due to normal exit, detach, etc.),
1585 then don't revert back to it, but instead simply drop back to no
1588 && find_inferior_ptid (tp->ptid) != NULL)
1589 restore_current_thread (old->inferior_ptid);
1592 restore_current_thread (null_ptid);
1593 set_current_inferior (find_inferior_id (old->inf_id));
1596 /* The running state of the originally selected thread may have
1597 changed, so we have to recheck it here. */
1598 if (!ptid_equal (inferior_ptid, null_ptid)
1600 && is_stopped (inferior_ptid)
1601 && target_has_registers
1603 && target_has_memory)
1604 restore_selected_frame (old->selected_frame_id,
1605 old->selected_frame_level);
1609 restore_current_thread_cleanup_dtor (void *arg)
1611 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1612 struct thread_info *tp;
1613 struct inferior *inf;
1615 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1617 tp = find_thread_ptid (old->inferior_ptid);
1620 inf = find_inferior_id (old->inf_id);
1622 inf->removable = old->was_removable;
1626 /* Set the thread reference count. */
1629 set_thread_refcount (void *data)
1632 struct thread_array_cleanup *ta_cleanup
1633 = (struct thread_array_cleanup *) data;
1635 for (k = 0; k != ta_cleanup->count; k++)
1636 ta_cleanup->tp_array[k]->refcount--;
1640 make_cleanup_restore_current_thread (void)
1642 struct thread_info *tp;
1643 struct frame_info *frame;
1644 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1646 old->inferior_ptid = inferior_ptid;
1647 old->inf_id = current_inferior ()->num;
1648 old->was_removable = current_inferior ()->removable;
1650 old->next = current_thread_cleanup_chain;
1651 current_thread_cleanup_chain = old;
1653 if (!ptid_equal (inferior_ptid, null_ptid))
1655 old->was_stopped = is_stopped (inferior_ptid);
1656 if (old->was_stopped
1657 && target_has_registers
1659 && target_has_memory)
1661 /* When processing internal events, there might not be a
1662 selected frame. If we naively call get_selected_frame
1663 here, then we can end up reading debuginfo for the
1664 current frame, but we don't generally need the debuginfo
1666 frame = get_selected_frame_if_set ();
1671 old->selected_frame_id = get_frame_id (frame);
1672 old->selected_frame_level = frame_relative_level (frame);
1674 tp = find_thread_ptid (inferior_ptid);
1679 current_inferior ()->removable = 0;
1681 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1682 restore_current_thread_cleanup_dtor);
1685 /* See gdbthread.h. */
1688 show_thread_that_caused_stop (void)
1690 return highest_thread_num > 1;
1693 /* See gdbthread.h. */
1696 show_inferior_qualified_tids (void)
1698 return (inferior_list->next != NULL || inferior_list->num != 1);
1701 /* See gdbthread.h. */
1704 print_thread_id (struct thread_info *thr)
1706 char *s = get_print_cell ();
1708 if (show_inferior_qualified_tids ())
1709 xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num);
1711 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num);
1715 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1716 descending order. */
1718 static int tp_array_compar_ascending;
1720 /* Sort an array for struct thread_info pointers by thread ID (first
1721 by inferior number, and then by per-inferior thread number). The
1722 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1725 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1727 const struct thread_info *a = *(const struct thread_info * const *) ap_voidp;
1728 const struct thread_info *b = *(const struct thread_info * const *) bp_voidp;
1730 if (a->inf->num != b->inf->num)
1732 return ((a->inf->num > b->inf->num) - (a->inf->num < b->inf->num)
1733 * (tp_array_compar_ascending ? +1 : -1));
1736 return (((a->per_inf_num > b->per_inf_num)
1737 - (a->per_inf_num < b->per_inf_num))
1738 * (tp_array_compar_ascending ? +1 : -1));
1741 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1742 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1743 of two numbers seperated by a hyphen. Examples:
1745 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1746 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1747 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1750 thread_apply_all_command (char *cmd, int from_tty)
1752 struct cleanup *old_chain;
1755 struct thread_array_cleanup ta_cleanup;
1757 tp_array_compar_ascending = 0;
1759 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1761 cmd = skip_spaces (cmd);
1762 tp_array_compar_ascending = 1;
1765 if (cmd == NULL || *cmd == '\000')
1766 error (_("Please specify a command following the thread ID list"));
1768 update_thread_list ();
1770 old_chain = make_cleanup_restore_current_thread ();
1772 /* Save a copy of the command in case it is clobbered by
1774 saved_cmd = xstrdup (cmd);
1775 make_cleanup (xfree, saved_cmd);
1777 /* Note this includes exited threads. */
1778 tc = thread_count ();
1781 struct thread_info **tp_array;
1782 struct thread_info *tp;
1785 /* Save a copy of the thread_list in case we execute detach
1787 tp_array = XNEWVEC (struct thread_info *, tc);
1788 make_cleanup (xfree, tp_array);
1790 ALL_NON_EXITED_THREADS (tp)
1796 /* Because we skipped exited threads, we may end up with fewer
1797 threads in the array than the total count of threads. */
1798 gdb_assert (i <= tc);
1801 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1803 ta_cleanup.tp_array = tp_array;
1804 ta_cleanup.count = i;
1805 make_cleanup (set_thread_refcount, &ta_cleanup);
1807 for (k = 0; k != i; k++)
1808 if (thread_alive (tp_array[k]))
1810 switch_to_thread (tp_array[k]->ptid);
1811 printf_filtered (_("\nThread %s (%s):\n"),
1812 print_thread_id (tp_array[k]),
1813 target_pid_to_str (inferior_ptid));
1814 execute_command (cmd, from_tty);
1816 /* Restore exact command used previously. */
1817 strcpy (cmd, saved_cmd);
1821 do_cleanups (old_chain);
1824 /* Implementation of the "thread apply" command. */
1827 thread_apply_command (char *tidlist, int from_tty)
1830 struct cleanup *old_chain;
1832 struct tid_range_parser parser;
1834 if (tidlist == NULL || *tidlist == '\000')
1835 error (_("Please specify a thread ID list"));
1837 tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
1838 while (!tid_range_parser_finished (&parser))
1840 int inf_num, thr_start, thr_end;
1842 if (!tid_range_parser_get_tid_range (&parser,
1843 &inf_num, &thr_start, &thr_end))
1845 cmd = (char *) tid_range_parser_string (&parser);
1851 error (_("Please specify a command following the thread ID list"));
1853 if (tidlist == cmd || !isalpha (cmd[0]))
1854 invalid_thread_id_error (cmd);
1856 /* Save a copy of the command in case it is clobbered by
1858 saved_cmd = xstrdup (cmd);
1859 old_chain = make_cleanup (xfree, saved_cmd);
1861 make_cleanup_restore_current_thread ();
1863 tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
1864 while (!tid_range_parser_finished (&parser)
1865 && tid_range_parser_string (&parser) < cmd)
1867 struct thread_info *tp = NULL;
1868 struct inferior *inf;
1869 int inf_num, thr_num;
1871 tid_range_parser_get_tid (&parser, &inf_num, &thr_num);
1872 inf = find_inferior_id (inf_num);
1874 tp = find_thread_id (inf, thr_num);
1876 if (tid_range_parser_star_range (&parser))
1880 warning (_("Unknown inferior %d"), inf_num);
1881 tid_range_parser_skip (&parser);
1885 /* No use looking for threads past the highest thread number
1886 the inferior ever had. */
1887 if (thr_num >= inf->highest_thread_num)
1888 tid_range_parser_skip (&parser);
1890 /* Be quiet about unknown threads numbers. */
1897 if (show_inferior_qualified_tids ()
1898 || tid_range_parser_qualified (&parser))
1899 warning (_("Unknown thread %d.%d"), inf_num, thr_num);
1901 warning (_("Unknown thread %d"), thr_num);
1905 if (!thread_alive (tp))
1907 warning (_("Thread %s has terminated."), print_thread_id (tp));
1911 switch_to_thread (tp->ptid);
1913 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp),
1914 target_pid_to_str (inferior_ptid));
1915 execute_command (cmd, from_tty);
1917 /* Restore exact command used previously. */
1918 strcpy (cmd, saved_cmd);
1921 do_cleanups (old_chain);
1924 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1925 if prefix of arg is `apply'. */
1928 thread_command (char *tidstr, int from_tty)
1932 if (ptid_equal (inferior_ptid, null_ptid))
1933 error (_("No thread selected"));
1935 if (target_has_stack)
1937 struct thread_info *tp = inferior_thread ();
1939 if (is_exited (inferior_ptid))
1940 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1941 print_thread_id (tp),
1942 target_pid_to_str (inferior_ptid));
1944 printf_filtered (_("[Current thread is %s (%s)]\n"),
1945 print_thread_id (tp),
1946 target_pid_to_str (inferior_ptid));
1949 error (_("No stack."));
1953 gdb_thread_select (current_uiout, tidstr, NULL);
1956 /* Implementation of `thread name'. */
1959 thread_name_command (char *arg, int from_tty)
1961 struct thread_info *info;
1963 if (ptid_equal (inferior_ptid, null_ptid))
1964 error (_("No thread selected"));
1966 arg = skip_spaces (arg);
1968 info = inferior_thread ();
1970 info->name = arg ? xstrdup (arg) : NULL;
1973 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1976 thread_find_command (char *arg, int from_tty)
1978 struct thread_info *tp;
1980 unsigned long match = 0;
1982 if (arg == NULL || *arg == '\0')
1983 error (_("Command requires an argument."));
1985 tmp = re_comp (arg);
1987 error (_("Invalid regexp (%s): %s"), tmp, arg);
1989 update_thread_list ();
1990 for (tp = thread_list; tp; tp = tp->next)
1992 if (tp->name != NULL && re_exec (tp->name))
1994 printf_filtered (_("Thread %s has name '%s'\n"),
1995 print_thread_id (tp), tp->name);
1999 tmp = target_thread_name (tp);
2000 if (tmp != NULL && re_exec (tmp))
2002 printf_filtered (_("Thread %s has target name '%s'\n"),
2003 print_thread_id (tp), tmp);
2007 tmp = target_pid_to_str (tp->ptid);
2008 if (tmp != NULL && re_exec (tmp))
2010 printf_filtered (_("Thread %s has target id '%s'\n"),
2011 print_thread_id (tp), tmp);
2015 tmp = target_extra_thread_info (tp);
2016 if (tmp != NULL && re_exec (tmp))
2018 printf_filtered (_("Thread %s has extra info '%s'\n"),
2019 print_thread_id (tp), tmp);
2024 printf_filtered (_("No threads match '%s'\n"), arg);
2027 /* Print notices when new threads are attached and detached. */
2028 int print_thread_events = 1;
2030 show_print_thread_events (struct ui_file *file, int from_tty,
2031 struct cmd_list_element *c, const char *value)
2033 fprintf_filtered (file,
2034 _("Printing of thread events is %s.\n"),
2039 do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
2041 const char *tidstr = (const char *) tidstr_v;
2042 struct thread_info *tp;
2044 if (ui_out_is_mi_like_p (uiout))
2046 int num = value_as_long (parse_and_eval (tidstr));
2048 tp = find_thread_global_id (num);
2050 error (_("Thread ID %d not known."), num);
2054 tp = parse_thread_id (tidstr, NULL);
2055 gdb_assert (tp != NULL);
2058 if (!thread_alive (tp))
2059 error (_("Thread ID %s has terminated."), tidstr);
2061 switch_to_thread (tp->ptid);
2063 annotate_thread_changed ();
2065 if (ui_out_is_mi_like_p (uiout))
2066 ui_out_field_int (uiout, "new-thread-id", inferior_thread ()->global_num);
2069 ui_out_text (uiout, "[Switching to thread ");
2070 ui_out_field_string (uiout, "new-thread-id", print_thread_id (tp));
2071 ui_out_text (uiout, " (");
2072 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
2073 ui_out_text (uiout, ")]");
2076 /* Note that we can't reach this with an exited thread, due to the
2077 thread_alive check above. */
2078 if (tp->state == THREAD_RUNNING)
2079 ui_out_text (uiout, "(running)\n");
2082 ui_out_text (uiout, "\n");
2083 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2086 /* Since the current thread may have changed, see if there is any
2087 exited thread we can now delete. */
2094 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
2096 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
2097 error_message, RETURN_MASK_ALL) < 0)
2102 /* Update the 'threads_executing' global based on the threads we know
2106 update_threads_executing (void)
2108 struct thread_info *tp;
2110 threads_executing = 0;
2111 ALL_NON_EXITED_THREADS (tp)
2115 threads_executing = 1;
2122 update_thread_list (void)
2124 target_update_thread_list ();
2125 update_threads_executing ();
2128 /* Return a new value for the selected thread's id. Return a value of
2129 0 if no thread is selected. If GLOBAL is true, return the thread's
2130 global number. Otherwise return the per-inferior number. */
2132 static struct value *
2133 thread_num_make_value_helper (struct gdbarch *gdbarch, int global)
2135 struct thread_info *tp = find_thread_ptid (inferior_ptid);
2141 int_val = tp->global_num;
2143 int_val = tp->per_inf_num;
2145 return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val);
2148 /* Return a new value for the selected thread's per-inferior thread
2149 number. Return a value of 0 if no thread is selected, or no
2152 static struct value *
2153 thread_id_per_inf_num_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2156 return thread_num_make_value_helper (gdbarch, 0);
2159 /* Return a new value for the selected thread's global id. Return a
2160 value of 0 if no thread is selected, or no threads exist. */
2162 static struct value *
2163 global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2166 return thread_num_make_value_helper (gdbarch, 1);
2169 /* Commands with a prefix of `thread'. */
2170 struct cmd_list_element *thread_cmd_list = NULL;
2172 /* Implementation of `thread' variable. */
2174 static const struct internalvar_funcs thread_funcs =
2176 thread_id_per_inf_num_make_value,
2181 /* Implementation of `gthread' variable. */
2183 static const struct internalvar_funcs gthread_funcs =
2185 global_thread_id_make_value,
2191 _initialize_thread (void)
2193 static struct cmd_list_element *thread_apply_list = NULL;
2195 add_info ("threads", info_threads_command,
2196 _("Display currently known threads.\n\
2197 Usage: info threads [-gid] [ID]...\n\
2198 -gid: Show global thread IDs.\n\
2199 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2200 Otherwise, all threads are displayed."));
2202 add_prefix_cmd ("thread", class_run, thread_command, _("\
2203 Use this command to switch between threads.\n\
2204 The new thread ID must be currently known."),
2205 &thread_cmd_list, "thread ", 1, &cmdlist);
2207 add_prefix_cmd ("apply", class_run, thread_apply_command,
2208 _("Apply a command to a list of threads."),
2209 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
2211 add_cmd ("all", class_run, thread_apply_all_command,
2213 Apply a command to all threads.\n\
2215 Usage: thread apply all [-ascending] <command>\n\
2216 -ascending: Call <command> for all threads in ascending order.\n\
2217 The default is descending order.\
2219 &thread_apply_list);
2221 add_cmd ("name", class_run, thread_name_command,
2222 _("Set the current thread's name.\n\
2223 Usage: thread name [NAME]\n\
2224 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2226 add_cmd ("find", class_run, thread_find_command, _("\
2227 Find threads that match a regular expression.\n\
2228 Usage: thread find REGEXP\n\
2229 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2232 add_com_alias ("t", "thread", class_run, 1);
2234 add_setshow_boolean_cmd ("thread-events", no_class,
2235 &print_thread_events, _("\
2236 Set printing of thread events (such as thread start and exit)."), _("\
2237 Show printing of thread events (such as thread start and exit)."), NULL,
2239 show_print_thread_events,
2240 &setprintlist, &showprintlist);
2242 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
2243 create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL);
2245 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);