1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2015 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 "continuations.h"
46 #include "thread-fsm.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->num);
188 bpstat_clear (&tp->control.stop_bpstat);
190 btrace_teardown (tp);
192 thread_cancel_execution_command (tp);
194 do_all_intermediate_continuations_thread (tp, 1);
195 do_all_continuations_thread (tp, 1);
199 free_thread (struct thread_info *tp)
203 if (tp->private_dtor)
204 tp->private_dtor (tp->priv);
214 init_thread_list (void)
216 struct thread_info *tp, *tpnext;
218 highest_thread_num = 0;
223 for (tp = thread_list; tp; tp = tpnext)
230 threads_executing = 0;
233 /* Allocate a new thread with target id PTID and add it to the thread
236 static struct thread_info *
237 new_thread (ptid_t ptid)
239 struct thread_info *tp = XCNEW (struct thread_info);
242 tp->num = ++highest_thread_num;
243 tp->next = thread_list;
246 /* Nothing to follow yet. */
247 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
248 tp->state = THREAD_STOPPED;
249 tp->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
255 add_thread_silent (ptid_t ptid)
257 struct thread_info *tp;
259 tp = find_thread_ptid (ptid);
261 /* Found an old thread with the same id. It has to be dead,
262 otherwise we wouldn't be adding a new thread with the same id.
263 The OS is reusing this id --- delete it, and recreate a new
266 /* In addition to deleting the thread, if this is the current
267 thread, then we need to take care that delete_thread doesn't
268 really delete the thread if it is inferior_ptid. Create a
269 new template thread in the list with an invalid ptid, switch
270 to it, delete the original thread, reset the new thread's
271 ptid, and switch to it. */
273 if (ptid_equal (inferior_ptid, ptid))
275 tp = new_thread (null_ptid);
277 /* Make switch_to_thread not read from the thread. */
278 tp->state = THREAD_EXITED;
279 switch_to_thread (null_ptid);
281 /* Now we can delete it. */
282 delete_thread (ptid);
284 /* Now reset its ptid, and reswitch inferior_ptid to it. */
286 tp->state = THREAD_STOPPED;
287 switch_to_thread (ptid);
289 observer_notify_new_thread (tp);
295 /* Just go ahead and delete it. */
296 delete_thread (ptid);
299 tp = new_thread (ptid);
300 observer_notify_new_thread (tp);
306 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
308 struct thread_info *result = add_thread_silent (ptid);
312 if (print_thread_events)
313 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
315 annotate_new_thread ();
320 add_thread (ptid_t ptid)
322 return add_thread_with_info (ptid, NULL);
325 /* Add TP to the end of the step-over chain LIST_P. */
328 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
330 gdb_assert (tp->step_over_next == NULL);
331 gdb_assert (tp->step_over_prev == NULL);
336 tp->step_over_prev = tp->step_over_next = tp;
340 struct thread_info *head = *list_p;
341 struct thread_info *tail = head->step_over_prev;
343 tp->step_over_prev = tail;
344 tp->step_over_next = head;
345 head->step_over_prev = tp;
346 tail->step_over_next = tp;
350 /* Remove TP from step-over chain LIST_P. */
353 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
355 gdb_assert (tp->step_over_next != NULL);
356 gdb_assert (tp->step_over_prev != NULL);
360 if (tp == tp->step_over_next)
363 *list_p = tp->step_over_next;
366 tp->step_over_prev->step_over_next = tp->step_over_next;
367 tp->step_over_next->step_over_prev = tp->step_over_prev;
368 tp->step_over_prev = tp->step_over_next = NULL;
371 /* See gdbthread.h. */
374 thread_step_over_chain_next (struct thread_info *tp)
376 struct thread_info *next = tp->step_over_next;
378 return (next == step_over_queue_head ? NULL : next);
381 /* See gdbthread.h. */
384 thread_is_in_step_over_chain (struct thread_info *tp)
386 return (tp->step_over_next != NULL);
389 /* See gdbthread.h. */
392 thread_step_over_chain_enqueue (struct thread_info *tp)
394 step_over_chain_enqueue (&step_over_queue_head, tp);
397 /* See gdbthread.h. */
400 thread_step_over_chain_remove (struct thread_info *tp)
402 step_over_chain_remove (&step_over_queue_head, tp);
405 /* Delete thread PTID. If SILENT, don't notify the observer of this
408 delete_thread_1 (ptid_t ptid, int silent)
410 struct thread_info *tp, *tpprev;
414 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
415 if (ptid_equal (tp->ptid, ptid))
421 /* Dead threads don't need to step-over. Remove from queue. */
422 if (tp->step_over_next != NULL)
423 thread_step_over_chain_remove (tp);
425 /* If this is the current thread, or there's code out there that
426 relies on it existing (refcount > 0) we can't delete yet. Mark
427 it as exited, and notify it. */
429 || ptid_equal (tp->ptid, inferior_ptid))
431 if (tp->state != THREAD_EXITED)
433 observer_notify_thread_exit (tp, silent);
435 /* Tag it as exited. */
436 tp->state = THREAD_EXITED;
438 /* Clear breakpoints, etc. associated with this thread. */
439 clear_thread_inferior_resources (tp);
442 /* Will be really deleted some other time. */
446 /* Notify thread exit, but only if we haven't already. */
447 if (tp->state != THREAD_EXITED)
448 observer_notify_thread_exit (tp, silent);
450 /* Tag it as exited. */
451 tp->state = THREAD_EXITED;
452 clear_thread_inferior_resources (tp);
455 tpprev->next = tp->next;
457 thread_list = tp->next;
462 /* Delete thread PTID and notify of thread exit. If this is
463 inferior_ptid, don't actually delete it, but tag it as exited and
464 do the notification. If PTID is the user selected thread, clear
467 delete_thread (ptid_t ptid)
469 delete_thread_1 (ptid, 0 /* not silent */);
473 delete_thread_silent (ptid_t ptid)
475 delete_thread_1 (ptid, 1 /* silent */);
479 find_thread_id (int num)
481 struct thread_info *tp;
483 for (tp = thread_list; tp; tp = tp->next)
490 /* Find a thread_info by matching PTID. */
492 find_thread_ptid (ptid_t ptid)
494 struct thread_info *tp;
496 for (tp = thread_list; tp; tp = tp->next)
497 if (ptid_equal (tp->ptid, ptid))
504 * Thread iterator function.
506 * Calls a callback function once for each thread, so long as
507 * the callback function returns false. If the callback function
508 * returns true, the iteration will end and the current thread
509 * will be returned. This can be useful for implementing a
510 * search for a thread with arbitrary attributes, or for applying
511 * some operation to every thread.
513 * FIXME: some of the existing functionality, such as
514 * "Thread apply all", might be rewritten using this functionality.
518 iterate_over_threads (int (*callback) (struct thread_info *, void *),
521 struct thread_info *tp, *next;
523 for (tp = thread_list; tp; tp = next)
526 if ((*callback) (tp, data))
537 struct thread_info *tp;
539 for (tp = thread_list; tp; tp = tp->next)
546 valid_thread_id (int num)
548 struct thread_info *tp;
550 for (tp = thread_list; tp; tp = tp->next)
558 pid_to_thread_id (ptid_t ptid)
560 struct thread_info *tp;
562 for (tp = thread_list; tp; tp = tp->next)
563 if (ptid_equal (tp->ptid, ptid))
570 thread_id_to_pid (int num)
572 struct thread_info *thread = find_thread_id (num);
577 return pid_to_ptid (-1);
581 in_thread_list (ptid_t ptid)
583 struct thread_info *tp;
585 for (tp = thread_list; tp; tp = tp->next)
586 if (ptid_equal (tp->ptid, ptid))
589 return 0; /* Never heard of 'im. */
592 /* Finds the first thread of the inferior given by PID. If PID is -1,
593 return the first thread in the list. */
596 first_thread_of_process (int pid)
598 struct thread_info *tp, *ret = NULL;
600 for (tp = thread_list; tp; tp = tp->next)
601 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
602 if (ret == NULL || tp->num < ret->num)
609 any_thread_of_process (int pid)
611 struct thread_info *tp;
613 gdb_assert (pid != 0);
615 /* Prefer the current thread. */
616 if (ptid_get_pid (inferior_ptid) == pid)
617 return inferior_thread ();
619 ALL_NON_EXITED_THREADS (tp)
620 if (ptid_get_pid (tp->ptid) == pid)
627 any_live_thread_of_process (int pid)
629 struct thread_info *curr_tp = NULL;
630 struct thread_info *tp;
631 struct thread_info *tp_executing = NULL;
633 gdb_assert (pid != 0);
635 /* Prefer the current thread if it's not executing. */
636 if (ptid_get_pid (inferior_ptid) == pid)
638 /* If the current thread is dead, forget it. If it's not
639 executing, use it. Otherwise, still choose it (below), but
640 only if no other non-executing thread is found. */
641 curr_tp = inferior_thread ();
642 if (curr_tp->state == THREAD_EXITED)
644 else if (!curr_tp->executing)
648 ALL_NON_EXITED_THREADS (tp)
649 if (ptid_get_pid (tp->ptid) == pid)
657 /* If both the current thread and all live threads are executing,
658 prefer the current thread. */
662 /* Otherwise, just return an executing thread, if any. */
666 /* Print a list of thread ids currently known, and the total number of
667 threads. To be used from within catch_errors. */
669 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
671 struct thread_info *tp;
673 struct cleanup *cleanup_chain;
674 int current_thread = -1;
676 update_thread_list ();
678 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
680 for (tp = thread_list; tp; tp = tp->next)
682 if (tp->state == THREAD_EXITED)
685 if (ptid_equal (tp->ptid, inferior_ptid))
686 current_thread = tp->num;
689 ui_out_field_int (uiout, "thread-id", tp->num);
692 do_cleanups (cleanup_chain);
694 if (current_thread != -1)
695 ui_out_field_int (uiout, "current-thread-id", current_thread);
696 ui_out_field_int (uiout, "number-of-threads", num);
700 /* Official gdblib interface function to get a list of thread ids and
703 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
705 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
706 error_message, RETURN_MASK_ALL) < 0)
711 /* Return true if TP is an active thread. */
713 thread_alive (struct thread_info *tp)
715 if (tp->state == THREAD_EXITED)
717 if (!target_thread_alive (tp->ptid))
722 /* See gdbthreads.h. */
727 struct thread_info *tp, *tmp;
729 ALL_THREADS_SAFE (tp, tmp)
731 if (!thread_alive (tp))
732 delete_thread (tp->ptid);
736 /* See gdbthreads.h. */
739 delete_exited_threads (void)
741 struct thread_info *tp, *tmp;
743 ALL_THREADS_SAFE (tp, tmp)
745 if (tp->state == THREAD_EXITED)
746 delete_thread (tp->ptid);
750 /* Disable storing stack temporaries for the thread whose id is
754 disable_thread_stack_temporaries (void *data)
757 struct thread_info *tp = find_thread_ptid (*pd);
761 tp->stack_temporaries_enabled = 0;
762 VEC_free (value_ptr, tp->stack_temporaries);
768 /* Enable storing stack temporaries for thread with id PTID and return a
769 cleanup which can disable and clear the stack temporaries. */
772 enable_thread_stack_temporaries (ptid_t ptid)
774 struct thread_info *tp = find_thread_ptid (ptid);
778 gdb_assert (tp != NULL);
780 tp->stack_temporaries_enabled = 1;
781 tp->stack_temporaries = NULL;
782 data = XNEW (ptid_t);
784 c = make_cleanup (disable_thread_stack_temporaries, data);
789 /* Return non-zero value if stack temporaies are enabled for the thread
793 thread_stack_temporaries_enabled_p (ptid_t ptid)
795 struct thread_info *tp = find_thread_ptid (ptid);
800 return tp->stack_temporaries_enabled;
803 /* Push V on to the stack temporaries of the thread with id PTID. */
806 push_thread_stack_temporary (ptid_t ptid, struct value *v)
808 struct thread_info *tp = find_thread_ptid (ptid);
810 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
811 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
814 /* Return 1 if VAL is among the stack temporaries of the thread
815 with id PTID. Return 0 otherwise. */
818 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
820 struct thread_info *tp = find_thread_ptid (ptid);
822 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
823 if (!VEC_empty (value_ptr, tp->stack_temporaries))
828 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
836 /* Return the last of the stack temporaries for thread with id PTID.
837 Return NULL if there are no stack temporaries for the thread. */
840 get_last_thread_stack_temporary (ptid_t ptid)
842 struct value *lastval = NULL;
843 struct thread_info *tp = find_thread_ptid (ptid);
845 gdb_assert (tp != NULL);
846 if (!VEC_empty (value_ptr, tp->stack_temporaries))
847 lastval = VEC_last (value_ptr, tp->stack_temporaries);
853 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
855 struct inferior *inf;
856 struct thread_info *tp;
858 /* It can happen that what we knew as the target inferior id
859 changes. E.g, target remote may only discover the remote process
860 pid after adding the inferior to GDB's list. */
861 inf = find_inferior_ptid (old_ptid);
862 inf->pid = ptid_get_pid (new_ptid);
864 tp = find_thread_ptid (old_ptid);
867 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
870 /* See gdbthread.h. */
873 set_resumed (ptid_t ptid, int resumed)
875 struct thread_info *tp;
876 int all = ptid_equal (ptid, minus_one_ptid);
878 if (all || ptid_is_pid (ptid))
880 for (tp = thread_list; tp; tp = tp->next)
881 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
882 tp->resumed = resumed;
886 tp = find_thread_ptid (ptid);
887 gdb_assert (tp != NULL);
888 tp->resumed = resumed;
892 /* Helper for set_running, that marks one thread either running or
896 set_running_thread (struct thread_info *tp, int running)
900 if (running && tp->state == THREAD_STOPPED)
902 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
906 /* If the thread is now marked stopped, remove it from
907 the step-over queue, so that we don't try to resume
908 it until the user wants it to. */
909 if (tp->step_over_next != NULL)
910 thread_step_over_chain_remove (tp);
917 set_running (ptid_t ptid, int running)
919 struct thread_info *tp;
920 int all = ptid_equal (ptid, minus_one_ptid);
923 /* We try not to notify the observer if no thread has actually changed
924 the running state -- merely to reduce the number of messages to
925 frontend. Frontend is supposed to handle multiple *running just fine. */
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))
931 if (tp->state == THREAD_EXITED)
934 if (set_running_thread (tp, running))
940 tp = find_thread_ptid (ptid);
941 gdb_assert (tp != NULL);
942 gdb_assert (tp->state != THREAD_EXITED);
943 if (set_running_thread (tp, running))
947 observer_notify_target_resumed (ptid);
951 is_thread_state (ptid_t ptid, enum thread_state state)
953 struct thread_info *tp;
955 tp = find_thread_ptid (ptid);
957 return tp->state == state;
961 is_stopped (ptid_t ptid)
963 return is_thread_state (ptid, THREAD_STOPPED);
967 is_exited (ptid_t ptid)
969 return is_thread_state (ptid, THREAD_EXITED);
973 is_running (ptid_t ptid)
975 return is_thread_state (ptid, THREAD_RUNNING);
979 is_executing (ptid_t ptid)
981 struct thread_info *tp;
983 tp = find_thread_ptid (ptid);
985 return tp->executing;
989 set_executing (ptid_t ptid, int executing)
991 struct thread_info *tp;
992 int all = ptid_equal (ptid, minus_one_ptid);
994 if (all || ptid_is_pid (ptid))
996 for (tp = thread_list; tp; tp = tp->next)
997 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
998 tp->executing = executing;
1002 tp = find_thread_ptid (ptid);
1004 tp->executing = executing;
1007 /* It only takes one running thread to spawn more threads.*/
1009 threads_executing = 1;
1010 /* Only clear the flag if the caller is telling us everything is
1012 else if (ptid_equal (minus_one_ptid, ptid))
1013 threads_executing = 0;
1016 /* See gdbthread.h. */
1019 threads_are_executing (void)
1021 return threads_executing;
1025 set_stop_requested (ptid_t ptid, int stop)
1027 struct thread_info *tp;
1028 int all = ptid_equal (ptid, minus_one_ptid);
1030 if (all || ptid_is_pid (ptid))
1032 for (tp = thread_list; tp; tp = tp->next)
1033 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1034 tp->stop_requested = stop;
1038 tp = find_thread_ptid (ptid);
1040 tp->stop_requested = stop;
1043 /* Call the stop requested observer so other components of GDB can
1044 react to this request. */
1046 observer_notify_thread_stop_requested (ptid);
1050 finish_thread_state (ptid_t ptid)
1052 struct thread_info *tp;
1054 int any_started = 0;
1056 all = ptid_equal (ptid, minus_one_ptid);
1058 if (all || ptid_is_pid (ptid))
1060 for (tp = thread_list; tp; tp = tp->next)
1062 if (tp->state == THREAD_EXITED)
1064 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1066 if (set_running_thread (tp, tp->executing))
1073 tp = find_thread_ptid (ptid);
1075 if (tp->state != THREAD_EXITED)
1077 if (set_running_thread (tp, tp->executing))
1083 observer_notify_target_resumed (ptid);
1087 finish_thread_state_cleanup (void *arg)
1089 ptid_t *ptid_p = arg;
1093 finish_thread_state (*ptid_p);
1097 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1099 return (pc >= thread->control.step_range_start
1100 && pc < thread->control.step_range_end);
1103 /* Prints the list of threads and their details on UIOUT.
1104 This is a version of 'info_threads_command' suitable for
1106 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1107 that should be printed. Otherwise, all threads are
1109 If PID is not -1, only print threads from the process PID.
1110 Otherwise, threads from all attached PIDs are printed.
1111 If both REQUESTED_THREAD and PID are not -1, then the thread
1112 is printed if it belongs to the specified process. Otherwise,
1113 an error is raised. */
1115 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1117 struct thread_info *tp;
1118 ptid_t current_ptid;
1119 struct cleanup *old_chain;
1120 char *extra_info, *name, *target_id;
1121 int current_thread = -1;
1123 update_thread_list ();
1124 current_ptid = inferior_ptid;
1126 /* We'll be switching threads temporarily. */
1127 old_chain = make_cleanup_restore_current_thread ();
1129 /* For backward compatibility, we make a list for MI. A table is
1130 preferable for the CLI, though, because it shows table
1132 if (ui_out_is_mi_like_p (uiout))
1133 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1138 for (tp = thread_list; tp; tp = tp->next)
1140 if (!number_is_in_list (requested_threads, tp->num))
1143 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1146 if (tp->state == THREAD_EXITED)
1154 if (requested_threads == NULL || *requested_threads == '\0')
1155 ui_out_message (uiout, 0, _("No threads.\n"));
1157 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1159 do_cleanups (old_chain);
1163 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1165 ui_out_table_header (uiout, 1, ui_left, "current", "");
1166 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1167 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1168 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1169 ui_out_table_body (uiout);
1172 for (tp = thread_list; tp; tp = tp->next)
1174 struct cleanup *chain2;
1177 if (!number_is_in_list (requested_threads, tp->num))
1180 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1182 if (requested_threads != NULL && *requested_threads != '\0')
1183 error (_("Requested thread not found in requested process"));
1187 if (ptid_equal (tp->ptid, current_ptid))
1188 current_thread = tp->num;
1190 if (tp->state == THREAD_EXITED)
1193 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1195 if (ui_out_is_mi_like_p (uiout))
1197 /* Compatibility. */
1198 if (ptid_equal (tp->ptid, current_ptid))
1199 ui_out_text (uiout, "* ");
1201 ui_out_text (uiout, " ");
1205 if (ptid_equal (tp->ptid, current_ptid))
1206 ui_out_field_string (uiout, "current", "*");
1208 ui_out_field_skip (uiout, "current");
1211 ui_out_field_int (uiout, "id", tp->num);
1213 /* For the CLI, we stuff everything into the target-id field.
1214 This is a gross hack to make the output come out looking
1215 correct. The underlying problem here is that ui-out has no
1216 way to specify that a field's space allocation should be
1217 shared by several fields. For MI, we do the right thing
1220 target_id = target_pid_to_str (tp->ptid);
1221 extra_info = target_extra_thread_info (tp);
1222 name = tp->name ? tp->name : target_thread_name (tp);
1224 if (ui_out_is_mi_like_p (uiout))
1226 ui_out_field_string (uiout, "target-id", target_id);
1228 ui_out_field_string (uiout, "details", extra_info);
1230 ui_out_field_string (uiout, "name", name);
1234 struct cleanup *str_cleanup;
1237 if (extra_info && name)
1238 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1240 else if (extra_info)
1241 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1243 contents = xstrprintf ("%s \"%s\"", target_id, name);
1245 contents = xstrdup (target_id);
1246 str_cleanup = make_cleanup (xfree, contents);
1248 ui_out_field_string (uiout, "target-id", contents);
1249 do_cleanups (str_cleanup);
1252 if (tp->state == THREAD_RUNNING)
1253 ui_out_text (uiout, "(running)\n");
1256 /* The switch below puts us at the top of the stack (leaf
1258 switch_to_thread (tp->ptid);
1259 print_stack_frame (get_selected_frame (NULL),
1260 /* For MI output, print frame level. */
1261 ui_out_is_mi_like_p (uiout),
1265 if (ui_out_is_mi_like_p (uiout))
1267 char *state = "stopped";
1269 if (tp->state == THREAD_RUNNING)
1271 ui_out_field_string (uiout, "state", state);
1274 core = target_core_of_thread (tp->ptid);
1275 if (ui_out_is_mi_like_p (uiout) && core != -1)
1276 ui_out_field_int (uiout, "core", core);
1278 do_cleanups (chain2);
1281 /* Restores the current thread and the frame selected before
1282 the "info threads" command. */
1283 do_cleanups (old_chain);
1285 if (pid == -1 && requested_threads == NULL)
1287 gdb_assert (current_thread != -1
1289 || ptid_equal (inferior_ptid, null_ptid));
1290 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1291 ui_out_field_int (uiout, "current-thread-id", current_thread);
1293 if (current_thread != -1 && is_exited (current_ptid))
1294 ui_out_message (uiout, 0, "\n\
1295 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1297 else if (thread_list
1298 && current_thread == -1
1299 && ptid_equal (current_ptid, null_ptid))
1300 ui_out_message (uiout, 0, "\n\
1301 No selected thread. See `help thread'.\n");
1305 /* Print information about currently known threads
1307 Optional ARG is a thread id, or list of thread ids.
1309 Note: this has the drawback that it _really_ switches
1310 threads, which frees the frame cache. A no-side
1311 effects info-threads command would be nicer. */
1314 info_threads_command (char *arg, int from_tty)
1316 print_thread_info (current_uiout, arg, -1);
1319 /* Switch from one thread to another. */
1322 switch_to_thread (ptid_t ptid)
1324 /* Switch the program space as well, if we can infer it from the now
1325 current thread. Otherwise, it's up to the caller to select the
1327 if (!ptid_equal (ptid, null_ptid))
1329 struct inferior *inf;
1331 inf = find_inferior_ptid (ptid);
1332 gdb_assert (inf != NULL);
1333 set_current_program_space (inf->pspace);
1334 set_current_inferior (inf);
1337 if (ptid_equal (ptid, inferior_ptid))
1340 inferior_ptid = ptid;
1341 reinit_frame_cache ();
1343 /* We don't check for is_stopped, because we're called at times
1344 while in the TARGET_RUNNING state, e.g., while handling an
1346 if (!ptid_equal (inferior_ptid, null_ptid)
1347 && !is_exited (ptid)
1348 && !is_executing (ptid))
1349 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1351 stop_pc = ~(CORE_ADDR) 0;
1355 restore_current_thread (ptid_t ptid)
1357 switch_to_thread (ptid);
1361 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1363 struct frame_info *frame = NULL;
1366 /* This means there was no selected frame. */
1367 if (frame_level == -1)
1369 select_frame (NULL);
1373 gdb_assert (frame_level >= 0);
1375 /* Restore by level first, check if the frame id is the same as
1376 expected. If that fails, try restoring by frame id. If that
1377 fails, nothing to do, just warn the user. */
1379 count = frame_level;
1380 frame = find_relative_frame (get_current_frame (), &count);
1383 /* The frame ids must match - either both valid or both outer_frame_id.
1384 The latter case is not failsafe, but since it's highly unlikely
1385 the search by level finds the wrong frame, it's 99.9(9)% of
1386 the time (for all practical purposes) safe. */
1387 && frame_id_eq (get_frame_id (frame), a_frame_id))
1389 /* Cool, all is fine. */
1390 select_frame (frame);
1394 frame = frame_find_by_id (a_frame_id);
1397 /* Cool, refound it. */
1398 select_frame (frame);
1402 /* Nothing else to do, the frame layout really changed. Select the
1403 innermost stack frame. */
1404 select_frame (get_current_frame ());
1406 /* Warn the user. */
1407 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1409 warning (_("Couldn't restore frame #%d in "
1410 "current thread. Bottom (innermost) frame selected:"),
1412 /* For MI, we should probably have a notification about
1413 current frame change. But this error is not very
1414 likely, so don't bother for now. */
1415 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1419 /* Data used by the cleanup installed by
1420 'make_cleanup_restore_current_thread'. */
1422 struct current_thread_cleanup
1424 /* Next in list of currently installed 'struct
1425 current_thread_cleanup' cleanups. See
1426 'current_thread_cleanup_chain' below. */
1427 struct current_thread_cleanup *next;
1429 ptid_t inferior_ptid;
1430 struct frame_id selected_frame_id;
1431 int selected_frame_level;
1437 /* A chain of currently installed 'struct current_thread_cleanup'
1438 cleanups. Restoring the previously selected thread looks up the
1439 old thread in the thread list by ptid. If the thread changes ptid,
1440 we need to update the cleanup's thread structure so the look up
1442 static struct current_thread_cleanup *current_thread_cleanup_chain;
1444 /* A thread_ptid_changed observer. Update all currently installed
1445 current_thread_cleanup cleanups that want to switch back to
1446 OLD_PTID to switch back to NEW_PTID instead. */
1449 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1451 struct current_thread_cleanup *it;
1453 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1455 if (ptid_equal (it->inferior_ptid, old_ptid))
1456 it->inferior_ptid = new_ptid;
1461 do_restore_current_thread_cleanup (void *arg)
1463 struct thread_info *tp;
1464 struct current_thread_cleanup *old = arg;
1466 tp = find_thread_ptid (old->inferior_ptid);
1468 /* If the previously selected thread belonged to a process that has
1469 in the mean time been deleted (due to normal exit, detach, etc.),
1470 then don't revert back to it, but instead simply drop back to no
1473 && find_inferior_ptid (tp->ptid) != NULL)
1474 restore_current_thread (old->inferior_ptid);
1477 restore_current_thread (null_ptid);
1478 set_current_inferior (find_inferior_id (old->inf_id));
1481 /* The running state of the originally selected thread may have
1482 changed, so we have to recheck it here. */
1483 if (!ptid_equal (inferior_ptid, null_ptid)
1485 && is_stopped (inferior_ptid)
1486 && target_has_registers
1488 && target_has_memory)
1489 restore_selected_frame (old->selected_frame_id,
1490 old->selected_frame_level);
1494 restore_current_thread_cleanup_dtor (void *arg)
1496 struct current_thread_cleanup *old = arg;
1497 struct thread_info *tp;
1498 struct inferior *inf;
1500 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1502 tp = find_thread_ptid (old->inferior_ptid);
1505 inf = find_inferior_id (old->inf_id);
1507 inf->removable = old->was_removable;
1511 /* Set the thread reference count. */
1514 set_thread_refcount (void *data)
1517 struct thread_array_cleanup *ta_cleanup = data;
1519 for (k = 0; k != ta_cleanup->count; k++)
1520 ta_cleanup->tp_array[k]->refcount--;
1524 make_cleanup_restore_current_thread (void)
1526 struct thread_info *tp;
1527 struct frame_info *frame;
1528 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1530 old->inferior_ptid = inferior_ptid;
1531 old->inf_id = current_inferior ()->num;
1532 old->was_removable = current_inferior ()->removable;
1534 old->next = current_thread_cleanup_chain;
1535 current_thread_cleanup_chain = old;
1537 if (!ptid_equal (inferior_ptid, null_ptid))
1539 old->was_stopped = is_stopped (inferior_ptid);
1540 if (old->was_stopped
1541 && target_has_registers
1543 && target_has_memory)
1545 /* When processing internal events, there might not be a
1546 selected frame. If we naively call get_selected_frame
1547 here, then we can end up reading debuginfo for the
1548 current frame, but we don't generally need the debuginfo
1550 frame = get_selected_frame_if_set ();
1555 old->selected_frame_id = get_frame_id (frame);
1556 old->selected_frame_level = frame_relative_level (frame);
1558 tp = find_thread_ptid (inferior_ptid);
1563 current_inferior ()->removable = 0;
1565 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1566 restore_current_thread_cleanup_dtor);
1569 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1570 descending order. */
1572 static int tp_array_compar_ascending;
1574 /* Sort an array for struct thread_info pointers by their NUM, order is
1575 determined by TP_ARRAY_COMPAR_ASCENDING. */
1578 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1580 const struct thread_info *const *ap = ap_voidp;
1581 const struct thread_info *const *bp = bp_voidp;
1583 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1584 * (tp_array_compar_ascending ? +1 : -1));
1587 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1588 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1589 of two numbers seperated by a hyphen. Examples:
1591 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1592 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1593 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1596 thread_apply_all_command (char *cmd, int from_tty)
1598 struct cleanup *old_chain;
1601 struct thread_array_cleanup ta_cleanup;
1603 tp_array_compar_ascending = 0;
1605 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1607 cmd = skip_spaces (cmd);
1608 tp_array_compar_ascending = 1;
1611 if (cmd == NULL || *cmd == '\000')
1612 error (_("Please specify a command following the thread ID list"));
1614 update_thread_list ();
1616 old_chain = make_cleanup_restore_current_thread ();
1618 /* Save a copy of the command in case it is clobbered by
1620 saved_cmd = xstrdup (cmd);
1621 make_cleanup (xfree, saved_cmd);
1623 /* Note this includes exited threads. */
1624 tc = thread_count ();
1627 struct thread_info **tp_array;
1628 struct thread_info *tp;
1631 /* Save a copy of the thread_list in case we execute detach
1633 tp_array = XNEWVEC (struct thread_info *, tc);
1634 make_cleanup (xfree, tp_array);
1636 ALL_NON_EXITED_THREADS (tp)
1642 /* Because we skipped exited threads, we may end up with fewer
1643 threads in the array than the total count of threads. */
1644 gdb_assert (i <= tc);
1647 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1649 ta_cleanup.tp_array = tp_array;
1650 ta_cleanup.count = i;
1651 make_cleanup (set_thread_refcount, &ta_cleanup);
1653 for (k = 0; k != i; k++)
1654 if (thread_alive (tp_array[k]))
1656 switch_to_thread (tp_array[k]->ptid);
1657 printf_filtered (_("\nThread %d (%s):\n"),
1659 target_pid_to_str (inferior_ptid));
1660 execute_command (cmd, from_tty);
1662 /* Restore exact command used previously. */
1663 strcpy (cmd, saved_cmd);
1667 do_cleanups (old_chain);
1671 thread_apply_command (char *tidlist, int from_tty)
1674 struct cleanup *old_chain;
1676 struct get_number_or_range_state state;
1678 if (tidlist == NULL || *tidlist == '\000')
1679 error (_("Please specify a thread ID list"));
1681 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1684 error (_("Please specify a command following the thread ID list"));
1686 /* Save a copy of the command in case it is clobbered by
1688 saved_cmd = xstrdup (cmd);
1689 old_chain = make_cleanup (xfree, saved_cmd);
1691 init_number_or_range (&state, tidlist);
1692 while (!state.finished && state.string < cmd)
1694 struct thread_info *tp;
1697 start = get_number_or_range (&state);
1699 make_cleanup_restore_current_thread ();
1701 tp = find_thread_id (start);
1704 warning (_("Unknown thread %d."), start);
1705 else if (!thread_alive (tp))
1706 warning (_("Thread %d has terminated."), start);
1709 switch_to_thread (tp->ptid);
1711 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1712 target_pid_to_str (inferior_ptid));
1713 execute_command (cmd, from_tty);
1715 /* Restore exact command used previously. */
1716 strcpy (cmd, saved_cmd);
1720 do_cleanups (old_chain);
1723 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1724 if prefix of arg is `apply'. */
1727 thread_command (char *tidstr, int from_tty)
1731 if (ptid_equal (inferior_ptid, null_ptid))
1732 error (_("No thread selected"));
1734 if (target_has_stack)
1736 if (is_exited (inferior_ptid))
1737 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1738 pid_to_thread_id (inferior_ptid),
1739 target_pid_to_str (inferior_ptid));
1741 printf_filtered (_("[Current thread is %d (%s)]\n"),
1742 pid_to_thread_id (inferior_ptid),
1743 target_pid_to_str (inferior_ptid));
1746 error (_("No stack."));
1750 gdb_thread_select (current_uiout, tidstr, NULL);
1753 /* Implementation of `thread name'. */
1756 thread_name_command (char *arg, int from_tty)
1758 struct thread_info *info;
1760 if (ptid_equal (inferior_ptid, null_ptid))
1761 error (_("No thread selected"));
1763 arg = skip_spaces (arg);
1765 info = inferior_thread ();
1767 info->name = arg ? xstrdup (arg) : NULL;
1770 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1773 thread_find_command (char *arg, int from_tty)
1775 struct thread_info *tp;
1777 unsigned long match = 0;
1779 if (arg == NULL || *arg == '\0')
1780 error (_("Command requires an argument."));
1782 tmp = re_comp (arg);
1784 error (_("Invalid regexp (%s): %s"), tmp, arg);
1786 update_thread_list ();
1787 for (tp = thread_list; tp; tp = tp->next)
1789 if (tp->name != NULL && re_exec (tp->name))
1791 printf_filtered (_("Thread %d has name '%s'\n"),
1796 tmp = target_thread_name (tp);
1797 if (tmp != NULL && re_exec (tmp))
1799 printf_filtered (_("Thread %d has target name '%s'\n"),
1804 tmp = target_pid_to_str (tp->ptid);
1805 if (tmp != NULL && re_exec (tmp))
1807 printf_filtered (_("Thread %d has target id '%s'\n"),
1812 tmp = target_extra_thread_info (tp);
1813 if (tmp != NULL && re_exec (tmp))
1815 printf_filtered (_("Thread %d has extra info '%s'\n"),
1821 printf_filtered (_("No threads match '%s'\n"), arg);
1824 /* Print notices when new threads are attached and detached. */
1825 int print_thread_events = 1;
1827 show_print_thread_events (struct ui_file *file, int from_tty,
1828 struct cmd_list_element *c, const char *value)
1830 fprintf_filtered (file,
1831 _("Printing of thread events is %s.\n"),
1836 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1839 struct thread_info *tp;
1841 num = value_as_long (parse_and_eval (tidstr));
1843 tp = find_thread_id (num);
1846 error (_("Thread ID %d not known."), num);
1848 if (!thread_alive (tp))
1849 error (_("Thread ID %d has terminated."), num);
1851 switch_to_thread (tp->ptid);
1853 annotate_thread_changed ();
1855 ui_out_text (uiout, "[Switching to thread ");
1856 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1857 ui_out_text (uiout, " (");
1858 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1859 ui_out_text (uiout, ")]");
1861 /* Note that we can't reach this with an exited thread, due to the
1862 thread_alive check above. */
1863 if (tp->state == THREAD_RUNNING)
1864 ui_out_text (uiout, "(running)\n");
1867 ui_out_text (uiout, "\n");
1868 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1871 /* Since the current thread may have changed, see if there is any
1872 exited thread we can now delete. */
1879 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1881 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1882 error_message, RETURN_MASK_ALL) < 0)
1887 /* Update the 'threads_executing' global based on the threads we know
1891 update_threads_executing (void)
1893 struct thread_info *tp;
1895 threads_executing = 0;
1896 ALL_NON_EXITED_THREADS (tp)
1900 threads_executing = 1;
1907 update_thread_list (void)
1909 target_update_thread_list ();
1910 update_threads_executing ();
1913 /* Return a new value for the selected thread's id. Return a value of 0 if
1914 no thread is selected, or no threads exist. */
1916 static struct value *
1917 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1920 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1922 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1923 (tp ? tp->num : 0));
1926 /* Commands with a prefix of `thread'. */
1927 struct cmd_list_element *thread_cmd_list = NULL;
1929 /* Implementation of `thread' variable. */
1931 static const struct internalvar_funcs thread_funcs =
1933 thread_id_make_value,
1939 _initialize_thread (void)
1941 static struct cmd_list_element *thread_apply_list = NULL;
1943 add_info ("threads", info_threads_command,
1944 _("Display currently known threads.\n\
1945 Usage: info threads [ID]...\n\
1946 Optional arguments are thread IDs with spaces between.\n\
1947 If no arguments, all threads are displayed."));
1949 add_prefix_cmd ("thread", class_run, thread_command, _("\
1950 Use this command to switch between threads.\n\
1951 The new thread ID must be currently known."),
1952 &thread_cmd_list, "thread ", 1, &cmdlist);
1954 add_prefix_cmd ("apply", class_run, thread_apply_command,
1955 _("Apply a command to a list of threads."),
1956 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1958 add_cmd ("all", class_run, thread_apply_all_command,
1960 Apply a command to all threads.\n\
1962 Usage: thread apply all [-ascending] <command>\n\
1963 -ascending: Call <command> for all threads in ascending order.\n\
1964 The default is descending order.\
1966 &thread_apply_list);
1968 add_cmd ("name", class_run, thread_name_command,
1969 _("Set the current thread's name.\n\
1970 Usage: thread name [NAME]\n\
1971 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1973 add_cmd ("find", class_run, thread_find_command, _("\
1974 Find threads that match a regular expression.\n\
1975 Usage: thread find REGEXP\n\
1976 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1979 add_com_alias ("t", "thread", class_run, 1);
1981 add_setshow_boolean_cmd ("thread-events", no_class,
1982 &print_thread_events, _("\
1983 Set printing of thread events (such as thread start and exit)."), _("\
1984 Show printing of thread events (such as thread start and exit)."), NULL,
1986 show_print_thread_events,
1987 &setprintlist, &showprintlist);
1989 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1991 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);