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"
30 #include "exceptions.h"
38 #include <sys/types.h>
43 #include "cli/cli-decode.h"
44 #include "gdb_regex.h"
45 #include "cli/cli-utils.h"
46 #include "continuations.h"
48 /* Definition of struct thread_info exported to gdbthread.h. */
50 /* Prototypes for exported functions. */
52 void _initialize_thread (void);
54 /* Prototypes for local functions. */
56 struct thread_info
*thread_list
= NULL
;
57 static int highest_thread_num
;
59 /* True if any thread is, or may be executing. We need to track this
60 separately because until we fully sync the thread list, we won't
61 know whether the target is fully stopped, even if we see stop
62 events for all known threads, because any of those threads may have
63 spawned new threads we haven't heard of yet. */
64 static int threads_executing
;
66 static void thread_command (char *tidstr
, int from_tty
);
67 static void thread_apply_all_command (char *, int);
68 static int thread_alive (struct thread_info
*);
69 static void info_threads_command (char *, int);
70 static void thread_apply_command (char *, int);
71 static void restore_current_thread (ptid_t
);
72 static void prune_threads (void);
74 /* Data to cleanup thread array. */
76 struct thread_array_cleanup
78 /* Array of thread pointers used to set
80 struct thread_info
**tp_array
;
82 /* Thread count in the array. */
88 inferior_thread (void)
90 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
96 delete_step_resume_breakpoint (struct thread_info
*tp
)
98 if (tp
&& tp
->control
.step_resume_breakpoint
)
100 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
101 tp
->control
.step_resume_breakpoint
= NULL
;
106 delete_exception_resume_breakpoint (struct thread_info
*tp
)
108 if (tp
&& tp
->control
.exception_resume_breakpoint
)
110 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
111 tp
->control
.exception_resume_breakpoint
= NULL
;
116 clear_thread_inferior_resources (struct thread_info
*tp
)
118 /* NOTE: this will take care of any left-over step_resume breakpoints,
119 but not any user-specified thread-specific breakpoints. We can not
120 delete the breakpoint straight-off, because the inferior might not
121 be stopped at the moment. */
122 if (tp
->control
.step_resume_breakpoint
)
124 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
125 tp
->control
.step_resume_breakpoint
= NULL
;
128 if (tp
->control
.exception_resume_breakpoint
)
130 tp
->control
.exception_resume_breakpoint
->disposition
131 = disp_del_at_next_stop
;
132 tp
->control
.exception_resume_breakpoint
= NULL
;
135 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
137 bpstat_clear (&tp
->control
.stop_bpstat
);
139 btrace_teardown (tp
);
141 do_all_intermediate_continuations_thread (tp
, 1);
142 do_all_continuations_thread (tp
, 1);
146 free_thread (struct thread_info
*tp
)
150 if (tp
->private_dtor
)
151 tp
->private_dtor (tp
->private);
161 init_thread_list (void)
163 struct thread_info
*tp
, *tpnext
;
165 highest_thread_num
= 0;
170 for (tp
= thread_list
; tp
; tp
= tpnext
)
177 threads_executing
= 0;
180 /* Allocate a new thread with target id PTID and add it to the thread
183 static struct thread_info
*
184 new_thread (ptid_t ptid
)
186 struct thread_info
*tp
;
188 tp
= xcalloc (1, sizeof (*tp
));
191 tp
->num
= ++highest_thread_num
;
192 tp
->next
= thread_list
;
195 /* Nothing to follow yet. */
196 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
197 tp
->state
= THREAD_STOPPED
;
203 add_thread_silent (ptid_t ptid
)
205 struct thread_info
*tp
;
207 tp
= find_thread_ptid (ptid
);
209 /* Found an old thread with the same id. It has to be dead,
210 otherwise we wouldn't be adding a new thread with the same id.
211 The OS is reusing this id --- delete it, and recreate a new
214 /* In addition to deleting the thread, if this is the current
215 thread, then we need to take care that delete_thread doesn't
216 really delete the thread if it is inferior_ptid. Create a
217 new template thread in the list with an invalid ptid, switch
218 to it, delete the original thread, reset the new thread's
219 ptid, and switch to it. */
221 if (ptid_equal (inferior_ptid
, ptid
))
223 tp
= new_thread (null_ptid
);
225 /* Make switch_to_thread not read from the thread. */
226 tp
->state
= THREAD_EXITED
;
227 switch_to_thread (null_ptid
);
229 /* Now we can delete it. */
230 delete_thread (ptid
);
232 /* Now reset its ptid, and reswitch inferior_ptid to it. */
234 tp
->state
= THREAD_STOPPED
;
235 switch_to_thread (ptid
);
237 observer_notify_new_thread (tp
);
243 /* Just go ahead and delete it. */
244 delete_thread (ptid
);
247 tp
= new_thread (ptid
);
248 observer_notify_new_thread (tp
);
254 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
256 struct thread_info
*result
= add_thread_silent (ptid
);
258 result
->private = private;
260 if (print_thread_events
)
261 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
263 annotate_new_thread ();
268 add_thread (ptid_t ptid
)
270 return add_thread_with_info (ptid
, NULL
);
273 /* Delete thread PTID. If SILENT, don't notify the observer of this
276 delete_thread_1 (ptid_t ptid
, int silent
)
278 struct thread_info
*tp
, *tpprev
;
282 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
283 if (ptid_equal (tp
->ptid
, ptid
))
289 /* If this is the current thread, or there's code out there that
290 relies on it existing (refcount > 0) we can't delete yet. Mark
291 it as exited, and notify it. */
293 || ptid_equal (tp
->ptid
, inferior_ptid
))
295 if (tp
->state
!= THREAD_EXITED
)
297 observer_notify_thread_exit (tp
, silent
);
299 /* Tag it as exited. */
300 tp
->state
= THREAD_EXITED
;
302 /* Clear breakpoints, etc. associated with this thread. */
303 clear_thread_inferior_resources (tp
);
306 /* Will be really deleted some other time. */
310 /* Notify thread exit, but only if we haven't already. */
311 if (tp
->state
!= THREAD_EXITED
)
312 observer_notify_thread_exit (tp
, silent
);
314 /* Tag it as exited. */
315 tp
->state
= THREAD_EXITED
;
316 clear_thread_inferior_resources (tp
);
319 tpprev
->next
= tp
->next
;
321 thread_list
= tp
->next
;
326 /* Delete thread PTID and notify of thread exit. If this is
327 inferior_ptid, don't actually delete it, but tag it as exited and
328 do the notification. If PTID is the user selected thread, clear
331 delete_thread (ptid_t ptid
)
333 delete_thread_1 (ptid
, 0 /* not silent */);
337 delete_thread_silent (ptid_t ptid
)
339 delete_thread_1 (ptid
, 1 /* silent */);
343 find_thread_id (int num
)
345 struct thread_info
*tp
;
347 for (tp
= thread_list
; tp
; tp
= tp
->next
)
354 /* Find a thread_info by matching PTID. */
356 find_thread_ptid (ptid_t ptid
)
358 struct thread_info
*tp
;
360 for (tp
= thread_list
; tp
; tp
= tp
->next
)
361 if (ptid_equal (tp
->ptid
, ptid
))
368 * Thread iterator function.
370 * Calls a callback function once for each thread, so long as
371 * the callback function returns false. If the callback function
372 * returns true, the iteration will end and the current thread
373 * will be returned. This can be useful for implementing a
374 * search for a thread with arbitrary attributes, or for applying
375 * some operation to every thread.
377 * FIXME: some of the existing functionality, such as
378 * "Thread apply all", might be rewritten using this functionality.
382 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
385 struct thread_info
*tp
, *next
;
387 for (tp
= thread_list
; tp
; tp
= next
)
390 if ((*callback
) (tp
, data
))
401 struct thread_info
*tp
;
403 for (tp
= thread_list
; tp
; tp
= tp
->next
)
410 valid_thread_id (int num
)
412 struct thread_info
*tp
;
414 for (tp
= thread_list
; tp
; tp
= tp
->next
)
422 pid_to_thread_id (ptid_t ptid
)
424 struct thread_info
*tp
;
426 for (tp
= thread_list
; tp
; tp
= tp
->next
)
427 if (ptid_equal (tp
->ptid
, ptid
))
434 thread_id_to_pid (int num
)
436 struct thread_info
*thread
= find_thread_id (num
);
441 return pid_to_ptid (-1);
445 in_thread_list (ptid_t ptid
)
447 struct thread_info
*tp
;
449 for (tp
= thread_list
; tp
; tp
= tp
->next
)
450 if (ptid_equal (tp
->ptid
, ptid
))
453 return 0; /* Never heard of 'im. */
456 /* Finds the first thread of the inferior given by PID. If PID is -1,
457 return the first thread in the list. */
460 first_thread_of_process (int pid
)
462 struct thread_info
*tp
, *ret
= NULL
;
464 for (tp
= thread_list
; tp
; tp
= tp
->next
)
465 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
466 if (ret
== NULL
|| tp
->num
< ret
->num
)
473 any_thread_of_process (int pid
)
475 struct thread_info
*tp
;
477 gdb_assert (pid
!= 0);
479 /* Prefer the current thread. */
480 if (ptid_get_pid (inferior_ptid
) == pid
)
481 return inferior_thread ();
483 ALL_NON_EXITED_THREADS (tp
)
484 if (ptid_get_pid (tp
->ptid
) == pid
)
491 any_live_thread_of_process (int pid
)
493 struct thread_info
*curr_tp
= NULL
;
494 struct thread_info
*tp
;
495 struct thread_info
*tp_executing
= NULL
;
497 gdb_assert (pid
!= 0);
499 /* Prefer the current thread if it's not executing. */
500 if (ptid_get_pid (inferior_ptid
) == pid
)
502 /* If the current thread is dead, forget it. If it's not
503 executing, use it. Otherwise, still choose it (below), but
504 only if no other non-executing thread is found. */
505 curr_tp
= inferior_thread ();
506 if (curr_tp
->state
== THREAD_EXITED
)
508 else if (!curr_tp
->executing
)
512 ALL_NON_EXITED_THREADS (tp
)
513 if (ptid_get_pid (tp
->ptid
) == pid
)
521 /* If both the current thread and all live threads are executing,
522 prefer the current thread. */
526 /* Otherwise, just return an executing thread, if any. */
530 /* Print a list of thread ids currently known, and the total number of
531 threads. To be used from within catch_errors. */
533 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
535 struct thread_info
*tp
;
537 struct cleanup
*cleanup_chain
;
538 int current_thread
= -1;
540 update_thread_list ();
542 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
544 for (tp
= thread_list
; tp
; tp
= tp
->next
)
546 if (tp
->state
== THREAD_EXITED
)
549 if (ptid_equal (tp
->ptid
, inferior_ptid
))
550 current_thread
= tp
->num
;
553 ui_out_field_int (uiout
, "thread-id", tp
->num
);
556 do_cleanups (cleanup_chain
);
558 if (current_thread
!= -1)
559 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
560 ui_out_field_int (uiout
, "number-of-threads", num
);
564 /* Official gdblib interface function to get a list of thread ids and
567 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
569 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
570 error_message
, RETURN_MASK_ALL
) < 0)
575 /* Return true if TP is an active thread. */
577 thread_alive (struct thread_info
*tp
)
579 if (tp
->state
== THREAD_EXITED
)
581 if (!target_thread_alive (tp
->ptid
))
589 struct thread_info
*tp
, *next
;
591 for (tp
= thread_list
; tp
; tp
= next
)
594 if (!thread_alive (tp
))
595 delete_thread (tp
->ptid
);
600 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
602 struct inferior
*inf
;
603 struct thread_info
*tp
;
605 /* It can happen that what we knew as the target inferior id
606 changes. E.g, target remote may only discover the remote process
607 pid after adding the inferior to GDB's list. */
608 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
609 inf
->pid
= ptid_get_pid (new_ptid
);
611 tp
= find_thread_ptid (old_ptid
);
614 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
618 set_running (ptid_t ptid
, int running
)
620 struct thread_info
*tp
;
621 int all
= ptid_equal (ptid
, minus_one_ptid
);
623 /* We try not to notify the observer if no thread has actually changed
624 the running state -- merely to reduce the number of messages to
625 frontend. Frontend is supposed to handle multiple *running just fine. */
626 if (all
|| ptid_is_pid (ptid
))
630 for (tp
= thread_list
; tp
; tp
= tp
->next
)
631 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
633 if (tp
->state
== THREAD_EXITED
)
635 if (running
&& tp
->state
== THREAD_STOPPED
)
637 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
640 observer_notify_target_resumed (ptid
);
646 tp
= find_thread_ptid (ptid
);
648 gdb_assert (tp
->state
!= THREAD_EXITED
);
649 if (running
&& tp
->state
== THREAD_STOPPED
)
651 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
653 observer_notify_target_resumed (ptid
);
658 is_thread_state (ptid_t ptid
, enum thread_state state
)
660 struct thread_info
*tp
;
662 tp
= find_thread_ptid (ptid
);
664 return tp
->state
== state
;
668 is_stopped (ptid_t ptid
)
670 return is_thread_state (ptid
, THREAD_STOPPED
);
674 is_exited (ptid_t ptid
)
676 return is_thread_state (ptid
, THREAD_EXITED
);
680 is_running (ptid_t ptid
)
682 return is_thread_state (ptid
, THREAD_RUNNING
);
686 is_executing (ptid_t ptid
)
688 struct thread_info
*tp
;
690 tp
= find_thread_ptid (ptid
);
692 return tp
->executing
;
696 set_executing (ptid_t ptid
, int executing
)
698 struct thread_info
*tp
;
699 int all
= ptid_equal (ptid
, minus_one_ptid
);
701 if (all
|| ptid_is_pid (ptid
))
703 for (tp
= thread_list
; tp
; tp
= tp
->next
)
704 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
705 tp
->executing
= executing
;
709 tp
= find_thread_ptid (ptid
);
711 tp
->executing
= executing
;
714 /* It only takes one running thread to spawn more threads.*/
716 threads_executing
= 1;
717 /* Only clear the flag if the caller is telling us everything is
719 else if (ptid_equal (minus_one_ptid
, ptid
))
720 threads_executing
= 0;
723 /* See gdbthread.h. */
726 threads_are_executing (void)
728 return threads_executing
;
732 set_stop_requested (ptid_t ptid
, int stop
)
734 struct thread_info
*tp
;
735 int all
= ptid_equal (ptid
, minus_one_ptid
);
737 if (all
|| ptid_is_pid (ptid
))
739 for (tp
= thread_list
; tp
; tp
= tp
->next
)
740 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
741 tp
->stop_requested
= stop
;
745 tp
= find_thread_ptid (ptid
);
747 tp
->stop_requested
= stop
;
750 /* Call the stop requested observer so other components of GDB can
751 react to this request. */
753 observer_notify_thread_stop_requested (ptid
);
757 finish_thread_state (ptid_t ptid
)
759 struct thread_info
*tp
;
763 all
= ptid_equal (ptid
, minus_one_ptid
);
765 if (all
|| ptid_is_pid (ptid
))
767 for (tp
= thread_list
; tp
; tp
= tp
->next
)
769 if (tp
->state
== THREAD_EXITED
)
771 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
773 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
775 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
781 tp
= find_thread_ptid (ptid
);
783 if (tp
->state
!= THREAD_EXITED
)
785 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
787 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
792 observer_notify_target_resumed (ptid
);
796 finish_thread_state_cleanup (void *arg
)
798 ptid_t
*ptid_p
= arg
;
802 finish_thread_state (*ptid_p
);
806 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
808 return (pc
>= thread
->control
.step_range_start
809 && pc
< thread
->control
.step_range_end
);
812 /* Prints the list of threads and their details on UIOUT.
813 This is a version of 'info_threads_command' suitable for
815 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
816 that should be printed. Otherwise, all threads are
818 If PID is not -1, only print threads from the process PID.
819 Otherwise, threads from all attached PIDs are printed.
820 If both REQUESTED_THREAD and PID are not -1, then the thread
821 is printed if it belongs to the specified process. Otherwise,
822 an error is raised. */
824 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
826 struct thread_info
*tp
;
828 struct cleanup
*old_chain
;
829 char *extra_info
, *name
, *target_id
;
830 int current_thread
= -1;
832 update_thread_list ();
833 current_ptid
= inferior_ptid
;
835 /* We'll be switching threads temporarily. */
836 old_chain
= make_cleanup_restore_current_thread ();
838 /* For backward compatibility, we make a list for MI. A table is
839 preferable for the CLI, though, because it shows table
841 if (ui_out_is_mi_like_p (uiout
))
842 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
847 for (tp
= thread_list
; tp
; tp
= tp
->next
)
849 if (!number_is_in_list (requested_threads
, tp
->num
))
852 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
855 if (tp
->state
== THREAD_EXITED
)
863 if (requested_threads
== NULL
|| *requested_threads
== '\0')
864 ui_out_message (uiout
, 0, _("No threads.\n"));
866 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
868 do_cleanups (old_chain
);
872 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
874 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
875 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
876 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
877 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
878 ui_out_table_body (uiout
);
881 for (tp
= thread_list
; tp
; tp
= tp
->next
)
883 struct cleanup
*chain2
;
886 if (!number_is_in_list (requested_threads
, tp
->num
))
889 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
891 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
892 error (_("Requested thread not found in requested process"));
896 if (ptid_equal (tp
->ptid
, current_ptid
))
897 current_thread
= tp
->num
;
899 if (tp
->state
== THREAD_EXITED
)
902 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
904 if (ui_out_is_mi_like_p (uiout
))
907 if (ptid_equal (tp
->ptid
, current_ptid
))
908 ui_out_text (uiout
, "* ");
910 ui_out_text (uiout
, " ");
914 if (ptid_equal (tp
->ptid
, current_ptid
))
915 ui_out_field_string (uiout
, "current", "*");
917 ui_out_field_skip (uiout
, "current");
920 ui_out_field_int (uiout
, "id", tp
->num
);
922 /* For the CLI, we stuff everything into the target-id field.
923 This is a gross hack to make the output come out looking
924 correct. The underlying problem here is that ui-out has no
925 way to specify that a field's space allocation should be
926 shared by several fields. For MI, we do the right thing
929 target_id
= target_pid_to_str (tp
->ptid
);
930 extra_info
= target_extra_thread_info (tp
);
931 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
933 if (ui_out_is_mi_like_p (uiout
))
935 ui_out_field_string (uiout
, "target-id", target_id
);
937 ui_out_field_string (uiout
, "details", extra_info
);
939 ui_out_field_string (uiout
, "name", name
);
943 struct cleanup
*str_cleanup
;
946 if (extra_info
&& name
)
947 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
950 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
952 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
954 contents
= xstrdup (target_id
);
955 str_cleanup
= make_cleanup (xfree
, contents
);
957 ui_out_field_string (uiout
, "target-id", contents
);
958 do_cleanups (str_cleanup
);
961 if (tp
->state
== THREAD_RUNNING
)
962 ui_out_text (uiout
, "(running)\n");
965 /* The switch below puts us at the top of the stack (leaf
967 switch_to_thread (tp
->ptid
);
968 print_stack_frame (get_selected_frame (NULL
),
969 /* For MI output, print frame level. */
970 ui_out_is_mi_like_p (uiout
),
974 if (ui_out_is_mi_like_p (uiout
))
976 char *state
= "stopped";
978 if (tp
->state
== THREAD_RUNNING
)
980 ui_out_field_string (uiout
, "state", state
);
983 core
= target_core_of_thread (tp
->ptid
);
984 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
985 ui_out_field_int (uiout
, "core", core
);
987 do_cleanups (chain2
);
990 /* Restores the current thread and the frame selected before
991 the "info threads" command. */
992 do_cleanups (old_chain
);
994 if (pid
== -1 && requested_threads
== NULL
)
996 gdb_assert (current_thread
!= -1
998 || ptid_equal (inferior_ptid
, null_ptid
));
999 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
1000 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
1002 if (current_thread
!= -1 && is_exited (current_ptid
))
1003 ui_out_message (uiout
, 0, "\n\
1004 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1006 else if (thread_list
1007 && current_thread
== -1
1008 && ptid_equal (current_ptid
, null_ptid
))
1009 ui_out_message (uiout
, 0, "\n\
1010 No selected thread. See `help thread'.\n");
1014 /* Print information about currently known threads
1016 Optional ARG is a thread id, or list of thread ids.
1018 Note: this has the drawback that it _really_ switches
1019 threads, which frees the frame cache. A no-side
1020 effects info-threads command would be nicer. */
1023 info_threads_command (char *arg
, int from_tty
)
1025 print_thread_info (current_uiout
, arg
, -1);
1028 /* Switch from one thread to another. */
1031 switch_to_thread (ptid_t ptid
)
1033 /* Switch the program space as well, if we can infer it from the now
1034 current thread. Otherwise, it's up to the caller to select the
1036 if (!ptid_equal (ptid
, null_ptid
))
1038 struct inferior
*inf
;
1040 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1041 gdb_assert (inf
!= NULL
);
1042 set_current_program_space (inf
->pspace
);
1043 set_current_inferior (inf
);
1046 if (ptid_equal (ptid
, inferior_ptid
))
1049 inferior_ptid
= ptid
;
1050 reinit_frame_cache ();
1052 /* We don't check for is_stopped, because we're called at times
1053 while in the TARGET_RUNNING state, e.g., while handling an
1055 if (!ptid_equal (inferior_ptid
, null_ptid
)
1056 && !is_exited (ptid
)
1057 && !is_executing (ptid
))
1058 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1060 stop_pc
= ~(CORE_ADDR
) 0;
1064 restore_current_thread (ptid_t ptid
)
1066 switch_to_thread (ptid
);
1070 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1072 struct frame_info
*frame
= NULL
;
1075 /* This means there was no selected frame. */
1076 if (frame_level
== -1)
1078 select_frame (NULL
);
1082 gdb_assert (frame_level
>= 0);
1084 /* Restore by level first, check if the frame id is the same as
1085 expected. If that fails, try restoring by frame id. If that
1086 fails, nothing to do, just warn the user. */
1088 count
= frame_level
;
1089 frame
= find_relative_frame (get_current_frame (), &count
);
1092 /* The frame ids must match - either both valid or both outer_frame_id.
1093 The latter case is not failsafe, but since it's highly unlikely
1094 the search by level finds the wrong frame, it's 99.9(9)% of
1095 the time (for all practical purposes) safe. */
1096 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1098 /* Cool, all is fine. */
1099 select_frame (frame
);
1103 frame
= frame_find_by_id (a_frame_id
);
1106 /* Cool, refound it. */
1107 select_frame (frame
);
1111 /* Nothing else to do, the frame layout really changed. Select the
1112 innermost stack frame. */
1113 select_frame (get_current_frame ());
1115 /* Warn the user. */
1116 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1118 warning (_("Couldn't restore frame #%d in "
1119 "current thread. Bottom (innermost) frame selected:"),
1121 /* For MI, we should probably have a notification about
1122 current frame change. But this error is not very
1123 likely, so don't bother for now. */
1124 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1128 struct current_thread_cleanup
1130 ptid_t inferior_ptid
;
1131 struct frame_id selected_frame_id
;
1132 int selected_frame_level
;
1139 do_restore_current_thread_cleanup (void *arg
)
1141 struct thread_info
*tp
;
1142 struct current_thread_cleanup
*old
= arg
;
1144 tp
= find_thread_ptid (old
->inferior_ptid
);
1146 /* If the previously selected thread belonged to a process that has
1147 in the mean time been deleted (due to normal exit, detach, etc.),
1148 then don't revert back to it, but instead simply drop back to no
1151 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1152 restore_current_thread (old
->inferior_ptid
);
1155 restore_current_thread (null_ptid
);
1156 set_current_inferior (find_inferior_id (old
->inf_id
));
1159 /* The running state of the originally selected thread may have
1160 changed, so we have to recheck it here. */
1161 if (!ptid_equal (inferior_ptid
, null_ptid
)
1163 && is_stopped (inferior_ptid
)
1164 && target_has_registers
1166 && target_has_memory
)
1167 restore_selected_frame (old
->selected_frame_id
,
1168 old
->selected_frame_level
);
1172 restore_current_thread_cleanup_dtor (void *arg
)
1174 struct current_thread_cleanup
*old
= arg
;
1175 struct thread_info
*tp
;
1176 struct inferior
*inf
;
1178 tp
= find_thread_ptid (old
->inferior_ptid
);
1181 inf
= find_inferior_id (old
->inf_id
);
1183 inf
->removable
= old
->was_removable
;
1187 /* Set the thread reference count. */
1190 set_thread_refcount (void *data
)
1193 struct thread_array_cleanup
*ta_cleanup
= data
;
1195 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1196 ta_cleanup
->tp_array
[k
]->refcount
--;
1200 make_cleanup_restore_current_thread (void)
1202 struct thread_info
*tp
;
1203 struct frame_info
*frame
;
1204 struct current_thread_cleanup
*old
;
1206 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1207 old
->inferior_ptid
= inferior_ptid
;
1208 old
->inf_id
= current_inferior ()->num
;
1209 old
->was_removable
= current_inferior ()->removable
;
1211 if (!ptid_equal (inferior_ptid
, null_ptid
))
1213 old
->was_stopped
= is_stopped (inferior_ptid
);
1214 if (old
->was_stopped
1215 && target_has_registers
1217 && target_has_memory
)
1219 /* When processing internal events, there might not be a
1220 selected frame. If we naively call get_selected_frame
1221 here, then we can end up reading debuginfo for the
1222 current frame, but we don't generally need the debuginfo
1224 frame
= get_selected_frame_if_set ();
1229 old
->selected_frame_id
= get_frame_id (frame
);
1230 old
->selected_frame_level
= frame_relative_level (frame
);
1232 tp
= find_thread_ptid (inferior_ptid
);
1237 current_inferior ()->removable
= 0;
1239 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1240 restore_current_thread_cleanup_dtor
);
1243 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1244 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1245 of two numbers seperated by a hyphen. Examples:
1247 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1248 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1249 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1252 thread_apply_all_command (char *cmd
, int from_tty
)
1254 struct cleanup
*old_chain
;
1257 struct thread_array_cleanup ta_cleanup
;
1259 if (cmd
== NULL
|| *cmd
== '\000')
1260 error (_("Please specify a command following the thread ID list"));
1262 update_thread_list ();
1264 old_chain
= make_cleanup_restore_current_thread ();
1266 /* Save a copy of the command in case it is clobbered by
1268 saved_cmd
= xstrdup (cmd
);
1269 make_cleanup (xfree
, saved_cmd
);
1270 tc
= thread_count ();
1274 struct thread_info
**tp_array
;
1275 struct thread_info
*tp
;
1278 /* Save a copy of the thread_list in case we execute detach
1280 tp_array
= xmalloc (sizeof (struct thread_info
*) * tc
);
1281 make_cleanup (xfree
, tp_array
);
1282 ta_cleanup
.tp_array
= tp_array
;
1283 ta_cleanup
.count
= tc
;
1285 ALL_NON_EXITED_THREADS (tp
)
1292 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1294 for (k
= 0; k
!= i
; k
++)
1295 if (thread_alive (tp_array
[k
]))
1297 switch_to_thread (tp_array
[k
]->ptid
);
1298 printf_filtered (_("\nThread %d (%s):\n"),
1300 target_pid_to_str (inferior_ptid
));
1301 execute_command (cmd
, from_tty
);
1303 /* Restore exact command used previously. */
1304 strcpy (cmd
, saved_cmd
);
1308 do_cleanups (old_chain
);
1312 thread_apply_command (char *tidlist
, int from_tty
)
1315 struct cleanup
*old_chain
;
1317 struct get_number_or_range_state state
;
1319 if (tidlist
== NULL
|| *tidlist
== '\000')
1320 error (_("Please specify a thread ID list"));
1322 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1325 error (_("Please specify a command following the thread ID list"));
1327 /* Save a copy of the command in case it is clobbered by
1329 saved_cmd
= xstrdup (cmd
);
1330 old_chain
= make_cleanup (xfree
, saved_cmd
);
1332 init_number_or_range (&state
, tidlist
);
1333 while (!state
.finished
&& state
.string
< cmd
)
1335 struct thread_info
*tp
;
1338 start
= get_number_or_range (&state
);
1340 make_cleanup_restore_current_thread ();
1342 tp
= find_thread_id (start
);
1345 warning (_("Unknown thread %d."), start
);
1346 else if (!thread_alive (tp
))
1347 warning (_("Thread %d has terminated."), start
);
1350 switch_to_thread (tp
->ptid
);
1352 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1353 target_pid_to_str (inferior_ptid
));
1354 execute_command (cmd
, from_tty
);
1356 /* Restore exact command used previously. */
1357 strcpy (cmd
, saved_cmd
);
1361 do_cleanups (old_chain
);
1364 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1365 if prefix of arg is `apply'. */
1368 thread_command (char *tidstr
, int from_tty
)
1372 if (ptid_equal (inferior_ptid
, null_ptid
))
1373 error (_("No thread selected"));
1375 if (target_has_stack
)
1377 if (is_exited (inferior_ptid
))
1378 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1379 pid_to_thread_id (inferior_ptid
),
1380 target_pid_to_str (inferior_ptid
));
1382 printf_filtered (_("[Current thread is %d (%s)]\n"),
1383 pid_to_thread_id (inferior_ptid
),
1384 target_pid_to_str (inferior_ptid
));
1387 error (_("No stack."));
1391 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1394 /* Implementation of `thread name'. */
1397 thread_name_command (char *arg
, int from_tty
)
1399 struct thread_info
*info
;
1401 if (ptid_equal (inferior_ptid
, null_ptid
))
1402 error (_("No thread selected"));
1404 arg
= skip_spaces (arg
);
1406 info
= inferior_thread ();
1408 info
->name
= arg
? xstrdup (arg
) : NULL
;
1411 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1414 thread_find_command (char *arg
, int from_tty
)
1416 struct thread_info
*tp
;
1418 unsigned long match
= 0;
1420 if (arg
== NULL
|| *arg
== '\0')
1421 error (_("Command requires an argument."));
1423 tmp
= re_comp (arg
);
1425 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1427 update_thread_list ();
1428 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1430 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1432 printf_filtered (_("Thread %d has name '%s'\n"),
1437 tmp
= target_thread_name (tp
);
1438 if (tmp
!= NULL
&& re_exec (tmp
))
1440 printf_filtered (_("Thread %d has target name '%s'\n"),
1445 tmp
= target_pid_to_str (tp
->ptid
);
1446 if (tmp
!= NULL
&& re_exec (tmp
))
1448 printf_filtered (_("Thread %d has target id '%s'\n"),
1453 tmp
= target_extra_thread_info (tp
);
1454 if (tmp
!= NULL
&& re_exec (tmp
))
1456 printf_filtered (_("Thread %d has extra info '%s'\n"),
1462 printf_filtered (_("No threads match '%s'\n"), arg
);
1465 /* Print notices when new threads are attached and detached. */
1466 int print_thread_events
= 1;
1468 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1469 struct cmd_list_element
*c
, const char *value
)
1471 fprintf_filtered (file
,
1472 _("Printing of thread events is %s.\n"),
1477 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1480 struct thread_info
*tp
;
1482 num
= value_as_long (parse_and_eval (tidstr
));
1484 tp
= find_thread_id (num
);
1487 error (_("Thread ID %d not known."), num
);
1489 if (!thread_alive (tp
))
1490 error (_("Thread ID %d has terminated."), num
);
1492 switch_to_thread (tp
->ptid
);
1494 annotate_thread_changed ();
1496 ui_out_text (uiout
, "[Switching to thread ");
1497 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1498 ui_out_text (uiout
, " (");
1499 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1500 ui_out_text (uiout
, ")]");
1502 /* Note that we can't reach this with an exited thread, due to the
1503 thread_alive check above. */
1504 if (tp
->state
== THREAD_RUNNING
)
1505 ui_out_text (uiout
, "(running)\n");
1508 ui_out_text (uiout
, "\n");
1509 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1512 /* Since the current thread may have changed, see if there is any
1513 exited thread we can now delete. */
1520 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1522 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1523 error_message
, RETURN_MASK_ALL
) < 0)
1528 /* Update the 'threads_executing' global based on the threads we know
1532 update_threads_executing (void)
1534 struct thread_info
*tp
;
1536 threads_executing
= 0;
1537 ALL_NON_EXITED_THREADS (tp
)
1541 threads_executing
= 1;
1548 update_thread_list (void)
1551 target_find_new_threads ();
1552 update_threads_executing ();
1555 /* Return a new value for the selected thread's id. Return a value of 0 if
1556 no thread is selected, or no threads exist. */
1558 static struct value
*
1559 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1562 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1564 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1565 (tp
? tp
->num
: 0));
1568 /* Commands with a prefix of `thread'. */
1569 struct cmd_list_element
*thread_cmd_list
= NULL
;
1571 /* Implementation of `thread' variable. */
1573 static const struct internalvar_funcs thread_funcs
=
1575 thread_id_make_value
,
1581 _initialize_thread (void)
1583 static struct cmd_list_element
*thread_apply_list
= NULL
;
1585 add_info ("threads", info_threads_command
,
1586 _("Display currently known threads.\n\
1587 Usage: info threads [ID]...\n\
1588 Optional arguments are thread IDs with spaces between.\n\
1589 If no arguments, all threads are displayed."));
1591 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1592 Use this command to switch between threads.\n\
1593 The new thread ID must be currently known."),
1594 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1596 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1597 _("Apply a command to a list of threads."),
1598 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1600 add_cmd ("all", class_run
, thread_apply_all_command
,
1601 _("Apply a command to all threads."), &thread_apply_list
);
1603 add_cmd ("name", class_run
, thread_name_command
,
1604 _("Set the current thread's name.\n\
1605 Usage: thread name [NAME]\n\
1606 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1608 add_cmd ("find", class_run
, thread_find_command
, _("\
1609 Find threads that match a regular expression.\n\
1610 Usage: thread find REGEXP\n\
1611 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1615 add_com_alias ("t", "thread", class_run
, 1);
1617 add_setshow_boolean_cmd ("thread-events", no_class
,
1618 &print_thread_events
, _("\
1619 Set printing of thread events (such as thread start and exit)."), _("\
1620 Show printing of thread events (such as thread start and exit)."), NULL
,
1622 show_print_thread_events
,
1623 &setprintlist
, &showprintlist
);
1625 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);