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),... */
76 inferior_thread (void)
78 struct thread_info
*tp
= find_thread_ptid (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
)
144 /* Allocate a new thread with target id PTID and add it to the thread
147 static struct thread_info
*
148 new_thread (ptid_t ptid
)
150 struct thread_info
*tp
;
152 tp
= xcalloc (1, sizeof (*tp
));
155 tp
->num
= ++highest_thread_num
;
156 tp
->next
= thread_list
;
159 /* Nothing to follow yet. */
160 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
161 tp
->state_
= THREAD_STOPPED
;
167 add_thread_silent (ptid_t ptid
)
169 struct thread_info
*tp
;
171 tp
= find_thread_ptid (ptid
);
173 /* Found an old thread with the same id. It has to be dead,
174 otherwise we wouldn't be adding a new thread with the same id.
175 The OS is reusing this id --- delete it, and recreate a new
178 /* In addition to deleting the thread, if this is the current
179 thread, then we need to take care that delete_thread doesn't
180 really delete the thread if it is inferior_ptid. Create a
181 new template thread in the list with an invalid ptid, switch
182 to it, delete the original thread, reset the new thread's
183 ptid, and switch to it. */
185 if (ptid_equal (inferior_ptid
, ptid
))
187 tp
= new_thread (ptid
);
189 /* Make switch_to_thread not read from the thread. */
190 tp
->state_
= THREAD_EXITED
;
191 switch_to_thread (minus_one_ptid
);
193 /* Now we can delete it. */
194 delete_thread (ptid
);
196 /* Now reset its ptid, and reswitch inferior_ptid to it. */
198 tp
->state_
= THREAD_STOPPED
;
199 switch_to_thread (ptid
);
201 observer_notify_new_thread (tp
);
207 /* Just go ahead and delete it. */
208 delete_thread (ptid
);
211 tp
= new_thread (ptid
);
212 observer_notify_new_thread (tp
);
218 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
220 struct thread_info
*result
= add_thread_silent (ptid
);
222 result
->private = private;
224 if (print_thread_events
)
225 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
227 annotate_new_thread ();
232 add_thread (ptid_t ptid
)
234 return add_thread_with_info (ptid
, NULL
);
237 /* Delete thread PTID. If SILENT, don't notify the observer of this
240 delete_thread_1 (ptid_t ptid
, int silent
)
242 struct thread_info
*tp
, *tpprev
;
246 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
247 if (ptid_equal (tp
->ptid
, ptid
))
253 /* If this is the current thread, or there's code out there that
254 relies on it existing (refcount > 0) we can't delete yet. Mark
255 it as exited, and notify it. */
257 || ptid_equal (tp
->ptid
, inferior_ptid
))
259 if (tp
->state_
!= THREAD_EXITED
)
261 observer_notify_thread_exit (tp
, silent
);
263 /* Tag it as exited. */
264 tp
->state_
= THREAD_EXITED
;
266 /* Clear breakpoints, etc. associated with this thread. */
267 clear_thread_inferior_resources (tp
);
270 /* Will be really deleted some other time. */
275 tpprev
->next
= tp
->next
;
277 thread_list
= tp
->next
;
279 /* Notify thread exit, but only if we haven't already. */
280 if (tp
->state_
!= THREAD_EXITED
)
281 observer_notify_thread_exit (tp
, silent
);
286 /* Delete thread PTID and notify of thread exit. If this is
287 inferior_ptid, don't actually delete it, but tag it as exited and
288 do the notification. If PTID is the user selected thread, clear
291 delete_thread (ptid_t ptid
)
293 delete_thread_1 (ptid
, 0 /* not silent */);
297 delete_thread_silent (ptid_t ptid
)
299 delete_thread_1 (ptid
, 1 /* silent */);
303 find_thread_id (int num
)
305 struct thread_info
*tp
;
307 for (tp
= thread_list
; tp
; tp
= tp
->next
)
314 /* Find a thread_info by matching PTID. */
316 find_thread_ptid (ptid_t ptid
)
318 struct thread_info
*tp
;
320 for (tp
= thread_list
; tp
; tp
= tp
->next
)
321 if (ptid_equal (tp
->ptid
, ptid
))
328 * Thread iterator function.
330 * Calls a callback function once for each thread, so long as
331 * the callback function returns false. If the callback function
332 * returns true, the iteration will end and the current thread
333 * will be returned. This can be useful for implementing a
334 * search for a thread with arbitrary attributes, or for applying
335 * some operation to every thread.
337 * FIXME: some of the existing functionality, such as
338 * "Thread apply all", might be rewritten using this functionality.
342 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
345 struct thread_info
*tp
, *next
;
347 for (tp
= thread_list
; tp
; tp
= next
)
350 if ((*callback
) (tp
, data
))
361 struct thread_info
*tp
;
363 for (tp
= thread_list
; tp
; tp
= tp
->next
)
370 valid_thread_id (int num
)
372 struct thread_info
*tp
;
374 for (tp
= thread_list
; tp
; tp
= tp
->next
)
382 pid_to_thread_id (ptid_t ptid
)
384 struct thread_info
*tp
;
386 for (tp
= thread_list
; tp
; tp
= tp
->next
)
387 if (ptid_equal (tp
->ptid
, ptid
))
394 thread_id_to_pid (int num
)
396 struct thread_info
*thread
= find_thread_id (num
);
400 return pid_to_ptid (-1);
404 in_thread_list (ptid_t ptid
)
406 struct thread_info
*tp
;
408 for (tp
= thread_list
; tp
; tp
= tp
->next
)
409 if (ptid_equal (tp
->ptid
, ptid
))
412 return 0; /* Never heard of 'im */
415 /* Finds the first thread of the inferior given by PID. If PID is -1,
416 return the first thread in the list. */
419 first_thread_of_process (int pid
)
421 struct thread_info
*tp
, *ret
= NULL
;
423 for (tp
= thread_list
; tp
; tp
= tp
->next
)
424 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
425 if (ret
== NULL
|| tp
->num
< ret
->num
)
431 /* Print a list of thread ids currently known, and the total number of
432 threads. To be used from within catch_errors. */
434 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
436 struct thread_info
*tp
;
438 struct cleanup
*cleanup_chain
;
439 int current_thread
= -1;
442 target_find_new_threads ();
444 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
446 for (tp
= thread_list
; tp
; tp
= tp
->next
)
448 if (tp
->state_
== THREAD_EXITED
)
451 if (ptid_equal (tp
->ptid
, inferior_ptid
))
452 current_thread
= tp
->num
;
455 ui_out_field_int (uiout
, "thread-id", tp
->num
);
458 do_cleanups (cleanup_chain
);
460 if (current_thread
!= -1)
461 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
462 ui_out_field_int (uiout
, "number-of-threads", num
);
466 /* Official gdblib interface function to get a list of thread ids and
469 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
471 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
472 error_message
, RETURN_MASK_ALL
) < 0)
477 /* Return true if TP is an active thread. */
479 thread_alive (struct thread_info
*tp
)
481 if (tp
->state_
== THREAD_EXITED
)
483 if (!target_thread_alive (tp
->ptid
))
491 struct thread_info
*tp
, *next
;
493 for (tp
= thread_list
; tp
; tp
= next
)
496 if (!thread_alive (tp
))
497 delete_thread (tp
->ptid
);
502 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
504 struct inferior
*inf
;
505 struct thread_info
*tp
;
507 /* It can happen that what we knew as the target inferior id
508 changes. E.g, target remote may only discover the remote process
509 pid after adding the inferior to GDB's list. */
510 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
511 inf
->pid
= ptid_get_pid (new_ptid
);
513 tp
= find_thread_ptid (old_ptid
);
516 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
520 set_running (ptid_t ptid
, int running
)
522 struct thread_info
*tp
;
523 int all
= ptid_equal (ptid
, minus_one_ptid
);
525 /* We try not to notify the observer if no thread has actually changed
526 the running state -- merely to reduce the number of messages to
527 frontend. Frontend is supposed to handle multiple *running just fine. */
528 if (all
|| ptid_is_pid (ptid
))
531 for (tp
= thread_list
; tp
; tp
= tp
->next
)
532 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
534 if (tp
->state_
== THREAD_EXITED
)
536 if (running
&& tp
->state_
== THREAD_STOPPED
)
538 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
541 observer_notify_target_resumed (ptid
);
546 tp
= find_thread_ptid (ptid
);
548 gdb_assert (tp
->state_
!= THREAD_EXITED
);
549 if (running
&& tp
->state_
== THREAD_STOPPED
)
551 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
553 observer_notify_target_resumed (ptid
);
558 is_thread_state (ptid_t ptid
, enum thread_state state
)
560 struct thread_info
*tp
;
562 if (!target_has_execution
)
565 tp
= find_thread_ptid (ptid
);
567 return tp
->state_
== state
;
571 is_stopped (ptid_t ptid
)
573 /* Without execution, this property is always true. */
574 if (!target_has_execution
)
577 return is_thread_state (ptid
, THREAD_STOPPED
);
581 is_exited (ptid_t ptid
)
583 /* Without execution, this property is always false. */
584 if (!target_has_execution
)
587 return is_thread_state (ptid
, THREAD_EXITED
);
591 is_running (ptid_t ptid
)
593 /* Without execution, this property is always false. */
594 if (!target_has_execution
)
597 return is_thread_state (ptid
, THREAD_RUNNING
);
603 struct thread_info
*tp
;
605 if (!target_has_execution
)
608 for (tp
= thread_list
; tp
; tp
= tp
->next
)
609 if (tp
->state_
== THREAD_RUNNING
)
616 is_executing (ptid_t ptid
)
618 struct thread_info
*tp
;
620 if (!target_has_execution
)
623 tp
= find_thread_ptid (ptid
);
625 return tp
->executing_
;
629 set_executing (ptid_t ptid
, int executing
)
631 struct thread_info
*tp
;
632 int all
= ptid_equal (ptid
, minus_one_ptid
);
634 if (all
|| ptid_is_pid (ptid
))
636 for (tp
= thread_list
; tp
; tp
= tp
->next
)
637 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
638 tp
->executing_
= executing
;
642 tp
= find_thread_ptid (ptid
);
644 tp
->executing_
= executing
;
649 set_stop_requested (ptid_t ptid
, int stop
)
651 struct thread_info
*tp
;
652 int all
= ptid_equal (ptid
, minus_one_ptid
);
654 if (all
|| ptid_is_pid (ptid
))
656 for (tp
= thread_list
; tp
; tp
= tp
->next
)
657 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
658 tp
->stop_requested
= stop
;
662 tp
= find_thread_ptid (ptid
);
664 tp
->stop_requested
= stop
;
667 /* Call the stop requested observer so other components of GDB can
668 react to this request. */
670 observer_notify_thread_stop_requested (ptid
);
674 finish_thread_state (ptid_t ptid
)
676 struct thread_info
*tp
;
680 all
= ptid_equal (ptid
, minus_one_ptid
);
682 if (all
|| ptid_is_pid (ptid
))
684 for (tp
= thread_list
; tp
; tp
= tp
->next
)
686 if (tp
->state_
== THREAD_EXITED
)
688 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
690 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
692 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
698 tp
= find_thread_ptid (ptid
);
700 if (tp
->state_
!= THREAD_EXITED
)
702 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
704 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
709 observer_notify_target_resumed (ptid
);
713 finish_thread_state_cleanup (void *arg
)
715 ptid_t
*ptid_p
= arg
;
719 finish_thread_state (*ptid_p
);
722 /* Prints the list of threads and their details on UIOUT.
723 This is a version of 'info_thread_command' suitable for
725 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
726 that should be printed. Otherwise, all threads are
728 If PID is not -1, only print threads from the process PID.
729 Otherwise, threads from all attached PIDs are printed.
730 If both REQUESTED_THREAD and PID are not -1, then the thread
731 is printed if it belongs to the specified process. Otherwise,
732 an error is raised. */
734 print_thread_info (struct ui_out
*uiout
, int requested_thread
, int pid
)
736 struct thread_info
*tp
;
738 struct cleanup
*old_chain
;
740 int current_thread
= -1;
743 target_find_new_threads ();
744 current_ptid
= inferior_ptid
;
746 /* We'll be switching threads temporarily. */
747 old_chain
= make_cleanup_restore_current_thread ();
749 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
750 for (tp
= thread_list
; tp
; tp
= tp
->next
)
752 struct cleanup
*chain2
;
754 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
757 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
759 if (requested_thread
!= -1)
760 error (_("Requested thread not found in requested process"));
764 if (ptid_equal (tp
->ptid
, current_ptid
))
765 current_thread
= tp
->num
;
767 if (tp
->state_
== THREAD_EXITED
)
770 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
772 if (ptid_equal (tp
->ptid
, current_ptid
))
773 ui_out_text (uiout
, "* ");
775 ui_out_text (uiout
, " ");
777 ui_out_field_int (uiout
, "id", tp
->num
);
778 ui_out_text (uiout
, " ");
779 ui_out_field_string (uiout
, "target-id", target_pid_to_str (tp
->ptid
));
781 extra_info
= target_extra_thread_info (tp
);
784 ui_out_text (uiout
, " (");
785 ui_out_field_string (uiout
, "details", extra_info
);
786 ui_out_text (uiout
, ")");
788 ui_out_text (uiout
, " ");
790 if (tp
->state_
== THREAD_RUNNING
)
791 ui_out_text (uiout
, "(running)\n");
794 /* The switch below puts us at the top of the stack (leaf
796 switch_to_thread (tp
->ptid
);
797 print_stack_frame (get_selected_frame (NULL
),
798 /* For MI output, print frame level. */
799 ui_out_is_mi_like_p (uiout
),
803 if (ui_out_is_mi_like_p (uiout
))
805 char *state
= "stopped";
806 if (tp
->state_
== THREAD_RUNNING
)
808 ui_out_field_string (uiout
, "state", state
);
811 do_cleanups (chain2
);
814 /* Restores the current thread and the frame selected before
815 the "info threads" command. */
816 do_cleanups (old_chain
);
818 if (pid
== -1 && requested_thread
== -1)
820 gdb_assert (current_thread
!= -1
822 || ptid_equal (inferior_ptid
, null_ptid
));
823 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
824 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
826 if (current_thread
!= -1 && is_exited (current_ptid
))
827 ui_out_message (uiout
, 0, "\n\
828 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
831 && current_thread
== -1
832 && ptid_equal (current_ptid
, null_ptid
))
833 ui_out_message (uiout
, 0, "\n\
834 No selected thread. See `help thread'.\n");
839 /* Print information about currently known threads
841 * Note: this has the drawback that it _really_ switches
842 * threads, which frees the frame cache. A no-side
843 * effects info-threads command would be nicer.
847 info_threads_command (char *arg
, int from_tty
)
849 print_thread_info (uiout
, -1, -1);
852 /* Switch from one thread to another. */
855 switch_to_thread (ptid_t ptid
)
857 if (ptid_equal (ptid
, inferior_ptid
))
860 inferior_ptid
= ptid
;
861 reinit_frame_cache ();
862 registers_changed ();
864 /* We don't check for is_stopped, because we're called at times
865 while in the TARGET_RUNNING state, e.g., while handling an
867 if (!ptid_equal (inferior_ptid
, null_ptid
)
869 && !is_executing (ptid
))
870 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
872 stop_pc
= ~(CORE_ADDR
) 0;
876 restore_current_thread (ptid_t ptid
)
878 switch_to_thread (ptid
);
882 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
884 struct frame_info
*frame
= NULL
;
887 gdb_assert (frame_level
>= 0);
889 /* Restore by level first, check if the frame id is the same as
890 expected. If that fails, try restoring by frame id. If that
891 fails, nothing to do, just warn the user. */
894 frame
= find_relative_frame (get_current_frame (), &count
);
897 /* Either the frame ids match, of they're both invalid. The
898 latter case is not failsafe, but since it's highly unlikely
899 the search by level finds the wrong frame, it's 99.9(9)% of
900 the time (for all practical purposes) safe. */
901 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
902 /* Note: could be better to check every frame_id
903 member for equality here. */
904 || (!frame_id_p (get_frame_id (frame
))
905 && !frame_id_p (a_frame_id
))))
907 /* Cool, all is fine. */
908 select_frame (frame
);
912 frame
= frame_find_by_id (a_frame_id
);
915 /* Cool, refound it. */
916 select_frame (frame
);
920 /* Nothing else to do, the frame layout really changed. Select the
921 innermost stack frame. */
922 select_frame (get_current_frame ());
925 if (!ui_out_is_mi_like_p (uiout
))
928 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
930 /* For MI, we should probably have a notification about
931 current frame change. But this error is not very
932 likely, so don't bother for now. */
933 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
937 struct current_thread_cleanup
939 ptid_t inferior_ptid
;
940 struct frame_id selected_frame_id
;
941 int selected_frame_level
;
946 do_restore_current_thread_cleanup (void *arg
)
948 struct thread_info
*tp
;
949 struct current_thread_cleanup
*old
= arg
;
951 tp
= find_thread_ptid (old
->inferior_ptid
);
953 /* If the previously selected thread belonged to a process that has
954 in the mean time been deleted (due to normal exit, detach, etc.),
955 then don't revert back to it, but instead simply drop back to no
958 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
959 restore_current_thread (old
->inferior_ptid
);
961 restore_current_thread (null_ptid
);
963 /* The running state of the originally selected thread may have
964 changed, so we have to recheck it here. */
965 if (!ptid_equal (inferior_ptid
, null_ptid
)
967 && is_stopped (inferior_ptid
)
968 && target_has_registers
970 && target_has_memory
)
971 restore_selected_frame (old
->selected_frame_id
,
972 old
->selected_frame_level
);
976 restore_current_thread_cleanup_dtor (void *arg
)
978 struct current_thread_cleanup
*old
= arg
;
979 struct thread_info
*tp
;
980 tp
= find_thread_ptid (old
->inferior_ptid
);
987 make_cleanup_restore_current_thread (void)
989 struct thread_info
*tp
;
990 struct frame_info
*frame
;
991 struct current_thread_cleanup
*old
;
993 old
= xmalloc (sizeof (struct current_thread_cleanup
));
994 old
->inferior_ptid
= inferior_ptid
;
996 if (!ptid_equal (inferior_ptid
, null_ptid
))
998 old
->was_stopped
= is_stopped (inferior_ptid
);
1000 && target_has_registers
1002 && target_has_memory
)
1003 frame
= get_selected_frame (NULL
);
1007 old
->selected_frame_id
= get_frame_id (frame
);
1008 old
->selected_frame_level
= frame_relative_level (frame
);
1010 tp
= find_thread_ptid (inferior_ptid
);
1015 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1016 restore_current_thread_cleanup_dtor
);
1019 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1020 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1021 of two numbers seperated by a hyphen. Examples:
1023 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1024 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1025 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1029 thread_apply_all_command (char *cmd
, int from_tty
)
1031 struct thread_info
*tp
;
1032 struct cleanup
*old_chain
;
1035 if (cmd
== NULL
|| *cmd
== '\000')
1036 error (_("Please specify a command following the thread ID list"));
1039 target_find_new_threads ();
1041 old_chain
= make_cleanup_restore_current_thread ();
1043 /* Save a copy of the command in case it is clobbered by
1045 saved_cmd
= xstrdup (cmd
);
1046 make_cleanup (xfree
, saved_cmd
);
1047 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1048 if (thread_alive (tp
))
1050 switch_to_thread (tp
->ptid
);
1052 printf_filtered (_("\nThread %d (%s):\n"),
1053 tp
->num
, target_pid_to_str (inferior_ptid
));
1054 execute_command (cmd
, from_tty
);
1055 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
1058 do_cleanups (old_chain
);
1062 thread_apply_command (char *tidlist
, int from_tty
)
1066 struct cleanup
*old_chain
;
1069 if (tidlist
== NULL
|| *tidlist
== '\000')
1070 error (_("Please specify a thread ID list"));
1072 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1075 error (_("Please specify a command following the thread ID list"));
1077 /* Save a copy of the command in case it is clobbered by
1079 saved_cmd
= xstrdup (cmd
);
1080 old_chain
= make_cleanup (xfree
, saved_cmd
);
1081 while (tidlist
< cmd
)
1083 struct thread_info
*tp
;
1086 start
= strtol (tidlist
, &p
, 10);
1088 error (_("Error parsing %s"), tidlist
);
1091 while (*tidlist
== ' ' || *tidlist
== '\t')
1094 if (*tidlist
== '-') /* Got a range of IDs? */
1096 tidlist
++; /* Skip the - */
1097 end
= strtol (tidlist
, &p
, 10);
1099 error (_("Error parsing %s"), tidlist
);
1102 while (*tidlist
== ' ' || *tidlist
== '\t')
1108 make_cleanup_restore_current_thread ();
1110 for (; start
<= end
; start
++)
1112 tp
= find_thread_id (start
);
1115 warning (_("Unknown thread %d."), start
);
1116 else if (!thread_alive (tp
))
1117 warning (_("Thread %d has terminated."), start
);
1120 switch_to_thread (tp
->ptid
);
1122 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1123 target_pid_to_str (inferior_ptid
));
1124 execute_command (cmd
, from_tty
);
1126 /* Restore exact command used previously. */
1127 strcpy (cmd
, saved_cmd
);
1132 do_cleanups (old_chain
);
1135 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1136 if prefix of arg is `apply'. */
1139 thread_command (char *tidstr
, int from_tty
)
1143 if (ptid_equal (inferior_ptid
, null_ptid
))
1144 error (_("No thread selected"));
1146 if (target_has_stack
)
1148 if (is_exited (inferior_ptid
))
1149 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1150 pid_to_thread_id (inferior_ptid
),
1151 target_pid_to_str (inferior_ptid
));
1153 printf_filtered (_("[Current thread is %d (%s)]\n"),
1154 pid_to_thread_id (inferior_ptid
),
1155 target_pid_to_str (inferior_ptid
));
1158 error (_("No stack."));
1162 gdb_thread_select (uiout
, tidstr
, NULL
);
1165 /* Print notices when new threads are attached and detached. */
1166 int print_thread_events
= 1;
1168 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1169 struct cmd_list_element
*c
, const char *value
)
1171 fprintf_filtered (file
, _("\
1172 Printing of thread events is %s.\n"),
1177 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1180 struct thread_info
*tp
;
1182 num
= value_as_long (parse_and_eval (tidstr
));
1184 tp
= find_thread_id (num
);
1187 error (_("Thread ID %d not known."), num
);
1189 if (!thread_alive (tp
))
1190 error (_("Thread ID %d has terminated."), num
);
1192 switch_to_thread (tp
->ptid
);
1194 annotate_thread_changed ();
1196 ui_out_text (uiout
, "[Switching to thread ");
1197 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1198 ui_out_text (uiout
, " (");
1199 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1200 ui_out_text (uiout
, ")]");
1202 /* Note that we can't reach this with an exited thread, due to the
1203 thread_alive check above. */
1204 if (tp
->state_
== THREAD_RUNNING
)
1205 ui_out_text (uiout
, "(running)\n");
1207 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1209 /* Since the current thread may have changed, see if there is any
1210 exited thread we can now delete. */
1217 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1219 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1220 error_message
, RETURN_MASK_ALL
) < 0)
1225 /* Commands with a prefix of `thread'. */
1226 struct cmd_list_element
*thread_cmd_list
= NULL
;
1229 _initialize_thread (void)
1231 static struct cmd_list_element
*thread_apply_list
= NULL
;
1233 add_info ("threads", info_threads_command
,
1234 _("IDs of currently known threads."));
1236 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1237 Use this command to switch between threads.\n\
1238 The new thread ID must be currently known."),
1239 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1241 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1242 _("Apply a command to a list of threads."),
1243 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1245 add_cmd ("all", class_run
, thread_apply_all_command
,
1246 _("Apply a command to all threads."), &thread_apply_list
);
1249 add_com_alias ("t", "thread", class_run
, 1);
1251 add_setshow_boolean_cmd ("thread-events", no_class
,
1252 &print_thread_events
, _("\
1253 Set printing of thread events (such as thread start and exit)."), _("\
1254 Show printing of thread events (such as thread start and exit)."), NULL
,
1256 show_print_thread_events
,
1257 &setprintlist
, &showprintlist
);