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;
78 delete_step_resume_breakpoint (void *arg
)
80 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
81 struct thread_info
*tp
;
83 if (*breakpointp
!= NULL
)
85 delete_breakpoint (*breakpointp
);
86 for (tp
= thread_list
; tp
; tp
= tp
->next
)
87 if (tp
->step_resume_breakpoint
== *breakpointp
)
88 tp
->step_resume_breakpoint
= NULL
;
95 clear_thread_inferior_resources (struct thread_info
*tp
)
97 /* NOTE: this will take care of any left-over step_resume breakpoints,
98 but not any user-specified thread-specific breakpoints. We can not
99 delete the breakpoint straight-off, because the inferior might not
100 be stopped at the moment. */
101 if (tp
->step_resume_breakpoint
)
103 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
104 tp
->step_resume_breakpoint
= NULL
;
107 bpstat_clear (&tp
->stop_bpstat
);
111 free_thread (struct thread_info
*tp
)
113 clear_thread_inferior_resources (tp
);
115 /* FIXME: do I ever need to call the back-end to give it a
116 chance at this private data before deleting the thread? */
124 init_thread_list (void)
126 struct thread_info
*tp
, *tpnext
;
128 highest_thread_num
= 0;
129 main_thread_state
= THREAD_STOPPED
;
130 main_thread_executing
= 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 also get rid of the current infrun
158 context, and take care that delete_thread doesn't really
159 delete the thread if it is inferior_ptid. Create a new
160 template thread in the list with an invalid ptid, context
161 switch to it, delete the original thread, reset the new
162 thread's ptid, and switch to it. */
164 if (ptid_equal (inferior_ptid
, ptid
))
166 tp
= xmalloc (sizeof (*tp
));
167 memset (tp
, 0, sizeof (*tp
));
168 tp
->ptid
= minus_one_ptid
;
169 tp
->num
= ++highest_thread_num
;
170 tp
->next
= thread_list
;
172 context_switch_to (minus_one_ptid
);
174 /* Now we can delete it. */
175 delete_thread (ptid
);
177 /* Since the context is already set to this new thread,
178 reset its ptid, and reswitch inferior_ptid to it. */
180 switch_to_thread (ptid
);
182 observer_notify_new_thread (tp
);
188 /* Just go ahead and delete it. */
189 delete_thread (ptid
);
192 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
193 memset (tp
, 0, sizeof (*tp
));
195 tp
->num
= ++highest_thread_num
;
196 tp
->next
= thread_list
;
199 observer_notify_new_thread (tp
);
205 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
207 struct thread_info
*result
= add_thread_silent (ptid
);
209 result
->private = private;
211 if (print_thread_events
)
212 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
214 annotate_new_thread ();
219 add_thread (ptid_t ptid
)
221 return add_thread_with_info (ptid
, NULL
);
224 /* Delete thread PTID. If SILENT, don't notify the observer of this
227 delete_thread_1 (ptid_t ptid
, int silent
)
229 struct thread_info
*tp
, *tpprev
;
233 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
234 if (ptid_equal (tp
->ptid
, ptid
))
240 /* If this is the current thread, or there's code out there that
241 relies on it existing (refcount > 0) we can't delete yet. Mark
242 it as exited, and notify it. */
244 || ptid_equal (tp
->ptid
, inferior_ptid
))
246 if (tp
->state_
!= THREAD_EXITED
)
249 observer_notify_thread_exit (tp
);
251 /* Tag it as exited. */
252 tp
->state_
= THREAD_EXITED
;
254 /* Clear breakpoints, etc. associated with this thread. */
255 clear_thread_inferior_resources (tp
);
258 /* Will be really deleted some other time. */
263 tpprev
->next
= tp
->next
;
265 thread_list
= tp
->next
;
267 /* Notify thread exit, but only if we haven't already. */
268 if (!silent
&& tp
->state_
!= THREAD_EXITED
)
269 observer_notify_thread_exit (tp
);
274 /* Delete thread PTID and notify of thread exit. If this is
275 inferior_ptid, don't actually delete it, but tag it as exited and
276 do the notification. If PTID is the user selected thread, clear
279 delete_thread (ptid_t ptid
)
281 delete_thread_1 (ptid
, 0 /* not silent */);
285 delete_thread_silent (ptid_t ptid
)
287 delete_thread_1 (ptid
, 1 /* silent */);
291 find_thread_id (int num
)
293 struct thread_info
*tp
;
295 for (tp
= thread_list
; tp
; tp
= tp
->next
)
302 /* Find a thread_info by matching PTID. */
304 find_thread_pid (ptid_t ptid
)
306 struct thread_info
*tp
;
308 for (tp
= thread_list
; tp
; tp
= tp
->next
)
309 if (ptid_equal (tp
->ptid
, ptid
))
316 * Thread iterator function.
318 * Calls a callback function once for each thread, so long as
319 * the callback function returns false. If the callback function
320 * returns true, the iteration will end and the current thread
321 * will be returned. This can be useful for implementing a
322 * search for a thread with arbitrary attributes, or for applying
323 * some operation to every thread.
325 * FIXME: some of the existing functionality, such as
326 * "Thread apply all", might be rewritten using this functionality.
330 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
333 struct thread_info
*tp
, *next
;
335 for (tp
= thread_list
; tp
; tp
= next
)
338 if ((*callback
) (tp
, data
))
349 struct thread_info
*tp
;
351 for (tp
= thread_list
; tp
; tp
= tp
->next
)
358 valid_thread_id (int num
)
360 struct thread_info
*tp
;
362 for (tp
= thread_list
; tp
; tp
= tp
->next
)
370 pid_to_thread_id (ptid_t ptid
)
372 struct thread_info
*tp
;
374 for (tp
= thread_list
; tp
; tp
= tp
->next
)
375 if (ptid_equal (tp
->ptid
, ptid
))
382 thread_id_to_pid (int num
)
384 struct thread_info
*thread
= find_thread_id (num
);
388 return pid_to_ptid (-1);
392 in_thread_list (ptid_t ptid
)
394 struct thread_info
*tp
;
396 for (tp
= thread_list
; tp
; tp
= tp
->next
)
397 if (ptid_equal (tp
->ptid
, ptid
))
400 return 0; /* Never heard of 'im */
403 /* Print a list of thread ids currently known, and the total number of
404 threads. To be used from within catch_errors. */
406 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
408 struct thread_info
*tp
;
410 struct cleanup
*cleanup_chain
;
413 target_find_new_threads ();
415 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
417 for (tp
= thread_list
; tp
; tp
= tp
->next
)
419 if (tp
->state_
== THREAD_EXITED
)
422 ui_out_field_int (uiout
, "thread-id", tp
->num
);
425 do_cleanups (cleanup_chain
);
426 ui_out_field_int (uiout
, "number-of-threads", num
);
430 /* Official gdblib interface function to get a list of thread ids and
433 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
435 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
436 error_message
, RETURN_MASK_ALL
) < 0)
441 /* Load infrun state for the thread PID. */
444 load_infrun_state (ptid_t ptid
,
447 struct breakpoint
**step_resume_breakpoint
,
448 CORE_ADDR
*step_range_start
,
449 CORE_ADDR
*step_range_end
,
450 struct frame_id
*step_frame_id
,
451 int *stepping_over_breakpoint
,
452 int *stepping_through_solib_after_catch
,
453 bpstat
*stepping_through_solib_catchpoints
,
455 struct symtab
**current_symtab
,
456 struct continuation
**continuations
,
457 struct continuation
**intermediate_continuations
,
458 int *proceed_to_finish
,
459 enum step_over_calls_kind
*step_over_calls
,
462 enum target_signal
*stop_signal
,
465 struct thread_info
*tp
;
467 /* If we can't find the thread, then we're debugging a single threaded
468 process. No need to do anything in that case. */
469 tp
= find_thread_id (pid_to_thread_id (ptid
));
473 *prev_pc
= tp
->prev_pc
;
474 *trap_expected
= tp
->trap_expected
;
475 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
476 *step_range_start
= tp
->step_range_start
;
477 *step_range_end
= tp
->step_range_end
;
478 *step_frame_id
= tp
->step_frame_id
;
479 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
480 *stepping_through_solib_after_catch
=
481 tp
->stepping_through_solib_after_catch
;
482 *stepping_through_solib_catchpoints
=
483 tp
->stepping_through_solib_catchpoints
;
484 *current_line
= tp
->current_line
;
485 *current_symtab
= tp
->current_symtab
;
487 /* In all-stop mode, these are global state, while in non-stop mode,
488 they are per thread. */
491 *continuations
= tp
->continuations
;
492 tp
->continuations
= NULL
;
493 *intermediate_continuations
= tp
->intermediate_continuations
;
494 tp
->intermediate_continuations
= NULL
;
495 *proceed_to_finish
= tp
->proceed_to_finish
;
496 *step_over_calls
= tp
->step_over_calls
;
497 *stop_step
= tp
->stop_step
;
498 *step_multi
= tp
->step_multi
;
499 *stop_signal
= tp
->stop_signal
;
501 /* Swap instead of copy, so we only have to update one of
503 *stop_bpstat
= tp
->stop_bpstat
;
508 /* Save infrun state for the thread PID. */
511 save_infrun_state (ptid_t ptid
,
514 struct breakpoint
*step_resume_breakpoint
,
515 CORE_ADDR step_range_start
,
516 CORE_ADDR step_range_end
,
517 const struct frame_id
*step_frame_id
,
518 int stepping_over_breakpoint
,
519 int stepping_through_solib_after_catch
,
520 bpstat stepping_through_solib_catchpoints
,
522 struct symtab
*current_symtab
,
523 struct continuation
*continuations
,
524 struct continuation
*intermediate_continuations
,
525 int proceed_to_finish
,
526 enum step_over_calls_kind step_over_calls
,
529 enum target_signal stop_signal
,
532 struct thread_info
*tp
;
534 /* If we can't find the thread, then we're debugging a single-threaded
535 process. Nothing to do in that case. */
536 tp
= find_thread_id (pid_to_thread_id (ptid
));
540 tp
->prev_pc
= prev_pc
;
541 tp
->trap_expected
= trap_expected
;
542 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
543 tp
->step_range_start
= step_range_start
;
544 tp
->step_range_end
= step_range_end
;
545 tp
->step_frame_id
= (*step_frame_id
);
546 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
547 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
548 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
549 tp
->current_line
= current_line
;
550 tp
->current_symtab
= current_symtab
;
552 /* In all-stop mode, these are global state, while in non-stop mode,
553 they are per thread. */
556 tp
->continuations
= continuations
;
557 tp
->intermediate_continuations
= intermediate_continuations
;
558 tp
->proceed_to_finish
= proceed_to_finish
;
559 tp
->step_over_calls
= step_over_calls
;
560 tp
->stop_step
= stop_step
;
561 tp
->step_multi
= step_multi
;
562 tp
->stop_signal
= stop_signal
;
563 tp
->stop_bpstat
= stop_bpstat
;
567 /* Return true if TP is an active thread. */
569 thread_alive (struct thread_info
*tp
)
571 if (tp
->state_
== THREAD_EXITED
)
573 if (!target_thread_alive (tp
->ptid
))
581 struct thread_info
*tp
, *next
;
583 for (tp
= thread_list
; tp
; tp
= next
)
586 if (!thread_alive (tp
))
587 delete_thread (tp
->ptid
);
592 set_running (ptid_t ptid
, int running
)
594 struct thread_info
*tp
;
598 /* This is one of the targets that does not add main
599 thread to the thread list. Just use a single
600 global flag to indicate that a thread is running.
602 This problem is unique to ST programs. For MT programs,
603 the main thread is always present in the thread list. If it's
604 not, the first call to context_switch will mess up GDB internal
607 && main_thread_state
!= THREAD_RUNNING
608 && !suppress_resume_observer
)
609 observer_notify_target_resumed (ptid
);
610 main_thread_state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
614 /* We try not to notify the observer if no thread has actually changed
615 the running state -- merely to reduce the number of messages to
616 frontend. Frontend is supposed to handle multiple *running just fine. */
617 if (PIDGET (ptid
) == -1)
620 for (tp
= thread_list
; tp
; tp
= tp
->next
)
622 if (tp
->state_
== THREAD_EXITED
)
624 if (running
&& tp
->state_
== THREAD_STOPPED
)
626 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
628 if (any_started
&& !suppress_resume_observer
)
629 observer_notify_target_resumed (ptid
);
634 tp
= find_thread_pid (ptid
);
636 gdb_assert (tp
->state_
!= THREAD_EXITED
);
637 if (running
&& tp
->state_
== THREAD_STOPPED
)
639 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
640 if (started
&& !suppress_resume_observer
)
641 observer_notify_target_resumed (ptid
);
646 is_thread_state (ptid_t ptid
, enum thread_state state
)
648 struct thread_info
*tp
;
650 if (!target_has_execution
)
654 return main_thread_state
== state
;
656 tp
= find_thread_pid (ptid
);
658 return tp
->state_
== state
;
662 is_stopped (ptid_t ptid
)
664 /* Without execution, this property is always true. */
665 if (!target_has_execution
)
668 return is_thread_state (ptid
, THREAD_STOPPED
);
672 is_exited (ptid_t ptid
)
674 /* Without execution, this property is always false. */
675 if (!target_has_execution
)
678 return is_thread_state (ptid
, THREAD_EXITED
);
682 is_running (ptid_t ptid
)
684 /* Without execution, this property is always false. */
685 if (!target_has_execution
)
688 return is_thread_state (ptid
, THREAD_RUNNING
);
694 struct thread_info
*tp
;
696 if (!target_has_execution
)
700 return main_thread_state
== THREAD_RUNNING
;
702 for (tp
= thread_list
; tp
; tp
= tp
->next
)
703 if (tp
->state_
== THREAD_RUNNING
)
710 is_executing (ptid_t ptid
)
712 struct thread_info
*tp
;
714 if (!target_has_execution
)
718 return main_thread_executing
;
720 tp
= find_thread_pid (ptid
);
722 return tp
->executing_
;
726 set_executing (ptid_t ptid
, int executing
)
728 struct thread_info
*tp
;
732 /* This target does not add the main thread to the thread list.
733 Use a global flag to indicate that the thread is
735 main_thread_executing
= executing
;
739 if (PIDGET (ptid
) == -1)
741 for (tp
= thread_list
; tp
; tp
= tp
->next
)
742 tp
->executing_
= executing
;
746 tp
= find_thread_pid (ptid
);
748 tp
->executing_
= executing
;
752 /* Prints the list of threads and their details on UIOUT.
753 This is a version of 'info_thread_command' suitable for
755 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
756 that should be printed. Otherwise, all threads are
759 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
761 struct thread_info
*tp
;
763 struct cleanup
*old_chain
;
765 int current_thread
= -1;
768 target_find_new_threads ();
769 current_ptid
= inferior_ptid
;
771 /* We'll be switching threads temporarily. */
772 old_chain
= make_cleanup_restore_current_thread ();
774 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
775 for (tp
= thread_list
; tp
; tp
= tp
->next
)
777 struct cleanup
*chain2
;
779 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
782 if (ptid_equal (tp
->ptid
, current_ptid
))
783 current_thread
= tp
->num
;
785 if (tp
->state_
== THREAD_EXITED
)
788 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
790 if (ptid_equal (tp
->ptid
, current_ptid
))
791 ui_out_text (uiout
, "* ");
793 ui_out_text (uiout
, " ");
795 ui_out_field_int (uiout
, "id", tp
->num
);
796 ui_out_text (uiout
, " ");
797 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
799 if (tp
->state_
!= THREAD_EXITED
)
801 extra_info
= target_extra_thread_info (tp
);
804 ui_out_text (uiout
, " (");
805 ui_out_field_string (uiout
, "details", extra_info
);
806 ui_out_text (uiout
, ")");
808 ui_out_text (uiout
, " ");
811 if (tp
->state_
== THREAD_RUNNING
)
812 ui_out_text (uiout
, "(running)\n");
815 /* The switch below puts us at the top of the stack (leaf
817 switch_to_thread (tp
->ptid
);
818 print_stack_frame (get_selected_frame (NULL
),
819 /* For MI output, print frame level. */
820 ui_out_is_mi_like_p (uiout
),
824 if (ui_out_is_mi_like_p (uiout
))
826 char *state
= "stopped";
827 if (tp
->state_
== THREAD_EXITED
)
829 else if (tp
->state_
== THREAD_RUNNING
)
831 ui_out_field_string (uiout
, "state", state
);
834 do_cleanups (chain2
);
837 /* Restores the current thread and the frame selected before
838 the "info threads" command. */
839 do_cleanups (old_chain
);
841 if (requested_thread
== -1)
843 gdb_assert (current_thread
!= -1
845 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
846 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
848 if (current_thread
!= -1 && is_exited (current_ptid
))
849 ui_out_message (uiout
, 0, "\n\
850 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
856 /* Print information about currently known threads
858 * Note: this has the drawback that it _really_ switches
859 * threads, which frees the frame cache. A no-side
860 * effects info-threads command would be nicer.
864 info_threads_command (char *arg
, int from_tty
)
866 print_thread_info (uiout
, -1);
869 /* Switch from one thread to another. */
872 switch_to_thread (ptid_t ptid
)
874 if (ptid_equal (ptid
, inferior_ptid
))
877 inferior_ptid
= ptid
;
878 reinit_frame_cache ();
879 registers_changed ();
881 /* We don't check for is_stopped, because we're called at times
882 while in the TARGET_RUNNING state, e.g., while handling an
884 if (!is_exited (ptid
) && !is_executing (ptid
))
885 stop_pc
= read_pc ();
887 stop_pc
= ~(CORE_ADDR
) 0;
891 restore_current_thread (ptid_t ptid
)
893 if (!ptid_equal (ptid
, inferior_ptid
))
896 context_switch_to (ptid
);
898 switch_to_thread (ptid
);
903 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
905 struct frame_info
*frame
= NULL
;
908 gdb_assert (frame_level
>= 0);
910 /* Restore by level first, check if the frame id is the same as
911 expected. If that fails, try restoring by frame id. If that
912 fails, nothing to do, just warn the user. */
915 frame
= find_relative_frame (get_current_frame (), &count
);
918 /* Either the frame ids match, of they're both invalid. The
919 latter case is not failsafe, but since it's highly unlikely
920 the search by level finds the wrong frame, it's 99.9(9)% of
921 the time (for all practical purposes) safe. */
922 && (frame_id_eq (get_frame_id (frame
), a_frame_id
)
923 /* Note: could be better to check every frame_id
924 member for equality here. */
925 || (!frame_id_p (get_frame_id (frame
))
926 && !frame_id_p (a_frame_id
))))
928 /* Cool, all is fine. */
929 select_frame (frame
);
933 frame
= frame_find_by_id (a_frame_id
);
936 /* Cool, refound it. */
937 select_frame (frame
);
941 /* Nothing else to do, the frame layout really changed. Select the
942 innermost stack frame. */
943 select_frame (get_current_frame ());
946 if (!ui_out_is_mi_like_p (uiout
))
949 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
951 /* For MI, we should probably have a notification about
952 current frame change. But this error is not very
953 likely, so don't bother for now. */
954 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
958 struct current_thread_cleanup
960 ptid_t inferior_ptid
;
961 struct frame_id selected_frame_id
;
962 int selected_frame_level
;
967 do_restore_current_thread_cleanup (void *arg
)
969 struct thread_info
*tp
;
970 struct current_thread_cleanup
*old
= arg
;
971 restore_current_thread (old
->inferior_ptid
);
973 /* The running state of the originally selected thread may have
974 changed, so we have to recheck it here. */
976 && is_stopped (inferior_ptid
)
977 && target_has_registers
979 && target_has_memory
)
980 restore_selected_frame (old
->selected_frame_id
,
981 old
->selected_frame_level
);
985 restore_current_thread_cleanup_dtor (void *arg
)
987 struct current_thread_cleanup
*old
= arg
;
988 struct thread_info
*tp
;
989 tp
= find_thread_pid (old
->inferior_ptid
);
996 make_cleanup_restore_current_thread (void)
998 struct thread_info
*tp
;
999 struct frame_info
*frame
;
1000 struct current_thread_cleanup
*old
;
1002 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1003 old
->inferior_ptid
= inferior_ptid
;
1004 old
->was_stopped
= is_stopped (inferior_ptid
);
1005 if (old
->was_stopped
1006 && target_has_registers
1008 && target_has_memory
)
1009 frame
= get_selected_frame (NULL
);
1013 old
->selected_frame_id
= get_frame_id (frame
);
1014 old
->selected_frame_level
= frame_relative_level (frame
);
1016 tp
= find_thread_pid (inferior_ptid
);
1020 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1021 restore_current_thread_cleanup_dtor
);
1024 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1025 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1026 of two numbers seperated by a hyphen. Examples:
1028 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1029 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1030 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1034 thread_apply_all_command (char *cmd
, int from_tty
)
1036 struct thread_info
*tp
;
1037 struct cleanup
*old_chain
;
1040 if (cmd
== NULL
|| *cmd
== '\000')
1041 error (_("Please specify a command following the thread ID list"));
1044 target_find_new_threads ();
1046 old_chain
= make_cleanup_restore_current_thread ();
1048 /* Save a copy of the command in case it is clobbered by
1050 saved_cmd
= xstrdup (cmd
);
1051 make_cleanup (xfree
, saved_cmd
);
1052 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1053 if (thread_alive (tp
))
1056 context_switch_to (tp
->ptid
);
1058 switch_to_thread (tp
->ptid
);
1060 printf_filtered (_("\nThread %d (%s):\n"),
1061 tp
->num
, target_tid_to_str (inferior_ptid
));
1062 execute_command (cmd
, from_tty
);
1063 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
1066 do_cleanups (old_chain
);
1070 thread_apply_command (char *tidlist
, int from_tty
)
1074 struct cleanup
*old_chain
;
1077 if (tidlist
== NULL
|| *tidlist
== '\000')
1078 error (_("Please specify a thread ID list"));
1080 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1083 error (_("Please specify a command following the thread ID list"));
1085 /* Save a copy of the command in case it is clobbered by
1087 saved_cmd
= xstrdup (cmd
);
1088 old_chain
= make_cleanup (xfree
, saved_cmd
);
1089 while (tidlist
< cmd
)
1091 struct thread_info
*tp
;
1094 start
= strtol (tidlist
, &p
, 10);
1096 error (_("Error parsing %s"), tidlist
);
1099 while (*tidlist
== ' ' || *tidlist
== '\t')
1102 if (*tidlist
== '-') /* Got a range of IDs? */
1104 tidlist
++; /* Skip the - */
1105 end
= strtol (tidlist
, &p
, 10);
1107 error (_("Error parsing %s"), tidlist
);
1110 while (*tidlist
== ' ' || *tidlist
== '\t')
1116 make_cleanup_restore_current_thread ();
1118 for (; start
<= end
; start
++)
1120 tp
= find_thread_id (start
);
1123 warning (_("Unknown thread %d."), start
);
1124 else if (!thread_alive (tp
))
1125 warning (_("Thread %d has terminated."), start
);
1129 context_switch_to (tp
->ptid
);
1131 switch_to_thread (tp
->ptid
);
1133 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1134 target_tid_to_str (inferior_ptid
));
1135 execute_command (cmd
, from_tty
);
1137 /* Restore exact command used previously. */
1138 strcpy (cmd
, saved_cmd
);
1143 do_cleanups (old_chain
);
1146 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1147 if prefix of arg is `apply'. */
1150 thread_command (char *tidstr
, int from_tty
)
1154 if (target_has_stack
)
1156 if (is_exited (inferior_ptid
))
1157 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1158 pid_to_thread_id (inferior_ptid
),
1159 target_tid_to_str (inferior_ptid
));
1161 printf_filtered (_("[Current thread is %d (%s)]\n"),
1162 pid_to_thread_id (inferior_ptid
),
1163 target_tid_to_str (inferior_ptid
));
1166 error (_("No stack."));
1170 annotate_thread_changed ();
1171 gdb_thread_select (uiout
, tidstr
, NULL
);
1174 /* Print notices when new threads are attached and detached. */
1175 int print_thread_events
= 1;
1177 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1178 struct cmd_list_element
*c
, const char *value
)
1180 fprintf_filtered (file
, _("\
1181 Printing of thread events is %s.\n"),
1186 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1189 struct thread_info
*tp
;
1191 num
= value_as_long (parse_and_eval (tidstr
));
1193 tp
= find_thread_id (num
);
1196 error (_("Thread ID %d not known."), num
);
1198 if (!thread_alive (tp
))
1199 error (_("Thread ID %d has terminated."), num
);
1202 context_switch_to (tp
->ptid
);
1204 switch_to_thread (tp
->ptid
);
1206 ui_out_text (uiout
, "[Switching to thread ");
1207 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1208 ui_out_text (uiout
, " (");
1209 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
1210 ui_out_text (uiout
, ")]");
1212 /* Note that we can't reach this with an exited thread, due to the
1213 thread_alive check above. */
1214 if (tp
->state_
== THREAD_RUNNING
)
1215 ui_out_text (uiout
, "(running)\n");
1217 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1219 /* Since the current thread may have changed, see if there is any
1220 exited thread we can now delete. */
1227 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1229 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1230 error_message
, RETURN_MASK_ALL
) < 0)
1235 /* Commands with a prefix of `thread'. */
1236 struct cmd_list_element
*thread_cmd_list
= NULL
;
1239 _initialize_thread (void)
1241 static struct cmd_list_element
*thread_apply_list
= NULL
;
1242 struct cmd_list_element
*c
;
1244 c
= add_info ("threads", info_threads_command
,
1245 _("IDs of currently known threads."));
1246 set_cmd_no_selected_thread_ok (c
);
1248 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1249 Use this command to switch between threads.\n\
1250 The new thread ID must be currently known."),
1251 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1252 set_cmd_no_selected_thread_ok (c
);
1254 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1255 _("Apply a command to a list of threads."),
1256 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1257 set_cmd_no_selected_thread_ok (c
);
1259 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
1260 _("Apply a command to all threads."), &thread_apply_list
);
1261 set_cmd_no_selected_thread_ok (c
);
1264 add_com_alias ("t", "thread", class_run
, 1);
1266 add_setshow_boolean_cmd ("thread-events", no_class
,
1267 &print_thread_events
, _("\
1268 Set printing of thread events (such as thread start and exit)."), _("\
1269 Show printing of thread events (such as thread start and exit)."), NULL
,
1271 show_print_thread_events
,
1272 &setprintlist
, &showprintlist
);