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 Free Software Foundation, Inc.
6 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdbthread.h"
31 #include "exceptions.h"
36 #include "gdb_string.h"
39 #include <sys/types.h>
44 #include "cli/cli-decode.h"
46 /* Definition of struct thread_info exported to gdbthread.h */
48 /* Prototypes for exported functions. */
50 void _initialize_thread (void);
52 /* Prototypes for local functions. */
54 static struct thread_info *thread_list = NULL;
55 static int highest_thread_num;
57 static struct thread_info *find_thread_id (int num);
59 static void thread_command (char *tidstr, int from_tty);
60 static void thread_apply_all_command (char *, int);
61 static int thread_alive (struct thread_info *);
62 static void info_threads_command (char *, int);
63 static void thread_apply_command (char *, int);
64 static void restore_current_thread (ptid_t);
65 static void prune_threads (void);
67 /* Frontend view of the thread state. Possible extensions: stepping,
68 finishing, until(ling),... */
76 static enum thread_state main_thread_state = THREAD_STOPPED;
77 static int main_thread_executing = 0;
80 delete_step_resume_breakpoint (void *arg)
82 struct breakpoint **breakpointp = (struct breakpoint **) arg;
83 struct thread_info *tp;
85 if (*breakpointp != NULL)
87 delete_breakpoint (*breakpointp);
88 for (tp = thread_list; tp; tp = tp->next)
89 if (tp->step_resume_breakpoint == *breakpointp)
90 tp->step_resume_breakpoint = NULL;
97 clear_thread_inferior_resources (struct thread_info *tp)
99 /* NOTE: this will take care of any left-over step_resume breakpoints,
100 but not any user-specified thread-specific breakpoints. We can not
101 delete the breakpoint straight-off, because the inferior might not
102 be stopped at the moment. */
103 if (tp->step_resume_breakpoint)
105 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
106 tp->step_resume_breakpoint = NULL;
109 bpstat_clear (&tp->stop_bpstat);
113 free_thread (struct thread_info *tp)
115 clear_thread_inferior_resources (tp);
117 /* FIXME: do I ever need to call the back-end to give it a
118 chance at this private data before deleting the thread? */
126 init_thread_list (void)
128 struct thread_info *tp, *tpnext;
130 highest_thread_num = 0;
131 main_thread_state = THREAD_STOPPED;
132 main_thread_executing = 0;
137 for (tp = thread_list; tp; tp = tpnext)
147 add_thread_silent (ptid_t ptid)
149 struct thread_info *tp;
151 tp = find_thread_pid (ptid);
153 /* Found an old thread with the same id. It has to be dead,
154 otherwise we wouldn't be adding a new thread with the same id.
155 The OS is reusing this id --- delete it, and recreate a new
158 /* In addition to deleting the thread, if this is the current
159 thread, then we need to also get rid of the current infrun
160 context, and take care that delete_thread doesn't really
161 delete the thread if it is inferior_ptid. Create a new
162 template thread in the list with an invalid ptid, context
163 switch to it, delete the original thread, reset the new
164 thread's ptid, and switch to it. */
166 if (ptid_equal (inferior_ptid, ptid))
168 tp = xmalloc (sizeof (*tp));
169 memset (tp, 0, sizeof (*tp));
170 tp->ptid = minus_one_ptid;
171 tp->num = ++highest_thread_num;
172 tp->next = thread_list;
174 context_switch_to (minus_one_ptid);
176 /* Now we can delete it. */
177 delete_thread (ptid);
179 /* Since the context is already set to this new thread,
180 reset its ptid, and reswitch inferior_ptid to it. */
182 switch_to_thread (ptid);
184 observer_notify_new_thread (tp);
190 /* Just go ahead and delete it. */
191 delete_thread (ptid);
194 tp = (struct thread_info *) xmalloc (sizeof (*tp));
195 memset (tp, 0, sizeof (*tp));
197 tp->num = ++highest_thread_num;
198 tp->next = thread_list;
201 observer_notify_new_thread (tp);
207 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
209 struct thread_info *result = add_thread_silent (ptid);
211 result->private = private;
213 if (print_thread_events)
214 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
216 annotate_new_thread ();
221 add_thread (ptid_t ptid)
223 return add_thread_with_info (ptid, NULL);
226 /* Delete thread PTID. If SILENT, don't notify the observer of this
229 delete_thread_1 (ptid_t ptid, int silent)
231 struct thread_info *tp, *tpprev;
235 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
236 if (ptid_equal (tp->ptid, ptid))
242 /* If this is the current thread, or there's code out there that
243 relies on it existing (refcount > 0) we can't delete yet. Mark
244 it as exited, and notify it. */
246 || ptid_equal (tp->ptid, inferior_ptid))
248 if (tp->state_ != THREAD_EXITED)
251 observer_notify_thread_exit (tp);
253 /* Tag it as exited. */
254 tp->state_ = THREAD_EXITED;
256 /* Clear breakpoints, etc. associated with this thread. */
257 clear_thread_inferior_resources (tp);
260 /* Will be really deleted some other time. */
265 tpprev->next = tp->next;
267 thread_list = tp->next;
269 /* Notify thread exit, but only if we haven't already. */
270 if (!silent && tp->state_ != THREAD_EXITED)
271 observer_notify_thread_exit (tp);
276 /* Delete thread PTID and notify of thread exit. If this is
277 inferior_ptid, don't actually delete it, but tag it as exited and
278 do the notification. If PTID is the user selected thread, clear
281 delete_thread (ptid_t ptid)
283 delete_thread_1 (ptid, 0 /* not silent */);
287 delete_thread_silent (ptid_t ptid)
289 delete_thread_1 (ptid, 1 /* silent */);
292 static struct thread_info *
293 find_thread_id (int num)
295 struct thread_info *tp;
297 for (tp = thread_list; tp; tp = tp->next)
304 /* Find a thread_info by matching PTID. */
306 find_thread_pid (ptid_t ptid)
308 struct thread_info *tp;
310 for (tp = thread_list; tp; tp = tp->next)
311 if (ptid_equal (tp->ptid, ptid))
318 * Thread iterator function.
320 * Calls a callback function once for each thread, so long as
321 * the callback function returns false. If the callback function
322 * returns true, the iteration will end and the current thread
323 * will be returned. This can be useful for implementing a
324 * search for a thread with arbitrary attributes, or for applying
325 * some operation to every thread.
327 * FIXME: some of the existing functionality, such as
328 * "Thread apply all", might be rewritten using this functionality.
332 iterate_over_threads (int (*callback) (struct thread_info *, void *),
335 struct thread_info *tp, *next;
337 for (tp = thread_list; tp; tp = next)
340 if ((*callback) (tp, data))
351 struct thread_info *tp;
353 for (tp = thread_list; tp; tp = tp->next)
360 valid_thread_id (int num)
362 struct thread_info *tp;
364 for (tp = thread_list; tp; tp = tp->next)
372 pid_to_thread_id (ptid_t ptid)
374 struct thread_info *tp;
376 for (tp = thread_list; tp; tp = tp->next)
377 if (ptid_equal (tp->ptid, ptid))
384 thread_id_to_pid (int num)
386 struct thread_info *thread = find_thread_id (num);
390 return pid_to_ptid (-1);
394 in_thread_list (ptid_t ptid)
396 struct thread_info *tp;
398 for (tp = thread_list; tp; tp = tp->next)
399 if (ptid_equal (tp->ptid, ptid))
402 return 0; /* Never heard of 'im */
405 /* Print a list of thread ids currently known, and the total number of
406 threads. To be used from within catch_errors. */
408 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
410 struct thread_info *tp;
412 struct cleanup *cleanup_chain;
415 target_find_new_threads ();
417 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
419 for (tp = thread_list; tp; tp = tp->next)
421 if (tp->state_ == THREAD_EXITED)
424 ui_out_field_int (uiout, "thread-id", tp->num);
427 do_cleanups (cleanup_chain);
428 ui_out_field_int (uiout, "number-of-threads", num);
432 /* Official gdblib interface function to get a list of thread ids and
435 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
437 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
438 error_message, RETURN_MASK_ALL) < 0)
443 /* Load infrun state for the thread PID. */
446 load_infrun_state (ptid_t ptid,
449 struct breakpoint **step_resume_breakpoint,
450 CORE_ADDR *step_range_start,
451 CORE_ADDR *step_range_end,
452 struct frame_id *step_frame_id,
453 int *stepping_over_breakpoint,
454 int *stepping_through_solib_after_catch,
455 bpstat *stepping_through_solib_catchpoints,
457 struct symtab **current_symtab,
458 struct continuation **continuations,
459 struct continuation **intermediate_continuations,
460 int *proceed_to_finish,
461 enum step_over_calls_kind *step_over_calls,
464 enum target_signal *stop_signal,
467 struct thread_info *tp;
469 /* If we can't find the thread, then we're debugging a single threaded
470 process. No need to do anything in that case. */
471 tp = find_thread_id (pid_to_thread_id (ptid));
475 *prev_pc = tp->prev_pc;
476 *trap_expected = tp->trap_expected;
477 *step_resume_breakpoint = tp->step_resume_breakpoint;
478 *step_range_start = tp->step_range_start;
479 *step_range_end = tp->step_range_end;
480 *step_frame_id = tp->step_frame_id;
481 *stepping_over_breakpoint = tp->stepping_over_breakpoint;
482 *stepping_through_solib_after_catch =
483 tp->stepping_through_solib_after_catch;
484 *stepping_through_solib_catchpoints =
485 tp->stepping_through_solib_catchpoints;
486 *current_line = tp->current_line;
487 *current_symtab = tp->current_symtab;
489 /* In all-stop mode, these are global state, while in non-stop mode,
490 they are per thread. */
493 *continuations = tp->continuations;
494 tp->continuations = NULL;
495 *intermediate_continuations = tp->intermediate_continuations;
496 tp->intermediate_continuations = NULL;
497 *proceed_to_finish = tp->proceed_to_finish;
498 *step_over_calls = tp->step_over_calls;
499 *stop_step = tp->stop_step;
500 *step_multi = tp->step_multi;
501 *stop_signal = tp->stop_signal;
503 /* Swap instead of copy, so we only have to update one of
505 *stop_bpstat = tp->stop_bpstat;
510 /* Save infrun state for the thread PID. */
513 save_infrun_state (ptid_t ptid,
516 struct breakpoint *step_resume_breakpoint,
517 CORE_ADDR step_range_start,
518 CORE_ADDR step_range_end,
519 const struct frame_id *step_frame_id,
520 int stepping_over_breakpoint,
521 int stepping_through_solib_after_catch,
522 bpstat stepping_through_solib_catchpoints,
524 struct symtab *current_symtab,
525 struct continuation *continuations,
526 struct continuation *intermediate_continuations,
527 int proceed_to_finish,
528 enum step_over_calls_kind step_over_calls,
531 enum target_signal stop_signal,
534 struct thread_info *tp;
536 /* If we can't find the thread, then we're debugging a single-threaded
537 process. Nothing to do in that case. */
538 tp = find_thread_id (pid_to_thread_id (ptid));
542 tp->prev_pc = prev_pc;
543 tp->trap_expected = trap_expected;
544 tp->step_resume_breakpoint = step_resume_breakpoint;
545 tp->step_range_start = step_range_start;
546 tp->step_range_end = step_range_end;
547 tp->step_frame_id = (*step_frame_id);
548 tp->stepping_over_breakpoint = stepping_over_breakpoint;
549 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
550 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
551 tp->current_line = current_line;
552 tp->current_symtab = current_symtab;
554 /* In all-stop mode, these are global state, while in non-stop mode,
555 they are per thread. */
558 tp->continuations = continuations;
559 tp->intermediate_continuations = intermediate_continuations;
560 tp->proceed_to_finish = proceed_to_finish;
561 tp->step_over_calls = step_over_calls;
562 tp->stop_step = stop_step;
563 tp->step_multi = step_multi;
564 tp->stop_signal = stop_signal;
565 tp->stop_bpstat = stop_bpstat;
569 /* Return true if TP is an active thread. */
571 thread_alive (struct thread_info *tp)
573 if (tp->state_ == THREAD_EXITED)
575 if (!target_thread_alive (tp->ptid))
583 struct thread_info *tp, *next;
585 for (tp = thread_list; tp; tp = next)
588 if (!thread_alive (tp))
589 delete_thread (tp->ptid);
594 set_running (ptid_t ptid, int running)
596 struct thread_info *tp;
600 /* This is one of the targets that does not add main
601 thread to the thread list. Just use a single
602 global flag to indicate that a thread is running.
604 This problem is unique to ST programs. For MT programs,
605 the main thread is always present in the thread list. If it's
606 not, the first call to context_switch will mess up GDB internal
609 && main_thread_state != THREAD_RUNNING
610 && !suppress_resume_observer)
611 observer_notify_target_resumed (ptid);
612 main_thread_state = running ? THREAD_RUNNING : THREAD_STOPPED;
616 /* We try not to notify the observer if no thread has actually changed
617 the running state -- merely to reduce the number of messages to
618 frontend. Frontend is supposed to handle multiple *running just fine. */
619 if (PIDGET (ptid) == -1)
622 for (tp = thread_list; tp; tp = tp->next)
624 if (tp->state_ == THREAD_EXITED)
626 if (running && tp->state_ == THREAD_STOPPED)
628 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
630 if (any_started && !suppress_resume_observer)
631 observer_notify_target_resumed (ptid);
636 tp = find_thread_pid (ptid);
638 gdb_assert (tp->state_ != THREAD_EXITED);
639 if (running && tp->state_ == THREAD_STOPPED)
641 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
642 if (started && !suppress_resume_observer)
643 observer_notify_target_resumed (ptid);
648 is_thread_state (ptid_t ptid, enum thread_state state)
650 struct thread_info *tp;
652 if (!target_has_execution)
656 return main_thread_state == state;
658 tp = find_thread_pid (ptid);
660 return tp->state_ == state;
664 is_stopped (ptid_t ptid)
666 /* Without execution, this property is always true. */
667 if (!target_has_execution)
670 return is_thread_state (ptid, THREAD_STOPPED);
674 is_exited (ptid_t ptid)
676 /* Without execution, this property is always false. */
677 if (!target_has_execution)
680 return is_thread_state (ptid, THREAD_EXITED);
684 is_running (ptid_t ptid)
686 /* Without execution, this property is always false. */
687 if (!target_has_execution)
690 return is_thread_state (ptid, THREAD_RUNNING);
696 struct thread_info *tp;
698 if (!target_has_execution)
702 return main_thread_state == THREAD_RUNNING;
704 for (tp = thread_list; tp; tp = tp->next)
705 if (tp->state_ == THREAD_RUNNING)
712 is_executing (ptid_t ptid)
714 struct thread_info *tp;
716 if (!target_has_execution)
720 return main_thread_executing;
722 tp = find_thread_pid (ptid);
724 return tp->executing_;
728 set_executing (ptid_t ptid, int executing)
730 struct thread_info *tp;
734 /* This target does not add the main thread to the thread list.
735 Use a global flag to indicate that the thread is
737 main_thread_executing = executing;
741 if (PIDGET (ptid) == -1)
743 for (tp = thread_list; tp; tp = tp->next)
744 tp->executing_ = executing;
748 tp = find_thread_pid (ptid);
750 tp->executing_ = executing;
754 /* Prints the list of threads and their details on UIOUT.
755 This is a version of 'info_thread_command' suitable for
757 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
758 that should be printed. Otherwise, all threads are
761 print_thread_info (struct ui_out *uiout, int requested_thread)
763 struct thread_info *tp;
765 struct cleanup *old_chain;
767 int current_thread = -1;
770 target_find_new_threads ();
771 current_ptid = inferior_ptid;
773 /* We'll be switching threads temporarily. */
774 old_chain = make_cleanup_restore_current_thread ();
776 make_cleanup_ui_out_list_begin_end (uiout, "threads");
777 for (tp = thread_list; tp; tp = tp->next)
779 struct cleanup *chain2;
781 if (requested_thread != -1 && tp->num != requested_thread)
784 if (ptid_equal (tp->ptid, current_ptid))
785 current_thread = tp->num;
787 if (tp->state_ == THREAD_EXITED)
790 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
792 if (ptid_equal (tp->ptid, current_ptid))
793 ui_out_text (uiout, "* ");
795 ui_out_text (uiout, " ");
797 ui_out_field_int (uiout, "id", tp->num);
798 ui_out_text (uiout, " ");
799 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
801 if (tp->state_ != THREAD_EXITED)
803 extra_info = target_extra_thread_info (tp);
806 ui_out_text (uiout, " (");
807 ui_out_field_string (uiout, "details", extra_info);
808 ui_out_text (uiout, ")");
810 ui_out_text (uiout, " ");
813 if (tp->state_ == THREAD_RUNNING)
814 ui_out_text (uiout, "(running)\n");
817 /* The switch below puts us at the top of the stack (leaf
819 switch_to_thread (tp->ptid);
820 print_stack_frame (get_selected_frame (NULL),
821 /* For MI output, print frame level. */
822 ui_out_is_mi_like_p (uiout),
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 (requested_thread == -1)
835 gdb_assert (current_thread != -1
837 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
838 ui_out_field_int (uiout, "current-thread-id", current_thread);
840 if (current_thread != -1 && is_exited (current_ptid))
841 ui_out_message (uiout, 0, "\n\
842 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
848 /* Print information about currently known threads
850 * Note: this has the drawback that it _really_ switches
851 * threads, which frees the frame cache. A no-side
852 * effects info-threads command would be nicer.
856 info_threads_command (char *arg, int from_tty)
858 print_thread_info (uiout, -1);
861 /* Switch from one thread to another. */
864 switch_to_thread (ptid_t ptid)
866 if (ptid_equal (ptid, inferior_ptid))
869 inferior_ptid = ptid;
870 reinit_frame_cache ();
871 registers_changed ();
873 /* We don't check for is_stopped, because we're called at times
874 while in the TARGET_RUNNING state, e.g., while handling an
876 if (!is_exited (ptid) && !is_executing (ptid))
877 stop_pc = read_pc ();
879 stop_pc = ~(CORE_ADDR) 0;
883 restore_current_thread (ptid_t ptid)
885 if (!ptid_equal (ptid, inferior_ptid))
888 context_switch_to (ptid);
890 switch_to_thread (ptid);
895 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
897 struct frame_info *frame = NULL;
900 gdb_assert (frame_level >= 0);
902 /* Restore by level first, check if the frame id is the same as
903 expected. If that fails, try restoring by frame id. If that
904 fails, nothing to do, just warn the user. */
907 frame = find_relative_frame (get_current_frame (), &count);
910 /* Either the frame ids match, of they're both invalid. The
911 latter case is not failsafe, but since it's highly unlikely
912 the search by level finds the wrong frame, it's 99.9(9)% of
913 the time (for all practical purposes) safe. */
914 && (frame_id_eq (get_frame_id (frame), a_frame_id)
915 /* Note: could be better to check every frame_id
916 member for equality here. */
917 || (!frame_id_p (get_frame_id (frame))
918 && !frame_id_p (a_frame_id))))
920 /* Cool, all is fine. */
921 select_frame (frame);
925 frame = frame_find_by_id (a_frame_id);
928 /* Cool, refound it. */
929 select_frame (frame);
933 /* Nothing else to do, the frame layout really changed.
935 if (!ui_out_is_mi_like_p (uiout))
938 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
940 /* For MI, we should probably have a notification about
941 current frame change. But this error is not very
942 likely, so don't bother for now. */
943 print_stack_frame (frame, 1, SRC_LINE);
947 struct current_thread_cleanup
949 ptid_t inferior_ptid;
950 struct frame_id selected_frame_id;
951 int selected_frame_level;
956 do_restore_current_thread_cleanup (void *arg)
958 struct thread_info *tp;
959 struct current_thread_cleanup *old = arg;
960 restore_current_thread (old->inferior_ptid);
962 /* The running state of the originally selected thread may have
963 changed, so we have to recheck it here. */
965 && is_stopped (inferior_ptid)
966 && target_has_registers
968 && target_has_memory)
969 restore_selected_frame (old->selected_frame_id,
970 old->selected_frame_level);
974 restore_current_thread_cleanup_dtor (void *arg)
976 struct current_thread_cleanup *old = arg;
977 struct thread_info *tp;
978 tp = find_thread_pid (old->inferior_ptid);
985 make_cleanup_restore_current_thread (void)
987 struct thread_info *tp;
988 struct frame_info *frame;
989 struct current_thread_cleanup *old;
991 old = xmalloc (sizeof (struct current_thread_cleanup));
992 old->inferior_ptid = inferior_ptid;
993 old->was_stopped = is_stopped (inferior_ptid);
995 && target_has_registers
997 && target_has_memory)
998 frame = get_selected_frame (NULL);
1002 old->selected_frame_id = get_frame_id (frame);
1003 old->selected_frame_level = frame_relative_level (frame);
1005 tp = find_thread_pid (inferior_ptid);
1009 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1010 restore_current_thread_cleanup_dtor);
1013 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1014 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1015 of two numbers seperated by a hyphen. Examples:
1017 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1018 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1019 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1023 thread_apply_all_command (char *cmd, int from_tty)
1025 struct thread_info *tp;
1026 struct cleanup *old_chain;
1029 if (cmd == NULL || *cmd == '\000')
1030 error (_("Please specify a command following the thread ID list"));
1033 target_find_new_threads ();
1035 old_chain = make_cleanup_restore_current_thread ();
1037 /* Save a copy of the command in case it is clobbered by
1039 saved_cmd = xstrdup (cmd);
1040 make_cleanup (xfree, saved_cmd);
1041 for (tp = thread_list; tp; tp = tp->next)
1042 if (thread_alive (tp))
1045 context_switch_to (tp->ptid);
1047 switch_to_thread (tp->ptid);
1049 printf_filtered (_("\nThread %d (%s):\n"),
1050 tp->num, target_tid_to_str (inferior_ptid));
1051 execute_command (cmd, from_tty);
1052 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
1055 do_cleanups (old_chain);
1059 thread_apply_command (char *tidlist, int from_tty)
1063 struct cleanup *old_chain;
1066 if (tidlist == NULL || *tidlist == '\000')
1067 error (_("Please specify a thread ID list"));
1069 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1072 error (_("Please specify a command following the thread ID list"));
1074 /* Save a copy of the command in case it is clobbered by
1076 saved_cmd = xstrdup (cmd);
1077 old_chain = make_cleanup (xfree, saved_cmd);
1078 while (tidlist < cmd)
1080 struct thread_info *tp;
1083 start = strtol (tidlist, &p, 10);
1085 error (_("Error parsing %s"), tidlist);
1088 while (*tidlist == ' ' || *tidlist == '\t')
1091 if (*tidlist == '-') /* Got a range of IDs? */
1093 tidlist++; /* Skip the - */
1094 end = strtol (tidlist, &p, 10);
1096 error (_("Error parsing %s"), tidlist);
1099 while (*tidlist == ' ' || *tidlist == '\t')
1105 make_cleanup_restore_current_thread ();
1107 for (; start <= end; start++)
1109 tp = find_thread_id (start);
1112 warning (_("Unknown thread %d."), start);
1113 else if (!thread_alive (tp))
1114 warning (_("Thread %d has terminated."), start);
1118 context_switch_to (tp->ptid);
1120 switch_to_thread (tp->ptid);
1122 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1123 target_tid_to_str (inferior_ptid));
1124 execute_command (cmd, from_tty);
1126 /* Restore exact command used previously. */
1127 strcpy (cmd, saved_cmd);
1132 do_cleanups (old_chain);
1135 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1136 if prefix of arg is `apply'. */
1139 thread_command (char *tidstr, int from_tty)
1143 if (target_has_stack)
1145 if (is_exited (inferior_ptid))
1146 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1147 pid_to_thread_id (inferior_ptid),
1148 target_tid_to_str (inferior_ptid));
1150 printf_filtered (_("[Current thread is %d (%s)]\n"),
1151 pid_to_thread_id (inferior_ptid),
1152 target_tid_to_str (inferior_ptid));
1155 error (_("No stack."));
1159 annotate_thread_changed ();
1160 gdb_thread_select (uiout, tidstr, NULL);
1163 /* Print notices when new threads are attached and detached. */
1164 int print_thread_events = 1;
1166 show_print_thread_events (struct ui_file *file, int from_tty,
1167 struct cmd_list_element *c, const char *value)
1169 fprintf_filtered (file, _("\
1170 Printing of thread events is %s.\n"),
1175 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1178 struct thread_info *tp;
1180 num = value_as_long (parse_and_eval (tidstr));
1182 tp = find_thread_id (num);
1185 error (_("Thread ID %d not known."), num);
1187 if (!thread_alive (tp))
1188 error (_("Thread ID %d has terminated."), num);
1191 context_switch_to (tp->ptid);
1193 switch_to_thread (tp->ptid);
1195 ui_out_text (uiout, "[Switching to thread ");
1196 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1197 ui_out_text (uiout, " (");
1198 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
1199 ui_out_text (uiout, ")]");
1201 /* Note that we can't reach this with an exited thread, due to the
1202 thread_alive check above. */
1203 if (tp->state_ == THREAD_RUNNING)
1204 ui_out_text (uiout, "(running)\n");
1206 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1208 /* Since the current thread may have changed, see if there is any
1209 exited thread we can now delete. */
1216 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1218 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1219 error_message, RETURN_MASK_ALL) < 0)
1224 /* Commands with a prefix of `thread'. */
1225 struct cmd_list_element *thread_cmd_list = NULL;
1228 _initialize_thread (void)
1230 static struct cmd_list_element *thread_apply_list = NULL;
1231 struct cmd_list_element *c;
1233 c = add_info ("threads", info_threads_command,
1234 _("IDs of currently known threads."));
1235 set_cmd_async_ok (c);
1236 set_cmd_no_selected_thread_ok (c);
1238 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1239 Use this command to switch between threads.\n\
1240 The new thread ID must be currently known."),
1241 &thread_cmd_list, "thread ", 1, &cmdlist);
1242 set_cmd_async_ok (c);
1243 set_cmd_no_selected_thread_ok (c);
1245 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1246 _("Apply a command to a list of threads."),
1247 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1248 set_cmd_async_ok (c);
1249 set_cmd_no_selected_thread_ok (c);
1251 c = add_cmd ("all", class_run, thread_apply_all_command,
1252 _("Apply a command to all threads."), &thread_apply_list);
1253 set_cmd_async_ok (c);
1254 set_cmd_no_selected_thread_ok (c);
1257 add_com_alias ("t", "thread", class_run, 1);
1259 add_setshow_boolean_cmd ("thread-events", no_class,
1260 &print_thread_events, _("\
1261 Set printing of thread events (such as thread start and exit)."), _("\
1262 Show printing of thread events (such as thread start and exit)."), NULL,
1264 show_print_thread_events,
1265 &setprintlist, &showprintlist);