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"
36 #include <sys/types.h>
41 #include "cli/cli-decode.h"
42 #include "gdb_regex.h"
43 #include "cli/cli-utils.h"
44 #include "thread-fsm.h"
45 #include "tid-parse.h"
47 #include "common/gdb_optional.h"
49 /* Definition of struct thread_info exported to gdbthread.h. */
51 /* Prototypes for local functions. */
53 struct thread_info
*thread_list
= NULL
;
54 static int highest_thread_num
;
56 /* True if any thread is, or may be executing. We need to track this
57 separately because until we fully sync the thread list, we won't
58 know whether the target is fully stopped, even if we see stop
59 events for all known threads, because any of those threads may have
60 spawned new threads we haven't heard of yet. */
61 static int threads_executing
;
63 static int thread_alive (struct thread_info
*);
65 /* RAII type used to increase / decrease the refcount of each thread
66 in a given list of threads. */
68 class scoped_inc_dec_ref
71 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
74 for (thread_info
*thr
: m_thrds
)
78 ~scoped_inc_dec_ref ()
80 for (thread_info
*thr
: m_thrds
)
85 const std::vector
<thread_info
*> &m_thrds
;
90 inferior_thread (void)
92 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
97 /* Delete the breakpoint pointed at by BP_P, if there's one. */
100 delete_thread_breakpoint (struct breakpoint
**bp_p
)
104 delete_breakpoint (*bp_p
);
110 delete_step_resume_breakpoint (struct thread_info
*tp
)
113 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
117 delete_exception_resume_breakpoint (struct thread_info
*tp
)
120 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
123 /* See gdbthread.h. */
126 delete_single_step_breakpoints (struct thread_info
*tp
)
129 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
132 /* Delete the breakpoint pointed at by BP_P at the next stop, if
136 delete_at_next_stop (struct breakpoint
**bp
)
140 (*bp
)->disposition
= disp_del_at_next_stop
;
145 /* See gdbthread.h. */
148 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
150 return tp
->control
.single_step_breakpoints
!= NULL
;
153 /* See gdbthread.h. */
156 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
157 const address_space
*aspace
,
160 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
162 return (ss_bps
!= NULL
163 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
166 /* See gdbthread.h. */
169 thread_cancel_execution_command (struct thread_info
*thr
)
171 if (thr
->thread_fsm
!= NULL
)
173 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
174 thread_fsm_delete (thr
->thread_fsm
);
175 thr
->thread_fsm
= NULL
;
180 clear_thread_inferior_resources (struct thread_info
*tp
)
182 /* NOTE: this will take care of any left-over step_resume breakpoints,
183 but not any user-specified thread-specific breakpoints. We can not
184 delete the breakpoint straight-off, because the inferior might not
185 be stopped at the moment. */
186 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
187 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
188 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
190 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
192 bpstat_clear (&tp
->control
.stop_bpstat
);
194 btrace_teardown (tp
);
196 thread_cancel_execution_command (tp
);
199 /* Set the TP's state as exited. */
202 set_thread_exited (thread_info
*tp
, int silent
)
204 /* Dead threads don't need to step-over. Remove from queue. */
205 if (tp
->step_over_next
!= NULL
)
206 thread_step_over_chain_remove (tp
);
208 if (tp
->state
!= THREAD_EXITED
)
210 observer_notify_thread_exit (tp
, silent
);
212 /* Tag it as exited. */
213 tp
->state
= THREAD_EXITED
;
215 /* Clear breakpoints, etc. associated with this thread. */
216 clear_thread_inferior_resources (tp
);
221 init_thread_list (void)
223 struct thread_info
*tp
, *tpnext
;
225 highest_thread_num
= 0;
230 for (tp
= thread_list
; tp
; tp
= tpnext
)
233 if (tp
->deletable ())
236 set_thread_exited (tp
, 1);
240 threads_executing
= 0;
243 /* Allocate a new thread of inferior INF with target id PTID and add
244 it to the thread list. */
246 static struct thread_info
*
247 new_thread (struct inferior
*inf
, ptid_t ptid
)
249 thread_info
*tp
= new thread_info (inf
, ptid
);
251 if (thread_list
== NULL
)
255 struct thread_info
*last
;
257 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
266 add_thread_silent (ptid_t ptid
)
268 struct thread_info
*tp
;
269 struct inferior
*inf
= find_inferior_ptid (ptid
);
270 gdb_assert (inf
!= NULL
);
272 tp
= find_thread_ptid (ptid
);
274 /* Found an old thread with the same id. It has to be dead,
275 otherwise we wouldn't be adding a new thread with the same id.
276 The OS is reusing this id --- delete it, and recreate a new
279 /* In addition to deleting the thread, if this is the current
280 thread, then we need to take care that delete_thread doesn't
281 really delete the thread if it is inferior_ptid. Create a
282 new template thread in the list with an invalid ptid, switch
283 to it, delete the original thread, reset the new thread's
284 ptid, and switch to it. */
286 if (inferior_ptid
== ptid
)
288 tp
= new_thread (inf
, null_ptid
);
290 /* Make switch_to_thread not read from the thread. */
291 tp
->state
= THREAD_EXITED
;
292 switch_to_thread (null_ptid
);
294 /* Now we can delete it. */
295 delete_thread (ptid
);
297 /* Now reset its ptid, and reswitch inferior_ptid to it. */
299 tp
->state
= THREAD_STOPPED
;
300 switch_to_thread (ptid
);
302 observer_notify_new_thread (tp
);
308 /* Just go ahead and delete it. */
309 delete_thread (ptid
);
312 tp
= new_thread (inf
, ptid
);
313 observer_notify_new_thread (tp
);
319 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
321 struct thread_info
*result
= add_thread_silent (ptid
);
325 if (print_thread_events
)
326 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
328 annotate_new_thread ();
333 add_thread (ptid_t ptid
)
335 return add_thread_with_info (ptid
, NULL
);
338 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
339 : ptid (ptid_
), inf (inf_
)
341 gdb_assert (inf_
!= NULL
);
343 this->global_num
= ++highest_thread_num
;
344 this->per_inf_num
= ++inf_
->highest_thread_num
;
346 /* Nothing to follow yet. */
347 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
348 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
349 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
352 thread_info::~thread_info ()
356 if (this->private_dtor
)
357 this->private_dtor (this->priv
);
365 /* Add TP to the end of the step-over chain LIST_P. */
368 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
370 gdb_assert (tp
->step_over_next
== NULL
);
371 gdb_assert (tp
->step_over_prev
== NULL
);
376 tp
->step_over_prev
= tp
->step_over_next
= tp
;
380 struct thread_info
*head
= *list_p
;
381 struct thread_info
*tail
= head
->step_over_prev
;
383 tp
->step_over_prev
= tail
;
384 tp
->step_over_next
= head
;
385 head
->step_over_prev
= tp
;
386 tail
->step_over_next
= tp
;
390 /* Remove TP from step-over chain LIST_P. */
393 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
395 gdb_assert (tp
->step_over_next
!= NULL
);
396 gdb_assert (tp
->step_over_prev
!= NULL
);
400 if (tp
== tp
->step_over_next
)
403 *list_p
= tp
->step_over_next
;
406 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
407 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
408 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
411 /* See gdbthread.h. */
414 thread_step_over_chain_next (struct thread_info
*tp
)
416 struct thread_info
*next
= tp
->step_over_next
;
418 return (next
== step_over_queue_head
? NULL
: next
);
421 /* See gdbthread.h. */
424 thread_is_in_step_over_chain (struct thread_info
*tp
)
426 return (tp
->step_over_next
!= NULL
);
429 /* See gdbthread.h. */
432 thread_step_over_chain_enqueue (struct thread_info
*tp
)
434 step_over_chain_enqueue (&step_over_queue_head
, tp
);
437 /* See gdbthread.h. */
440 thread_step_over_chain_remove (struct thread_info
*tp
)
442 step_over_chain_remove (&step_over_queue_head
, tp
);
445 /* Delete thread PTID. If SILENT, don't notify the observer of this
448 delete_thread_1 (ptid_t ptid
, int silent
)
450 struct thread_info
*tp
, *tpprev
;
454 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
455 if (tp
->ptid
== ptid
)
461 set_thread_exited (tp
, silent
);
463 if (!tp
->deletable ())
465 /* Will be really deleted some other time. */
470 tpprev
->next
= tp
->next
;
472 thread_list
= tp
->next
;
477 /* Delete thread PTID and notify of thread exit. If this is
478 inferior_ptid, don't actually delete it, but tag it as exited and
479 do the notification. If PTID is the user selected thread, clear
482 delete_thread (ptid_t ptid
)
484 delete_thread_1 (ptid
, 0 /* not silent */);
488 delete_thread_silent (ptid_t ptid
)
490 delete_thread_1 (ptid
, 1 /* silent */);
494 find_thread_global_id (int global_id
)
496 struct thread_info
*tp
;
498 for (tp
= thread_list
; tp
; tp
= tp
->next
)
499 if (tp
->global_num
== global_id
)
505 static struct thread_info
*
506 find_thread_id (struct inferior
*inf
, int thr_num
)
508 struct thread_info
*tp
;
510 for (tp
= thread_list
; tp
; tp
= tp
->next
)
511 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
517 /* Find a thread_info by matching PTID. */
520 find_thread_ptid (ptid_t ptid
)
522 struct thread_info
*tp
;
524 for (tp
= thread_list
; tp
; tp
= tp
->next
)
525 if (tp
->ptid
== ptid
)
531 /* See gdbthread.h. */
534 find_thread_by_handle (struct value
*thread_handle
, struct inferior
*inf
)
536 return target_thread_handle_to_thread_info
537 (value_contents_all (thread_handle
),
538 TYPE_LENGTH (value_type (thread_handle
)),
543 * Thread iterator function.
545 * Calls a callback function once for each thread, so long as
546 * the callback function returns false. If the callback function
547 * returns true, the iteration will end and the current thread
548 * will be returned. This can be useful for implementing a
549 * search for a thread with arbitrary attributes, or for applying
550 * some operation to every thread.
552 * FIXME: some of the existing functionality, such as
553 * "Thread apply all", might be rewritten using this functionality.
557 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
560 struct thread_info
*tp
, *next
;
562 for (tp
= thread_list
; tp
; tp
= next
)
565 if ((*callback
) (tp
, data
))
576 struct thread_info
*tp
;
578 for (tp
= thread_list
; tp
; tp
= tp
->next
)
584 /* Return the number of non-exited threads in the thread list. */
587 live_threads_count (void)
590 struct thread_info
*tp
;
592 ALL_NON_EXITED_THREADS (tp
)
599 valid_global_thread_id (int global_id
)
601 struct thread_info
*tp
;
603 for (tp
= thread_list
; tp
; tp
= tp
->next
)
604 if (tp
->global_num
== global_id
)
611 ptid_to_global_thread_id (ptid_t ptid
)
613 struct thread_info
*tp
;
615 for (tp
= thread_list
; tp
; tp
= tp
->next
)
616 if (tp
->ptid
== ptid
)
617 return tp
->global_num
;
623 global_thread_id_to_ptid (int global_id
)
625 struct thread_info
*thread
= find_thread_global_id (global_id
);
630 return minus_one_ptid
;
634 in_thread_list (ptid_t ptid
)
636 struct thread_info
*tp
;
638 for (tp
= thread_list
; tp
; tp
= tp
->next
)
639 if (tp
->ptid
== ptid
)
642 return 0; /* Never heard of 'im. */
645 /* Finds the first thread of the inferior given by PID. If PID is -1,
646 return the first thread in the list. */
649 first_thread_of_process (int pid
)
651 struct thread_info
*tp
, *ret
= NULL
;
653 for (tp
= thread_list
; tp
; tp
= tp
->next
)
654 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
655 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
662 any_thread_of_process (int pid
)
664 struct thread_info
*tp
;
666 gdb_assert (pid
!= 0);
668 /* Prefer the current thread. */
669 if (ptid_get_pid (inferior_ptid
) == pid
)
670 return inferior_thread ();
672 ALL_NON_EXITED_THREADS (tp
)
673 if (ptid_get_pid (tp
->ptid
) == pid
)
680 any_live_thread_of_process (int pid
)
682 struct thread_info
*curr_tp
= NULL
;
683 struct thread_info
*tp
;
684 struct thread_info
*tp_executing
= NULL
;
686 gdb_assert (pid
!= 0);
688 /* Prefer the current thread if it's not executing. */
689 if (ptid_get_pid (inferior_ptid
) == pid
)
691 /* If the current thread is dead, forget it. If it's not
692 executing, use it. Otherwise, still choose it (below), but
693 only if no other non-executing thread is found. */
694 curr_tp
= inferior_thread ();
695 if (curr_tp
->state
== THREAD_EXITED
)
697 else if (!curr_tp
->executing
)
701 ALL_NON_EXITED_THREADS (tp
)
702 if (ptid_get_pid (tp
->ptid
) == pid
)
710 /* If both the current thread and all live threads are executing,
711 prefer the current thread. */
715 /* Otherwise, just return an executing thread, if any. */
719 /* Return true if TP is an active thread. */
721 thread_alive (struct thread_info
*tp
)
723 if (tp
->state
== THREAD_EXITED
)
725 if (!target_thread_alive (tp
->ptid
))
730 /* See gdbthreads.h. */
735 struct thread_info
*tp
, *tmp
;
737 ALL_THREADS_SAFE (tp
, tmp
)
739 if (!thread_alive (tp
))
740 delete_thread (tp
->ptid
);
744 /* See gdbthreads.h. */
747 delete_exited_threads (void)
749 struct thread_info
*tp
, *tmp
;
751 ALL_THREADS_SAFE (tp
, tmp
)
753 if (tp
->state
== THREAD_EXITED
)
754 delete_thread (tp
->ptid
);
758 /* Disable storing stack temporaries for the thread whose id is
762 disable_thread_stack_temporaries (void *data
)
764 ptid_t
*pd
= (ptid_t
*) data
;
765 struct thread_info
*tp
= find_thread_ptid (*pd
);
769 tp
->stack_temporaries_enabled
= 0;
770 VEC_free (value_ptr
, tp
->stack_temporaries
);
776 /* Enable storing stack temporaries for thread with id PTID and return a
777 cleanup which can disable and clear the stack temporaries. */
780 enable_thread_stack_temporaries (ptid_t ptid
)
782 struct thread_info
*tp
= find_thread_ptid (ptid
);
786 gdb_assert (tp
!= NULL
);
788 tp
->stack_temporaries_enabled
= 1;
789 tp
->stack_temporaries
= NULL
;
790 data
= XNEW (ptid_t
);
792 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
797 /* Return non-zero value if stack temporaies are enabled for the thread
801 thread_stack_temporaries_enabled_p (ptid_t ptid
)
803 struct thread_info
*tp
= find_thread_ptid (ptid
);
808 return tp
->stack_temporaries_enabled
;
811 /* Push V on to the stack temporaries of the thread with id PTID. */
814 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
816 struct thread_info
*tp
= find_thread_ptid (ptid
);
818 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
819 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
822 /* Return 1 if VAL is among the stack temporaries of the thread
823 with id PTID. Return 0 otherwise. */
826 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
828 struct thread_info
*tp
= find_thread_ptid (ptid
);
830 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
831 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
836 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
844 /* Return the last of the stack temporaries for thread with id PTID.
845 Return NULL if there are no stack temporaries for the thread. */
848 get_last_thread_stack_temporary (ptid_t ptid
)
850 struct value
*lastval
= NULL
;
851 struct thread_info
*tp
= find_thread_ptid (ptid
);
853 gdb_assert (tp
!= NULL
);
854 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
855 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
861 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
863 struct inferior
*inf
;
864 struct thread_info
*tp
;
866 /* It can happen that what we knew as the target inferior id
867 changes. E.g, target remote may only discover the remote process
868 pid after adding the inferior to GDB's list. */
869 inf
= find_inferior_ptid (old_ptid
);
870 inf
->pid
= ptid_get_pid (new_ptid
);
872 tp
= find_thread_ptid (old_ptid
);
875 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
878 /* See gdbthread.h. */
881 set_resumed (ptid_t ptid
, int resumed
)
883 struct thread_info
*tp
;
884 int all
= ptid
== minus_one_ptid
;
886 if (all
|| ptid_is_pid (ptid
))
888 for (tp
= thread_list
; tp
; tp
= tp
->next
)
889 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
890 tp
->resumed
= resumed
;
894 tp
= find_thread_ptid (ptid
);
895 gdb_assert (tp
!= NULL
);
896 tp
->resumed
= resumed
;
900 /* Helper for set_running, that marks one thread either running or
904 set_running_thread (struct thread_info
*tp
, int running
)
908 if (running
&& tp
->state
== THREAD_STOPPED
)
910 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
914 /* If the thread is now marked stopped, remove it from
915 the step-over queue, so that we don't try to resume
916 it until the user wants it to. */
917 if (tp
->step_over_next
!= NULL
)
918 thread_step_over_chain_remove (tp
);
925 set_running (ptid_t ptid
, int running
)
927 struct thread_info
*tp
;
928 int all
= ptid
== minus_one_ptid
;
931 /* We try not to notify the observer if no thread has actually changed
932 the running state -- merely to reduce the number of messages to
933 frontend. Frontend is supposed to handle multiple *running just fine. */
934 if (all
|| ptid_is_pid (ptid
))
936 for (tp
= thread_list
; tp
; tp
= tp
->next
)
937 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
939 if (tp
->state
== THREAD_EXITED
)
942 if (set_running_thread (tp
, running
))
948 tp
= find_thread_ptid (ptid
);
949 gdb_assert (tp
!= NULL
);
950 gdb_assert (tp
->state
!= THREAD_EXITED
);
951 if (set_running_thread (tp
, running
))
955 observer_notify_target_resumed (ptid
);
959 is_thread_state (ptid_t ptid
, enum thread_state state
)
961 struct thread_info
*tp
;
963 tp
= find_thread_ptid (ptid
);
965 return tp
->state
== state
;
969 is_stopped (ptid_t ptid
)
971 return is_thread_state (ptid
, THREAD_STOPPED
);
975 is_exited (ptid_t ptid
)
977 return is_thread_state (ptid
, THREAD_EXITED
);
981 is_running (ptid_t ptid
)
983 return is_thread_state (ptid
, THREAD_RUNNING
);
987 is_executing (ptid_t ptid
)
989 struct thread_info
*tp
;
991 tp
= find_thread_ptid (ptid
);
993 return tp
->executing
;
997 set_executing (ptid_t ptid
, int executing
)
999 struct thread_info
*tp
;
1000 int all
= ptid
== minus_one_ptid
;
1002 if (all
|| ptid_is_pid (ptid
))
1004 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1005 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1006 tp
->executing
= executing
;
1010 tp
= find_thread_ptid (ptid
);
1012 tp
->executing
= executing
;
1015 /* It only takes one running thread to spawn more threads.*/
1017 threads_executing
= 1;
1018 /* Only clear the flag if the caller is telling us everything is
1020 else if (minus_one_ptid
== ptid
)
1021 threads_executing
= 0;
1024 /* See gdbthread.h. */
1027 threads_are_executing (void)
1029 return threads_executing
;
1033 set_stop_requested (ptid_t ptid
, int stop
)
1035 struct thread_info
*tp
;
1036 int all
= ptid
== minus_one_ptid
;
1038 if (all
|| ptid_is_pid (ptid
))
1040 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1041 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1042 tp
->stop_requested
= stop
;
1046 tp
= find_thread_ptid (ptid
);
1048 tp
->stop_requested
= stop
;
1051 /* Call the stop requested observer so other components of GDB can
1052 react to this request. */
1054 observer_notify_thread_stop_requested (ptid
);
1058 finish_thread_state (ptid_t ptid
)
1060 struct thread_info
*tp
;
1062 int any_started
= 0;
1064 all
= ptid
== minus_one_ptid
;
1066 if (all
|| ptid_is_pid (ptid
))
1068 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1070 if (tp
->state
== THREAD_EXITED
)
1072 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1074 if (set_running_thread (tp
, tp
->executing
))
1081 tp
= find_thread_ptid (ptid
);
1083 if (tp
->state
!= THREAD_EXITED
)
1085 if (set_running_thread (tp
, tp
->executing
))
1091 observer_notify_target_resumed (ptid
);
1095 finish_thread_state_cleanup (void *arg
)
1097 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1101 finish_thread_state (*ptid_p
);
1104 /* See gdbthread.h. */
1107 validate_registers_access (void)
1109 /* No selected thread, no registers. */
1110 if (inferior_ptid
== null_ptid
)
1111 error (_("No thread selected."));
1113 /* Don't try to read from a dead thread. */
1114 if (is_exited (inferior_ptid
))
1115 error (_("The current thread has terminated"));
1117 /* ... or from a spinning thread. FIXME: This isn't actually fully
1118 correct. It'll allow an user-requested access (e.g., "print $pc"
1119 at the prompt) when a thread is not executing for some internal
1120 reason, but is marked running from the user's perspective. E.g.,
1121 the thread is waiting for its turn in the step-over queue. */
1122 if (is_executing (inferior_ptid
))
1123 error (_("Selected thread is running."));
1126 /* See gdbthread.h. */
1129 can_access_registers_ptid (ptid_t ptid
)
1131 /* No thread, no registers. */
1132 if (ptid
== null_ptid
)
1135 /* Don't try to read from a dead thread. */
1136 if (is_exited (ptid
))
1139 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1140 if (is_executing (ptid
))
1147 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1149 return (pc
>= thread
->control
.step_range_start
1150 && pc
< thread
->control
.step_range_end
);
1153 /* Helper for print_thread_info. Returns true if THR should be
1154 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1155 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1156 is true if REQUESTED_THREADS is list of global IDs, false if a list
1157 of per-inferior thread ids. If PID is not -1, only print THR if it
1158 is a thread from the process PID. Otherwise, threads from all
1159 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1160 and PID is not -1, then the thread is printed if it belongs to the
1161 specified process. Otherwise, an error is raised. */
1164 should_print_thread (const char *requested_threads
, int default_inf_num
,
1165 int global_ids
, int pid
, struct thread_info
*thr
)
1167 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1172 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1174 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1175 thr
->inf
->num
, thr
->per_inf_num
);
1180 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1182 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1183 error (_("Requested thread not found in requested process"));
1187 if (thr
->state
== THREAD_EXITED
)
1193 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1194 whether REQUESTED_THREADS is a list of global or per-inferior
1198 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1199 int global_ids
, int pid
,
1200 int show_global_ids
)
1202 struct thread_info
*tp
;
1203 ptid_t current_ptid
;
1204 const char *extra_info
, *name
, *target_id
;
1205 struct inferior
*inf
;
1206 int default_inf_num
= current_inferior ()->num
;
1208 update_thread_list ();
1209 current_ptid
= inferior_ptid
;
1212 /* For backward compatibility, we make a list for MI. A table is
1213 preferable for the CLI, though, because it shows table
1215 gdb::optional
<ui_out_emit_list
> list_emitter
;
1216 gdb::optional
<ui_out_emit_table
> table_emitter
;
1218 if (uiout
->is_mi_like_p ())
1219 list_emitter
.emplace (uiout
, "threads");
1224 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1226 if (!should_print_thread (requested_threads
, default_inf_num
,
1227 global_ids
, pid
, tp
))
1235 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1236 uiout
->message (_("No threads.\n"));
1238 uiout
->message (_("No threads match '%s'.\n"),
1243 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1244 n_threads
, "threads");
1246 uiout
->table_header (1, ui_left
, "current", "");
1247 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1248 if (show_global_ids
)
1249 uiout
->table_header (4, ui_left
, "id", "GId");
1250 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1251 uiout
->table_header (1, ui_left
, "frame", "Frame");
1252 uiout
->table_body ();
1255 /* We'll be switching threads temporarily. */
1256 scoped_restore_current_thread restore_thread
;
1258 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1262 if (!should_print_thread (requested_threads
, default_inf_num
,
1263 global_ids
, pid
, tp
))
1266 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1268 if (!uiout
->is_mi_like_p ())
1270 if (tp
->ptid
== current_ptid
)
1271 uiout
->field_string ("current", "*");
1273 uiout
->field_skip ("current");
1275 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1278 if (show_global_ids
|| uiout
->is_mi_like_p ())
1279 uiout
->field_int ("id", tp
->global_num
);
1281 /* For the CLI, we stuff everything into the target-id field.
1282 This is a gross hack to make the output come out looking
1283 correct. The underlying problem here is that ui-out has no
1284 way to specify that a field's space allocation should be
1285 shared by several fields. For MI, we do the right thing
1288 target_id
= target_pid_to_str (tp
->ptid
);
1289 extra_info
= target_extra_thread_info (tp
);
1290 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1292 if (uiout
->is_mi_like_p ())
1294 uiout
->field_string ("target-id", target_id
);
1296 uiout
->field_string ("details", extra_info
);
1298 uiout
->field_string ("name", name
);
1302 std::string contents
;
1304 if (extra_info
&& name
)
1305 contents
= string_printf ("%s \"%s\" (%s)", target_id
,
1307 else if (extra_info
)
1308 contents
= string_printf ("%s (%s)", target_id
, extra_info
);
1310 contents
= string_printf ("%s \"%s\"", target_id
, name
);
1312 contents
= target_id
;
1314 uiout
->field_string ("target-id", contents
.c_str ());
1317 if (tp
->state
== THREAD_RUNNING
)
1318 uiout
->text ("(running)\n");
1321 /* The switch below puts us at the top of the stack (leaf
1323 switch_to_thread (tp
->ptid
);
1324 print_stack_frame (get_selected_frame (NULL
),
1325 /* For MI output, print frame level. */
1326 uiout
->is_mi_like_p (),
1330 if (uiout
->is_mi_like_p ())
1332 const char *state
= "stopped";
1334 if (tp
->state
== THREAD_RUNNING
)
1336 uiout
->field_string ("state", state
);
1339 core
= target_core_of_thread (tp
->ptid
);
1340 if (uiout
->is_mi_like_p () && core
!= -1)
1341 uiout
->field_int ("core", core
);
1344 /* This end scope restores the current thread and the frame
1345 selected before the "info threads" command, and it finishes the
1346 ui-out list or table. */
1349 if (pid
== -1 && requested_threads
== NULL
)
1351 if (uiout
->is_mi_like_p ()
1352 && inferior_ptid
!= null_ptid
)
1354 int num
= ptid_to_global_thread_id (inferior_ptid
);
1356 gdb_assert (num
!= 0);
1357 uiout
->field_int ("current-thread-id", num
);
1360 if (inferior_ptid
!= null_ptid
&& is_exited (inferior_ptid
))
1361 uiout
->message ("\n\
1362 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1363 print_thread_id (inferior_thread ()));
1364 else if (thread_list
!= NULL
&& inferior_ptid
== null_ptid
)
1365 uiout
->message ("\n\
1366 No selected thread. See `help thread'.\n");
1370 /* See gdbthread.h. */
1373 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1375 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1378 /* Implementation of the "info threads" command.
1380 Note: this has the drawback that it _really_ switches
1381 threads, which frees the frame cache. A no-side
1382 effects info-threads command would be nicer. */
1385 info_threads_command (const char *arg
, int from_tty
)
1387 int show_global_ids
= 0;
1390 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1392 arg
= skip_spaces (arg
);
1393 show_global_ids
= 1;
1396 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1399 /* See gdbthread.h. */
1402 switch_to_thread_no_regs (struct thread_info
*thread
)
1404 struct inferior
*inf
= thread
->inf
;
1406 set_current_program_space (inf
->pspace
);
1407 set_current_inferior (inf
);
1409 inferior_ptid
= thread
->ptid
;
1410 stop_pc
= ~(CORE_ADDR
) 0;
1413 /* Switch to no thread selected. */
1416 switch_to_no_thread ()
1418 if (inferior_ptid
== null_ptid
)
1421 inferior_ptid
= null_ptid
;
1422 reinit_frame_cache ();
1423 stop_pc
= ~(CORE_ADDR
) 0;
1426 /* Switch from one thread to another. */
1429 switch_to_thread (thread_info
*thr
)
1431 gdb_assert (thr
!= NULL
);
1433 if (inferior_ptid
== thr
->ptid
)
1436 switch_to_thread_no_regs (thr
);
1438 reinit_frame_cache ();
1440 /* We don't check for is_stopped, because we're called at times
1441 while in the TARGET_RUNNING state, e.g., while handling an
1443 if (thr
->state
!= THREAD_EXITED
1445 stop_pc
= regcache_read_pc (get_thread_regcache (thr
->ptid
));
1448 /* See gdbthread.h. */
1451 switch_to_thread (ptid_t ptid
)
1453 if (ptid
== null_ptid
)
1454 switch_to_no_thread ();
1456 switch_to_thread (find_thread_ptid (ptid
));
1460 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1462 struct frame_info
*frame
= NULL
;
1465 /* This means there was no selected frame. */
1466 if (frame_level
== -1)
1468 select_frame (NULL
);
1472 gdb_assert (frame_level
>= 0);
1474 /* Restore by level first, check if the frame id is the same as
1475 expected. If that fails, try restoring by frame id. If that
1476 fails, nothing to do, just warn the user. */
1478 count
= frame_level
;
1479 frame
= find_relative_frame (get_current_frame (), &count
);
1482 /* The frame ids must match - either both valid or both outer_frame_id.
1483 The latter case is not failsafe, but since it's highly unlikely
1484 the search by level finds the wrong frame, it's 99.9(9)% of
1485 the time (for all practical purposes) safe. */
1486 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1488 /* Cool, all is fine. */
1489 select_frame (frame
);
1493 frame
= frame_find_by_id (a_frame_id
);
1496 /* Cool, refound it. */
1497 select_frame (frame
);
1501 /* Nothing else to do, the frame layout really changed. Select the
1502 innermost stack frame. */
1503 select_frame (get_current_frame ());
1505 /* Warn the user. */
1506 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1508 warning (_("Couldn't restore frame #%d in "
1509 "current thread. Bottom (innermost) frame selected:"),
1511 /* For MI, we should probably have a notification about
1512 current frame change. But this error is not very
1513 likely, so don't bother for now. */
1514 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1518 scoped_restore_current_thread::~scoped_restore_current_thread ()
1520 /* If an entry of thread_info was previously selected, it won't be
1521 deleted because we've increased its refcount. The thread represented
1522 by this thread_info entry may have already exited (due to normal exit,
1523 detach, etc), so the thread_info.state is THREAD_EXITED. */
1524 if (m_thread
!= NULL
1525 /* If the previously selected thread belonged to a process that has
1526 in the mean time exited (or killed, detached, etc.), then don't revert
1527 back to it, but instead simply drop back to no thread selected. */
1529 switch_to_thread (m_thread
);
1532 switch_to_no_thread ();
1533 set_current_inferior (m_inf
);
1536 /* The running state of the originally selected thread may have
1537 changed, so we have to recheck it here. */
1538 if (inferior_ptid
!= null_ptid
1540 && is_stopped (inferior_ptid
)
1541 && target_has_registers
1543 && target_has_memory
)
1544 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1546 if (m_thread
!= NULL
)
1547 m_thread
->decref ();
1551 scoped_restore_current_thread::scoped_restore_current_thread ()
1554 m_inf
= current_inferior ();
1556 if (inferior_ptid
!= null_ptid
)
1558 thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1559 struct frame_info
*frame
;
1561 gdb_assert (tp
!= NULL
);
1563 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1565 && target_has_registers
1567 && target_has_memory
)
1569 /* When processing internal events, there might not be a
1570 selected frame. If we naively call get_selected_frame
1571 here, then we can end up reading debuginfo for the
1572 current frame, but we don't generally need the debuginfo
1574 frame
= get_selected_frame_if_set ();
1579 m_selected_frame_id
= get_frame_id (frame
);
1580 m_selected_frame_level
= frame_relative_level (frame
);
1589 /* See gdbthread.h. */
1592 show_thread_that_caused_stop (void)
1594 return highest_thread_num
> 1;
1597 /* See gdbthread.h. */
1600 show_inferior_qualified_tids (void)
1602 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1605 /* See gdbthread.h. */
1608 print_thread_id (struct thread_info
*thr
)
1610 char *s
= get_print_cell ();
1612 if (show_inferior_qualified_tids ())
1613 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1615 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1619 /* If true, tp_array_compar should sort in ascending order, otherwise
1620 in descending order. */
1622 static bool tp_array_compar_ascending
;
1624 /* Sort an array for struct thread_info pointers by thread ID (first
1625 by inferior number, and then by per-inferior thread number). The
1626 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1629 tp_array_compar (const thread_info
*a
, const thread_info
*b
)
1631 if (a
->inf
->num
!= b
->inf
->num
)
1633 if (tp_array_compar_ascending
)
1634 return a
->inf
->num
< b
->inf
->num
;
1636 return a
->inf
->num
> b
->inf
->num
;
1639 if (tp_array_compar_ascending
)
1640 return (a
->per_inf_num
< b
->per_inf_num
);
1642 return (a
->per_inf_num
> b
->per_inf_num
);
1645 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1646 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1647 of two numbers seperated by a hyphen. Examples:
1649 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1650 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1651 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1654 thread_apply_all_command (const char *cmd
, int from_tty
)
1656 tp_array_compar_ascending
= false;
1658 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1660 cmd
= skip_spaces (cmd
);
1661 tp_array_compar_ascending
= true;
1664 if (cmd
== NULL
|| *cmd
== '\000')
1665 error (_("Please specify a command following the thread ID list"));
1667 update_thread_list ();
1669 int tc
= live_threads_count ();
1672 /* Save a copy of the thread list and increment each thread's
1673 refcount while executing the command in the context of each
1674 thread, in case the command is one that wipes threads. E.g.,
1675 detach, kill, disconnect, etc., or even normally continuing
1676 over an inferior or thread exit. */
1677 std::vector
<thread_info
*> thr_list_cpy
;
1678 thr_list_cpy
.reserve (tc
);
1683 ALL_NON_EXITED_THREADS (tp
)
1685 thr_list_cpy
.push_back (tp
);
1688 gdb_assert (thr_list_cpy
.size () == tc
);
1691 /* Increment the refcounts, and restore them back on scope
1693 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1695 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), tp_array_compar
);
1697 scoped_restore_current_thread restore_thread
;
1699 for (thread_info
*thr
: thr_list_cpy
)
1700 if (thread_alive (thr
))
1702 switch_to_thread (thr
->ptid
);
1703 printf_filtered (_("\nThread %s (%s):\n"),
1704 print_thread_id (thr
),
1705 target_pid_to_str (inferior_ptid
));
1707 execute_command (cmd
, from_tty
);
1712 /* Implementation of the "thread apply" command. */
1715 thread_apply_command (const char *tidlist
, int from_tty
)
1717 const char *cmd
= NULL
;
1718 tid_range_parser parser
;
1720 if (tidlist
== NULL
|| *tidlist
== '\000')
1721 error (_("Please specify a thread ID list"));
1723 parser
.init (tidlist
, current_inferior ()->num
);
1724 while (!parser
.finished ())
1726 int inf_num
, thr_start
, thr_end
;
1728 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1730 cmd
= parser
.cur_tok ();
1736 error (_("Please specify a command following the thread ID list"));
1738 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1739 invalid_thread_id_error (cmd
);
1741 scoped_restore_current_thread restore_thread
;
1743 parser
.init (tidlist
, current_inferior ()->num
);
1744 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1746 struct thread_info
*tp
= NULL
;
1747 struct inferior
*inf
;
1748 int inf_num
, thr_num
;
1750 parser
.get_tid (&inf_num
, &thr_num
);
1751 inf
= find_inferior_id (inf_num
);
1753 tp
= find_thread_id (inf
, thr_num
);
1755 if (parser
.in_star_range ())
1759 warning (_("Unknown inferior %d"), inf_num
);
1760 parser
.skip_range ();
1764 /* No use looking for threads past the highest thread number
1765 the inferior ever had. */
1766 if (thr_num
>= inf
->highest_thread_num
)
1767 parser
.skip_range ();
1769 /* Be quiet about unknown threads numbers. */
1776 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1777 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1779 warning (_("Unknown thread %d"), thr_num
);
1783 if (!thread_alive (tp
))
1785 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1789 switch_to_thread (tp
->ptid
);
1791 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1792 target_pid_to_str (inferior_ptid
));
1793 execute_command (cmd
, from_tty
);
1797 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1798 if prefix of arg is `apply'. */
1801 thread_command (const char *tidstr
, int from_tty
)
1805 if (inferior_ptid
== null_ptid
)
1806 error (_("No thread selected"));
1808 if (target_has_stack
)
1810 struct thread_info
*tp
= inferior_thread ();
1812 if (is_exited (inferior_ptid
))
1813 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1814 print_thread_id (tp
),
1815 target_pid_to_str (inferior_ptid
));
1817 printf_filtered (_("[Current thread is %s (%s)]\n"),
1818 print_thread_id (tp
),
1819 target_pid_to_str (inferior_ptid
));
1822 error (_("No stack."));
1826 ptid_t previous_ptid
= inferior_ptid
;
1828 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1830 /* Print if the thread has not changed, otherwise an event will
1832 if (inferior_ptid
== previous_ptid
)
1834 print_selected_thread_frame (current_uiout
,
1835 USER_SELECTED_THREAD
1836 | USER_SELECTED_FRAME
);
1840 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1841 | USER_SELECTED_FRAME
);
1846 /* Implementation of `thread name'. */
1849 thread_name_command (const char *arg
, int from_tty
)
1851 struct thread_info
*info
;
1853 if (inferior_ptid
== null_ptid
)
1854 error (_("No thread selected"));
1856 arg
= skip_spaces (arg
);
1858 info
= inferior_thread ();
1860 info
->name
= arg
? xstrdup (arg
) : NULL
;
1863 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1866 thread_find_command (const char *arg
, int from_tty
)
1868 struct thread_info
*tp
;
1870 unsigned long match
= 0;
1872 if (arg
== NULL
|| *arg
== '\0')
1873 error (_("Command requires an argument."));
1875 tmp
= re_comp (arg
);
1877 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1879 update_thread_list ();
1880 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1882 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1884 printf_filtered (_("Thread %s has name '%s'\n"),
1885 print_thread_id (tp
), tp
->name
);
1889 tmp
= target_thread_name (tp
);
1890 if (tmp
!= NULL
&& re_exec (tmp
))
1892 printf_filtered (_("Thread %s has target name '%s'\n"),
1893 print_thread_id (tp
), tmp
);
1897 tmp
= target_pid_to_str (tp
->ptid
);
1898 if (tmp
!= NULL
&& re_exec (tmp
))
1900 printf_filtered (_("Thread %s has target id '%s'\n"),
1901 print_thread_id (tp
), tmp
);
1905 tmp
= target_extra_thread_info (tp
);
1906 if (tmp
!= NULL
&& re_exec (tmp
))
1908 printf_filtered (_("Thread %s has extra info '%s'\n"),
1909 print_thread_id (tp
), tmp
);
1914 printf_filtered (_("No threads match '%s'\n"), arg
);
1917 /* Print notices when new threads are attached and detached. */
1918 int print_thread_events
= 1;
1920 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1921 struct cmd_list_element
*c
, const char *value
)
1923 fprintf_filtered (file
,
1924 _("Printing of thread events is %s.\n"),
1928 /* See gdbthread.h. */
1931 thread_select (const char *tidstr
, thread_info
*tp
)
1933 if (!thread_alive (tp
))
1934 error (_("Thread ID %s has terminated."), tidstr
);
1936 switch_to_thread (tp
->ptid
);
1938 annotate_thread_changed ();
1940 /* Since the current thread may have changed, see if there is any
1941 exited thread we can now delete. */
1945 /* Print thread and frame switch command response. */
1948 print_selected_thread_frame (struct ui_out
*uiout
,
1949 user_selected_what selection
)
1951 struct thread_info
*tp
= inferior_thread ();
1952 struct inferior
*inf
= current_inferior ();
1954 if (selection
& USER_SELECTED_THREAD
)
1956 if (uiout
->is_mi_like_p ())
1958 uiout
->field_int ("new-thread-id",
1959 inferior_thread ()->global_num
);
1963 uiout
->text ("[Switching to thread ");
1964 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
1966 uiout
->text (target_pid_to_str (inferior_ptid
));
1971 if (tp
->state
== THREAD_RUNNING
)
1973 if (selection
& USER_SELECTED_THREAD
)
1974 uiout
->text ("(running)\n");
1976 else if (selection
& USER_SELECTED_FRAME
)
1978 if (selection
& USER_SELECTED_THREAD
)
1981 if (has_stack_frames ())
1982 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
1987 /* Update the 'threads_executing' global based on the threads we know
1991 update_threads_executing (void)
1993 struct thread_info
*tp
;
1995 threads_executing
= 0;
1996 ALL_NON_EXITED_THREADS (tp
)
2000 threads_executing
= 1;
2007 update_thread_list (void)
2009 target_update_thread_list ();
2010 update_threads_executing ();
2013 /* Return a new value for the selected thread's id. Return a value of
2014 0 if no thread is selected. If GLOBAL is true, return the thread's
2015 global number. Otherwise return the per-inferior number. */
2017 static struct value
*
2018 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2020 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2026 int_val
= tp
->global_num
;
2028 int_val
= tp
->per_inf_num
;
2030 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2033 /* Return a new value for the selected thread's per-inferior thread
2034 number. Return a value of 0 if no thread is selected, or no
2037 static struct value
*
2038 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2039 struct internalvar
*var
,
2042 return thread_num_make_value_helper (gdbarch
, 0);
2045 /* Return a new value for the selected thread's global id. Return a
2046 value of 0 if no thread is selected, or no threads exist. */
2048 static struct value
*
2049 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2052 return thread_num_make_value_helper (gdbarch
, 1);
2055 /* Commands with a prefix of `thread'. */
2056 struct cmd_list_element
*thread_cmd_list
= NULL
;
2058 /* Implementation of `thread' variable. */
2060 static const struct internalvar_funcs thread_funcs
=
2062 thread_id_per_inf_num_make_value
,
2067 /* Implementation of `gthread' variable. */
2069 static const struct internalvar_funcs gthread_funcs
=
2071 global_thread_id_make_value
,
2077 _initialize_thread (void)
2079 static struct cmd_list_element
*thread_apply_list
= NULL
;
2081 add_info ("threads", info_threads_command
,
2082 _("Display currently known threads.\n\
2083 Usage: info threads [-gid] [ID]...\n\
2084 -gid: Show global thread IDs.\n\
2085 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2086 Otherwise, all threads are displayed."));
2088 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2089 Use this command to switch between threads.\n\
2090 The new thread ID must be currently known."),
2091 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2093 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2094 _("Apply a command to a list of threads."),
2095 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2097 add_cmd ("all", class_run
, thread_apply_all_command
,
2099 Apply a command to all threads.\n\
2101 Usage: thread apply all [-ascending] <command>\n\
2102 -ascending: Call <command> for all threads in ascending order.\n\
2103 The default is descending order.\
2105 &thread_apply_list
);
2107 add_cmd ("name", class_run
, thread_name_command
,
2108 _("Set the current thread's name.\n\
2109 Usage: thread name [NAME]\n\
2110 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2112 add_cmd ("find", class_run
, thread_find_command
, _("\
2113 Find threads that match a regular expression.\n\
2114 Usage: thread find REGEXP\n\
2115 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2118 add_com_alias ("t", "thread", class_run
, 1);
2120 add_setshow_boolean_cmd ("thread-events", no_class
,
2121 &print_thread_events
, _("\
2122 Set printing of thread events (such as thread start and exit)."), _("\
2123 Show printing of thread events (such as thread start and exit)."), NULL
,
2125 show_print_thread_events
,
2126 &setprintlist
, &showprintlist
);
2128 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2129 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);