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 "thread-fsm.h"
47 /* Definition of struct thread_info exported to gdbthread.h. */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 struct thread_info *thread_list = NULL;
56 static int highest_thread_num;
58 /* True if any thread is, or may be executing. We need to track this
59 separately because until we fully sync the thread list, we won't
60 know whether the target is fully stopped, even if we see stop
61 events for all known threads, because any of those threads may have
62 spawned new threads we haven't heard of yet. */
63 static int threads_executing;
65 static void thread_apply_all_command (char *, int);
66 static int thread_alive (struct thread_info *);
67 static void info_threads_command (char *, int);
68 static void thread_apply_command (char *, int);
69 static void restore_current_thread (ptid_t);
71 /* Data to cleanup thread array. */
73 struct thread_array_cleanup
75 /* Array of thread pointers used to set
77 struct thread_info **tp_array;
79 /* Thread count in the array. */
85 inferior_thread (void)
87 struct thread_info *tp = find_thread_ptid (inferior_ptid);
92 /* Delete the breakpoint pointed at by BP_P, if there's one. */
95 delete_thread_breakpoint (struct breakpoint **bp_p)
99 delete_breakpoint (*bp_p);
105 delete_step_resume_breakpoint (struct thread_info *tp)
108 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
112 delete_exception_resume_breakpoint (struct thread_info *tp)
115 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
118 /* See gdbthread.h. */
121 delete_single_step_breakpoints (struct thread_info *tp)
124 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
127 /* Delete the breakpoint pointed at by BP_P at the next stop, if
131 delete_at_next_stop (struct breakpoint **bp)
135 (*bp)->disposition = disp_del_at_next_stop;
140 /* See gdbthread.h. */
143 thread_has_single_step_breakpoints_set (struct thread_info *tp)
145 return tp->control.single_step_breakpoints != NULL;
148 /* See gdbthread.h. */
151 thread_has_single_step_breakpoint_here (struct thread_info *tp,
152 struct address_space *aspace,
155 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
157 return (ss_bps != NULL
158 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
161 /* See gdbthread.h. */
164 thread_cancel_execution_command (struct thread_info *thr)
166 if (thr->thread_fsm != NULL)
168 thread_fsm_clean_up (thr->thread_fsm);
169 thread_fsm_delete (thr->thread_fsm);
170 thr->thread_fsm = NULL;
175 clear_thread_inferior_resources (struct thread_info *tp)
177 /* NOTE: this will take care of any left-over step_resume breakpoints,
178 but not any user-specified thread-specific breakpoints. We can not
179 delete the breakpoint straight-off, because the inferior might not
180 be stopped at the moment. */
181 delete_at_next_stop (&tp->control.step_resume_breakpoint);
182 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
183 delete_at_next_stop (&tp->control.single_step_breakpoints);
185 delete_longjmp_breakpoint_at_next_stop (tp->num);
187 bpstat_clear (&tp->control.stop_bpstat);
189 btrace_teardown (tp);
191 thread_cancel_execution_command (tp);
195 free_thread (struct thread_info *tp)
199 if (tp->private_dtor)
200 tp->private_dtor (tp->priv);
210 init_thread_list (void)
212 struct thread_info *tp, *tpnext;
214 highest_thread_num = 0;
219 for (tp = thread_list; tp; tp = tpnext)
226 threads_executing = 0;
229 /* Allocate a new thread with target id PTID and add it to the thread
232 static struct thread_info *
233 new_thread (ptid_t ptid)
235 struct thread_info *tp = XCNEW (struct thread_info);
238 tp->num = ++highest_thread_num;
240 if (thread_list == NULL)
244 struct thread_info *last;
246 for (last = thread_list; last->next != NULL; last = last->next)
251 /* Nothing to follow yet. */
252 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
253 tp->state = THREAD_STOPPED;
254 tp->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
260 add_thread_silent (ptid_t ptid)
262 struct thread_info *tp;
264 tp = find_thread_ptid (ptid);
266 /* Found an old thread with the same id. It has to be dead,
267 otherwise we wouldn't be adding a new thread with the same id.
268 The OS is reusing this id --- delete it, and recreate a new
271 /* In addition to deleting the thread, if this is the current
272 thread, then we need to take care that delete_thread doesn't
273 really delete the thread if it is inferior_ptid. Create a
274 new template thread in the list with an invalid ptid, switch
275 to it, delete the original thread, reset the new thread's
276 ptid, and switch to it. */
278 if (ptid_equal (inferior_ptid, ptid))
280 tp = new_thread (null_ptid);
282 /* Make switch_to_thread not read from the thread. */
283 tp->state = THREAD_EXITED;
284 switch_to_thread (null_ptid);
286 /* Now we can delete it. */
287 delete_thread (ptid);
289 /* Now reset its ptid, and reswitch inferior_ptid to it. */
291 tp->state = THREAD_STOPPED;
292 switch_to_thread (ptid);
294 observer_notify_new_thread (tp);
300 /* Just go ahead and delete it. */
301 delete_thread (ptid);
304 tp = new_thread (ptid);
305 observer_notify_new_thread (tp);
311 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
313 struct thread_info *result = add_thread_silent (ptid);
317 if (print_thread_events)
318 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
320 annotate_new_thread ();
325 add_thread (ptid_t ptid)
327 return add_thread_with_info (ptid, NULL);
330 /* Add TP to the end of the step-over chain LIST_P. */
333 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
335 gdb_assert (tp->step_over_next == NULL);
336 gdb_assert (tp->step_over_prev == NULL);
341 tp->step_over_prev = tp->step_over_next = tp;
345 struct thread_info *head = *list_p;
346 struct thread_info *tail = head->step_over_prev;
348 tp->step_over_prev = tail;
349 tp->step_over_next = head;
350 head->step_over_prev = tp;
351 tail->step_over_next = tp;
355 /* Remove TP from step-over chain LIST_P. */
358 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
360 gdb_assert (tp->step_over_next != NULL);
361 gdb_assert (tp->step_over_prev != NULL);
365 if (tp == tp->step_over_next)
368 *list_p = tp->step_over_next;
371 tp->step_over_prev->step_over_next = tp->step_over_next;
372 tp->step_over_next->step_over_prev = tp->step_over_prev;
373 tp->step_over_prev = tp->step_over_next = NULL;
376 /* See gdbthread.h. */
379 thread_step_over_chain_next (struct thread_info *tp)
381 struct thread_info *next = tp->step_over_next;
383 return (next == step_over_queue_head ? NULL : next);
386 /* See gdbthread.h. */
389 thread_is_in_step_over_chain (struct thread_info *tp)
391 return (tp->step_over_next != NULL);
394 /* See gdbthread.h. */
397 thread_step_over_chain_enqueue (struct thread_info *tp)
399 step_over_chain_enqueue (&step_over_queue_head, tp);
402 /* See gdbthread.h. */
405 thread_step_over_chain_remove (struct thread_info *tp)
407 step_over_chain_remove (&step_over_queue_head, tp);
410 /* Delete thread PTID. If SILENT, don't notify the observer of this
413 delete_thread_1 (ptid_t ptid, int silent)
415 struct thread_info *tp, *tpprev;
419 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
420 if (ptid_equal (tp->ptid, ptid))
426 /* Dead threads don't need to step-over. Remove from queue. */
427 if (tp->step_over_next != NULL)
428 thread_step_over_chain_remove (tp);
430 /* If this is the current thread, or there's code out there that
431 relies on it existing (refcount > 0) we can't delete yet. Mark
432 it as exited, and notify it. */
434 || ptid_equal (tp->ptid, inferior_ptid))
436 if (tp->state != THREAD_EXITED)
438 observer_notify_thread_exit (tp, silent);
440 /* Tag it as exited. */
441 tp->state = THREAD_EXITED;
443 /* Clear breakpoints, etc. associated with this thread. */
444 clear_thread_inferior_resources (tp);
447 /* Will be really deleted some other time. */
451 /* Notify thread exit, but only if we haven't already. */
452 if (tp->state != THREAD_EXITED)
453 observer_notify_thread_exit (tp, silent);
455 /* Tag it as exited. */
456 tp->state = THREAD_EXITED;
457 clear_thread_inferior_resources (tp);
460 tpprev->next = tp->next;
462 thread_list = tp->next;
467 /* Delete thread PTID and notify of thread exit. If this is
468 inferior_ptid, don't actually delete it, but tag it as exited and
469 do the notification. If PTID is the user selected thread, clear
472 delete_thread (ptid_t ptid)
474 delete_thread_1 (ptid, 0 /* not silent */);
478 delete_thread_silent (ptid_t ptid)
480 delete_thread_1 (ptid, 1 /* silent */);
484 find_thread_id (int num)
486 struct thread_info *tp;
488 for (tp = thread_list; tp; tp = tp->next)
495 /* Find a thread_info by matching PTID. */
497 find_thread_ptid (ptid_t ptid)
499 struct thread_info *tp;
501 for (tp = thread_list; tp; tp = tp->next)
502 if (ptid_equal (tp->ptid, ptid))
509 * Thread iterator function.
511 * Calls a callback function once for each thread, so long as
512 * the callback function returns false. If the callback function
513 * returns true, the iteration will end and the current thread
514 * will be returned. This can be useful for implementing a
515 * search for a thread with arbitrary attributes, or for applying
516 * some operation to every thread.
518 * FIXME: some of the existing functionality, such as
519 * "Thread apply all", might be rewritten using this functionality.
523 iterate_over_threads (int (*callback) (struct thread_info *, void *),
526 struct thread_info *tp, *next;
528 for (tp = thread_list; tp; tp = next)
531 if ((*callback) (tp, data))
542 struct thread_info *tp;
544 for (tp = thread_list; tp; tp = tp->next)
551 valid_thread_id (int num)
553 struct thread_info *tp;
555 for (tp = thread_list; tp; tp = tp->next)
563 pid_to_thread_id (ptid_t ptid)
565 struct thread_info *tp;
567 for (tp = thread_list; tp; tp = tp->next)
568 if (ptid_equal (tp->ptid, ptid))
575 thread_id_to_pid (int num)
577 struct thread_info *thread = find_thread_id (num);
582 return pid_to_ptid (-1);
586 in_thread_list (ptid_t ptid)
588 struct thread_info *tp;
590 for (tp = thread_list; tp; tp = tp->next)
591 if (ptid_equal (tp->ptid, ptid))
594 return 0; /* Never heard of 'im. */
597 /* Finds the first thread of the inferior given by PID. If PID is -1,
598 return the first thread in the list. */
601 first_thread_of_process (int pid)
603 struct thread_info *tp, *ret = NULL;
605 for (tp = thread_list; tp; tp = tp->next)
606 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
607 if (ret == NULL || tp->num < ret->num)
614 any_thread_of_process (int pid)
616 struct thread_info *tp;
618 gdb_assert (pid != 0);
620 /* Prefer the current thread. */
621 if (ptid_get_pid (inferior_ptid) == pid)
622 return inferior_thread ();
624 ALL_NON_EXITED_THREADS (tp)
625 if (ptid_get_pid (tp->ptid) == pid)
632 any_live_thread_of_process (int pid)
634 struct thread_info *curr_tp = NULL;
635 struct thread_info *tp;
636 struct thread_info *tp_executing = NULL;
638 gdb_assert (pid != 0);
640 /* Prefer the current thread if it's not executing. */
641 if (ptid_get_pid (inferior_ptid) == pid)
643 /* If the current thread is dead, forget it. If it's not
644 executing, use it. Otherwise, still choose it (below), but
645 only if no other non-executing thread is found. */
646 curr_tp = inferior_thread ();
647 if (curr_tp->state == THREAD_EXITED)
649 else if (!curr_tp->executing)
653 ALL_NON_EXITED_THREADS (tp)
654 if (ptid_get_pid (tp->ptid) == pid)
662 /* If both the current thread and all live threads are executing,
663 prefer the current thread. */
667 /* Otherwise, just return an executing thread, if any. */
671 /* Print a list of thread ids currently known, and the total number of
672 threads. To be used from within catch_errors. */
674 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
676 struct thread_info *tp;
678 struct cleanup *cleanup_chain;
679 int current_thread = -1;
681 update_thread_list ();
683 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
685 for (tp = thread_list; tp; tp = tp->next)
687 if (tp->state == THREAD_EXITED)
690 if (ptid_equal (tp->ptid, inferior_ptid))
691 current_thread = tp->num;
694 ui_out_field_int (uiout, "thread-id", tp->num);
697 do_cleanups (cleanup_chain);
699 if (current_thread != -1)
700 ui_out_field_int (uiout, "current-thread-id", current_thread);
701 ui_out_field_int (uiout, "number-of-threads", num);
705 /* Official gdblib interface function to get a list of thread ids and
708 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
710 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
711 error_message, RETURN_MASK_ALL) < 0)
716 /* Return true if TP is an active thread. */
718 thread_alive (struct thread_info *tp)
720 if (tp->state == THREAD_EXITED)
722 if (!target_thread_alive (tp->ptid))
727 /* See gdbthreads.h. */
732 struct thread_info *tp, *tmp;
734 ALL_THREADS_SAFE (tp, tmp)
736 if (!thread_alive (tp))
737 delete_thread (tp->ptid);
741 /* See gdbthreads.h. */
744 delete_exited_threads (void)
746 struct thread_info *tp, *tmp;
748 ALL_THREADS_SAFE (tp, tmp)
750 if (tp->state == THREAD_EXITED)
751 delete_thread (tp->ptid);
755 /* Disable storing stack temporaries for the thread whose id is
759 disable_thread_stack_temporaries (void *data)
761 ptid_t *pd = (ptid_t *) data;
762 struct thread_info *tp = find_thread_ptid (*pd);
766 tp->stack_temporaries_enabled = 0;
767 VEC_free (value_ptr, tp->stack_temporaries);
773 /* Enable storing stack temporaries for thread with id PTID and return a
774 cleanup which can disable and clear the stack temporaries. */
777 enable_thread_stack_temporaries (ptid_t ptid)
779 struct thread_info *tp = find_thread_ptid (ptid);
783 gdb_assert (tp != NULL);
785 tp->stack_temporaries_enabled = 1;
786 tp->stack_temporaries = NULL;
787 data = XNEW (ptid_t);
789 c = make_cleanup (disable_thread_stack_temporaries, data);
794 /* Return non-zero value if stack temporaies are enabled for the thread
798 thread_stack_temporaries_enabled_p (ptid_t ptid)
800 struct thread_info *tp = find_thread_ptid (ptid);
805 return tp->stack_temporaries_enabled;
808 /* Push V on to the stack temporaries of the thread with id PTID. */
811 push_thread_stack_temporary (ptid_t ptid, struct value *v)
813 struct thread_info *tp = find_thread_ptid (ptid);
815 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
816 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
819 /* Return 1 if VAL is among the stack temporaries of the thread
820 with id PTID. Return 0 otherwise. */
823 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
825 struct thread_info *tp = find_thread_ptid (ptid);
827 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
828 if (!VEC_empty (value_ptr, tp->stack_temporaries))
833 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
841 /* Return the last of the stack temporaries for thread with id PTID.
842 Return NULL if there are no stack temporaries for the thread. */
845 get_last_thread_stack_temporary (ptid_t ptid)
847 struct value *lastval = NULL;
848 struct thread_info *tp = find_thread_ptid (ptid);
850 gdb_assert (tp != NULL);
851 if (!VEC_empty (value_ptr, tp->stack_temporaries))
852 lastval = VEC_last (value_ptr, tp->stack_temporaries);
858 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
860 struct inferior *inf;
861 struct thread_info *tp;
863 /* It can happen that what we knew as the target inferior id
864 changes. E.g, target remote may only discover the remote process
865 pid after adding the inferior to GDB's list. */
866 inf = find_inferior_ptid (old_ptid);
867 inf->pid = ptid_get_pid (new_ptid);
869 tp = find_thread_ptid (old_ptid);
872 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
875 /* See gdbthread.h. */
878 set_resumed (ptid_t ptid, int resumed)
880 struct thread_info *tp;
881 int all = ptid_equal (ptid, minus_one_ptid);
883 if (all || ptid_is_pid (ptid))
885 for (tp = thread_list; tp; tp = tp->next)
886 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
887 tp->resumed = resumed;
891 tp = find_thread_ptid (ptid);
892 gdb_assert (tp != NULL);
893 tp->resumed = resumed;
897 /* Helper for set_running, that marks one thread either running or
901 set_running_thread (struct thread_info *tp, int running)
905 if (running && tp->state == THREAD_STOPPED)
907 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
911 /* If the thread is now marked stopped, remove it from
912 the step-over queue, so that we don't try to resume
913 it until the user wants it to. */
914 if (tp->step_over_next != NULL)
915 thread_step_over_chain_remove (tp);
922 set_running (ptid_t ptid, int running)
924 struct thread_info *tp;
925 int all = ptid_equal (ptid, minus_one_ptid);
928 /* We try not to notify the observer if no thread has actually changed
929 the running state -- merely to reduce the number of messages to
930 frontend. Frontend is supposed to handle multiple *running just fine. */
931 if (all || ptid_is_pid (ptid))
933 for (tp = thread_list; tp; tp = tp->next)
934 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
936 if (tp->state == THREAD_EXITED)
939 if (set_running_thread (tp, running))
945 tp = find_thread_ptid (ptid);
946 gdb_assert (tp != NULL);
947 gdb_assert (tp->state != THREAD_EXITED);
948 if (set_running_thread (tp, running))
952 observer_notify_target_resumed (ptid);
956 is_thread_state (ptid_t ptid, enum thread_state state)
958 struct thread_info *tp;
960 tp = find_thread_ptid (ptid);
962 return tp->state == state;
966 is_stopped (ptid_t ptid)
968 return is_thread_state (ptid, THREAD_STOPPED);
972 is_exited (ptid_t ptid)
974 return is_thread_state (ptid, THREAD_EXITED);
978 is_running (ptid_t ptid)
980 return is_thread_state (ptid, THREAD_RUNNING);
984 is_executing (ptid_t ptid)
986 struct thread_info *tp;
988 tp = find_thread_ptid (ptid);
990 return tp->executing;
994 set_executing (ptid_t ptid, int executing)
996 struct thread_info *tp;
997 int all = ptid_equal (ptid, minus_one_ptid);
999 if (all || ptid_is_pid (ptid))
1001 for (tp = thread_list; tp; tp = tp->next)
1002 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1003 tp->executing = executing;
1007 tp = find_thread_ptid (ptid);
1009 tp->executing = executing;
1012 /* It only takes one running thread to spawn more threads.*/
1014 threads_executing = 1;
1015 /* Only clear the flag if the caller is telling us everything is
1017 else if (ptid_equal (minus_one_ptid, ptid))
1018 threads_executing = 0;
1021 /* See gdbthread.h. */
1024 threads_are_executing (void)
1026 return threads_executing;
1030 set_stop_requested (ptid_t ptid, int stop)
1032 struct thread_info *tp;
1033 int all = ptid_equal (ptid, minus_one_ptid);
1035 if (all || ptid_is_pid (ptid))
1037 for (tp = thread_list; tp; tp = tp->next)
1038 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1039 tp->stop_requested = stop;
1043 tp = find_thread_ptid (ptid);
1045 tp->stop_requested = stop;
1048 /* Call the stop requested observer so other components of GDB can
1049 react to this request. */
1051 observer_notify_thread_stop_requested (ptid);
1055 finish_thread_state (ptid_t ptid)
1057 struct thread_info *tp;
1059 int any_started = 0;
1061 all = ptid_equal (ptid, minus_one_ptid);
1063 if (all || ptid_is_pid (ptid))
1065 for (tp = thread_list; tp; tp = tp->next)
1067 if (tp->state == THREAD_EXITED)
1069 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1071 if (set_running_thread (tp, tp->executing))
1078 tp = find_thread_ptid (ptid);
1080 if (tp->state != THREAD_EXITED)
1082 if (set_running_thread (tp, tp->executing))
1088 observer_notify_target_resumed (ptid);
1092 finish_thread_state_cleanup (void *arg)
1094 ptid_t *ptid_p = (ptid_t *) arg;
1098 finish_thread_state (*ptid_p);
1102 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1104 return (pc >= thread->control.step_range_start
1105 && pc < thread->control.step_range_end);
1108 /* Prints the list of threads and their details on UIOUT.
1109 This is a version of 'info_threads_command' suitable for
1111 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1112 that should be printed. Otherwise, all threads are
1114 If PID is not -1, only print threads from the process PID.
1115 Otherwise, threads from all attached PIDs are printed.
1116 If both REQUESTED_THREAD and PID are not -1, then the thread
1117 is printed if it belongs to the specified process. Otherwise,
1118 an error is raised. */
1120 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1122 struct thread_info *tp;
1123 ptid_t current_ptid;
1124 struct cleanup *old_chain;
1125 char *extra_info, *name, *target_id;
1126 int current_thread = -1;
1128 update_thread_list ();
1129 current_ptid = inferior_ptid;
1131 /* We'll be switching threads temporarily. */
1132 old_chain = make_cleanup_restore_current_thread ();
1134 /* For backward compatibility, we make a list for MI. A table is
1135 preferable for the CLI, though, because it shows table
1137 if (ui_out_is_mi_like_p (uiout))
1138 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1143 for (tp = thread_list; tp; tp = tp->next)
1145 if (!number_is_in_list (requested_threads, tp->num))
1148 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1151 if (tp->state == THREAD_EXITED)
1159 if (requested_threads == NULL || *requested_threads == '\0')
1160 ui_out_message (uiout, 0, _("No threads.\n"));
1162 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1164 do_cleanups (old_chain);
1168 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1170 ui_out_table_header (uiout, 1, ui_left, "current", "");
1171 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1172 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1173 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1174 ui_out_table_body (uiout);
1177 for (tp = thread_list; tp; tp = tp->next)
1179 struct cleanup *chain2;
1182 if (!number_is_in_list (requested_threads, tp->num))
1185 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1187 if (requested_threads != NULL && *requested_threads != '\0')
1188 error (_("Requested thread not found in requested process"));
1192 if (ptid_equal (tp->ptid, current_ptid))
1193 current_thread = tp->num;
1195 if (tp->state == THREAD_EXITED)
1198 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1200 if (ui_out_is_mi_like_p (uiout))
1202 /* Compatibility. */
1203 if (ptid_equal (tp->ptid, current_ptid))
1204 ui_out_text (uiout, "* ");
1206 ui_out_text (uiout, " ");
1210 if (ptid_equal (tp->ptid, current_ptid))
1211 ui_out_field_string (uiout, "current", "*");
1213 ui_out_field_skip (uiout, "current");
1216 ui_out_field_int (uiout, "id", tp->num);
1218 /* For the CLI, we stuff everything into the target-id field.
1219 This is a gross hack to make the output come out looking
1220 correct. The underlying problem here is that ui-out has no
1221 way to specify that a field's space allocation should be
1222 shared by several fields. For MI, we do the right thing
1225 target_id = target_pid_to_str (tp->ptid);
1226 extra_info = target_extra_thread_info (tp);
1227 name = tp->name ? tp->name : target_thread_name (tp);
1229 if (ui_out_is_mi_like_p (uiout))
1231 ui_out_field_string (uiout, "target-id", target_id);
1233 ui_out_field_string (uiout, "details", extra_info);
1235 ui_out_field_string (uiout, "name", name);
1239 struct cleanup *str_cleanup;
1242 if (extra_info && name)
1243 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1245 else if (extra_info)
1246 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1248 contents = xstrprintf ("%s \"%s\"", target_id, name);
1250 contents = xstrdup (target_id);
1251 str_cleanup = make_cleanup (xfree, contents);
1253 ui_out_field_string (uiout, "target-id", contents);
1254 do_cleanups (str_cleanup);
1257 if (tp->state == THREAD_RUNNING)
1258 ui_out_text (uiout, "(running)\n");
1261 /* The switch below puts us at the top of the stack (leaf
1263 switch_to_thread (tp->ptid);
1264 print_stack_frame (get_selected_frame (NULL),
1265 /* For MI output, print frame level. */
1266 ui_out_is_mi_like_p (uiout),
1270 if (ui_out_is_mi_like_p (uiout))
1272 char *state = "stopped";
1274 if (tp->state == THREAD_RUNNING)
1276 ui_out_field_string (uiout, "state", state);
1279 core = target_core_of_thread (tp->ptid);
1280 if (ui_out_is_mi_like_p (uiout) && core != -1)
1281 ui_out_field_int (uiout, "core", core);
1283 do_cleanups (chain2);
1286 /* Restores the current thread and the frame selected before
1287 the "info threads" command. */
1288 do_cleanups (old_chain);
1290 if (pid == -1 && requested_threads == NULL)
1292 gdb_assert (current_thread != -1
1294 || ptid_equal (inferior_ptid, null_ptid));
1295 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1296 ui_out_field_int (uiout, "current-thread-id", current_thread);
1298 if (current_thread != -1 && is_exited (current_ptid))
1299 ui_out_message (uiout, 0, "\n\
1300 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1302 else if (thread_list
1303 && current_thread == -1
1304 && ptid_equal (current_ptid, null_ptid))
1305 ui_out_message (uiout, 0, "\n\
1306 No selected thread. See `help thread'.\n");
1310 /* Print information about currently known threads
1312 Optional ARG is a thread id, or list of thread ids.
1314 Note: this has the drawback that it _really_ switches
1315 threads, which frees the frame cache. A no-side
1316 effects info-threads command would be nicer. */
1319 info_threads_command (char *arg, int from_tty)
1321 print_thread_info (current_uiout, arg, -1);
1324 /* Switch from one thread to another. */
1327 switch_to_thread (ptid_t ptid)
1329 /* Switch the program space as well, if we can infer it from the now
1330 current thread. Otherwise, it's up to the caller to select the
1332 if (!ptid_equal (ptid, null_ptid))
1334 struct inferior *inf;
1336 inf = find_inferior_ptid (ptid);
1337 gdb_assert (inf != NULL);
1338 set_current_program_space (inf->pspace);
1339 set_current_inferior (inf);
1342 if (ptid_equal (ptid, inferior_ptid))
1345 inferior_ptid = ptid;
1346 reinit_frame_cache ();
1348 /* We don't check for is_stopped, because we're called at times
1349 while in the TARGET_RUNNING state, e.g., while handling an
1351 if (!ptid_equal (inferior_ptid, null_ptid)
1352 && !is_exited (ptid)
1353 && !is_executing (ptid))
1354 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1356 stop_pc = ~(CORE_ADDR) 0;
1360 restore_current_thread (ptid_t ptid)
1362 switch_to_thread (ptid);
1366 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1368 struct frame_info *frame = NULL;
1371 /* This means there was no selected frame. */
1372 if (frame_level == -1)
1374 select_frame (NULL);
1378 gdb_assert (frame_level >= 0);
1380 /* Restore by level first, check if the frame id is the same as
1381 expected. If that fails, try restoring by frame id. If that
1382 fails, nothing to do, just warn the user. */
1384 count = frame_level;
1385 frame = find_relative_frame (get_current_frame (), &count);
1388 /* The frame ids must match - either both valid or both outer_frame_id.
1389 The latter case is not failsafe, but since it's highly unlikely
1390 the search by level finds the wrong frame, it's 99.9(9)% of
1391 the time (for all practical purposes) safe. */
1392 && frame_id_eq (get_frame_id (frame), a_frame_id))
1394 /* Cool, all is fine. */
1395 select_frame (frame);
1399 frame = frame_find_by_id (a_frame_id);
1402 /* Cool, refound it. */
1403 select_frame (frame);
1407 /* Nothing else to do, the frame layout really changed. Select the
1408 innermost stack frame. */
1409 select_frame (get_current_frame ());
1411 /* Warn the user. */
1412 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1414 warning (_("Couldn't restore frame #%d in "
1415 "current thread. Bottom (innermost) frame selected:"),
1417 /* For MI, we should probably have a notification about
1418 current frame change. But this error is not very
1419 likely, so don't bother for now. */
1420 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1424 /* Data used by the cleanup installed by
1425 'make_cleanup_restore_current_thread'. */
1427 struct current_thread_cleanup
1429 /* Next in list of currently installed 'struct
1430 current_thread_cleanup' cleanups. See
1431 'current_thread_cleanup_chain' below. */
1432 struct current_thread_cleanup *next;
1434 ptid_t inferior_ptid;
1435 struct frame_id selected_frame_id;
1436 int selected_frame_level;
1442 /* A chain of currently installed 'struct current_thread_cleanup'
1443 cleanups. Restoring the previously selected thread looks up the
1444 old thread in the thread list by ptid. If the thread changes ptid,
1445 we need to update the cleanup's thread structure so the look up
1447 static struct current_thread_cleanup *current_thread_cleanup_chain;
1449 /* A thread_ptid_changed observer. Update all currently installed
1450 current_thread_cleanup cleanups that want to switch back to
1451 OLD_PTID to switch back to NEW_PTID instead. */
1454 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1456 struct current_thread_cleanup *it;
1458 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1460 if (ptid_equal (it->inferior_ptid, old_ptid))
1461 it->inferior_ptid = new_ptid;
1466 do_restore_current_thread_cleanup (void *arg)
1468 struct thread_info *tp;
1469 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1471 tp = find_thread_ptid (old->inferior_ptid);
1473 /* If the previously selected thread belonged to a process that has
1474 in the mean time been deleted (due to normal exit, detach, etc.),
1475 then don't revert back to it, but instead simply drop back to no
1478 && find_inferior_ptid (tp->ptid) != NULL)
1479 restore_current_thread (old->inferior_ptid);
1482 restore_current_thread (null_ptid);
1483 set_current_inferior (find_inferior_id (old->inf_id));
1486 /* The running state of the originally selected thread may have
1487 changed, so we have to recheck it here. */
1488 if (!ptid_equal (inferior_ptid, null_ptid)
1490 && is_stopped (inferior_ptid)
1491 && target_has_registers
1493 && target_has_memory)
1494 restore_selected_frame (old->selected_frame_id,
1495 old->selected_frame_level);
1499 restore_current_thread_cleanup_dtor (void *arg)
1501 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1502 struct thread_info *tp;
1503 struct inferior *inf;
1505 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1507 tp = find_thread_ptid (old->inferior_ptid);
1510 inf = find_inferior_id (old->inf_id);
1512 inf->removable = old->was_removable;
1516 /* Set the thread reference count. */
1519 set_thread_refcount (void *data)
1522 struct thread_array_cleanup *ta_cleanup
1523 = (struct thread_array_cleanup *) data;
1525 for (k = 0; k != ta_cleanup->count; k++)
1526 ta_cleanup->tp_array[k]->refcount--;
1530 make_cleanup_restore_current_thread (void)
1532 struct thread_info *tp;
1533 struct frame_info *frame;
1534 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1536 old->inferior_ptid = inferior_ptid;
1537 old->inf_id = current_inferior ()->num;
1538 old->was_removable = current_inferior ()->removable;
1540 old->next = current_thread_cleanup_chain;
1541 current_thread_cleanup_chain = old;
1543 if (!ptid_equal (inferior_ptid, null_ptid))
1545 old->was_stopped = is_stopped (inferior_ptid);
1546 if (old->was_stopped
1547 && target_has_registers
1549 && target_has_memory)
1551 /* When processing internal events, there might not be a
1552 selected frame. If we naively call get_selected_frame
1553 here, then we can end up reading debuginfo for the
1554 current frame, but we don't generally need the debuginfo
1556 frame = get_selected_frame_if_set ();
1561 old->selected_frame_id = get_frame_id (frame);
1562 old->selected_frame_level = frame_relative_level (frame);
1564 tp = find_thread_ptid (inferior_ptid);
1569 current_inferior ()->removable = 0;
1571 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1572 restore_current_thread_cleanup_dtor);
1575 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1576 descending order. */
1578 static int tp_array_compar_ascending;
1580 /* Sort an array for struct thread_info pointers by their NUM, order is
1581 determined by TP_ARRAY_COMPAR_ASCENDING. */
1584 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1586 const struct thread_info *const *ap
1587 = (const struct thread_info * const*) ap_voidp;
1588 const struct thread_info *const *bp
1589 = (const struct thread_info * const*) bp_voidp;
1591 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1592 * (tp_array_compar_ascending ? +1 : -1));
1595 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1596 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1597 of two numbers seperated by a hyphen. Examples:
1599 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1600 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1601 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1604 thread_apply_all_command (char *cmd, int from_tty)
1606 struct cleanup *old_chain;
1609 struct thread_array_cleanup ta_cleanup;
1611 tp_array_compar_ascending = 0;
1613 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1615 cmd = skip_spaces (cmd);
1616 tp_array_compar_ascending = 1;
1619 if (cmd == NULL || *cmd == '\000')
1620 error (_("Please specify a command following the thread ID list"));
1622 update_thread_list ();
1624 old_chain = make_cleanup_restore_current_thread ();
1626 /* Save a copy of the command in case it is clobbered by
1628 saved_cmd = xstrdup (cmd);
1629 make_cleanup (xfree, saved_cmd);
1631 /* Note this includes exited threads. */
1632 tc = thread_count ();
1635 struct thread_info **tp_array;
1636 struct thread_info *tp;
1639 /* Save a copy of the thread_list in case we execute detach
1641 tp_array = XNEWVEC (struct thread_info *, tc);
1642 make_cleanup (xfree, tp_array);
1644 ALL_NON_EXITED_THREADS (tp)
1650 /* Because we skipped exited threads, we may end up with fewer
1651 threads in the array than the total count of threads. */
1652 gdb_assert (i <= tc);
1655 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1657 ta_cleanup.tp_array = tp_array;
1658 ta_cleanup.count = i;
1659 make_cleanup (set_thread_refcount, &ta_cleanup);
1661 for (k = 0; k != i; k++)
1662 if (thread_alive (tp_array[k]))
1664 switch_to_thread (tp_array[k]->ptid);
1665 printf_filtered (_("\nThread %d (%s):\n"),
1667 target_pid_to_str (inferior_ptid));
1668 execute_command (cmd, from_tty);
1670 /* Restore exact command used previously. */
1671 strcpy (cmd, saved_cmd);
1675 do_cleanups (old_chain);
1679 thread_apply_command (char *tidlist, int from_tty)
1682 struct cleanup *old_chain;
1684 struct get_number_or_range_state state;
1686 if (tidlist == NULL || *tidlist == '\000')
1687 error (_("Please specify a thread ID list"));
1689 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1692 error (_("Please specify a command following the thread ID list"));
1694 /* Save a copy of the command in case it is clobbered by
1696 saved_cmd = xstrdup (cmd);
1697 old_chain = make_cleanup (xfree, saved_cmd);
1699 init_number_or_range (&state, tidlist);
1700 while (!state.finished && state.string < cmd)
1702 struct thread_info *tp;
1705 start = get_number_or_range (&state);
1707 make_cleanup_restore_current_thread ();
1709 tp = find_thread_id (start);
1712 warning (_("Unknown thread %d."), start);
1713 else if (!thread_alive (tp))
1714 warning (_("Thread %d has terminated."), start);
1717 switch_to_thread (tp->ptid);
1719 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1720 target_pid_to_str (inferior_ptid));
1721 execute_command (cmd, from_tty);
1723 /* Restore exact command used previously. */
1724 strcpy (cmd, saved_cmd);
1728 do_cleanups (old_chain);
1731 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1732 if prefix of arg is `apply'. */
1735 thread_command (char *tidstr, int from_tty)
1739 if (ptid_equal (inferior_ptid, null_ptid))
1740 error (_("No thread selected"));
1742 if (target_has_stack)
1744 if (is_exited (inferior_ptid))
1745 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1746 pid_to_thread_id (inferior_ptid),
1747 target_pid_to_str (inferior_ptid));
1749 printf_filtered (_("[Current thread is %d (%s)]\n"),
1750 pid_to_thread_id (inferior_ptid),
1751 target_pid_to_str (inferior_ptid));
1754 error (_("No stack."));
1758 gdb_thread_select (current_uiout, tidstr, NULL);
1761 /* Implementation of `thread name'. */
1764 thread_name_command (char *arg, int from_tty)
1766 struct thread_info *info;
1768 if (ptid_equal (inferior_ptid, null_ptid))
1769 error (_("No thread selected"));
1771 arg = skip_spaces (arg);
1773 info = inferior_thread ();
1775 info->name = arg ? xstrdup (arg) : NULL;
1778 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1781 thread_find_command (char *arg, int from_tty)
1783 struct thread_info *tp;
1785 unsigned long match = 0;
1787 if (arg == NULL || *arg == '\0')
1788 error (_("Command requires an argument."));
1790 tmp = re_comp (arg);
1792 error (_("Invalid regexp (%s): %s"), tmp, arg);
1794 update_thread_list ();
1795 for (tp = thread_list; tp; tp = tp->next)
1797 if (tp->name != NULL && re_exec (tp->name))
1799 printf_filtered (_("Thread %d has name '%s'\n"),
1804 tmp = target_thread_name (tp);
1805 if (tmp != NULL && re_exec (tmp))
1807 printf_filtered (_("Thread %d has target name '%s'\n"),
1812 tmp = target_pid_to_str (tp->ptid);
1813 if (tmp != NULL && re_exec (tmp))
1815 printf_filtered (_("Thread %d has target id '%s'\n"),
1820 tmp = target_extra_thread_info (tp);
1821 if (tmp != NULL && re_exec (tmp))
1823 printf_filtered (_("Thread %d has extra info '%s'\n"),
1829 printf_filtered (_("No threads match '%s'\n"), arg);
1832 /* Print notices when new threads are attached and detached. */
1833 int print_thread_events = 1;
1835 show_print_thread_events (struct ui_file *file, int from_tty,
1836 struct cmd_list_element *c, const char *value)
1838 fprintf_filtered (file,
1839 _("Printing of thread events is %s.\n"),
1844 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1847 struct thread_info *tp;
1849 num = value_as_long (parse_and_eval ((const char *) tidstr));
1851 tp = find_thread_id (num);
1854 error (_("Thread ID %d not known."), num);
1856 if (!thread_alive (tp))
1857 error (_("Thread ID %d has terminated."), num);
1859 switch_to_thread (tp->ptid);
1861 annotate_thread_changed ();
1863 ui_out_text (uiout, "[Switching to thread ");
1864 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1865 ui_out_text (uiout, " (");
1866 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1867 ui_out_text (uiout, ")]");
1869 /* Note that we can't reach this with an exited thread, due to the
1870 thread_alive check above. */
1871 if (tp->state == THREAD_RUNNING)
1872 ui_out_text (uiout, "(running)\n");
1875 ui_out_text (uiout, "\n");
1876 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1879 /* Since the current thread may have changed, see if there is any
1880 exited thread we can now delete. */
1887 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1889 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1890 error_message, RETURN_MASK_ALL) < 0)
1895 /* Update the 'threads_executing' global based on the threads we know
1899 update_threads_executing (void)
1901 struct thread_info *tp;
1903 threads_executing = 0;
1904 ALL_NON_EXITED_THREADS (tp)
1908 threads_executing = 1;
1915 update_thread_list (void)
1917 target_update_thread_list ();
1918 update_threads_executing ();
1921 /* Return a new value for the selected thread's id. Return a value of 0 if
1922 no thread is selected, or no threads exist. */
1924 static struct value *
1925 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1928 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1930 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1931 (tp ? tp->num : 0));
1934 /* Commands with a prefix of `thread'. */
1935 struct cmd_list_element *thread_cmd_list = NULL;
1937 /* Implementation of `thread' variable. */
1939 static const struct internalvar_funcs thread_funcs =
1941 thread_id_make_value,
1947 _initialize_thread (void)
1949 static struct cmd_list_element *thread_apply_list = NULL;
1951 add_info ("threads", info_threads_command,
1952 _("Display currently known threads.\n\
1953 Usage: info threads [ID]...\n\
1954 Optional arguments are thread IDs with spaces between.\n\
1955 If no arguments, all threads are displayed."));
1957 add_prefix_cmd ("thread", class_run, thread_command, _("\
1958 Use this command to switch between threads.\n\
1959 The new thread ID must be currently known."),
1960 &thread_cmd_list, "thread ", 1, &cmdlist);
1962 add_prefix_cmd ("apply", class_run, thread_apply_command,
1963 _("Apply a command to a list of threads."),
1964 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1966 add_cmd ("all", class_run, thread_apply_all_command,
1968 Apply a command to all threads.\n\
1970 Usage: thread apply all [-ascending] <command>\n\
1971 -ascending: Call <command> for all threads in ascending order.\n\
1972 The default is descending order.\
1974 &thread_apply_list);
1976 add_cmd ("name", class_run, thread_name_command,
1977 _("Set the current thread's name.\n\
1978 Usage: thread name [NAME]\n\
1979 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1981 add_cmd ("find", class_run, thread_find_command, _("\
1982 Find threads that match a regular expression.\n\
1983 Usage: thread find REGEXP\n\
1984 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1987 add_com_alias ("t", "thread", class_run, 1);
1989 add_setshow_boolean_cmd ("thread-events", no_class,
1990 &print_thread_events, _("\
1991 Set printing of thread events (such as thread start and exit)."), _("\
1992 Show printing of thread events (such as thread start and exit)."), NULL,
1994 show_print_thread_events,
1995 &setprintlist, &showprintlist);
1997 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1999 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);