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 extern struct thread_info
*
75 inferior_thread (void)
77 struct thread_info
*tp
= find_thread_pid (inferior_ptid
);
83 delete_step_resume_breakpoint (struct thread_info
*tp
)
85 if (tp
&& tp
->step_resume_breakpoint
)
87 delete_breakpoint (tp
->step_resume_breakpoint
);
88 tp
->step_resume_breakpoint
= NULL
;
93 clear_thread_inferior_resources (struct thread_info
*tp
)
95 /* NOTE: this will take care of any left-over step_resume breakpoints,
96 but not any user-specified thread-specific breakpoints. We can not
97 delete the breakpoint straight-off, because the inferior might not
98 be stopped at the moment. */
99 if (tp
->step_resume_breakpoint
)
101 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
102 tp
->step_resume_breakpoint
= NULL
;
105 bpstat_clear (&tp
->stop_bpstat
);
107 discard_all_intermediate_continuations_thread (tp
);
108 discard_all_continuations_thread (tp
);
112 free_thread (struct thread_info
*tp
)
114 clear_thread_inferior_resources (tp
);
116 /* FIXME: do I ever need to call the back-end to give it a
117 chance at this private data before deleting the thread? */
125 init_thread_list (void)
127 struct thread_info
*tp
, *tpnext
;
129 highest_thread_num
= 0;
134 for (tp
= thread_list
; tp
; tp
= tpnext
)
144 add_thread_silent (ptid_t ptid
)
146 struct thread_info
*tp
;
148 tp
= find_thread_pid (ptid
);
150 /* Found an old thread with the same id. It has to be dead,
151 otherwise we wouldn't be adding a new thread with the same id.
152 The OS is reusing this id --- delete it, and recreate a new
155 /* In addition to deleting the thread, if this is the current
156 thread, then we need to take care that delete_thread doesn't
157 really delete the thread if it is inferior_ptid. Create a
158 new template thread in the list with an invalid ptid, switch
159 to it, delete the original thread, reset the new thread's
160 ptid, and switch to it. */
162 if (ptid_equal (inferior_ptid
, ptid
))
164 tp
= xmalloc (sizeof (*tp
));
165 memset (tp
, 0, sizeof (*tp
));
166 tp
->ptid
= minus_one_ptid
;
167 tp
->num
= ++highest_thread_num
;
168 tp
->next
= thread_list
;
171 /* Make switch_to_thread not read from the thread. */
172 tp
->state_
= THREAD_EXITED
;
173 switch_to_thread (minus_one_ptid
);
175 /* Now we can delete it. */
176 delete_thread (ptid
);
178 /* Now reset its ptid, and reswitch inferior_ptid to it. */
180 tp
->state_
= THREAD_STOPPED
;
181 switch_to_thread (ptid
);
183 observer_notify_new_thread (tp
);
189 /* Just go ahead and delete it. */
190 delete_thread (ptid
);
193 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
194 memset (tp
, 0, sizeof (*tp
));
196 tp
->num
= ++highest_thread_num
;
197 tp
->next
= thread_list
;
200 observer_notify_new_thread (tp
);
206 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
208 struct thread_info
*result
= add_thread_silent (ptid
);
210 result
->private = private;
212 if (print_thread_events
)
213 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
215 annotate_new_thread ();
220 add_thread (ptid_t ptid
)
222 return add_thread_with_info (ptid
, NULL
);
225 /* Delete thread PTID. If SILENT, don't notify the observer of this
228 delete_thread_1 (ptid_t ptid
, int silent
)
230 struct thread_info
*tp
, *tpprev
;
234 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
235 if (ptid_equal (tp
->ptid
, ptid
))
241 /* If this is the current thread, or there's code out there that
242 relies on it existing (refcount > 0) we can't delete yet. Mark
243 it as exited, and notify it. */
245 || ptid_equal (tp
->ptid
, inferior_ptid
))
247 if (tp
->state_
!= THREAD_EXITED
)
250 observer_notify_thread_exit (tp
);
252 /* Tag it as exited. */
253 tp
->state_
= THREAD_EXITED
;
255 /* Clear breakpoints, etc. associated with this thread. */
256 clear_thread_inferior_resources (tp
);
259 /* Will be really deleted some other time. */
264 tpprev
->next
= tp
->next
;
266 thread_list
= tp
->next
;
268 /* Notify thread exit, but only if we haven't already. */
269 if (!silent
&& tp
->state_
!= THREAD_EXITED
)
270 observer_notify_thread_exit (tp
);
275 /* Delete thread PTID and notify of thread exit. If this is
276 inferior_ptid, don't actually delete it, but tag it as exited and
277 do the notification. If PTID is the user selected thread, clear
280 delete_thread (ptid_t ptid
)
282 delete_thread_1 (ptid
, 0 /* not silent */);
286 delete_thread_silent (ptid_t ptid
)
288 delete_thread_1 (ptid
, 1 /* silent */);
292 find_thread_id (int num
)
294 struct thread_info
*tp
;
296 for (tp
= thread_list
; tp
; tp
= tp
->next
)
303 /* Find a thread_info by matching PTID. */
305 find_thread_pid (ptid_t ptid
)
307 struct thread_info
*tp
;
309 for (tp
= thread_list
; tp
; tp
= tp
->next
)
310 if (ptid_equal (tp
->ptid
, ptid
))
317 * Thread iterator function.
319 * Calls a callback function once for each thread, so long as
320 * the callback function returns false. If the callback function
321 * returns true, the iteration will end and the current thread
322 * will be returned. This can be useful for implementing a
323 * search for a thread with arbitrary attributes, or for applying
324 * some operation to every thread.
326 * FIXME: some of the existing functionality, such as
327 * "Thread apply all", might be rewritten using this functionality.
331 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
334 struct thread_info
*tp
, *next
;
336 for (tp
= thread_list
; tp
; tp
= next
)
339 if ((*callback
) (tp
, data
))
350 struct thread_info
*tp
;
352 for (tp
= thread_list
; tp
; tp
= tp
->next
)
359 valid_thread_id (int num
)
361 struct thread_info
*tp
;
363 for (tp
= thread_list
; tp
; tp
= tp
->next
)
371 pid_to_thread_id (ptid_t ptid
)
373 struct thread_info
*tp
;
375 for (tp
= thread_list
; tp
; tp
= tp
->next
)
376 if (ptid_equal (tp
->ptid
, ptid
))
383 thread_id_to_pid (int num
)
385 struct thread_info
*thread
= find_thread_id (num
);
389 return pid_to_ptid (-1);
393 in_thread_list (ptid_t ptid
)
395 struct thread_info
*tp
;
397 for (tp
= thread_list
; tp
; tp
= tp
->next
)
398 if (ptid_equal (tp
->ptid
, ptid
))
401 return 0; /* Never heard of 'im */
404 /* Print a list of thread ids currently known, and the total number of
405 threads. To be used from within catch_errors. */
407 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
409 struct thread_info
*tp
;
411 struct cleanup
*cleanup_chain
;
414 target_find_new_threads ();
416 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
418 for (tp
= thread_list
; tp
; tp
= tp
->next
)
420 if (tp
->state_
== THREAD_EXITED
)
423 ui_out_field_int (uiout
, "thread-id", tp
->num
);
426 do_cleanups (cleanup_chain
);
427 ui_out_field_int (uiout
, "number-of-threads", num
);
431 /* Official gdblib interface function to get a list of thread ids and
434 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
436 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
437 error_message
, RETURN_MASK_ALL
) < 0)
442 /* Return true if TP is an active thread. */
444 thread_alive (struct thread_info
*tp
)
446 if (tp
->state_
== THREAD_EXITED
)
448 if (!target_thread_alive (tp
->ptid
))
456 struct thread_info
*tp
, *next
;
458 for (tp
= thread_list
; tp
; tp
= next
)
461 if (!thread_alive (tp
))
462 delete_thread (tp
->ptid
);
467 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
469 struct inferior
*inf
;
470 struct thread_info
*tp
;
472 /* It can happen that what we knew as the target inferior id
473 changes. E.g, target remote may only discover the remote process
474 pid after adding the inferior to GDB's list. */
475 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
476 inf
->pid
= ptid_get_pid (new_ptid
);
478 tp
= find_thread_pid (old_ptid
);
481 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
485 set_running (ptid_t ptid
, int running
)
487 struct thread_info
*tp
;
489 /* We try not to notify the observer if no thread has actually changed
490 the running state -- merely to reduce the number of messages to
491 frontend. Frontend is supposed to handle multiple *running just fine. */
492 if (PIDGET (ptid
) == -1)
495 for (tp
= thread_list
; tp
; tp
= tp
->next
)
497 if (tp
->state_
== THREAD_EXITED
)
499 if (running
&& tp
->state_
== THREAD_STOPPED
)
501 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
503 if (any_started
&& !suppress_resume_observer
)
504 observer_notify_target_resumed (ptid
);
509 tp
= find_thread_pid (ptid
);
511 gdb_assert (tp
->state_
!= THREAD_EXITED
);
512 if (running
&& tp
->state_
== THREAD_STOPPED
)
514 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
515 if (started
&& !suppress_resume_observer
)
516 observer_notify_target_resumed (ptid
);
521 is_thread_state (ptid_t ptid
, enum thread_state state
)
523 struct thread_info
*tp
;
525 if (!target_has_execution
)
528 tp
= find_thread_pid (ptid
);
530 return tp
->state_
== state
;
534 is_stopped (ptid_t ptid
)
536 /* Without execution, this property is always true. */
537 if (!target_has_execution
)
540 return is_thread_state (ptid
, THREAD_STOPPED
);
544 is_exited (ptid_t ptid
)
546 /* Without execution, this property is always false. */
547 if (!target_has_execution
)
550 return is_thread_state (ptid
, THREAD_EXITED
);
554 is_running (ptid_t ptid
)
556 /* Without execution, this property is always false. */
557 if (!target_has_execution
)
560 return is_thread_state (ptid
, THREAD_RUNNING
);
566 struct thread_info
*tp
;
568 if (!target_has_execution
)
571 for (tp
= thread_list
; tp
; tp
= tp
->next
)
572 if (tp
->state_
== THREAD_RUNNING
)
579 is_executing (ptid_t ptid
)
581 struct thread_info
*tp
;
583 if (!target_has_execution
)
586 tp
= find_thread_pid (ptid
);
588 return tp
->executing_
;
592 set_executing (ptid_t ptid
, int executing
)
594 struct thread_info
*tp
;
596 if (PIDGET (ptid
) == -1)
598 for (tp
= thread_list
; tp
; tp
= tp
->next
)
599 tp
->executing_
= executing
;
603 tp
= find_thread_pid (ptid
);
605 tp
->executing_
= executing
;
610 set_stop_requested (ptid_t ptid
, int stop
)
612 struct thread_info
*tp
;
613 int all
= ptid_equal (ptid
, minus_one_ptid
);
615 if (all
|| ptid_is_pid (ptid
))
617 for (tp
= thread_list
; tp
; tp
= tp
->next
)
618 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
619 tp
->stop_requested
= stop
;
623 tp
= find_thread_pid (ptid
);
625 tp
->stop_requested
= stop
;
628 /* Call the stop requested observer so other components of GDB can
629 react to this request. */
631 observer_notify_thread_stop_requested (ptid
);
634 /* Prints the list of threads and their details on UIOUT.
635 This is a version of 'info_thread_command' suitable for
637 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
638 that should be printed. Otherwise, all threads are
641 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
643 struct thread_info
*tp
;
645 struct cleanup
*old_chain
;
647 int current_thread
= -1;
650 target_find_new_threads ();
651 current_ptid
= inferior_ptid
;
653 /* We'll be switching threads temporarily. */
654 old_chain
= make_cleanup_restore_current_thread ();
656 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
657 for (tp
= thread_list
; tp
; tp
= tp
->next
)
659 struct cleanup
*chain2
;
661 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
664 if (ptid_equal (tp
->ptid
, current_ptid
))
665 current_thread
= tp
->num
;
667 if (tp
->state_
== THREAD_EXITED
)
670 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
672 if (ptid_equal (tp
->ptid
, current_ptid
))
673 ui_out_text (uiout
, "* ");
675 ui_out_text (uiout
, " ");
677 ui_out_field_int (uiout
, "id", tp
->num
);
678 ui_out_text (uiout
, " ");
679 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
681 extra_info
= target_extra_thread_info (tp
);
684 ui_out_text (uiout
, " (");
685 ui_out_field_string (uiout
, "details", extra_info
);
686 ui_out_text (uiout
, ")");
688 ui_out_text (uiout
, " ");
690 if (tp
->state_
== THREAD_RUNNING
)
691 ui_out_text (uiout
, "(running)\n");
694 /* The switch below puts us at the top of the stack (leaf
696 switch_to_thread (tp
->ptid
);
697 print_stack_frame (get_selected_frame (NULL
),
698 /* For MI output, print frame level. */
699 ui_out_is_mi_like_p (uiout
),
703 if (ui_out_is_mi_like_p (uiout
))
705 char *state
= "stopped";
706 if (tp
->state_
== THREAD_RUNNING
)
708 ui_out_field_string (uiout
, "state", state
);
711 do_cleanups (chain2
);
714 /* Restores the current thread and the frame selected before
715 the "info threads" command. */
716 do_cleanups (old_chain
);
718 if (requested_thread
== -1)
720 gdb_assert (current_thread
!= -1
722 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
723 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
725 if (current_thread
!= -1 && is_exited (current_ptid
))
726 ui_out_message (uiout
, 0, "\n\
727 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
733 /* Print information about currently known threads
735 * Note: this has the drawback that it _really_ switches
736 * threads, which frees the frame cache. A no-side
737 * effects info-threads command would be nicer.
741 info_threads_command (char *arg
, int from_tty
)
743 print_thread_info (uiout
, -1);
746 /* Switch from one thread to another. */
749 switch_to_thread (ptid_t ptid
)
751 if (ptid_equal (ptid
, inferior_ptid
))
754 inferior_ptid
= ptid
;
755 reinit_frame_cache ();
756 registers_changed ();
758 /* We don't check for is_stopped, because we're called at times
759 while in the TARGET_RUNNING state, e.g., while handling an
761 if (!is_exited (ptid
) && !is_executing (ptid
))
762 stop_pc
= read_pc ();
764 stop_pc
= ~(CORE_ADDR
) 0;
768 restore_current_thread (ptid_t ptid
)
770 switch_to_thread (ptid
);
774 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
776 struct frame_info
*frame
= NULL
;
779 gdb_assert (frame_level
>= 0);
781 /* Restore by level first, check if the frame id is the same as
782 expected. If that fails, try restoring by frame id. If that
783 fails, nothing to do, just warn the user. */
786 frame
= find_relative_frame (get_current_frame (), &count
);
789 /* Either the frame ids match, of they're both invalid. The
790 latter case is not failsafe, but since it's highly unlikely
791 the search by level finds the wrong frame, it's 99.9(9)% of
792 the time (for all practical purposes) safe. */
793 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
794 /* Note: could be better to check every frame_id
795 member for equality here. */
796 || (!frame_id_p (get_frame_id (frame
))
797 && !frame_id_p (a_frame_id
))))
799 /* Cool, all is fine. */
800 select_frame (frame
);
804 frame
= frame_find_by_id (a_frame_id
);
807 /* Cool, refound it. */
808 select_frame (frame
);
812 /* Nothing else to do, the frame layout really changed. Select the
813 innermost stack frame. */
814 select_frame (get_current_frame ());
817 if (!ui_out_is_mi_like_p (uiout
))
820 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
822 /* For MI, we should probably have a notification about
823 current frame change. But this error is not very
824 likely, so don't bother for now. */
825 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
829 struct current_thread_cleanup
831 ptid_t inferior_ptid
;
832 struct frame_id selected_frame_id
;
833 int selected_frame_level
;
838 do_restore_current_thread_cleanup (void *arg
)
840 struct thread_info
*tp
;
841 struct current_thread_cleanup
*old
= arg
;
842 restore_current_thread (old
->inferior_ptid
);
844 /* The running state of the originally selected thread may have
845 changed, so we have to recheck it here. */
847 && is_stopped (inferior_ptid
)
848 && target_has_registers
850 && target_has_memory
)
851 restore_selected_frame (old
->selected_frame_id
,
852 old
->selected_frame_level
);
856 restore_current_thread_cleanup_dtor (void *arg
)
858 struct current_thread_cleanup
*old
= arg
;
859 struct thread_info
*tp
;
860 tp
= find_thread_pid (old
->inferior_ptid
);
867 make_cleanup_restore_current_thread (void)
869 struct thread_info
*tp
;
870 struct frame_info
*frame
;
871 struct current_thread_cleanup
*old
;
873 old
= xmalloc (sizeof (struct current_thread_cleanup
));
874 old
->inferior_ptid
= inferior_ptid
;
875 old
->was_stopped
= is_stopped (inferior_ptid
);
877 && target_has_registers
879 && target_has_memory
)
880 frame
= get_selected_frame (NULL
);
884 old
->selected_frame_id
= get_frame_id (frame
);
885 old
->selected_frame_level
= frame_relative_level (frame
);
887 tp
= find_thread_pid (inferior_ptid
);
891 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
892 restore_current_thread_cleanup_dtor
);
895 /* Apply a GDB command to a list of threads. List syntax is a whitespace
896 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
897 of two numbers seperated by a hyphen. Examples:
899 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
900 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
901 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
905 thread_apply_all_command (char *cmd
, int from_tty
)
907 struct thread_info
*tp
;
908 struct cleanup
*old_chain
;
911 if (cmd
== NULL
|| *cmd
== '\000')
912 error (_("Please specify a command following the thread ID list"));
915 target_find_new_threads ();
917 old_chain
= make_cleanup_restore_current_thread ();
919 /* Save a copy of the command in case it is clobbered by
921 saved_cmd
= xstrdup (cmd
);
922 make_cleanup (xfree
, saved_cmd
);
923 for (tp
= thread_list
; tp
; tp
= tp
->next
)
924 if (thread_alive (tp
))
926 switch_to_thread (tp
->ptid
);
928 printf_filtered (_("\nThread %d (%s):\n"),
929 tp
->num
, target_tid_to_str (inferior_ptid
));
930 execute_command (cmd
, from_tty
);
931 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
934 do_cleanups (old_chain
);
938 thread_apply_command (char *tidlist
, int from_tty
)
942 struct cleanup
*old_chain
;
945 if (tidlist
== NULL
|| *tidlist
== '\000')
946 error (_("Please specify a thread ID list"));
948 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
951 error (_("Please specify a command following the thread ID list"));
953 /* Save a copy of the command in case it is clobbered by
955 saved_cmd
= xstrdup (cmd
);
956 old_chain
= make_cleanup (xfree
, saved_cmd
);
957 while (tidlist
< cmd
)
959 struct thread_info
*tp
;
962 start
= strtol (tidlist
, &p
, 10);
964 error (_("Error parsing %s"), tidlist
);
967 while (*tidlist
== ' ' || *tidlist
== '\t')
970 if (*tidlist
== '-') /* Got a range of IDs? */
972 tidlist
++; /* Skip the - */
973 end
= strtol (tidlist
, &p
, 10);
975 error (_("Error parsing %s"), tidlist
);
978 while (*tidlist
== ' ' || *tidlist
== '\t')
984 make_cleanup_restore_current_thread ();
986 for (; start
<= end
; start
++)
988 tp
= find_thread_id (start
);
991 warning (_("Unknown thread %d."), start
);
992 else if (!thread_alive (tp
))
993 warning (_("Thread %d has terminated."), start
);
996 switch_to_thread (tp
->ptid
);
998 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
999 target_tid_to_str (inferior_ptid
));
1000 execute_command (cmd
, from_tty
);
1002 /* Restore exact command used previously. */
1003 strcpy (cmd
, saved_cmd
);
1008 do_cleanups (old_chain
);
1011 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1012 if prefix of arg is `apply'. */
1015 thread_command (char *tidstr
, int from_tty
)
1019 if (target_has_stack
)
1021 if (is_exited (inferior_ptid
))
1022 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1023 pid_to_thread_id (inferior_ptid
),
1024 target_tid_to_str (inferior_ptid
));
1026 printf_filtered (_("[Current thread is %d (%s)]\n"),
1027 pid_to_thread_id (inferior_ptid
),
1028 target_tid_to_str (inferior_ptid
));
1031 error (_("No stack."));
1035 annotate_thread_changed ();
1036 gdb_thread_select (uiout
, tidstr
, NULL
);
1039 /* Print notices when new threads are attached and detached. */
1040 int print_thread_events
= 1;
1042 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1043 struct cmd_list_element
*c
, const char *value
)
1045 fprintf_filtered (file
, _("\
1046 Printing of thread events is %s.\n"),
1051 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1054 struct thread_info
*tp
;
1056 num
= value_as_long (parse_and_eval (tidstr
));
1058 tp
= find_thread_id (num
);
1061 error (_("Thread ID %d not known."), num
);
1063 if (!thread_alive (tp
))
1064 error (_("Thread ID %d has terminated."), num
);
1066 switch_to_thread (tp
->ptid
);
1068 ui_out_text (uiout
, "[Switching to thread ");
1069 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1070 ui_out_text (uiout
, " (");
1071 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
1072 ui_out_text (uiout
, ")]");
1074 /* Note that we can't reach this with an exited thread, due to the
1075 thread_alive check above. */
1076 if (tp
->state_
== THREAD_RUNNING
)
1077 ui_out_text (uiout
, "(running)\n");
1079 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1081 /* Since the current thread may have changed, see if there is any
1082 exited thread we can now delete. */
1089 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1091 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1092 error_message
, RETURN_MASK_ALL
) < 0)
1097 /* Commands with a prefix of `thread'. */
1098 struct cmd_list_element
*thread_cmd_list
= NULL
;
1101 _initialize_thread (void)
1103 static struct cmd_list_element
*thread_apply_list
= NULL
;
1104 struct cmd_list_element
*c
;
1106 c
= add_info ("threads", info_threads_command
,
1107 _("IDs of currently known threads."));
1108 set_cmd_no_selected_thread_ok (c
);
1110 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1111 Use this command to switch between threads.\n\
1112 The new thread ID must be currently known."),
1113 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1114 set_cmd_no_selected_thread_ok (c
);
1116 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1117 _("Apply a command to a list of threads."),
1118 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1119 set_cmd_no_selected_thread_ok (c
);
1121 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
1122 _("Apply a command to all threads."), &thread_apply_list
);
1123 set_cmd_no_selected_thread_ok (c
);
1126 add_com_alias ("t", "thread", class_run
, 1);
1128 add_setshow_boolean_cmd ("thread-events", no_class
,
1129 &print_thread_events
, _("\
1130 Set printing of thread events (such as thread start and exit)."), _("\
1131 Show printing of thread events (such as thread start and exit)."), NULL
,
1133 show_print_thread_events
,
1134 &setprintlist
, &showprintlist
);