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
);
95 delete_step_resume_breakpoint (struct thread_info
*tp
)
97 if (tp
&& tp
->control
.step_resume_breakpoint
)
99 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
100 tp
->control
.step_resume_breakpoint
= NULL
;
105 delete_exception_resume_breakpoint (struct thread_info
*tp
)
107 if (tp
&& tp
->control
.exception_resume_breakpoint
)
109 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
110 tp
->control
.exception_resume_breakpoint
= NULL
;
115 clear_thread_inferior_resources (struct thread_info
*tp
)
117 /* NOTE: this will take care of any left-over step_resume breakpoints,
118 but not any user-specified thread-specific breakpoints. We can not
119 delete the breakpoint straight-off, because the inferior might not
120 be stopped at the moment. */
121 if (tp
->control
.step_resume_breakpoint
)
123 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
124 tp
->control
.step_resume_breakpoint
= NULL
;
127 if (tp
->control
.exception_resume_breakpoint
)
129 tp
->control
.exception_resume_breakpoint
->disposition
130 = disp_del_at_next_stop
;
131 tp
->control
.exception_resume_breakpoint
= NULL
;
134 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
136 bpstat_clear (&tp
->control
.stop_bpstat
);
138 btrace_teardown (tp
);
140 do_all_intermediate_continuations_thread (tp
, 1);
141 do_all_continuations_thread (tp
, 1);
145 free_thread (struct thread_info
*tp
)
149 if (tp
->private_dtor
)
150 tp
->private_dtor (tp
->private);
160 init_thread_list (void)
162 struct thread_info
*tp
, *tpnext
;
164 highest_thread_num
= 0;
169 for (tp
= thread_list
; tp
; tp
= tpnext
)
176 threads_executing
= 0;
179 /* Allocate a new thread with target id PTID and add it to the thread
182 static struct thread_info
*
183 new_thread (ptid_t ptid
)
185 struct thread_info
*tp
;
187 tp
= xcalloc (1, sizeof (*tp
));
190 tp
->num
= ++highest_thread_num
;
191 tp
->next
= thread_list
;
194 /* Nothing to follow yet. */
195 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
196 tp
->state
= THREAD_STOPPED
;
202 add_thread_silent (ptid_t ptid
)
204 struct thread_info
*tp
;
206 tp
= find_thread_ptid (ptid
);
208 /* Found an old thread with the same id. It has to be dead,
209 otherwise we wouldn't be adding a new thread with the same id.
210 The OS is reusing this id --- delete it, and recreate a new
213 /* In addition to deleting the thread, if this is the current
214 thread, then we need to take care that delete_thread doesn't
215 really delete the thread if it is inferior_ptid. Create a
216 new template thread in the list with an invalid ptid, switch
217 to it, delete the original thread, reset the new thread's
218 ptid, and switch to it. */
220 if (ptid_equal (inferior_ptid
, ptid
))
222 tp
= new_thread (null_ptid
);
224 /* Make switch_to_thread not read from the thread. */
225 tp
->state
= THREAD_EXITED
;
226 switch_to_thread (null_ptid
);
228 /* Now we can delete it. */
229 delete_thread (ptid
);
231 /* Now reset its ptid, and reswitch inferior_ptid to it. */
233 tp
->state
= THREAD_STOPPED
;
234 switch_to_thread (ptid
);
236 observer_notify_new_thread (tp
);
242 /* Just go ahead and delete it. */
243 delete_thread (ptid
);
246 tp
= new_thread (ptid
);
247 observer_notify_new_thread (tp
);
253 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
255 struct thread_info
*result
= add_thread_silent (ptid
);
257 result
->private = private;
259 if (print_thread_events
)
260 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
262 annotate_new_thread ();
267 add_thread (ptid_t ptid
)
269 return add_thread_with_info (ptid
, NULL
);
272 /* Delete thread PTID. If SILENT, don't notify the observer of this
275 delete_thread_1 (ptid_t ptid
, int silent
)
277 struct thread_info
*tp
, *tpprev
;
281 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
282 if (ptid_equal (tp
->ptid
, ptid
))
288 /* If this is the current thread, or there's code out there that
289 relies on it existing (refcount > 0) we can't delete yet. Mark
290 it as exited, and notify it. */
292 || ptid_equal (tp
->ptid
, inferior_ptid
))
294 if (tp
->state
!= THREAD_EXITED
)
296 observer_notify_thread_exit (tp
, silent
);
298 /* Tag it as exited. */
299 tp
->state
= THREAD_EXITED
;
301 /* Clear breakpoints, etc. associated with this thread. */
302 clear_thread_inferior_resources (tp
);
305 /* Will be really deleted some other time. */
309 /* Notify thread exit, but only if we haven't already. */
310 if (tp
->state
!= THREAD_EXITED
)
311 observer_notify_thread_exit (tp
, silent
);
313 /* Tag it as exited. */
314 tp
->state
= THREAD_EXITED
;
315 clear_thread_inferior_resources (tp
);
318 tpprev
->next
= tp
->next
;
320 thread_list
= tp
->next
;
325 /* Delete thread PTID and notify of thread exit. If this is
326 inferior_ptid, don't actually delete it, but tag it as exited and
327 do the notification. If PTID is the user selected thread, clear
330 delete_thread (ptid_t ptid
)
332 delete_thread_1 (ptid
, 0 /* not silent */);
336 delete_thread_silent (ptid_t ptid
)
338 delete_thread_1 (ptid
, 1 /* silent */);
342 find_thread_id (int num
)
344 struct thread_info
*tp
;
346 for (tp
= thread_list
; tp
; tp
= tp
->next
)
353 /* Find a thread_info by matching PTID. */
355 find_thread_ptid (ptid_t ptid
)
357 struct thread_info
*tp
;
359 for (tp
= thread_list
; tp
; tp
= tp
->next
)
360 if (ptid_equal (tp
->ptid
, ptid
))
367 * Thread iterator function.
369 * Calls a callback function once for each thread, so long as
370 * the callback function returns false. If the callback function
371 * returns true, the iteration will end and the current thread
372 * will be returned. This can be useful for implementing a
373 * search for a thread with arbitrary attributes, or for applying
374 * some operation to every thread.
376 * FIXME: some of the existing functionality, such as
377 * "Thread apply all", might be rewritten using this functionality.
381 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
384 struct thread_info
*tp
, *next
;
386 for (tp
= thread_list
; tp
; tp
= next
)
389 if ((*callback
) (tp
, data
))
400 struct thread_info
*tp
;
402 for (tp
= thread_list
; tp
; tp
= tp
->next
)
409 valid_thread_id (int num
)
411 struct thread_info
*tp
;
413 for (tp
= thread_list
; tp
; tp
= tp
->next
)
421 pid_to_thread_id (ptid_t ptid
)
423 struct thread_info
*tp
;
425 for (tp
= thread_list
; tp
; tp
= tp
->next
)
426 if (ptid_equal (tp
->ptid
, ptid
))
433 thread_id_to_pid (int num
)
435 struct thread_info
*thread
= find_thread_id (num
);
440 return pid_to_ptid (-1);
444 in_thread_list (ptid_t ptid
)
446 struct thread_info
*tp
;
448 for (tp
= thread_list
; tp
; tp
= tp
->next
)
449 if (ptid_equal (tp
->ptid
, ptid
))
452 return 0; /* Never heard of 'im. */
455 /* Finds the first thread of the inferior given by PID. If PID is -1,
456 return the first thread in the list. */
459 first_thread_of_process (int pid
)
461 struct thread_info
*tp
, *ret
= NULL
;
463 for (tp
= thread_list
; tp
; tp
= tp
->next
)
464 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
465 if (ret
== NULL
|| tp
->num
< ret
->num
)
472 any_thread_of_process (int pid
)
474 struct thread_info
*tp
;
476 gdb_assert (pid
!= 0);
478 /* Prefer the current thread. */
479 if (ptid_get_pid (inferior_ptid
) == pid
)
480 return inferior_thread ();
482 ALL_NON_EXITED_THREADS (tp
)
483 if (ptid_get_pid (tp
->ptid
) == pid
)
490 any_live_thread_of_process (int pid
)
492 struct thread_info
*curr_tp
= NULL
;
493 struct thread_info
*tp
;
494 struct thread_info
*tp_executing
= NULL
;
496 gdb_assert (pid
!= 0);
498 /* Prefer the current thread if it's not executing. */
499 if (ptid_get_pid (inferior_ptid
) == pid
)
501 /* If the current thread is dead, forget it. If it's not
502 executing, use it. Otherwise, still choose it (below), but
503 only if no other non-executing thread is found. */
504 curr_tp
= inferior_thread ();
505 if (curr_tp
->state
== THREAD_EXITED
)
507 else if (!curr_tp
->executing
)
511 ALL_NON_EXITED_THREADS (tp
)
512 if (ptid_get_pid (tp
->ptid
) == pid
)
520 /* If both the current thread and all live threads are executing,
521 prefer the current thread. */
525 /* Otherwise, just return an executing thread, if any. */
529 /* Print a list of thread ids currently known, and the total number of
530 threads. To be used from within catch_errors. */
532 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
534 struct thread_info
*tp
;
536 struct cleanup
*cleanup_chain
;
537 int current_thread
= -1;
539 update_thread_list ();
541 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
543 for (tp
= thread_list
; tp
; tp
= tp
->next
)
545 if (tp
->state
== THREAD_EXITED
)
548 if (ptid_equal (tp
->ptid
, inferior_ptid
))
549 current_thread
= tp
->num
;
552 ui_out_field_int (uiout
, "thread-id", tp
->num
);
555 do_cleanups (cleanup_chain
);
557 if (current_thread
!= -1)
558 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
559 ui_out_field_int (uiout
, "number-of-threads", num
);
563 /* Official gdblib interface function to get a list of thread ids and
566 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
568 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
569 error_message
, RETURN_MASK_ALL
) < 0)
574 /* Return true if TP is an active thread. */
576 thread_alive (struct thread_info
*tp
)
578 if (tp
->state
== THREAD_EXITED
)
580 if (!target_thread_alive (tp
->ptid
))
588 struct thread_info
*tp
, *next
;
590 for (tp
= thread_list
; tp
; tp
= next
)
593 if (!thread_alive (tp
))
594 delete_thread (tp
->ptid
);
599 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
601 struct inferior
*inf
;
602 struct thread_info
*tp
;
604 /* It can happen that what we knew as the target inferior id
605 changes. E.g, target remote may only discover the remote process
606 pid after adding the inferior to GDB's list. */
607 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
608 inf
->pid
= ptid_get_pid (new_ptid
);
610 tp
= find_thread_ptid (old_ptid
);
613 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
617 set_running (ptid_t ptid
, int running
)
619 struct thread_info
*tp
;
620 int all
= ptid_equal (ptid
, minus_one_ptid
);
622 /* We try not to notify the observer if no thread has actually changed
623 the running state -- merely to reduce the number of messages to
624 frontend. Frontend is supposed to handle multiple *running just fine. */
625 if (all
|| ptid_is_pid (ptid
))
629 for (tp
= thread_list
; tp
; tp
= tp
->next
)
630 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
632 if (tp
->state
== THREAD_EXITED
)
634 if (running
&& tp
->state
== THREAD_STOPPED
)
636 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
639 observer_notify_target_resumed (ptid
);
645 tp
= find_thread_ptid (ptid
);
647 gdb_assert (tp
->state
!= THREAD_EXITED
);
648 if (running
&& tp
->state
== THREAD_STOPPED
)
650 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
652 observer_notify_target_resumed (ptid
);
657 is_thread_state (ptid_t ptid
, enum thread_state state
)
659 struct thread_info
*tp
;
661 tp
= find_thread_ptid (ptid
);
663 return tp
->state
== state
;
667 is_stopped (ptid_t ptid
)
669 return is_thread_state (ptid
, THREAD_STOPPED
);
673 is_exited (ptid_t ptid
)
675 return is_thread_state (ptid
, THREAD_EXITED
);
679 is_running (ptid_t ptid
)
681 return is_thread_state (ptid
, THREAD_RUNNING
);
685 is_executing (ptid_t ptid
)
687 struct thread_info
*tp
;
689 tp
= find_thread_ptid (ptid
);
691 return tp
->executing
;
695 set_executing (ptid_t ptid
, int executing
)
697 struct thread_info
*tp
;
698 int all
= ptid_equal (ptid
, minus_one_ptid
);
700 if (all
|| ptid_is_pid (ptid
))
702 for (tp
= thread_list
; tp
; tp
= tp
->next
)
703 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
704 tp
->executing
= executing
;
708 tp
= find_thread_ptid (ptid
);
710 tp
->executing
= executing
;
713 /* It only takes one running thread to spawn more threads.*/
715 threads_executing
= 1;
716 /* Only clear the flag if the caller is telling us everything is
718 else if (ptid_equal (minus_one_ptid
, ptid
))
719 threads_executing
= 0;
722 /* See gdbthread.h. */
725 threads_are_executing (void)
727 return threads_executing
;
731 set_stop_requested (ptid_t ptid
, int stop
)
733 struct thread_info
*tp
;
734 int all
= ptid_equal (ptid
, minus_one_ptid
);
736 if (all
|| ptid_is_pid (ptid
))
738 for (tp
= thread_list
; tp
; tp
= tp
->next
)
739 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
740 tp
->stop_requested
= stop
;
744 tp
= find_thread_ptid (ptid
);
746 tp
->stop_requested
= stop
;
749 /* Call the stop requested observer so other components of GDB can
750 react to this request. */
752 observer_notify_thread_stop_requested (ptid
);
756 finish_thread_state (ptid_t ptid
)
758 struct thread_info
*tp
;
762 all
= ptid_equal (ptid
, minus_one_ptid
);
764 if (all
|| ptid_is_pid (ptid
))
766 for (tp
= thread_list
; tp
; tp
= tp
->next
)
768 if (tp
->state
== THREAD_EXITED
)
770 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
772 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
774 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
780 tp
= find_thread_ptid (ptid
);
782 if (tp
->state
!= THREAD_EXITED
)
784 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
786 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
791 observer_notify_target_resumed (ptid
);
795 finish_thread_state_cleanup (void *arg
)
797 ptid_t
*ptid_p
= arg
;
801 finish_thread_state (*ptid_p
);
805 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
807 return (pc
>= thread
->control
.step_range_start
808 && pc
< thread
->control
.step_range_end
);
811 /* Prints the list of threads and their details on UIOUT.
812 This is a version of 'info_threads_command' suitable for
814 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
815 that should be printed. Otherwise, all threads are
817 If PID is not -1, only print threads from the process PID.
818 Otherwise, threads from all attached PIDs are printed.
819 If both REQUESTED_THREAD and PID are not -1, then the thread
820 is printed if it belongs to the specified process. Otherwise,
821 an error is raised. */
823 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
825 struct thread_info
*tp
;
827 struct cleanup
*old_chain
;
828 char *extra_info
, *name
, *target_id
;
829 int current_thread
= -1;
831 update_thread_list ();
832 current_ptid
= inferior_ptid
;
834 /* We'll be switching threads temporarily. */
835 old_chain
= make_cleanup_restore_current_thread ();
837 /* For backward compatibility, we make a list for MI. A table is
838 preferable for the CLI, though, because it shows table
840 if (ui_out_is_mi_like_p (uiout
))
841 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
846 for (tp
= thread_list
; tp
; tp
= tp
->next
)
848 if (!number_is_in_list (requested_threads
, tp
->num
))
851 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
854 if (tp
->state
== THREAD_EXITED
)
862 if (requested_threads
== NULL
|| *requested_threads
== '\0')
863 ui_out_message (uiout
, 0, _("No threads.\n"));
865 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
867 do_cleanups (old_chain
);
871 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
873 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
874 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
875 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
876 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
877 ui_out_table_body (uiout
);
880 for (tp
= thread_list
; tp
; tp
= tp
->next
)
882 struct cleanup
*chain2
;
885 if (!number_is_in_list (requested_threads
, tp
->num
))
888 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
890 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
891 error (_("Requested thread not found in requested process"));
895 if (ptid_equal (tp
->ptid
, current_ptid
))
896 current_thread
= tp
->num
;
898 if (tp
->state
== THREAD_EXITED
)
901 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
903 if (ui_out_is_mi_like_p (uiout
))
906 if (ptid_equal (tp
->ptid
, current_ptid
))
907 ui_out_text (uiout
, "* ");
909 ui_out_text (uiout
, " ");
913 if (ptid_equal (tp
->ptid
, current_ptid
))
914 ui_out_field_string (uiout
, "current", "*");
916 ui_out_field_skip (uiout
, "current");
919 ui_out_field_int (uiout
, "id", tp
->num
);
921 /* For the CLI, we stuff everything into the target-id field.
922 This is a gross hack to make the output come out looking
923 correct. The underlying problem here is that ui-out has no
924 way to specify that a field's space allocation should be
925 shared by several fields. For MI, we do the right thing
928 target_id
= target_pid_to_str (tp
->ptid
);
929 extra_info
= target_extra_thread_info (tp
);
930 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
932 if (ui_out_is_mi_like_p (uiout
))
934 ui_out_field_string (uiout
, "target-id", target_id
);
936 ui_out_field_string (uiout
, "details", extra_info
);
938 ui_out_field_string (uiout
, "name", name
);
942 struct cleanup
*str_cleanup
;
945 if (extra_info
&& name
)
946 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
949 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
951 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
953 contents
= xstrdup (target_id
);
954 str_cleanup
= make_cleanup (xfree
, contents
);
956 ui_out_field_string (uiout
, "target-id", contents
);
957 do_cleanups (str_cleanup
);
960 if (tp
->state
== THREAD_RUNNING
)
961 ui_out_text (uiout
, "(running)\n");
964 /* The switch below puts us at the top of the stack (leaf
966 switch_to_thread (tp
->ptid
);
967 print_stack_frame (get_selected_frame (NULL
),
968 /* For MI output, print frame level. */
969 ui_out_is_mi_like_p (uiout
),
973 if (ui_out_is_mi_like_p (uiout
))
975 char *state
= "stopped";
977 if (tp
->state
== THREAD_RUNNING
)
979 ui_out_field_string (uiout
, "state", state
);
982 core
= target_core_of_thread (tp
->ptid
);
983 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
984 ui_out_field_int (uiout
, "core", core
);
986 do_cleanups (chain2
);
989 /* Restores the current thread and the frame selected before
990 the "info threads" command. */
991 do_cleanups (old_chain
);
993 if (pid
== -1 && requested_threads
== NULL
)
995 gdb_assert (current_thread
!= -1
997 || ptid_equal (inferior_ptid
, null_ptid
));
998 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
999 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
1001 if (current_thread
!= -1 && is_exited (current_ptid
))
1002 ui_out_message (uiout
, 0, "\n\
1003 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1005 else if (thread_list
1006 && current_thread
== -1
1007 && ptid_equal (current_ptid
, null_ptid
))
1008 ui_out_message (uiout
, 0, "\n\
1009 No selected thread. See `help thread'.\n");
1013 /* Print information about currently known threads
1015 Optional ARG is a thread id, or list of thread ids.
1017 Note: this has the drawback that it _really_ switches
1018 threads, which frees the frame cache. A no-side
1019 effects info-threads command would be nicer. */
1022 info_threads_command (char *arg
, int from_tty
)
1024 print_thread_info (current_uiout
, arg
, -1);
1027 /* Switch from one thread to another. */
1030 switch_to_thread (ptid_t ptid
)
1032 /* Switch the program space as well, if we can infer it from the now
1033 current thread. Otherwise, it's up to the caller to select the
1035 if (!ptid_equal (ptid
, null_ptid
))
1037 struct inferior
*inf
;
1039 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1040 gdb_assert (inf
!= NULL
);
1041 set_current_program_space (inf
->pspace
);
1042 set_current_inferior (inf
);
1045 if (ptid_equal (ptid
, inferior_ptid
))
1048 inferior_ptid
= ptid
;
1049 reinit_frame_cache ();
1051 /* We don't check for is_stopped, because we're called at times
1052 while in the TARGET_RUNNING state, e.g., while handling an
1054 if (!ptid_equal (inferior_ptid
, null_ptid
)
1055 && !is_exited (ptid
)
1056 && !is_executing (ptid
))
1057 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1059 stop_pc
= ~(CORE_ADDR
) 0;
1063 restore_current_thread (ptid_t ptid
)
1065 switch_to_thread (ptid
);
1069 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1071 struct frame_info
*frame
= NULL
;
1074 /* This means there was no selected frame. */
1075 if (frame_level
== -1)
1077 select_frame (NULL
);
1081 gdb_assert (frame_level
>= 0);
1083 /* Restore by level first, check if the frame id is the same as
1084 expected. If that fails, try restoring by frame id. If that
1085 fails, nothing to do, just warn the user. */
1087 count
= frame_level
;
1088 frame
= find_relative_frame (get_current_frame (), &count
);
1091 /* The frame ids must match - either both valid or both outer_frame_id.
1092 The latter case is not failsafe, but since it's highly unlikely
1093 the search by level finds the wrong frame, it's 99.9(9)% of
1094 the time (for all practical purposes) safe. */
1095 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1097 /* Cool, all is fine. */
1098 select_frame (frame
);
1102 frame
= frame_find_by_id (a_frame_id
);
1105 /* Cool, refound it. */
1106 select_frame (frame
);
1110 /* Nothing else to do, the frame layout really changed. Select the
1111 innermost stack frame. */
1112 select_frame (get_current_frame ());
1114 /* Warn the user. */
1115 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1117 warning (_("Couldn't restore frame #%d in "
1118 "current thread. Bottom (innermost) frame selected:"),
1120 /* For MI, we should probably have a notification about
1121 current frame change. But this error is not very
1122 likely, so don't bother for now. */
1123 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1127 struct current_thread_cleanup
1129 ptid_t inferior_ptid
;
1130 struct frame_id selected_frame_id
;
1131 int selected_frame_level
;
1138 do_restore_current_thread_cleanup (void *arg
)
1140 struct thread_info
*tp
;
1141 struct current_thread_cleanup
*old
= arg
;
1143 tp
= find_thread_ptid (old
->inferior_ptid
);
1145 /* If the previously selected thread belonged to a process that has
1146 in the mean time been deleted (due to normal exit, detach, etc.),
1147 then don't revert back to it, but instead simply drop back to no
1150 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1151 restore_current_thread (old
->inferior_ptid
);
1154 restore_current_thread (null_ptid
);
1155 set_current_inferior (find_inferior_id (old
->inf_id
));
1158 /* The running state of the originally selected thread may have
1159 changed, so we have to recheck it here. */
1160 if (!ptid_equal (inferior_ptid
, null_ptid
)
1162 && is_stopped (inferior_ptid
)
1163 && target_has_registers
1165 && target_has_memory
)
1166 restore_selected_frame (old
->selected_frame_id
,
1167 old
->selected_frame_level
);
1171 restore_current_thread_cleanup_dtor (void *arg
)
1173 struct current_thread_cleanup
*old
= arg
;
1174 struct thread_info
*tp
;
1175 struct inferior
*inf
;
1177 tp
= find_thread_ptid (old
->inferior_ptid
);
1180 inf
= find_inferior_id (old
->inf_id
);
1182 inf
->removable
= old
->was_removable
;
1186 /* Set the thread reference count. */
1189 set_thread_refcount (void *data
)
1192 struct thread_array_cleanup
*ta_cleanup
= data
;
1194 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1195 ta_cleanup
->tp_array
[k
]->refcount
--;
1199 make_cleanup_restore_current_thread (void)
1201 struct thread_info
*tp
;
1202 struct frame_info
*frame
;
1203 struct current_thread_cleanup
*old
;
1205 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1206 old
->inferior_ptid
= inferior_ptid
;
1207 old
->inf_id
= current_inferior ()->num
;
1208 old
->was_removable
= current_inferior ()->removable
;
1210 if (!ptid_equal (inferior_ptid
, null_ptid
))
1212 old
->was_stopped
= is_stopped (inferior_ptid
);
1213 if (old
->was_stopped
1214 && target_has_registers
1216 && target_has_memory
)
1218 /* When processing internal events, there might not be a
1219 selected frame. If we naively call get_selected_frame
1220 here, then we can end up reading debuginfo for the
1221 current frame, but we don't generally need the debuginfo
1223 frame
= get_selected_frame_if_set ();
1228 old
->selected_frame_id
= get_frame_id (frame
);
1229 old
->selected_frame_level
= frame_relative_level (frame
);
1231 tp
= find_thread_ptid (inferior_ptid
);
1236 current_inferior ()->removable
= 0;
1238 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1239 restore_current_thread_cleanup_dtor
);
1242 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1243 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1244 of two numbers seperated by a hyphen. Examples:
1246 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1247 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1248 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1251 thread_apply_all_command (char *cmd
, int from_tty
)
1253 struct cleanup
*old_chain
;
1256 struct thread_array_cleanup ta_cleanup
;
1258 if (cmd
== NULL
|| *cmd
== '\000')
1259 error (_("Please specify a command following the thread ID list"));
1261 update_thread_list ();
1263 old_chain
= make_cleanup_restore_current_thread ();
1265 /* Save a copy of the command in case it is clobbered by
1267 saved_cmd
= xstrdup (cmd
);
1268 make_cleanup (xfree
, saved_cmd
);
1269 tc
= thread_count ();
1273 struct thread_info
**tp_array
;
1274 struct thread_info
*tp
;
1277 /* Save a copy of the thread_list in case we execute detach
1279 tp_array
= xmalloc (sizeof (struct thread_info
*) * tc
);
1280 make_cleanup (xfree
, tp_array
);
1281 ta_cleanup
.tp_array
= tp_array
;
1282 ta_cleanup
.count
= tc
;
1284 ALL_NON_EXITED_THREADS (tp
)
1291 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1293 for (k
= 0; k
!= i
; k
++)
1294 if (thread_alive (tp_array
[k
]))
1296 switch_to_thread (tp_array
[k
]->ptid
);
1297 printf_filtered (_("\nThread %d (%s):\n"),
1299 target_pid_to_str (inferior_ptid
));
1300 execute_command (cmd
, from_tty
);
1302 /* Restore exact command used previously. */
1303 strcpy (cmd
, saved_cmd
);
1307 do_cleanups (old_chain
);
1311 thread_apply_command (char *tidlist
, int from_tty
)
1314 struct cleanup
*old_chain
;
1316 struct get_number_or_range_state state
;
1318 if (tidlist
== NULL
|| *tidlist
== '\000')
1319 error (_("Please specify a thread ID list"));
1321 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1324 error (_("Please specify a command following the thread ID list"));
1326 /* Save a copy of the command in case it is clobbered by
1328 saved_cmd
= xstrdup (cmd
);
1329 old_chain
= make_cleanup (xfree
, saved_cmd
);
1331 init_number_or_range (&state
, tidlist
);
1332 while (!state
.finished
&& state
.string
< cmd
)
1334 struct thread_info
*tp
;
1337 start
= get_number_or_range (&state
);
1339 make_cleanup_restore_current_thread ();
1341 tp
= find_thread_id (start
);
1344 warning (_("Unknown thread %d."), start
);
1345 else if (!thread_alive (tp
))
1346 warning (_("Thread %d has terminated."), start
);
1349 switch_to_thread (tp
->ptid
);
1351 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1352 target_pid_to_str (inferior_ptid
));
1353 execute_command (cmd
, from_tty
);
1355 /* Restore exact command used previously. */
1356 strcpy (cmd
, saved_cmd
);
1360 do_cleanups (old_chain
);
1363 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1364 if prefix of arg is `apply'. */
1367 thread_command (char *tidstr
, int from_tty
)
1371 if (ptid_equal (inferior_ptid
, null_ptid
))
1372 error (_("No thread selected"));
1374 if (target_has_stack
)
1376 if (is_exited (inferior_ptid
))
1377 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1378 pid_to_thread_id (inferior_ptid
),
1379 target_pid_to_str (inferior_ptid
));
1381 printf_filtered (_("[Current thread is %d (%s)]\n"),
1382 pid_to_thread_id (inferior_ptid
),
1383 target_pid_to_str (inferior_ptid
));
1386 error (_("No stack."));
1390 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1393 /* Implementation of `thread name'. */
1396 thread_name_command (char *arg
, int from_tty
)
1398 struct thread_info
*info
;
1400 if (ptid_equal (inferior_ptid
, null_ptid
))
1401 error (_("No thread selected"));
1403 arg
= skip_spaces (arg
);
1405 info
= inferior_thread ();
1407 info
->name
= arg
? xstrdup (arg
) : NULL
;
1410 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1413 thread_find_command (char *arg
, int from_tty
)
1415 struct thread_info
*tp
;
1417 unsigned long match
= 0;
1419 if (arg
== NULL
|| *arg
== '\0')
1420 error (_("Command requires an argument."));
1422 tmp
= re_comp (arg
);
1424 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1426 update_thread_list ();
1427 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1429 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1431 printf_filtered (_("Thread %d has name '%s'\n"),
1436 tmp
= target_thread_name (tp
);
1437 if (tmp
!= NULL
&& re_exec (tmp
))
1439 printf_filtered (_("Thread %d has target name '%s'\n"),
1444 tmp
= target_pid_to_str (tp
->ptid
);
1445 if (tmp
!= NULL
&& re_exec (tmp
))
1447 printf_filtered (_("Thread %d has target id '%s'\n"),
1452 tmp
= target_extra_thread_info (tp
);
1453 if (tmp
!= NULL
&& re_exec (tmp
))
1455 printf_filtered (_("Thread %d has extra info '%s'\n"),
1461 printf_filtered (_("No threads match '%s'\n"), arg
);
1464 /* Print notices when new threads are attached and detached. */
1465 int print_thread_events
= 1;
1467 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1468 struct cmd_list_element
*c
, const char *value
)
1470 fprintf_filtered (file
,
1471 _("Printing of thread events is %s.\n"),
1476 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1479 struct thread_info
*tp
;
1481 num
= value_as_long (parse_and_eval (tidstr
));
1483 tp
= find_thread_id (num
);
1486 error (_("Thread ID %d not known."), num
);
1488 if (!thread_alive (tp
))
1489 error (_("Thread ID %d has terminated."), num
);
1491 switch_to_thread (tp
->ptid
);
1493 annotate_thread_changed ();
1495 ui_out_text (uiout
, "[Switching to thread ");
1496 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1497 ui_out_text (uiout
, " (");
1498 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1499 ui_out_text (uiout
, ")]");
1501 /* Note that we can't reach this with an exited thread, due to the
1502 thread_alive check above. */
1503 if (tp
->state
== THREAD_RUNNING
)
1504 ui_out_text (uiout
, "(running)\n");
1507 ui_out_text (uiout
, "\n");
1508 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1511 /* Since the current thread may have changed, see if there is any
1512 exited thread we can now delete. */
1519 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1521 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1522 error_message
, RETURN_MASK_ALL
) < 0)
1527 /* Update the 'threads_executing' global based on the threads we know
1531 update_threads_executing (void)
1533 struct thread_info
*tp
;
1535 threads_executing
= 0;
1536 ALL_NON_EXITED_THREADS (tp
)
1540 threads_executing
= 1;
1547 update_thread_list (void)
1550 target_find_new_threads ();
1551 update_threads_executing ();
1554 /* Return a new value for the selected thread's id. Return a value of 0 if
1555 no thread is selected, or no threads exist. */
1557 static struct value
*
1558 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1561 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1563 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1564 (tp
? tp
->num
: 0));
1567 /* Commands with a prefix of `thread'. */
1568 struct cmd_list_element
*thread_cmd_list
= NULL
;
1570 /* Implementation of `thread' variable. */
1572 static const struct internalvar_funcs thread_funcs
=
1574 thread_id_make_value
,
1580 _initialize_thread (void)
1582 static struct cmd_list_element
*thread_apply_list
= NULL
;
1584 add_info ("threads", info_threads_command
,
1585 _("Display currently known threads.\n\
1586 Usage: info threads [ID]...\n\
1587 Optional arguments are thread IDs with spaces between.\n\
1588 If no arguments, all threads are displayed."));
1590 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1591 Use this command to switch between threads.\n\
1592 The new thread ID must be currently known."),
1593 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1595 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1596 _("Apply a command to a list of threads."),
1597 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1599 add_cmd ("all", class_run
, thread_apply_all_command
,
1600 _("Apply a command to all threads."), &thread_apply_list
);
1602 add_cmd ("name", class_run
, thread_name_command
,
1603 _("Set the current thread's name.\n\
1604 Usage: thread name [NAME]\n\
1605 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1607 add_cmd ("find", class_run
, thread_find_command
, _("\
1608 Find threads that match a regular expression.\n\
1609 Usage: thread find REGEXP\n\
1610 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1614 add_com_alias ("t", "thread", class_run
, 1);
1616 add_setshow_boolean_cmd ("thread-events", no_class
,
1617 &print_thread_events
, _("\
1618 Set printing of thread events (such as thread start and exit)."), _("\
1619 Show printing of thread events (such as thread start and exit)."), NULL
,
1621 show_print_thread_events
,
1622 &setprintlist
, &showprintlist
);
1624 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);