1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "gdbthread.h"
32 #include "exceptions.h"
37 #include "gdb_string.h"
40 #include <sys/types.h>
45 #include "cli/cli-decode.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 static struct thread_info *thread_list = NULL;
56 static int highest_thread_num;
58 static void thread_command (char *tidstr, int from_tty);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info *);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t);
64 static void prune_threads (void);
66 /* Frontend view of the thread state. Possible extensions: stepping,
67 finishing, until(ling),... */
76 inferior_thread (void)
78 struct thread_info *tp = find_thread_ptid (inferior_ptid);
84 delete_step_resume_breakpoint (struct thread_info *tp)
86 if (tp && tp->control.step_resume_breakpoint)
88 delete_breakpoint (tp->control.step_resume_breakpoint);
89 tp->control.step_resume_breakpoint = NULL;
94 delete_exception_resume_breakpoint (struct thread_info *tp)
96 if (tp && tp->control.exception_resume_breakpoint)
98 delete_breakpoint (tp->control.exception_resume_breakpoint);
99 tp->control.exception_resume_breakpoint = NULL;
104 clear_thread_inferior_resources (struct thread_info *tp)
106 /* NOTE: this will take care of any left-over step_resume breakpoints,
107 but not any user-specified thread-specific breakpoints. We can not
108 delete the breakpoint straight-off, because the inferior might not
109 be stopped at the moment. */
110 if (tp->control.step_resume_breakpoint)
112 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
113 tp->control.step_resume_breakpoint = NULL;
116 if (tp->control.exception_resume_breakpoint)
118 tp->control.exception_resume_breakpoint->disposition
119 = disp_del_at_next_stop;
120 tp->control.exception_resume_breakpoint = NULL;
123 bpstat_clear (&tp->control.stop_bpstat);
125 discard_all_intermediate_continuations_thread (tp);
126 discard_all_continuations_thread (tp);
128 delete_longjmp_breakpoint (tp->num);
132 free_thread (struct thread_info *tp)
134 clear_thread_inferior_resources (tp);
138 if (tp->private_dtor)
139 tp->private_dtor (tp->private);
148 init_thread_list (void)
150 struct thread_info *tp, *tpnext;
152 highest_thread_num = 0;
157 for (tp = thread_list; tp; tp = tpnext)
166 /* Allocate a new thread with target id PTID and add it to the thread
169 static struct thread_info *
170 new_thread (ptid_t ptid)
172 struct thread_info *tp;
174 tp = xcalloc (1, sizeof (*tp));
177 tp->num = ++highest_thread_num;
178 tp->next = thread_list;
181 /* Nothing to follow yet. */
182 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
183 tp->state_ = THREAD_STOPPED;
189 add_thread_silent (ptid_t ptid)
191 struct thread_info *tp;
193 tp = find_thread_ptid (ptid);
195 /* Found an old thread with the same id. It has to be dead,
196 otherwise we wouldn't be adding a new thread with the same id.
197 The OS is reusing this id --- delete it, and recreate a new
200 /* In addition to deleting the thread, if this is the current
201 thread, then we need to take care that delete_thread doesn't
202 really delete the thread if it is inferior_ptid. Create a
203 new template thread in the list with an invalid ptid, switch
204 to it, delete the original thread, reset the new thread's
205 ptid, and switch to it. */
207 if (ptid_equal (inferior_ptid, ptid))
209 tp = new_thread (null_ptid);
211 /* Make switch_to_thread not read from the thread. */
212 tp->state_ = THREAD_EXITED;
213 switch_to_thread (null_ptid);
215 /* Now we can delete it. */
216 delete_thread (ptid);
218 /* Now reset its ptid, and reswitch inferior_ptid to it. */
220 tp->state_ = THREAD_STOPPED;
221 switch_to_thread (ptid);
223 observer_notify_new_thread (tp);
229 /* Just go ahead and delete it. */
230 delete_thread (ptid);
233 tp = new_thread (ptid);
234 observer_notify_new_thread (tp);
240 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
242 struct thread_info *result = add_thread_silent (ptid);
244 result->private = private;
246 if (print_thread_events)
247 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
249 annotate_new_thread ();
254 add_thread (ptid_t ptid)
256 return add_thread_with_info (ptid, NULL);
259 /* Delete thread PTID. If SILENT, don't notify the observer of this
262 delete_thread_1 (ptid_t ptid, int silent)
264 struct thread_info *tp, *tpprev;
268 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
269 if (ptid_equal (tp->ptid, ptid))
275 /* If this is the current thread, or there's code out there that
276 relies on it existing (refcount > 0) we can't delete yet. Mark
277 it as exited, and notify it. */
279 || ptid_equal (tp->ptid, inferior_ptid))
281 if (tp->state_ != THREAD_EXITED)
283 observer_notify_thread_exit (tp, silent);
285 /* Tag it as exited. */
286 tp->state_ = THREAD_EXITED;
288 /* Clear breakpoints, etc. associated with this thread. */
289 clear_thread_inferior_resources (tp);
292 /* Will be really deleted some other time. */
297 tpprev->next = tp->next;
299 thread_list = tp->next;
301 /* Notify thread exit, but only if we haven't already. */
302 if (tp->state_ != THREAD_EXITED)
303 observer_notify_thread_exit (tp, silent);
308 /* Delete thread PTID and notify of thread exit. If this is
309 inferior_ptid, don't actually delete it, but tag it as exited and
310 do the notification. If PTID is the user selected thread, clear
313 delete_thread (ptid_t ptid)
315 delete_thread_1 (ptid, 0 /* not silent */);
319 delete_thread_silent (ptid_t ptid)
321 delete_thread_1 (ptid, 1 /* silent */);
325 find_thread_id (int num)
327 struct thread_info *tp;
329 for (tp = thread_list; tp; tp = tp->next)
336 /* Find a thread_info by matching PTID. */
338 find_thread_ptid (ptid_t ptid)
340 struct thread_info *tp;
342 for (tp = thread_list; tp; tp = tp->next)
343 if (ptid_equal (tp->ptid, ptid))
350 * Thread iterator function.
352 * Calls a callback function once for each thread, so long as
353 * the callback function returns false. If the callback function
354 * returns true, the iteration will end and the current thread
355 * will be returned. This can be useful for implementing a
356 * search for a thread with arbitrary attributes, or for applying
357 * some operation to every thread.
359 * FIXME: some of the existing functionality, such as
360 * "Thread apply all", might be rewritten using this functionality.
364 iterate_over_threads (int (*callback) (struct thread_info *, void *),
367 struct thread_info *tp, *next;
369 for (tp = thread_list; tp; tp = next)
372 if ((*callback) (tp, data))
383 struct thread_info *tp;
385 for (tp = thread_list; tp; tp = tp->next)
392 valid_thread_id (int num)
394 struct thread_info *tp;
396 for (tp = thread_list; tp; tp = tp->next)
404 pid_to_thread_id (ptid_t ptid)
406 struct thread_info *tp;
408 for (tp = thread_list; tp; tp = tp->next)
409 if (ptid_equal (tp->ptid, ptid))
416 thread_id_to_pid (int num)
418 struct thread_info *thread = find_thread_id (num);
423 return pid_to_ptid (-1);
427 in_thread_list (ptid_t ptid)
429 struct thread_info *tp;
431 for (tp = thread_list; tp; tp = tp->next)
432 if (ptid_equal (tp->ptid, ptid))
435 return 0; /* Never heard of 'im. */
438 /* Finds the first thread of the inferior given by PID. If PID is -1,
439 return the first thread in the list. */
442 first_thread_of_process (int pid)
444 struct thread_info *tp, *ret = NULL;
446 for (tp = thread_list; tp; tp = tp->next)
447 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
448 if (ret == NULL || tp->num < ret->num)
455 any_thread_of_process (int pid)
457 struct thread_info *tp;
459 for (tp = thread_list; tp; tp = tp->next)
460 if (ptid_get_pid (tp->ptid) == pid)
467 any_live_thread_of_process (int pid)
469 struct thread_info *tp;
470 struct thread_info *tp_running = NULL;
472 for (tp = thread_list; tp; tp = tp->next)
473 if (ptid_get_pid (tp->ptid) == pid)
475 if (tp->state_ == THREAD_STOPPED)
477 else if (tp->state_ == THREAD_RUNNING)
484 /* Print a list of thread ids currently known, and the total number of
485 threads. To be used from within catch_errors. */
487 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
489 struct thread_info *tp;
491 struct cleanup *cleanup_chain;
492 int current_thread = -1;
494 update_thread_list ();
496 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
498 for (tp = thread_list; tp; tp = tp->next)
500 if (tp->state_ == THREAD_EXITED)
503 if (ptid_equal (tp->ptid, inferior_ptid))
504 current_thread = tp->num;
507 ui_out_field_int (uiout, "thread-id", tp->num);
510 do_cleanups (cleanup_chain);
512 if (current_thread != -1)
513 ui_out_field_int (uiout, "current-thread-id", current_thread);
514 ui_out_field_int (uiout, "number-of-threads", num);
518 /* Official gdblib interface function to get a list of thread ids and
521 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
523 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
524 error_message, RETURN_MASK_ALL) < 0)
529 /* Return true if TP is an active thread. */
531 thread_alive (struct thread_info *tp)
533 if (tp->state_ == THREAD_EXITED)
535 if (!target_thread_alive (tp->ptid))
543 struct thread_info *tp, *next;
545 for (tp = thread_list; tp; tp = next)
548 if (!thread_alive (tp))
549 delete_thread (tp->ptid);
554 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
556 struct inferior *inf;
557 struct thread_info *tp;
559 /* It can happen that what we knew as the target inferior id
560 changes. E.g, target remote may only discover the remote process
561 pid after adding the inferior to GDB's list. */
562 inf = find_inferior_pid (ptid_get_pid (old_ptid));
563 inf->pid = ptid_get_pid (new_ptid);
565 tp = find_thread_ptid (old_ptid);
568 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
572 set_running (ptid_t ptid, int running)
574 struct thread_info *tp;
575 int all = ptid_equal (ptid, minus_one_ptid);
577 /* We try not to notify the observer if no thread has actually changed
578 the running state -- merely to reduce the number of messages to
579 frontend. Frontend is supposed to handle multiple *running just fine. */
580 if (all || ptid_is_pid (ptid))
584 for (tp = thread_list; tp; tp = tp->next)
585 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
587 if (tp->state_ == THREAD_EXITED)
589 if (running && tp->state_ == THREAD_STOPPED)
591 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
594 observer_notify_target_resumed (ptid);
600 tp = find_thread_ptid (ptid);
602 gdb_assert (tp->state_ != THREAD_EXITED);
603 if (running && tp->state_ == THREAD_STOPPED)
605 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
607 observer_notify_target_resumed (ptid);
612 is_thread_state (ptid_t ptid, enum thread_state state)
614 struct thread_info *tp;
616 tp = find_thread_ptid (ptid);
618 return tp->state_ == state;
622 is_stopped (ptid_t ptid)
624 return is_thread_state (ptid, THREAD_STOPPED);
628 is_exited (ptid_t ptid)
630 return is_thread_state (ptid, THREAD_EXITED);
634 is_running (ptid_t ptid)
636 return is_thread_state (ptid, THREAD_RUNNING);
642 struct thread_info *tp;
644 for (tp = thread_list; tp; tp = tp->next)
645 if (tp->state_ == THREAD_RUNNING)
652 is_executing (ptid_t ptid)
654 struct thread_info *tp;
656 tp = find_thread_ptid (ptid);
658 return tp->executing_;
662 set_executing (ptid_t ptid, int executing)
664 struct thread_info *tp;
665 int all = ptid_equal (ptid, minus_one_ptid);
667 if (all || ptid_is_pid (ptid))
669 for (tp = thread_list; tp; tp = tp->next)
670 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
671 tp->executing_ = executing;
675 tp = find_thread_ptid (ptid);
677 tp->executing_ = executing;
682 set_stop_requested (ptid_t ptid, int stop)
684 struct thread_info *tp;
685 int all = ptid_equal (ptid, minus_one_ptid);
687 if (all || ptid_is_pid (ptid))
689 for (tp = thread_list; tp; tp = tp->next)
690 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
691 tp->stop_requested = stop;
695 tp = find_thread_ptid (ptid);
697 tp->stop_requested = stop;
700 /* Call the stop requested observer so other components of GDB can
701 react to this request. */
703 observer_notify_thread_stop_requested (ptid);
707 finish_thread_state (ptid_t ptid)
709 struct thread_info *tp;
713 all = ptid_equal (ptid, minus_one_ptid);
715 if (all || ptid_is_pid (ptid))
717 for (tp = thread_list; tp; tp = tp->next)
719 if (tp->state_ == THREAD_EXITED)
721 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
723 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
725 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
731 tp = find_thread_ptid (ptid);
733 if (tp->state_ != THREAD_EXITED)
735 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
737 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
742 observer_notify_target_resumed (ptid);
746 finish_thread_state_cleanup (void *arg)
748 ptid_t *ptid_p = arg;
752 finish_thread_state (*ptid_p);
755 /* Prints the list of threads and their details on UIOUT.
756 This is a version of 'info_thread_command' suitable for
758 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
759 that should be printed. Otherwise, all threads are
761 If PID is not -1, only print threads from the process PID.
762 Otherwise, threads from all attached PIDs are printed.
763 If both REQUESTED_THREAD and PID are not -1, then the thread
764 is printed if it belongs to the specified process. Otherwise,
765 an error is raised. */
767 print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
769 struct thread_info *tp;
771 struct cleanup *old_chain;
773 int current_thread = -1;
775 update_thread_list ();
776 current_ptid = inferior_ptid;
778 /* We'll be switching threads temporarily. */
779 old_chain = make_cleanup_restore_current_thread ();
781 /* For backward compatibility, we make a list for MI. A table is
782 preferable for the CLI, though, because it shows table
784 if (ui_out_is_mi_like_p (uiout))
785 make_cleanup_ui_out_list_begin_end (uiout, "threads");
790 for (tp = thread_list; tp; tp = tp->next)
792 if (requested_thread != -1 && tp->num != requested_thread)
795 if (pid != -1 && PIDGET (tp->ptid) != pid)
798 if (tp->state_ == THREAD_EXITED)
806 if (requested_thread == -1)
807 ui_out_message (uiout, 0, _("No threads.\n"));
809 ui_out_message (uiout, 0, _("No thread %d.\n"), requested_thread);
810 do_cleanups (old_chain);
814 make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads");
816 ui_out_table_header (uiout, 1, ui_left, "current", "");
817 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
818 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
819 ui_out_table_header (uiout, 1, ui_noalign, "details", "");
820 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
821 ui_out_table_body (uiout);
824 for (tp = thread_list; tp; tp = tp->next)
826 struct cleanup *chain2;
829 if (requested_thread != -1 && tp->num != requested_thread)
832 if (pid != -1 && PIDGET (tp->ptid) != pid)
834 if (requested_thread != -1)
835 error (_("Requested thread not found in requested process"));
839 if (ptid_equal (tp->ptid, current_ptid))
840 current_thread = tp->num;
842 if (tp->state_ == THREAD_EXITED)
845 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
847 if (ui_out_is_mi_like_p (uiout))
850 if (ptid_equal (tp->ptid, current_ptid))
851 ui_out_text (uiout, "* ");
853 ui_out_text (uiout, " ");
857 if (ptid_equal (tp->ptid, current_ptid))
858 ui_out_field_string (uiout, "current", "*");
860 ui_out_field_skip (uiout, "current");
863 ui_out_field_int (uiout, "id", tp->num);
864 ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
866 extra_info = target_extra_thread_info (tp);
869 ui_out_text (uiout, " (");
870 ui_out_field_string (uiout, "details", extra_info);
871 ui_out_text (uiout, ")");
873 else if (! ui_out_is_mi_like_p (uiout))
874 ui_out_field_skip (uiout, "details");
876 if (tp->state_ == THREAD_RUNNING)
877 ui_out_text (uiout, "(running)\n");
880 /* The switch below puts us at the top of the stack (leaf
882 switch_to_thread (tp->ptid);
883 print_stack_frame (get_selected_frame (NULL),
884 /* For MI output, print frame level. */
885 ui_out_is_mi_like_p (uiout),
889 if (ui_out_is_mi_like_p (uiout))
891 char *state = "stopped";
893 if (tp->state_ == THREAD_RUNNING)
895 ui_out_field_string (uiout, "state", state);
898 core = target_core_of_thread (tp->ptid);
899 if (ui_out_is_mi_like_p (uiout) && core != -1)
900 ui_out_field_int (uiout, "core", core);
902 do_cleanups (chain2);
905 /* Restores the current thread and the frame selected before
906 the "info threads" command. */
907 do_cleanups (old_chain);
909 if (pid == -1 && requested_thread == -1)
911 gdb_assert (current_thread != -1
913 || ptid_equal (inferior_ptid, null_ptid));
914 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
915 ui_out_field_int (uiout, "current-thread-id", current_thread);
917 if (current_thread != -1 && is_exited (current_ptid))
918 ui_out_message (uiout, 0, "\n\
919 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
922 && current_thread == -1
923 && ptid_equal (current_ptid, null_ptid))
924 ui_out_message (uiout, 0, "\n\
925 No selected thread. See `help thread'.\n");
930 /* Print information about currently known threads
932 * Note: this has the drawback that it _really_ switches
933 * threads, which frees the frame cache. A no-side
934 * effects info-threads command would be nicer.
938 info_threads_command (char *arg, int from_tty)
940 print_thread_info (uiout, -1, -1);
943 /* Switch from one thread to another. */
946 switch_to_thread (ptid_t ptid)
948 /* Switch the program space as well, if we can infer it from the now
949 current thread. Otherwise, it's up to the caller to select the
951 if (!ptid_equal (ptid, null_ptid))
953 struct inferior *inf;
955 inf = find_inferior_pid (ptid_get_pid (ptid));
956 gdb_assert (inf != NULL);
957 set_current_program_space (inf->pspace);
958 set_current_inferior (inf);
961 if (ptid_equal (ptid, inferior_ptid))
964 inferior_ptid = ptid;
965 reinit_frame_cache ();
966 registers_changed ();
968 /* We don't check for is_stopped, because we're called at times
969 while in the TARGET_RUNNING state, e.g., while handling an
971 if (!ptid_equal (inferior_ptid, null_ptid)
973 && !is_executing (ptid))
974 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
976 stop_pc = ~(CORE_ADDR) 0;
980 restore_current_thread (ptid_t ptid)
982 switch_to_thread (ptid);
986 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
988 struct frame_info *frame = NULL;
991 gdb_assert (frame_level >= 0);
993 /* Restore by level first, check if the frame id is the same as
994 expected. If that fails, try restoring by frame id. If that
995 fails, nothing to do, just warn the user. */
998 frame = find_relative_frame (get_current_frame (), &count);
1001 /* The frame ids must match - either both valid or both outer_frame_id.
1002 The latter case is not failsafe, but since it's highly unlikely
1003 the search by level finds the wrong frame, it's 99.9(9)% of
1004 the time (for all practical purposes) safe. */
1005 && frame_id_eq (get_frame_id (frame), a_frame_id))
1007 /* Cool, all is fine. */
1008 select_frame (frame);
1012 frame = frame_find_by_id (a_frame_id);
1015 /* Cool, refound it. */
1016 select_frame (frame);
1020 /* Nothing else to do, the frame layout really changed. Select the
1021 innermost stack frame. */
1022 select_frame (get_current_frame ());
1024 /* Warn the user. */
1025 if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
1027 warning (_("Couldn't restore frame #%d in "
1028 "current thread, at reparsed frame #0\n"),
1030 /* For MI, we should probably have a notification about
1031 current frame change. But this error is not very
1032 likely, so don't bother for now. */
1033 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
1037 struct current_thread_cleanup
1039 ptid_t inferior_ptid;
1040 struct frame_id selected_frame_id;
1041 int selected_frame_level;
1047 do_restore_current_thread_cleanup (void *arg)
1049 struct thread_info *tp;
1050 struct current_thread_cleanup *old = arg;
1052 tp = find_thread_ptid (old->inferior_ptid);
1054 /* If the previously selected thread belonged to a process that has
1055 in the mean time been deleted (due to normal exit, detach, etc.),
1056 then don't revert back to it, but instead simply drop back to no
1059 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
1060 restore_current_thread (old->inferior_ptid);
1063 restore_current_thread (null_ptid);
1064 set_current_inferior (find_inferior_id (old->inf_id));
1067 /* The running state of the originally selected thread may have
1068 changed, so we have to recheck it here. */
1069 if (!ptid_equal (inferior_ptid, null_ptid)
1071 && is_stopped (inferior_ptid)
1072 && target_has_registers
1074 && target_has_memory)
1075 restore_selected_frame (old->selected_frame_id,
1076 old->selected_frame_level);
1080 restore_current_thread_cleanup_dtor (void *arg)
1082 struct current_thread_cleanup *old = arg;
1083 struct thread_info *tp;
1085 tp = find_thread_ptid (old->inferior_ptid);
1092 make_cleanup_restore_current_thread (void)
1094 struct thread_info *tp;
1095 struct frame_info *frame;
1096 struct current_thread_cleanup *old;
1098 old = xmalloc (sizeof (struct current_thread_cleanup));
1099 old->inferior_ptid = inferior_ptid;
1100 old->inf_id = current_inferior ()->num;
1102 if (!ptid_equal (inferior_ptid, null_ptid))
1104 old->was_stopped = is_stopped (inferior_ptid);
1105 if (old->was_stopped
1106 && target_has_registers
1108 && target_has_memory)
1109 frame = get_selected_frame (NULL);
1113 old->selected_frame_id = get_frame_id (frame);
1114 old->selected_frame_level = frame_relative_level (frame);
1116 tp = find_thread_ptid (inferior_ptid);
1121 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1122 restore_current_thread_cleanup_dtor);
1125 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1126 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1127 of two numbers seperated by a hyphen. Examples:
1129 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1130 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1131 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1134 thread_apply_all_command (char *cmd, int from_tty)
1136 struct thread_info *tp;
1137 struct cleanup *old_chain;
1140 if (cmd == NULL || *cmd == '\000')
1141 error (_("Please specify a command following the thread ID list"));
1143 update_thread_list ();
1145 old_chain = make_cleanup_restore_current_thread ();
1147 /* Save a copy of the command in case it is clobbered by
1149 saved_cmd = xstrdup (cmd);
1150 make_cleanup (xfree, saved_cmd);
1151 for (tp = thread_list; tp; tp = tp->next)
1152 if (thread_alive (tp))
1154 switch_to_thread (tp->ptid);
1156 printf_filtered (_("\nThread %d (%s):\n"),
1157 tp->num, target_pid_to_str (inferior_ptid));
1158 execute_command (cmd, from_tty);
1159 strcpy (cmd, saved_cmd); /* Restore exact command used
1163 do_cleanups (old_chain);
1167 thread_apply_command (char *tidlist, int from_tty)
1171 struct cleanup *old_chain;
1174 if (tidlist == NULL || *tidlist == '\000')
1175 error (_("Please specify a thread ID list"));
1177 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1180 error (_("Please specify a command following the thread ID list"));
1182 /* Save a copy of the command in case it is clobbered by
1184 saved_cmd = xstrdup (cmd);
1185 old_chain = make_cleanup (xfree, saved_cmd);
1186 while (tidlist < cmd)
1188 struct thread_info *tp;
1191 start = strtol (tidlist, &p, 10);
1193 error (_("Error parsing %s"), tidlist);
1196 while (*tidlist == ' ' || *tidlist == '\t')
1199 if (*tidlist == '-') /* Got a range of IDs? */
1201 tidlist++; /* Skip the - */
1202 end = strtol (tidlist, &p, 10);
1204 error (_("Error parsing %s"), tidlist);
1207 while (*tidlist == ' ' || *tidlist == '\t')
1213 make_cleanup_restore_current_thread ();
1215 for (; start <= end; start++)
1217 tp = find_thread_id (start);
1220 warning (_("Unknown thread %d."), start);
1221 else if (!thread_alive (tp))
1222 warning (_("Thread %d has terminated."), start);
1225 switch_to_thread (tp->ptid);
1227 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1228 target_pid_to_str (inferior_ptid));
1229 execute_command (cmd, from_tty);
1231 /* Restore exact command used previously. */
1232 strcpy (cmd, saved_cmd);
1237 do_cleanups (old_chain);
1240 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1241 if prefix of arg is `apply'. */
1244 thread_command (char *tidstr, int from_tty)
1248 if (ptid_equal (inferior_ptid, null_ptid))
1249 error (_("No thread selected"));
1251 if (target_has_stack)
1253 if (is_exited (inferior_ptid))
1254 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1255 pid_to_thread_id (inferior_ptid),
1256 target_pid_to_str (inferior_ptid));
1258 printf_filtered (_("[Current thread is %d (%s)]\n"),
1259 pid_to_thread_id (inferior_ptid),
1260 target_pid_to_str (inferior_ptid));
1263 error (_("No stack."));
1267 gdb_thread_select (uiout, tidstr, NULL);
1270 /* Print notices when new threads are attached and detached. */
1271 int print_thread_events = 1;
1273 show_print_thread_events (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1276 fprintf_filtered (file,
1277 _("Printing of thread events is %s.\n"),
1282 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1285 struct thread_info *tp;
1287 num = value_as_long (parse_and_eval (tidstr));
1289 tp = find_thread_id (num);
1292 error (_("Thread ID %d not known."), num);
1294 if (!thread_alive (tp))
1295 error (_("Thread ID %d has terminated."), num);
1297 switch_to_thread (tp->ptid);
1299 annotate_thread_changed ();
1301 ui_out_text (uiout, "[Switching to thread ");
1302 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1303 ui_out_text (uiout, " (");
1304 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1305 ui_out_text (uiout, ")]");
1307 /* Note that we can't reach this with an exited thread, due to the
1308 thread_alive check above. */
1309 if (tp->state_ == THREAD_RUNNING)
1310 ui_out_text (uiout, "(running)\n");
1313 ui_out_text (uiout, "\n");
1314 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1317 /* Since the current thread may have changed, see if there is any
1318 exited thread we can now delete. */
1325 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1327 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1328 error_message, RETURN_MASK_ALL) < 0)
1334 update_thread_list (void)
1337 target_find_new_threads ();
1340 /* Return a new value for the selected thread's id. Return a value of 0 if
1341 no thread is selected, or no threads exist. */
1343 static struct value *
1344 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var)
1346 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1348 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1349 (tp ? tp->num : 0));
1352 /* Commands with a prefix of `thread'. */
1353 struct cmd_list_element *thread_cmd_list = NULL;
1356 _initialize_thread (void)
1358 static struct cmd_list_element *thread_apply_list = NULL;
1360 add_info ("threads", info_threads_command,
1361 _("IDs of currently known threads."));
1363 add_prefix_cmd ("thread", class_run, thread_command, _("\
1364 Use this command to switch between threads.\n\
1365 The new thread ID must be currently known."),
1366 &thread_cmd_list, "thread ", 1, &cmdlist);
1368 add_prefix_cmd ("apply", class_run, thread_apply_command,
1369 _("Apply a command to a list of threads."),
1370 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1372 add_cmd ("all", class_run, thread_apply_all_command,
1373 _("Apply a command to all threads."), &thread_apply_list);
1376 add_com_alias ("t", "thread", class_run, 1);
1378 add_setshow_boolean_cmd ("thread-events", no_class,
1379 &print_thread_events, _("\
1380 Set printing of thread events (such as thread start and exit)."), _("\
1381 Show printing of thread events (such as thread start and exit)."), NULL,
1383 show_print_thread_events,
1384 &setprintlist, &showprintlist);
1386 create_internalvar_type_lazy ("_thread", thread_id_make_value);