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, 2010, 2011
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"
46 #include "gdb_regex.h"
47 #include "cli/cli-utils.h"
49 /* Definition of struct thread_info exported to gdbthread.h. */
51 /* Prototypes for exported functions. */
53 void _initialize_thread (void);
55 /* Prototypes for local functions. */
57 static struct thread_info
*thread_list
= NULL
;
58 static int highest_thread_num
;
60 static void thread_command (char *tidstr
, int from_tty
);
61 static void thread_apply_all_command (char *, int);
62 static int thread_alive (struct thread_info
*);
63 static void info_threads_command (char *, int);
64 static void thread_apply_command (char *, int);
65 static void restore_current_thread (ptid_t
);
66 static void prune_threads (void);
68 /* Frontend view of the thread state. Possible extensions: stepping,
69 finishing, until(ling),... */
78 inferior_thread (void)
80 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
86 delete_step_resume_breakpoint (struct thread_info
*tp
)
88 if (tp
&& tp
->control
.step_resume_breakpoint
)
90 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
91 tp
->control
.step_resume_breakpoint
= NULL
;
96 delete_exception_resume_breakpoint (struct thread_info
*tp
)
98 if (tp
&& tp
->control
.exception_resume_breakpoint
)
100 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
101 tp
->control
.exception_resume_breakpoint
= NULL
;
106 clear_thread_inferior_resources (struct thread_info
*tp
)
108 /* NOTE: this will take care of any left-over step_resume breakpoints,
109 but not any user-specified thread-specific breakpoints. We can not
110 delete the breakpoint straight-off, because the inferior might not
111 be stopped at the moment. */
112 if (tp
->control
.step_resume_breakpoint
)
114 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
115 tp
->control
.step_resume_breakpoint
= NULL
;
118 if (tp
->control
.exception_resume_breakpoint
)
120 tp
->control
.exception_resume_breakpoint
->disposition
121 = disp_del_at_next_stop
;
122 tp
->control
.exception_resume_breakpoint
= NULL
;
125 bpstat_clear (&tp
->control
.stop_bpstat
);
127 discard_all_intermediate_continuations_thread (tp
);
128 discard_all_continuations_thread (tp
);
130 delete_longjmp_breakpoint (tp
->num
);
134 free_thread (struct thread_info
*tp
)
136 clear_thread_inferior_resources (tp
);
140 if (tp
->private_dtor
)
141 tp
->private_dtor (tp
->private);
151 init_thread_list (void)
153 struct thread_info
*tp
, *tpnext
;
155 highest_thread_num
= 0;
160 for (tp
= thread_list
; tp
; tp
= tpnext
)
169 /* Allocate a new thread with target id PTID and add it to the thread
172 static struct thread_info
*
173 new_thread (ptid_t ptid
)
175 struct thread_info
*tp
;
177 tp
= xcalloc (1, sizeof (*tp
));
180 tp
->num
= ++highest_thread_num
;
181 tp
->next
= thread_list
;
184 /* Nothing to follow yet. */
185 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
186 tp
->state_
= THREAD_STOPPED
;
192 add_thread_silent (ptid_t ptid
)
194 struct thread_info
*tp
;
196 tp
= find_thread_ptid (ptid
);
198 /* Found an old thread with the same id. It has to be dead,
199 otherwise we wouldn't be adding a new thread with the same id.
200 The OS is reusing this id --- delete it, and recreate a new
203 /* In addition to deleting the thread, if this is the current
204 thread, then we need to take care that delete_thread doesn't
205 really delete the thread if it is inferior_ptid. Create a
206 new template thread in the list with an invalid ptid, switch
207 to it, delete the original thread, reset the new thread's
208 ptid, and switch to it. */
210 if (ptid_equal (inferior_ptid
, ptid
))
212 tp
= new_thread (null_ptid
);
214 /* Make switch_to_thread not read from the thread. */
215 tp
->state_
= THREAD_EXITED
;
216 switch_to_thread (null_ptid
);
218 /* Now we can delete it. */
219 delete_thread (ptid
);
221 /* Now reset its ptid, and reswitch inferior_ptid to it. */
223 tp
->state_
= THREAD_STOPPED
;
224 switch_to_thread (ptid
);
226 observer_notify_new_thread (tp
);
232 /* Just go ahead and delete it. */
233 delete_thread (ptid
);
236 tp
= new_thread (ptid
);
237 observer_notify_new_thread (tp
);
243 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
245 struct thread_info
*result
= add_thread_silent (ptid
);
247 result
->private = private;
249 if (print_thread_events
)
250 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
252 annotate_new_thread ();
257 add_thread (ptid_t ptid
)
259 return add_thread_with_info (ptid
, NULL
);
262 /* Delete thread PTID. If SILENT, don't notify the observer of this
265 delete_thread_1 (ptid_t ptid
, int silent
)
267 struct thread_info
*tp
, *tpprev
;
271 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
272 if (ptid_equal (tp
->ptid
, ptid
))
278 /* If this is the current thread, or there's code out there that
279 relies on it existing (refcount > 0) we can't delete yet. Mark
280 it as exited, and notify it. */
282 || ptid_equal (tp
->ptid
, inferior_ptid
))
284 if (tp
->state_
!= THREAD_EXITED
)
286 observer_notify_thread_exit (tp
, silent
);
288 /* Tag it as exited. */
289 tp
->state_
= THREAD_EXITED
;
291 /* Clear breakpoints, etc. associated with this thread. */
292 clear_thread_inferior_resources (tp
);
295 /* Will be really deleted some other time. */
300 tpprev
->next
= tp
->next
;
302 thread_list
= tp
->next
;
304 /* Notify thread exit, but only if we haven't already. */
305 if (tp
->state_
!= THREAD_EXITED
)
306 observer_notify_thread_exit (tp
, silent
);
311 /* Delete thread PTID and notify of thread exit. If this is
312 inferior_ptid, don't actually delete it, but tag it as exited and
313 do the notification. If PTID is the user selected thread, clear
316 delete_thread (ptid_t ptid
)
318 delete_thread_1 (ptid
, 0 /* not silent */);
322 delete_thread_silent (ptid_t ptid
)
324 delete_thread_1 (ptid
, 1 /* silent */);
328 find_thread_id (int num
)
330 struct thread_info
*tp
;
332 for (tp
= thread_list
; tp
; tp
= tp
->next
)
339 /* Find a thread_info by matching PTID. */
341 find_thread_ptid (ptid_t ptid
)
343 struct thread_info
*tp
;
345 for (tp
= thread_list
; tp
; tp
= tp
->next
)
346 if (ptid_equal (tp
->ptid
, ptid
))
353 * Thread iterator function.
355 * Calls a callback function once for each thread, so long as
356 * the callback function returns false. If the callback function
357 * returns true, the iteration will end and the current thread
358 * will be returned. This can be useful for implementing a
359 * search for a thread with arbitrary attributes, or for applying
360 * some operation to every thread.
362 * FIXME: some of the existing functionality, such as
363 * "Thread apply all", might be rewritten using this functionality.
367 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
370 struct thread_info
*tp
, *next
;
372 for (tp
= thread_list
; tp
; tp
= next
)
375 if ((*callback
) (tp
, data
))
386 struct thread_info
*tp
;
388 for (tp
= thread_list
; tp
; tp
= tp
->next
)
395 valid_thread_id (int num
)
397 struct thread_info
*tp
;
399 for (tp
= thread_list
; tp
; tp
= tp
->next
)
407 pid_to_thread_id (ptid_t ptid
)
409 struct thread_info
*tp
;
411 for (tp
= thread_list
; tp
; tp
= tp
->next
)
412 if (ptid_equal (tp
->ptid
, ptid
))
419 thread_id_to_pid (int num
)
421 struct thread_info
*thread
= find_thread_id (num
);
426 return pid_to_ptid (-1);
430 in_thread_list (ptid_t ptid
)
432 struct thread_info
*tp
;
434 for (tp
= thread_list
; tp
; tp
= tp
->next
)
435 if (ptid_equal (tp
->ptid
, ptid
))
438 return 0; /* Never heard of 'im. */
441 /* Finds the first thread of the inferior given by PID. If PID is -1,
442 return the first thread in the list. */
445 first_thread_of_process (int pid
)
447 struct thread_info
*tp
, *ret
= NULL
;
449 for (tp
= thread_list
; tp
; tp
= tp
->next
)
450 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
451 if (ret
== NULL
|| tp
->num
< ret
->num
)
458 any_thread_of_process (int pid
)
460 struct thread_info
*tp
;
462 for (tp
= thread_list
; tp
; tp
= tp
->next
)
463 if (ptid_get_pid (tp
->ptid
) == pid
)
470 any_live_thread_of_process (int pid
)
472 struct thread_info
*tp
;
473 struct thread_info
*tp_running
= NULL
;
475 for (tp
= thread_list
; tp
; tp
= tp
->next
)
476 if (ptid_get_pid (tp
->ptid
) == pid
)
478 if (tp
->state_
== THREAD_STOPPED
)
480 else if (tp
->state_
== THREAD_RUNNING
)
487 /* Print a list of thread ids currently known, and the total number of
488 threads. To be used from within catch_errors. */
490 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
492 struct thread_info
*tp
;
494 struct cleanup
*cleanup_chain
;
495 int current_thread
= -1;
497 update_thread_list ();
499 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
501 for (tp
= thread_list
; tp
; tp
= tp
->next
)
503 if (tp
->state_
== THREAD_EXITED
)
506 if (ptid_equal (tp
->ptid
, inferior_ptid
))
507 current_thread
= tp
->num
;
510 ui_out_field_int (uiout
, "thread-id", tp
->num
);
513 do_cleanups (cleanup_chain
);
515 if (current_thread
!= -1)
516 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
517 ui_out_field_int (uiout
, "number-of-threads", num
);
521 /* Official gdblib interface function to get a list of thread ids and
524 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
526 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
527 error_message
, RETURN_MASK_ALL
) < 0)
532 /* Return true if TP is an active thread. */
534 thread_alive (struct thread_info
*tp
)
536 if (tp
->state_
== THREAD_EXITED
)
538 if (!target_thread_alive (tp
->ptid
))
546 struct thread_info
*tp
, *next
;
548 for (tp
= thread_list
; tp
; tp
= next
)
551 if (!thread_alive (tp
))
552 delete_thread (tp
->ptid
);
557 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
559 struct inferior
*inf
;
560 struct thread_info
*tp
;
562 /* It can happen that what we knew as the target inferior id
563 changes. E.g, target remote may only discover the remote process
564 pid after adding the inferior to GDB's list. */
565 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
566 inf
->pid
= ptid_get_pid (new_ptid
);
568 tp
= find_thread_ptid (old_ptid
);
571 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
575 set_running (ptid_t ptid
, int running
)
577 struct thread_info
*tp
;
578 int all
= ptid_equal (ptid
, minus_one_ptid
);
580 /* We try not to notify the observer if no thread has actually changed
581 the running state -- merely to reduce the number of messages to
582 frontend. Frontend is supposed to handle multiple *running just fine. */
583 if (all
|| ptid_is_pid (ptid
))
587 for (tp
= thread_list
; tp
; tp
= tp
->next
)
588 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
590 if (tp
->state_
== THREAD_EXITED
)
592 if (running
&& tp
->state_
== THREAD_STOPPED
)
594 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
597 observer_notify_target_resumed (ptid
);
603 tp
= find_thread_ptid (ptid
);
605 gdb_assert (tp
->state_
!= THREAD_EXITED
);
606 if (running
&& tp
->state_
== THREAD_STOPPED
)
608 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
610 observer_notify_target_resumed (ptid
);
615 is_thread_state (ptid_t ptid
, enum thread_state state
)
617 struct thread_info
*tp
;
619 tp
= find_thread_ptid (ptid
);
621 return tp
->state_
== state
;
625 is_stopped (ptid_t ptid
)
627 return is_thread_state (ptid
, THREAD_STOPPED
);
631 is_exited (ptid_t ptid
)
633 return is_thread_state (ptid
, THREAD_EXITED
);
637 is_running (ptid_t ptid
)
639 return is_thread_state (ptid
, THREAD_RUNNING
);
645 struct thread_info
*tp
;
647 for (tp
= thread_list
; tp
; tp
= tp
->next
)
648 if (tp
->state_
== THREAD_RUNNING
)
655 is_executing (ptid_t ptid
)
657 struct thread_info
*tp
;
659 tp
= find_thread_ptid (ptid
);
661 return tp
->executing_
;
665 set_executing (ptid_t ptid
, int executing
)
667 struct thread_info
*tp
;
668 int all
= ptid_equal (ptid
, minus_one_ptid
);
670 if (all
|| ptid_is_pid (ptid
))
672 for (tp
= thread_list
; tp
; tp
= tp
->next
)
673 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
674 tp
->executing_
= executing
;
678 tp
= find_thread_ptid (ptid
);
680 tp
->executing_
= executing
;
685 set_stop_requested (ptid_t ptid
, int stop
)
687 struct thread_info
*tp
;
688 int all
= ptid_equal (ptid
, minus_one_ptid
);
690 if (all
|| ptid_is_pid (ptid
))
692 for (tp
= thread_list
; tp
; tp
= tp
->next
)
693 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
694 tp
->stop_requested
= stop
;
698 tp
= find_thread_ptid (ptid
);
700 tp
->stop_requested
= stop
;
703 /* Call the stop requested observer so other components of GDB can
704 react to this request. */
706 observer_notify_thread_stop_requested (ptid
);
710 finish_thread_state (ptid_t ptid
)
712 struct thread_info
*tp
;
716 all
= ptid_equal (ptid
, minus_one_ptid
);
718 if (all
|| ptid_is_pid (ptid
))
720 for (tp
= thread_list
; tp
; tp
= tp
->next
)
722 if (tp
->state_
== THREAD_EXITED
)
724 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
726 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
728 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
734 tp
= find_thread_ptid (ptid
);
736 if (tp
->state_
!= THREAD_EXITED
)
738 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
740 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
745 observer_notify_target_resumed (ptid
);
749 finish_thread_state_cleanup (void *arg
)
751 ptid_t
*ptid_p
= arg
;
755 finish_thread_state (*ptid_p
);
758 /* Prints the list of threads and their details on UIOUT.
759 This is a version of 'info_threads_command' suitable for
761 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
762 that should be printed. Otherwise, all threads are
764 If PID is not -1, only print threads from the process PID.
765 Otherwise, threads from all attached PIDs are printed.
766 If both REQUESTED_THREAD and PID are not -1, then the thread
767 is printed if it belongs to the specified process. Otherwise,
768 an error is raised. */
770 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
772 struct thread_info
*tp
;
774 struct cleanup
*old_chain
;
775 char *extra_info
, *name
, *target_id
;
776 int current_thread
= -1;
778 update_thread_list ();
779 current_ptid
= inferior_ptid
;
781 /* We'll be switching threads temporarily. */
782 old_chain
= make_cleanup_restore_current_thread ();
784 /* For backward compatibility, we make a list for MI. A table is
785 preferable for the CLI, though, because it shows table
787 if (ui_out_is_mi_like_p (uiout
))
788 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
793 for (tp
= thread_list
; tp
; tp
= tp
->next
)
795 if (!number_is_in_list (requested_threads
, tp
->num
))
798 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
801 if (tp
->state_
== THREAD_EXITED
)
809 if (requested_threads
== NULL
|| *requested_threads
== '\0')
810 ui_out_message (uiout
, 0, _("No threads.\n"));
812 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
814 do_cleanups (old_chain
);
818 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
820 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
821 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
822 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
823 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
824 ui_out_table_body (uiout
);
827 for (tp
= thread_list
; tp
; tp
= tp
->next
)
829 struct cleanup
*chain2
;
832 if (!number_is_in_list (requested_threads
, tp
->num
))
835 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
837 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
838 error (_("Requested thread not found in requested process"));
842 if (ptid_equal (tp
->ptid
, current_ptid
))
843 current_thread
= tp
->num
;
845 if (tp
->state_
== THREAD_EXITED
)
848 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
850 if (ui_out_is_mi_like_p (uiout
))
853 if (ptid_equal (tp
->ptid
, current_ptid
))
854 ui_out_text (uiout
, "* ");
856 ui_out_text (uiout
, " ");
860 if (ptid_equal (tp
->ptid
, current_ptid
))
861 ui_out_field_string (uiout
, "current", "*");
863 ui_out_field_skip (uiout
, "current");
866 ui_out_field_int (uiout
, "id", tp
->num
);
868 /* For the CLI, we stuff everything into the target-id field.
869 This is a gross hack to make the output come out looking
870 correct. The underlying problem here is that ui-out has no
871 way to specify that a field's space allocation should be
872 shared by several fields. For MI, we do the right thing
875 target_id
= target_pid_to_str (tp
->ptid
);
876 extra_info
= target_extra_thread_info (tp
);
877 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
879 if (ui_out_is_mi_like_p (uiout
))
881 ui_out_field_string (uiout
, "target-id", target_id
);
883 ui_out_field_string (uiout
, "details", extra_info
);
885 ui_out_field_string (uiout
, "name", name
);
889 struct cleanup
*str_cleanup
;
892 if (extra_info
&& name
)
893 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
896 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
898 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
900 contents
= xstrdup (target_id
);
901 str_cleanup
= make_cleanup (xfree
, contents
);
903 ui_out_field_string (uiout
, "target-id", contents
);
904 do_cleanups (str_cleanup
);
907 if (tp
->state_
== THREAD_RUNNING
)
908 ui_out_text (uiout
, "(running)\n");
911 /* The switch below puts us at the top of the stack (leaf
913 switch_to_thread (tp
->ptid
);
914 print_stack_frame (get_selected_frame (NULL
),
915 /* For MI output, print frame level. */
916 ui_out_is_mi_like_p (uiout
),
920 if (ui_out_is_mi_like_p (uiout
))
922 char *state
= "stopped";
924 if (tp
->state_
== THREAD_RUNNING
)
926 ui_out_field_string (uiout
, "state", state
);
929 core
= target_core_of_thread (tp
->ptid
);
930 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
931 ui_out_field_int (uiout
, "core", core
);
933 do_cleanups (chain2
);
936 /* Restores the current thread and the frame selected before
937 the "info threads" command. */
938 do_cleanups (old_chain
);
940 if (pid
== -1 && requested_threads
== NULL
)
942 gdb_assert (current_thread
!= -1
944 || ptid_equal (inferior_ptid
, null_ptid
));
945 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
946 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
948 if (current_thread
!= -1 && is_exited (current_ptid
))
949 ui_out_message (uiout
, 0, "\n\
950 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
953 && current_thread
== -1
954 && ptid_equal (current_ptid
, null_ptid
))
955 ui_out_message (uiout
, 0, "\n\
956 No selected thread. See `help thread'.\n");
960 /* Print information about currently known threads
962 Optional ARG is a thread id, or list of thread ids.
964 Note: this has the drawback that it _really_ switches
965 threads, which frees the frame cache. A no-side
966 effects info-threads command would be nicer. */
969 info_threads_command (char *arg
, int from_tty
)
971 print_thread_info (uiout
, arg
, -1);
974 /* Switch from one thread to another. */
977 switch_to_thread (ptid_t ptid
)
979 /* Switch the program space as well, if we can infer it from the now
980 current thread. Otherwise, it's up to the caller to select the
982 if (!ptid_equal (ptid
, null_ptid
))
984 struct inferior
*inf
;
986 inf
= find_inferior_pid (ptid_get_pid (ptid
));
987 gdb_assert (inf
!= NULL
);
988 set_current_program_space (inf
->pspace
);
989 set_current_inferior (inf
);
992 if (ptid_equal (ptid
, inferior_ptid
))
995 inferior_ptid
= ptid
;
996 reinit_frame_cache ();
997 registers_changed ();
999 /* We don't check for is_stopped, because we're called at times
1000 while in the TARGET_RUNNING state, e.g., while handling an
1002 if (!ptid_equal (inferior_ptid
, null_ptid
)
1003 && !is_exited (ptid
)
1004 && !is_executing (ptid
))
1005 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1007 stop_pc
= ~(CORE_ADDR
) 0;
1011 restore_current_thread (ptid_t ptid
)
1013 switch_to_thread (ptid
);
1017 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1019 struct frame_info
*frame
= NULL
;
1022 gdb_assert (frame_level
>= 0);
1024 /* Restore by level first, check if the frame id is the same as
1025 expected. If that fails, try restoring by frame id. If that
1026 fails, nothing to do, just warn the user. */
1028 count
= frame_level
;
1029 frame
= find_relative_frame (get_current_frame (), &count
);
1032 /* The frame ids must match - either both valid or both outer_frame_id.
1033 The latter case is not failsafe, but since it's highly unlikely
1034 the search by level finds the wrong frame, it's 99.9(9)% of
1035 the time (for all practical purposes) safe. */
1036 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1038 /* Cool, all is fine. */
1039 select_frame (frame
);
1043 frame
= frame_find_by_id (a_frame_id
);
1046 /* Cool, refound it. */
1047 select_frame (frame
);
1051 /* Nothing else to do, the frame layout really changed. Select the
1052 innermost stack frame. */
1053 select_frame (get_current_frame ());
1055 /* Warn the user. */
1056 if (frame_level
> 0 && !ui_out_is_mi_like_p (uiout
))
1058 warning (_("Couldn't restore frame #%d in "
1059 "current thread, at reparsed frame #0\n"),
1061 /* For MI, we should probably have a notification about
1062 current frame change. But this error is not very
1063 likely, so don't bother for now. */
1064 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1068 struct current_thread_cleanup
1070 ptid_t inferior_ptid
;
1071 struct frame_id selected_frame_id
;
1072 int selected_frame_level
;
1078 do_restore_current_thread_cleanup (void *arg
)
1080 struct thread_info
*tp
;
1081 struct current_thread_cleanup
*old
= arg
;
1083 tp
= find_thread_ptid (old
->inferior_ptid
);
1085 /* If the previously selected thread belonged to a process that has
1086 in the mean time been deleted (due to normal exit, detach, etc.),
1087 then don't revert back to it, but instead simply drop back to no
1090 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1091 restore_current_thread (old
->inferior_ptid
);
1094 restore_current_thread (null_ptid
);
1095 set_current_inferior (find_inferior_id (old
->inf_id
));
1098 /* The running state of the originally selected thread may have
1099 changed, so we have to recheck it here. */
1100 if (!ptid_equal (inferior_ptid
, null_ptid
)
1102 && is_stopped (inferior_ptid
)
1103 && target_has_registers
1105 && target_has_memory
)
1106 restore_selected_frame (old
->selected_frame_id
,
1107 old
->selected_frame_level
);
1111 restore_current_thread_cleanup_dtor (void *arg
)
1113 struct current_thread_cleanup
*old
= arg
;
1114 struct thread_info
*tp
;
1116 tp
= find_thread_ptid (old
->inferior_ptid
);
1123 make_cleanup_restore_current_thread (void)
1125 struct thread_info
*tp
;
1126 struct frame_info
*frame
;
1127 struct current_thread_cleanup
*old
;
1129 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1130 old
->inferior_ptid
= inferior_ptid
;
1131 old
->inf_id
= current_inferior ()->num
;
1133 if (!ptid_equal (inferior_ptid
, null_ptid
))
1135 old
->was_stopped
= is_stopped (inferior_ptid
);
1136 if (old
->was_stopped
1137 && target_has_registers
1139 && target_has_memory
)
1140 frame
= get_selected_frame (NULL
);
1144 old
->selected_frame_id
= get_frame_id (frame
);
1145 old
->selected_frame_level
= frame_relative_level (frame
);
1147 tp
= find_thread_ptid (inferior_ptid
);
1152 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1153 restore_current_thread_cleanup_dtor
);
1156 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1157 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1158 of two numbers seperated by a hyphen. Examples:
1160 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1161 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1162 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1165 thread_apply_all_command (char *cmd
, int from_tty
)
1167 struct thread_info
*tp
;
1168 struct cleanup
*old_chain
;
1171 if (cmd
== NULL
|| *cmd
== '\000')
1172 error (_("Please specify a command following the thread ID list"));
1174 update_thread_list ();
1176 old_chain
= make_cleanup_restore_current_thread ();
1178 /* Save a copy of the command in case it is clobbered by
1180 saved_cmd
= xstrdup (cmd
);
1181 make_cleanup (xfree
, saved_cmd
);
1182 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1183 if (thread_alive (tp
))
1185 switch_to_thread (tp
->ptid
);
1187 printf_filtered (_("\nThread %d (%s):\n"),
1188 tp
->num
, target_pid_to_str (inferior_ptid
));
1189 execute_command (cmd
, from_tty
);
1190 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1194 do_cleanups (old_chain
);
1198 thread_apply_command (char *tidlist
, int from_tty
)
1201 struct cleanup
*old_chain
;
1204 if (tidlist
== NULL
|| *tidlist
== '\000')
1205 error (_("Please specify a thread ID list"));
1207 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1210 error (_("Please specify a command following the thread ID list"));
1212 /* Save a copy of the command in case it is clobbered by
1214 saved_cmd
= xstrdup (cmd
);
1215 old_chain
= make_cleanup (xfree
, saved_cmd
);
1216 while (tidlist
< cmd
)
1218 struct thread_info
*tp
;
1222 start
= get_number_or_range (&tidlist
);
1224 make_cleanup_restore_current_thread ();
1226 tp
= find_thread_id (start
);
1229 warning (_("Unknown thread %d."), start
);
1230 else if (!thread_alive (tp
))
1231 warning (_("Thread %d has terminated."), start
);
1234 switch_to_thread (tp
->ptid
);
1236 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1237 target_pid_to_str (inferior_ptid
));
1238 execute_command (cmd
, from_tty
);
1240 /* Restore exact command used previously. */
1241 strcpy (cmd
, saved_cmd
);
1245 do_cleanups (old_chain
);
1248 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1249 if prefix of arg is `apply'. */
1252 thread_command (char *tidstr
, int from_tty
)
1256 if (ptid_equal (inferior_ptid
, null_ptid
))
1257 error (_("No thread selected"));
1259 if (target_has_stack
)
1261 if (is_exited (inferior_ptid
))
1262 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1263 pid_to_thread_id (inferior_ptid
),
1264 target_pid_to_str (inferior_ptid
));
1266 printf_filtered (_("[Current thread is %d (%s)]\n"),
1267 pid_to_thread_id (inferior_ptid
),
1268 target_pid_to_str (inferior_ptid
));
1271 error (_("No stack."));
1275 gdb_thread_select (uiout
, tidstr
, NULL
);
1278 /* Implementation of `thread name'. */
1281 thread_name_command (char *arg
, int from_tty
)
1283 struct thread_info
*info
;
1285 if (ptid_equal (inferior_ptid
, null_ptid
))
1286 error (_("No thread selected"));
1288 while (arg
&& isspace (*arg
))
1291 info
= inferior_thread ();
1293 info
->name
= arg
? xstrdup (arg
) : NULL
;
1296 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1299 thread_find_command (char *arg
, int from_tty
)
1301 struct thread_info
*tp
;
1303 unsigned long match
= 0;
1305 if (arg
== NULL
|| *arg
== '\0')
1306 error (_("Command requires an argument."));
1308 tmp
= re_comp (arg
);
1310 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1312 update_thread_list ();
1313 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1315 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1317 printf_filtered (_("Thread %d has name '%s'\n"),
1322 tmp
= target_thread_name (tp
);
1323 if (tmp
!= NULL
&& re_exec (tmp
))
1325 printf_filtered (_("Thread %d has target name '%s'\n"),
1330 tmp
= target_pid_to_str (tp
->ptid
);
1331 if (tmp
!= NULL
&& re_exec (tmp
))
1333 printf_filtered (_("Thread %d has target id '%s'\n"),
1338 tmp
= target_extra_thread_info (tp
);
1339 if (tmp
!= NULL
&& re_exec (tmp
))
1341 printf_filtered (_("Thread %d has extra info '%s'\n"),
1347 printf_filtered (_("No threads match '%s'\n"), arg
);
1350 /* Print notices when new threads are attached and detached. */
1351 int print_thread_events
= 1;
1353 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1354 struct cmd_list_element
*c
, const char *value
)
1356 fprintf_filtered (file
,
1357 _("Printing of thread events is %s.\n"),
1362 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1365 struct thread_info
*tp
;
1367 num
= value_as_long (parse_and_eval (tidstr
));
1369 tp
= find_thread_id (num
);
1372 error (_("Thread ID %d not known."), num
);
1374 if (!thread_alive (tp
))
1375 error (_("Thread ID %d has terminated."), num
);
1377 switch_to_thread (tp
->ptid
);
1379 annotate_thread_changed ();
1381 ui_out_text (uiout
, "[Switching to thread ");
1382 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1383 ui_out_text (uiout
, " (");
1384 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1385 ui_out_text (uiout
, ")]");
1387 /* Note that we can't reach this with an exited thread, due to the
1388 thread_alive check above. */
1389 if (tp
->state_
== THREAD_RUNNING
)
1390 ui_out_text (uiout
, "(running)\n");
1393 ui_out_text (uiout
, "\n");
1394 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1397 /* Since the current thread may have changed, see if there is any
1398 exited thread we can now delete. */
1405 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1407 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1408 error_message
, RETURN_MASK_ALL
) < 0)
1414 update_thread_list (void)
1417 target_find_new_threads ();
1420 /* Return a new value for the selected thread's id. Return a value of 0 if
1421 no thread is selected, or no threads exist. */
1423 static struct value
*
1424 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1426 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1428 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1429 (tp
? tp
->num
: 0));
1432 /* Commands with a prefix of `thread'. */
1433 struct cmd_list_element
*thread_cmd_list
= NULL
;
1436 _initialize_thread (void)
1438 static struct cmd_list_element
*thread_apply_list
= NULL
;
1440 add_info ("threads", info_threads_command
,
1441 _("Display currently known threads.\n\
1442 Usage: info threads [ID]...\n\
1443 Optional arguments are thread IDs with spaces between.\n\
1444 If no arguments, all threads are displayed."));
1446 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1447 Use this command to switch between threads.\n\
1448 The new thread ID must be currently known."),
1449 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1451 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1452 _("Apply a command to a list of threads."),
1453 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1455 add_cmd ("all", class_run
, thread_apply_all_command
,
1456 _("Apply a command to all threads."), &thread_apply_list
);
1458 add_cmd ("name", class_run
, thread_name_command
,
1459 _("Set the current thread's name.\n\
1460 Usage: thread name [NAME]\n\
1461 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1463 add_cmd ("find", class_run
, thread_find_command
, _("\
1464 Find threads that match a regular expression.\n\
1465 Usage: thread find REGEXP\n\
1466 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1470 add_com_alias ("t", "thread", class_run
, 1);
1472 add_setshow_boolean_cmd ("thread-events", no_class
,
1473 &print_thread_events
, _("\
1474 Set printing of thread events (such as thread start and exit)."), _("\
1475 Show printing of thread events (such as thread start and exit)."), NULL
,
1477 show_print_thread_events
,
1478 &setprintlist
, &showprintlist
);
1480 create_internalvar_type_lazy ("_thread", thread_id_make_value
);