1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 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"
37 #include <sys/types.h>
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "continuations.h"
47 /* Definition of struct thread_info exported to gdbthread.h. */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 /* True if any thread is, or may be executing. We need to track this
59 separately because until we fully sync the thread list, we won't
60 know whether the target is fully stopped, even if we see stop
61 events for all known threads, because any of those threads may have
62 spawned new threads we haven't heard of yet. */
63 static int threads_executing
;
65 static void thread_command (char *tidstr
, int from_tty
);
66 static void thread_apply_all_command (char *, int);
67 static int thread_alive (struct thread_info
*);
68 static void info_threads_command (char *, int);
69 static void thread_apply_command (char *, int);
70 static void restore_current_thread (ptid_t
);
71 static void prune_threads (void);
73 /* Data to cleanup thread array. */
75 struct thread_array_cleanup
77 /* Array of thread pointers used to set
79 struct thread_info
**tp_array
;
81 /* Thread count in the array. */
87 inferior_thread (void)
89 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
94 /* Delete the breakpoint pointed at by BP_P, if there's one. */
97 delete_thread_breakpoint (struct breakpoint
**bp_p
)
101 delete_breakpoint (*bp_p
);
107 delete_step_resume_breakpoint (struct thread_info
*tp
)
110 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
114 delete_exception_resume_breakpoint (struct thread_info
*tp
)
117 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
120 /* Delete the breakpoint pointed at by BP_P at the next stop, if
124 delete_at_next_stop (struct breakpoint
**bp
)
128 (*bp
)->disposition
= disp_del_at_next_stop
;
134 clear_thread_inferior_resources (struct thread_info
*tp
)
136 /* NOTE: this will take care of any left-over step_resume breakpoints,
137 but not any user-specified thread-specific breakpoints. We can not
138 delete the breakpoint straight-off, because the inferior might not
139 be stopped at the moment. */
140 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
141 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
143 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
145 bpstat_clear (&tp
->control
.stop_bpstat
);
147 btrace_teardown (tp
);
149 do_all_intermediate_continuations_thread (tp
, 1);
150 do_all_continuations_thread (tp
, 1);
154 free_thread (struct thread_info
*tp
)
158 if (tp
->private_dtor
)
159 tp
->private_dtor (tp
->private);
169 init_thread_list (void)
171 struct thread_info
*tp
, *tpnext
;
173 highest_thread_num
= 0;
178 for (tp
= thread_list
; tp
; tp
= tpnext
)
185 threads_executing
= 0;
188 /* Allocate a new thread with target id PTID and add it to the thread
191 static struct thread_info
*
192 new_thread (ptid_t ptid
)
194 struct thread_info
*tp
;
196 tp
= xcalloc (1, sizeof (*tp
));
199 tp
->num
= ++highest_thread_num
;
200 tp
->next
= thread_list
;
203 /* Nothing to follow yet. */
204 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
205 tp
->state
= THREAD_STOPPED
;
211 add_thread_silent (ptid_t ptid
)
213 struct thread_info
*tp
;
215 tp
= find_thread_ptid (ptid
);
217 /* Found an old thread with the same id. It has to be dead,
218 otherwise we wouldn't be adding a new thread with the same id.
219 The OS is reusing this id --- delete it, and recreate a new
222 /* In addition to deleting the thread, if this is the current
223 thread, then we need to take care that delete_thread doesn't
224 really delete the thread if it is inferior_ptid. Create a
225 new template thread in the list with an invalid ptid, switch
226 to it, delete the original thread, reset the new thread's
227 ptid, and switch to it. */
229 if (ptid_equal (inferior_ptid
, ptid
))
231 tp
= new_thread (null_ptid
);
233 /* Make switch_to_thread not read from the thread. */
234 tp
->state
= THREAD_EXITED
;
235 switch_to_thread (null_ptid
);
237 /* Now we can delete it. */
238 delete_thread (ptid
);
240 /* Now reset its ptid, and reswitch inferior_ptid to it. */
242 tp
->state
= THREAD_STOPPED
;
243 switch_to_thread (ptid
);
245 observer_notify_new_thread (tp
);
251 /* Just go ahead and delete it. */
252 delete_thread (ptid
);
255 tp
= new_thread (ptid
);
256 observer_notify_new_thread (tp
);
262 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
264 struct thread_info
*result
= add_thread_silent (ptid
);
266 result
->private = private;
268 if (print_thread_events
)
269 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
271 annotate_new_thread ();
276 add_thread (ptid_t ptid
)
278 return add_thread_with_info (ptid
, NULL
);
281 /* Delete thread PTID. If SILENT, don't notify the observer of this
284 delete_thread_1 (ptid_t ptid
, int silent
)
286 struct thread_info
*tp
, *tpprev
;
290 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
291 if (ptid_equal (tp
->ptid
, ptid
))
297 /* If this is the current thread, or there's code out there that
298 relies on it existing (refcount > 0) we can't delete yet. Mark
299 it as exited, and notify it. */
301 || ptid_equal (tp
->ptid
, inferior_ptid
))
303 if (tp
->state
!= THREAD_EXITED
)
305 observer_notify_thread_exit (tp
, silent
);
307 /* Tag it as exited. */
308 tp
->state
= THREAD_EXITED
;
310 /* Clear breakpoints, etc. associated with this thread. */
311 clear_thread_inferior_resources (tp
);
314 /* Will be really deleted some other time. */
318 /* Notify thread exit, but only if we haven't already. */
319 if (tp
->state
!= THREAD_EXITED
)
320 observer_notify_thread_exit (tp
, silent
);
322 /* Tag it as exited. */
323 tp
->state
= THREAD_EXITED
;
324 clear_thread_inferior_resources (tp
);
327 tpprev
->next
= tp
->next
;
329 thread_list
= tp
->next
;
334 /* Delete thread PTID and notify of thread exit. If this is
335 inferior_ptid, don't actually delete it, but tag it as exited and
336 do the notification. If PTID is the user selected thread, clear
339 delete_thread (ptid_t ptid
)
341 delete_thread_1 (ptid
, 0 /* not silent */);
345 delete_thread_silent (ptid_t ptid
)
347 delete_thread_1 (ptid
, 1 /* silent */);
351 find_thread_id (int num
)
353 struct thread_info
*tp
;
355 for (tp
= thread_list
; tp
; tp
= tp
->next
)
362 /* Find a thread_info by matching PTID. */
364 find_thread_ptid (ptid_t ptid
)
366 struct thread_info
*tp
;
368 for (tp
= thread_list
; tp
; tp
= tp
->next
)
369 if (ptid_equal (tp
->ptid
, ptid
))
376 * Thread iterator function.
378 * Calls a callback function once for each thread, so long as
379 * the callback function returns false. If the callback function
380 * returns true, the iteration will end and the current thread
381 * will be returned. This can be useful for implementing a
382 * search for a thread with arbitrary attributes, or for applying
383 * some operation to every thread.
385 * FIXME: some of the existing functionality, such as
386 * "Thread apply all", might be rewritten using this functionality.
390 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
393 struct thread_info
*tp
, *next
;
395 for (tp
= thread_list
; tp
; tp
= next
)
398 if ((*callback
) (tp
, data
))
409 struct thread_info
*tp
;
411 for (tp
= thread_list
; tp
; tp
= tp
->next
)
418 valid_thread_id (int num
)
420 struct thread_info
*tp
;
422 for (tp
= thread_list
; tp
; tp
= tp
->next
)
430 pid_to_thread_id (ptid_t ptid
)
432 struct thread_info
*tp
;
434 for (tp
= thread_list
; tp
; tp
= tp
->next
)
435 if (ptid_equal (tp
->ptid
, ptid
))
442 thread_id_to_pid (int num
)
444 struct thread_info
*thread
= find_thread_id (num
);
449 return pid_to_ptid (-1);
453 in_thread_list (ptid_t ptid
)
455 struct thread_info
*tp
;
457 for (tp
= thread_list
; tp
; tp
= tp
->next
)
458 if (ptid_equal (tp
->ptid
, ptid
))
461 return 0; /* Never heard of 'im. */
464 /* Finds the first thread of the inferior given by PID. If PID is -1,
465 return the first thread in the list. */
468 first_thread_of_process (int pid
)
470 struct thread_info
*tp
, *ret
= NULL
;
472 for (tp
= thread_list
; tp
; tp
= tp
->next
)
473 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
474 if (ret
== NULL
|| tp
->num
< ret
->num
)
481 any_thread_of_process (int pid
)
483 struct thread_info
*tp
;
485 gdb_assert (pid
!= 0);
487 /* Prefer the current thread. */
488 if (ptid_get_pid (inferior_ptid
) == pid
)
489 return inferior_thread ();
491 ALL_NON_EXITED_THREADS (tp
)
492 if (ptid_get_pid (tp
->ptid
) == pid
)
499 any_live_thread_of_process (int pid
)
501 struct thread_info
*curr_tp
= NULL
;
502 struct thread_info
*tp
;
503 struct thread_info
*tp_executing
= NULL
;
505 gdb_assert (pid
!= 0);
507 /* Prefer the current thread if it's not executing. */
508 if (ptid_get_pid (inferior_ptid
) == pid
)
510 /* If the current thread is dead, forget it. If it's not
511 executing, use it. Otherwise, still choose it (below), but
512 only if no other non-executing thread is found. */
513 curr_tp
= inferior_thread ();
514 if (curr_tp
->state
== THREAD_EXITED
)
516 else if (!curr_tp
->executing
)
520 ALL_NON_EXITED_THREADS (tp
)
521 if (ptid_get_pid (tp
->ptid
) == pid
)
529 /* If both the current thread and all live threads are executing,
530 prefer the current thread. */
534 /* Otherwise, just return an executing thread, if any. */
538 /* Print a list of thread ids currently known, and the total number of
539 threads. To be used from within catch_errors. */
541 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
543 struct thread_info
*tp
;
545 struct cleanup
*cleanup_chain
;
546 int current_thread
= -1;
548 update_thread_list ();
550 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
552 for (tp
= thread_list
; tp
; tp
= tp
->next
)
554 if (tp
->state
== THREAD_EXITED
)
557 if (ptid_equal (tp
->ptid
, inferior_ptid
))
558 current_thread
= tp
->num
;
561 ui_out_field_int (uiout
, "thread-id", tp
->num
);
564 do_cleanups (cleanup_chain
);
566 if (current_thread
!= -1)
567 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
568 ui_out_field_int (uiout
, "number-of-threads", num
);
572 /* Official gdblib interface function to get a list of thread ids and
575 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
577 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
578 error_message
, RETURN_MASK_ALL
) < 0)
583 /* Return true if TP is an active thread. */
585 thread_alive (struct thread_info
*tp
)
587 if (tp
->state
== THREAD_EXITED
)
589 if (!target_thread_alive (tp
->ptid
))
597 struct thread_info
*tp
, *next
;
599 for (tp
= thread_list
; tp
; tp
= next
)
602 if (!thread_alive (tp
))
603 delete_thread (tp
->ptid
);
608 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
610 struct inferior
*inf
;
611 struct thread_info
*tp
;
613 /* It can happen that what we knew as the target inferior id
614 changes. E.g, target remote may only discover the remote process
615 pid after adding the inferior to GDB's list. */
616 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
617 inf
->pid
= ptid_get_pid (new_ptid
);
619 tp
= find_thread_ptid (old_ptid
);
622 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
626 set_running (ptid_t ptid
, int running
)
628 struct thread_info
*tp
;
629 int all
= ptid_equal (ptid
, minus_one_ptid
);
631 /* We try not to notify the observer if no thread has actually changed
632 the running state -- merely to reduce the number of messages to
633 frontend. Frontend is supposed to handle multiple *running just fine. */
634 if (all
|| ptid_is_pid (ptid
))
638 for (tp
= thread_list
; tp
; tp
= tp
->next
)
639 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
641 if (tp
->state
== THREAD_EXITED
)
643 if (running
&& tp
->state
== THREAD_STOPPED
)
645 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
648 observer_notify_target_resumed (ptid
);
654 tp
= find_thread_ptid (ptid
);
656 gdb_assert (tp
->state
!= THREAD_EXITED
);
657 if (running
&& tp
->state
== THREAD_STOPPED
)
659 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
661 observer_notify_target_resumed (ptid
);
666 is_thread_state (ptid_t ptid
, enum thread_state state
)
668 struct thread_info
*tp
;
670 tp
= find_thread_ptid (ptid
);
672 return tp
->state
== state
;
676 is_stopped (ptid_t ptid
)
678 return is_thread_state (ptid
, THREAD_STOPPED
);
682 is_exited (ptid_t ptid
)
684 return is_thread_state (ptid
, THREAD_EXITED
);
688 is_running (ptid_t ptid
)
690 return is_thread_state (ptid
, THREAD_RUNNING
);
694 is_executing (ptid_t ptid
)
696 struct thread_info
*tp
;
698 tp
= find_thread_ptid (ptid
);
700 return tp
->executing
;
704 set_executing (ptid_t ptid
, int executing
)
706 struct thread_info
*tp
;
707 int all
= ptid_equal (ptid
, minus_one_ptid
);
709 if (all
|| ptid_is_pid (ptid
))
711 for (tp
= thread_list
; tp
; tp
= tp
->next
)
712 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
713 tp
->executing
= executing
;
717 tp
= find_thread_ptid (ptid
);
719 tp
->executing
= executing
;
722 /* It only takes one running thread to spawn more threads.*/
724 threads_executing
= 1;
725 /* Only clear the flag if the caller is telling us everything is
727 else if (ptid_equal (minus_one_ptid
, ptid
))
728 threads_executing
= 0;
731 /* See gdbthread.h. */
734 threads_are_executing (void)
736 return threads_executing
;
740 set_stop_requested (ptid_t ptid
, int stop
)
742 struct thread_info
*tp
;
743 int all
= ptid_equal (ptid
, minus_one_ptid
);
745 if (all
|| ptid_is_pid (ptid
))
747 for (tp
= thread_list
; tp
; tp
= tp
->next
)
748 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
749 tp
->stop_requested
= stop
;
753 tp
= find_thread_ptid (ptid
);
755 tp
->stop_requested
= stop
;
758 /* Call the stop requested observer so other components of GDB can
759 react to this request. */
761 observer_notify_thread_stop_requested (ptid
);
765 finish_thread_state (ptid_t ptid
)
767 struct thread_info
*tp
;
771 all
= ptid_equal (ptid
, minus_one_ptid
);
773 if (all
|| ptid_is_pid (ptid
))
775 for (tp
= thread_list
; tp
; tp
= tp
->next
)
777 if (tp
->state
== THREAD_EXITED
)
779 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
781 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
783 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
789 tp
= find_thread_ptid (ptid
);
791 if (tp
->state
!= THREAD_EXITED
)
793 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
795 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
800 observer_notify_target_resumed (ptid
);
804 finish_thread_state_cleanup (void *arg
)
806 ptid_t
*ptid_p
= arg
;
810 finish_thread_state (*ptid_p
);
814 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
816 return (pc
>= thread
->control
.step_range_start
817 && pc
< thread
->control
.step_range_end
);
820 /* Prints the list of threads and their details on UIOUT.
821 This is a version of 'info_threads_command' suitable for
823 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
824 that should be printed. Otherwise, all threads are
826 If PID is not -1, only print threads from the process PID.
827 Otherwise, threads from all attached PIDs are printed.
828 If both REQUESTED_THREAD and PID are not -1, then the thread
829 is printed if it belongs to the specified process. Otherwise,
830 an error is raised. */
832 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
834 struct thread_info
*tp
;
836 struct cleanup
*old_chain
;
837 char *extra_info
, *name
, *target_id
;
838 int current_thread
= -1;
840 update_thread_list ();
841 current_ptid
= inferior_ptid
;
843 /* We'll be switching threads temporarily. */
844 old_chain
= make_cleanup_restore_current_thread ();
846 /* For backward compatibility, we make a list for MI. A table is
847 preferable for the CLI, though, because it shows table
849 if (ui_out_is_mi_like_p (uiout
))
850 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
855 for (tp
= thread_list
; tp
; tp
= tp
->next
)
857 if (!number_is_in_list (requested_threads
, tp
->num
))
860 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
863 if (tp
->state
== THREAD_EXITED
)
871 if (requested_threads
== NULL
|| *requested_threads
== '\0')
872 ui_out_message (uiout
, 0, _("No threads.\n"));
874 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
876 do_cleanups (old_chain
);
880 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
882 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
883 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
884 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
885 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
886 ui_out_table_body (uiout
);
889 for (tp
= thread_list
; tp
; tp
= tp
->next
)
891 struct cleanup
*chain2
;
894 if (!number_is_in_list (requested_threads
, tp
->num
))
897 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
899 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
900 error (_("Requested thread not found in requested process"));
904 if (ptid_equal (tp
->ptid
, current_ptid
))
905 current_thread
= tp
->num
;
907 if (tp
->state
== THREAD_EXITED
)
910 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
912 if (ui_out_is_mi_like_p (uiout
))
915 if (ptid_equal (tp
->ptid
, current_ptid
))
916 ui_out_text (uiout
, "* ");
918 ui_out_text (uiout
, " ");
922 if (ptid_equal (tp
->ptid
, current_ptid
))
923 ui_out_field_string (uiout
, "current", "*");
925 ui_out_field_skip (uiout
, "current");
928 ui_out_field_int (uiout
, "id", tp
->num
);
930 /* For the CLI, we stuff everything into the target-id field.
931 This is a gross hack to make the output come out looking
932 correct. The underlying problem here is that ui-out has no
933 way to specify that a field's space allocation should be
934 shared by several fields. For MI, we do the right thing
937 target_id
= target_pid_to_str (tp
->ptid
);
938 extra_info
= target_extra_thread_info (tp
);
939 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
941 if (ui_out_is_mi_like_p (uiout
))
943 ui_out_field_string (uiout
, "target-id", target_id
);
945 ui_out_field_string (uiout
, "details", extra_info
);
947 ui_out_field_string (uiout
, "name", name
);
951 struct cleanup
*str_cleanup
;
954 if (extra_info
&& name
)
955 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
958 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
960 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
962 contents
= xstrdup (target_id
);
963 str_cleanup
= make_cleanup (xfree
, contents
);
965 ui_out_field_string (uiout
, "target-id", contents
);
966 do_cleanups (str_cleanup
);
969 if (tp
->state
== THREAD_RUNNING
)
970 ui_out_text (uiout
, "(running)\n");
973 /* The switch below puts us at the top of the stack (leaf
975 switch_to_thread (tp
->ptid
);
976 print_stack_frame (get_selected_frame (NULL
),
977 /* For MI output, print frame level. */
978 ui_out_is_mi_like_p (uiout
),
982 if (ui_out_is_mi_like_p (uiout
))
984 char *state
= "stopped";
986 if (tp
->state
== THREAD_RUNNING
)
988 ui_out_field_string (uiout
, "state", state
);
991 core
= target_core_of_thread (tp
->ptid
);
992 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
993 ui_out_field_int (uiout
, "core", core
);
995 do_cleanups (chain2
);
998 /* Restores the current thread and the frame selected before
999 the "info threads" command. */
1000 do_cleanups (old_chain
);
1002 if (pid
== -1 && requested_threads
== NULL
)
1004 gdb_assert (current_thread
!= -1
1006 || ptid_equal (inferior_ptid
, null_ptid
));
1007 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
1008 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
1010 if (current_thread
!= -1 && is_exited (current_ptid
))
1011 ui_out_message (uiout
, 0, "\n\
1012 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1014 else if (thread_list
1015 && current_thread
== -1
1016 && ptid_equal (current_ptid
, null_ptid
))
1017 ui_out_message (uiout
, 0, "\n\
1018 No selected thread. See `help thread'.\n");
1022 /* Print information about currently known threads
1024 Optional ARG is a thread id, or list of thread ids.
1026 Note: this has the drawback that it _really_ switches
1027 threads, which frees the frame cache. A no-side
1028 effects info-threads command would be nicer. */
1031 info_threads_command (char *arg
, int from_tty
)
1033 print_thread_info (current_uiout
, arg
, -1);
1036 /* Switch from one thread to another. */
1039 switch_to_thread (ptid_t ptid
)
1041 /* Switch the program space as well, if we can infer it from the now
1042 current thread. Otherwise, it's up to the caller to select the
1044 if (!ptid_equal (ptid
, null_ptid
))
1046 struct inferior
*inf
;
1048 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1049 gdb_assert (inf
!= NULL
);
1050 set_current_program_space (inf
->pspace
);
1051 set_current_inferior (inf
);
1054 if (ptid_equal (ptid
, inferior_ptid
))
1057 inferior_ptid
= ptid
;
1058 reinit_frame_cache ();
1060 /* We don't check for is_stopped, because we're called at times
1061 while in the TARGET_RUNNING state, e.g., while handling an
1063 if (!ptid_equal (inferior_ptid
, null_ptid
)
1064 && !is_exited (ptid
)
1065 && !is_executing (ptid
))
1066 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1068 stop_pc
= ~(CORE_ADDR
) 0;
1072 restore_current_thread (ptid_t ptid
)
1074 switch_to_thread (ptid
);
1078 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1080 struct frame_info
*frame
= NULL
;
1083 /* This means there was no selected frame. */
1084 if (frame_level
== -1)
1086 select_frame (NULL
);
1090 gdb_assert (frame_level
>= 0);
1092 /* Restore by level first, check if the frame id is the same as
1093 expected. If that fails, try restoring by frame id. If that
1094 fails, nothing to do, just warn the user. */
1096 count
= frame_level
;
1097 frame
= find_relative_frame (get_current_frame (), &count
);
1100 /* The frame ids must match - either both valid or both outer_frame_id.
1101 The latter case is not failsafe, but since it's highly unlikely
1102 the search by level finds the wrong frame, it's 99.9(9)% of
1103 the time (for all practical purposes) safe. */
1104 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1106 /* Cool, all is fine. */
1107 select_frame (frame
);
1111 frame
= frame_find_by_id (a_frame_id
);
1114 /* Cool, refound it. */
1115 select_frame (frame
);
1119 /* Nothing else to do, the frame layout really changed. Select the
1120 innermost stack frame. */
1121 select_frame (get_current_frame ());
1123 /* Warn the user. */
1124 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1126 warning (_("Couldn't restore frame #%d in "
1127 "current thread. Bottom (innermost) frame selected:"),
1129 /* For MI, we should probably have a notification about
1130 current frame change. But this error is not very
1131 likely, so don't bother for now. */
1132 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1136 struct current_thread_cleanup
1138 ptid_t inferior_ptid
;
1139 struct frame_id selected_frame_id
;
1140 int selected_frame_level
;
1147 do_restore_current_thread_cleanup (void *arg
)
1149 struct thread_info
*tp
;
1150 struct current_thread_cleanup
*old
= arg
;
1152 tp
= find_thread_ptid (old
->inferior_ptid
);
1154 /* If the previously selected thread belonged to a process that has
1155 in the mean time been deleted (due to normal exit, detach, etc.),
1156 then don't revert back to it, but instead simply drop back to no
1159 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1160 restore_current_thread (old
->inferior_ptid
);
1163 restore_current_thread (null_ptid
);
1164 set_current_inferior (find_inferior_id (old
->inf_id
));
1167 /* The running state of the originally selected thread may have
1168 changed, so we have to recheck it here. */
1169 if (!ptid_equal (inferior_ptid
, null_ptid
)
1171 && is_stopped (inferior_ptid
)
1172 && target_has_registers
1174 && target_has_memory
)
1175 restore_selected_frame (old
->selected_frame_id
,
1176 old
->selected_frame_level
);
1180 restore_current_thread_cleanup_dtor (void *arg
)
1182 struct current_thread_cleanup
*old
= arg
;
1183 struct thread_info
*tp
;
1184 struct inferior
*inf
;
1186 tp
= find_thread_ptid (old
->inferior_ptid
);
1189 inf
= find_inferior_id (old
->inf_id
);
1191 inf
->removable
= old
->was_removable
;
1195 /* Set the thread reference count. */
1198 set_thread_refcount (void *data
)
1201 struct thread_array_cleanup
*ta_cleanup
= data
;
1203 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1204 ta_cleanup
->tp_array
[k
]->refcount
--;
1208 make_cleanup_restore_current_thread (void)
1210 struct thread_info
*tp
;
1211 struct frame_info
*frame
;
1212 struct current_thread_cleanup
*old
;
1214 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1215 old
->inferior_ptid
= inferior_ptid
;
1216 old
->inf_id
= current_inferior ()->num
;
1217 old
->was_removable
= current_inferior ()->removable
;
1219 if (!ptid_equal (inferior_ptid
, null_ptid
))
1221 old
->was_stopped
= is_stopped (inferior_ptid
);
1222 if (old
->was_stopped
1223 && target_has_registers
1225 && target_has_memory
)
1227 /* When processing internal events, there might not be a
1228 selected frame. If we naively call get_selected_frame
1229 here, then we can end up reading debuginfo for the
1230 current frame, but we don't generally need the debuginfo
1232 frame
= get_selected_frame_if_set ();
1237 old
->selected_frame_id
= get_frame_id (frame
);
1238 old
->selected_frame_level
= frame_relative_level (frame
);
1240 tp
= find_thread_ptid (inferior_ptid
);
1245 current_inferior ()->removable
= 0;
1247 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1248 restore_current_thread_cleanup_dtor
);
1251 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1252 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1253 of two numbers seperated by a hyphen. Examples:
1255 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1256 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1257 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1260 thread_apply_all_command (char *cmd
, int from_tty
)
1262 struct cleanup
*old_chain
;
1265 struct thread_array_cleanup ta_cleanup
;
1267 if (cmd
== NULL
|| *cmd
== '\000')
1268 error (_("Please specify a command following the thread ID list"));
1270 update_thread_list ();
1272 old_chain
= make_cleanup_restore_current_thread ();
1274 /* Save a copy of the command in case it is clobbered by
1276 saved_cmd
= xstrdup (cmd
);
1277 make_cleanup (xfree
, saved_cmd
);
1278 tc
= thread_count ();
1282 struct thread_info
**tp_array
;
1283 struct thread_info
*tp
;
1286 /* Save a copy of the thread_list in case we execute detach
1288 tp_array
= xmalloc (sizeof (struct thread_info
*) * tc
);
1289 make_cleanup (xfree
, tp_array
);
1290 ta_cleanup
.tp_array
= tp_array
;
1291 ta_cleanup
.count
= tc
;
1293 ALL_NON_EXITED_THREADS (tp
)
1300 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1302 for (k
= 0; k
!= i
; k
++)
1303 if (thread_alive (tp_array
[k
]))
1305 switch_to_thread (tp_array
[k
]->ptid
);
1306 printf_filtered (_("\nThread %d (%s):\n"),
1308 target_pid_to_str (inferior_ptid
));
1309 execute_command (cmd
, from_tty
);
1311 /* Restore exact command used previously. */
1312 strcpy (cmd
, saved_cmd
);
1316 do_cleanups (old_chain
);
1320 thread_apply_command (char *tidlist
, int from_tty
)
1323 struct cleanup
*old_chain
;
1325 struct get_number_or_range_state state
;
1327 if (tidlist
== NULL
|| *tidlist
== '\000')
1328 error (_("Please specify a thread ID list"));
1330 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1333 error (_("Please specify a command following the thread ID list"));
1335 /* Save a copy of the command in case it is clobbered by
1337 saved_cmd
= xstrdup (cmd
);
1338 old_chain
= make_cleanup (xfree
, saved_cmd
);
1340 init_number_or_range (&state
, tidlist
);
1341 while (!state
.finished
&& state
.string
< cmd
)
1343 struct thread_info
*tp
;
1346 start
= get_number_or_range (&state
);
1348 make_cleanup_restore_current_thread ();
1350 tp
= find_thread_id (start
);
1353 warning (_("Unknown thread %d."), start
);
1354 else if (!thread_alive (tp
))
1355 warning (_("Thread %d has terminated."), start
);
1358 switch_to_thread (tp
->ptid
);
1360 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1361 target_pid_to_str (inferior_ptid
));
1362 execute_command (cmd
, from_tty
);
1364 /* Restore exact command used previously. */
1365 strcpy (cmd
, saved_cmd
);
1369 do_cleanups (old_chain
);
1372 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1373 if prefix of arg is `apply'. */
1376 thread_command (char *tidstr
, int from_tty
)
1380 if (ptid_equal (inferior_ptid
, null_ptid
))
1381 error (_("No thread selected"));
1383 if (target_has_stack
)
1385 if (is_exited (inferior_ptid
))
1386 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1387 pid_to_thread_id (inferior_ptid
),
1388 target_pid_to_str (inferior_ptid
));
1390 printf_filtered (_("[Current thread is %d (%s)]\n"),
1391 pid_to_thread_id (inferior_ptid
),
1392 target_pid_to_str (inferior_ptid
));
1395 error (_("No stack."));
1399 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1402 /* Implementation of `thread name'. */
1405 thread_name_command (char *arg
, int from_tty
)
1407 struct thread_info
*info
;
1409 if (ptid_equal (inferior_ptid
, null_ptid
))
1410 error (_("No thread selected"));
1412 arg
= skip_spaces (arg
);
1414 info
= inferior_thread ();
1416 info
->name
= arg
? xstrdup (arg
) : NULL
;
1419 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1422 thread_find_command (char *arg
, int from_tty
)
1424 struct thread_info
*tp
;
1426 unsigned long match
= 0;
1428 if (arg
== NULL
|| *arg
== '\0')
1429 error (_("Command requires an argument."));
1431 tmp
= re_comp (arg
);
1433 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1435 update_thread_list ();
1436 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1438 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1440 printf_filtered (_("Thread %d has name '%s'\n"),
1445 tmp
= target_thread_name (tp
);
1446 if (tmp
!= NULL
&& re_exec (tmp
))
1448 printf_filtered (_("Thread %d has target name '%s'\n"),
1453 tmp
= target_pid_to_str (tp
->ptid
);
1454 if (tmp
!= NULL
&& re_exec (tmp
))
1456 printf_filtered (_("Thread %d has target id '%s'\n"),
1461 tmp
= target_extra_thread_info (tp
);
1462 if (tmp
!= NULL
&& re_exec (tmp
))
1464 printf_filtered (_("Thread %d has extra info '%s'\n"),
1470 printf_filtered (_("No threads match '%s'\n"), arg
);
1473 /* Print notices when new threads are attached and detached. */
1474 int print_thread_events
= 1;
1476 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1477 struct cmd_list_element
*c
, const char *value
)
1479 fprintf_filtered (file
,
1480 _("Printing of thread events is %s.\n"),
1485 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1488 struct thread_info
*tp
;
1490 num
= value_as_long (parse_and_eval (tidstr
));
1492 tp
= find_thread_id (num
);
1495 error (_("Thread ID %d not known."), num
);
1497 if (!thread_alive (tp
))
1498 error (_("Thread ID %d has terminated."), num
);
1500 switch_to_thread (tp
->ptid
);
1502 annotate_thread_changed ();
1504 ui_out_text (uiout
, "[Switching to thread ");
1505 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1506 ui_out_text (uiout
, " (");
1507 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1508 ui_out_text (uiout
, ")]");
1510 /* Note that we can't reach this with an exited thread, due to the
1511 thread_alive check above. */
1512 if (tp
->state
== THREAD_RUNNING
)
1513 ui_out_text (uiout
, "(running)\n");
1516 ui_out_text (uiout
, "\n");
1517 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1520 /* Since the current thread may have changed, see if there is any
1521 exited thread we can now delete. */
1528 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1530 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1531 error_message
, RETURN_MASK_ALL
) < 0)
1536 /* Update the 'threads_executing' global based on the threads we know
1540 update_threads_executing (void)
1542 struct thread_info
*tp
;
1544 threads_executing
= 0;
1545 ALL_NON_EXITED_THREADS (tp
)
1549 threads_executing
= 1;
1556 update_thread_list (void)
1559 target_find_new_threads ();
1560 update_threads_executing ();
1563 /* Return a new value for the selected thread's id. Return a value of 0 if
1564 no thread is selected, or no threads exist. */
1566 static struct value
*
1567 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1570 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1572 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1573 (tp
? tp
->num
: 0));
1576 /* Commands with a prefix of `thread'. */
1577 struct cmd_list_element
*thread_cmd_list
= NULL
;
1579 /* Implementation of `thread' variable. */
1581 static const struct internalvar_funcs thread_funcs
=
1583 thread_id_make_value
,
1589 _initialize_thread (void)
1591 static struct cmd_list_element
*thread_apply_list
= NULL
;
1593 add_info ("threads", info_threads_command
,
1594 _("Display currently known threads.\n\
1595 Usage: info threads [ID]...\n\
1596 Optional arguments are thread IDs with spaces between.\n\
1597 If no arguments, all threads are displayed."));
1599 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1600 Use this command to switch between threads.\n\
1601 The new thread ID must be currently known."),
1602 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1604 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1605 _("Apply a command to a list of threads."),
1606 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1608 add_cmd ("all", class_run
, thread_apply_all_command
,
1609 _("Apply a command to all threads."), &thread_apply_list
);
1611 add_cmd ("name", class_run
, thread_name_command
,
1612 _("Set the current thread's name.\n\
1613 Usage: thread name [NAME]\n\
1614 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1616 add_cmd ("find", class_run
, thread_find_command
, _("\
1617 Find threads that match a regular expression.\n\
1618 Usage: thread find REGEXP\n\
1619 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1623 add_com_alias ("t", "thread", class_run
, 1);
1625 add_setshow_boolean_cmd ("thread-events", no_class
,
1626 &print_thread_events
, _("\
1627 Set printing of thread events (such as thread start and exit)."), _("\
1628 Show printing of thread events (such as thread start and exit)."), NULL
,
1630 show_print_thread_events
,
1631 &setprintlist
, &showprintlist
);
1633 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);