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
);
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;
130 main_thread_state
= THREAD_STOPPED
;
131 main_thread_executing
= 0;
136 for (tp
= thread_list
; tp
; tp
= tpnext
)
146 add_thread_silent (ptid_t ptid
)
148 struct thread_info
*tp
;
150 tp
= find_thread_pid (ptid
);
152 /* Found an old thread with the same id. It has to be dead,
153 otherwise we wouldn't be adding a new thread with the same id.
154 The OS is reusing this id --- delete it, and recreate a new
157 /* In addition to deleting the thread, if this is the current
158 thread, then we need to also get rid of the current infrun
159 context, and take care that delete_thread doesn't really
160 delete the thread if it is inferior_ptid. Create a new
161 template thread in the list with an invalid ptid, context
162 switch to it, delete the original thread, reset the new
163 thread's ptid, and switch to it. */
165 if (ptid_equal (inferior_ptid
, ptid
))
167 tp
= xmalloc (sizeof (*tp
));
168 memset (tp
, 0, sizeof (*tp
));
169 tp
->ptid
= minus_one_ptid
;
170 tp
->num
= ++highest_thread_num
;
171 tp
->next
= thread_list
;
173 context_switch_to (minus_one_ptid
);
175 /* Now we can delete it. */
176 delete_thread (ptid
);
178 /* Since the context is already set to this new thread,
179 reset its ptid, and reswitch inferior_ptid to it. */
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 /* Load infrun state for the thread PID. */
445 load_infrun_state (ptid_t ptid
,
446 struct continuation
**continuations
,
447 struct continuation
**intermediate_continuations
,
448 int *proceed_to_finish
,
449 enum step_over_calls_kind
*step_over_calls
,
452 enum target_signal
*stop_signal
,
455 struct thread_info
*tp
;
457 /* If we can't find the thread, then we're debugging a single threaded
458 process. No need to do anything in that case. */
459 tp
= find_thread_id (pid_to_thread_id (ptid
));
463 /* In all-stop mode, these are global state, while in non-stop mode,
464 they are per thread. */
467 *continuations
= tp
->continuations
;
468 tp
->continuations
= NULL
;
469 *intermediate_continuations
= tp
->intermediate_continuations
;
470 tp
->intermediate_continuations
= NULL
;
471 *proceed_to_finish
= tp
->proceed_to_finish
;
472 *step_over_calls
= tp
->step_over_calls
;
473 *stop_step
= tp
->stop_step
;
474 *step_multi
= tp
->step_multi
;
475 *stop_signal
= tp
->stop_signal
;
477 /* Swap instead of copy, so we only have to update one of
479 *stop_bpstat
= tp
->stop_bpstat
;
484 /* Save infrun state for the thread PID. */
487 save_infrun_state (ptid_t ptid
,
488 struct continuation
*continuations
,
489 struct continuation
*intermediate_continuations
,
490 int proceed_to_finish
,
491 enum step_over_calls_kind step_over_calls
,
494 enum target_signal stop_signal
,
497 struct thread_info
*tp
;
499 /* If we can't find the thread, then we're debugging a single-threaded
500 process. Nothing to do in that case. */
501 tp
= find_thread_id (pid_to_thread_id (ptid
));
505 /* In all-stop mode, these are global state, while in non-stop mode,
506 they are per thread. */
509 tp
->continuations
= continuations
;
510 tp
->intermediate_continuations
= intermediate_continuations
;
511 tp
->proceed_to_finish
= proceed_to_finish
;
512 tp
->step_over_calls
= step_over_calls
;
513 tp
->stop_step
= stop_step
;
514 tp
->step_multi
= step_multi
;
515 tp
->stop_signal
= stop_signal
;
516 tp
->stop_bpstat
= stop_bpstat
;
520 /* Return true if TP is an active thread. */
522 thread_alive (struct thread_info
*tp
)
524 if (tp
->state_
== THREAD_EXITED
)
526 if (!target_thread_alive (tp
->ptid
))
534 struct thread_info
*tp
, *next
;
536 for (tp
= thread_list
; tp
; tp
= next
)
539 if (!thread_alive (tp
))
540 delete_thread (tp
->ptid
);
545 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
547 struct thread_info
* tp
= find_thread_pid (old_ptid
);
550 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
554 set_running (ptid_t ptid
, int running
)
556 struct thread_info
*tp
;
560 /* This is one of the targets that does not add main
561 thread to the thread list. Just use a single
562 global flag to indicate that a thread is running.
564 This problem is unique to ST programs. For MT programs,
565 the main thread is always present in the thread list. If it's
566 not, the first call to context_switch will mess up GDB internal
569 && main_thread_state
!= THREAD_RUNNING
570 && !suppress_resume_observer
)
571 observer_notify_target_resumed (ptid
);
572 main_thread_state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
576 /* We try not to notify the observer if no thread has actually changed
577 the running state -- merely to reduce the number of messages to
578 frontend. Frontend is supposed to handle multiple *running just fine. */
579 if (PIDGET (ptid
) == -1)
582 for (tp
= thread_list
; tp
; tp
= tp
->next
)
584 if (tp
->state_
== THREAD_EXITED
)
586 if (running
&& tp
->state_
== THREAD_STOPPED
)
588 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
590 if (any_started
&& !suppress_resume_observer
)
591 observer_notify_target_resumed (ptid
);
596 tp
= find_thread_pid (ptid
);
598 gdb_assert (tp
->state_
!= THREAD_EXITED
);
599 if (running
&& tp
->state_
== THREAD_STOPPED
)
601 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
602 if (started
&& !suppress_resume_observer
)
603 observer_notify_target_resumed (ptid
);
608 is_thread_state (ptid_t ptid
, enum thread_state state
)
610 struct thread_info
*tp
;
612 if (!target_has_execution
)
616 return main_thread_state
== state
;
618 tp
= find_thread_pid (ptid
);
620 return tp
->state_
== state
;
624 is_stopped (ptid_t ptid
)
626 /* Without execution, this property is always true. */
627 if (!target_has_execution
)
630 return is_thread_state (ptid
, THREAD_STOPPED
);
634 is_exited (ptid_t ptid
)
636 /* Without execution, this property is always false. */
637 if (!target_has_execution
)
640 return is_thread_state (ptid
, THREAD_EXITED
);
644 is_running (ptid_t ptid
)
646 /* Without execution, this property is always false. */
647 if (!target_has_execution
)
650 return is_thread_state (ptid
, THREAD_RUNNING
);
656 struct thread_info
*tp
;
658 if (!target_has_execution
)
662 return main_thread_state
== THREAD_RUNNING
;
664 for (tp
= thread_list
; tp
; tp
= tp
->next
)
665 if (tp
->state_
== THREAD_RUNNING
)
672 is_executing (ptid_t ptid
)
674 struct thread_info
*tp
;
676 if (!target_has_execution
)
680 return main_thread_executing
;
682 tp
= find_thread_pid (ptid
);
684 return tp
->executing_
;
688 set_executing (ptid_t ptid
, int executing
)
690 struct thread_info
*tp
;
694 /* This target does not add the main thread to the thread list.
695 Use a global flag to indicate that the thread is
697 main_thread_executing
= executing
;
701 if (PIDGET (ptid
) == -1)
703 for (tp
= thread_list
; tp
; tp
= tp
->next
)
704 tp
->executing_
= executing
;
708 tp
= find_thread_pid (ptid
);
710 tp
->executing_
= executing
;
714 /* Prints the list of threads and their details on UIOUT.
715 This is a version of 'info_thread_command' suitable for
717 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
718 that should be printed. Otherwise, all threads are
721 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
723 struct thread_info
*tp
;
725 struct cleanup
*old_chain
;
727 int current_thread
= -1;
730 target_find_new_threads ();
731 current_ptid
= inferior_ptid
;
733 /* We'll be switching threads temporarily. */
734 old_chain
= make_cleanup_restore_current_thread ();
736 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
737 for (tp
= thread_list
; tp
; tp
= tp
->next
)
739 struct cleanup
*chain2
;
741 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
744 if (ptid_equal (tp
->ptid
, current_ptid
))
745 current_thread
= tp
->num
;
747 if (tp
->state_
== THREAD_EXITED
)
750 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
752 if (ptid_equal (tp
->ptid
, current_ptid
))
753 ui_out_text (uiout
, "* ");
755 ui_out_text (uiout
, " ");
757 ui_out_field_int (uiout
, "id", tp
->num
);
758 ui_out_text (uiout
, " ");
759 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
761 if (tp
->state_
!= THREAD_EXITED
)
763 extra_info
= target_extra_thread_info (tp
);
766 ui_out_text (uiout
, " (");
767 ui_out_field_string (uiout
, "details", extra_info
);
768 ui_out_text (uiout
, ")");
770 ui_out_text (uiout
, " ");
773 if (tp
->state_
== THREAD_RUNNING
)
774 ui_out_text (uiout
, "(running)\n");
777 /* The switch below puts us at the top of the stack (leaf
779 switch_to_thread (tp
->ptid
);
780 print_stack_frame (get_selected_frame (NULL
),
781 /* For MI output, print frame level. */
782 ui_out_is_mi_like_p (uiout
),
786 if (ui_out_is_mi_like_p (uiout
))
788 char *state
= "stopped";
789 if (tp
->state_
== THREAD_EXITED
)
791 else if (tp
->state_
== THREAD_RUNNING
)
793 ui_out_field_string (uiout
, "state", state
);
796 do_cleanups (chain2
);
799 /* Restores the current thread and the frame selected before
800 the "info threads" command. */
801 do_cleanups (old_chain
);
803 if (requested_thread
== -1)
805 gdb_assert (current_thread
!= -1
807 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
808 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
810 if (current_thread
!= -1 && is_exited (current_ptid
))
811 ui_out_message (uiout
, 0, "\n\
812 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
818 /* Print information about currently known threads
820 * Note: this has the drawback that it _really_ switches
821 * threads, which frees the frame cache. A no-side
822 * effects info-threads command would be nicer.
826 info_threads_command (char *arg
, int from_tty
)
828 print_thread_info (uiout
, -1);
831 /* Switch from one thread to another. */
834 switch_to_thread (ptid_t ptid
)
836 if (ptid_equal (ptid
, inferior_ptid
))
839 inferior_ptid
= ptid
;
840 reinit_frame_cache ();
841 registers_changed ();
843 /* We don't check for is_stopped, because we're called at times
844 while in the TARGET_RUNNING state, e.g., while handling an
846 if (!is_exited (ptid
) && !is_executing (ptid
))
847 stop_pc
= read_pc ();
849 stop_pc
= ~(CORE_ADDR
) 0;
853 restore_current_thread (ptid_t ptid
)
855 if (!ptid_equal (ptid
, inferior_ptid
))
858 context_switch_to (ptid
);
860 switch_to_thread (ptid
);
865 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
867 struct frame_info
*frame
= NULL
;
870 gdb_assert (frame_level
>= 0);
872 /* Restore by level first, check if the frame id is the same as
873 expected. If that fails, try restoring by frame id. If that
874 fails, nothing to do, just warn the user. */
877 frame
= find_relative_frame (get_current_frame (), &count
);
880 /* Either the frame ids match, of they're both invalid. The
881 latter case is not failsafe, but since it's highly unlikely
882 the search by level finds the wrong frame, it's 99.9(9)% of
883 the time (for all practical purposes) safe. */
884 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
885 /* Note: could be better to check every frame_id
886 member for equality here. */
887 || (!frame_id_p (get_frame_id (frame
))
888 && !frame_id_p (a_frame_id
))))
890 /* Cool, all is fine. */
891 select_frame (frame
);
895 frame
= frame_find_by_id (a_frame_id
);
898 /* Cool, refound it. */
899 select_frame (frame
);
903 /* Nothing else to do, the frame layout really changed. Select the
904 innermost stack frame. */
905 select_frame (get_current_frame ());
908 if (!ui_out_is_mi_like_p (uiout
))
911 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
913 /* For MI, we should probably have a notification about
914 current frame change. But this error is not very
915 likely, so don't bother for now. */
916 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
920 struct current_thread_cleanup
922 ptid_t inferior_ptid
;
923 struct frame_id selected_frame_id
;
924 int selected_frame_level
;
929 do_restore_current_thread_cleanup (void *arg
)
931 struct thread_info
*tp
;
932 struct current_thread_cleanup
*old
= arg
;
933 restore_current_thread (old
->inferior_ptid
);
935 /* The running state of the originally selected thread may have
936 changed, so we have to recheck it here. */
938 && is_stopped (inferior_ptid
)
939 && target_has_registers
941 && target_has_memory
)
942 restore_selected_frame (old
->selected_frame_id
,
943 old
->selected_frame_level
);
947 restore_current_thread_cleanup_dtor (void *arg
)
949 struct current_thread_cleanup
*old
= arg
;
950 struct thread_info
*tp
;
951 tp
= find_thread_pid (old
->inferior_ptid
);
958 make_cleanup_restore_current_thread (void)
960 struct thread_info
*tp
;
961 struct frame_info
*frame
;
962 struct current_thread_cleanup
*old
;
964 old
= xmalloc (sizeof (struct current_thread_cleanup
));
965 old
->inferior_ptid
= inferior_ptid
;
966 old
->was_stopped
= is_stopped (inferior_ptid
);
968 && target_has_registers
970 && target_has_memory
)
971 frame
= get_selected_frame (NULL
);
975 old
->selected_frame_id
= get_frame_id (frame
);
976 old
->selected_frame_level
= frame_relative_level (frame
);
978 tp
= find_thread_pid (inferior_ptid
);
982 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
983 restore_current_thread_cleanup_dtor
);
986 /* Apply a GDB command to a list of threads. List syntax is a whitespace
987 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
988 of two numbers seperated by a hyphen. Examples:
990 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
991 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
992 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
996 thread_apply_all_command (char *cmd
, int from_tty
)
998 struct thread_info
*tp
;
999 struct cleanup
*old_chain
;
1002 if (cmd
== NULL
|| *cmd
== '\000')
1003 error (_("Please specify a command following the thread ID list"));
1006 target_find_new_threads ();
1008 old_chain
= make_cleanup_restore_current_thread ();
1010 /* Save a copy of the command in case it is clobbered by
1012 saved_cmd
= xstrdup (cmd
);
1013 make_cleanup (xfree
, saved_cmd
);
1014 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1015 if (thread_alive (tp
))
1018 context_switch_to (tp
->ptid
);
1020 switch_to_thread (tp
->ptid
);
1022 printf_filtered (_("\nThread %d (%s):\n"),
1023 tp
->num
, target_tid_to_str (inferior_ptid
));
1024 execute_command (cmd
, from_tty
);
1025 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
1028 do_cleanups (old_chain
);
1032 thread_apply_command (char *tidlist
, int from_tty
)
1036 struct cleanup
*old_chain
;
1039 if (tidlist
== NULL
|| *tidlist
== '\000')
1040 error (_("Please specify a thread ID list"));
1042 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1045 error (_("Please specify a command following the thread ID list"));
1047 /* Save a copy of the command in case it is clobbered by
1049 saved_cmd
= xstrdup (cmd
);
1050 old_chain
= make_cleanup (xfree
, saved_cmd
);
1051 while (tidlist
< cmd
)
1053 struct thread_info
*tp
;
1056 start
= strtol (tidlist
, &p
, 10);
1058 error (_("Error parsing %s"), tidlist
);
1061 while (*tidlist
== ' ' || *tidlist
== '\t')
1064 if (*tidlist
== '-') /* Got a range of IDs? */
1066 tidlist
++; /* Skip the - */
1067 end
= strtol (tidlist
, &p
, 10);
1069 error (_("Error parsing %s"), tidlist
);
1072 while (*tidlist
== ' ' || *tidlist
== '\t')
1078 make_cleanup_restore_current_thread ();
1080 for (; start
<= end
; start
++)
1082 tp
= find_thread_id (start
);
1085 warning (_("Unknown thread %d."), start
);
1086 else if (!thread_alive (tp
))
1087 warning (_("Thread %d has terminated."), start
);
1091 context_switch_to (tp
->ptid
);
1093 switch_to_thread (tp
->ptid
);
1095 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1096 target_tid_to_str (inferior_ptid
));
1097 execute_command (cmd
, from_tty
);
1099 /* Restore exact command used previously. */
1100 strcpy (cmd
, saved_cmd
);
1105 do_cleanups (old_chain
);
1108 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1109 if prefix of arg is `apply'. */
1112 thread_command (char *tidstr
, int from_tty
)
1116 if (target_has_stack
)
1118 if (is_exited (inferior_ptid
))
1119 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1120 pid_to_thread_id (inferior_ptid
),
1121 target_tid_to_str (inferior_ptid
));
1123 printf_filtered (_("[Current thread is %d (%s)]\n"),
1124 pid_to_thread_id (inferior_ptid
),
1125 target_tid_to_str (inferior_ptid
));
1128 error (_("No stack."));
1132 annotate_thread_changed ();
1133 gdb_thread_select (uiout
, tidstr
, NULL
);
1136 /* Print notices when new threads are attached and detached. */
1137 int print_thread_events
= 1;
1139 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1140 struct cmd_list_element
*c
, const char *value
)
1142 fprintf_filtered (file
, _("\
1143 Printing of thread events is %s.\n"),
1148 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1151 struct thread_info
*tp
;
1153 num
= value_as_long (parse_and_eval (tidstr
));
1155 tp
= find_thread_id (num
);
1158 error (_("Thread ID %d not known."), num
);
1160 if (!thread_alive (tp
))
1161 error (_("Thread ID %d has terminated."), num
);
1164 context_switch_to (tp
->ptid
);
1166 switch_to_thread (tp
->ptid
);
1168 ui_out_text (uiout
, "[Switching to thread ");
1169 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1170 ui_out_text (uiout
, " (");
1171 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
1172 ui_out_text (uiout
, ")]");
1174 /* Note that we can't reach this with an exited thread, due to the
1175 thread_alive check above. */
1176 if (tp
->state_
== THREAD_RUNNING
)
1177 ui_out_text (uiout
, "(running)\n");
1179 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1181 /* Since the current thread may have changed, see if there is any
1182 exited thread we can now delete. */
1189 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1191 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1192 error_message
, RETURN_MASK_ALL
) < 0)
1197 /* Commands with a prefix of `thread'. */
1198 struct cmd_list_element
*thread_cmd_list
= NULL
;
1201 _initialize_thread (void)
1203 static struct cmd_list_element
*thread_apply_list
= NULL
;
1204 struct cmd_list_element
*c
;
1206 c
= add_info ("threads", info_threads_command
,
1207 _("IDs of currently known threads."));
1208 set_cmd_no_selected_thread_ok (c
);
1210 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1211 Use this command to switch between threads.\n\
1212 The new thread ID must be currently known."),
1213 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1214 set_cmd_no_selected_thread_ok (c
);
1216 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1217 _("Apply a command to a list of threads."),
1218 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1219 set_cmd_no_selected_thread_ok (c
);
1221 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
1222 _("Apply a command to all threads."), &thread_apply_list
);
1223 set_cmd_no_selected_thread_ok (c
);
1226 add_com_alias ("t", "thread", class_run
, 1);
1228 add_setshow_boolean_cmd ("thread-events", no_class
,
1229 &print_thread_events
, _("\
1230 Set printing of thread events (such as thread start and exit)."), _("\
1231 Show printing of thread events (such as thread start and exit)."), NULL
,
1233 show_print_thread_events
,
1234 &setprintlist
, &showprintlist
);