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
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->step_resume_breakpoint)
88 delete_breakpoint (tp->step_resume_breakpoint);
89 tp->step_resume_breakpoint = NULL;
94 clear_thread_inferior_resources (struct thread_info *tp)
96 /* NOTE: this will take care of any left-over step_resume breakpoints,
97 but not any user-specified thread-specific breakpoints. We can not
98 delete the breakpoint straight-off, because the inferior might not
99 be stopped at the moment. */
100 if (tp->step_resume_breakpoint)
102 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
103 tp->step_resume_breakpoint = NULL;
106 bpstat_clear (&tp->stop_bpstat);
108 discard_all_intermediate_continuations_thread (tp);
109 discard_all_continuations_thread (tp);
113 free_thread (struct thread_info *tp)
115 clear_thread_inferior_resources (tp);
119 if (tp->private_dtor)
120 tp->private_dtor (tp->private);
129 init_thread_list (void)
131 struct thread_info *tp, *tpnext;
133 highest_thread_num = 0;
138 for (tp = thread_list; tp; tp = tpnext)
147 /* Allocate a new thread with target id PTID and add it to the thread
150 static struct thread_info *
151 new_thread (ptid_t ptid)
153 struct thread_info *tp;
155 tp = xcalloc (1, sizeof (*tp));
158 tp->num = ++highest_thread_num;
159 tp->next = thread_list;
162 /* Nothing to follow yet. */
163 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
164 tp->state_ = THREAD_STOPPED;
170 add_thread_silent (ptid_t ptid)
172 struct thread_info *tp;
174 tp = find_thread_ptid (ptid);
176 /* Found an old thread with the same id. It has to be dead,
177 otherwise we wouldn't be adding a new thread with the same id.
178 The OS is reusing this id --- delete it, and recreate a new
181 /* In addition to deleting the thread, if this is the current
182 thread, then we need to take care that delete_thread doesn't
183 really delete the thread if it is inferior_ptid. Create a
184 new template thread in the list with an invalid ptid, switch
185 to it, delete the original thread, reset the new thread's
186 ptid, and switch to it. */
188 if (ptid_equal (inferior_ptid, ptid))
190 tp = new_thread (ptid);
192 /* Make switch_to_thread not read from the thread. */
193 tp->state_ = THREAD_EXITED;
194 switch_to_thread (minus_one_ptid);
196 /* Now we can delete it. */
197 delete_thread (ptid);
199 /* Now reset its ptid, and reswitch inferior_ptid to it. */
201 tp->state_ = THREAD_STOPPED;
202 switch_to_thread (ptid);
204 observer_notify_new_thread (tp);
210 /* Just go ahead and delete it. */
211 delete_thread (ptid);
214 tp = new_thread (ptid);
215 observer_notify_new_thread (tp);
221 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
223 struct thread_info *result = add_thread_silent (ptid);
225 result->private = private;
227 if (print_thread_events)
228 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
230 annotate_new_thread ();
235 add_thread (ptid_t ptid)
237 return add_thread_with_info (ptid, NULL);
240 /* Delete thread PTID. If SILENT, don't notify the observer of this
243 delete_thread_1 (ptid_t ptid, int silent)
245 struct thread_info *tp, *tpprev;
249 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
250 if (ptid_equal (tp->ptid, ptid))
256 /* If this is the current thread, or there's code out there that
257 relies on it existing (refcount > 0) we can't delete yet. Mark
258 it as exited, and notify it. */
260 || ptid_equal (tp->ptid, inferior_ptid))
262 if (tp->state_ != THREAD_EXITED)
264 observer_notify_thread_exit (tp, silent);
266 /* Tag it as exited. */
267 tp->state_ = THREAD_EXITED;
269 /* Clear breakpoints, etc. associated with this thread. */
270 clear_thread_inferior_resources (tp);
273 /* Will be really deleted some other time. */
278 tpprev->next = tp->next;
280 thread_list = tp->next;
282 /* Notify thread exit, but only if we haven't already. */
283 if (tp->state_ != THREAD_EXITED)
284 observer_notify_thread_exit (tp, silent);
289 /* Delete thread PTID and notify of thread exit. If this is
290 inferior_ptid, don't actually delete it, but tag it as exited and
291 do the notification. If PTID is the user selected thread, clear
294 delete_thread (ptid_t ptid)
296 delete_thread_1 (ptid, 0 /* not silent */);
300 delete_thread_silent (ptid_t ptid)
302 delete_thread_1 (ptid, 1 /* silent */);
306 find_thread_id (int num)
308 struct thread_info *tp;
310 for (tp = thread_list; tp; tp = tp->next)
317 /* Find a thread_info by matching PTID. */
319 find_thread_ptid (ptid_t ptid)
321 struct thread_info *tp;
323 for (tp = thread_list; tp; tp = tp->next)
324 if (ptid_equal (tp->ptid, ptid))
331 * Thread iterator function.
333 * Calls a callback function once for each thread, so long as
334 * the callback function returns false. If the callback function
335 * returns true, the iteration will end and the current thread
336 * will be returned. This can be useful for implementing a
337 * search for a thread with arbitrary attributes, or for applying
338 * some operation to every thread.
340 * FIXME: some of the existing functionality, such as
341 * "Thread apply all", might be rewritten using this functionality.
345 iterate_over_threads (int (*callback) (struct thread_info *, void *),
348 struct thread_info *tp, *next;
350 for (tp = thread_list; tp; tp = next)
353 if ((*callback) (tp, data))
364 struct thread_info *tp;
366 for (tp = thread_list; tp; tp = tp->next)
373 valid_thread_id (int num)
375 struct thread_info *tp;
377 for (tp = thread_list; tp; tp = tp->next)
385 pid_to_thread_id (ptid_t ptid)
387 struct thread_info *tp;
389 for (tp = thread_list; tp; tp = tp->next)
390 if (ptid_equal (tp->ptid, ptid))
397 thread_id_to_pid (int num)
399 struct thread_info *thread = find_thread_id (num);
403 return pid_to_ptid (-1);
407 in_thread_list (ptid_t ptid)
409 struct thread_info *tp;
411 for (tp = thread_list; tp; tp = tp->next)
412 if (ptid_equal (tp->ptid, ptid))
415 return 0; /* Never heard of 'im */
418 /* Finds the first thread of the inferior given by PID. If PID is -1,
419 return the first thread in the list. */
422 first_thread_of_process (int pid)
424 struct thread_info *tp, *ret = NULL;
426 for (tp = thread_list; tp; tp = tp->next)
427 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
428 if (ret == NULL || tp->num < ret->num)
435 any_thread_of_process (int pid)
437 struct thread_info *tp;
439 for (tp = thread_list; tp; tp = tp->next)
440 if (ptid_get_pid (tp->ptid) == pid)
447 any_live_thread_of_process (int pid)
449 struct thread_info *tp;
450 struct thread_info *tp_running = NULL;
452 for (tp = thread_list; tp; tp = tp->next)
453 if (ptid_get_pid (tp->ptid) == pid)
455 if (tp->state_ == THREAD_STOPPED)
457 else if (tp->state_ == THREAD_RUNNING)
464 /* Print a list of thread ids currently known, and the total number of
465 threads. To be used from within catch_errors. */
467 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
469 struct thread_info *tp;
471 struct cleanup *cleanup_chain;
472 int current_thread = -1;
474 update_thread_list ();
476 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
478 for (tp = thread_list; tp; tp = tp->next)
480 if (tp->state_ == THREAD_EXITED)
483 if (ptid_equal (tp->ptid, inferior_ptid))
484 current_thread = tp->num;
487 ui_out_field_int (uiout, "thread-id", tp->num);
490 do_cleanups (cleanup_chain);
492 if (current_thread != -1)
493 ui_out_field_int (uiout, "current-thread-id", current_thread);
494 ui_out_field_int (uiout, "number-of-threads", num);
498 /* Official gdblib interface function to get a list of thread ids and
501 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
503 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
504 error_message, RETURN_MASK_ALL) < 0)
509 /* Return true if TP is an active thread. */
511 thread_alive (struct thread_info *tp)
513 if (tp->state_ == THREAD_EXITED)
515 if (!target_thread_alive (tp->ptid))
523 struct thread_info *tp, *next;
525 for (tp = thread_list; tp; tp = next)
528 if (!thread_alive (tp))
529 delete_thread (tp->ptid);
534 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
536 struct inferior *inf;
537 struct thread_info *tp;
539 /* It can happen that what we knew as the target inferior id
540 changes. E.g, target remote may only discover the remote process
541 pid after adding the inferior to GDB's list. */
542 inf = find_inferior_pid (ptid_get_pid (old_ptid));
543 inf->pid = ptid_get_pid (new_ptid);
545 tp = find_thread_ptid (old_ptid);
548 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
552 set_running (ptid_t ptid, int running)
554 struct thread_info *tp;
555 int all = ptid_equal (ptid, minus_one_ptid);
557 /* We try not to notify the observer if no thread has actually changed
558 the running state -- merely to reduce the number of messages to
559 frontend. Frontend is supposed to handle multiple *running just fine. */
560 if (all || ptid_is_pid (ptid))
563 for (tp = thread_list; tp; tp = tp->next)
564 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
566 if (tp->state_ == THREAD_EXITED)
568 if (running && tp->state_ == THREAD_STOPPED)
570 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
573 observer_notify_target_resumed (ptid);
578 tp = find_thread_ptid (ptid);
580 gdb_assert (tp->state_ != THREAD_EXITED);
581 if (running && tp->state_ == THREAD_STOPPED)
583 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
585 observer_notify_target_resumed (ptid);
590 is_thread_state (ptid_t ptid, enum thread_state state)
592 struct thread_info *tp;
594 tp = find_thread_ptid (ptid);
596 return tp->state_ == state;
600 is_stopped (ptid_t ptid)
602 return is_thread_state (ptid, THREAD_STOPPED);
606 is_exited (ptid_t ptid)
608 return is_thread_state (ptid, THREAD_EXITED);
612 is_running (ptid_t ptid)
614 return is_thread_state (ptid, THREAD_RUNNING);
620 struct thread_info *tp;
622 for (tp = thread_list; tp; tp = tp->next)
623 if (tp->state_ == THREAD_RUNNING)
630 is_executing (ptid_t ptid)
632 struct thread_info *tp;
634 tp = find_thread_ptid (ptid);
636 return tp->executing_;
640 set_executing (ptid_t ptid, int executing)
642 struct thread_info *tp;
643 int all = ptid_equal (ptid, minus_one_ptid);
645 if (all || ptid_is_pid (ptid))
647 for (tp = thread_list; tp; tp = tp->next)
648 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
649 tp->executing_ = executing;
653 tp = find_thread_ptid (ptid);
655 tp->executing_ = executing;
660 set_stop_requested (ptid_t ptid, int stop)
662 struct thread_info *tp;
663 int all = ptid_equal (ptid, minus_one_ptid);
665 if (all || ptid_is_pid (ptid))
667 for (tp = thread_list; tp; tp = tp->next)
668 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
669 tp->stop_requested = stop;
673 tp = find_thread_ptid (ptid);
675 tp->stop_requested = stop;
678 /* Call the stop requested observer so other components of GDB can
679 react to this request. */
681 observer_notify_thread_stop_requested (ptid);
685 finish_thread_state (ptid_t ptid)
687 struct thread_info *tp;
691 all = ptid_equal (ptid, minus_one_ptid);
693 if (all || ptid_is_pid (ptid))
695 for (tp = thread_list; tp; tp = tp->next)
697 if (tp->state_ == THREAD_EXITED)
699 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
701 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
703 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
709 tp = find_thread_ptid (ptid);
711 if (tp->state_ != THREAD_EXITED)
713 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
715 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
720 observer_notify_target_resumed (ptid);
724 finish_thread_state_cleanup (void *arg)
726 ptid_t *ptid_p = arg;
730 finish_thread_state (*ptid_p);
733 /* Prints the list of threads and their details on UIOUT.
734 This is a version of 'info_thread_command' suitable for
736 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
737 that should be printed. Otherwise, all threads are
739 If PID is not -1, only print threads from the process PID.
740 Otherwise, threads from all attached PIDs are printed.
741 If both REQUESTED_THREAD and PID are not -1, then the thread
742 is printed if it belongs to the specified process. Otherwise,
743 an error is raised. */
745 print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
747 struct thread_info *tp;
749 struct cleanup *old_chain;
751 int current_thread = -1;
753 update_thread_list ();
754 current_ptid = inferior_ptid;
756 /* We'll be switching threads temporarily. */
757 old_chain = make_cleanup_restore_current_thread ();
759 make_cleanup_ui_out_list_begin_end (uiout, "threads");
760 for (tp = thread_list; tp; tp = tp->next)
762 struct cleanup *chain2;
765 if (requested_thread != -1 && tp->num != requested_thread)
768 if (pid != -1 && PIDGET (tp->ptid) != pid)
770 if (requested_thread != -1)
771 error (_("Requested thread not found in requested process"));
775 if (ptid_equal (tp->ptid, current_ptid))
776 current_thread = tp->num;
778 if (tp->state_ == THREAD_EXITED)
781 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
783 if (ptid_equal (tp->ptid, current_ptid))
784 ui_out_text (uiout, "* ");
786 ui_out_text (uiout, " ");
788 ui_out_field_int (uiout, "id", tp->num);
789 ui_out_text (uiout, " ");
790 ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
792 extra_info = target_extra_thread_info (tp);
795 ui_out_text (uiout, " (");
796 ui_out_field_string (uiout, "details", extra_info);
797 ui_out_text (uiout, ")");
799 ui_out_text (uiout, " ");
801 if (tp->state_ == THREAD_RUNNING)
802 ui_out_text (uiout, "(running)\n");
805 /* The switch below puts us at the top of the stack (leaf
807 switch_to_thread (tp->ptid);
808 print_stack_frame (get_selected_frame (NULL),
809 /* For MI output, print frame level. */
810 ui_out_is_mi_like_p (uiout),
814 if (ui_out_is_mi_like_p (uiout))
816 char *state = "stopped";
817 if (tp->state_ == THREAD_RUNNING)
819 ui_out_field_string (uiout, "state", state);
822 core = target_core_of_thread (tp->ptid);
823 if (ui_out_is_mi_like_p (uiout) && core != -1)
824 ui_out_field_int (uiout, "core", core);
826 do_cleanups (chain2);
829 /* Restores the current thread and the frame selected before
830 the "info threads" command. */
831 do_cleanups (old_chain);
833 if (pid == -1 && requested_thread == -1)
835 gdb_assert (current_thread != -1
837 || ptid_equal (inferior_ptid, null_ptid));
838 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
839 ui_out_field_int (uiout, "current-thread-id", current_thread);
841 if (current_thread != -1 && is_exited (current_ptid))
842 ui_out_message (uiout, 0, "\n\
843 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
846 && current_thread == -1
847 && ptid_equal (current_ptid, null_ptid))
848 ui_out_message (uiout, 0, "\n\
849 No selected thread. See `help thread'.\n");
854 /* Print information about currently known threads
856 * Note: this has the drawback that it _really_ switches
857 * threads, which frees the frame cache. A no-side
858 * effects info-threads command would be nicer.
862 info_threads_command (char *arg, int from_tty)
864 print_thread_info (uiout, -1, -1);
867 /* Switch from one thread to another. */
870 switch_to_thread (ptid_t ptid)
872 /* Switch the program space as well, if we can infer it from the now
873 current thread. Otherwise, it's up to the caller to select the
875 if (!ptid_equal (ptid, null_ptid))
877 struct inferior *inf;
879 inf = find_inferior_pid (ptid_get_pid (ptid));
880 gdb_assert (inf != NULL);
881 set_current_program_space (inf->pspace);
882 set_current_inferior (inf);
885 if (ptid_equal (ptid, inferior_ptid))
888 inferior_ptid = ptid;
889 reinit_frame_cache ();
890 registers_changed ();
892 /* We don't check for is_stopped, because we're called at times
893 while in the TARGET_RUNNING state, e.g., while handling an
895 if (!ptid_equal (inferior_ptid, null_ptid)
897 && !is_executing (ptid))
898 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
900 stop_pc = ~(CORE_ADDR) 0;
904 restore_current_thread (ptid_t ptid)
906 switch_to_thread (ptid);
910 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
912 struct frame_info *frame = NULL;
915 gdb_assert (frame_level >= 0);
917 /* Restore by level first, check if the frame id is the same as
918 expected. If that fails, try restoring by frame id. If that
919 fails, nothing to do, just warn the user. */
922 frame = find_relative_frame (get_current_frame (), &count);
925 /* The frame ids must match - either both valid or both outer_frame_id.
926 The latter case is not failsafe, but since it's highly unlikely
927 the search by level finds the wrong frame, it's 99.9(9)% of
928 the time (for all practical purposes) safe. */
929 && frame_id_eq (get_frame_id (frame), a_frame_id))
931 /* Cool, all is fine. */
932 select_frame (frame);
936 frame = frame_find_by_id (a_frame_id);
939 /* Cool, refound it. */
940 select_frame (frame);
944 /* Nothing else to do, the frame layout really changed. Select the
945 innermost stack frame. */
946 select_frame (get_current_frame ());
949 if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
952 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
954 /* For MI, we should probably have a notification about
955 current frame change. But this error is not very
956 likely, so don't bother for now. */
957 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
961 struct current_thread_cleanup
963 ptid_t inferior_ptid;
964 struct frame_id selected_frame_id;
965 int selected_frame_level;
971 do_restore_current_thread_cleanup (void *arg)
973 struct thread_info *tp;
974 struct current_thread_cleanup *old = arg;
976 tp = find_thread_ptid (old->inferior_ptid);
978 /* If the previously selected thread belonged to a process that has
979 in the mean time been deleted (due to normal exit, detach, etc.),
980 then don't revert back to it, but instead simply drop back to no
983 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
984 restore_current_thread (old->inferior_ptid);
987 restore_current_thread (null_ptid);
988 set_current_inferior (find_inferior_id (old->inf_id));
991 /* The running state of the originally selected thread may have
992 changed, so we have to recheck it here. */
993 if (!ptid_equal (inferior_ptid, null_ptid)
995 && is_stopped (inferior_ptid)
996 && target_has_registers
998 && target_has_memory)
999 restore_selected_frame (old->selected_frame_id,
1000 old->selected_frame_level);
1004 restore_current_thread_cleanup_dtor (void *arg)
1006 struct current_thread_cleanup *old = arg;
1007 struct thread_info *tp;
1008 tp = find_thread_ptid (old->inferior_ptid);
1015 make_cleanup_restore_current_thread (void)
1017 struct thread_info *tp;
1018 struct frame_info *frame;
1019 struct current_thread_cleanup *old;
1021 old = xmalloc (sizeof (struct current_thread_cleanup));
1022 old->inferior_ptid = inferior_ptid;
1023 old->inf_id = current_inferior ()->num;
1025 if (!ptid_equal (inferior_ptid, null_ptid))
1027 old->was_stopped = is_stopped (inferior_ptid);
1028 if (old->was_stopped
1029 && target_has_registers
1031 && target_has_memory)
1032 frame = get_selected_frame (NULL);
1036 old->selected_frame_id = get_frame_id (frame);
1037 old->selected_frame_level = frame_relative_level (frame);
1039 tp = find_thread_ptid (inferior_ptid);
1044 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1045 restore_current_thread_cleanup_dtor);
1048 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1049 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1050 of two numbers seperated by a hyphen. Examples:
1052 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1053 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1054 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1058 thread_apply_all_command (char *cmd, int from_tty)
1060 struct thread_info *tp;
1061 struct cleanup *old_chain;
1064 if (cmd == NULL || *cmd == '\000')
1065 error (_("Please specify a command following the thread ID list"));
1067 update_thread_list ();
1069 old_chain = make_cleanup_restore_current_thread ();
1071 /* Save a copy of the command in case it is clobbered by
1073 saved_cmd = xstrdup (cmd);
1074 make_cleanup (xfree, saved_cmd);
1075 for (tp = thread_list; tp; tp = tp->next)
1076 if (thread_alive (tp))
1078 switch_to_thread (tp->ptid);
1080 printf_filtered (_("\nThread %d (%s):\n"),
1081 tp->num, target_pid_to_str (inferior_ptid));
1082 execute_command (cmd, from_tty);
1083 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
1086 do_cleanups (old_chain);
1090 thread_apply_command (char *tidlist, int from_tty)
1094 struct cleanup *old_chain;
1097 if (tidlist == NULL || *tidlist == '\000')
1098 error (_("Please specify a thread ID list"));
1100 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1103 error (_("Please specify a command following the thread ID list"));
1105 /* Save a copy of the command in case it is clobbered by
1107 saved_cmd = xstrdup (cmd);
1108 old_chain = make_cleanup (xfree, saved_cmd);
1109 while (tidlist < cmd)
1111 struct thread_info *tp;
1114 start = strtol (tidlist, &p, 10);
1116 error (_("Error parsing %s"), tidlist);
1119 while (*tidlist == ' ' || *tidlist == '\t')
1122 if (*tidlist == '-') /* Got a range of IDs? */
1124 tidlist++; /* Skip the - */
1125 end = strtol (tidlist, &p, 10);
1127 error (_("Error parsing %s"), tidlist);
1130 while (*tidlist == ' ' || *tidlist == '\t')
1136 make_cleanup_restore_current_thread ();
1138 for (; start <= end; start++)
1140 tp = find_thread_id (start);
1143 warning (_("Unknown thread %d."), start);
1144 else if (!thread_alive (tp))
1145 warning (_("Thread %d has terminated."), start);
1148 switch_to_thread (tp->ptid);
1150 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1151 target_pid_to_str (inferior_ptid));
1152 execute_command (cmd, from_tty);
1154 /* Restore exact command used previously. */
1155 strcpy (cmd, saved_cmd);
1160 do_cleanups (old_chain);
1163 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1164 if prefix of arg is `apply'. */
1167 thread_command (char *tidstr, int from_tty)
1171 if (ptid_equal (inferior_ptid, null_ptid))
1172 error (_("No thread selected"));
1174 if (target_has_stack)
1176 if (is_exited (inferior_ptid))
1177 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1178 pid_to_thread_id (inferior_ptid),
1179 target_pid_to_str (inferior_ptid));
1181 printf_filtered (_("[Current thread is %d (%s)]\n"),
1182 pid_to_thread_id (inferior_ptid),
1183 target_pid_to_str (inferior_ptid));
1186 error (_("No stack."));
1190 gdb_thread_select (uiout, tidstr, NULL);
1193 /* Print notices when new threads are attached and detached. */
1194 int print_thread_events = 1;
1196 show_print_thread_events (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1199 fprintf_filtered (file, _("\
1200 Printing of thread events is %s.\n"),
1205 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1208 struct thread_info *tp;
1210 num = value_as_long (parse_and_eval (tidstr));
1212 tp = find_thread_id (num);
1215 error (_("Thread ID %d not known."), num);
1217 if (!thread_alive (tp))
1218 error (_("Thread ID %d has terminated."), num);
1220 switch_to_thread (tp->ptid);
1222 annotate_thread_changed ();
1224 ui_out_text (uiout, "[Switching to thread ");
1225 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1226 ui_out_text (uiout, " (");
1227 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1228 ui_out_text (uiout, ")]");
1230 /* Note that we can't reach this with an exited thread, due to the
1231 thread_alive check above. */
1232 if (tp->state_ == THREAD_RUNNING)
1233 ui_out_text (uiout, "(running)\n");
1235 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1237 /* Since the current thread may have changed, see if there is any
1238 exited thread we can now delete. */
1245 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1247 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1248 error_message, RETURN_MASK_ALL) < 0)
1254 update_thread_list (void)
1257 target_find_new_threads ();
1260 /* Commands with a prefix of `thread'. */
1261 struct cmd_list_element *thread_cmd_list = NULL;
1264 _initialize_thread (void)
1266 static struct cmd_list_element *thread_apply_list = NULL;
1268 add_info ("threads", info_threads_command,
1269 _("IDs of currently known threads."));
1271 add_prefix_cmd ("thread", class_run, thread_command, _("\
1272 Use this command to switch between threads.\n\
1273 The new thread ID must be currently known."),
1274 &thread_cmd_list, "thread ", 1, &cmdlist);
1276 add_prefix_cmd ("apply", class_run, thread_apply_command,
1277 _("Apply a command to a list of threads."),
1278 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1280 add_cmd ("all", class_run, thread_apply_all_command,
1281 _("Apply a command to all threads."), &thread_apply_list);
1284 add_com_alias ("t", "thread", class_run, 1);
1286 add_setshow_boolean_cmd ("thread-events", no_class,
1287 &print_thread_events, _("\
1288 Set printing of thread events (such as thread start and exit)."), _("\
1289 Show printing of thread events (such as thread start and exit)."), NULL,
1291 show_print_thread_events,
1292 &setprintlist, &showprintlist);