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 void thread_command (char *tidstr
, int from_tty
);
58 static void thread_apply_all_command (char *, int);
59 static int thread_alive (struct thread_info
*);
60 static void info_threads_command (char *, int);
61 static void thread_apply_command (char *, int);
62 static void restore_current_thread (ptid_t
);
63 static void prune_threads (void);
65 /* Frontend view of the thread state. Possible extensions: stepping,
66 finishing, until(ling),... */
74 static enum thread_state main_thread_state
= THREAD_STOPPED
;
75 static int main_thread_executing
= 0;
77 extern struct thread_info
*
78 inferior_thread (void)
80 struct thread_info
*tp
= find_thread_pid (inferior_ptid
);
86 delete_step_resume_breakpoint (struct thread_info
*tp
)
88 if (tp
&& tp
->step_resume_breakpoint
)
90 delete_breakpoint (tp
->step_resume_breakpoint
);
91 tp
->step_resume_breakpoint
= NULL
;
96 clear_thread_inferior_resources (struct thread_info
*tp
)
98 /* NOTE: this will take care of any left-over step_resume breakpoints,
99 but not any user-specified thread-specific breakpoints. We can not
100 delete the breakpoint straight-off, because the inferior might not
101 be stopped at the moment. */
102 if (tp
->step_resume_breakpoint
)
104 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
105 tp
->step_resume_breakpoint
= NULL
;
108 bpstat_clear (&tp
->stop_bpstat
);
110 discard_all_intermediate_continuations_thread (tp
);
111 discard_all_continuations_thread (tp
);
115 free_thread (struct thread_info
*tp
)
117 clear_thread_inferior_resources (tp
);
119 /* FIXME: do I ever need to call the back-end to give it a
120 chance at this private data before deleting the thread? */
128 init_thread_list (void)
130 struct thread_info
*tp
, *tpnext
;
132 highest_thread_num
= 0;
133 main_thread_state
= THREAD_STOPPED
;
134 main_thread_executing
= 0;
139 for (tp
= thread_list
; tp
; tp
= tpnext
)
149 add_thread_silent (ptid_t ptid
)
151 struct thread_info
*tp
;
153 tp
= find_thread_pid (ptid
);
155 /* Found an old thread with the same id. It has to be dead,
156 otherwise we wouldn't be adding a new thread with the same id.
157 The OS is reusing this id --- delete it, and recreate a new
160 /* In addition to deleting the thread, if this is the current
161 thread, then we need to also get rid of the current infrun
162 context, and take care that delete_thread doesn't really
163 delete the thread if it is inferior_ptid. Create a new
164 template thread in the list with an invalid ptid, context
165 switch to it, delete the original thread, reset the new
166 thread's ptid, and switch to it. */
168 if (ptid_equal (inferior_ptid
, ptid
))
170 tp
= xmalloc (sizeof (*tp
));
171 memset (tp
, 0, sizeof (*tp
));
172 tp
->ptid
= minus_one_ptid
;
173 tp
->num
= ++highest_thread_num
;
174 tp
->next
= thread_list
;
176 context_switch_to (minus_one_ptid
);
178 /* Now we can delete it. */
179 delete_thread (ptid
);
181 /* Since the context is already set to this new thread,
182 reset its ptid, and reswitch inferior_ptid to it. */
184 switch_to_thread (ptid
);
186 observer_notify_new_thread (tp
);
192 /* Just go ahead and delete it. */
193 delete_thread (ptid
);
196 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
197 memset (tp
, 0, sizeof (*tp
));
199 tp
->num
= ++highest_thread_num
;
200 tp
->next
= thread_list
;
203 observer_notify_new_thread (tp
);
209 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
211 struct thread_info
*result
= add_thread_silent (ptid
);
213 result
->private = private;
215 if (print_thread_events
)
216 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
218 annotate_new_thread ();
223 add_thread (ptid_t ptid
)
225 return add_thread_with_info (ptid
, NULL
);
228 /* Delete thread PTID. If SILENT, don't notify the observer of this
231 delete_thread_1 (ptid_t ptid
, int silent
)
233 struct thread_info
*tp
, *tpprev
;
237 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
238 if (ptid_equal (tp
->ptid
, ptid
))
244 /* If this is the current thread, or there's code out there that
245 relies on it existing (refcount > 0) we can't delete yet. Mark
246 it as exited, and notify it. */
248 || ptid_equal (tp
->ptid
, inferior_ptid
))
250 if (tp
->state_
!= THREAD_EXITED
)
253 observer_notify_thread_exit (tp
);
255 /* Tag it as exited. */
256 tp
->state_
= THREAD_EXITED
;
258 /* Clear breakpoints, etc. associated with this thread. */
259 clear_thread_inferior_resources (tp
);
262 /* Will be really deleted some other time. */
267 tpprev
->next
= tp
->next
;
269 thread_list
= tp
->next
;
271 /* Notify thread exit, but only if we haven't already. */
272 if (!silent
&& tp
->state_
!= THREAD_EXITED
)
273 observer_notify_thread_exit (tp
);
278 /* Delete thread PTID and notify of thread exit. If this is
279 inferior_ptid, don't actually delete it, but tag it as exited and
280 do the notification. If PTID is the user selected thread, clear
283 delete_thread (ptid_t ptid
)
285 delete_thread_1 (ptid
, 0 /* not silent */);
289 delete_thread_silent (ptid_t ptid
)
291 delete_thread_1 (ptid
, 1 /* silent */);
295 find_thread_id (int num
)
297 struct thread_info
*tp
;
299 for (tp
= thread_list
; tp
; tp
= tp
->next
)
306 /* Find a thread_info by matching PTID. */
308 find_thread_pid (ptid_t ptid
)
310 struct thread_info
*tp
;
312 for (tp
= thread_list
; tp
; tp
= tp
->next
)
313 if (ptid_equal (tp
->ptid
, ptid
))
320 * Thread iterator function.
322 * Calls a callback function once for each thread, so long as
323 * the callback function returns false. If the callback function
324 * returns true, the iteration will end and the current thread
325 * will be returned. This can be useful for implementing a
326 * search for a thread with arbitrary attributes, or for applying
327 * some operation to every thread.
329 * FIXME: some of the existing functionality, such as
330 * "Thread apply all", might be rewritten using this functionality.
334 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
337 struct thread_info
*tp
, *next
;
339 for (tp
= thread_list
; tp
; tp
= next
)
342 if ((*callback
) (tp
, data
))
353 struct thread_info
*tp
;
355 for (tp
= thread_list
; tp
; tp
= tp
->next
)
362 valid_thread_id (int num
)
364 struct thread_info
*tp
;
366 for (tp
= thread_list
; tp
; tp
= tp
->next
)
374 pid_to_thread_id (ptid_t ptid
)
376 struct thread_info
*tp
;
378 for (tp
= thread_list
; tp
; tp
= tp
->next
)
379 if (ptid_equal (tp
->ptid
, ptid
))
386 thread_id_to_pid (int num
)
388 struct thread_info
*thread
= find_thread_id (num
);
392 return pid_to_ptid (-1);
396 in_thread_list (ptid_t ptid
)
398 struct thread_info
*tp
;
400 for (tp
= thread_list
; tp
; tp
= tp
->next
)
401 if (ptid_equal (tp
->ptid
, ptid
))
404 return 0; /* Never heard of 'im */
407 /* Print a list of thread ids currently known, and the total number of
408 threads. To be used from within catch_errors. */
410 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
412 struct thread_info
*tp
;
414 struct cleanup
*cleanup_chain
;
417 target_find_new_threads ();
419 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
421 for (tp
= thread_list
; tp
; tp
= tp
->next
)
423 if (tp
->state_
== THREAD_EXITED
)
426 ui_out_field_int (uiout
, "thread-id", tp
->num
);
429 do_cleanups (cleanup_chain
);
430 ui_out_field_int (uiout
, "number-of-threads", num
);
434 /* Official gdblib interface function to get a list of thread ids and
437 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
439 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
440 error_message
, RETURN_MASK_ALL
) < 0)
445 /* Load infrun state for the thread PID. */
448 load_infrun_state (ptid_t ptid
)
450 struct thread_info
*tp
;
452 /* If we can't find the thread, then we're debugging a single threaded
453 process. No need to do anything in that case. */
454 tp
= find_thread_id (pid_to_thread_id (ptid
));
459 /* Save infrun state for the thread PID. */
462 save_infrun_state (ptid_t ptid
)
464 struct thread_info
*tp
;
466 /* If we can't find the thread, then we're debugging a single-threaded
467 process. Nothing to do in that case. */
468 tp
= find_thread_id (pid_to_thread_id (ptid
));
473 /* Return true if TP is an active thread. */
475 thread_alive (struct thread_info
*tp
)
477 if (tp
->state_
== THREAD_EXITED
)
479 if (!target_thread_alive (tp
->ptid
))
487 struct thread_info
*tp
, *next
;
489 for (tp
= thread_list
; tp
; tp
= next
)
492 if (!thread_alive (tp
))
493 delete_thread (tp
->ptid
);
498 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
500 struct thread_info
* tp
= find_thread_pid (old_ptid
);
503 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
507 set_running (ptid_t ptid
, int running
)
509 struct thread_info
*tp
;
513 /* This is one of the targets that does not add main
514 thread to the thread list. Just use a single
515 global flag to indicate that a thread is running.
517 This problem is unique to ST programs. For MT programs,
518 the main thread is always present in the thread list. If it's
519 not, the first call to context_switch will mess up GDB internal
522 && main_thread_state
!= THREAD_RUNNING
523 && !suppress_resume_observer
)
524 observer_notify_target_resumed (ptid
);
525 main_thread_state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
529 /* We try not to notify the observer if no thread has actually changed
530 the running state -- merely to reduce the number of messages to
531 frontend. Frontend is supposed to handle multiple *running just fine. */
532 if (PIDGET (ptid
) == -1)
535 for (tp
= thread_list
; tp
; tp
= tp
->next
)
537 if (tp
->state_
== THREAD_EXITED
)
539 if (running
&& tp
->state_
== THREAD_STOPPED
)
541 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
543 if (any_started
&& !suppress_resume_observer
)
544 observer_notify_target_resumed (ptid
);
549 tp
= find_thread_pid (ptid
);
551 gdb_assert (tp
->state_
!= THREAD_EXITED
);
552 if (running
&& tp
->state_
== THREAD_STOPPED
)
554 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
555 if (started
&& !suppress_resume_observer
)
556 observer_notify_target_resumed (ptid
);
561 is_thread_state (ptid_t ptid
, enum thread_state state
)
563 struct thread_info
*tp
;
565 if (!target_has_execution
)
569 return main_thread_state
== state
;
571 tp
= find_thread_pid (ptid
);
573 return tp
->state_
== state
;
577 is_stopped (ptid_t ptid
)
579 /* Without execution, this property is always true. */
580 if (!target_has_execution
)
583 return is_thread_state (ptid
, THREAD_STOPPED
);
587 is_exited (ptid_t ptid
)
589 /* Without execution, this property is always false. */
590 if (!target_has_execution
)
593 return is_thread_state (ptid
, THREAD_EXITED
);
597 is_running (ptid_t ptid
)
599 /* Without execution, this property is always false. */
600 if (!target_has_execution
)
603 return is_thread_state (ptid
, THREAD_RUNNING
);
609 struct thread_info
*tp
;
611 if (!target_has_execution
)
615 return main_thread_state
== THREAD_RUNNING
;
617 for (tp
= thread_list
; tp
; tp
= tp
->next
)
618 if (tp
->state_
== THREAD_RUNNING
)
625 is_executing (ptid_t ptid
)
627 struct thread_info
*tp
;
629 if (!target_has_execution
)
633 return main_thread_executing
;
635 tp
= find_thread_pid (ptid
);
637 return tp
->executing_
;
641 set_executing (ptid_t ptid
, int executing
)
643 struct thread_info
*tp
;
647 /* This target does not add the main thread to the thread list.
648 Use a global flag to indicate that the thread is
650 main_thread_executing
= executing
;
654 if (PIDGET (ptid
) == -1)
656 for (tp
= thread_list
; tp
; tp
= tp
->next
)
657 tp
->executing_
= executing
;
661 tp
= find_thread_pid (ptid
);
663 tp
->executing_
= executing
;
667 /* Prints the list of threads and their details on UIOUT.
668 This is a version of 'info_thread_command' suitable for
670 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
671 that should be printed. Otherwise, all threads are
674 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
676 struct thread_info
*tp
;
678 struct cleanup
*old_chain
;
680 int current_thread
= -1;
683 target_find_new_threads ();
684 current_ptid
= inferior_ptid
;
686 /* We'll be switching threads temporarily. */
687 old_chain
= make_cleanup_restore_current_thread ();
689 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
690 for (tp
= thread_list
; tp
; tp
= tp
->next
)
692 struct cleanup
*chain2
;
694 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
697 if (ptid_equal (tp
->ptid
, current_ptid
))
698 current_thread
= tp
->num
;
700 if (tp
->state_
== THREAD_EXITED
)
703 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
705 if (ptid_equal (tp
->ptid
, current_ptid
))
706 ui_out_text (uiout
, "* ");
708 ui_out_text (uiout
, " ");
710 ui_out_field_int (uiout
, "id", tp
->num
);
711 ui_out_text (uiout
, " ");
712 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
714 if (tp
->state_
!= THREAD_EXITED
)
716 extra_info
= target_extra_thread_info (tp
);
719 ui_out_text (uiout
, " (");
720 ui_out_field_string (uiout
, "details", extra_info
);
721 ui_out_text (uiout
, ")");
723 ui_out_text (uiout
, " ");
726 if (tp
->state_
== THREAD_RUNNING
)
727 ui_out_text (uiout
, "(running)\n");
730 /* The switch below puts us at the top of the stack (leaf
732 switch_to_thread (tp
->ptid
);
733 print_stack_frame (get_selected_frame (NULL
),
734 /* For MI output, print frame level. */
735 ui_out_is_mi_like_p (uiout
),
739 if (ui_out_is_mi_like_p (uiout
))
741 char *state
= "stopped";
742 if (tp
->state_
== THREAD_EXITED
)
744 else if (tp
->state_
== THREAD_RUNNING
)
746 ui_out_field_string (uiout
, "state", state
);
749 do_cleanups (chain2
);
752 /* Restores the current thread and the frame selected before
753 the "info threads" command. */
754 do_cleanups (old_chain
);
756 if (requested_thread
== -1)
758 gdb_assert (current_thread
!= -1
760 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
761 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
763 if (current_thread
!= -1 && is_exited (current_ptid
))
764 ui_out_message (uiout
, 0, "\n\
765 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
771 /* Print information about currently known threads
773 * Note: this has the drawback that it _really_ switches
774 * threads, which frees the frame cache. A no-side
775 * effects info-threads command would be nicer.
779 info_threads_command (char *arg
, int from_tty
)
781 print_thread_info (uiout
, -1);
784 /* Switch from one thread to another. */
787 switch_to_thread (ptid_t ptid
)
789 if (ptid_equal (ptid
, inferior_ptid
))
792 inferior_ptid
= ptid
;
793 reinit_frame_cache ();
794 registers_changed ();
796 /* We don't check for is_stopped, because we're called at times
797 while in the TARGET_RUNNING state, e.g., while handling an
799 if (!is_exited (ptid
) && !is_executing (ptid
))
800 stop_pc
= read_pc ();
802 stop_pc
= ~(CORE_ADDR
) 0;
806 restore_current_thread (ptid_t ptid
)
808 if (!ptid_equal (ptid
, inferior_ptid
))
811 context_switch_to (ptid
);
813 switch_to_thread (ptid
);
818 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
820 struct frame_info
*frame
= NULL
;
823 gdb_assert (frame_level
>= 0);
825 /* Restore by level first, check if the frame id is the same as
826 expected. If that fails, try restoring by frame id. If that
827 fails, nothing to do, just warn the user. */
830 frame
= find_relative_frame (get_current_frame (), &count
);
833 /* Either the frame ids match, of they're both invalid. The
834 latter case is not failsafe, but since it's highly unlikely
835 the search by level finds the wrong frame, it's 99.9(9)% of
836 the time (for all practical purposes) safe. */
837 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
838 /* Note: could be better to check every frame_id
839 member for equality here. */
840 || (!frame_id_p (get_frame_id (frame
))
841 && !frame_id_p (a_frame_id
))))
843 /* Cool, all is fine. */
844 select_frame (frame
);
848 frame
= frame_find_by_id (a_frame_id
);
851 /* Cool, refound it. */
852 select_frame (frame
);
856 /* Nothing else to do, the frame layout really changed. Select the
857 innermost stack frame. */
858 select_frame (get_current_frame ());
861 if (!ui_out_is_mi_like_p (uiout
))
864 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
866 /* For MI, we should probably have a notification about
867 current frame change. But this error is not very
868 likely, so don't bother for now. */
869 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
873 struct current_thread_cleanup
875 ptid_t inferior_ptid
;
876 struct frame_id selected_frame_id
;
877 int selected_frame_level
;
882 do_restore_current_thread_cleanup (void *arg
)
884 struct thread_info
*tp
;
885 struct current_thread_cleanup
*old
= arg
;
886 restore_current_thread (old
->inferior_ptid
);
888 /* The running state of the originally selected thread may have
889 changed, so we have to recheck it here. */
891 && is_stopped (inferior_ptid
)
892 && target_has_registers
894 && target_has_memory
)
895 restore_selected_frame (old
->selected_frame_id
,
896 old
->selected_frame_level
);
900 restore_current_thread_cleanup_dtor (void *arg
)
902 struct current_thread_cleanup
*old
= arg
;
903 struct thread_info
*tp
;
904 tp
= find_thread_pid (old
->inferior_ptid
);
911 make_cleanup_restore_current_thread (void)
913 struct thread_info
*tp
;
914 struct frame_info
*frame
;
915 struct current_thread_cleanup
*old
;
917 old
= xmalloc (sizeof (struct current_thread_cleanup
));
918 old
->inferior_ptid
= inferior_ptid
;
919 old
->was_stopped
= is_stopped (inferior_ptid
);
921 && target_has_registers
923 && target_has_memory
)
924 frame
= get_selected_frame (NULL
);
928 old
->selected_frame_id
= get_frame_id (frame
);
929 old
->selected_frame_level
= frame_relative_level (frame
);
931 tp
= find_thread_pid (inferior_ptid
);
935 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
936 restore_current_thread_cleanup_dtor
);
939 /* Apply a GDB command to a list of threads. List syntax is a whitespace
940 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
941 of two numbers seperated by a hyphen. Examples:
943 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
944 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
945 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
949 thread_apply_all_command (char *cmd
, int from_tty
)
951 struct thread_info
*tp
;
952 struct cleanup
*old_chain
;
955 if (cmd
== NULL
|| *cmd
== '\000')
956 error (_("Please specify a command following the thread ID list"));
959 target_find_new_threads ();
961 old_chain
= make_cleanup_restore_current_thread ();
963 /* Save a copy of the command in case it is clobbered by
965 saved_cmd
= xstrdup (cmd
);
966 make_cleanup (xfree
, saved_cmd
);
967 for (tp
= thread_list
; tp
; tp
= tp
->next
)
968 if (thread_alive (tp
))
971 context_switch_to (tp
->ptid
);
973 switch_to_thread (tp
->ptid
);
975 printf_filtered (_("\nThread %d (%s):\n"),
976 tp
->num
, target_tid_to_str (inferior_ptid
));
977 execute_command (cmd
, from_tty
);
978 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
981 do_cleanups (old_chain
);
985 thread_apply_command (char *tidlist
, int from_tty
)
989 struct cleanup
*old_chain
;
992 if (tidlist
== NULL
|| *tidlist
== '\000')
993 error (_("Please specify a thread ID list"));
995 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
998 error (_("Please specify a command following the thread ID list"));
1000 /* Save a copy of the command in case it is clobbered by
1002 saved_cmd
= xstrdup (cmd
);
1003 old_chain
= make_cleanup (xfree
, saved_cmd
);
1004 while (tidlist
< cmd
)
1006 struct thread_info
*tp
;
1009 start
= strtol (tidlist
, &p
, 10);
1011 error (_("Error parsing %s"), tidlist
);
1014 while (*tidlist
== ' ' || *tidlist
== '\t')
1017 if (*tidlist
== '-') /* Got a range of IDs? */
1019 tidlist
++; /* Skip the - */
1020 end
= strtol (tidlist
, &p
, 10);
1022 error (_("Error parsing %s"), tidlist
);
1025 while (*tidlist
== ' ' || *tidlist
== '\t')
1031 make_cleanup_restore_current_thread ();
1033 for (; start
<= end
; start
++)
1035 tp
= find_thread_id (start
);
1038 warning (_("Unknown thread %d."), start
);
1039 else if (!thread_alive (tp
))
1040 warning (_("Thread %d has terminated."), start
);
1044 context_switch_to (tp
->ptid
);
1046 switch_to_thread (tp
->ptid
);
1048 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1049 target_tid_to_str (inferior_ptid
));
1050 execute_command (cmd
, from_tty
);
1052 /* Restore exact command used previously. */
1053 strcpy (cmd
, saved_cmd
);
1058 do_cleanups (old_chain
);
1061 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1062 if prefix of arg is `apply'. */
1065 thread_command (char *tidstr
, int from_tty
)
1069 if (target_has_stack
)
1071 if (is_exited (inferior_ptid
))
1072 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1073 pid_to_thread_id (inferior_ptid
),
1074 target_tid_to_str (inferior_ptid
));
1076 printf_filtered (_("[Current thread is %d (%s)]\n"),
1077 pid_to_thread_id (inferior_ptid
),
1078 target_tid_to_str (inferior_ptid
));
1081 error (_("No stack."));
1085 annotate_thread_changed ();
1086 gdb_thread_select (uiout
, tidstr
, NULL
);
1089 /* Print notices when new threads are attached and detached. */
1090 int print_thread_events
= 1;
1092 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1093 struct cmd_list_element
*c
, const char *value
)
1095 fprintf_filtered (file
, _("\
1096 Printing of thread events is %s.\n"),
1101 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1104 struct thread_info
*tp
;
1106 num
= value_as_long (parse_and_eval (tidstr
));
1108 tp
= find_thread_id (num
);
1111 error (_("Thread ID %d not known."), num
);
1113 if (!thread_alive (tp
))
1114 error (_("Thread ID %d has terminated."), num
);
1117 context_switch_to (tp
->ptid
);
1119 switch_to_thread (tp
->ptid
);
1121 ui_out_text (uiout
, "[Switching to thread ");
1122 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1123 ui_out_text (uiout
, " (");
1124 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
1125 ui_out_text (uiout
, ")]");
1127 /* Note that we can't reach this with an exited thread, due to the
1128 thread_alive check above. */
1129 if (tp
->state_
== THREAD_RUNNING
)
1130 ui_out_text (uiout
, "(running)\n");
1132 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1134 /* Since the current thread may have changed, see if there is any
1135 exited thread we can now delete. */
1142 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1144 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1145 error_message
, RETURN_MASK_ALL
) < 0)
1150 /* Commands with a prefix of `thread'. */
1151 struct cmd_list_element
*thread_cmd_list
= NULL
;
1154 _initialize_thread (void)
1156 static struct cmd_list_element
*thread_apply_list
= NULL
;
1157 struct cmd_list_element
*c
;
1159 c
= add_info ("threads", info_threads_command
,
1160 _("IDs of currently known threads."));
1161 set_cmd_no_selected_thread_ok (c
);
1163 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1164 Use this command to switch between threads.\n\
1165 The new thread ID must be currently known."),
1166 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1167 set_cmd_no_selected_thread_ok (c
);
1169 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1170 _("Apply a command to a list of threads."),
1171 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1172 set_cmd_no_selected_thread_ok (c
);
1174 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
1175 _("Apply a command to all threads."), &thread_apply_list
);
1176 set_cmd_no_selected_thread_ok (c
);
1179 add_com_alias ("t", "thread", class_run
, 1);
1181 add_setshow_boolean_cmd ("thread-events", no_class
,
1182 &print_thread_events
, _("\
1183 Set printing of thread events (such as thread start and exit)."), _("\
1184 Show printing of thread events (such as thread start and exit)."), NULL
,
1186 show_print_thread_events
,
1187 &setprintlist
, &showprintlist
);