1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "gdbthread.h"
32 #include "exceptions.h"
37 #include "gdb_string.h"
40 #include <sys/types.h>
45 #include "cli/cli-decode.h"
47 /* Definition of struct thread_info exported to gdbthread.h */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 static struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 static void thread_command (char *tidstr
, int from_tty
);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info
*);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t
);
64 static void prune_threads (void);
66 /* Frontend view of the thread state. Possible extensions: stepping,
67 finishing, until(ling),... */
75 extern struct thread_info
*
76 inferior_thread (void)
78 struct thread_info
*tp
= find_thread_pid (inferior_ptid
);
84 delete_step_resume_breakpoint (struct thread_info
*tp
)
86 if (tp
&& tp
->step_resume_breakpoint
)
88 delete_breakpoint (tp
->step_resume_breakpoint
);
89 tp
->step_resume_breakpoint
= NULL
;
94 clear_thread_inferior_resources (struct thread_info
*tp
)
96 /* NOTE: this will take care of any left-over step_resume breakpoints,
97 but not any user-specified thread-specific breakpoints. We can not
98 delete the breakpoint straight-off, because the inferior might not
99 be stopped at the moment. */
100 if (tp
->step_resume_breakpoint
)
102 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
103 tp
->step_resume_breakpoint
= NULL
;
106 bpstat_clear (&tp
->stop_bpstat
);
108 discard_all_intermediate_continuations_thread (tp
);
109 discard_all_continuations_thread (tp
);
113 free_thread (struct thread_info
*tp
)
115 clear_thread_inferior_resources (tp
);
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;
135 for (tp
= thread_list
; tp
; tp
= tpnext
)
145 add_thread_silent (ptid_t ptid
)
147 struct thread_info
*tp
;
149 tp
= find_thread_pid (ptid
);
151 /* Found an old thread with the same id. It has to be dead,
152 otherwise we wouldn't be adding a new thread with the same id.
153 The OS is reusing this id --- delete it, and recreate a new
156 /* In addition to deleting the thread, if this is the current
157 thread, then we need to take care that delete_thread doesn't
158 really delete the thread if it is inferior_ptid. Create a
159 new template thread in the list with an invalid ptid, switch
160 to it, delete the original thread, reset the new thread's
161 ptid, and switch to it. */
163 if (ptid_equal (inferior_ptid
, ptid
))
165 tp
= xmalloc (sizeof (*tp
));
166 memset (tp
, 0, sizeof (*tp
));
167 tp
->ptid
= minus_one_ptid
;
168 tp
->num
= ++highest_thread_num
;
169 tp
->next
= thread_list
;
172 /* Make switch_to_thread not read from the thread. */
173 tp
->state_
= THREAD_EXITED
;
174 switch_to_thread (minus_one_ptid
);
176 /* Now we can delete it. */
177 delete_thread (ptid
);
179 /* Now reset its ptid, and reswitch inferior_ptid to it. */
181 tp
->state_
= THREAD_STOPPED
;
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 */);
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
;
413 int current_thread
= -1;
416 target_find_new_threads ();
418 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
420 for (tp
= thread_list
; tp
; tp
= tp
->next
)
422 if (tp
->state_
== THREAD_EXITED
)
425 if (ptid_equal (tp
->ptid
, inferior_ptid
))
426 current_thread
= tp
->num
;
429 ui_out_field_int (uiout
, "thread-id", tp
->num
);
432 do_cleanups (cleanup_chain
);
434 if (current_thread
!= -1)
435 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
436 ui_out_field_int (uiout
, "number-of-threads", num
);
440 /* Official gdblib interface function to get a list of thread ids and
443 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
445 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
446 error_message
, RETURN_MASK_ALL
) < 0)
451 /* Return true if TP is an active thread. */
453 thread_alive (struct thread_info
*tp
)
455 if (tp
->state_
== THREAD_EXITED
)
457 if (!target_thread_alive (tp
->ptid
))
465 struct thread_info
*tp
, *next
;
467 for (tp
= thread_list
; tp
; tp
= next
)
470 if (!thread_alive (tp
))
471 delete_thread (tp
->ptid
);
476 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
478 struct inferior
*inf
;
479 struct thread_info
*tp
;
481 /* It can happen that what we knew as the target inferior id
482 changes. E.g, target remote may only discover the remote process
483 pid after adding the inferior to GDB's list. */
484 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
485 inf
->pid
= ptid_get_pid (new_ptid
);
487 tp
= find_thread_pid (old_ptid
);
490 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
494 set_running (ptid_t ptid
, int running
)
496 struct thread_info
*tp
;
498 /* We try not to notify the observer if no thread has actually changed
499 the running state -- merely to reduce the number of messages to
500 frontend. Frontend is supposed to handle multiple *running just fine. */
501 if (PIDGET (ptid
) == -1)
504 for (tp
= thread_list
; tp
; tp
= tp
->next
)
506 if (tp
->state_
== THREAD_EXITED
)
508 if (running
&& tp
->state_
== THREAD_STOPPED
)
510 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
512 if (any_started
&& !suppress_resume_observer
)
513 observer_notify_target_resumed (ptid
);
518 tp
= find_thread_pid (ptid
);
520 gdb_assert (tp
->state_
!= THREAD_EXITED
);
521 if (running
&& tp
->state_
== THREAD_STOPPED
)
523 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
524 if (started
&& !suppress_resume_observer
)
525 observer_notify_target_resumed (ptid
);
530 is_thread_state (ptid_t ptid
, enum thread_state state
)
532 struct thread_info
*tp
;
534 if (!target_has_execution
)
537 tp
= find_thread_pid (ptid
);
539 return tp
->state_
== state
;
543 is_stopped (ptid_t ptid
)
545 /* Without execution, this property is always true. */
546 if (!target_has_execution
)
549 return is_thread_state (ptid
, THREAD_STOPPED
);
553 is_exited (ptid_t ptid
)
555 /* Without execution, this property is always false. */
556 if (!target_has_execution
)
559 return is_thread_state (ptid
, THREAD_EXITED
);
563 is_running (ptid_t ptid
)
565 /* Without execution, this property is always false. */
566 if (!target_has_execution
)
569 return is_thread_state (ptid
, THREAD_RUNNING
);
575 struct thread_info
*tp
;
577 if (!target_has_execution
)
580 for (tp
= thread_list
; tp
; tp
= tp
->next
)
581 if (tp
->state_
== THREAD_RUNNING
)
588 is_executing (ptid_t ptid
)
590 struct thread_info
*tp
;
592 if (!target_has_execution
)
595 tp
= find_thread_pid (ptid
);
597 return tp
->executing_
;
601 set_executing (ptid_t ptid
, int executing
)
603 struct thread_info
*tp
;
605 if (PIDGET (ptid
) == -1)
607 for (tp
= thread_list
; tp
; tp
= tp
->next
)
608 tp
->executing_
= executing
;
612 tp
= find_thread_pid (ptid
);
614 tp
->executing_
= executing
;
619 set_stop_requested (ptid_t ptid
, int stop
)
621 struct thread_info
*tp
;
622 int all
= ptid_equal (ptid
, minus_one_ptid
);
624 if (all
|| ptid_is_pid (ptid
))
626 for (tp
= thread_list
; tp
; tp
= tp
->next
)
627 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
628 tp
->stop_requested
= stop
;
632 tp
= find_thread_pid (ptid
);
634 tp
->stop_requested
= stop
;
637 /* Call the stop requested observer so other components of GDB can
638 react to this request. */
640 observer_notify_thread_stop_requested (ptid
);
644 finish_thread_state (ptid_t ptid
)
646 struct thread_info
*tp
;
650 all
= ptid_equal (ptid
, minus_one_ptid
);
652 if (all
|| ptid_is_pid (ptid
))
654 for (tp
= thread_list
; tp
; tp
= tp
->next
)
656 if (tp
->state_
== THREAD_EXITED
)
658 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
660 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
662 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
668 tp
= find_thread_pid (ptid
);
670 if (tp
->state_
!= THREAD_EXITED
)
672 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
674 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
679 observer_notify_target_resumed (ptid
);
683 finish_thread_state_cleanup (void *arg
)
685 ptid_t
*ptid_p
= arg
;
689 finish_thread_state (*ptid_p
);
692 /* Prints the list of threads and their details on UIOUT.
693 This is a version of 'info_thread_command' suitable for
695 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
696 that should be printed. Otherwise, all threads are
698 If PID is not -1, only print threads from the process PID.
699 Otherwise, threads from all attached PIDs are printed.
700 If both REQUESTED_THREAD and PID are not -1, then the thread
701 is printed if it belongs to the specified process. Otherwise,
702 an error is raised. */
704 print_thread_info (struct ui_out
*uiout
, int requested_thread
, int pid
)
706 struct thread_info
*tp
;
708 struct cleanup
*old_chain
;
710 int current_thread
= -1;
713 target_find_new_threads ();
714 current_ptid
= inferior_ptid
;
716 /* We'll be switching threads temporarily. */
717 old_chain
= make_cleanup_restore_current_thread ();
719 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
720 for (tp
= thread_list
; tp
; tp
= tp
->next
)
722 struct cleanup
*chain2
;
724 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
727 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
729 if (requested_thread
!= -1)
730 error (_("Requested thread not found in requested process"));
734 if (ptid_equal (tp
->ptid
, current_ptid
))
735 current_thread
= tp
->num
;
737 if (tp
->state_
== THREAD_EXITED
)
740 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
742 if (ptid_equal (tp
->ptid
, current_ptid
))
743 ui_out_text (uiout
, "* ");
745 ui_out_text (uiout
, " ");
747 ui_out_field_int (uiout
, "id", tp
->num
);
748 ui_out_text (uiout
, " ");
749 ui_out_field_string (uiout
, "target-id", target_pid_to_str (tp
->ptid
));
751 extra_info
= target_extra_thread_info (tp
);
754 ui_out_text (uiout
, " (");
755 ui_out_field_string (uiout
, "details", extra_info
);
756 ui_out_text (uiout
, ")");
758 ui_out_text (uiout
, " ");
760 if (tp
->state_
== THREAD_RUNNING
)
761 ui_out_text (uiout
, "(running)\n");
764 /* The switch below puts us at the top of the stack (leaf
766 switch_to_thread (tp
->ptid
);
767 print_stack_frame (get_selected_frame (NULL
),
768 /* For MI output, print frame level. */
769 ui_out_is_mi_like_p (uiout
),
773 if (ui_out_is_mi_like_p (uiout
))
775 char *state
= "stopped";
776 if (tp
->state_
== THREAD_RUNNING
)
778 ui_out_field_string (uiout
, "state", state
);
781 do_cleanups (chain2
);
784 /* Restores the current thread and the frame selected before
785 the "info threads" command. */
786 do_cleanups (old_chain
);
788 if (pid
== -1 && requested_thread
== -1 )
790 gdb_assert (current_thread
!= -1
792 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
793 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
795 if (current_thread
!= -1 && is_exited (current_ptid
))
796 ui_out_message (uiout
, 0, "\n\
797 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
803 /* Print information about currently known threads
805 * Note: this has the drawback that it _really_ switches
806 * threads, which frees the frame cache. A no-side
807 * effects info-threads command would be nicer.
811 info_threads_command (char *arg
, int from_tty
)
813 print_thread_info (uiout
, -1, -1);
816 /* Switch from one thread to another. */
819 switch_to_thread (ptid_t ptid
)
821 if (ptid_equal (ptid
, inferior_ptid
))
824 inferior_ptid
= ptid
;
825 reinit_frame_cache ();
826 registers_changed ();
828 /* We don't check for is_stopped, because we're called at times
829 while in the TARGET_RUNNING state, e.g., while handling an
831 if (!is_exited (ptid
) && !is_executing (ptid
))
832 stop_pc
= read_pc ();
834 stop_pc
= ~(CORE_ADDR
) 0;
838 restore_current_thread (ptid_t ptid
)
840 switch_to_thread (ptid
);
844 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
846 struct frame_info
*frame
= NULL
;
849 gdb_assert (frame_level
>= 0);
851 /* Restore by level first, check if the frame id is the same as
852 expected. If that fails, try restoring by frame id. If that
853 fails, nothing to do, just warn the user. */
856 frame
= find_relative_frame (get_current_frame (), &count
);
859 /* Either the frame ids match, of they're both invalid. The
860 latter case is not failsafe, but since it's highly unlikely
861 the search by level finds the wrong frame, it's 99.9(9)% of
862 the time (for all practical purposes) safe. */
863 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
864 /* Note: could be better to check every frame_id
865 member for equality here. */
866 || (!frame_id_p (get_frame_id (frame
))
867 && !frame_id_p (a_frame_id
))))
869 /* Cool, all is fine. */
870 select_frame (frame
);
874 frame
= frame_find_by_id (a_frame_id
);
877 /* Cool, refound it. */
878 select_frame (frame
);
882 /* Nothing else to do, the frame layout really changed. Select the
883 innermost stack frame. */
884 select_frame (get_current_frame ());
887 if (!ui_out_is_mi_like_p (uiout
))
890 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
892 /* For MI, we should probably have a notification about
893 current frame change. But this error is not very
894 likely, so don't bother for now. */
895 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
899 struct current_thread_cleanup
901 ptid_t inferior_ptid
;
902 struct frame_id selected_frame_id
;
903 int selected_frame_level
;
908 do_restore_current_thread_cleanup (void *arg
)
910 struct thread_info
*tp
;
911 struct current_thread_cleanup
*old
= arg
;
912 restore_current_thread (old
->inferior_ptid
);
914 /* The running state of the originally selected thread may have
915 changed, so we have to recheck it here. */
917 && is_stopped (inferior_ptid
)
918 && target_has_registers
920 && target_has_memory
)
921 restore_selected_frame (old
->selected_frame_id
,
922 old
->selected_frame_level
);
926 restore_current_thread_cleanup_dtor (void *arg
)
928 struct current_thread_cleanup
*old
= arg
;
929 struct thread_info
*tp
;
930 tp
= find_thread_pid (old
->inferior_ptid
);
937 make_cleanup_restore_current_thread (void)
939 struct thread_info
*tp
;
940 struct frame_info
*frame
;
941 struct current_thread_cleanup
*old
;
943 old
= xmalloc (sizeof (struct current_thread_cleanup
));
944 old
->inferior_ptid
= inferior_ptid
;
945 old
->was_stopped
= is_stopped (inferior_ptid
);
947 && target_has_registers
949 && target_has_memory
)
950 frame
= get_selected_frame (NULL
);
954 old
->selected_frame_id
= get_frame_id (frame
);
955 old
->selected_frame_level
= frame_relative_level (frame
);
957 tp
= find_thread_pid (inferior_ptid
);
961 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
962 restore_current_thread_cleanup_dtor
);
965 /* Apply a GDB command to a list of threads. List syntax is a whitespace
966 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
967 of two numbers seperated by a hyphen. Examples:
969 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
970 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
971 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
975 thread_apply_all_command (char *cmd
, int from_tty
)
977 struct thread_info
*tp
;
978 struct cleanup
*old_chain
;
981 if (cmd
== NULL
|| *cmd
== '\000')
982 error (_("Please specify a command following the thread ID list"));
985 target_find_new_threads ();
987 old_chain
= make_cleanup_restore_current_thread ();
989 /* Save a copy of the command in case it is clobbered by
991 saved_cmd
= xstrdup (cmd
);
992 make_cleanup (xfree
, saved_cmd
);
993 for (tp
= thread_list
; tp
; tp
= tp
->next
)
994 if (thread_alive (tp
))
996 switch_to_thread (tp
->ptid
);
998 printf_filtered (_("\nThread %d (%s):\n"),
999 tp
->num
, target_pid_to_str (inferior_ptid
));
1000 execute_command (cmd
, from_tty
);
1001 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
1004 do_cleanups (old_chain
);
1008 thread_apply_command (char *tidlist
, int from_tty
)
1012 struct cleanup
*old_chain
;
1015 if (tidlist
== NULL
|| *tidlist
== '\000')
1016 error (_("Please specify a thread ID list"));
1018 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1021 error (_("Please specify a command following the thread ID list"));
1023 /* Save a copy of the command in case it is clobbered by
1025 saved_cmd
= xstrdup (cmd
);
1026 old_chain
= make_cleanup (xfree
, saved_cmd
);
1027 while (tidlist
< cmd
)
1029 struct thread_info
*tp
;
1032 start
= strtol (tidlist
, &p
, 10);
1034 error (_("Error parsing %s"), tidlist
);
1037 while (*tidlist
== ' ' || *tidlist
== '\t')
1040 if (*tidlist
== '-') /* Got a range of IDs? */
1042 tidlist
++; /* Skip the - */
1043 end
= strtol (tidlist
, &p
, 10);
1045 error (_("Error parsing %s"), tidlist
);
1048 while (*tidlist
== ' ' || *tidlist
== '\t')
1054 make_cleanup_restore_current_thread ();
1056 for (; start
<= end
; start
++)
1058 tp
= find_thread_id (start
);
1061 warning (_("Unknown thread %d."), start
);
1062 else if (!thread_alive (tp
))
1063 warning (_("Thread %d has terminated."), start
);
1066 switch_to_thread (tp
->ptid
);
1068 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1069 target_pid_to_str (inferior_ptid
));
1070 execute_command (cmd
, from_tty
);
1072 /* Restore exact command used previously. */
1073 strcpy (cmd
, saved_cmd
);
1078 do_cleanups (old_chain
);
1081 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1082 if prefix of arg is `apply'. */
1085 thread_command (char *tidstr
, int from_tty
)
1089 if (target_has_stack
)
1091 if (is_exited (inferior_ptid
))
1092 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1093 pid_to_thread_id (inferior_ptid
),
1094 target_pid_to_str (inferior_ptid
));
1096 printf_filtered (_("[Current thread is %d (%s)]\n"),
1097 pid_to_thread_id (inferior_ptid
),
1098 target_pid_to_str (inferior_ptid
));
1101 error (_("No stack."));
1105 gdb_thread_select (uiout
, tidstr
, NULL
);
1108 /* Print notices when new threads are attached and detached. */
1109 int print_thread_events
= 1;
1111 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1112 struct cmd_list_element
*c
, const char *value
)
1114 fprintf_filtered (file
, _("\
1115 Printing of thread events is %s.\n"),
1120 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1123 struct thread_info
*tp
;
1125 num
= value_as_long (parse_and_eval (tidstr
));
1127 tp
= find_thread_id (num
);
1130 error (_("Thread ID %d not known."), num
);
1132 if (!thread_alive (tp
))
1133 error (_("Thread ID %d has terminated."), num
);
1135 switch_to_thread (tp
->ptid
);
1137 annotate_thread_changed ();
1139 ui_out_text (uiout
, "[Switching to thread ");
1140 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1141 ui_out_text (uiout
, " (");
1142 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1143 ui_out_text (uiout
, ")]");
1145 /* Note that we can't reach this with an exited thread, due to the
1146 thread_alive check above. */
1147 if (tp
->state_
== THREAD_RUNNING
)
1148 ui_out_text (uiout
, "(running)\n");
1150 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1152 /* Since the current thread may have changed, see if there is any
1153 exited thread we can now delete. */
1160 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1162 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1163 error_message
, RETURN_MASK_ALL
) < 0)
1168 /* Commands with a prefix of `thread'. */
1169 struct cmd_list_element
*thread_cmd_list
= NULL
;
1172 _initialize_thread (void)
1174 static struct cmd_list_element
*thread_apply_list
= NULL
;
1175 struct cmd_list_element
*c
;
1177 c
= add_info ("threads", info_threads_command
,
1178 _("IDs of currently known threads."));
1179 set_cmd_no_selected_thread_ok (c
);
1181 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1182 Use this command to switch between threads.\n\
1183 The new thread ID must be currently known."),
1184 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1185 set_cmd_no_selected_thread_ok (c
);
1187 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1188 _("Apply a command to a list of threads."),
1189 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1190 set_cmd_no_selected_thread_ok (c
);
1192 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
1193 _("Apply a command to all threads."), &thread_apply_list
);
1194 set_cmd_no_selected_thread_ok (c
);
1197 add_com_alias ("t", "thread", class_run
, 1);
1199 add_setshow_boolean_cmd ("thread-events", no_class
,
1200 &print_thread_events
, _("\
1201 Set printing of thread events (such as thread start and exit)."), _("\
1202 Show printing of thread events (such as thread start and exit)."), NULL
,
1204 show_print_thread_events
,
1205 &setprintlist
, &showprintlist
);