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"
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));
162 clear_thread_inferior_resources (struct thread_info *tp)
164 /* NOTE: this will take care of any left-over step_resume breakpoints,
165 but not any user-specified thread-specific breakpoints. We can not
166 delete the breakpoint straight-off, because the inferior might not
167 be stopped at the moment. */
168 delete_at_next_stop (&tp->control.step_resume_breakpoint);
169 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
170 delete_at_next_stop (&tp->control.single_step_breakpoints);
172 delete_longjmp_breakpoint_at_next_stop (tp->num);
174 bpstat_clear (&tp->control.stop_bpstat);
176 btrace_teardown (tp);
178 do_all_intermediate_continuations_thread (tp, 1);
179 do_all_continuations_thread (tp, 1);
183 free_thread (struct thread_info *tp)
187 if (tp->private_dtor)
188 tp->private_dtor (tp->priv);
198 init_thread_list (void)
200 struct thread_info *tp, *tpnext;
202 highest_thread_num = 0;
207 for (tp = thread_list; tp; tp = tpnext)
214 threads_executing = 0;
217 /* Allocate a new thread with target id PTID and add it to the thread
220 static struct thread_info *
221 new_thread (ptid_t ptid)
223 struct thread_info *tp;
225 tp = xcalloc (1, sizeof (*tp));
228 tp->num = ++highest_thread_num;
229 tp->next = thread_list;
232 /* Nothing to follow yet. */
233 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
234 tp->state = THREAD_STOPPED;
240 add_thread_silent (ptid_t ptid)
242 struct thread_info *tp;
244 tp = find_thread_ptid (ptid);
246 /* Found an old thread with the same id. It has to be dead,
247 otherwise we wouldn't be adding a new thread with the same id.
248 The OS is reusing this id --- delete it, and recreate a new
251 /* In addition to deleting the thread, if this is the current
252 thread, then we need to take care that delete_thread doesn't
253 really delete the thread if it is inferior_ptid. Create a
254 new template thread in the list with an invalid ptid, switch
255 to it, delete the original thread, reset the new thread's
256 ptid, and switch to it. */
258 if (ptid_equal (inferior_ptid, ptid))
260 tp = new_thread (null_ptid);
262 /* Make switch_to_thread not read from the thread. */
263 tp->state = THREAD_EXITED;
264 switch_to_thread (null_ptid);
266 /* Now we can delete it. */
267 delete_thread (ptid);
269 /* Now reset its ptid, and reswitch inferior_ptid to it. */
271 tp->state = THREAD_STOPPED;
272 switch_to_thread (ptid);
274 observer_notify_new_thread (tp);
280 /* Just go ahead and delete it. */
281 delete_thread (ptid);
284 tp = new_thread (ptid);
285 observer_notify_new_thread (tp);
291 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
293 struct thread_info *result = add_thread_silent (ptid);
297 if (print_thread_events)
298 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
300 annotate_new_thread ();
305 add_thread (ptid_t ptid)
307 return add_thread_with_info (ptid, NULL);
310 /* Add TP to the end of the step-over chain LIST_P. */
313 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
315 gdb_assert (tp->step_over_next == NULL);
316 gdb_assert (tp->step_over_prev == NULL);
321 tp->step_over_prev = tp->step_over_next = tp;
325 struct thread_info *head = *list_p;
326 struct thread_info *tail = head->step_over_prev;
328 tp->step_over_prev = tail;
329 tp->step_over_next = head;
330 head->step_over_prev = tp;
331 tail->step_over_next = tp;
335 /* Remove TP from step-over chain LIST_P. */
338 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
340 gdb_assert (tp->step_over_next != NULL);
341 gdb_assert (tp->step_over_prev != NULL);
345 if (tp == tp->step_over_next)
348 *list_p = tp->step_over_next;
351 tp->step_over_prev->step_over_next = tp->step_over_next;
352 tp->step_over_next->step_over_prev = tp->step_over_prev;
353 tp->step_over_prev = tp->step_over_next = NULL;
356 /* See gdbthread.h. */
359 thread_step_over_chain_next (struct thread_info *tp)
361 struct thread_info *next = tp->step_over_next;
363 return (next == step_over_queue_head ? NULL : next);
366 /* See gdbthread.h. */
369 thread_is_in_step_over_chain (struct thread_info *tp)
371 return (tp->step_over_next != NULL);
374 /* See gdbthread.h. */
377 thread_step_over_chain_enqueue (struct thread_info *tp)
379 step_over_chain_enqueue (&step_over_queue_head, tp);
382 /* See gdbthread.h. */
385 thread_step_over_chain_remove (struct thread_info *tp)
387 step_over_chain_remove (&step_over_queue_head, tp);
390 /* Delete thread PTID. If SILENT, don't notify the observer of this
393 delete_thread_1 (ptid_t ptid, int silent)
395 struct thread_info *tp, *tpprev;
399 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
400 if (ptid_equal (tp->ptid, ptid))
406 /* Dead threads don't need to step-over. Remove from queue. */
407 if (tp->step_over_next != NULL)
408 thread_step_over_chain_remove (tp);
410 /* If this is the current thread, or there's code out there that
411 relies on it existing (refcount > 0) we can't delete yet. Mark
412 it as exited, and notify it. */
414 || ptid_equal (tp->ptid, inferior_ptid))
416 if (tp->state != THREAD_EXITED)
418 observer_notify_thread_exit (tp, silent);
420 /* Tag it as exited. */
421 tp->state = THREAD_EXITED;
423 /* Clear breakpoints, etc. associated with this thread. */
424 clear_thread_inferior_resources (tp);
427 /* Will be really deleted some other time. */
431 /* Notify thread exit, but only if we haven't already. */
432 if (tp->state != THREAD_EXITED)
433 observer_notify_thread_exit (tp, silent);
435 /* Tag it as exited. */
436 tp->state = THREAD_EXITED;
437 clear_thread_inferior_resources (tp);
440 tpprev->next = tp->next;
442 thread_list = tp->next;
447 /* Delete thread PTID and notify of thread exit. If this is
448 inferior_ptid, don't actually delete it, but tag it as exited and
449 do the notification. If PTID is the user selected thread, clear
452 delete_thread (ptid_t ptid)
454 delete_thread_1 (ptid, 0 /* not silent */);
458 delete_thread_silent (ptid_t ptid)
460 delete_thread_1 (ptid, 1 /* silent */);
464 find_thread_id (int num)
466 struct thread_info *tp;
468 for (tp = thread_list; tp; tp = tp->next)
475 /* Find a thread_info by matching PTID. */
477 find_thread_ptid (ptid_t ptid)
479 struct thread_info *tp;
481 for (tp = thread_list; tp; tp = tp->next)
482 if (ptid_equal (tp->ptid, ptid))
489 * Thread iterator function.
491 * Calls a callback function once for each thread, so long as
492 * the callback function returns false. If the callback function
493 * returns true, the iteration will end and the current thread
494 * will be returned. This can be useful for implementing a
495 * search for a thread with arbitrary attributes, or for applying
496 * some operation to every thread.
498 * FIXME: some of the existing functionality, such as
499 * "Thread apply all", might be rewritten using this functionality.
503 iterate_over_threads (int (*callback) (struct thread_info *, void *),
506 struct thread_info *tp, *next;
508 for (tp = thread_list; tp; tp = next)
511 if ((*callback) (tp, data))
522 struct thread_info *tp;
524 for (tp = thread_list; tp; tp = tp->next)
531 valid_thread_id (int num)
533 struct thread_info *tp;
535 for (tp = thread_list; tp; tp = tp->next)
543 pid_to_thread_id (ptid_t ptid)
545 struct thread_info *tp;
547 for (tp = thread_list; tp; tp = tp->next)
548 if (ptid_equal (tp->ptid, ptid))
555 thread_id_to_pid (int num)
557 struct thread_info *thread = find_thread_id (num);
562 return pid_to_ptid (-1);
566 in_thread_list (ptid_t ptid)
568 struct thread_info *tp;
570 for (tp = thread_list; tp; tp = tp->next)
571 if (ptid_equal (tp->ptid, ptid))
574 return 0; /* Never heard of 'im. */
577 /* Finds the first thread of the inferior given by PID. If PID is -1,
578 return the first thread in the list. */
581 first_thread_of_process (int pid)
583 struct thread_info *tp, *ret = NULL;
585 for (tp = thread_list; tp; tp = tp->next)
586 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
587 if (ret == NULL || tp->num < ret->num)
594 any_thread_of_process (int pid)
596 struct thread_info *tp;
598 gdb_assert (pid != 0);
600 /* Prefer the current thread. */
601 if (ptid_get_pid (inferior_ptid) == pid)
602 return inferior_thread ();
604 ALL_NON_EXITED_THREADS (tp)
605 if (ptid_get_pid (tp->ptid) == pid)
612 any_live_thread_of_process (int pid)
614 struct thread_info *curr_tp = NULL;
615 struct thread_info *tp;
616 struct thread_info *tp_executing = NULL;
618 gdb_assert (pid != 0);
620 /* Prefer the current thread if it's not executing. */
621 if (ptid_get_pid (inferior_ptid) == pid)
623 /* If the current thread is dead, forget it. If it's not
624 executing, use it. Otherwise, still choose it (below), but
625 only if no other non-executing thread is found. */
626 curr_tp = inferior_thread ();
627 if (curr_tp->state == THREAD_EXITED)
629 else if (!curr_tp->executing)
633 ALL_NON_EXITED_THREADS (tp)
634 if (ptid_get_pid (tp->ptid) == pid)
642 /* If both the current thread and all live threads are executing,
643 prefer the current thread. */
647 /* Otherwise, just return an executing thread, if any. */
651 /* Print a list of thread ids currently known, and the total number of
652 threads. To be used from within catch_errors. */
654 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
656 struct thread_info *tp;
658 struct cleanup *cleanup_chain;
659 int current_thread = -1;
661 update_thread_list ();
663 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
665 for (tp = thread_list; tp; tp = tp->next)
667 if (tp->state == THREAD_EXITED)
670 if (ptid_equal (tp->ptid, inferior_ptid))
671 current_thread = tp->num;
674 ui_out_field_int (uiout, "thread-id", tp->num);
677 do_cleanups (cleanup_chain);
679 if (current_thread != -1)
680 ui_out_field_int (uiout, "current-thread-id", current_thread);
681 ui_out_field_int (uiout, "number-of-threads", num);
685 /* Official gdblib interface function to get a list of thread ids and
688 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
690 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
691 error_message, RETURN_MASK_ALL) < 0)
696 /* Return true if TP is an active thread. */
698 thread_alive (struct thread_info *tp)
700 if (tp->state == THREAD_EXITED)
702 if (!target_thread_alive (tp->ptid))
707 /* See gdbthreads.h. */
712 struct thread_info *tp, *tmp;
714 ALL_THREADS_SAFE (tp, tmp)
716 if (!thread_alive (tp))
717 delete_thread (tp->ptid);
721 /* See gdbthreads.h. */
724 delete_exited_threads (void)
726 struct thread_info *tp, *tmp;
728 ALL_THREADS_SAFE (tp, tmp)
730 if (tp->state == THREAD_EXITED)
731 delete_thread (tp->ptid);
735 /* Disable storing stack temporaries for the thread whose id is
739 disable_thread_stack_temporaries (void *data)
742 struct thread_info *tp = find_thread_ptid (*pd);
746 tp->stack_temporaries_enabled = 0;
747 VEC_free (value_ptr, tp->stack_temporaries);
753 /* Enable storing stack temporaries for thread with id PTID and return a
754 cleanup which can disable and clear the stack temporaries. */
757 enable_thread_stack_temporaries (ptid_t ptid)
759 struct thread_info *tp = find_thread_ptid (ptid);
763 gdb_assert (tp != NULL);
765 tp->stack_temporaries_enabled = 1;
766 tp->stack_temporaries = NULL;
767 data = (ptid_t *) xmalloc (sizeof (ptid_t));
769 c = make_cleanup (disable_thread_stack_temporaries, data);
774 /* Return non-zero value if stack temporaies are enabled for the thread
778 thread_stack_temporaries_enabled_p (ptid_t ptid)
780 struct thread_info *tp = find_thread_ptid (ptid);
785 return tp->stack_temporaries_enabled;
788 /* Push V on to the stack temporaries of the thread with id PTID. */
791 push_thread_stack_temporary (ptid_t ptid, struct value *v)
793 struct thread_info *tp = find_thread_ptid (ptid);
795 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
796 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
799 /* Return 1 if VAL is among the stack temporaries of the thread
800 with id PTID. Return 0 otherwise. */
803 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
805 struct thread_info *tp = find_thread_ptid (ptid);
807 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
808 if (!VEC_empty (value_ptr, tp->stack_temporaries))
813 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
821 /* Return the last of the stack temporaries for thread with id PTID.
822 Return NULL if there are no stack temporaries for the thread. */
825 get_last_thread_stack_temporary (ptid_t ptid)
827 struct value *lastval = NULL;
828 struct thread_info *tp = find_thread_ptid (ptid);
830 gdb_assert (tp != NULL);
831 if (!VEC_empty (value_ptr, tp->stack_temporaries))
832 lastval = VEC_last (value_ptr, tp->stack_temporaries);
838 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
840 struct inferior *inf;
841 struct thread_info *tp;
843 /* It can happen that what we knew as the target inferior id
844 changes. E.g, target remote may only discover the remote process
845 pid after adding the inferior to GDB's list. */
846 inf = find_inferior_ptid (old_ptid);
847 inf->pid = ptid_get_pid (new_ptid);
849 tp = find_thread_ptid (old_ptid);
852 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
855 /* Helper for set_running, that marks one thread either running or
859 set_running_thread (struct thread_info *tp, int running)
863 if (running && tp->state == THREAD_STOPPED)
865 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
869 /* If the thread is now marked stopped, remove it from
870 the step-over queue, so that we don't try to resume
871 it until the user wants it to. */
872 if (tp->step_over_next != NULL)
873 thread_step_over_chain_remove (tp);
880 set_running (ptid_t ptid, int running)
882 struct thread_info *tp;
883 int all = ptid_equal (ptid, minus_one_ptid);
886 /* We try not to notify the observer if no thread has actually changed
887 the running state -- merely to reduce the number of messages to
888 frontend. Frontend is supposed to handle multiple *running just fine. */
889 if (all || ptid_is_pid (ptid))
891 for (tp = thread_list; tp; tp = tp->next)
892 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
894 if (tp->state == THREAD_EXITED)
897 if (set_running_thread (tp, running))
903 tp = find_thread_ptid (ptid);
904 gdb_assert (tp != NULL);
905 gdb_assert (tp->state != THREAD_EXITED);
906 if (set_running_thread (tp, running))
910 observer_notify_target_resumed (ptid);
914 is_thread_state (ptid_t ptid, enum thread_state state)
916 struct thread_info *tp;
918 tp = find_thread_ptid (ptid);
920 return tp->state == state;
924 is_stopped (ptid_t ptid)
926 return is_thread_state (ptid, THREAD_STOPPED);
930 is_exited (ptid_t ptid)
932 return is_thread_state (ptid, THREAD_EXITED);
936 is_running (ptid_t ptid)
938 return is_thread_state (ptid, THREAD_RUNNING);
942 is_executing (ptid_t ptid)
944 struct thread_info *tp;
946 tp = find_thread_ptid (ptid);
948 return tp->executing;
952 set_executing (ptid_t ptid, int executing)
954 struct thread_info *tp;
955 int all = ptid_equal (ptid, minus_one_ptid);
957 if (all || ptid_is_pid (ptid))
959 for (tp = thread_list; tp; tp = tp->next)
960 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
961 tp->executing = executing;
965 tp = find_thread_ptid (ptid);
967 tp->executing = executing;
970 /* It only takes one running thread to spawn more threads.*/
972 threads_executing = 1;
973 /* Only clear the flag if the caller is telling us everything is
975 else if (ptid_equal (minus_one_ptid, ptid))
976 threads_executing = 0;
979 /* See gdbthread.h. */
982 threads_are_executing (void)
984 return threads_executing;
988 set_stop_requested (ptid_t ptid, int stop)
990 struct thread_info *tp;
991 int all = ptid_equal (ptid, minus_one_ptid);
993 if (all || ptid_is_pid (ptid))
995 for (tp = thread_list; tp; tp = tp->next)
996 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
997 tp->stop_requested = stop;
1001 tp = find_thread_ptid (ptid);
1003 tp->stop_requested = stop;
1006 /* Call the stop requested observer so other components of GDB can
1007 react to this request. */
1009 observer_notify_thread_stop_requested (ptid);
1013 finish_thread_state (ptid_t ptid)
1015 struct thread_info *tp;
1017 int any_started = 0;
1019 all = ptid_equal (ptid, minus_one_ptid);
1021 if (all || ptid_is_pid (ptid))
1023 for (tp = thread_list; tp; tp = tp->next)
1025 if (tp->state == THREAD_EXITED)
1027 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1029 if (set_running_thread (tp, tp->executing))
1036 tp = find_thread_ptid (ptid);
1038 if (tp->state != THREAD_EXITED)
1040 if (set_running_thread (tp, tp->executing))
1046 observer_notify_target_resumed (ptid);
1050 finish_thread_state_cleanup (void *arg)
1052 ptid_t *ptid_p = arg;
1056 finish_thread_state (*ptid_p);
1060 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1062 return (pc >= thread->control.step_range_start
1063 && pc < thread->control.step_range_end);
1066 /* Prints the list of threads and their details on UIOUT.
1067 This is a version of 'info_threads_command' suitable for
1069 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1070 that should be printed. Otherwise, all threads are
1072 If PID is not -1, only print threads from the process PID.
1073 Otherwise, threads from all attached PIDs are printed.
1074 If both REQUESTED_THREAD and PID are not -1, then the thread
1075 is printed if it belongs to the specified process. Otherwise,
1076 an error is raised. */
1078 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1080 struct thread_info *tp;
1081 ptid_t current_ptid;
1082 struct cleanup *old_chain;
1083 char *extra_info, *name, *target_id;
1084 int current_thread = -1;
1086 update_thread_list ();
1087 current_ptid = inferior_ptid;
1089 /* We'll be switching threads temporarily. */
1090 old_chain = make_cleanup_restore_current_thread ();
1092 /* For backward compatibility, we make a list for MI. A table is
1093 preferable for the CLI, though, because it shows table
1095 if (ui_out_is_mi_like_p (uiout))
1096 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1101 for (tp = thread_list; tp; tp = tp->next)
1103 if (!number_is_in_list (requested_threads, tp->num))
1106 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1109 if (tp->state == THREAD_EXITED)
1117 if (requested_threads == NULL || *requested_threads == '\0')
1118 ui_out_message (uiout, 0, _("No threads.\n"));
1120 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1122 do_cleanups (old_chain);
1126 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1128 ui_out_table_header (uiout, 1, ui_left, "current", "");
1129 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1130 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1131 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1132 ui_out_table_body (uiout);
1135 for (tp = thread_list; tp; tp = tp->next)
1137 struct cleanup *chain2;
1140 if (!number_is_in_list (requested_threads, tp->num))
1143 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1145 if (requested_threads != NULL && *requested_threads != '\0')
1146 error (_("Requested thread not found in requested process"));
1150 if (ptid_equal (tp->ptid, current_ptid))
1151 current_thread = tp->num;
1153 if (tp->state == THREAD_EXITED)
1156 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1158 if (ui_out_is_mi_like_p (uiout))
1160 /* Compatibility. */
1161 if (ptid_equal (tp->ptid, current_ptid))
1162 ui_out_text (uiout, "* ");
1164 ui_out_text (uiout, " ");
1168 if (ptid_equal (tp->ptid, current_ptid))
1169 ui_out_field_string (uiout, "current", "*");
1171 ui_out_field_skip (uiout, "current");
1174 ui_out_field_int (uiout, "id", tp->num);
1176 /* For the CLI, we stuff everything into the target-id field.
1177 This is a gross hack to make the output come out looking
1178 correct. The underlying problem here is that ui-out has no
1179 way to specify that a field's space allocation should be
1180 shared by several fields. For MI, we do the right thing
1183 target_id = target_pid_to_str (tp->ptid);
1184 extra_info = target_extra_thread_info (tp);
1185 name = tp->name ? tp->name : target_thread_name (tp);
1187 if (ui_out_is_mi_like_p (uiout))
1189 ui_out_field_string (uiout, "target-id", target_id);
1191 ui_out_field_string (uiout, "details", extra_info);
1193 ui_out_field_string (uiout, "name", name);
1197 struct cleanup *str_cleanup;
1200 if (extra_info && name)
1201 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1203 else if (extra_info)
1204 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1206 contents = xstrprintf ("%s \"%s\"", target_id, name);
1208 contents = xstrdup (target_id);
1209 str_cleanup = make_cleanup (xfree, contents);
1211 ui_out_field_string (uiout, "target-id", contents);
1212 do_cleanups (str_cleanup);
1215 if (tp->state == THREAD_RUNNING)
1216 ui_out_text (uiout, "(running)\n");
1219 /* The switch below puts us at the top of the stack (leaf
1221 switch_to_thread (tp->ptid);
1222 print_stack_frame (get_selected_frame (NULL),
1223 /* For MI output, print frame level. */
1224 ui_out_is_mi_like_p (uiout),
1228 if (ui_out_is_mi_like_p (uiout))
1230 char *state = "stopped";
1232 if (tp->state == THREAD_RUNNING)
1234 ui_out_field_string (uiout, "state", state);
1237 core = target_core_of_thread (tp->ptid);
1238 if (ui_out_is_mi_like_p (uiout) && core != -1)
1239 ui_out_field_int (uiout, "core", core);
1241 do_cleanups (chain2);
1244 /* Restores the current thread and the frame selected before
1245 the "info threads" command. */
1246 do_cleanups (old_chain);
1248 if (pid == -1 && requested_threads == NULL)
1250 gdb_assert (current_thread != -1
1252 || ptid_equal (inferior_ptid, null_ptid));
1253 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1254 ui_out_field_int (uiout, "current-thread-id", current_thread);
1256 if (current_thread != -1 && is_exited (current_ptid))
1257 ui_out_message (uiout, 0, "\n\
1258 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1260 else if (thread_list
1261 && current_thread == -1
1262 && ptid_equal (current_ptid, null_ptid))
1263 ui_out_message (uiout, 0, "\n\
1264 No selected thread. See `help thread'.\n");
1268 /* Print information about currently known threads
1270 Optional ARG is a thread id, or list of thread ids.
1272 Note: this has the drawback that it _really_ switches
1273 threads, which frees the frame cache. A no-side
1274 effects info-threads command would be nicer. */
1277 info_threads_command (char *arg, int from_tty)
1279 print_thread_info (current_uiout, arg, -1);
1282 /* Switch from one thread to another. */
1285 switch_to_thread (ptid_t ptid)
1287 /* Switch the program space as well, if we can infer it from the now
1288 current thread. Otherwise, it's up to the caller to select the
1290 if (!ptid_equal (ptid, null_ptid))
1292 struct inferior *inf;
1294 inf = find_inferior_ptid (ptid);
1295 gdb_assert (inf != NULL);
1296 set_current_program_space (inf->pspace);
1297 set_current_inferior (inf);
1300 if (ptid_equal (ptid, inferior_ptid))
1303 inferior_ptid = ptid;
1304 reinit_frame_cache ();
1306 /* We don't check for is_stopped, because we're called at times
1307 while in the TARGET_RUNNING state, e.g., while handling an
1309 if (!ptid_equal (inferior_ptid, null_ptid)
1310 && !is_exited (ptid)
1311 && !is_executing (ptid))
1312 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1314 stop_pc = ~(CORE_ADDR) 0;
1318 restore_current_thread (ptid_t ptid)
1320 switch_to_thread (ptid);
1324 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1326 struct frame_info *frame = NULL;
1329 /* This means there was no selected frame. */
1330 if (frame_level == -1)
1332 select_frame (NULL);
1336 gdb_assert (frame_level >= 0);
1338 /* Restore by level first, check if the frame id is the same as
1339 expected. If that fails, try restoring by frame id. If that
1340 fails, nothing to do, just warn the user. */
1342 count = frame_level;
1343 frame = find_relative_frame (get_current_frame (), &count);
1346 /* The frame ids must match - either both valid or both outer_frame_id.
1347 The latter case is not failsafe, but since it's highly unlikely
1348 the search by level finds the wrong frame, it's 99.9(9)% of
1349 the time (for all practical purposes) safe. */
1350 && frame_id_eq (get_frame_id (frame), a_frame_id))
1352 /* Cool, all is fine. */
1353 select_frame (frame);
1357 frame = frame_find_by_id (a_frame_id);
1360 /* Cool, refound it. */
1361 select_frame (frame);
1365 /* Nothing else to do, the frame layout really changed. Select the
1366 innermost stack frame. */
1367 select_frame (get_current_frame ());
1369 /* Warn the user. */
1370 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1372 warning (_("Couldn't restore frame #%d in "
1373 "current thread. Bottom (innermost) frame selected:"),
1375 /* For MI, we should probably have a notification about
1376 current frame change. But this error is not very
1377 likely, so don't bother for now. */
1378 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1382 /* Data used by the cleanup installed by
1383 'make_cleanup_restore_current_thread'. */
1385 struct current_thread_cleanup
1387 /* Next in list of currently installed 'struct
1388 current_thread_cleanup' cleanups. See
1389 'current_thread_cleanup_chain' below. */
1390 struct current_thread_cleanup *next;
1392 ptid_t inferior_ptid;
1393 struct frame_id selected_frame_id;
1394 int selected_frame_level;
1400 /* A chain of currently installed 'struct current_thread_cleanup'
1401 cleanups. Restoring the previously selected thread looks up the
1402 old thread in the thread list by ptid. If the thread changes ptid,
1403 we need to update the cleanup's thread structure so the look up
1405 static struct current_thread_cleanup *current_thread_cleanup_chain;
1407 /* A thread_ptid_changed observer. Update all currently installed
1408 current_thread_cleanup cleanups that want to switch back to
1409 OLD_PTID to switch back to NEW_PTID instead. */
1412 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1414 struct current_thread_cleanup *it;
1416 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1418 if (ptid_equal (it->inferior_ptid, old_ptid))
1419 it->inferior_ptid = new_ptid;
1424 do_restore_current_thread_cleanup (void *arg)
1426 struct thread_info *tp;
1427 struct current_thread_cleanup *old = arg;
1429 tp = find_thread_ptid (old->inferior_ptid);
1431 /* If the previously selected thread belonged to a process that has
1432 in the mean time been deleted (due to normal exit, detach, etc.),
1433 then don't revert back to it, but instead simply drop back to no
1436 && find_inferior_ptid (tp->ptid) != NULL)
1437 restore_current_thread (old->inferior_ptid);
1440 restore_current_thread (null_ptid);
1441 set_current_inferior (find_inferior_id (old->inf_id));
1444 /* The running state of the originally selected thread may have
1445 changed, so we have to recheck it here. */
1446 if (!ptid_equal (inferior_ptid, null_ptid)
1448 && is_stopped (inferior_ptid)
1449 && target_has_registers
1451 && target_has_memory)
1452 restore_selected_frame (old->selected_frame_id,
1453 old->selected_frame_level);
1457 restore_current_thread_cleanup_dtor (void *arg)
1459 struct current_thread_cleanup *old = arg;
1460 struct thread_info *tp;
1461 struct inferior *inf;
1463 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1465 tp = find_thread_ptid (old->inferior_ptid);
1468 inf = find_inferior_id (old->inf_id);
1470 inf->removable = old->was_removable;
1474 /* Set the thread reference count. */
1477 set_thread_refcount (void *data)
1480 struct thread_array_cleanup *ta_cleanup = data;
1482 for (k = 0; k != ta_cleanup->count; k++)
1483 ta_cleanup->tp_array[k]->refcount--;
1487 make_cleanup_restore_current_thread (void)
1489 struct thread_info *tp;
1490 struct frame_info *frame;
1491 struct current_thread_cleanup *old;
1493 old = xmalloc (sizeof (struct current_thread_cleanup));
1494 old->inferior_ptid = inferior_ptid;
1495 old->inf_id = current_inferior ()->num;
1496 old->was_removable = current_inferior ()->removable;
1498 old->next = current_thread_cleanup_chain;
1499 current_thread_cleanup_chain = old;
1501 if (!ptid_equal (inferior_ptid, null_ptid))
1503 old->was_stopped = is_stopped (inferior_ptid);
1504 if (old->was_stopped
1505 && target_has_registers
1507 && target_has_memory)
1509 /* When processing internal events, there might not be a
1510 selected frame. If we naively call get_selected_frame
1511 here, then we can end up reading debuginfo for the
1512 current frame, but we don't generally need the debuginfo
1514 frame = get_selected_frame_if_set ();
1519 old->selected_frame_id = get_frame_id (frame);
1520 old->selected_frame_level = frame_relative_level (frame);
1522 tp = find_thread_ptid (inferior_ptid);
1527 current_inferior ()->removable = 0;
1529 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1530 restore_current_thread_cleanup_dtor);
1533 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1534 descending order. */
1536 static int tp_array_compar_ascending;
1538 /* Sort an array for struct thread_info pointers by their NUM, order is
1539 determined by TP_ARRAY_COMPAR_ASCENDING. */
1542 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1544 const struct thread_info *const *ap = ap_voidp;
1545 const struct thread_info *const *bp = bp_voidp;
1547 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1548 * (tp_array_compar_ascending ? +1 : -1));
1551 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1552 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1553 of two numbers seperated by a hyphen. Examples:
1555 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1556 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1557 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1560 thread_apply_all_command (char *cmd, int from_tty)
1562 struct cleanup *old_chain;
1565 struct thread_array_cleanup ta_cleanup;
1567 tp_array_compar_ascending = 0;
1569 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1571 cmd = skip_spaces (cmd);
1572 tp_array_compar_ascending = 1;
1575 if (cmd == NULL || *cmd == '\000')
1576 error (_("Please specify a command following the thread ID list"));
1578 update_thread_list ();
1580 old_chain = make_cleanup_restore_current_thread ();
1582 /* Save a copy of the command in case it is clobbered by
1584 saved_cmd = xstrdup (cmd);
1585 make_cleanup (xfree, saved_cmd);
1587 /* Note this includes exited threads. */
1588 tc = thread_count ();
1591 struct thread_info **tp_array;
1592 struct thread_info *tp;
1595 /* Save a copy of the thread_list in case we execute detach
1597 tp_array = xmalloc (sizeof (struct thread_info *) * tc);
1598 make_cleanup (xfree, tp_array);
1600 ALL_NON_EXITED_THREADS (tp)
1606 /* Because we skipped exited threads, we may end up with fewer
1607 threads in the array than the total count of threads. */
1608 gdb_assert (i <= tc);
1611 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1613 ta_cleanup.tp_array = tp_array;
1614 ta_cleanup.count = i;
1615 make_cleanup (set_thread_refcount, &ta_cleanup);
1617 for (k = 0; k != i; k++)
1618 if (thread_alive (tp_array[k]))
1620 switch_to_thread (tp_array[k]->ptid);
1621 printf_filtered (_("\nThread %d (%s):\n"),
1623 target_pid_to_str (inferior_ptid));
1624 execute_command (cmd, from_tty);
1626 /* Restore exact command used previously. */
1627 strcpy (cmd, saved_cmd);
1631 do_cleanups (old_chain);
1635 thread_apply_command (char *tidlist, int from_tty)
1638 struct cleanup *old_chain;
1640 struct get_number_or_range_state state;
1642 if (tidlist == NULL || *tidlist == '\000')
1643 error (_("Please specify a thread ID list"));
1645 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1648 error (_("Please specify a command following the thread ID list"));
1650 /* Save a copy of the command in case it is clobbered by
1652 saved_cmd = xstrdup (cmd);
1653 old_chain = make_cleanup (xfree, saved_cmd);
1655 init_number_or_range (&state, tidlist);
1656 while (!state.finished && state.string < cmd)
1658 struct thread_info *tp;
1661 start = get_number_or_range (&state);
1663 make_cleanup_restore_current_thread ();
1665 tp = find_thread_id (start);
1668 warning (_("Unknown thread %d."), start);
1669 else if (!thread_alive (tp))
1670 warning (_("Thread %d has terminated."), start);
1673 switch_to_thread (tp->ptid);
1675 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1676 target_pid_to_str (inferior_ptid));
1677 execute_command (cmd, from_tty);
1679 /* Restore exact command used previously. */
1680 strcpy (cmd, saved_cmd);
1684 do_cleanups (old_chain);
1687 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1688 if prefix of arg is `apply'. */
1691 thread_command (char *tidstr, int from_tty)
1695 if (ptid_equal (inferior_ptid, null_ptid))
1696 error (_("No thread selected"));
1698 if (target_has_stack)
1700 if (is_exited (inferior_ptid))
1701 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1702 pid_to_thread_id (inferior_ptid),
1703 target_pid_to_str (inferior_ptid));
1705 printf_filtered (_("[Current thread is %d (%s)]\n"),
1706 pid_to_thread_id (inferior_ptid),
1707 target_pid_to_str (inferior_ptid));
1710 error (_("No stack."));
1714 gdb_thread_select (current_uiout, tidstr, NULL);
1717 /* Implementation of `thread name'. */
1720 thread_name_command (char *arg, int from_tty)
1722 struct thread_info *info;
1724 if (ptid_equal (inferior_ptid, null_ptid))
1725 error (_("No thread selected"));
1727 arg = skip_spaces (arg);
1729 info = inferior_thread ();
1731 info->name = arg ? xstrdup (arg) : NULL;
1734 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1737 thread_find_command (char *arg, int from_tty)
1739 struct thread_info *tp;
1741 unsigned long match = 0;
1743 if (arg == NULL || *arg == '\0')
1744 error (_("Command requires an argument."));
1746 tmp = re_comp (arg);
1748 error (_("Invalid regexp (%s): %s"), tmp, arg);
1750 update_thread_list ();
1751 for (tp = thread_list; tp; tp = tp->next)
1753 if (tp->name != NULL && re_exec (tp->name))
1755 printf_filtered (_("Thread %d has name '%s'\n"),
1760 tmp = target_thread_name (tp);
1761 if (tmp != NULL && re_exec (tmp))
1763 printf_filtered (_("Thread %d has target name '%s'\n"),
1768 tmp = target_pid_to_str (tp->ptid);
1769 if (tmp != NULL && re_exec (tmp))
1771 printf_filtered (_("Thread %d has target id '%s'\n"),
1776 tmp = target_extra_thread_info (tp);
1777 if (tmp != NULL && re_exec (tmp))
1779 printf_filtered (_("Thread %d has extra info '%s'\n"),
1785 printf_filtered (_("No threads match '%s'\n"), arg);
1788 /* Print notices when new threads are attached and detached. */
1789 int print_thread_events = 1;
1791 show_print_thread_events (struct ui_file *file, int from_tty,
1792 struct cmd_list_element *c, const char *value)
1794 fprintf_filtered (file,
1795 _("Printing of thread events is %s.\n"),
1800 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1803 struct thread_info *tp;
1805 num = value_as_long (parse_and_eval (tidstr));
1807 tp = find_thread_id (num);
1810 error (_("Thread ID %d not known."), num);
1812 if (!thread_alive (tp))
1813 error (_("Thread ID %d has terminated."), num);
1815 switch_to_thread (tp->ptid);
1817 annotate_thread_changed ();
1819 ui_out_text (uiout, "[Switching to thread ");
1820 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1821 ui_out_text (uiout, " (");
1822 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1823 ui_out_text (uiout, ")]");
1825 /* Note that we can't reach this with an exited thread, due to the
1826 thread_alive check above. */
1827 if (tp->state == THREAD_RUNNING)
1828 ui_out_text (uiout, "(running)\n");
1831 ui_out_text (uiout, "\n");
1832 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1835 /* Since the current thread may have changed, see if there is any
1836 exited thread we can now delete. */
1843 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1845 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1846 error_message, RETURN_MASK_ALL) < 0)
1851 /* Update the 'threads_executing' global based on the threads we know
1855 update_threads_executing (void)
1857 struct thread_info *tp;
1859 threads_executing = 0;
1860 ALL_NON_EXITED_THREADS (tp)
1864 threads_executing = 1;
1871 update_thread_list (void)
1873 target_update_thread_list ();
1874 update_threads_executing ();
1877 /* Return a new value for the selected thread's id. Return a value of 0 if
1878 no thread is selected, or no threads exist. */
1880 static struct value *
1881 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1884 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1886 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1887 (tp ? tp->num : 0));
1890 /* Commands with a prefix of `thread'. */
1891 struct cmd_list_element *thread_cmd_list = NULL;
1893 /* Implementation of `thread' variable. */
1895 static const struct internalvar_funcs thread_funcs =
1897 thread_id_make_value,
1903 _initialize_thread (void)
1905 static struct cmd_list_element *thread_apply_list = NULL;
1907 add_info ("threads", info_threads_command,
1908 _("Display currently known threads.\n\
1909 Usage: info threads [ID]...\n\
1910 Optional arguments are thread IDs with spaces between.\n\
1911 If no arguments, all threads are displayed."));
1913 add_prefix_cmd ("thread", class_run, thread_command, _("\
1914 Use this command to switch between threads.\n\
1915 The new thread ID must be currently known."),
1916 &thread_cmd_list, "thread ", 1, &cmdlist);
1918 add_prefix_cmd ("apply", class_run, thread_apply_command,
1919 _("Apply a command to a list of threads."),
1920 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1922 add_cmd ("all", class_run, thread_apply_all_command,
1924 Apply a command to all threads.\n\
1926 Usage: thread apply all [-ascending] <command>\n\
1927 -ascending: Call <command> for all threads in ascending order.\n\
1928 The default is descending order.\
1930 &thread_apply_list);
1932 add_cmd ("name", class_run, thread_name_command,
1933 _("Set the current thread's name.\n\
1934 Usage: thread name [NAME]\n\
1935 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1937 add_cmd ("find", class_run, thread_find_command, _("\
1938 Find threads that match a regular expression.\n\
1939 Usage: thread find REGEXP\n\
1940 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1943 add_com_alias ("t", "thread", class_run, 1);
1945 add_setshow_boolean_cmd ("thread-events", no_class,
1946 &print_thread_events, _("\
1947 Set printing of thread events (such as thread start and exit)."), _("\
1948 Show printing of thread events (such as thread start and exit)."), NULL,
1950 show_print_thread_events,
1951 &setprintlist, &showprintlist);
1953 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1955 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);