1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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 "thread-fsm.h"
46 #include "tid-parse.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_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
);
72 /* Data to cleanup thread array. */
74 struct thread_array_cleanup
76 /* Array of thread pointers used to set
78 struct thread_info
**tp_array
;
80 /* Thread count in the array. */
86 inferior_thread (void)
88 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
93 /* Delete the breakpoint pointed at by BP_P, if there's one. */
96 delete_thread_breakpoint (struct breakpoint
**bp_p
)
100 delete_breakpoint (*bp_p
);
106 delete_step_resume_breakpoint (struct thread_info
*tp
)
109 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
113 delete_exception_resume_breakpoint (struct thread_info
*tp
)
116 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
119 /* See gdbthread.h. */
122 delete_single_step_breakpoints (struct thread_info
*tp
)
125 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
128 /* Delete the breakpoint pointed at by BP_P at the next stop, if
132 delete_at_next_stop (struct breakpoint
**bp
)
136 (*bp
)->disposition
= disp_del_at_next_stop
;
141 /* See gdbthread.h. */
144 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
146 return tp
->control
.single_step_breakpoints
!= NULL
;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
153 struct address_space
*aspace
,
156 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
158 return (ss_bps
!= NULL
159 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
162 /* See gdbthread.h. */
165 thread_cancel_execution_command (struct thread_info
*thr
)
167 if (thr
->thread_fsm
!= NULL
)
169 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
170 thread_fsm_delete (thr
->thread_fsm
);
171 thr
->thread_fsm
= NULL
;
176 clear_thread_inferior_resources (struct thread_info
*tp
)
178 /* NOTE: this will take care of any left-over step_resume breakpoints,
179 but not any user-specified thread-specific breakpoints. We can not
180 delete the breakpoint straight-off, because the inferior might not
181 be stopped at the moment. */
182 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
183 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
184 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
186 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
188 bpstat_clear (&tp
->control
.stop_bpstat
);
190 btrace_teardown (tp
);
192 thread_cancel_execution_command (tp
);
195 /* Set the TP's state as exited. */
198 set_thread_exited (thread_info
*tp
, int silent
)
200 /* Dead threads don't need to step-over. Remove from queue. */
201 if (tp
->step_over_next
!= NULL
)
202 thread_step_over_chain_remove (tp
);
204 if (tp
->state
!= THREAD_EXITED
)
206 observer_notify_thread_exit (tp
, silent
);
208 /* Tag it as exited. */
209 tp
->state
= THREAD_EXITED
;
211 /* Clear breakpoints, etc. associated with this thread. */
212 clear_thread_inferior_resources (tp
);
217 init_thread_list (void)
219 struct thread_info
*tp
, *tpnext
;
221 highest_thread_num
= 0;
226 for (tp
= thread_list
; tp
; tp
= tpnext
)
229 if (tp
->deletable ())
232 set_thread_exited (tp
, 1);
236 threads_executing
= 0;
239 /* Allocate a new thread of inferior INF with target id PTID and add
240 it to the thread list. */
242 static struct thread_info
*
243 new_thread (struct inferior
*inf
, ptid_t ptid
)
245 thread_info
*tp
= new thread_info (inf
, ptid
);
247 if (thread_list
== NULL
)
251 struct thread_info
*last
;
253 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
262 add_thread_silent (ptid_t ptid
)
264 struct thread_info
*tp
;
265 struct inferior
*inf
= find_inferior_ptid (ptid
);
266 gdb_assert (inf
!= NULL
);
268 tp
= find_thread_ptid (ptid
);
270 /* Found an old thread with the same id. It has to be dead,
271 otherwise we wouldn't be adding a new thread with the same id.
272 The OS is reusing this id --- delete it, and recreate a new
275 /* In addition to deleting the thread, if this is the current
276 thread, then we need to take care that delete_thread doesn't
277 really delete the thread if it is inferior_ptid. Create a
278 new template thread in the list with an invalid ptid, switch
279 to it, delete the original thread, reset the new thread's
280 ptid, and switch to it. */
282 if (ptid_equal (inferior_ptid
, ptid
))
284 tp
= new_thread (inf
, null_ptid
);
286 /* Make switch_to_thread not read from the thread. */
287 tp
->state
= THREAD_EXITED
;
288 switch_to_thread (null_ptid
);
290 /* Now we can delete it. */
291 delete_thread (ptid
);
293 /* Now reset its ptid, and reswitch inferior_ptid to it. */
295 tp
->state
= THREAD_STOPPED
;
296 switch_to_thread (ptid
);
298 observer_notify_new_thread (tp
);
304 /* Just go ahead and delete it. */
305 delete_thread (ptid
);
308 tp
= new_thread (inf
, ptid
);
309 observer_notify_new_thread (tp
);
315 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
317 struct thread_info
*result
= add_thread_silent (ptid
);
321 if (print_thread_events
)
322 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
324 annotate_new_thread ();
329 add_thread (ptid_t ptid
)
331 return add_thread_with_info (ptid
, NULL
);
334 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
335 : ptid (ptid_
), inf (inf_
)
337 gdb_assert (inf_
!= NULL
);
339 this->global_num
= ++highest_thread_num
;
340 this->per_inf_num
= ++inf_
->highest_thread_num
;
342 /* Nothing to follow yet. */
343 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
344 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
345 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
348 thread_info::~thread_info ()
352 if (this->private_dtor
)
353 this->private_dtor (this->priv
);
361 /* Add TP to the end of the step-over chain LIST_P. */
364 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
366 gdb_assert (tp
->step_over_next
== NULL
);
367 gdb_assert (tp
->step_over_prev
== NULL
);
372 tp
->step_over_prev
= tp
->step_over_next
= tp
;
376 struct thread_info
*head
= *list_p
;
377 struct thread_info
*tail
= head
->step_over_prev
;
379 tp
->step_over_prev
= tail
;
380 tp
->step_over_next
= head
;
381 head
->step_over_prev
= tp
;
382 tail
->step_over_next
= tp
;
386 /* Remove TP from step-over chain LIST_P. */
389 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
391 gdb_assert (tp
->step_over_next
!= NULL
);
392 gdb_assert (tp
->step_over_prev
!= NULL
);
396 if (tp
== tp
->step_over_next
)
399 *list_p
= tp
->step_over_next
;
402 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
403 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
404 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
407 /* See gdbthread.h. */
410 thread_step_over_chain_next (struct thread_info
*tp
)
412 struct thread_info
*next
= tp
->step_over_next
;
414 return (next
== step_over_queue_head
? NULL
: next
);
417 /* See gdbthread.h. */
420 thread_is_in_step_over_chain (struct thread_info
*tp
)
422 return (tp
->step_over_next
!= NULL
);
425 /* See gdbthread.h. */
428 thread_step_over_chain_enqueue (struct thread_info
*tp
)
430 step_over_chain_enqueue (&step_over_queue_head
, tp
);
433 /* See gdbthread.h. */
436 thread_step_over_chain_remove (struct thread_info
*tp
)
438 step_over_chain_remove (&step_over_queue_head
, tp
);
441 /* Delete thread PTID. If SILENT, don't notify the observer of this
444 delete_thread_1 (ptid_t ptid
, int silent
)
446 struct thread_info
*tp
, *tpprev
;
450 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
451 if (ptid_equal (tp
->ptid
, ptid
))
457 set_thread_exited (tp
, silent
);
459 if (!tp
->deletable ())
461 /* Will be really deleted some other time. */
466 tpprev
->next
= tp
->next
;
468 thread_list
= tp
->next
;
473 /* Delete thread PTID and notify of thread exit. If this is
474 inferior_ptid, don't actually delete it, but tag it as exited and
475 do the notification. If PTID is the user selected thread, clear
478 delete_thread (ptid_t ptid
)
480 delete_thread_1 (ptid
, 0 /* not silent */);
484 delete_thread_silent (ptid_t ptid
)
486 delete_thread_1 (ptid
, 1 /* silent */);
490 find_thread_global_id (int global_id
)
492 struct thread_info
*tp
;
494 for (tp
= thread_list
; tp
; tp
= tp
->next
)
495 if (tp
->global_num
== global_id
)
501 static struct thread_info
*
502 find_thread_id (struct inferior
*inf
, int thr_num
)
504 struct thread_info
*tp
;
506 for (tp
= thread_list
; tp
; tp
= tp
->next
)
507 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
513 /* Find a thread_info by matching PTID. */
515 find_thread_ptid (ptid_t ptid
)
517 struct thread_info
*tp
;
519 for (tp
= thread_list
; tp
; tp
= tp
->next
)
520 if (ptid_equal (tp
->ptid
, ptid
))
527 * Thread iterator function.
529 * Calls a callback function once for each thread, so long as
530 * the callback function returns false. If the callback function
531 * returns true, the iteration will end and the current thread
532 * will be returned. This can be useful for implementing a
533 * search for a thread with arbitrary attributes, or for applying
534 * some operation to every thread.
536 * FIXME: some of the existing functionality, such as
537 * "Thread apply all", might be rewritten using this functionality.
541 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
544 struct thread_info
*tp
, *next
;
546 for (tp
= thread_list
; tp
; tp
= next
)
549 if ((*callback
) (tp
, data
))
560 struct thread_info
*tp
;
562 for (tp
= thread_list
; tp
; tp
= tp
->next
)
569 valid_global_thread_id (int global_id
)
571 struct thread_info
*tp
;
573 for (tp
= thread_list
; tp
; tp
= tp
->next
)
574 if (tp
->global_num
== global_id
)
581 ptid_to_global_thread_id (ptid_t ptid
)
583 struct thread_info
*tp
;
585 for (tp
= thread_list
; tp
; tp
= tp
->next
)
586 if (ptid_equal (tp
->ptid
, ptid
))
587 return tp
->global_num
;
593 global_thread_id_to_ptid (int global_id
)
595 struct thread_info
*thread
= find_thread_global_id (global_id
);
600 return minus_one_ptid
;
604 in_thread_list (ptid_t ptid
)
606 struct thread_info
*tp
;
608 for (tp
= thread_list
; tp
; tp
= tp
->next
)
609 if (ptid_equal (tp
->ptid
, ptid
))
612 return 0; /* Never heard of 'im. */
615 /* Finds the first thread of the inferior given by PID. If PID is -1,
616 return the first thread in the list. */
619 first_thread_of_process (int pid
)
621 struct thread_info
*tp
, *ret
= NULL
;
623 for (tp
= thread_list
; tp
; tp
= tp
->next
)
624 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
625 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
632 any_thread_of_process (int pid
)
634 struct thread_info
*tp
;
636 gdb_assert (pid
!= 0);
638 /* Prefer the current thread. */
639 if (ptid_get_pid (inferior_ptid
) == pid
)
640 return inferior_thread ();
642 ALL_NON_EXITED_THREADS (tp
)
643 if (ptid_get_pid (tp
->ptid
) == pid
)
650 any_live_thread_of_process (int pid
)
652 struct thread_info
*curr_tp
= NULL
;
653 struct thread_info
*tp
;
654 struct thread_info
*tp_executing
= NULL
;
656 gdb_assert (pid
!= 0);
658 /* Prefer the current thread if it's not executing. */
659 if (ptid_get_pid (inferior_ptid
) == pid
)
661 /* If the current thread is dead, forget it. If it's not
662 executing, use it. Otherwise, still choose it (below), but
663 only if no other non-executing thread is found. */
664 curr_tp
= inferior_thread ();
665 if (curr_tp
->state
== THREAD_EXITED
)
667 else if (!curr_tp
->executing
)
671 ALL_NON_EXITED_THREADS (tp
)
672 if (ptid_get_pid (tp
->ptid
) == pid
)
680 /* If both the current thread and all live threads are executing,
681 prefer the current thread. */
685 /* Otherwise, just return an executing thread, if any. */
689 /* Print a list of thread ids currently known, and the total number of
690 threads. To be used from within catch_errors. */
692 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
694 struct thread_info
*tp
;
696 struct cleanup
*cleanup_chain
;
697 int current_thread
= -1;
699 update_thread_list ();
701 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
703 for (tp
= thread_list
; tp
; tp
= tp
->next
)
705 if (tp
->state
== THREAD_EXITED
)
708 if (ptid_equal (tp
->ptid
, inferior_ptid
))
709 current_thread
= tp
->global_num
;
712 uiout
->field_int ("thread-id", tp
->global_num
);
715 do_cleanups (cleanup_chain
);
717 if (current_thread
!= -1)
718 uiout
->field_int ("current-thread-id", current_thread
);
719 uiout
->field_int ("number-of-threads", num
);
723 /* Official gdblib interface function to get a list of thread ids and
726 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
728 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
729 error_message
, RETURN_MASK_ALL
) < 0)
734 /* Return true if TP is an active thread. */
736 thread_alive (struct thread_info
*tp
)
738 if (tp
->state
== THREAD_EXITED
)
740 if (!target_thread_alive (tp
->ptid
))
745 /* See gdbthreads.h. */
750 struct thread_info
*tp
, *tmp
;
752 ALL_THREADS_SAFE (tp
, tmp
)
754 if (!thread_alive (tp
))
755 delete_thread (tp
->ptid
);
759 /* See gdbthreads.h. */
762 delete_exited_threads (void)
764 struct thread_info
*tp
, *tmp
;
766 ALL_THREADS_SAFE (tp
, tmp
)
768 if (tp
->state
== THREAD_EXITED
)
769 delete_thread (tp
->ptid
);
773 /* Disable storing stack temporaries for the thread whose id is
777 disable_thread_stack_temporaries (void *data
)
779 ptid_t
*pd
= (ptid_t
*) data
;
780 struct thread_info
*tp
= find_thread_ptid (*pd
);
784 tp
->stack_temporaries_enabled
= 0;
785 VEC_free (value_ptr
, tp
->stack_temporaries
);
791 /* Enable storing stack temporaries for thread with id PTID and return a
792 cleanup which can disable and clear the stack temporaries. */
795 enable_thread_stack_temporaries (ptid_t ptid
)
797 struct thread_info
*tp
= find_thread_ptid (ptid
);
801 gdb_assert (tp
!= NULL
);
803 tp
->stack_temporaries_enabled
= 1;
804 tp
->stack_temporaries
= NULL
;
805 data
= XNEW (ptid_t
);
807 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
812 /* Return non-zero value if stack temporaies are enabled for the thread
816 thread_stack_temporaries_enabled_p (ptid_t ptid
)
818 struct thread_info
*tp
= find_thread_ptid (ptid
);
823 return tp
->stack_temporaries_enabled
;
826 /* Push V on to the stack temporaries of the thread with id PTID. */
829 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
831 struct thread_info
*tp
= find_thread_ptid (ptid
);
833 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
834 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
837 /* Return 1 if VAL is among the stack temporaries of the thread
838 with id PTID. Return 0 otherwise. */
841 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
843 struct thread_info
*tp
= find_thread_ptid (ptid
);
845 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
846 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
851 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
859 /* Return the last of the stack temporaries for thread with id PTID.
860 Return NULL if there are no stack temporaries for the thread. */
863 get_last_thread_stack_temporary (ptid_t ptid
)
865 struct value
*lastval
= NULL
;
866 struct thread_info
*tp
= find_thread_ptid (ptid
);
868 gdb_assert (tp
!= NULL
);
869 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
870 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
876 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
878 struct inferior
*inf
;
879 struct thread_info
*tp
;
881 /* It can happen that what we knew as the target inferior id
882 changes. E.g, target remote may only discover the remote process
883 pid after adding the inferior to GDB's list. */
884 inf
= find_inferior_ptid (old_ptid
);
885 inf
->pid
= ptid_get_pid (new_ptid
);
887 tp
= find_thread_ptid (old_ptid
);
890 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
893 /* See gdbthread.h. */
896 set_resumed (ptid_t ptid
, int resumed
)
898 struct thread_info
*tp
;
899 int all
= ptid_equal (ptid
, minus_one_ptid
);
901 if (all
|| ptid_is_pid (ptid
))
903 for (tp
= thread_list
; tp
; tp
= tp
->next
)
904 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
905 tp
->resumed
= resumed
;
909 tp
= find_thread_ptid (ptid
);
910 gdb_assert (tp
!= NULL
);
911 tp
->resumed
= resumed
;
915 /* Helper for set_running, that marks one thread either running or
919 set_running_thread (struct thread_info
*tp
, int running
)
923 if (running
&& tp
->state
== THREAD_STOPPED
)
925 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
929 /* If the thread is now marked stopped, remove it from
930 the step-over queue, so that we don't try to resume
931 it until the user wants it to. */
932 if (tp
->step_over_next
!= NULL
)
933 thread_step_over_chain_remove (tp
);
940 set_running (ptid_t ptid
, int running
)
942 struct thread_info
*tp
;
943 int all
= ptid_equal (ptid
, minus_one_ptid
);
946 /* We try not to notify the observer if no thread has actually changed
947 the running state -- merely to reduce the number of messages to
948 frontend. Frontend is supposed to handle multiple *running just fine. */
949 if (all
|| ptid_is_pid (ptid
))
951 for (tp
= thread_list
; tp
; tp
= tp
->next
)
952 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
954 if (tp
->state
== THREAD_EXITED
)
957 if (set_running_thread (tp
, running
))
963 tp
= find_thread_ptid (ptid
);
964 gdb_assert (tp
!= NULL
);
965 gdb_assert (tp
->state
!= THREAD_EXITED
);
966 if (set_running_thread (tp
, running
))
970 observer_notify_target_resumed (ptid
);
974 is_thread_state (ptid_t ptid
, enum thread_state state
)
976 struct thread_info
*tp
;
978 tp
= find_thread_ptid (ptid
);
980 return tp
->state
== state
;
984 is_stopped (ptid_t ptid
)
986 return is_thread_state (ptid
, THREAD_STOPPED
);
990 is_exited (ptid_t ptid
)
992 return is_thread_state (ptid
, THREAD_EXITED
);
996 is_running (ptid_t ptid
)
998 return is_thread_state (ptid
, THREAD_RUNNING
);
1002 is_executing (ptid_t ptid
)
1004 struct thread_info
*tp
;
1006 tp
= find_thread_ptid (ptid
);
1008 return tp
->executing
;
1012 set_executing (ptid_t ptid
, int executing
)
1014 struct thread_info
*tp
;
1015 int all
= ptid_equal (ptid
, minus_one_ptid
);
1017 if (all
|| ptid_is_pid (ptid
))
1019 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1020 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1021 tp
->executing
= executing
;
1025 tp
= find_thread_ptid (ptid
);
1027 tp
->executing
= executing
;
1030 /* It only takes one running thread to spawn more threads.*/
1032 threads_executing
= 1;
1033 /* Only clear the flag if the caller is telling us everything is
1035 else if (ptid_equal (minus_one_ptid
, ptid
))
1036 threads_executing
= 0;
1039 /* See gdbthread.h. */
1042 threads_are_executing (void)
1044 return threads_executing
;
1048 set_stop_requested (ptid_t ptid
, int stop
)
1050 struct thread_info
*tp
;
1051 int all
= ptid_equal (ptid
, minus_one_ptid
);
1053 if (all
|| ptid_is_pid (ptid
))
1055 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1056 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1057 tp
->stop_requested
= stop
;
1061 tp
= find_thread_ptid (ptid
);
1063 tp
->stop_requested
= stop
;
1066 /* Call the stop requested observer so other components of GDB can
1067 react to this request. */
1069 observer_notify_thread_stop_requested (ptid
);
1073 finish_thread_state (ptid_t ptid
)
1075 struct thread_info
*tp
;
1077 int any_started
= 0;
1079 all
= ptid_equal (ptid
, minus_one_ptid
);
1081 if (all
|| ptid_is_pid (ptid
))
1083 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1085 if (tp
->state
== THREAD_EXITED
)
1087 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1089 if (set_running_thread (tp
, tp
->executing
))
1096 tp
= find_thread_ptid (ptid
);
1098 if (tp
->state
!= THREAD_EXITED
)
1100 if (set_running_thread (tp
, tp
->executing
))
1106 observer_notify_target_resumed (ptid
);
1110 finish_thread_state_cleanup (void *arg
)
1112 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1116 finish_thread_state (*ptid_p
);
1119 /* See gdbthread.h. */
1122 validate_registers_access (void)
1124 /* No selected thread, no registers. */
1125 if (ptid_equal (inferior_ptid
, null_ptid
))
1126 error (_("No thread selected."));
1128 /* Don't try to read from a dead thread. */
1129 if (is_exited (inferior_ptid
))
1130 error (_("The current thread has terminated"));
1132 /* ... or from a spinning thread. FIXME: This isn't actually fully
1133 correct. It'll allow an user-requested access (e.g., "print $pc"
1134 at the prompt) when a thread is not executing for some internal
1135 reason, but is marked running from the user's perspective. E.g.,
1136 the thread is waiting for its turn in the step-over queue. */
1137 if (is_executing (inferior_ptid
))
1138 error (_("Selected thread is running."));
1141 /* See gdbthread.h. */
1144 can_access_registers_ptid (ptid_t ptid
)
1146 /* No thread, no registers. */
1147 if (ptid_equal (ptid
, null_ptid
))
1150 /* Don't try to read from a dead thread. */
1151 if (is_exited (ptid
))
1154 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1155 if (is_executing (ptid
))
1162 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1164 return (pc
>= thread
->control
.step_range_start
1165 && pc
< thread
->control
.step_range_end
);
1168 /* Helper for print_thread_info. Returns true if THR should be
1169 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1170 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1171 is true if REQUESTED_THREADS is list of global IDs, false if a list
1172 of per-inferior thread ids. If PID is not -1, only print THR if it
1173 is a thread from the process PID. Otherwise, threads from all
1174 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1175 and PID is not -1, then the thread is printed if it belongs to the
1176 specified process. Otherwise, an error is raised. */
1179 should_print_thread (const char *requested_threads
, int default_inf_num
,
1180 int global_ids
, int pid
, struct thread_info
*thr
)
1182 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1187 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1189 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1190 thr
->inf
->num
, thr
->per_inf_num
);
1195 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1197 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1198 error (_("Requested thread not found in requested process"));
1202 if (thr
->state
== THREAD_EXITED
)
1208 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1209 whether REQUESTED_THREADS is a list of global or per-inferior
1213 print_thread_info_1 (struct ui_out
*uiout
, char *requested_threads
,
1214 int global_ids
, int pid
,
1215 int show_global_ids
)
1217 struct thread_info
*tp
;
1218 ptid_t current_ptid
;
1219 struct cleanup
*old_chain
;
1220 const char *extra_info
, *name
, *target_id
;
1221 struct inferior
*inf
;
1222 int default_inf_num
= current_inferior ()->num
;
1224 update_thread_list ();
1225 current_ptid
= inferior_ptid
;
1227 /* We'll be switching threads temporarily. */
1228 old_chain
= make_cleanup_restore_current_thread ();
1230 /* For backward compatibility, we make a list for MI. A table is
1231 preferable for the CLI, though, because it shows table
1233 if (uiout
->is_mi_like_p ())
1234 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
1239 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1241 if (!should_print_thread (requested_threads
, default_inf_num
,
1242 global_ids
, pid
, tp
))
1250 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1251 uiout
->message (_("No threads.\n"));
1253 uiout
->message (_("No threads match '%s'.\n"),
1255 do_cleanups (old_chain
);
1259 if (show_global_ids
|| uiout
->is_mi_like_p ())
1260 make_cleanup_ui_out_table_begin_end (uiout
, 5, n_threads
, "threads");
1262 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
1264 uiout
->table_header (1, ui_left
, "current", "");
1266 if (!uiout
->is_mi_like_p ())
1267 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1268 if (show_global_ids
|| uiout
->is_mi_like_p ())
1269 uiout
->table_header (4, ui_left
, "id", "GId");
1270 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1271 uiout
->table_header (1, ui_left
, "frame", "Frame");
1272 uiout
->table_body ();
1275 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1277 struct cleanup
*chain2
;
1280 if (!should_print_thread (requested_threads
, default_inf_num
,
1281 global_ids
, pid
, tp
))
1284 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1286 if (uiout
->is_mi_like_p ())
1288 /* Compatibility. */
1289 if (ptid_equal (tp
->ptid
, current_ptid
))
1296 if (ptid_equal (tp
->ptid
, current_ptid
))
1297 uiout
->field_string ("current", "*");
1299 uiout
->field_skip ("current");
1302 if (!uiout
->is_mi_like_p ())
1303 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1305 if (show_global_ids
|| uiout
->is_mi_like_p ())
1306 uiout
->field_int ("id", tp
->global_num
);
1308 /* For the CLI, we stuff everything into the target-id field.
1309 This is a gross hack to make the output come out looking
1310 correct. The underlying problem here is that ui-out has no
1311 way to specify that a field's space allocation should be
1312 shared by several fields. For MI, we do the right thing
1315 target_id
= target_pid_to_str (tp
->ptid
);
1316 extra_info
= target_extra_thread_info (tp
);
1317 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1319 if (uiout
->is_mi_like_p ())
1321 uiout
->field_string ("target-id", target_id
);
1323 uiout
->field_string ("details", extra_info
);
1325 uiout
->field_string ("name", name
);
1329 struct cleanup
*str_cleanup
;
1332 if (extra_info
&& name
)
1333 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
1335 else if (extra_info
)
1336 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
1338 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
1340 contents
= xstrdup (target_id
);
1341 str_cleanup
= make_cleanup (xfree
, contents
);
1343 uiout
->field_string ("target-id", contents
);
1344 do_cleanups (str_cleanup
);
1347 if (tp
->state
== THREAD_RUNNING
)
1348 uiout
->text ("(running)\n");
1351 /* The switch below puts us at the top of the stack (leaf
1353 switch_to_thread (tp
->ptid
);
1354 print_stack_frame (get_selected_frame (NULL
),
1355 /* For MI output, print frame level. */
1356 uiout
->is_mi_like_p (),
1360 if (uiout
->is_mi_like_p ())
1362 const char *state
= "stopped";
1364 if (tp
->state
== THREAD_RUNNING
)
1366 uiout
->field_string ("state", state
);
1369 core
= target_core_of_thread (tp
->ptid
);
1370 if (uiout
->is_mi_like_p () && core
!= -1)
1371 uiout
->field_int ("core", core
);
1373 do_cleanups (chain2
);
1376 /* Restores the current thread and the frame selected before
1377 the "info threads" command. */
1378 do_cleanups (old_chain
);
1380 if (pid
== -1 && requested_threads
== NULL
)
1382 if (uiout
->is_mi_like_p ()
1383 && !ptid_equal (inferior_ptid
, null_ptid
))
1385 int num
= ptid_to_global_thread_id (inferior_ptid
);
1387 gdb_assert (num
!= 0);
1388 uiout
->field_int ("current-thread-id", num
);
1391 if (!ptid_equal (inferior_ptid
, null_ptid
) && is_exited (inferior_ptid
))
1392 uiout
->message ("\n\
1393 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1394 print_thread_id (inferior_thread ()));
1395 else if (thread_list
!= NULL
1396 && ptid_equal (inferior_ptid
, null_ptid
))
1397 uiout
->message ("\n\
1398 No selected thread. See `help thread'.\n");
1402 /* See gdbthread.h. */
1405 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1407 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1410 /* Implementation of the "info threads" command.
1412 Note: this has the drawback that it _really_ switches
1413 threads, which frees the frame cache. A no-side
1414 effects info-threads command would be nicer. */
1417 info_threads_command (char *arg
, int from_tty
)
1419 int show_global_ids
= 0;
1422 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1424 arg
= skip_spaces (arg
);
1425 show_global_ids
= 1;
1428 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1431 /* See gdbthread.h. */
1434 switch_to_thread_no_regs (struct thread_info
*thread
)
1436 struct inferior
*inf
;
1438 inf
= find_inferior_ptid (thread
->ptid
);
1439 gdb_assert (inf
!= NULL
);
1440 set_current_program_space (inf
->pspace
);
1441 set_current_inferior (inf
);
1443 inferior_ptid
= thread
->ptid
;
1444 stop_pc
= ~(CORE_ADDR
) 0;
1447 /* Switch from one thread to another. */
1450 switch_to_thread (ptid_t ptid
)
1452 /* Switch the program space as well, if we can infer it from the now
1453 current thread. Otherwise, it's up to the caller to select the
1455 if (!ptid_equal (ptid
, null_ptid
))
1457 struct inferior
*inf
;
1459 inf
= find_inferior_ptid (ptid
);
1460 gdb_assert (inf
!= NULL
);
1461 set_current_program_space (inf
->pspace
);
1462 set_current_inferior (inf
);
1465 if (ptid_equal (ptid
, inferior_ptid
))
1468 inferior_ptid
= ptid
;
1469 reinit_frame_cache ();
1471 /* We don't check for is_stopped, because we're called at times
1472 while in the TARGET_RUNNING state, e.g., while handling an
1474 if (!ptid_equal (inferior_ptid
, null_ptid
)
1475 && !is_exited (ptid
)
1476 && !is_executing (ptid
))
1477 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1479 stop_pc
= ~(CORE_ADDR
) 0;
1483 restore_current_thread (ptid_t ptid
)
1485 switch_to_thread (ptid
);
1489 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1491 struct frame_info
*frame
= NULL
;
1494 /* This means there was no selected frame. */
1495 if (frame_level
== -1)
1497 select_frame (NULL
);
1501 gdb_assert (frame_level
>= 0);
1503 /* Restore by level first, check if the frame id is the same as
1504 expected. If that fails, try restoring by frame id. If that
1505 fails, nothing to do, just warn the user. */
1507 count
= frame_level
;
1508 frame
= find_relative_frame (get_current_frame (), &count
);
1511 /* The frame ids must match - either both valid or both outer_frame_id.
1512 The latter case is not failsafe, but since it's highly unlikely
1513 the search by level finds the wrong frame, it's 99.9(9)% of
1514 the time (for all practical purposes) safe. */
1515 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1517 /* Cool, all is fine. */
1518 select_frame (frame
);
1522 frame
= frame_find_by_id (a_frame_id
);
1525 /* Cool, refound it. */
1526 select_frame (frame
);
1530 /* Nothing else to do, the frame layout really changed. Select the
1531 innermost stack frame. */
1532 select_frame (get_current_frame ());
1534 /* Warn the user. */
1535 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1537 warning (_("Couldn't restore frame #%d in "
1538 "current thread. Bottom (innermost) frame selected:"),
1540 /* For MI, we should probably have a notification about
1541 current frame change. But this error is not very
1542 likely, so don't bother for now. */
1543 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1547 /* Data used by the cleanup installed by
1548 'make_cleanup_restore_current_thread'. */
1550 struct current_thread_cleanup
1552 /* Next in list of currently installed 'struct
1553 current_thread_cleanup' cleanups. See
1554 'current_thread_cleanup_chain' below. */
1555 struct current_thread_cleanup
*next
;
1557 thread_info
*thread
;
1558 struct frame_id selected_frame_id
;
1559 int selected_frame_level
;
1565 /* A chain of currently installed 'struct current_thread_cleanup'
1566 cleanups. Restoring the previously selected thread looks up the
1567 old thread in the thread list by ptid. If the thread changes ptid,
1568 we need to update the cleanup's thread structure so the look up
1570 static struct current_thread_cleanup
*current_thread_cleanup_chain
;
1573 do_restore_current_thread_cleanup (void *arg
)
1575 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1577 /* If an entry of thread_info was previously selected, it won't be
1578 deleted because we've increased its refcount. The thread represented
1579 by this thread_info entry may have already exited (due to normal exit,
1580 detach, etc), so the thread_info.state is THREAD_EXITED. */
1581 if (old
->thread
!= NULL
1582 /* If the previously selected thread belonged to a process that has
1583 in the mean time exited (or killed, detached, etc.), then don't revert
1584 back to it, but instead simply drop back to no thread selected. */
1585 && find_inferior_ptid (old
->thread
->ptid
) != NULL
)
1586 restore_current_thread (old
->thread
->ptid
);
1589 restore_current_thread (null_ptid
);
1590 set_current_inferior (find_inferior_id (old
->inf_id
));
1593 /* The running state of the originally selected thread may have
1594 changed, so we have to recheck it here. */
1595 if (!ptid_equal (inferior_ptid
, null_ptid
)
1597 && is_stopped (inferior_ptid
)
1598 && target_has_registers
1600 && target_has_memory
)
1601 restore_selected_frame (old
->selected_frame_id
,
1602 old
->selected_frame_level
);
1606 restore_current_thread_cleanup_dtor (void *arg
)
1608 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1609 struct thread_info
*tp
;
1610 struct inferior
*inf
;
1612 current_thread_cleanup_chain
= current_thread_cleanup_chain
->next
;
1614 if (old
->thread
!= NULL
)
1615 old
->thread
->decref ();
1617 inf
= find_inferior_id (old
->inf_id
);
1619 inf
->removable
= old
->was_removable
;
1623 /* Set the thread reference count. */
1626 set_thread_refcount (void *data
)
1629 struct thread_array_cleanup
*ta_cleanup
1630 = (struct thread_array_cleanup
*) data
;
1632 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1633 ta_cleanup
->tp_array
[k
]->decref ();
1637 make_cleanup_restore_current_thread (void)
1639 struct current_thread_cleanup
*old
= XNEW (struct current_thread_cleanup
);
1642 old
->inf_id
= current_inferior ()->num
;
1643 old
->was_removable
= current_inferior ()->removable
;
1645 old
->next
= current_thread_cleanup_chain
;
1646 current_thread_cleanup_chain
= old
;
1648 if (!ptid_equal (inferior_ptid
, null_ptid
))
1650 struct frame_info
*frame
;
1652 old
->was_stopped
= is_stopped (inferior_ptid
);
1653 if (old
->was_stopped
1654 && target_has_registers
1656 && target_has_memory
)
1658 /* When processing internal events, there might not be a
1659 selected frame. If we naively call get_selected_frame
1660 here, then we can end up reading debuginfo for the
1661 current frame, but we don't generally need the debuginfo
1663 frame
= get_selected_frame_if_set ();
1668 old
->selected_frame_id
= get_frame_id (frame
);
1669 old
->selected_frame_level
= frame_relative_level (frame
);
1671 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1678 current_inferior ()->removable
= 0;
1680 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1681 restore_current_thread_cleanup_dtor
);
1684 /* See gdbthread.h. */
1687 show_thread_that_caused_stop (void)
1689 return highest_thread_num
> 1;
1692 /* See gdbthread.h. */
1695 show_inferior_qualified_tids (void)
1697 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1700 /* See gdbthread.h. */
1703 print_thread_id (struct thread_info
*thr
)
1705 char *s
= get_print_cell ();
1707 if (show_inferior_qualified_tids ())
1708 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1710 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1714 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1715 descending order. */
1717 static int tp_array_compar_ascending
;
1719 /* Sort an array for struct thread_info pointers by thread ID (first
1720 by inferior number, and then by per-inferior thread number). The
1721 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1724 tp_array_compar (const void *ap_voidp
, const void *bp_voidp
)
1726 const struct thread_info
*a
= *(const struct thread_info
* const *) ap_voidp
;
1727 const struct thread_info
*b
= *(const struct thread_info
* const *) bp_voidp
;
1729 if (a
->inf
->num
!= b
->inf
->num
)
1731 return (((a
->inf
->num
> b
->inf
->num
) - (a
->inf
->num
< b
->inf
->num
))
1732 * (tp_array_compar_ascending
? +1 : -1));
1735 return (((a
->per_inf_num
> b
->per_inf_num
)
1736 - (a
->per_inf_num
< b
->per_inf_num
))
1737 * (tp_array_compar_ascending
? +1 : -1));
1740 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1741 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1742 of two numbers seperated by a hyphen. Examples:
1744 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1745 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1746 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1749 thread_apply_all_command (char *cmd
, int from_tty
)
1751 struct cleanup
*old_chain
;
1754 struct thread_array_cleanup ta_cleanup
;
1756 tp_array_compar_ascending
= 0;
1758 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1760 cmd
= skip_spaces (cmd
);
1761 tp_array_compar_ascending
= 1;
1764 if (cmd
== NULL
|| *cmd
== '\000')
1765 error (_("Please specify a command following the thread ID list"));
1767 update_thread_list ();
1769 old_chain
= make_cleanup_restore_current_thread ();
1771 /* Save a copy of the command in case it is clobbered by
1773 saved_cmd
= xstrdup (cmd
);
1774 make_cleanup (xfree
, saved_cmd
);
1776 /* Note this includes exited threads. */
1777 tc
= thread_count ();
1780 struct thread_info
**tp_array
;
1781 struct thread_info
*tp
;
1784 /* Save a copy of the thread_list in case we execute detach
1786 tp_array
= XNEWVEC (struct thread_info
*, tc
);
1787 make_cleanup (xfree
, tp_array
);
1789 ALL_NON_EXITED_THREADS (tp
)
1795 /* Because we skipped exited threads, we may end up with fewer
1796 threads in the array than the total count of threads. */
1797 gdb_assert (i
<= tc
);
1800 qsort (tp_array
, i
, sizeof (*tp_array
), tp_array_compar
);
1802 ta_cleanup
.tp_array
= tp_array
;
1803 ta_cleanup
.count
= i
;
1804 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1806 for (k
= 0; k
!= i
; k
++)
1807 if (thread_alive (tp_array
[k
]))
1809 switch_to_thread (tp_array
[k
]->ptid
);
1810 printf_filtered (_("\nThread %s (%s):\n"),
1811 print_thread_id (tp_array
[k
]),
1812 target_pid_to_str (inferior_ptid
));
1813 execute_command (cmd
, from_tty
);
1815 /* Restore exact command used previously. */
1816 strcpy (cmd
, saved_cmd
);
1820 do_cleanups (old_chain
);
1823 /* Implementation of the "thread apply" command. */
1826 thread_apply_command (char *tidlist
, int from_tty
)
1829 struct cleanup
*old_chain
;
1831 tid_range_parser parser
;
1833 if (tidlist
== NULL
|| *tidlist
== '\000')
1834 error (_("Please specify a thread ID list"));
1836 parser
.init (tidlist
, current_inferior ()->num
);
1837 while (!parser
.finished ())
1839 int inf_num
, thr_start
, thr_end
;
1841 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1843 cmd
= (char *) parser
.cur_tok ();
1849 error (_("Please specify a command following the thread ID list"));
1851 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1852 invalid_thread_id_error (cmd
);
1854 /* Save a copy of the command in case it is clobbered by
1856 saved_cmd
= xstrdup (cmd
);
1857 old_chain
= make_cleanup (xfree
, saved_cmd
);
1859 make_cleanup_restore_current_thread ();
1861 parser
.init (tidlist
, current_inferior ()->num
);
1862 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1864 struct thread_info
*tp
= NULL
;
1865 struct inferior
*inf
;
1866 int inf_num
, thr_num
;
1868 parser
.get_tid (&inf_num
, &thr_num
);
1869 inf
= find_inferior_id (inf_num
);
1871 tp
= find_thread_id (inf
, thr_num
);
1873 if (parser
.in_star_range ())
1877 warning (_("Unknown inferior %d"), inf_num
);
1878 parser
.skip_range ();
1882 /* No use looking for threads past the highest thread number
1883 the inferior ever had. */
1884 if (thr_num
>= inf
->highest_thread_num
)
1885 parser
.skip_range ();
1887 /* Be quiet about unknown threads numbers. */
1894 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1895 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1897 warning (_("Unknown thread %d"), thr_num
);
1901 if (!thread_alive (tp
))
1903 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1907 switch_to_thread (tp
->ptid
);
1909 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1910 target_pid_to_str (inferior_ptid
));
1911 execute_command (cmd
, from_tty
);
1913 /* Restore exact command used previously. */
1914 strcpy (cmd
, saved_cmd
);
1917 do_cleanups (old_chain
);
1920 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1921 if prefix of arg is `apply'. */
1924 thread_command (char *tidstr
, int from_tty
)
1928 if (ptid_equal (inferior_ptid
, null_ptid
))
1929 error (_("No thread selected"));
1931 if (target_has_stack
)
1933 struct thread_info
*tp
= inferior_thread ();
1935 if (is_exited (inferior_ptid
))
1936 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1937 print_thread_id (tp
),
1938 target_pid_to_str (inferior_ptid
));
1940 printf_filtered (_("[Current thread is %s (%s)]\n"),
1941 print_thread_id (tp
),
1942 target_pid_to_str (inferior_ptid
));
1945 error (_("No stack."));
1949 ptid_t previous_ptid
= inferior_ptid
;
1952 result
= gdb_thread_select (current_uiout
, tidstr
, NULL
);
1954 /* If thread switch did not succeed don't notify or print. */
1955 if (result
== GDB_RC_FAIL
)
1958 /* Print if the thread has not changed, otherwise an event will be sent. */
1959 if (ptid_equal (inferior_ptid
, previous_ptid
))
1961 print_selected_thread_frame (current_uiout
,
1962 USER_SELECTED_THREAD
1963 | USER_SELECTED_FRAME
);
1967 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1968 | USER_SELECTED_FRAME
);
1973 /* Implementation of `thread name'. */
1976 thread_name_command (char *arg
, int from_tty
)
1978 struct thread_info
*info
;
1980 if (ptid_equal (inferior_ptid
, null_ptid
))
1981 error (_("No thread selected"));
1983 arg
= skip_spaces (arg
);
1985 info
= inferior_thread ();
1987 info
->name
= arg
? xstrdup (arg
) : NULL
;
1990 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1993 thread_find_command (char *arg
, int from_tty
)
1995 struct thread_info
*tp
;
1997 unsigned long match
= 0;
1999 if (arg
== NULL
|| *arg
== '\0')
2000 error (_("Command requires an argument."));
2002 tmp
= re_comp (arg
);
2004 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
2006 update_thread_list ();
2007 for (tp
= thread_list
; tp
; tp
= tp
->next
)
2009 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
2011 printf_filtered (_("Thread %s has name '%s'\n"),
2012 print_thread_id (tp
), tp
->name
);
2016 tmp
= target_thread_name (tp
);
2017 if (tmp
!= NULL
&& re_exec (tmp
))
2019 printf_filtered (_("Thread %s has target name '%s'\n"),
2020 print_thread_id (tp
), tmp
);
2024 tmp
= target_pid_to_str (tp
->ptid
);
2025 if (tmp
!= NULL
&& re_exec (tmp
))
2027 printf_filtered (_("Thread %s has target id '%s'\n"),
2028 print_thread_id (tp
), tmp
);
2032 tmp
= target_extra_thread_info (tp
);
2033 if (tmp
!= NULL
&& re_exec (tmp
))
2035 printf_filtered (_("Thread %s has extra info '%s'\n"),
2036 print_thread_id (tp
), tmp
);
2041 printf_filtered (_("No threads match '%s'\n"), arg
);
2044 /* Print notices when new threads are attached and detached. */
2045 int print_thread_events
= 1;
2047 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2048 struct cmd_list_element
*c
, const char *value
)
2050 fprintf_filtered (file
,
2051 _("Printing of thread events is %s.\n"),
2056 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr_v
)
2058 const char *tidstr
= (const char *) tidstr_v
;
2059 struct thread_info
*tp
;
2061 if (uiout
->is_mi_like_p ())
2063 int num
= value_as_long (parse_and_eval (tidstr
));
2065 tp
= find_thread_global_id (num
);
2067 error (_("Thread ID %d not known."), num
);
2071 tp
= parse_thread_id (tidstr
, NULL
);
2072 gdb_assert (tp
!= NULL
);
2075 if (!thread_alive (tp
))
2076 error (_("Thread ID %s has terminated."), tidstr
);
2078 switch_to_thread (tp
->ptid
);
2080 annotate_thread_changed ();
2082 /* Since the current thread may have changed, see if there is any
2083 exited thread we can now delete. */
2089 /* Print thread and frame switch command response. */
2092 print_selected_thread_frame (struct ui_out
*uiout
,
2093 user_selected_what selection
)
2095 struct thread_info
*tp
= inferior_thread ();
2096 struct inferior
*inf
= current_inferior ();
2098 if (selection
& USER_SELECTED_THREAD
)
2100 if (uiout
->is_mi_like_p ())
2102 uiout
->field_int ("new-thread-id",
2103 inferior_thread ()->global_num
);
2107 uiout
->text ("[Switching to thread ");
2108 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2110 uiout
->text (target_pid_to_str (inferior_ptid
));
2115 if (tp
->state
== THREAD_RUNNING
)
2117 if (selection
& USER_SELECTED_THREAD
)
2118 uiout
->text ("(running)\n");
2120 else if (selection
& USER_SELECTED_FRAME
)
2122 if (selection
& USER_SELECTED_THREAD
)
2125 if (has_stack_frames ())
2126 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2132 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
2134 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
2135 error_message
, RETURN_MASK_ALL
) < 0)
2140 /* Update the 'threads_executing' global based on the threads we know
2144 update_threads_executing (void)
2146 struct thread_info
*tp
;
2148 threads_executing
= 0;
2149 ALL_NON_EXITED_THREADS (tp
)
2153 threads_executing
= 1;
2160 update_thread_list (void)
2162 target_update_thread_list ();
2163 update_threads_executing ();
2166 /* Return a new value for the selected thread's id. Return a value of
2167 0 if no thread is selected. If GLOBAL is true, return the thread's
2168 global number. Otherwise return the per-inferior number. */
2170 static struct value
*
2171 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2173 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2179 int_val
= tp
->global_num
;
2181 int_val
= tp
->per_inf_num
;
2183 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2186 /* Return a new value for the selected thread's per-inferior thread
2187 number. Return a value of 0 if no thread is selected, or no
2190 static struct value
*
2191 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2194 return thread_num_make_value_helper (gdbarch
, 0);
2197 /* Return a new value for the selected thread's global id. Return a
2198 value of 0 if no thread is selected, or no threads exist. */
2200 static struct value
*
2201 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2204 return thread_num_make_value_helper (gdbarch
, 1);
2207 /* Commands with a prefix of `thread'. */
2208 struct cmd_list_element
*thread_cmd_list
= NULL
;
2210 /* Implementation of `thread' variable. */
2212 static const struct internalvar_funcs thread_funcs
=
2214 thread_id_per_inf_num_make_value
,
2219 /* Implementation of `gthread' variable. */
2221 static const struct internalvar_funcs gthread_funcs
=
2223 global_thread_id_make_value
,
2229 _initialize_thread (void)
2231 static struct cmd_list_element
*thread_apply_list
= NULL
;
2233 add_info ("threads", info_threads_command
,
2234 _("Display currently known threads.\n\
2235 Usage: info threads [-gid] [ID]...\n\
2236 -gid: Show global thread IDs.\n\
2237 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2238 Otherwise, all threads are displayed."));
2240 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2241 Use this command to switch between threads.\n\
2242 The new thread ID must be currently known."),
2243 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2245 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2246 _("Apply a command to a list of threads."),
2247 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2249 add_cmd ("all", class_run
, thread_apply_all_command
,
2251 Apply a command to all threads.\n\
2253 Usage: thread apply all [-ascending] <command>\n\
2254 -ascending: Call <command> for all threads in ascending order.\n\
2255 The default is descending order.\
2257 &thread_apply_list
);
2259 add_cmd ("name", class_run
, thread_name_command
,
2260 _("Set the current thread's name.\n\
2261 Usage: thread name [NAME]\n\
2262 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2264 add_cmd ("find", class_run
, thread_find_command
, _("\
2265 Find threads that match a regular expression.\n\
2266 Usage: thread find REGEXP\n\
2267 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2270 add_com_alias ("t", "thread", class_run
, 1);
2272 add_setshow_boolean_cmd ("thread-events", no_class
,
2273 &print_thread_events
, _("\
2274 Set printing of thread events (such as thread start and exit)."), _("\
2275 Show printing of thread events (such as thread start and exit)."), NULL
,
2277 show_print_thread_events
,
2278 &setprintlist
, &showprintlist
);
2280 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2281 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);