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 /* Delete thread PTID. If SILENT, don't notify the observer of this
313 delete_thread_1 (ptid_t ptid, int silent)
315 struct thread_info *tp, *tpprev;
319 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
320 if (ptid_equal (tp->ptid, ptid))
326 /* If this is the current thread, or there's code out there that
327 relies on it existing (refcount > 0) we can't delete yet. Mark
328 it as exited, and notify it. */
330 || ptid_equal (tp->ptid, inferior_ptid))
332 if (tp->state != THREAD_EXITED)
334 observer_notify_thread_exit (tp, silent);
336 /* Tag it as exited. */
337 tp->state = THREAD_EXITED;
339 /* Clear breakpoints, etc. associated with this thread. */
340 clear_thread_inferior_resources (tp);
343 /* Will be really deleted some other time. */
347 /* Notify thread exit, but only if we haven't already. */
348 if (tp->state != THREAD_EXITED)
349 observer_notify_thread_exit (tp, silent);
351 /* Tag it as exited. */
352 tp->state = THREAD_EXITED;
353 clear_thread_inferior_resources (tp);
356 tpprev->next = tp->next;
358 thread_list = tp->next;
363 /* Delete thread PTID and notify of thread exit. If this is
364 inferior_ptid, don't actually delete it, but tag it as exited and
365 do the notification. If PTID is the user selected thread, clear
368 delete_thread (ptid_t ptid)
370 delete_thread_1 (ptid, 0 /* not silent */);
374 delete_thread_silent (ptid_t ptid)
376 delete_thread_1 (ptid, 1 /* silent */);
380 find_thread_id (int num)
382 struct thread_info *tp;
384 for (tp = thread_list; tp; tp = tp->next)
391 /* Find a thread_info by matching PTID. */
393 find_thread_ptid (ptid_t ptid)
395 struct thread_info *tp;
397 for (tp = thread_list; tp; tp = tp->next)
398 if (ptid_equal (tp->ptid, ptid))
405 * Thread iterator function.
407 * Calls a callback function once for each thread, so long as
408 * the callback function returns false. If the callback function
409 * returns true, the iteration will end and the current thread
410 * will be returned. This can be useful for implementing a
411 * search for a thread with arbitrary attributes, or for applying
412 * some operation to every thread.
414 * FIXME: some of the existing functionality, such as
415 * "Thread apply all", might be rewritten using this functionality.
419 iterate_over_threads (int (*callback) (struct thread_info *, void *),
422 struct thread_info *tp, *next;
424 for (tp = thread_list; tp; tp = next)
427 if ((*callback) (tp, data))
438 struct thread_info *tp;
440 for (tp = thread_list; tp; tp = tp->next)
447 valid_thread_id (int num)
449 struct thread_info *tp;
451 for (tp = thread_list; tp; tp = tp->next)
459 pid_to_thread_id (ptid_t ptid)
461 struct thread_info *tp;
463 for (tp = thread_list; tp; tp = tp->next)
464 if (ptid_equal (tp->ptid, ptid))
471 thread_id_to_pid (int num)
473 struct thread_info *thread = find_thread_id (num);
478 return pid_to_ptid (-1);
482 in_thread_list (ptid_t ptid)
484 struct thread_info *tp;
486 for (tp = thread_list; tp; tp = tp->next)
487 if (ptid_equal (tp->ptid, ptid))
490 return 0; /* Never heard of 'im. */
493 /* Finds the first thread of the inferior given by PID. If PID is -1,
494 return the first thread in the list. */
497 first_thread_of_process (int pid)
499 struct thread_info *tp, *ret = NULL;
501 for (tp = thread_list; tp; tp = tp->next)
502 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
503 if (ret == NULL || tp->num < ret->num)
510 any_thread_of_process (int pid)
512 struct thread_info *tp;
514 gdb_assert (pid != 0);
516 /* Prefer the current thread. */
517 if (ptid_get_pid (inferior_ptid) == pid)
518 return inferior_thread ();
520 ALL_NON_EXITED_THREADS (tp)
521 if (ptid_get_pid (tp->ptid) == pid)
528 any_live_thread_of_process (int pid)
530 struct thread_info *curr_tp = NULL;
531 struct thread_info *tp;
532 struct thread_info *tp_executing = NULL;
534 gdb_assert (pid != 0);
536 /* Prefer the current thread if it's not executing. */
537 if (ptid_get_pid (inferior_ptid) == pid)
539 /* If the current thread is dead, forget it. If it's not
540 executing, use it. Otherwise, still choose it (below), but
541 only if no other non-executing thread is found. */
542 curr_tp = inferior_thread ();
543 if (curr_tp->state == THREAD_EXITED)
545 else if (!curr_tp->executing)
549 ALL_NON_EXITED_THREADS (tp)
550 if (ptid_get_pid (tp->ptid) == pid)
558 /* If both the current thread and all live threads are executing,
559 prefer the current thread. */
563 /* Otherwise, just return an executing thread, if any. */
567 /* Print a list of thread ids currently known, and the total number of
568 threads. To be used from within catch_errors. */
570 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
572 struct thread_info *tp;
574 struct cleanup *cleanup_chain;
575 int current_thread = -1;
577 update_thread_list ();
579 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
581 for (tp = thread_list; tp; tp = tp->next)
583 if (tp->state == THREAD_EXITED)
586 if (ptid_equal (tp->ptid, inferior_ptid))
587 current_thread = tp->num;
590 ui_out_field_int (uiout, "thread-id", tp->num);
593 do_cleanups (cleanup_chain);
595 if (current_thread != -1)
596 ui_out_field_int (uiout, "current-thread-id", current_thread);
597 ui_out_field_int (uiout, "number-of-threads", num);
601 /* Official gdblib interface function to get a list of thread ids and
604 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
606 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
607 error_message, RETURN_MASK_ALL) < 0)
612 /* Return true if TP is an active thread. */
614 thread_alive (struct thread_info *tp)
616 if (tp->state == THREAD_EXITED)
618 if (!target_thread_alive (tp->ptid))
623 /* See gdbthreads.h. */
628 struct thread_info *tp, *tmp;
630 ALL_THREADS_SAFE (tp, tmp)
632 if (!thread_alive (tp))
633 delete_thread (tp->ptid);
637 /* See gdbthreads.h. */
640 delete_exited_threads (void)
642 struct thread_info *tp, *tmp;
644 ALL_THREADS_SAFE (tp, tmp)
646 if (tp->state == THREAD_EXITED)
647 delete_thread (tp->ptid);
651 /* Disable storing stack temporaries for the thread whose id is
655 disable_thread_stack_temporaries (void *data)
658 struct thread_info *tp = find_thread_ptid (*pd);
662 tp->stack_temporaries_enabled = 0;
663 VEC_free (value_ptr, tp->stack_temporaries);
669 /* Enable storing stack temporaries for thread with id PTID and return a
670 cleanup which can disable and clear the stack temporaries. */
673 enable_thread_stack_temporaries (ptid_t ptid)
675 struct thread_info *tp = find_thread_ptid (ptid);
679 gdb_assert (tp != NULL);
681 tp->stack_temporaries_enabled = 1;
682 tp->stack_temporaries = NULL;
683 data = (ptid_t *) xmalloc (sizeof (ptid_t));
685 c = make_cleanup (disable_thread_stack_temporaries, data);
690 /* Return non-zero value if stack temporaies are enabled for the thread
694 thread_stack_temporaries_enabled_p (ptid_t ptid)
696 struct thread_info *tp = find_thread_ptid (ptid);
701 return tp->stack_temporaries_enabled;
704 /* Push V on to the stack temporaries of the thread with id PTID. */
707 push_thread_stack_temporary (ptid_t ptid, struct value *v)
709 struct thread_info *tp = find_thread_ptid (ptid);
711 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
712 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
715 /* Return 1 if VAL is among the stack temporaries of the thread
716 with id PTID. Return 0 otherwise. */
719 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
721 struct thread_info *tp = find_thread_ptid (ptid);
723 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
724 if (!VEC_empty (value_ptr, tp->stack_temporaries))
729 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
737 /* Return the last of the stack temporaries for thread with id PTID.
738 Return NULL if there are no stack temporaries for the thread. */
741 get_last_thread_stack_temporary (ptid_t ptid)
743 struct value *lastval = NULL;
744 struct thread_info *tp = find_thread_ptid (ptid);
746 gdb_assert (tp != NULL);
747 if (!VEC_empty (value_ptr, tp->stack_temporaries))
748 lastval = VEC_last (value_ptr, tp->stack_temporaries);
754 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
756 struct inferior *inf;
757 struct thread_info *tp;
759 /* It can happen that what we knew as the target inferior id
760 changes. E.g, target remote may only discover the remote process
761 pid after adding the inferior to GDB's list. */
762 inf = find_inferior_ptid (old_ptid);
763 inf->pid = ptid_get_pid (new_ptid);
765 tp = find_thread_ptid (old_ptid);
768 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
772 set_running (ptid_t ptid, int running)
774 struct thread_info *tp;
775 int all = ptid_equal (ptid, minus_one_ptid);
777 /* We try not to notify the observer if no thread has actually changed
778 the running state -- merely to reduce the number of messages to
779 frontend. Frontend is supposed to handle multiple *running just fine. */
780 if (all || ptid_is_pid (ptid))
784 for (tp = thread_list; tp; tp = tp->next)
785 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
787 if (tp->state == THREAD_EXITED)
789 if (running && tp->state == THREAD_STOPPED)
791 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
794 observer_notify_target_resumed (ptid);
800 tp = find_thread_ptid (ptid);
802 gdb_assert (tp->state != THREAD_EXITED);
803 if (running && tp->state == THREAD_STOPPED)
805 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
807 observer_notify_target_resumed (ptid);
812 is_thread_state (ptid_t ptid, enum thread_state state)
814 struct thread_info *tp;
816 tp = find_thread_ptid (ptid);
818 return tp->state == state;
822 is_stopped (ptid_t ptid)
824 return is_thread_state (ptid, THREAD_STOPPED);
828 is_exited (ptid_t ptid)
830 return is_thread_state (ptid, THREAD_EXITED);
834 is_running (ptid_t ptid)
836 return is_thread_state (ptid, THREAD_RUNNING);
840 is_executing (ptid_t ptid)
842 struct thread_info *tp;
844 tp = find_thread_ptid (ptid);
846 return tp->executing;
850 set_executing (ptid_t ptid, int executing)
852 struct thread_info *tp;
853 int all = ptid_equal (ptid, minus_one_ptid);
855 if (all || ptid_is_pid (ptid))
857 for (tp = thread_list; tp; tp = tp->next)
858 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
859 tp->executing = executing;
863 tp = find_thread_ptid (ptid);
865 tp->executing = executing;
868 /* It only takes one running thread to spawn more threads.*/
870 threads_executing = 1;
871 /* Only clear the flag if the caller is telling us everything is
873 else if (ptid_equal (minus_one_ptid, ptid))
874 threads_executing = 0;
877 /* See gdbthread.h. */
880 threads_are_executing (void)
882 return threads_executing;
886 set_stop_requested (ptid_t ptid, int stop)
888 struct thread_info *tp;
889 int all = ptid_equal (ptid, minus_one_ptid);
891 if (all || ptid_is_pid (ptid))
893 for (tp = thread_list; tp; tp = tp->next)
894 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
895 tp->stop_requested = stop;
899 tp = find_thread_ptid (ptid);
901 tp->stop_requested = stop;
904 /* Call the stop requested observer so other components of GDB can
905 react to this request. */
907 observer_notify_thread_stop_requested (ptid);
911 finish_thread_state (ptid_t ptid)
913 struct thread_info *tp;
917 all = ptid_equal (ptid, minus_one_ptid);
919 if (all || ptid_is_pid (ptid))
921 for (tp = thread_list; tp; tp = tp->next)
923 if (tp->state == THREAD_EXITED)
925 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
927 if (tp->executing && tp->state == THREAD_STOPPED)
929 tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
935 tp = find_thread_ptid (ptid);
937 if (tp->state != THREAD_EXITED)
939 if (tp->executing && tp->state == THREAD_STOPPED)
941 tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
946 observer_notify_target_resumed (ptid);
950 finish_thread_state_cleanup (void *arg)
952 ptid_t *ptid_p = arg;
956 finish_thread_state (*ptid_p);
960 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
962 return (pc >= thread->control.step_range_start
963 && pc < thread->control.step_range_end);
966 /* Prints the list of threads and their details on UIOUT.
967 This is a version of 'info_threads_command' suitable for
969 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
970 that should be printed. Otherwise, all threads are
972 If PID is not -1, only print threads from the process PID.
973 Otherwise, threads from all attached PIDs are printed.
974 If both REQUESTED_THREAD and PID are not -1, then the thread
975 is printed if it belongs to the specified process. Otherwise,
976 an error is raised. */
978 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
980 struct thread_info *tp;
982 struct cleanup *old_chain;
983 char *extra_info, *name, *target_id;
984 int current_thread = -1;
986 update_thread_list ();
987 current_ptid = inferior_ptid;
989 /* We'll be switching threads temporarily. */
990 old_chain = make_cleanup_restore_current_thread ();
992 /* For backward compatibility, we make a list for MI. A table is
993 preferable for the CLI, though, because it shows table
995 if (ui_out_is_mi_like_p (uiout))
996 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1001 for (tp = thread_list; tp; tp = tp->next)
1003 if (!number_is_in_list (requested_threads, tp->num))
1006 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1009 if (tp->state == THREAD_EXITED)
1017 if (requested_threads == NULL || *requested_threads == '\0')
1018 ui_out_message (uiout, 0, _("No threads.\n"));
1020 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1022 do_cleanups (old_chain);
1026 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1028 ui_out_table_header (uiout, 1, ui_left, "current", "");
1029 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1030 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1031 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1032 ui_out_table_body (uiout);
1035 for (tp = thread_list; tp; tp = tp->next)
1037 struct cleanup *chain2;
1040 if (!number_is_in_list (requested_threads, tp->num))
1043 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1045 if (requested_threads != NULL && *requested_threads != '\0')
1046 error (_("Requested thread not found in requested process"));
1050 if (ptid_equal (tp->ptid, current_ptid))
1051 current_thread = tp->num;
1053 if (tp->state == THREAD_EXITED)
1056 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1058 if (ui_out_is_mi_like_p (uiout))
1060 /* Compatibility. */
1061 if (ptid_equal (tp->ptid, current_ptid))
1062 ui_out_text (uiout, "* ");
1064 ui_out_text (uiout, " ");
1068 if (ptid_equal (tp->ptid, current_ptid))
1069 ui_out_field_string (uiout, "current", "*");
1071 ui_out_field_skip (uiout, "current");
1074 ui_out_field_int (uiout, "id", tp->num);
1076 /* For the CLI, we stuff everything into the target-id field.
1077 This is a gross hack to make the output come out looking
1078 correct. The underlying problem here is that ui-out has no
1079 way to specify that a field's space allocation should be
1080 shared by several fields. For MI, we do the right thing
1083 target_id = target_pid_to_str (tp->ptid);
1084 extra_info = target_extra_thread_info (tp);
1085 name = tp->name ? tp->name : target_thread_name (tp);
1087 if (ui_out_is_mi_like_p (uiout))
1089 ui_out_field_string (uiout, "target-id", target_id);
1091 ui_out_field_string (uiout, "details", extra_info);
1093 ui_out_field_string (uiout, "name", name);
1097 struct cleanup *str_cleanup;
1100 if (extra_info && name)
1101 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1103 else if (extra_info)
1104 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1106 contents = xstrprintf ("%s \"%s\"", target_id, name);
1108 contents = xstrdup (target_id);
1109 str_cleanup = make_cleanup (xfree, contents);
1111 ui_out_field_string (uiout, "target-id", contents);
1112 do_cleanups (str_cleanup);
1115 if (tp->state == THREAD_RUNNING)
1116 ui_out_text (uiout, "(running)\n");
1119 /* The switch below puts us at the top of the stack (leaf
1121 switch_to_thread (tp->ptid);
1122 print_stack_frame (get_selected_frame (NULL),
1123 /* For MI output, print frame level. */
1124 ui_out_is_mi_like_p (uiout),
1128 if (ui_out_is_mi_like_p (uiout))
1130 char *state = "stopped";
1132 if (tp->state == THREAD_RUNNING)
1134 ui_out_field_string (uiout, "state", state);
1137 core = target_core_of_thread (tp->ptid);
1138 if (ui_out_is_mi_like_p (uiout) && core != -1)
1139 ui_out_field_int (uiout, "core", core);
1141 do_cleanups (chain2);
1144 /* Restores the current thread and the frame selected before
1145 the "info threads" command. */
1146 do_cleanups (old_chain);
1148 if (pid == -1 && requested_threads == NULL)
1150 gdb_assert (current_thread != -1
1152 || ptid_equal (inferior_ptid, null_ptid));
1153 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1154 ui_out_field_int (uiout, "current-thread-id", current_thread);
1156 if (current_thread != -1 && is_exited (current_ptid))
1157 ui_out_message (uiout, 0, "\n\
1158 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1160 else if (thread_list
1161 && current_thread == -1
1162 && ptid_equal (current_ptid, null_ptid))
1163 ui_out_message (uiout, 0, "\n\
1164 No selected thread. See `help thread'.\n");
1168 /* Print information about currently known threads
1170 Optional ARG is a thread id, or list of thread ids.
1172 Note: this has the drawback that it _really_ switches
1173 threads, which frees the frame cache. A no-side
1174 effects info-threads command would be nicer. */
1177 info_threads_command (char *arg, int from_tty)
1179 print_thread_info (current_uiout, arg, -1);
1182 /* Switch from one thread to another. */
1185 switch_to_thread (ptid_t ptid)
1187 /* Switch the program space as well, if we can infer it from the now
1188 current thread. Otherwise, it's up to the caller to select the
1190 if (!ptid_equal (ptid, null_ptid))
1192 struct inferior *inf;
1194 inf = find_inferior_ptid (ptid);
1195 gdb_assert (inf != NULL);
1196 set_current_program_space (inf->pspace);
1197 set_current_inferior (inf);
1200 if (ptid_equal (ptid, inferior_ptid))
1203 inferior_ptid = ptid;
1204 reinit_frame_cache ();
1206 /* We don't check for is_stopped, because we're called at times
1207 while in the TARGET_RUNNING state, e.g., while handling an
1209 if (!ptid_equal (inferior_ptid, null_ptid)
1210 && !is_exited (ptid)
1211 && !is_executing (ptid))
1212 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1214 stop_pc = ~(CORE_ADDR) 0;
1218 restore_current_thread (ptid_t ptid)
1220 switch_to_thread (ptid);
1224 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1226 struct frame_info *frame = NULL;
1229 /* This means there was no selected frame. */
1230 if (frame_level == -1)
1232 select_frame (NULL);
1236 gdb_assert (frame_level >= 0);
1238 /* Restore by level first, check if the frame id is the same as
1239 expected. If that fails, try restoring by frame id. If that
1240 fails, nothing to do, just warn the user. */
1242 count = frame_level;
1243 frame = find_relative_frame (get_current_frame (), &count);
1246 /* The frame ids must match - either both valid or both outer_frame_id.
1247 The latter case is not failsafe, but since it's highly unlikely
1248 the search by level finds the wrong frame, it's 99.9(9)% of
1249 the time (for all practical purposes) safe. */
1250 && frame_id_eq (get_frame_id (frame), a_frame_id))
1252 /* Cool, all is fine. */
1253 select_frame (frame);
1257 frame = frame_find_by_id (a_frame_id);
1260 /* Cool, refound it. */
1261 select_frame (frame);
1265 /* Nothing else to do, the frame layout really changed. Select the
1266 innermost stack frame. */
1267 select_frame (get_current_frame ());
1269 /* Warn the user. */
1270 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1272 warning (_("Couldn't restore frame #%d in "
1273 "current thread. Bottom (innermost) frame selected:"),
1275 /* For MI, we should probably have a notification about
1276 current frame change. But this error is not very
1277 likely, so don't bother for now. */
1278 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1282 /* Data used by the cleanup installed by
1283 'make_cleanup_restore_current_thread'. */
1285 struct current_thread_cleanup
1287 /* Next in list of currently installed 'struct
1288 current_thread_cleanup' cleanups. See
1289 'current_thread_cleanup_chain' below. */
1290 struct current_thread_cleanup *next;
1292 ptid_t inferior_ptid;
1293 struct frame_id selected_frame_id;
1294 int selected_frame_level;
1300 /* A chain of currently installed 'struct current_thread_cleanup'
1301 cleanups. Restoring the previously selected thread looks up the
1302 old thread in the thread list by ptid. If the thread changes ptid,
1303 we need to update the cleanup's thread structure so the look up
1305 static struct current_thread_cleanup *current_thread_cleanup_chain;
1307 /* A thread_ptid_changed observer. Update all currently installed
1308 current_thread_cleanup cleanups that want to switch back to
1309 OLD_PTID to switch back to NEW_PTID instead. */
1312 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1314 struct current_thread_cleanup *it;
1316 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1318 if (ptid_equal (it->inferior_ptid, old_ptid))
1319 it->inferior_ptid = new_ptid;
1324 do_restore_current_thread_cleanup (void *arg)
1326 struct thread_info *tp;
1327 struct current_thread_cleanup *old = arg;
1329 tp = find_thread_ptid (old->inferior_ptid);
1331 /* If the previously selected thread belonged to a process that has
1332 in the mean time been deleted (due to normal exit, detach, etc.),
1333 then don't revert back to it, but instead simply drop back to no
1336 && find_inferior_ptid (tp->ptid) != NULL)
1337 restore_current_thread (old->inferior_ptid);
1340 restore_current_thread (null_ptid);
1341 set_current_inferior (find_inferior_id (old->inf_id));
1344 /* The running state of the originally selected thread may have
1345 changed, so we have to recheck it here. */
1346 if (!ptid_equal (inferior_ptid, null_ptid)
1348 && is_stopped (inferior_ptid)
1349 && target_has_registers
1351 && target_has_memory)
1352 restore_selected_frame (old->selected_frame_id,
1353 old->selected_frame_level);
1357 restore_current_thread_cleanup_dtor (void *arg)
1359 struct current_thread_cleanup *old = arg;
1360 struct thread_info *tp;
1361 struct inferior *inf;
1363 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1365 tp = find_thread_ptid (old->inferior_ptid);
1368 inf = find_inferior_id (old->inf_id);
1370 inf->removable = old->was_removable;
1374 /* Set the thread reference count. */
1377 set_thread_refcount (void *data)
1380 struct thread_array_cleanup *ta_cleanup = data;
1382 for (k = 0; k != ta_cleanup->count; k++)
1383 ta_cleanup->tp_array[k]->refcount--;
1387 make_cleanup_restore_current_thread (void)
1389 struct thread_info *tp;
1390 struct frame_info *frame;
1391 struct current_thread_cleanup *old;
1393 old = xmalloc (sizeof (struct current_thread_cleanup));
1394 old->inferior_ptid = inferior_ptid;
1395 old->inf_id = current_inferior ()->num;
1396 old->was_removable = current_inferior ()->removable;
1398 old->next = current_thread_cleanup_chain;
1399 current_thread_cleanup_chain = old;
1401 if (!ptid_equal (inferior_ptid, null_ptid))
1403 old->was_stopped = is_stopped (inferior_ptid);
1404 if (old->was_stopped
1405 && target_has_registers
1407 && target_has_memory)
1409 /* When processing internal events, there might not be a
1410 selected frame. If we naively call get_selected_frame
1411 here, then we can end up reading debuginfo for the
1412 current frame, but we don't generally need the debuginfo
1414 frame = get_selected_frame_if_set ();
1419 old->selected_frame_id = get_frame_id (frame);
1420 old->selected_frame_level = frame_relative_level (frame);
1422 tp = find_thread_ptid (inferior_ptid);
1427 current_inferior ()->removable = 0;
1429 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1430 restore_current_thread_cleanup_dtor);
1433 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1434 descending order. */
1436 static int tp_array_compar_ascending;
1438 /* Sort an array for struct thread_info pointers by their NUM, order is
1439 determined by TP_ARRAY_COMPAR_ASCENDING. */
1442 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1444 const struct thread_info *const *ap = ap_voidp;
1445 const struct thread_info *const *bp = bp_voidp;
1447 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1448 * (tp_array_compar_ascending ? +1 : -1));
1451 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1452 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1453 of two numbers seperated by a hyphen. Examples:
1455 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1456 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1457 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1460 thread_apply_all_command (char *cmd, int from_tty)
1462 struct cleanup *old_chain;
1465 struct thread_array_cleanup ta_cleanup;
1467 tp_array_compar_ascending = 0;
1469 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1471 cmd = skip_spaces (cmd);
1472 tp_array_compar_ascending = 1;
1475 if (cmd == NULL || *cmd == '\000')
1476 error (_("Please specify a command following the thread ID list"));
1478 update_thread_list ();
1480 old_chain = make_cleanup_restore_current_thread ();
1482 /* Save a copy of the command in case it is clobbered by
1484 saved_cmd = xstrdup (cmd);
1485 make_cleanup (xfree, saved_cmd);
1487 /* Note this includes exited threads. */
1488 tc = thread_count ();
1491 struct thread_info **tp_array;
1492 struct thread_info *tp;
1495 /* Save a copy of the thread_list in case we execute detach
1497 tp_array = xmalloc (sizeof (struct thread_info *) * tc);
1498 make_cleanup (xfree, tp_array);
1500 ALL_NON_EXITED_THREADS (tp)
1506 /* Because we skipped exited threads, we may end up with fewer
1507 threads in the array than the total count of threads. */
1508 gdb_assert (i <= tc);
1511 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1513 ta_cleanup.tp_array = tp_array;
1514 ta_cleanup.count = i;
1515 make_cleanup (set_thread_refcount, &ta_cleanup);
1517 for (k = 0; k != i; k++)
1518 if (thread_alive (tp_array[k]))
1520 switch_to_thread (tp_array[k]->ptid);
1521 printf_filtered (_("\nThread %d (%s):\n"),
1523 target_pid_to_str (inferior_ptid));
1524 execute_command (cmd, from_tty);
1526 /* Restore exact command used previously. */
1527 strcpy (cmd, saved_cmd);
1531 do_cleanups (old_chain);
1535 thread_apply_command (char *tidlist, int from_tty)
1538 struct cleanup *old_chain;
1540 struct get_number_or_range_state state;
1542 if (tidlist == NULL || *tidlist == '\000')
1543 error (_("Please specify a thread ID list"));
1545 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1548 error (_("Please specify a command following the thread ID list"));
1550 /* Save a copy of the command in case it is clobbered by
1552 saved_cmd = xstrdup (cmd);
1553 old_chain = make_cleanup (xfree, saved_cmd);
1555 init_number_or_range (&state, tidlist);
1556 while (!state.finished && state.string < cmd)
1558 struct thread_info *tp;
1561 start = get_number_or_range (&state);
1563 make_cleanup_restore_current_thread ();
1565 tp = find_thread_id (start);
1568 warning (_("Unknown thread %d."), start);
1569 else if (!thread_alive (tp))
1570 warning (_("Thread %d has terminated."), start);
1573 switch_to_thread (tp->ptid);
1575 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1576 target_pid_to_str (inferior_ptid));
1577 execute_command (cmd, from_tty);
1579 /* Restore exact command used previously. */
1580 strcpy (cmd, saved_cmd);
1584 do_cleanups (old_chain);
1587 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1588 if prefix of arg is `apply'. */
1591 thread_command (char *tidstr, int from_tty)
1595 if (ptid_equal (inferior_ptid, null_ptid))
1596 error (_("No thread selected"));
1598 if (target_has_stack)
1600 if (is_exited (inferior_ptid))
1601 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1602 pid_to_thread_id (inferior_ptid),
1603 target_pid_to_str (inferior_ptid));
1605 printf_filtered (_("[Current thread is %d (%s)]\n"),
1606 pid_to_thread_id (inferior_ptid),
1607 target_pid_to_str (inferior_ptid));
1610 error (_("No stack."));
1614 gdb_thread_select (current_uiout, tidstr, NULL);
1617 /* Implementation of `thread name'. */
1620 thread_name_command (char *arg, int from_tty)
1622 struct thread_info *info;
1624 if (ptid_equal (inferior_ptid, null_ptid))
1625 error (_("No thread selected"));
1627 arg = skip_spaces (arg);
1629 info = inferior_thread ();
1631 info->name = arg ? xstrdup (arg) : NULL;
1634 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1637 thread_find_command (char *arg, int from_tty)
1639 struct thread_info *tp;
1641 unsigned long match = 0;
1643 if (arg == NULL || *arg == '\0')
1644 error (_("Command requires an argument."));
1646 tmp = re_comp (arg);
1648 error (_("Invalid regexp (%s): %s"), tmp, arg);
1650 update_thread_list ();
1651 for (tp = thread_list; tp; tp = tp->next)
1653 if (tp->name != NULL && re_exec (tp->name))
1655 printf_filtered (_("Thread %d has name '%s'\n"),
1660 tmp = target_thread_name (tp);
1661 if (tmp != NULL && re_exec (tmp))
1663 printf_filtered (_("Thread %d has target name '%s'\n"),
1668 tmp = target_pid_to_str (tp->ptid);
1669 if (tmp != NULL && re_exec (tmp))
1671 printf_filtered (_("Thread %d has target id '%s'\n"),
1676 tmp = target_extra_thread_info (tp);
1677 if (tmp != NULL && re_exec (tmp))
1679 printf_filtered (_("Thread %d has extra info '%s'\n"),
1685 printf_filtered (_("No threads match '%s'\n"), arg);
1688 /* Print notices when new threads are attached and detached. */
1689 int print_thread_events = 1;
1691 show_print_thread_events (struct ui_file *file, int from_tty,
1692 struct cmd_list_element *c, const char *value)
1694 fprintf_filtered (file,
1695 _("Printing of thread events is %s.\n"),
1700 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1703 struct thread_info *tp;
1705 num = value_as_long (parse_and_eval (tidstr));
1707 tp = find_thread_id (num);
1710 error (_("Thread ID %d not known."), num);
1712 if (!thread_alive (tp))
1713 error (_("Thread ID %d has terminated."), num);
1715 switch_to_thread (tp->ptid);
1717 annotate_thread_changed ();
1719 ui_out_text (uiout, "[Switching to thread ");
1720 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1721 ui_out_text (uiout, " (");
1722 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1723 ui_out_text (uiout, ")]");
1725 /* Note that we can't reach this with an exited thread, due to the
1726 thread_alive check above. */
1727 if (tp->state == THREAD_RUNNING)
1728 ui_out_text (uiout, "(running)\n");
1731 ui_out_text (uiout, "\n");
1732 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1735 /* Since the current thread may have changed, see if there is any
1736 exited thread we can now delete. */
1743 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1745 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1746 error_message, RETURN_MASK_ALL) < 0)
1751 /* Update the 'threads_executing' global based on the threads we know
1755 update_threads_executing (void)
1757 struct thread_info *tp;
1759 threads_executing = 0;
1760 ALL_NON_EXITED_THREADS (tp)
1764 threads_executing = 1;
1771 update_thread_list (void)
1773 target_update_thread_list ();
1774 update_threads_executing ();
1777 /* Return a new value for the selected thread's id. Return a value of 0 if
1778 no thread is selected, or no threads exist. */
1780 static struct value *
1781 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1784 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1786 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1787 (tp ? tp->num : 0));
1790 /* Commands with a prefix of `thread'. */
1791 struct cmd_list_element *thread_cmd_list = NULL;
1793 /* Implementation of `thread' variable. */
1795 static const struct internalvar_funcs thread_funcs =
1797 thread_id_make_value,
1803 _initialize_thread (void)
1805 static struct cmd_list_element *thread_apply_list = NULL;
1807 add_info ("threads", info_threads_command,
1808 _("Display currently known threads.\n\
1809 Usage: info threads [ID]...\n\
1810 Optional arguments are thread IDs with spaces between.\n\
1811 If no arguments, all threads are displayed."));
1813 add_prefix_cmd ("thread", class_run, thread_command, _("\
1814 Use this command to switch between threads.\n\
1815 The new thread ID must be currently known."),
1816 &thread_cmd_list, "thread ", 1, &cmdlist);
1818 add_prefix_cmd ("apply", class_run, thread_apply_command,
1819 _("Apply a command to a list of threads."),
1820 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1822 add_cmd ("all", class_run, thread_apply_all_command,
1824 Apply a command to all threads.\n\
1826 Usage: thread apply all [-ascending] <command>\n\
1827 -ascending: Call <command> for all threads in ascending order.\n\
1828 The default is descending order.\
1830 &thread_apply_list);
1832 add_cmd ("name", class_run, thread_name_command,
1833 _("Set the current thread's name.\n\
1834 Usage: thread name [NAME]\n\
1835 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1837 add_cmd ("find", class_run, thread_find_command, _("\
1838 Find threads that match a regular expression.\n\
1839 Usage: thread find REGEXP\n\
1840 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1843 add_com_alias ("t", "thread", class_run, 1);
1845 add_setshow_boolean_cmd ("thread-events", no_class,
1846 &print_thread_events, _("\
1847 Set printing of thread events (such as thread start and exit)."), _("\
1848 Show printing of thread events (such as thread start and exit)."), NULL,
1850 show_print_thread_events,
1851 &setprintlist, &showprintlist);
1853 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1855 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);