1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdbthread.h"
30 #include "exceptions.h"
35 #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 btrace_teardown (tp
);
122 do_all_intermediate_continuations_thread (tp
, 1);
123 do_all_continuations_thread (tp
, 1);
127 free_thread (struct thread_info
*tp
)
131 if (tp
->private_dtor
)
132 tp
->private_dtor (tp
->private);
142 init_thread_list (void)
144 struct thread_info
*tp
, *tpnext
;
146 highest_thread_num
= 0;
151 for (tp
= thread_list
; tp
; tp
= tpnext
)
160 /* Allocate a new thread with target id PTID and add it to the thread
163 static struct thread_info
*
164 new_thread (ptid_t ptid
)
166 struct thread_info
*tp
;
168 tp
= xcalloc (1, sizeof (*tp
));
171 tp
->num
= ++highest_thread_num
;
172 tp
->next
= thread_list
;
175 /* Nothing to follow yet. */
176 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
177 tp
->state
= THREAD_STOPPED
;
183 add_thread_silent (ptid_t ptid
)
185 struct thread_info
*tp
;
187 tp
= find_thread_ptid (ptid
);
189 /* Found an old thread with the same id. It has to be dead,
190 otherwise we wouldn't be adding a new thread with the same id.
191 The OS is reusing this id --- delete it, and recreate a new
194 /* In addition to deleting the thread, if this is the current
195 thread, then we need to take care that delete_thread doesn't
196 really delete the thread if it is inferior_ptid. Create a
197 new template thread in the list with an invalid ptid, switch
198 to it, delete the original thread, reset the new thread's
199 ptid, and switch to it. */
201 if (ptid_equal (inferior_ptid
, ptid
))
203 tp
= new_thread (null_ptid
);
205 /* Make switch_to_thread not read from the thread. */
206 tp
->state
= THREAD_EXITED
;
207 switch_to_thread (null_ptid
);
209 /* Now we can delete it. */
210 delete_thread (ptid
);
212 /* Now reset its ptid, and reswitch inferior_ptid to it. */
214 tp
->state
= THREAD_STOPPED
;
215 switch_to_thread (ptid
);
217 observer_notify_new_thread (tp
);
223 /* Just go ahead and delete it. */
224 delete_thread (ptid
);
227 tp
= new_thread (ptid
);
228 observer_notify_new_thread (tp
);
234 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
236 struct thread_info
*result
= add_thread_silent (ptid
);
238 result
->private = private;
240 if (print_thread_events
)
241 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
243 annotate_new_thread ();
248 add_thread (ptid_t ptid
)
250 return add_thread_with_info (ptid
, NULL
);
253 /* Delete thread PTID. If SILENT, don't notify the observer of this
256 delete_thread_1 (ptid_t ptid
, int silent
)
258 struct thread_info
*tp
, *tpprev
;
262 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
263 if (ptid_equal (tp
->ptid
, ptid
))
269 /* If this is the current thread, or there's code out there that
270 relies on it existing (refcount > 0) we can't delete yet. Mark
271 it as exited, and notify it. */
273 || ptid_equal (tp
->ptid
, inferior_ptid
))
275 if (tp
->state
!= THREAD_EXITED
)
277 observer_notify_thread_exit (tp
, silent
);
279 /* Tag it as exited. */
280 tp
->state
= THREAD_EXITED
;
282 /* Clear breakpoints, etc. associated with this thread. */
283 clear_thread_inferior_resources (tp
);
286 /* Will be really deleted some other time. */
290 /* Notify thread exit, but only if we haven't already. */
291 if (tp
->state
!= THREAD_EXITED
)
292 observer_notify_thread_exit (tp
, silent
);
294 /* Tag it as exited. */
295 tp
->state
= THREAD_EXITED
;
296 clear_thread_inferior_resources (tp
);
299 tpprev
->next
= tp
->next
;
301 thread_list
= tp
->next
;
306 /* Delete thread PTID and notify of thread exit. If this is
307 inferior_ptid, don't actually delete it, but tag it as exited and
308 do the notification. If PTID is the user selected thread, clear
311 delete_thread (ptid_t ptid
)
313 delete_thread_1 (ptid
, 0 /* not silent */);
317 delete_thread_silent (ptid_t ptid
)
319 delete_thread_1 (ptid
, 1 /* silent */);
323 find_thread_id (int num
)
325 struct thread_info
*tp
;
327 for (tp
= thread_list
; tp
; tp
= tp
->next
)
334 /* Find a thread_info by matching PTID. */
336 find_thread_ptid (ptid_t ptid
)
338 struct thread_info
*tp
;
340 for (tp
= thread_list
; tp
; tp
= tp
->next
)
341 if (ptid_equal (tp
->ptid
, ptid
))
348 * Thread iterator function.
350 * Calls a callback function once for each thread, so long as
351 * the callback function returns false. If the callback function
352 * returns true, the iteration will end and the current thread
353 * will be returned. This can be useful for implementing a
354 * search for a thread with arbitrary attributes, or for applying
355 * some operation to every thread.
357 * FIXME: some of the existing functionality, such as
358 * "Thread apply all", might be rewritten using this functionality.
362 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
365 struct thread_info
*tp
, *next
;
367 for (tp
= thread_list
; tp
; tp
= next
)
370 if ((*callback
) (tp
, data
))
381 struct thread_info
*tp
;
383 for (tp
= thread_list
; tp
; tp
= tp
->next
)
390 valid_thread_id (int num
)
392 struct thread_info
*tp
;
394 for (tp
= thread_list
; tp
; tp
= tp
->next
)
402 pid_to_thread_id (ptid_t ptid
)
404 struct thread_info
*tp
;
406 for (tp
= thread_list
; tp
; tp
= tp
->next
)
407 if (ptid_equal (tp
->ptid
, ptid
))
414 thread_id_to_pid (int num
)
416 struct thread_info
*thread
= find_thread_id (num
);
421 return pid_to_ptid (-1);
425 in_thread_list (ptid_t ptid
)
427 struct thread_info
*tp
;
429 for (tp
= thread_list
; tp
; tp
= tp
->next
)
430 if (ptid_equal (tp
->ptid
, ptid
))
433 return 0; /* Never heard of 'im. */
436 /* Finds the first thread of the inferior given by PID. If PID is -1,
437 return the first thread in the list. */
440 first_thread_of_process (int pid
)
442 struct thread_info
*tp
, *ret
= NULL
;
444 for (tp
= thread_list
; tp
; tp
= tp
->next
)
445 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
446 if (ret
== NULL
|| tp
->num
< ret
->num
)
453 any_thread_of_process (int pid
)
455 struct thread_info
*tp
;
457 for (tp
= thread_list
; tp
; tp
= tp
->next
)
458 if (ptid_get_pid (tp
->ptid
) == pid
)
465 any_live_thread_of_process (int pid
)
467 struct thread_info
*tp
;
468 struct thread_info
*tp_executing
= NULL
;
470 for (tp
= thread_list
; tp
; tp
= tp
->next
)
471 if (tp
->state
!= THREAD_EXITED
&& ptid_get_pid (tp
->ptid
) == pid
)
482 /* Print a list of thread ids currently known, and the total number of
483 threads. To be used from within catch_errors. */
485 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
487 struct thread_info
*tp
;
489 struct cleanup
*cleanup_chain
;
490 int current_thread
= -1;
492 update_thread_list ();
494 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
496 for (tp
= thread_list
; tp
; tp
= tp
->next
)
498 if (tp
->state
== THREAD_EXITED
)
501 if (ptid_equal (tp
->ptid
, inferior_ptid
))
502 current_thread
= tp
->num
;
505 ui_out_field_int (uiout
, "thread-id", tp
->num
);
508 do_cleanups (cleanup_chain
);
510 if (current_thread
!= -1)
511 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
512 ui_out_field_int (uiout
, "number-of-threads", num
);
516 /* Official gdblib interface function to get a list of thread ids and
519 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
521 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
522 error_message
, RETURN_MASK_ALL
) < 0)
527 /* Return true if TP is an active thread. */
529 thread_alive (struct thread_info
*tp
)
531 if (tp
->state
== THREAD_EXITED
)
533 if (!target_thread_alive (tp
->ptid
))
541 struct thread_info
*tp
, *next
;
543 for (tp
= thread_list
; tp
; tp
= next
)
546 if (!thread_alive (tp
))
547 delete_thread (tp
->ptid
);
552 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
554 struct inferior
*inf
;
555 struct thread_info
*tp
;
557 /* It can happen that what we knew as the target inferior id
558 changes. E.g, target remote may only discover the remote process
559 pid after adding the inferior to GDB's list. */
560 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
561 inf
->pid
= ptid_get_pid (new_ptid
);
563 tp
= find_thread_ptid (old_ptid
);
566 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
570 set_running (ptid_t ptid
, int running
)
572 struct thread_info
*tp
;
573 int all
= ptid_equal (ptid
, minus_one_ptid
);
575 /* We try not to notify the observer if no thread has actually changed
576 the running state -- merely to reduce the number of messages to
577 frontend. Frontend is supposed to handle multiple *running just fine. */
578 if (all
|| ptid_is_pid (ptid
))
582 for (tp
= thread_list
; tp
; tp
= tp
->next
)
583 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
585 if (tp
->state
== THREAD_EXITED
)
587 if (running
&& tp
->state
== THREAD_STOPPED
)
589 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
592 observer_notify_target_resumed (ptid
);
598 tp
= find_thread_ptid (ptid
);
600 gdb_assert (tp
->state
!= THREAD_EXITED
);
601 if (running
&& tp
->state
== THREAD_STOPPED
)
603 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
605 observer_notify_target_resumed (ptid
);
610 is_thread_state (ptid_t ptid
, enum thread_state state
)
612 struct thread_info
*tp
;
614 tp
= find_thread_ptid (ptid
);
616 return tp
->state
== state
;
620 is_stopped (ptid_t ptid
)
622 return is_thread_state (ptid
, THREAD_STOPPED
);
626 is_exited (ptid_t ptid
)
628 return is_thread_state (ptid
, THREAD_EXITED
);
632 is_running (ptid_t ptid
)
634 return is_thread_state (ptid
, THREAD_RUNNING
);
640 struct thread_info
*tp
;
642 for (tp
= thread_list
; tp
; tp
= tp
->next
)
643 if (tp
->state
== THREAD_RUNNING
)
650 is_executing (ptid_t ptid
)
652 struct thread_info
*tp
;
654 tp
= find_thread_ptid (ptid
);
656 return tp
->executing
;
660 set_executing (ptid_t ptid
, int executing
)
662 struct thread_info
*tp
;
663 int all
= ptid_equal (ptid
, minus_one_ptid
);
665 if (all
|| ptid_is_pid (ptid
))
667 for (tp
= thread_list
; tp
; tp
= tp
->next
)
668 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
669 tp
->executing
= executing
;
673 tp
= find_thread_ptid (ptid
);
675 tp
->executing
= executing
;
680 set_stop_requested (ptid_t ptid
, int stop
)
682 struct thread_info
*tp
;
683 int all
= ptid_equal (ptid
, minus_one_ptid
);
685 if (all
|| ptid_is_pid (ptid
))
687 for (tp
= thread_list
; tp
; tp
= tp
->next
)
688 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
689 tp
->stop_requested
= stop
;
693 tp
= find_thread_ptid (ptid
);
695 tp
->stop_requested
= stop
;
698 /* Call the stop requested observer so other components of GDB can
699 react to this request. */
701 observer_notify_thread_stop_requested (ptid
);
705 finish_thread_state (ptid_t ptid
)
707 struct thread_info
*tp
;
711 all
= ptid_equal (ptid
, minus_one_ptid
);
713 if (all
|| ptid_is_pid (ptid
))
715 for (tp
= thread_list
; tp
; tp
= tp
->next
)
717 if (tp
->state
== THREAD_EXITED
)
719 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
721 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
723 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
729 tp
= find_thread_ptid (ptid
);
731 if (tp
->state
!= THREAD_EXITED
)
733 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
735 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
740 observer_notify_target_resumed (ptid
);
744 finish_thread_state_cleanup (void *arg
)
746 ptid_t
*ptid_p
= arg
;
750 finish_thread_state (*ptid_p
);
753 /* Prints the list of threads and their details on UIOUT.
754 This is a version of 'info_threads_command' suitable for
756 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
757 that should be printed. Otherwise, all threads are
759 If PID is not -1, only print threads from the process PID.
760 Otherwise, threads from all attached PIDs are printed.
761 If both REQUESTED_THREAD and PID are not -1, then the thread
762 is printed if it belongs to the specified process. Otherwise,
763 an error is raised. */
765 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
767 struct thread_info
*tp
;
769 struct cleanup
*old_chain
;
770 char *extra_info
, *name
, *target_id
;
771 int current_thread
= -1;
773 update_thread_list ();
774 current_ptid
= inferior_ptid
;
776 /* We'll be switching threads temporarily. */
777 old_chain
= make_cleanup_restore_current_thread ();
779 /* For backward compatibility, we make a list for MI. A table is
780 preferable for the CLI, though, because it shows table
782 if (ui_out_is_mi_like_p (uiout
))
783 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
788 for (tp
= thread_list
; tp
; tp
= tp
->next
)
790 if (!number_is_in_list (requested_threads
, tp
->num
))
793 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
796 if (tp
->state
== THREAD_EXITED
)
804 if (requested_threads
== NULL
|| *requested_threads
== '\0')
805 ui_out_message (uiout
, 0, _("No threads.\n"));
807 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
809 do_cleanups (old_chain
);
813 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
815 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
816 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
817 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
818 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
819 ui_out_table_body (uiout
);
822 for (tp
= thread_list
; tp
; tp
= tp
->next
)
824 struct cleanup
*chain2
;
827 if (!number_is_in_list (requested_threads
, tp
->num
))
830 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
832 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
833 error (_("Requested thread not found in requested process"));
837 if (ptid_equal (tp
->ptid
, current_ptid
))
838 current_thread
= tp
->num
;
840 if (tp
->state
== THREAD_EXITED
)
843 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
845 if (ui_out_is_mi_like_p (uiout
))
848 if (ptid_equal (tp
->ptid
, current_ptid
))
849 ui_out_text (uiout
, "* ");
851 ui_out_text (uiout
, " ");
855 if (ptid_equal (tp
->ptid
, current_ptid
))
856 ui_out_field_string (uiout
, "current", "*");
858 ui_out_field_skip (uiout
, "current");
861 ui_out_field_int (uiout
, "id", tp
->num
);
863 /* For the CLI, we stuff everything into the target-id field.
864 This is a gross hack to make the output come out looking
865 correct. The underlying problem here is that ui-out has no
866 way to specify that a field's space allocation should be
867 shared by several fields. For MI, we do the right thing
870 target_id
= target_pid_to_str (tp
->ptid
);
871 extra_info
= target_extra_thread_info (tp
);
872 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
874 if (ui_out_is_mi_like_p (uiout
))
876 ui_out_field_string (uiout
, "target-id", target_id
);
878 ui_out_field_string (uiout
, "details", extra_info
);
880 ui_out_field_string (uiout
, "name", name
);
884 struct cleanup
*str_cleanup
;
887 if (extra_info
&& name
)
888 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
891 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
893 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
895 contents
= xstrdup (target_id
);
896 str_cleanup
= make_cleanup (xfree
, contents
);
898 ui_out_field_string (uiout
, "target-id", contents
);
899 do_cleanups (str_cleanup
);
902 if (tp
->state
== THREAD_RUNNING
)
903 ui_out_text (uiout
, "(running)\n");
906 /* The switch below puts us at the top of the stack (leaf
908 switch_to_thread (tp
->ptid
);
909 print_stack_frame (get_selected_frame (NULL
),
910 /* For MI output, print frame level. */
911 ui_out_is_mi_like_p (uiout
),
915 if (ui_out_is_mi_like_p (uiout
))
917 char *state
= "stopped";
919 if (tp
->state
== THREAD_RUNNING
)
921 ui_out_field_string (uiout
, "state", state
);
924 core
= target_core_of_thread (tp
->ptid
);
925 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
926 ui_out_field_int (uiout
, "core", core
);
928 do_cleanups (chain2
);
931 /* Restores the current thread and the frame selected before
932 the "info threads" command. */
933 do_cleanups (old_chain
);
935 if (pid
== -1 && requested_threads
== NULL
)
937 gdb_assert (current_thread
!= -1
939 || ptid_equal (inferior_ptid
, null_ptid
));
940 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
941 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
943 if (current_thread
!= -1 && is_exited (current_ptid
))
944 ui_out_message (uiout
, 0, "\n\
945 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
948 && current_thread
== -1
949 && ptid_equal (current_ptid
, null_ptid
))
950 ui_out_message (uiout
, 0, "\n\
951 No selected thread. See `help thread'.\n");
955 /* Print information about currently known threads
957 Optional ARG is a thread id, or list of thread ids.
959 Note: this has the drawback that it _really_ switches
960 threads, which frees the frame cache. A no-side
961 effects info-threads command would be nicer. */
964 info_threads_command (char *arg
, int from_tty
)
966 print_thread_info (current_uiout
, arg
, -1);
969 /* Switch from one thread to another. */
972 switch_to_thread (ptid_t ptid
)
974 /* Switch the program space as well, if we can infer it from the now
975 current thread. Otherwise, it's up to the caller to select the
977 if (!ptid_equal (ptid
, null_ptid
))
979 struct inferior
*inf
;
981 inf
= find_inferior_pid (ptid_get_pid (ptid
));
982 gdb_assert (inf
!= NULL
);
983 set_current_program_space (inf
->pspace
);
984 set_current_inferior (inf
);
987 if (ptid_equal (ptid
, inferior_ptid
))
990 inferior_ptid
= ptid
;
991 reinit_frame_cache ();
993 /* We don't check for is_stopped, because we're called at times
994 while in the TARGET_RUNNING state, e.g., while handling an
996 if (!ptid_equal (inferior_ptid
, null_ptid
)
998 && !is_executing (ptid
))
999 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1001 stop_pc
= ~(CORE_ADDR
) 0;
1005 restore_current_thread (ptid_t ptid
)
1007 switch_to_thread (ptid
);
1011 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1013 struct frame_info
*frame
= NULL
;
1016 /* This means there was no selected frame. */
1017 if (frame_level
== -1)
1019 select_frame (NULL
);
1023 gdb_assert (frame_level
>= 0);
1025 /* Restore by level first, check if the frame id is the same as
1026 expected. If that fails, try restoring by frame id. If that
1027 fails, nothing to do, just warn the user. */
1029 count
= frame_level
;
1030 frame
= find_relative_frame (get_current_frame (), &count
);
1033 /* The frame ids must match - either both valid or both outer_frame_id.
1034 The latter case is not failsafe, but since it's highly unlikely
1035 the search by level finds the wrong frame, it's 99.9(9)% of
1036 the time (for all practical purposes) safe. */
1037 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1039 /* Cool, all is fine. */
1040 select_frame (frame
);
1044 frame
= frame_find_by_id (a_frame_id
);
1047 /* Cool, refound it. */
1048 select_frame (frame
);
1052 /* Nothing else to do, the frame layout really changed. Select the
1053 innermost stack frame. */
1054 select_frame (get_current_frame ());
1056 /* Warn the user. */
1057 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1059 warning (_("Couldn't restore frame #%d in "
1060 "current thread, at reparsed frame #0\n"),
1062 /* For MI, we should probably have a notification about
1063 current frame change. But this error is not very
1064 likely, so don't bother for now. */
1065 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1069 struct current_thread_cleanup
1071 ptid_t inferior_ptid
;
1072 struct frame_id selected_frame_id
;
1073 int selected_frame_level
;
1080 do_restore_current_thread_cleanup (void *arg
)
1082 struct thread_info
*tp
;
1083 struct current_thread_cleanup
*old
= arg
;
1085 tp
= find_thread_ptid (old
->inferior_ptid
);
1087 /* If the previously selected thread belonged to a process that has
1088 in the mean time been deleted (due to normal exit, detach, etc.),
1089 then don't revert back to it, but instead simply drop back to no
1092 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1093 restore_current_thread (old
->inferior_ptid
);
1096 restore_current_thread (null_ptid
);
1097 set_current_inferior (find_inferior_id (old
->inf_id
));
1100 /* The running state of the originally selected thread may have
1101 changed, so we have to recheck it here. */
1102 if (!ptid_equal (inferior_ptid
, null_ptid
)
1104 && is_stopped (inferior_ptid
)
1105 && target_has_registers
1107 && target_has_memory
)
1108 restore_selected_frame (old
->selected_frame_id
,
1109 old
->selected_frame_level
);
1113 restore_current_thread_cleanup_dtor (void *arg
)
1115 struct current_thread_cleanup
*old
= arg
;
1116 struct thread_info
*tp
;
1117 struct inferior
*inf
;
1119 tp
= find_thread_ptid (old
->inferior_ptid
);
1122 inf
= find_inferior_id (old
->inf_id
);
1124 inf
->removable
= old
->was_removable
;
1129 make_cleanup_restore_current_thread (void)
1131 struct thread_info
*tp
;
1132 struct frame_info
*frame
;
1133 struct current_thread_cleanup
*old
;
1135 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1136 old
->inferior_ptid
= inferior_ptid
;
1137 old
->inf_id
= current_inferior ()->num
;
1138 old
->was_removable
= current_inferior ()->removable
;
1140 if (!ptid_equal (inferior_ptid
, null_ptid
))
1142 old
->was_stopped
= is_stopped (inferior_ptid
);
1143 if (old
->was_stopped
1144 && target_has_registers
1146 && target_has_memory
)
1148 /* When processing internal events, there might not be a
1149 selected frame. If we naively call get_selected_frame
1150 here, then we can end up reading debuginfo for the
1151 current frame, but we don't generally need the debuginfo
1153 frame
= get_selected_frame_if_set ();
1158 old
->selected_frame_id
= get_frame_id (frame
);
1159 old
->selected_frame_level
= frame_relative_level (frame
);
1161 tp
= find_thread_ptid (inferior_ptid
);
1166 current_inferior ()->removable
= 0;
1168 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1169 restore_current_thread_cleanup_dtor
);
1172 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1173 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1174 of two numbers seperated by a hyphen. Examples:
1176 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1177 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1178 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1181 thread_apply_all_command (char *cmd
, int from_tty
)
1183 struct thread_info
*tp
;
1184 struct cleanup
*old_chain
;
1187 if (cmd
== NULL
|| *cmd
== '\000')
1188 error (_("Please specify a command following the thread ID list"));
1190 update_thread_list ();
1192 old_chain
= make_cleanup_restore_current_thread ();
1194 /* Save a copy of the command in case it is clobbered by
1196 saved_cmd
= xstrdup (cmd
);
1197 make_cleanup (xfree
, saved_cmd
);
1198 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1199 if (thread_alive (tp
))
1201 switch_to_thread (tp
->ptid
);
1203 printf_filtered (_("\nThread %d (%s):\n"),
1204 tp
->num
, target_pid_to_str (inferior_ptid
));
1205 execute_command (cmd
, from_tty
);
1206 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1210 do_cleanups (old_chain
);
1214 thread_apply_command (char *tidlist
, int from_tty
)
1217 struct cleanup
*old_chain
;
1219 struct get_number_or_range_state state
;
1221 if (tidlist
== NULL
|| *tidlist
== '\000')
1222 error (_("Please specify a thread ID list"));
1224 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1227 error (_("Please specify a command following the thread ID list"));
1229 /* Save a copy of the command in case it is clobbered by
1231 saved_cmd
= xstrdup (cmd
);
1232 old_chain
= make_cleanup (xfree
, saved_cmd
);
1234 init_number_or_range (&state
, tidlist
);
1235 while (!state
.finished
&& state
.string
< cmd
)
1237 struct thread_info
*tp
;
1240 start
= get_number_or_range (&state
);
1242 make_cleanup_restore_current_thread ();
1244 tp
= find_thread_id (start
);
1247 warning (_("Unknown thread %d."), start
);
1248 else if (!thread_alive (tp
))
1249 warning (_("Thread %d has terminated."), start
);
1252 switch_to_thread (tp
->ptid
);
1254 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1255 target_pid_to_str (inferior_ptid
));
1256 execute_command (cmd
, from_tty
);
1258 /* Restore exact command used previously. */
1259 strcpy (cmd
, saved_cmd
);
1263 do_cleanups (old_chain
);
1266 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1267 if prefix of arg is `apply'. */
1270 thread_command (char *tidstr
, int from_tty
)
1274 if (ptid_equal (inferior_ptid
, null_ptid
))
1275 error (_("No thread selected"));
1277 if (target_has_stack
)
1279 if (is_exited (inferior_ptid
))
1280 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1281 pid_to_thread_id (inferior_ptid
),
1282 target_pid_to_str (inferior_ptid
));
1284 printf_filtered (_("[Current thread is %d (%s)]\n"),
1285 pid_to_thread_id (inferior_ptid
),
1286 target_pid_to_str (inferior_ptid
));
1289 error (_("No stack."));
1293 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1296 /* Implementation of `thread name'. */
1299 thread_name_command (char *arg
, int from_tty
)
1301 struct thread_info
*info
;
1303 if (ptid_equal (inferior_ptid
, null_ptid
))
1304 error (_("No thread selected"));
1306 arg
= skip_spaces (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
);