1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-1988, 1993-2004, 2007-2012 Free Software
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"
45 #include "gdb_regex.h"
46 #include "cli/cli-utils.h"
47 #include "continuations.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 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);
69 inferior_thread (void)
71 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
77 delete_step_resume_breakpoint (struct thread_info
*tp
)
79 if (tp
&& tp
->control
.step_resume_breakpoint
)
81 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
82 tp
->control
.step_resume_breakpoint
= NULL
;
87 delete_exception_resume_breakpoint (struct thread_info
*tp
)
89 if (tp
&& tp
->control
.exception_resume_breakpoint
)
91 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
92 tp
->control
.exception_resume_breakpoint
= NULL
;
97 clear_thread_inferior_resources (struct thread_info
*tp
)
99 /* NOTE: this will take care of any left-over step_resume breakpoints,
100 but not any user-specified thread-specific breakpoints. We can not
101 delete the breakpoint straight-off, because the inferior might not
102 be stopped at the moment. */
103 if (tp
->control
.step_resume_breakpoint
)
105 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
106 tp
->control
.step_resume_breakpoint
= NULL
;
109 if (tp
->control
.exception_resume_breakpoint
)
111 tp
->control
.exception_resume_breakpoint
->disposition
112 = disp_del_at_next_stop
;
113 tp
->control
.exception_resume_breakpoint
= NULL
;
116 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
118 bpstat_clear (&tp
->control
.stop_bpstat
);
120 do_all_intermediate_continuations_thread (tp
, 1);
121 do_all_continuations_thread (tp
, 1);
125 free_thread (struct thread_info
*tp
)
129 if (tp
->private_dtor
)
130 tp
->private_dtor (tp
->private);
140 init_thread_list (void)
142 struct thread_info
*tp
, *tpnext
;
144 highest_thread_num
= 0;
149 for (tp
= thread_list
; tp
; tp
= tpnext
)
158 /* Allocate a new thread with target id PTID and add it to the thread
161 static struct thread_info
*
162 new_thread (ptid_t ptid
)
164 struct thread_info
*tp
;
166 tp
= xcalloc (1, sizeof (*tp
));
169 tp
->num
= ++highest_thread_num
;
170 tp
->next
= thread_list
;
173 /* Nothing to follow yet. */
174 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
175 tp
->state
= THREAD_STOPPED
;
181 add_thread_silent (ptid_t ptid
)
183 struct thread_info
*tp
;
185 tp
= find_thread_ptid (ptid
);
187 /* Found an old thread with the same id. It has to be dead,
188 otherwise we wouldn't be adding a new thread with the same id.
189 The OS is reusing this id --- delete it, and recreate a new
192 /* In addition to deleting the thread, if this is the current
193 thread, then we need to take care that delete_thread doesn't
194 really delete the thread if it is inferior_ptid. Create a
195 new template thread in the list with an invalid ptid, switch
196 to it, delete the original thread, reset the new thread's
197 ptid, and switch to it. */
199 if (ptid_equal (inferior_ptid
, ptid
))
201 tp
= new_thread (null_ptid
);
203 /* Make switch_to_thread not read from the thread. */
204 tp
->state
= THREAD_EXITED
;
205 switch_to_thread (null_ptid
);
207 /* Now we can delete it. */
208 delete_thread (ptid
);
210 /* Now reset its ptid, and reswitch inferior_ptid to it. */
212 tp
->state
= THREAD_STOPPED
;
213 switch_to_thread (ptid
);
215 observer_notify_new_thread (tp
);
221 /* Just go ahead and delete it. */
222 delete_thread (ptid
);
225 tp
= new_thread (ptid
);
226 observer_notify_new_thread (tp
);
232 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
234 struct thread_info
*result
= add_thread_silent (ptid
);
236 result
->private = private;
238 if (print_thread_events
)
239 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
241 annotate_new_thread ();
246 add_thread (ptid_t ptid
)
248 return add_thread_with_info (ptid
, NULL
);
251 /* Delete thread PTID. If SILENT, don't notify the observer of this
254 delete_thread_1 (ptid_t ptid
, int silent
)
256 struct thread_info
*tp
, *tpprev
;
260 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
261 if (ptid_equal (tp
->ptid
, ptid
))
267 /* If this is the current thread, or there's code out there that
268 relies on it existing (refcount > 0) we can't delete yet. Mark
269 it as exited, and notify it. */
271 || ptid_equal (tp
->ptid
, inferior_ptid
))
273 if (tp
->state
!= THREAD_EXITED
)
275 observer_notify_thread_exit (tp
, silent
);
277 /* Tag it as exited. */
278 tp
->state
= THREAD_EXITED
;
280 /* Clear breakpoints, etc. associated with this thread. */
281 clear_thread_inferior_resources (tp
);
284 /* Will be really deleted some other time. */
288 /* Notify thread exit, but only if we haven't already. */
289 if (tp
->state
!= THREAD_EXITED
)
290 observer_notify_thread_exit (tp
, silent
);
292 /* Tag it as exited. */
293 tp
->state
= THREAD_EXITED
;
294 clear_thread_inferior_resources (tp
);
297 tpprev
->next
= tp
->next
;
299 thread_list
= tp
->next
;
304 /* Delete thread PTID and notify of thread exit. If this is
305 inferior_ptid, don't actually delete it, but tag it as exited and
306 do the notification. If PTID is the user selected thread, clear
309 delete_thread (ptid_t ptid
)
311 delete_thread_1 (ptid
, 0 /* not silent */);
315 delete_thread_silent (ptid_t ptid
)
317 delete_thread_1 (ptid
, 1 /* silent */);
321 find_thread_id (int num
)
323 struct thread_info
*tp
;
325 for (tp
= thread_list
; tp
; tp
= tp
->next
)
332 /* Find a thread_info by matching PTID. */
334 find_thread_ptid (ptid_t ptid
)
336 struct thread_info
*tp
;
338 for (tp
= thread_list
; tp
; tp
= tp
->next
)
339 if (ptid_equal (tp
->ptid
, ptid
))
346 * Thread iterator function.
348 * Calls a callback function once for each thread, so long as
349 * the callback function returns false. If the callback function
350 * returns true, the iteration will end and the current thread
351 * will be returned. This can be useful for implementing a
352 * search for a thread with arbitrary attributes, or for applying
353 * some operation to every thread.
355 * FIXME: some of the existing functionality, such as
356 * "Thread apply all", might be rewritten using this functionality.
360 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
363 struct thread_info
*tp
, *next
;
365 for (tp
= thread_list
; tp
; tp
= next
)
368 if ((*callback
) (tp
, data
))
379 struct thread_info
*tp
;
381 for (tp
= thread_list
; tp
; tp
= tp
->next
)
388 valid_thread_id (int num
)
390 struct thread_info
*tp
;
392 for (tp
= thread_list
; tp
; tp
= tp
->next
)
400 pid_to_thread_id (ptid_t ptid
)
402 struct thread_info
*tp
;
404 for (tp
= thread_list
; tp
; tp
= tp
->next
)
405 if (ptid_equal (tp
->ptid
, ptid
))
412 thread_id_to_pid (int num
)
414 struct thread_info
*thread
= find_thread_id (num
);
419 return pid_to_ptid (-1);
423 in_thread_list (ptid_t ptid
)
425 struct thread_info
*tp
;
427 for (tp
= thread_list
; tp
; tp
= tp
->next
)
428 if (ptid_equal (tp
->ptid
, ptid
))
431 return 0; /* Never heard of 'im. */
434 /* Finds the first thread of the inferior given by PID. If PID is -1,
435 return the first thread in the list. */
438 first_thread_of_process (int pid
)
440 struct thread_info
*tp
, *ret
= NULL
;
442 for (tp
= thread_list
; tp
; tp
= tp
->next
)
443 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
444 if (ret
== NULL
|| tp
->num
< ret
->num
)
451 any_thread_of_process (int pid
)
453 struct thread_info
*tp
;
455 for (tp
= thread_list
; tp
; tp
= tp
->next
)
456 if (ptid_get_pid (tp
->ptid
) == pid
)
463 any_live_thread_of_process (int pid
)
465 struct thread_info
*tp
;
466 struct thread_info
*tp_executing
= NULL
;
468 for (tp
= thread_list
; tp
; tp
= tp
->next
)
469 if (tp
->state
!= THREAD_EXITED
&& ptid_get_pid (tp
->ptid
) == pid
)
480 /* Print a list of thread ids currently known, and the total number of
481 threads. To be used from within catch_errors. */
483 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
485 struct thread_info
*tp
;
487 struct cleanup
*cleanup_chain
;
488 int current_thread
= -1;
490 update_thread_list ();
492 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
494 for (tp
= thread_list
; tp
; tp
= tp
->next
)
496 if (tp
->state
== THREAD_EXITED
)
499 if (ptid_equal (tp
->ptid
, inferior_ptid
))
500 current_thread
= tp
->num
;
503 ui_out_field_int (uiout
, "thread-id", tp
->num
);
506 do_cleanups (cleanup_chain
);
508 if (current_thread
!= -1)
509 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
510 ui_out_field_int (uiout
, "number-of-threads", num
);
514 /* Official gdblib interface function to get a list of thread ids and
517 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
519 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
520 error_message
, RETURN_MASK_ALL
) < 0)
525 /* Return true if TP is an active thread. */
527 thread_alive (struct thread_info
*tp
)
529 if (tp
->state
== THREAD_EXITED
)
531 if (!target_thread_alive (tp
->ptid
))
539 struct thread_info
*tp
, *next
;
541 for (tp
= thread_list
; tp
; tp
= next
)
544 if (!thread_alive (tp
))
545 delete_thread (tp
->ptid
);
550 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
552 struct inferior
*inf
;
553 struct thread_info
*tp
;
555 /* It can happen that what we knew as the target inferior id
556 changes. E.g, target remote may only discover the remote process
557 pid after adding the inferior to GDB's list. */
558 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
559 inf
->pid
= ptid_get_pid (new_ptid
);
561 tp
= find_thread_ptid (old_ptid
);
564 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
568 set_running (ptid_t ptid
, int running
)
570 struct thread_info
*tp
;
571 int all
= ptid_equal (ptid
, minus_one_ptid
);
573 /* We try not to notify the observer if no thread has actually changed
574 the running state -- merely to reduce the number of messages to
575 frontend. Frontend is supposed to handle multiple *running just fine. */
576 if (all
|| ptid_is_pid (ptid
))
580 for (tp
= thread_list
; tp
; tp
= tp
->next
)
581 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
583 if (tp
->state
== THREAD_EXITED
)
585 if (running
&& tp
->state
== THREAD_STOPPED
)
587 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
590 observer_notify_target_resumed (ptid
);
596 tp
= find_thread_ptid (ptid
);
598 gdb_assert (tp
->state
!= THREAD_EXITED
);
599 if (running
&& tp
->state
== THREAD_STOPPED
)
601 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
603 observer_notify_target_resumed (ptid
);
608 is_thread_state (ptid_t ptid
, enum thread_state state
)
610 struct thread_info
*tp
;
612 tp
= find_thread_ptid (ptid
);
614 return tp
->state
== state
;
618 is_stopped (ptid_t ptid
)
620 return is_thread_state (ptid
, THREAD_STOPPED
);
624 is_exited (ptid_t ptid
)
626 return is_thread_state (ptid
, THREAD_EXITED
);
630 is_running (ptid_t ptid
)
632 return is_thread_state (ptid
, THREAD_RUNNING
);
638 struct thread_info
*tp
;
640 for (tp
= thread_list
; tp
; tp
= tp
->next
)
641 if (tp
->state
== THREAD_RUNNING
)
648 is_executing (ptid_t ptid
)
650 struct thread_info
*tp
;
652 tp
= find_thread_ptid (ptid
);
654 return tp
->executing
;
658 set_executing (ptid_t ptid
, int executing
)
660 struct thread_info
*tp
;
661 int all
= ptid_equal (ptid
, minus_one_ptid
);
663 if (all
|| ptid_is_pid (ptid
))
665 for (tp
= thread_list
; tp
; tp
= tp
->next
)
666 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
667 tp
->executing
= executing
;
671 tp
= find_thread_ptid (ptid
);
673 tp
->executing
= executing
;
678 set_stop_requested (ptid_t ptid
, int stop
)
680 struct thread_info
*tp
;
681 int all
= ptid_equal (ptid
, minus_one_ptid
);
683 if (all
|| ptid_is_pid (ptid
))
685 for (tp
= thread_list
; tp
; tp
= tp
->next
)
686 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
687 tp
->stop_requested
= stop
;
691 tp
= find_thread_ptid (ptid
);
693 tp
->stop_requested
= stop
;
696 /* Call the stop requested observer so other components of GDB can
697 react to this request. */
699 observer_notify_thread_stop_requested (ptid
);
703 finish_thread_state (ptid_t ptid
)
705 struct thread_info
*tp
;
709 all
= ptid_equal (ptid
, minus_one_ptid
);
711 if (all
|| ptid_is_pid (ptid
))
713 for (tp
= thread_list
; tp
; tp
= tp
->next
)
715 if (tp
->state
== THREAD_EXITED
)
717 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
719 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
721 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
727 tp
= find_thread_ptid (ptid
);
729 if (tp
->state
!= THREAD_EXITED
)
731 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
733 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
738 observer_notify_target_resumed (ptid
);
742 finish_thread_state_cleanup (void *arg
)
744 ptid_t
*ptid_p
= arg
;
748 finish_thread_state (*ptid_p
);
751 /* Prints the list of threads and their details on UIOUT.
752 This is a version of 'info_threads_command' suitable for
754 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
755 that should be printed. Otherwise, all threads are
757 If PID is not -1, only print threads from the process PID.
758 Otherwise, threads from all attached PIDs are printed.
759 If both REQUESTED_THREAD and PID are not -1, then the thread
760 is printed if it belongs to the specified process. Otherwise,
761 an error is raised. */
763 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
765 struct thread_info
*tp
;
767 struct cleanup
*old_chain
;
768 char *extra_info
, *name
, *target_id
;
769 int current_thread
= -1;
771 update_thread_list ();
772 current_ptid
= inferior_ptid
;
774 /* We'll be switching threads temporarily. */
775 old_chain
= make_cleanup_restore_current_thread ();
777 /* For backward compatibility, we make a list for MI. A table is
778 preferable for the CLI, though, because it shows table
780 if (ui_out_is_mi_like_p (uiout
))
781 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
786 for (tp
= thread_list
; tp
; tp
= tp
->next
)
788 if (!number_is_in_list (requested_threads
, tp
->num
))
791 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
794 if (tp
->state
== THREAD_EXITED
)
802 if (requested_threads
== NULL
|| *requested_threads
== '\0')
803 ui_out_message (uiout
, 0, _("No threads.\n"));
805 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
807 do_cleanups (old_chain
);
811 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
813 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
814 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
815 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
816 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
817 ui_out_table_body (uiout
);
820 for (tp
= thread_list
; tp
; tp
= tp
->next
)
822 struct cleanup
*chain2
;
825 if (!number_is_in_list (requested_threads
, tp
->num
))
828 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
830 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
831 error (_("Requested thread not found in requested process"));
835 if (ptid_equal (tp
->ptid
, current_ptid
))
836 current_thread
= tp
->num
;
838 if (tp
->state
== THREAD_EXITED
)
841 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
843 if (ui_out_is_mi_like_p (uiout
))
846 if (ptid_equal (tp
->ptid
, current_ptid
))
847 ui_out_text (uiout
, "* ");
849 ui_out_text (uiout
, " ");
853 if (ptid_equal (tp
->ptid
, current_ptid
))
854 ui_out_field_string (uiout
, "current", "*");
856 ui_out_field_skip (uiout
, "current");
859 ui_out_field_int (uiout
, "id", tp
->num
);
861 /* For the CLI, we stuff everything into the target-id field.
862 This is a gross hack to make the output come out looking
863 correct. The underlying problem here is that ui-out has no
864 way to specify that a field's space allocation should be
865 shared by several fields. For MI, we do the right thing
868 target_id
= target_pid_to_str (tp
->ptid
);
869 extra_info
= target_extra_thread_info (tp
);
870 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
872 if (ui_out_is_mi_like_p (uiout
))
874 ui_out_field_string (uiout
, "target-id", target_id
);
876 ui_out_field_string (uiout
, "details", extra_info
);
878 ui_out_field_string (uiout
, "name", name
);
882 struct cleanup
*str_cleanup
;
885 if (extra_info
&& name
)
886 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
889 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
891 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
893 contents
= xstrdup (target_id
);
894 str_cleanup
= make_cleanup (xfree
, contents
);
896 ui_out_field_string (uiout
, "target-id", contents
);
897 do_cleanups (str_cleanup
);
900 if (tp
->state
== THREAD_RUNNING
)
901 ui_out_text (uiout
, "(running)\n");
904 /* The switch below puts us at the top of the stack (leaf
906 switch_to_thread (tp
->ptid
);
907 print_stack_frame (get_selected_frame (NULL
),
908 /* For MI output, print frame level. */
909 ui_out_is_mi_like_p (uiout
),
913 if (ui_out_is_mi_like_p (uiout
))
915 char *state
= "stopped";
917 if (tp
->state
== THREAD_RUNNING
)
919 ui_out_field_string (uiout
, "state", state
);
922 core
= target_core_of_thread (tp
->ptid
);
923 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
924 ui_out_field_int (uiout
, "core", core
);
926 do_cleanups (chain2
);
929 /* Restores the current thread and the frame selected before
930 the "info threads" command. */
931 do_cleanups (old_chain
);
933 if (pid
== -1 && requested_threads
== NULL
)
935 gdb_assert (current_thread
!= -1
937 || ptid_equal (inferior_ptid
, null_ptid
));
938 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
939 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
941 if (current_thread
!= -1 && is_exited (current_ptid
))
942 ui_out_message (uiout
, 0, "\n\
943 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
946 && current_thread
== -1
947 && ptid_equal (current_ptid
, null_ptid
))
948 ui_out_message (uiout
, 0, "\n\
949 No selected thread. See `help thread'.\n");
953 /* Print information about currently known threads
955 Optional ARG is a thread id, or list of thread ids.
957 Note: this has the drawback that it _really_ switches
958 threads, which frees the frame cache. A no-side
959 effects info-threads command would be nicer. */
962 info_threads_command (char *arg
, int from_tty
)
964 print_thread_info (current_uiout
, arg
, -1);
967 /* Switch from one thread to another. */
970 switch_to_thread (ptid_t ptid
)
972 /* Switch the program space as well, if we can infer it from the now
973 current thread. Otherwise, it's up to the caller to select the
975 if (!ptid_equal (ptid
, null_ptid
))
977 struct inferior
*inf
;
979 inf
= find_inferior_pid (ptid_get_pid (ptid
));
980 gdb_assert (inf
!= NULL
);
981 set_current_program_space (inf
->pspace
);
982 set_current_inferior (inf
);
985 if (ptid_equal (ptid
, inferior_ptid
))
988 inferior_ptid
= ptid
;
989 reinit_frame_cache ();
991 /* We don't check for is_stopped, because we're called at times
992 while in the TARGET_RUNNING state, e.g., while handling an
994 if (!ptid_equal (inferior_ptid
, null_ptid
)
996 && !is_executing (ptid
))
997 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
999 stop_pc
= ~(CORE_ADDR
) 0;
1003 restore_current_thread (ptid_t ptid
)
1005 switch_to_thread (ptid
);
1009 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1011 struct frame_info
*frame
= NULL
;
1014 /* This means there was no selected frame. */
1015 if (frame_level
== -1)
1017 select_frame (NULL
);
1021 gdb_assert (frame_level
>= 0);
1023 /* Restore by level first, check if the frame id is the same as
1024 expected. If that fails, try restoring by frame id. If that
1025 fails, nothing to do, just warn the user. */
1027 count
= frame_level
;
1028 frame
= find_relative_frame (get_current_frame (), &count
);
1031 /* The frame ids must match - either both valid or both outer_frame_id.
1032 The latter case is not failsafe, but since it's highly unlikely
1033 the search by level finds the wrong frame, it's 99.9(9)% of
1034 the time (for all practical purposes) safe. */
1035 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1037 /* Cool, all is fine. */
1038 select_frame (frame
);
1042 frame
= frame_find_by_id (a_frame_id
);
1045 /* Cool, refound it. */
1046 select_frame (frame
);
1050 /* Nothing else to do, the frame layout really changed. Select the
1051 innermost stack frame. */
1052 select_frame (get_current_frame ());
1054 /* Warn the user. */
1055 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1057 warning (_("Couldn't restore frame #%d in "
1058 "current thread, at reparsed frame #0\n"),
1060 /* For MI, we should probably have a notification about
1061 current frame change. But this error is not very
1062 likely, so don't bother for now. */
1063 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1067 struct current_thread_cleanup
1069 ptid_t inferior_ptid
;
1070 struct frame_id selected_frame_id
;
1071 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
;
1115 struct inferior
*inf
;
1117 tp
= find_thread_ptid (old
->inferior_ptid
);
1120 inf
= find_inferior_id (old
->inf_id
);
1122 inf
->removable
= old
->was_removable
;
1127 make_cleanup_restore_current_thread (void)
1129 struct thread_info
*tp
;
1130 struct frame_info
*frame
;
1131 struct current_thread_cleanup
*old
;
1133 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1134 old
->inferior_ptid
= inferior_ptid
;
1135 old
->inf_id
= current_inferior ()->num
;
1136 old
->was_removable
= current_inferior ()->removable
;
1138 if (!ptid_equal (inferior_ptid
, null_ptid
))
1140 old
->was_stopped
= is_stopped (inferior_ptid
);
1141 if (old
->was_stopped
1142 && target_has_registers
1144 && target_has_memory
)
1146 /* When processing internal events, there might not be a
1147 selected frame. If we naively call get_selected_frame
1148 here, then we can end up reading debuginfo for the
1149 current frame, but we don't generally need the debuginfo
1151 frame
= get_selected_frame_if_set ();
1156 old
->selected_frame_id
= get_frame_id (frame
);
1157 old
->selected_frame_level
= frame_relative_level (frame
);
1159 tp
= find_thread_ptid (inferior_ptid
);
1164 current_inferior ()->removable
= 0;
1166 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1167 restore_current_thread_cleanup_dtor
);
1170 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1171 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1172 of two numbers seperated by a hyphen. Examples:
1174 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1175 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1176 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1179 thread_apply_all_command (char *cmd
, int from_tty
)
1181 struct thread_info
*tp
;
1182 struct cleanup
*old_chain
;
1185 if (cmd
== NULL
|| *cmd
== '\000')
1186 error (_("Please specify a command following the thread ID list"));
1188 update_thread_list ();
1190 old_chain
= make_cleanup_restore_current_thread ();
1192 /* Save a copy of the command in case it is clobbered by
1194 saved_cmd
= xstrdup (cmd
);
1195 make_cleanup (xfree
, saved_cmd
);
1196 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1197 if (thread_alive (tp
))
1199 switch_to_thread (tp
->ptid
);
1201 printf_filtered (_("\nThread %d (%s):\n"),
1202 tp
->num
, target_pid_to_str (inferior_ptid
));
1203 execute_command (cmd
, from_tty
);
1204 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1208 do_cleanups (old_chain
);
1212 thread_apply_command (char *tidlist
, int from_tty
)
1215 struct cleanup
*old_chain
;
1217 struct get_number_or_range_state state
;
1219 if (tidlist
== NULL
|| *tidlist
== '\000')
1220 error (_("Please specify a thread ID list"));
1222 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1225 error (_("Please specify a command following the thread ID list"));
1227 /* Save a copy of the command in case it is clobbered by
1229 saved_cmd
= xstrdup (cmd
);
1230 old_chain
= make_cleanup (xfree
, saved_cmd
);
1232 init_number_or_range (&state
, tidlist
);
1233 while (!state
.finished
&& state
.string
< cmd
)
1235 struct thread_info
*tp
;
1239 start
= get_number_or_range (&state
);
1241 make_cleanup_restore_current_thread ();
1243 tp
= find_thread_id (start
);
1246 warning (_("Unknown thread %d."), start
);
1247 else if (!thread_alive (tp
))
1248 warning (_("Thread %d has terminated."), start
);
1251 switch_to_thread (tp
->ptid
);
1253 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1254 target_pid_to_str (inferior_ptid
));
1255 execute_command (cmd
, from_tty
);
1257 /* Restore exact command used previously. */
1258 strcpy (cmd
, saved_cmd
);
1262 do_cleanups (old_chain
);
1265 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1266 if prefix of arg is `apply'. */
1269 thread_command (char *tidstr
, int from_tty
)
1273 if (ptid_equal (inferior_ptid
, null_ptid
))
1274 error (_("No thread selected"));
1276 if (target_has_stack
)
1278 if (is_exited (inferior_ptid
))
1279 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1280 pid_to_thread_id (inferior_ptid
),
1281 target_pid_to_str (inferior_ptid
));
1283 printf_filtered (_("[Current thread is %d (%s)]\n"),
1284 pid_to_thread_id (inferior_ptid
),
1285 target_pid_to_str (inferior_ptid
));
1288 error (_("No stack."));
1292 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1295 /* Implementation of `thread name'. */
1298 thread_name_command (char *arg
, int from_tty
)
1300 struct thread_info
*info
;
1302 if (ptid_equal (inferior_ptid
, null_ptid
))
1303 error (_("No thread selected"));
1305 while (arg
&& isspace (*arg
))
1308 info
= inferior_thread ();
1310 info
->name
= arg
? xstrdup (arg
) : NULL
;
1313 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1316 thread_find_command (char *arg
, int from_tty
)
1318 struct thread_info
*tp
;
1320 unsigned long match
= 0;
1322 if (arg
== NULL
|| *arg
== '\0')
1323 error (_("Command requires an argument."));
1325 tmp
= re_comp (arg
);
1327 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1329 update_thread_list ();
1330 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1332 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1334 printf_filtered (_("Thread %d has name '%s'\n"),
1339 tmp
= target_thread_name (tp
);
1340 if (tmp
!= NULL
&& re_exec (tmp
))
1342 printf_filtered (_("Thread %d has target name '%s'\n"),
1347 tmp
= target_pid_to_str (tp
->ptid
);
1348 if (tmp
!= NULL
&& re_exec (tmp
))
1350 printf_filtered (_("Thread %d has target id '%s'\n"),
1355 tmp
= target_extra_thread_info (tp
);
1356 if (tmp
!= NULL
&& re_exec (tmp
))
1358 printf_filtered (_("Thread %d has extra info '%s'\n"),
1364 printf_filtered (_("No threads match '%s'\n"), arg
);
1367 /* Print notices when new threads are attached and detached. */
1368 int print_thread_events
= 1;
1370 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1371 struct cmd_list_element
*c
, const char *value
)
1373 fprintf_filtered (file
,
1374 _("Printing of thread events is %s.\n"),
1379 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1382 struct thread_info
*tp
;
1384 num
= value_as_long (parse_and_eval (tidstr
));
1386 tp
= find_thread_id (num
);
1389 error (_("Thread ID %d not known."), num
);
1391 if (!thread_alive (tp
))
1392 error (_("Thread ID %d has terminated."), num
);
1394 switch_to_thread (tp
->ptid
);
1396 annotate_thread_changed ();
1398 ui_out_text (uiout
, "[Switching to thread ");
1399 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1400 ui_out_text (uiout
, " (");
1401 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1402 ui_out_text (uiout
, ")]");
1404 /* Note that we can't reach this with an exited thread, due to the
1405 thread_alive check above. */
1406 if (tp
->state
== THREAD_RUNNING
)
1407 ui_out_text (uiout
, "(running)\n");
1410 ui_out_text (uiout
, "\n");
1411 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1414 /* Since the current thread may have changed, see if there is any
1415 exited thread we can now delete. */
1422 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1424 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1425 error_message
, RETURN_MASK_ALL
) < 0)
1431 update_thread_list (void)
1434 target_find_new_threads ();
1437 /* Return a new value for the selected thread's id. Return a value of 0 if
1438 no thread is selected, or no threads exist. */
1440 static struct value
*
1441 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1444 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1446 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1447 (tp
? tp
->num
: 0));
1450 /* Commands with a prefix of `thread'. */
1451 struct cmd_list_element
*thread_cmd_list
= NULL
;
1453 /* Implementation of `thread' variable. */
1455 static const struct internalvar_funcs thread_funcs
=
1457 thread_id_make_value
,
1463 _initialize_thread (void)
1465 static struct cmd_list_element
*thread_apply_list
= NULL
;
1467 add_info ("threads", info_threads_command
,
1468 _("Display currently known threads.\n\
1469 Usage: info threads [ID]...\n\
1470 Optional arguments are thread IDs with spaces between.\n\
1471 If no arguments, all threads are displayed."));
1473 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1474 Use this command to switch between threads.\n\
1475 The new thread ID must be currently known."),
1476 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1478 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1479 _("Apply a command to a list of threads."),
1480 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1482 add_cmd ("all", class_run
, thread_apply_all_command
,
1483 _("Apply a command to all threads."), &thread_apply_list
);
1485 add_cmd ("name", class_run
, thread_name_command
,
1486 _("Set the current thread's name.\n\
1487 Usage: thread name [NAME]\n\
1488 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1490 add_cmd ("find", class_run
, thread_find_command
, _("\
1491 Find threads that match a regular expression.\n\
1492 Usage: thread find REGEXP\n\
1493 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1497 add_com_alias ("t", "thread", class_run
, 1);
1499 add_setshow_boolean_cmd ("thread-events", no_class
,
1500 &print_thread_events
, _("\
1501 Set printing of thread events (such as thread start and exit)."), _("\
1502 Show printing of thread events (such as thread start and exit)."), NULL
,
1504 show_print_thread_events
,
1505 &setprintlist
, &showprintlist
);
1507 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);