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_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
)
250 observer_notify_thread_exit (tp
, silent
);
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 (tp
->state_
!= THREAD_EXITED
)
270 observer_notify_thread_exit (tp
, silent
);
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 /* Finds the first thread of the inferior given by PID. If PID is -1,
405 return the first thread in the list. */
408 first_thread_of_process (int pid
)
410 struct thread_info
*tp
, *ret
= NULL
;
412 for (tp
= thread_list
; tp
; tp
= tp
->next
)
413 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
414 if (ret
== NULL
|| tp
->num
< ret
->num
)
420 /* Print a list of thread ids currently known, and the total number of
421 threads. To be used from within catch_errors. */
423 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
425 struct thread_info
*tp
;
427 struct cleanup
*cleanup_chain
;
428 int current_thread
= -1;
431 target_find_new_threads ();
433 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
435 for (tp
= thread_list
; tp
; tp
= tp
->next
)
437 if (tp
->state_
== THREAD_EXITED
)
440 if (ptid_equal (tp
->ptid
, inferior_ptid
))
441 current_thread
= tp
->num
;
444 ui_out_field_int (uiout
, "thread-id", tp
->num
);
447 do_cleanups (cleanup_chain
);
449 if (current_thread
!= -1)
450 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
451 ui_out_field_int (uiout
, "number-of-threads", num
);
455 /* Official gdblib interface function to get a list of thread ids and
458 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
460 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
461 error_message
, RETURN_MASK_ALL
) < 0)
466 /* Return true if TP is an active thread. */
468 thread_alive (struct thread_info
*tp
)
470 if (tp
->state_
== THREAD_EXITED
)
472 if (!target_thread_alive (tp
->ptid
))
480 struct thread_info
*tp
, *next
;
482 for (tp
= thread_list
; tp
; tp
= next
)
485 if (!thread_alive (tp
))
486 delete_thread (tp
->ptid
);
491 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
493 struct inferior
*inf
;
494 struct thread_info
*tp
;
496 /* It can happen that what we knew as the target inferior id
497 changes. E.g, target remote may only discover the remote process
498 pid after adding the inferior to GDB's list. */
499 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
500 inf
->pid
= ptid_get_pid (new_ptid
);
502 tp
= find_thread_pid (old_ptid
);
505 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
509 set_running (ptid_t ptid
, int running
)
511 struct thread_info
*tp
;
512 int all
= ptid_equal (ptid
, minus_one_ptid
);
514 /* We try not to notify the observer if no thread has actually changed
515 the running state -- merely to reduce the number of messages to
516 frontend. Frontend is supposed to handle multiple *running just fine. */
517 if (all
|| ptid_is_pid (ptid
))
520 for (tp
= thread_list
; tp
; tp
= tp
->next
)
521 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
523 if (tp
->state_
== THREAD_EXITED
)
525 if (running
&& tp
->state_
== THREAD_STOPPED
)
527 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
530 observer_notify_target_resumed (ptid
);
535 tp
= find_thread_pid (ptid
);
537 gdb_assert (tp
->state_
!= THREAD_EXITED
);
538 if (running
&& tp
->state_
== THREAD_STOPPED
)
540 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
542 observer_notify_target_resumed (ptid
);
547 is_thread_state (ptid_t ptid
, enum thread_state state
)
549 struct thread_info
*tp
;
551 if (!target_has_execution
)
554 tp
= find_thread_pid (ptid
);
556 return tp
->state_
== state
;
560 is_stopped (ptid_t ptid
)
562 /* Without execution, this property is always true. */
563 if (!target_has_execution
)
566 return is_thread_state (ptid
, THREAD_STOPPED
);
570 is_exited (ptid_t ptid
)
572 /* Without execution, this property is always false. */
573 if (!target_has_execution
)
576 return is_thread_state (ptid
, THREAD_EXITED
);
580 is_running (ptid_t ptid
)
582 /* Without execution, this property is always false. */
583 if (!target_has_execution
)
586 return is_thread_state (ptid
, THREAD_RUNNING
);
592 struct thread_info
*tp
;
594 if (!target_has_execution
)
597 for (tp
= thread_list
; tp
; tp
= tp
->next
)
598 if (tp
->state_
== THREAD_RUNNING
)
605 is_executing (ptid_t ptid
)
607 struct thread_info
*tp
;
609 if (!target_has_execution
)
612 tp
= find_thread_pid (ptid
);
614 return tp
->executing_
;
618 set_executing (ptid_t ptid
, int executing
)
620 struct thread_info
*tp
;
621 int all
= ptid_equal (ptid
, minus_one_ptid
);
623 if (all
|| ptid_is_pid (ptid
))
625 for (tp
= thread_list
; tp
; tp
= tp
->next
)
626 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
627 tp
->executing_
= executing
;
631 tp
= find_thread_pid (ptid
);
633 tp
->executing_
= executing
;
638 set_stop_requested (ptid_t ptid
, int stop
)
640 struct thread_info
*tp
;
641 int all
= ptid_equal (ptid
, minus_one_ptid
);
643 if (all
|| ptid_is_pid (ptid
))
645 for (tp
= thread_list
; tp
; tp
= tp
->next
)
646 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
647 tp
->stop_requested
= stop
;
651 tp
= find_thread_pid (ptid
);
653 tp
->stop_requested
= stop
;
656 /* Call the stop requested observer so other components of GDB can
657 react to this request. */
659 observer_notify_thread_stop_requested (ptid
);
663 finish_thread_state (ptid_t ptid
)
665 struct thread_info
*tp
;
669 all
= ptid_equal (ptid
, minus_one_ptid
);
671 if (all
|| ptid_is_pid (ptid
))
673 for (tp
= thread_list
; tp
; tp
= tp
->next
)
675 if (tp
->state_
== THREAD_EXITED
)
677 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
679 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
681 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
687 tp
= find_thread_pid (ptid
);
689 if (tp
->state_
!= THREAD_EXITED
)
691 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
693 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
698 observer_notify_target_resumed (ptid
);
702 finish_thread_state_cleanup (void *arg
)
704 ptid_t
*ptid_p
= arg
;
708 finish_thread_state (*ptid_p
);
711 /* Prints the list of threads and their details on UIOUT.
712 This is a version of 'info_thread_command' suitable for
714 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
715 that should be printed. Otherwise, all threads are
717 If PID is not -1, only print threads from the process PID.
718 Otherwise, threads from all attached PIDs are printed.
719 If both REQUESTED_THREAD and PID are not -1, then the thread
720 is printed if it belongs to the specified process. Otherwise,
721 an error is raised. */
723 print_thread_info (struct ui_out
*uiout
, int requested_thread
, int pid
)
725 struct thread_info
*tp
;
727 struct cleanup
*old_chain
;
729 int current_thread
= -1;
732 target_find_new_threads ();
733 current_ptid
= inferior_ptid
;
735 /* We'll be switching threads temporarily. */
736 old_chain
= make_cleanup_restore_current_thread ();
738 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
739 for (tp
= thread_list
; tp
; tp
= tp
->next
)
741 struct cleanup
*chain2
;
743 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
746 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
748 if (requested_thread
!= -1)
749 error (_("Requested thread not found in requested process"));
753 if (ptid_equal (tp
->ptid
, current_ptid
))
754 current_thread
= tp
->num
;
756 if (tp
->state_
== THREAD_EXITED
)
759 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
761 if (ptid_equal (tp
->ptid
, current_ptid
))
762 ui_out_text (uiout
, "* ");
764 ui_out_text (uiout
, " ");
766 ui_out_field_int (uiout
, "id", tp
->num
);
767 ui_out_text (uiout
, " ");
768 ui_out_field_string (uiout
, "target-id", target_pid_to_str (tp
->ptid
));
770 extra_info
= target_extra_thread_info (tp
);
773 ui_out_text (uiout
, " (");
774 ui_out_field_string (uiout
, "details", extra_info
);
775 ui_out_text (uiout
, ")");
777 ui_out_text (uiout
, " ");
779 if (tp
->state_
== THREAD_RUNNING
)
780 ui_out_text (uiout
, "(running)\n");
783 /* The switch below puts us at the top of the stack (leaf
785 switch_to_thread (tp
->ptid
);
786 print_stack_frame (get_selected_frame (NULL
),
787 /* For MI output, print frame level. */
788 ui_out_is_mi_like_p (uiout
),
792 if (ui_out_is_mi_like_p (uiout
))
794 char *state
= "stopped";
795 if (tp
->state_
== THREAD_RUNNING
)
797 ui_out_field_string (uiout
, "state", state
);
800 do_cleanups (chain2
);
803 /* Restores the current thread and the frame selected before
804 the "info threads" command. */
805 do_cleanups (old_chain
);
807 if (pid
== -1 && requested_thread
== -1)
809 gdb_assert (current_thread
!= -1
811 || ptid_equal (inferior_ptid
, null_ptid
));
812 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
813 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
815 if (current_thread
!= -1 && is_exited (current_ptid
))
816 ui_out_message (uiout
, 0, "\n\
817 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
820 && current_thread
== -1
821 && ptid_equal (current_ptid
, null_ptid
))
822 ui_out_message (uiout
, 0, "\n\
823 No selected thread. See `help thread'.\n");
828 /* Print information about currently known threads
830 * Note: this has the drawback that it _really_ switches
831 * threads, which frees the frame cache. A no-side
832 * effects info-threads command would be nicer.
836 info_threads_command (char *arg
, int from_tty
)
838 print_thread_info (uiout
, -1, -1);
841 /* Switch from one thread to another. */
844 switch_to_thread (ptid_t ptid
)
846 if (ptid_equal (ptid
, inferior_ptid
))
849 inferior_ptid
= ptid
;
850 reinit_frame_cache ();
851 registers_changed ();
853 /* We don't check for is_stopped, because we're called at times
854 while in the TARGET_RUNNING state, e.g., while handling an
856 if (!ptid_equal (inferior_ptid
, null_ptid
)
858 && !is_executing (ptid
))
859 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
861 stop_pc
= ~(CORE_ADDR
) 0;
865 restore_current_thread (ptid_t ptid
)
867 switch_to_thread (ptid
);
871 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
873 struct frame_info
*frame
= NULL
;
876 gdb_assert (frame_level
>= 0);
878 /* Restore by level first, check if the frame id is the same as
879 expected. If that fails, try restoring by frame id. If that
880 fails, nothing to do, just warn the user. */
883 frame
= find_relative_frame (get_current_frame (), &count
);
886 /* Either the frame ids match, of they're both invalid. The
887 latter case is not failsafe, but since it's highly unlikely
888 the search by level finds the wrong frame, it's 99.9(9)% of
889 the time (for all practical purposes) safe. */
890 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
891 /* Note: could be better to check every frame_id
892 member for equality here. */
893 || (!frame_id_p (get_frame_id (frame
))
894 && !frame_id_p (a_frame_id
))))
896 /* Cool, all is fine. */
897 select_frame (frame
);
901 frame
= frame_find_by_id (a_frame_id
);
904 /* Cool, refound it. */
905 select_frame (frame
);
909 /* Nothing else to do, the frame layout really changed. Select the
910 innermost stack frame. */
911 select_frame (get_current_frame ());
914 if (!ui_out_is_mi_like_p (uiout
))
917 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
919 /* For MI, we should probably have a notification about
920 current frame change. But this error is not very
921 likely, so don't bother for now. */
922 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
926 struct current_thread_cleanup
928 ptid_t inferior_ptid
;
929 struct frame_id selected_frame_id
;
930 int selected_frame_level
;
935 do_restore_current_thread_cleanup (void *arg
)
937 struct thread_info
*tp
;
938 struct current_thread_cleanup
*old
= arg
;
940 tp
= find_thread_pid (old
->inferior_ptid
);
942 /* If the previously selected thread belonged to a process that has
943 in the mean time been deleted (due to normal exit, detach, etc.),
944 then don't revert back to it, but instead simply drop back to no
947 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
948 restore_current_thread (old
->inferior_ptid
);
950 restore_current_thread (null_ptid
);
952 /* The running state of the originally selected thread may have
953 changed, so we have to recheck it here. */
954 if (!ptid_equal (inferior_ptid
, null_ptid
)
956 && is_stopped (inferior_ptid
)
957 && target_has_registers
959 && target_has_memory
)
960 restore_selected_frame (old
->selected_frame_id
,
961 old
->selected_frame_level
);
965 restore_current_thread_cleanup_dtor (void *arg
)
967 struct current_thread_cleanup
*old
= arg
;
968 struct thread_info
*tp
;
969 tp
= find_thread_pid (old
->inferior_ptid
);
976 make_cleanup_restore_current_thread (void)
978 struct thread_info
*tp
;
979 struct frame_info
*frame
;
980 struct current_thread_cleanup
*old
;
982 old
= xmalloc (sizeof (struct current_thread_cleanup
));
983 old
->inferior_ptid
= inferior_ptid
;
985 if (!ptid_equal (inferior_ptid
, null_ptid
))
987 old
->was_stopped
= is_stopped (inferior_ptid
);
989 && target_has_registers
991 && target_has_memory
)
992 frame
= get_selected_frame (NULL
);
996 old
->selected_frame_id
= get_frame_id (frame
);
997 old
->selected_frame_level
= frame_relative_level (frame
);
999 tp
= find_thread_pid (inferior_ptid
);
1004 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1005 restore_current_thread_cleanup_dtor
);
1008 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1009 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1010 of two numbers seperated by a hyphen. Examples:
1012 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1013 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1014 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1018 thread_apply_all_command (char *cmd
, int from_tty
)
1020 struct thread_info
*tp
;
1021 struct cleanup
*old_chain
;
1024 if (cmd
== NULL
|| *cmd
== '\000')
1025 error (_("Please specify a command following the thread ID list"));
1028 target_find_new_threads ();
1030 old_chain
= make_cleanup_restore_current_thread ();
1032 /* Save a copy of the command in case it is clobbered by
1034 saved_cmd
= xstrdup (cmd
);
1035 make_cleanup (xfree
, saved_cmd
);
1036 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1037 if (thread_alive (tp
))
1039 switch_to_thread (tp
->ptid
);
1041 printf_filtered (_("\nThread %d (%s):\n"),
1042 tp
->num
, target_pid_to_str (inferior_ptid
));
1043 execute_command (cmd
, from_tty
);
1044 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
1047 do_cleanups (old_chain
);
1051 thread_apply_command (char *tidlist
, int from_tty
)
1055 struct cleanup
*old_chain
;
1058 if (tidlist
== NULL
|| *tidlist
== '\000')
1059 error (_("Please specify a thread ID list"));
1061 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1064 error (_("Please specify a command following the thread ID list"));
1066 /* Save a copy of the command in case it is clobbered by
1068 saved_cmd
= xstrdup (cmd
);
1069 old_chain
= make_cleanup (xfree
, saved_cmd
);
1070 while (tidlist
< cmd
)
1072 struct thread_info
*tp
;
1075 start
= strtol (tidlist
, &p
, 10);
1077 error (_("Error parsing %s"), tidlist
);
1080 while (*tidlist
== ' ' || *tidlist
== '\t')
1083 if (*tidlist
== '-') /* Got a range of IDs? */
1085 tidlist
++; /* Skip the - */
1086 end
= strtol (tidlist
, &p
, 10);
1088 error (_("Error parsing %s"), tidlist
);
1091 while (*tidlist
== ' ' || *tidlist
== '\t')
1097 make_cleanup_restore_current_thread ();
1099 for (; start
<= end
; start
++)
1101 tp
= find_thread_id (start
);
1104 warning (_("Unknown thread %d."), start
);
1105 else if (!thread_alive (tp
))
1106 warning (_("Thread %d has terminated."), start
);
1109 switch_to_thread (tp
->ptid
);
1111 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1112 target_pid_to_str (inferior_ptid
));
1113 execute_command (cmd
, from_tty
);
1115 /* Restore exact command used previously. */
1116 strcpy (cmd
, saved_cmd
);
1121 do_cleanups (old_chain
);
1124 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1125 if prefix of arg is `apply'. */
1128 thread_command (char *tidstr
, int from_tty
)
1132 if (ptid_equal (inferior_ptid
, null_ptid
))
1133 error (_("No thread selected"));
1135 if (target_has_stack
)
1137 if (is_exited (inferior_ptid
))
1138 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1139 pid_to_thread_id (inferior_ptid
),
1140 target_pid_to_str (inferior_ptid
));
1142 printf_filtered (_("[Current thread is %d (%s)]\n"),
1143 pid_to_thread_id (inferior_ptid
),
1144 target_pid_to_str (inferior_ptid
));
1147 error (_("No stack."));
1151 gdb_thread_select (uiout
, tidstr
, NULL
);
1154 /* Print notices when new threads are attached and detached. */
1155 int print_thread_events
= 1;
1157 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1158 struct cmd_list_element
*c
, const char *value
)
1160 fprintf_filtered (file
, _("\
1161 Printing of thread events is %s.\n"),
1166 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1169 struct thread_info
*tp
;
1171 num
= value_as_long (parse_and_eval (tidstr
));
1173 tp
= find_thread_id (num
);
1176 error (_("Thread ID %d not known."), num
);
1178 if (!thread_alive (tp
))
1179 error (_("Thread ID %d has terminated."), num
);
1181 switch_to_thread (tp
->ptid
);
1183 annotate_thread_changed ();
1185 ui_out_text (uiout
, "[Switching to thread ");
1186 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1187 ui_out_text (uiout
, " (");
1188 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1189 ui_out_text (uiout
, ")]");
1191 /* Note that we can't reach this with an exited thread, due to the
1192 thread_alive check above. */
1193 if (tp
->state_
== THREAD_RUNNING
)
1194 ui_out_text (uiout
, "(running)\n");
1196 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1198 /* Since the current thread may have changed, see if there is any
1199 exited thread we can now delete. */
1206 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1208 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1209 error_message
, RETURN_MASK_ALL
) < 0)
1214 /* Commands with a prefix of `thread'. */
1215 struct cmd_list_element
*thread_cmd_list
= NULL
;
1218 _initialize_thread (void)
1220 static struct cmd_list_element
*thread_apply_list
= NULL
;
1222 add_info ("threads", info_threads_command
,
1223 _("IDs of currently known threads."));
1225 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1226 Use this command to switch between threads.\n\
1227 The new thread ID must be currently known."),
1228 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1230 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1231 _("Apply a command to a list of threads."),
1232 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1234 add_cmd ("all", class_run
, thread_apply_all_command
,
1235 _("Apply a command to all threads."), &thread_apply_list
);
1238 add_com_alias ("t", "thread", class_run
, 1);
1240 add_setshow_boolean_cmd ("thread-events", no_class
,
1241 &print_thread_events
, _("\
1242 Set printing of thread events (such as thread start and exit)."), _("\
1243 Show printing of thread events (such as thread start and exit)."), NULL
,
1245 show_print_thread_events
,
1246 &setprintlist
, &showprintlist
);