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 void thread_apply_all_command (char *, int);
64 static int thread_alive (struct thread_info
*);
65 static void info_threads_command (char *, int);
66 static void thread_apply_command (char *, int);
68 /* RAII type used to increase / decrease the refcount of each thread
69 in a given list of threads. */
71 class scoped_inc_dec_ref
74 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
77 for (thread_info
*thr
: m_thrds
)
81 ~scoped_inc_dec_ref ()
83 for (thread_info
*thr
: m_thrds
)
88 const std::vector
<thread_info
*> &m_thrds
;
93 inferior_thread (void)
95 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
100 /* Delete the breakpoint pointed at by BP_P, if there's one. */
103 delete_thread_breakpoint (struct breakpoint
**bp_p
)
107 delete_breakpoint (*bp_p
);
113 delete_step_resume_breakpoint (struct thread_info
*tp
)
116 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
120 delete_exception_resume_breakpoint (struct thread_info
*tp
)
123 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
126 /* See gdbthread.h. */
129 delete_single_step_breakpoints (struct thread_info
*tp
)
132 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
135 /* Delete the breakpoint pointed at by BP_P at the next stop, if
139 delete_at_next_stop (struct breakpoint
**bp
)
143 (*bp
)->disposition
= disp_del_at_next_stop
;
148 /* See gdbthread.h. */
151 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
153 return tp
->control
.single_step_breakpoints
!= NULL
;
156 /* See gdbthread.h. */
159 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
160 struct address_space
*aspace
,
163 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
165 return (ss_bps
!= NULL
166 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
169 /* See gdbthread.h. */
172 thread_cancel_execution_command (struct thread_info
*thr
)
174 if (thr
->thread_fsm
!= NULL
)
176 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
177 thread_fsm_delete (thr
->thread_fsm
);
178 thr
->thread_fsm
= NULL
;
183 clear_thread_inferior_resources (struct thread_info
*tp
)
185 /* NOTE: this will take care of any left-over step_resume breakpoints,
186 but not any user-specified thread-specific breakpoints. We can not
187 delete the breakpoint straight-off, because the inferior might not
188 be stopped at the moment. */
189 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
190 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
191 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
193 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
195 bpstat_clear (&tp
->control
.stop_bpstat
);
197 btrace_teardown (tp
);
199 thread_cancel_execution_command (tp
);
202 /* Set the TP's state as exited. */
205 set_thread_exited (thread_info
*tp
, int silent
)
207 /* Dead threads don't need to step-over. Remove from queue. */
208 if (tp
->step_over_next
!= NULL
)
209 thread_step_over_chain_remove (tp
);
211 if (tp
->state
!= THREAD_EXITED
)
213 observer_notify_thread_exit (tp
, silent
);
215 /* Tag it as exited. */
216 tp
->state
= THREAD_EXITED
;
218 /* Clear breakpoints, etc. associated with this thread. */
219 clear_thread_inferior_resources (tp
);
224 init_thread_list (void)
226 struct thread_info
*tp
, *tpnext
;
228 highest_thread_num
= 0;
233 for (tp
= thread_list
; tp
; tp
= tpnext
)
236 if (tp
->deletable ())
239 set_thread_exited (tp
, 1);
243 threads_executing
= 0;
246 /* Allocate a new thread of inferior INF with target id PTID and add
247 it to the thread list. */
249 static struct thread_info
*
250 new_thread (struct inferior
*inf
, ptid_t ptid
)
252 thread_info
*tp
= new thread_info (inf
, ptid
);
254 if (thread_list
== NULL
)
258 struct thread_info
*last
;
260 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
269 add_thread_silent (ptid_t ptid
)
271 struct thread_info
*tp
;
272 struct inferior
*inf
= find_inferior_ptid (ptid
);
273 gdb_assert (inf
!= NULL
);
275 tp
= find_thread_ptid (ptid
);
277 /* Found an old thread with the same id. It has to be dead,
278 otherwise we wouldn't be adding a new thread with the same id.
279 The OS is reusing this id --- delete it, and recreate a new
282 /* In addition to deleting the thread, if this is the current
283 thread, then we need to take care that delete_thread doesn't
284 really delete the thread if it is inferior_ptid. Create a
285 new template thread in the list with an invalid ptid, switch
286 to it, delete the original thread, reset the new thread's
287 ptid, and switch to it. */
289 if (inferior_ptid
== ptid
)
291 tp
= new_thread (inf
, null_ptid
);
293 /* Make switch_to_thread not read from the thread. */
294 tp
->state
= THREAD_EXITED
;
295 switch_to_thread (null_ptid
);
297 /* Now we can delete it. */
298 delete_thread (ptid
);
300 /* Now reset its ptid, and reswitch inferior_ptid to it. */
302 tp
->state
= THREAD_STOPPED
;
303 switch_to_thread (ptid
);
305 observer_notify_new_thread (tp
);
311 /* Just go ahead and delete it. */
312 delete_thread (ptid
);
315 tp
= new_thread (inf
, ptid
);
316 observer_notify_new_thread (tp
);
322 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
324 struct thread_info
*result
= add_thread_silent (ptid
);
328 if (print_thread_events
)
329 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
331 annotate_new_thread ();
336 add_thread (ptid_t ptid
)
338 return add_thread_with_info (ptid
, NULL
);
341 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
342 : ptid (ptid_
), inf (inf_
)
344 gdb_assert (inf_
!= NULL
);
346 this->global_num
= ++highest_thread_num
;
347 this->per_inf_num
= ++inf_
->highest_thread_num
;
349 /* Nothing to follow yet. */
350 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
351 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
352 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
355 thread_info::~thread_info ()
359 if (this->private_dtor
)
360 this->private_dtor (this->priv
);
368 /* Add TP to the end of the step-over chain LIST_P. */
371 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
373 gdb_assert (tp
->step_over_next
== NULL
);
374 gdb_assert (tp
->step_over_prev
== NULL
);
379 tp
->step_over_prev
= tp
->step_over_next
= tp
;
383 struct thread_info
*head
= *list_p
;
384 struct thread_info
*tail
= head
->step_over_prev
;
386 tp
->step_over_prev
= tail
;
387 tp
->step_over_next
= head
;
388 head
->step_over_prev
= tp
;
389 tail
->step_over_next
= tp
;
393 /* Remove TP from step-over chain LIST_P. */
396 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
398 gdb_assert (tp
->step_over_next
!= NULL
);
399 gdb_assert (tp
->step_over_prev
!= NULL
);
403 if (tp
== tp
->step_over_next
)
406 *list_p
= tp
->step_over_next
;
409 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
410 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
411 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
414 /* See gdbthread.h. */
417 thread_step_over_chain_next (struct thread_info
*tp
)
419 struct thread_info
*next
= tp
->step_over_next
;
421 return (next
== step_over_queue_head
? NULL
: next
);
424 /* See gdbthread.h. */
427 thread_is_in_step_over_chain (struct thread_info
*tp
)
429 return (tp
->step_over_next
!= NULL
);
432 /* See gdbthread.h. */
435 thread_step_over_chain_enqueue (struct thread_info
*tp
)
437 step_over_chain_enqueue (&step_over_queue_head
, tp
);
440 /* See gdbthread.h. */
443 thread_step_over_chain_remove (struct thread_info
*tp
)
445 step_over_chain_remove (&step_over_queue_head
, tp
);
448 /* Delete thread PTID. If SILENT, don't notify the observer of this
451 delete_thread_1 (ptid_t ptid
, int silent
)
453 struct thread_info
*tp
, *tpprev
;
457 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
458 if (tp
->ptid
== ptid
)
464 set_thread_exited (tp
, silent
);
466 if (!tp
->deletable ())
468 /* Will be really deleted some other time. */
473 tpprev
->next
= tp
->next
;
475 thread_list
= tp
->next
;
480 /* Delete thread PTID and notify of thread exit. If this is
481 inferior_ptid, don't actually delete it, but tag it as exited and
482 do the notification. If PTID is the user selected thread, clear
485 delete_thread (ptid_t ptid
)
487 delete_thread_1 (ptid
, 0 /* not silent */);
491 delete_thread_silent (ptid_t ptid
)
493 delete_thread_1 (ptid
, 1 /* silent */);
497 find_thread_global_id (int global_id
)
499 struct thread_info
*tp
;
501 for (tp
= thread_list
; tp
; tp
= tp
->next
)
502 if (tp
->global_num
== global_id
)
508 static struct thread_info
*
509 find_thread_id (struct inferior
*inf
, int thr_num
)
511 struct thread_info
*tp
;
513 for (tp
= thread_list
; tp
; tp
= tp
->next
)
514 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
520 /* Find a thread_info by matching PTID. */
523 find_thread_ptid (ptid_t ptid
)
525 struct thread_info
*tp
;
527 for (tp
= thread_list
; tp
; tp
= tp
->next
)
528 if (tp
->ptid
== ptid
)
534 /* See gdbthread.h. */
537 find_thread_by_handle (struct value
*thread_handle
, struct inferior
*inf
)
539 return target_thread_handle_to_thread_info
540 (value_contents_all (thread_handle
),
541 TYPE_LENGTH (value_type (thread_handle
)),
546 * Thread iterator function.
548 * Calls a callback function once for each thread, so long as
549 * the callback function returns false. If the callback function
550 * returns true, the iteration will end and the current thread
551 * will be returned. This can be useful for implementing a
552 * search for a thread with arbitrary attributes, or for applying
553 * some operation to every thread.
555 * FIXME: some of the existing functionality, such as
556 * "Thread apply all", might be rewritten using this functionality.
560 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
563 struct thread_info
*tp
, *next
;
565 for (tp
= thread_list
; tp
; tp
= next
)
568 if ((*callback
) (tp
, data
))
579 struct thread_info
*tp
;
581 for (tp
= thread_list
; tp
; tp
= tp
->next
)
587 /* Return the number of non-exited threads in the thread list. */
590 live_threads_count (void)
593 struct thread_info
*tp
;
595 ALL_NON_EXITED_THREADS (tp
)
602 valid_global_thread_id (int global_id
)
604 struct thread_info
*tp
;
606 for (tp
= thread_list
; tp
; tp
= tp
->next
)
607 if (tp
->global_num
== global_id
)
614 ptid_to_global_thread_id (ptid_t ptid
)
616 struct thread_info
*tp
;
618 for (tp
= thread_list
; tp
; tp
= tp
->next
)
619 if (tp
->ptid
== ptid
)
620 return tp
->global_num
;
626 global_thread_id_to_ptid (int global_id
)
628 struct thread_info
*thread
= find_thread_global_id (global_id
);
633 return minus_one_ptid
;
637 in_thread_list (ptid_t ptid
)
639 struct thread_info
*tp
;
641 for (tp
= thread_list
; tp
; tp
= tp
->next
)
642 if (tp
->ptid
== ptid
)
645 return 0; /* Never heard of 'im. */
648 /* Finds the first thread of the inferior given by PID. If PID is -1,
649 return the first thread in the list. */
652 first_thread_of_process (int pid
)
654 struct thread_info
*tp
, *ret
= NULL
;
656 for (tp
= thread_list
; tp
; tp
= tp
->next
)
657 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
658 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
665 any_thread_of_process (int pid
)
667 struct thread_info
*tp
;
669 gdb_assert (pid
!= 0);
671 /* Prefer the current thread. */
672 if (ptid_get_pid (inferior_ptid
) == pid
)
673 return inferior_thread ();
675 ALL_NON_EXITED_THREADS (tp
)
676 if (ptid_get_pid (tp
->ptid
) == pid
)
683 any_live_thread_of_process (int pid
)
685 struct thread_info
*curr_tp
= NULL
;
686 struct thread_info
*tp
;
687 struct thread_info
*tp_executing
= NULL
;
689 gdb_assert (pid
!= 0);
691 /* Prefer the current thread if it's not executing. */
692 if (ptid_get_pid (inferior_ptid
) == pid
)
694 /* If the current thread is dead, forget it. If it's not
695 executing, use it. Otherwise, still choose it (below), but
696 only if no other non-executing thread is found. */
697 curr_tp
= inferior_thread ();
698 if (curr_tp
->state
== THREAD_EXITED
)
700 else if (!curr_tp
->executing
)
704 ALL_NON_EXITED_THREADS (tp
)
705 if (ptid_get_pid (tp
->ptid
) == pid
)
713 /* If both the current thread and all live threads are executing,
714 prefer the current thread. */
718 /* Otherwise, just return an executing thread, if any. */
722 /* Return true if TP is an active thread. */
724 thread_alive (struct thread_info
*tp
)
726 if (tp
->state
== THREAD_EXITED
)
728 if (!target_thread_alive (tp
->ptid
))
733 /* See gdbthreads.h. */
738 struct thread_info
*tp
, *tmp
;
740 ALL_THREADS_SAFE (tp
, tmp
)
742 if (!thread_alive (tp
))
743 delete_thread (tp
->ptid
);
747 /* See gdbthreads.h. */
750 delete_exited_threads (void)
752 struct thread_info
*tp
, *tmp
;
754 ALL_THREADS_SAFE (tp
, tmp
)
756 if (tp
->state
== THREAD_EXITED
)
757 delete_thread (tp
->ptid
);
761 /* Disable storing stack temporaries for the thread whose id is
765 disable_thread_stack_temporaries (void *data
)
767 ptid_t
*pd
= (ptid_t
*) data
;
768 struct thread_info
*tp
= find_thread_ptid (*pd
);
772 tp
->stack_temporaries_enabled
= 0;
773 VEC_free (value_ptr
, tp
->stack_temporaries
);
779 /* Enable storing stack temporaries for thread with id PTID and return a
780 cleanup which can disable and clear the stack temporaries. */
783 enable_thread_stack_temporaries (ptid_t ptid
)
785 struct thread_info
*tp
= find_thread_ptid (ptid
);
789 gdb_assert (tp
!= NULL
);
791 tp
->stack_temporaries_enabled
= 1;
792 tp
->stack_temporaries
= NULL
;
793 data
= XNEW (ptid_t
);
795 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
800 /* Return non-zero value if stack temporaies are enabled for the thread
804 thread_stack_temporaries_enabled_p (ptid_t ptid
)
806 struct thread_info
*tp
= find_thread_ptid (ptid
);
811 return tp
->stack_temporaries_enabled
;
814 /* Push V on to the stack temporaries of the thread with id PTID. */
817 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
819 struct thread_info
*tp
= find_thread_ptid (ptid
);
821 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
822 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
825 /* Return 1 if VAL is among the stack temporaries of the thread
826 with id PTID. Return 0 otherwise. */
829 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
831 struct thread_info
*tp
= find_thread_ptid (ptid
);
833 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
834 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
839 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
847 /* Return the last of the stack temporaries for thread with id PTID.
848 Return NULL if there are no stack temporaries for the thread. */
851 get_last_thread_stack_temporary (ptid_t ptid
)
853 struct value
*lastval
= NULL
;
854 struct thread_info
*tp
= find_thread_ptid (ptid
);
856 gdb_assert (tp
!= NULL
);
857 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
858 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
864 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
866 struct inferior
*inf
;
867 struct thread_info
*tp
;
869 /* It can happen that what we knew as the target inferior id
870 changes. E.g, target remote may only discover the remote process
871 pid after adding the inferior to GDB's list. */
872 inf
= find_inferior_ptid (old_ptid
);
873 inf
->pid
= ptid_get_pid (new_ptid
);
875 tp
= find_thread_ptid (old_ptid
);
878 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
881 /* See gdbthread.h. */
884 set_resumed (ptid_t ptid
, int resumed
)
886 struct thread_info
*tp
;
887 int all
= ptid
== minus_one_ptid
;
889 if (all
|| ptid_is_pid (ptid
))
891 for (tp
= thread_list
; tp
; tp
= tp
->next
)
892 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
893 tp
->resumed
= resumed
;
897 tp
= find_thread_ptid (ptid
);
898 gdb_assert (tp
!= NULL
);
899 tp
->resumed
= resumed
;
903 /* Helper for set_running, that marks one thread either running or
907 set_running_thread (struct thread_info
*tp
, int running
)
911 if (running
&& tp
->state
== THREAD_STOPPED
)
913 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
917 /* If the thread is now marked stopped, remove it from
918 the step-over queue, so that we don't try to resume
919 it until the user wants it to. */
920 if (tp
->step_over_next
!= NULL
)
921 thread_step_over_chain_remove (tp
);
928 set_running (ptid_t ptid
, int running
)
930 struct thread_info
*tp
;
931 int all
= ptid
== minus_one_ptid
;
934 /* We try not to notify the observer if no thread has actually changed
935 the running state -- merely to reduce the number of messages to
936 frontend. Frontend is supposed to handle multiple *running just fine. */
937 if (all
|| ptid_is_pid (ptid
))
939 for (tp
= thread_list
; tp
; tp
= tp
->next
)
940 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
942 if (tp
->state
== THREAD_EXITED
)
945 if (set_running_thread (tp
, running
))
951 tp
= find_thread_ptid (ptid
);
952 gdb_assert (tp
!= NULL
);
953 gdb_assert (tp
->state
!= THREAD_EXITED
);
954 if (set_running_thread (tp
, running
))
958 observer_notify_target_resumed (ptid
);
962 is_thread_state (ptid_t ptid
, enum thread_state state
)
964 struct thread_info
*tp
;
966 tp
= find_thread_ptid (ptid
);
968 return tp
->state
== state
;
972 is_stopped (ptid_t ptid
)
974 return is_thread_state (ptid
, THREAD_STOPPED
);
978 is_exited (ptid_t ptid
)
980 return is_thread_state (ptid
, THREAD_EXITED
);
984 is_running (ptid_t ptid
)
986 return is_thread_state (ptid
, THREAD_RUNNING
);
990 is_executing (ptid_t ptid
)
992 struct thread_info
*tp
;
994 tp
= find_thread_ptid (ptid
);
996 return tp
->executing
;
1000 set_executing (ptid_t ptid
, int executing
)
1002 struct thread_info
*tp
;
1003 int all
= ptid
== minus_one_ptid
;
1005 if (all
|| ptid_is_pid (ptid
))
1007 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1008 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1009 tp
->executing
= executing
;
1013 tp
= find_thread_ptid (ptid
);
1015 tp
->executing
= executing
;
1018 /* It only takes one running thread to spawn more threads.*/
1020 threads_executing
= 1;
1021 /* Only clear the flag if the caller is telling us everything is
1023 else if (minus_one_ptid
== ptid
)
1024 threads_executing
= 0;
1027 /* See gdbthread.h. */
1030 threads_are_executing (void)
1032 return threads_executing
;
1036 set_stop_requested (ptid_t ptid
, int stop
)
1038 struct thread_info
*tp
;
1039 int all
= ptid
== minus_one_ptid
;
1041 if (all
|| ptid_is_pid (ptid
))
1043 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1044 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1045 tp
->stop_requested
= stop
;
1049 tp
= find_thread_ptid (ptid
);
1051 tp
->stop_requested
= stop
;
1054 /* Call the stop requested observer so other components of GDB can
1055 react to this request. */
1057 observer_notify_thread_stop_requested (ptid
);
1061 finish_thread_state (ptid_t ptid
)
1063 struct thread_info
*tp
;
1065 int any_started
= 0;
1067 all
= ptid
== minus_one_ptid
;
1069 if (all
|| ptid_is_pid (ptid
))
1071 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1073 if (tp
->state
== THREAD_EXITED
)
1075 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1077 if (set_running_thread (tp
, tp
->executing
))
1084 tp
= find_thread_ptid (ptid
);
1086 if (tp
->state
!= THREAD_EXITED
)
1088 if (set_running_thread (tp
, tp
->executing
))
1094 observer_notify_target_resumed (ptid
);
1098 finish_thread_state_cleanup (void *arg
)
1100 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1104 finish_thread_state (*ptid_p
);
1107 /* See gdbthread.h. */
1110 validate_registers_access (void)
1112 /* No selected thread, no registers. */
1113 if (inferior_ptid
== null_ptid
)
1114 error (_("No thread selected."));
1116 /* Don't try to read from a dead thread. */
1117 if (is_exited (inferior_ptid
))
1118 error (_("The current thread has terminated"));
1120 /* ... or from a spinning thread. FIXME: This isn't actually fully
1121 correct. It'll allow an user-requested access (e.g., "print $pc"
1122 at the prompt) when a thread is not executing for some internal
1123 reason, but is marked running from the user's perspective. E.g.,
1124 the thread is waiting for its turn in the step-over queue. */
1125 if (is_executing (inferior_ptid
))
1126 error (_("Selected thread is running."));
1129 /* See gdbthread.h. */
1132 can_access_registers_ptid (ptid_t ptid
)
1134 /* No thread, no registers. */
1135 if (ptid
== null_ptid
)
1138 /* Don't try to read from a dead thread. */
1139 if (is_exited (ptid
))
1142 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1143 if (is_executing (ptid
))
1150 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1152 return (pc
>= thread
->control
.step_range_start
1153 && pc
< thread
->control
.step_range_end
);
1156 /* Helper for print_thread_info. Returns true if THR should be
1157 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1158 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1159 is true if REQUESTED_THREADS is list of global IDs, false if a list
1160 of per-inferior thread ids. If PID is not -1, only print THR if it
1161 is a thread from the process PID. Otherwise, threads from all
1162 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1163 and PID is not -1, then the thread is printed if it belongs to the
1164 specified process. Otherwise, an error is raised. */
1167 should_print_thread (const char *requested_threads
, int default_inf_num
,
1168 int global_ids
, int pid
, struct thread_info
*thr
)
1170 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1175 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1177 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1178 thr
->inf
->num
, thr
->per_inf_num
);
1183 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1185 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1186 error (_("Requested thread not found in requested process"));
1190 if (thr
->state
== THREAD_EXITED
)
1196 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1197 whether REQUESTED_THREADS is a list of global or per-inferior
1201 print_thread_info_1 (struct ui_out
*uiout
, char *requested_threads
,
1202 int global_ids
, int pid
,
1203 int show_global_ids
)
1205 struct thread_info
*tp
;
1206 ptid_t current_ptid
;
1207 const char *extra_info
, *name
, *target_id
;
1208 struct inferior
*inf
;
1209 int default_inf_num
= current_inferior ()->num
;
1211 update_thread_list ();
1212 current_ptid
= inferior_ptid
;
1215 /* For backward compatibility, we make a list for MI. A table is
1216 preferable for the CLI, though, because it shows table
1218 gdb::optional
<ui_out_emit_list
> list_emitter
;
1219 gdb::optional
<ui_out_emit_table
> table_emitter
;
1221 if (uiout
->is_mi_like_p ())
1222 list_emitter
.emplace (uiout
, "threads");
1227 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1229 if (!should_print_thread (requested_threads
, default_inf_num
,
1230 global_ids
, pid
, tp
))
1238 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1239 uiout
->message (_("No threads.\n"));
1241 uiout
->message (_("No threads match '%s'.\n"),
1246 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1247 n_threads
, "threads");
1249 uiout
->table_header (1, ui_left
, "current", "");
1250 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1251 if (show_global_ids
)
1252 uiout
->table_header (4, ui_left
, "id", "GId");
1253 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1254 uiout
->table_header (1, ui_left
, "frame", "Frame");
1255 uiout
->table_body ();
1258 /* We'll be switching threads temporarily. */
1259 scoped_restore_current_thread restore_thread
;
1261 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1265 if (!should_print_thread (requested_threads
, default_inf_num
,
1266 global_ids
, pid
, tp
))
1269 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1271 if (!uiout
->is_mi_like_p ())
1273 if (tp
->ptid
== current_ptid
)
1274 uiout
->field_string ("current", "*");
1276 uiout
->field_skip ("current");
1278 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1281 if (show_global_ids
|| uiout
->is_mi_like_p ())
1282 uiout
->field_int ("id", tp
->global_num
);
1284 /* For the CLI, we stuff everything into the target-id field.
1285 This is a gross hack to make the output come out looking
1286 correct. The underlying problem here is that ui-out has no
1287 way to specify that a field's space allocation should be
1288 shared by several fields. For MI, we do the right thing
1291 target_id
= target_pid_to_str (tp
->ptid
);
1292 extra_info
= target_extra_thread_info (tp
);
1293 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1295 if (uiout
->is_mi_like_p ())
1297 uiout
->field_string ("target-id", target_id
);
1299 uiout
->field_string ("details", extra_info
);
1301 uiout
->field_string ("name", name
);
1305 std::string contents
;
1307 if (extra_info
&& name
)
1308 contents
= string_printf ("%s \"%s\" (%s)", target_id
,
1310 else if (extra_info
)
1311 contents
= string_printf ("%s (%s)", target_id
, extra_info
);
1313 contents
= string_printf ("%s \"%s\"", target_id
, name
);
1315 contents
= target_id
;
1317 uiout
->field_string ("target-id", contents
.c_str ());
1320 if (tp
->state
== THREAD_RUNNING
)
1321 uiout
->text ("(running)\n");
1324 /* The switch below puts us at the top of the stack (leaf
1326 switch_to_thread (tp
->ptid
);
1327 print_stack_frame (get_selected_frame (NULL
),
1328 /* For MI output, print frame level. */
1329 uiout
->is_mi_like_p (),
1333 if (uiout
->is_mi_like_p ())
1335 const char *state
= "stopped";
1337 if (tp
->state
== THREAD_RUNNING
)
1339 uiout
->field_string ("state", state
);
1342 core
= target_core_of_thread (tp
->ptid
);
1343 if (uiout
->is_mi_like_p () && core
!= -1)
1344 uiout
->field_int ("core", core
);
1347 /* This end scope restores the current thread and the frame
1348 selected before the "info threads" command, and it finishes the
1349 ui-out list or table. */
1352 if (pid
== -1 && requested_threads
== NULL
)
1354 if (uiout
->is_mi_like_p ()
1355 && inferior_ptid
!= null_ptid
)
1357 int num
= ptid_to_global_thread_id (inferior_ptid
);
1359 gdb_assert (num
!= 0);
1360 uiout
->field_int ("current-thread-id", num
);
1363 if (inferior_ptid
!= null_ptid
&& is_exited (inferior_ptid
))
1364 uiout
->message ("\n\
1365 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1366 print_thread_id (inferior_thread ()));
1367 else if (thread_list
!= NULL
&& inferior_ptid
== null_ptid
)
1368 uiout
->message ("\n\
1369 No selected thread. See `help thread'.\n");
1373 /* See gdbthread.h. */
1376 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1378 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1381 /* Implementation of the "info threads" command.
1383 Note: this has the drawback that it _really_ switches
1384 threads, which frees the frame cache. A no-side
1385 effects info-threads command would be nicer. */
1388 info_threads_command (char *arg
, int from_tty
)
1390 int show_global_ids
= 0;
1393 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1395 arg
= skip_spaces (arg
);
1396 show_global_ids
= 1;
1399 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1402 /* See gdbthread.h. */
1405 switch_to_thread_no_regs (struct thread_info
*thread
)
1407 struct inferior
*inf
= thread
->inf
;
1409 set_current_program_space (inf
->pspace
);
1410 set_current_inferior (inf
);
1412 inferior_ptid
= thread
->ptid
;
1413 stop_pc
= ~(CORE_ADDR
) 0;
1416 /* Switch to no thread selected. */
1419 switch_to_no_thread ()
1421 if (inferior_ptid
== null_ptid
)
1424 inferior_ptid
= null_ptid
;
1425 reinit_frame_cache ();
1426 stop_pc
= ~(CORE_ADDR
) 0;
1429 /* Switch from one thread to another. */
1432 switch_to_thread (thread_info
*thr
)
1434 gdb_assert (thr
!= NULL
);
1436 if (inferior_ptid
== thr
->ptid
)
1439 switch_to_thread_no_regs (thr
);
1441 reinit_frame_cache ();
1443 /* We don't check for is_stopped, because we're called at times
1444 while in the TARGET_RUNNING state, e.g., while handling an
1446 if (thr
->state
!= THREAD_EXITED
1448 stop_pc
= regcache_read_pc (get_thread_regcache (thr
->ptid
));
1451 /* See gdbthread.h. */
1454 switch_to_thread (ptid_t ptid
)
1456 if (ptid
== null_ptid
)
1457 switch_to_no_thread ();
1459 switch_to_thread (find_thread_ptid (ptid
));
1463 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1465 struct frame_info
*frame
= NULL
;
1468 /* This means there was no selected frame. */
1469 if (frame_level
== -1)
1471 select_frame (NULL
);
1475 gdb_assert (frame_level
>= 0);
1477 /* Restore by level first, check if the frame id is the same as
1478 expected. If that fails, try restoring by frame id. If that
1479 fails, nothing to do, just warn the user. */
1481 count
= frame_level
;
1482 frame
= find_relative_frame (get_current_frame (), &count
);
1485 /* The frame ids must match - either both valid or both outer_frame_id.
1486 The latter case is not failsafe, but since it's highly unlikely
1487 the search by level finds the wrong frame, it's 99.9(9)% of
1488 the time (for all practical purposes) safe. */
1489 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1491 /* Cool, all is fine. */
1492 select_frame (frame
);
1496 frame
= frame_find_by_id (a_frame_id
);
1499 /* Cool, refound it. */
1500 select_frame (frame
);
1504 /* Nothing else to do, the frame layout really changed. Select the
1505 innermost stack frame. */
1506 select_frame (get_current_frame ());
1508 /* Warn the user. */
1509 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1511 warning (_("Couldn't restore frame #%d in "
1512 "current thread. Bottom (innermost) frame selected:"),
1514 /* For MI, we should probably have a notification about
1515 current frame change. But this error is not very
1516 likely, so don't bother for now. */
1517 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1521 scoped_restore_current_thread::~scoped_restore_current_thread ()
1523 /* If an entry of thread_info was previously selected, it won't be
1524 deleted because we've increased its refcount. The thread represented
1525 by this thread_info entry may have already exited (due to normal exit,
1526 detach, etc), so the thread_info.state is THREAD_EXITED. */
1527 if (m_thread
!= NULL
1528 /* If the previously selected thread belonged to a process that has
1529 in the mean time exited (or killed, detached, etc.), then don't revert
1530 back to it, but instead simply drop back to no thread selected. */
1532 switch_to_thread (m_thread
);
1535 switch_to_no_thread ();
1536 set_current_inferior (m_inf
);
1539 /* The running state of the originally selected thread may have
1540 changed, so we have to recheck it here. */
1541 if (inferior_ptid
!= null_ptid
1543 && is_stopped (inferior_ptid
)
1544 && target_has_registers
1546 && target_has_memory
)
1547 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1549 if (m_thread
!= NULL
)
1550 m_thread
->decref ();
1554 scoped_restore_current_thread::scoped_restore_current_thread ()
1557 m_inf
= current_inferior ();
1559 if (inferior_ptid
!= null_ptid
)
1561 thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1562 struct frame_info
*frame
;
1564 gdb_assert (tp
!= NULL
);
1566 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1568 && target_has_registers
1570 && target_has_memory
)
1572 /* When processing internal events, there might not be a
1573 selected frame. If we naively call get_selected_frame
1574 here, then we can end up reading debuginfo for the
1575 current frame, but we don't generally need the debuginfo
1577 frame
= get_selected_frame_if_set ();
1582 m_selected_frame_id
= get_frame_id (frame
);
1583 m_selected_frame_level
= frame_relative_level (frame
);
1592 /* See gdbthread.h. */
1595 show_thread_that_caused_stop (void)
1597 return highest_thread_num
> 1;
1600 /* See gdbthread.h. */
1603 show_inferior_qualified_tids (void)
1605 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1608 /* See gdbthread.h. */
1611 print_thread_id (struct thread_info
*thr
)
1613 char *s
= get_print_cell ();
1615 if (show_inferior_qualified_tids ())
1616 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1618 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1622 /* If true, tp_array_compar should sort in ascending order, otherwise
1623 in descending order. */
1625 static bool tp_array_compar_ascending
;
1627 /* Sort an array for struct thread_info pointers by thread ID (first
1628 by inferior number, and then by per-inferior thread number). The
1629 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1632 tp_array_compar (const thread_info
*a
, const thread_info
*b
)
1634 if (a
->inf
->num
!= b
->inf
->num
)
1636 if (tp_array_compar_ascending
)
1637 return a
->inf
->num
< b
->inf
->num
;
1639 return a
->inf
->num
> b
->inf
->num
;
1642 if (tp_array_compar_ascending
)
1643 return (a
->per_inf_num
< b
->per_inf_num
);
1645 return (a
->per_inf_num
> b
->per_inf_num
);
1648 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1649 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1650 of two numbers seperated by a hyphen. Examples:
1652 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1653 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1654 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1657 thread_apply_all_command (char *cmd
, int from_tty
)
1659 tp_array_compar_ascending
= false;
1661 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1663 cmd
= skip_spaces (cmd
);
1664 tp_array_compar_ascending
= true;
1667 if (cmd
== NULL
|| *cmd
== '\000')
1668 error (_("Please specify a command following the thread ID list"));
1670 update_thread_list ();
1672 /* Save a copy of the command in case it is clobbered by
1674 std::string saved_cmd
= cmd
;
1676 int tc
= live_threads_count ();
1679 /* Save a copy of the thread list and increment each thread's
1680 refcount while executing the command in the context of each
1681 thread, in case the command is one that wipes threads. E.g.,
1682 detach, kill, disconnect, etc., or even normally continuing
1683 over an inferior or thread exit. */
1684 std::vector
<thread_info
*> thr_list_cpy
;
1685 thr_list_cpy
.reserve (tc
);
1690 ALL_NON_EXITED_THREADS (tp
)
1692 thr_list_cpy
.push_back (tp
);
1695 gdb_assert (thr_list_cpy
.size () == tc
);
1698 /* Increment the refcounts, and restore them back on scope
1700 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1702 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), tp_array_compar
);
1704 scoped_restore_current_thread restore_thread
;
1706 for (thread_info
*thr
: thr_list_cpy
)
1707 if (thread_alive (thr
))
1709 switch_to_thread (thr
->ptid
);
1710 printf_filtered (_("\nThread %s (%s):\n"),
1711 print_thread_id (thr
),
1712 target_pid_to_str (inferior_ptid
));
1713 execute_command (cmd
, from_tty
);
1715 /* Restore exact command used previously. */
1716 strcpy (cmd
, saved_cmd
.c_str ());
1721 /* Implementation of the "thread apply" command. */
1724 thread_apply_command (char *tidlist
, int from_tty
)
1727 tid_range_parser parser
;
1729 if (tidlist
== NULL
|| *tidlist
== '\000')
1730 error (_("Please specify a thread ID list"));
1732 parser
.init (tidlist
, current_inferior ()->num
);
1733 while (!parser
.finished ())
1735 int inf_num
, thr_start
, thr_end
;
1737 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1739 cmd
= (char *) parser
.cur_tok ();
1745 error (_("Please specify a command following the thread ID list"));
1747 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1748 invalid_thread_id_error (cmd
);
1750 /* Save a copy of the command in case it is clobbered by
1752 std::string saved_cmd
= cmd
;
1754 scoped_restore_current_thread restore_thread
;
1756 parser
.init (tidlist
, current_inferior ()->num
);
1757 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1759 struct thread_info
*tp
= NULL
;
1760 struct inferior
*inf
;
1761 int inf_num
, thr_num
;
1763 parser
.get_tid (&inf_num
, &thr_num
);
1764 inf
= find_inferior_id (inf_num
);
1766 tp
= find_thread_id (inf
, thr_num
);
1768 if (parser
.in_star_range ())
1772 warning (_("Unknown inferior %d"), inf_num
);
1773 parser
.skip_range ();
1777 /* No use looking for threads past the highest thread number
1778 the inferior ever had. */
1779 if (thr_num
>= inf
->highest_thread_num
)
1780 parser
.skip_range ();
1782 /* Be quiet about unknown threads numbers. */
1789 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1790 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1792 warning (_("Unknown thread %d"), thr_num
);
1796 if (!thread_alive (tp
))
1798 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1802 switch_to_thread (tp
->ptid
);
1804 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1805 target_pid_to_str (inferior_ptid
));
1806 execute_command (cmd
, from_tty
);
1808 /* Restore exact command used previously. */
1809 strcpy (cmd
, saved_cmd
.c_str ());
1813 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1814 if prefix of arg is `apply'. */
1817 thread_command (char *tidstr
, int from_tty
)
1821 if (inferior_ptid
== null_ptid
)
1822 error (_("No thread selected"));
1824 if (target_has_stack
)
1826 struct thread_info
*tp
= inferior_thread ();
1828 if (is_exited (inferior_ptid
))
1829 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1830 print_thread_id (tp
),
1831 target_pid_to_str (inferior_ptid
));
1833 printf_filtered (_("[Current thread is %s (%s)]\n"),
1834 print_thread_id (tp
),
1835 target_pid_to_str (inferior_ptid
));
1838 error (_("No stack."));
1842 ptid_t previous_ptid
= inferior_ptid
;
1844 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1846 /* Print if the thread has not changed, otherwise an event will
1848 if (inferior_ptid
== previous_ptid
)
1850 print_selected_thread_frame (current_uiout
,
1851 USER_SELECTED_THREAD
1852 | USER_SELECTED_FRAME
);
1856 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1857 | USER_SELECTED_FRAME
);
1862 /* Implementation of `thread name'. */
1865 thread_name_command (const char *arg
, int from_tty
)
1867 struct thread_info
*info
;
1869 if (inferior_ptid
== null_ptid
)
1870 error (_("No thread selected"));
1872 arg
= skip_spaces (arg
);
1874 info
= inferior_thread ();
1876 info
->name
= arg
? xstrdup (arg
) : NULL
;
1879 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1882 thread_find_command (const char *arg
, int from_tty
)
1884 struct thread_info
*tp
;
1886 unsigned long match
= 0;
1888 if (arg
== NULL
|| *arg
== '\0')
1889 error (_("Command requires an argument."));
1891 tmp
= re_comp (arg
);
1893 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1895 update_thread_list ();
1896 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1898 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1900 printf_filtered (_("Thread %s has name '%s'\n"),
1901 print_thread_id (tp
), tp
->name
);
1905 tmp
= target_thread_name (tp
);
1906 if (tmp
!= NULL
&& re_exec (tmp
))
1908 printf_filtered (_("Thread %s has target name '%s'\n"),
1909 print_thread_id (tp
), tmp
);
1913 tmp
= target_pid_to_str (tp
->ptid
);
1914 if (tmp
!= NULL
&& re_exec (tmp
))
1916 printf_filtered (_("Thread %s has target id '%s'\n"),
1917 print_thread_id (tp
), tmp
);
1921 tmp
= target_extra_thread_info (tp
);
1922 if (tmp
!= NULL
&& re_exec (tmp
))
1924 printf_filtered (_("Thread %s has extra info '%s'\n"),
1925 print_thread_id (tp
), tmp
);
1930 printf_filtered (_("No threads match '%s'\n"), arg
);
1933 /* Print notices when new threads are attached and detached. */
1934 int print_thread_events
= 1;
1936 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1937 struct cmd_list_element
*c
, const char *value
)
1939 fprintf_filtered (file
,
1940 _("Printing of thread events is %s.\n"),
1944 /* See gdbthread.h. */
1947 thread_select (const char *tidstr
, thread_info
*tp
)
1949 if (!thread_alive (tp
))
1950 error (_("Thread ID %s has terminated."), tidstr
);
1952 switch_to_thread (tp
->ptid
);
1954 annotate_thread_changed ();
1956 /* Since the current thread may have changed, see if there is any
1957 exited thread we can now delete. */
1961 /* Print thread and frame switch command response. */
1964 print_selected_thread_frame (struct ui_out
*uiout
,
1965 user_selected_what selection
)
1967 struct thread_info
*tp
= inferior_thread ();
1968 struct inferior
*inf
= current_inferior ();
1970 if (selection
& USER_SELECTED_THREAD
)
1972 if (uiout
->is_mi_like_p ())
1974 uiout
->field_int ("new-thread-id",
1975 inferior_thread ()->global_num
);
1979 uiout
->text ("[Switching to thread ");
1980 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
1982 uiout
->text (target_pid_to_str (inferior_ptid
));
1987 if (tp
->state
== THREAD_RUNNING
)
1989 if (selection
& USER_SELECTED_THREAD
)
1990 uiout
->text ("(running)\n");
1992 else if (selection
& USER_SELECTED_FRAME
)
1994 if (selection
& USER_SELECTED_THREAD
)
1997 if (has_stack_frames ())
1998 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2003 /* Update the 'threads_executing' global based on the threads we know
2007 update_threads_executing (void)
2009 struct thread_info
*tp
;
2011 threads_executing
= 0;
2012 ALL_NON_EXITED_THREADS (tp
)
2016 threads_executing
= 1;
2023 update_thread_list (void)
2025 target_update_thread_list ();
2026 update_threads_executing ();
2029 /* Return a new value for the selected thread's id. Return a value of
2030 0 if no thread is selected. If GLOBAL is true, return the thread's
2031 global number. Otherwise return the per-inferior number. */
2033 static struct value
*
2034 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2036 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2042 int_val
= tp
->global_num
;
2044 int_val
= tp
->per_inf_num
;
2046 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2049 /* Return a new value for the selected thread's per-inferior thread
2050 number. Return a value of 0 if no thread is selected, or no
2053 static struct value
*
2054 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2055 struct internalvar
*var
,
2058 return thread_num_make_value_helper (gdbarch
, 0);
2061 /* Return a new value for the selected thread's global id. Return a
2062 value of 0 if no thread is selected, or no threads exist. */
2064 static struct value
*
2065 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2068 return thread_num_make_value_helper (gdbarch
, 1);
2071 /* Commands with a prefix of `thread'. */
2072 struct cmd_list_element
*thread_cmd_list
= NULL
;
2074 /* Implementation of `thread' variable. */
2076 static const struct internalvar_funcs thread_funcs
=
2078 thread_id_per_inf_num_make_value
,
2083 /* Implementation of `gthread' variable. */
2085 static const struct internalvar_funcs gthread_funcs
=
2087 global_thread_id_make_value
,
2093 _initialize_thread (void)
2095 static struct cmd_list_element
*thread_apply_list
= NULL
;
2097 add_info ("threads", info_threads_command
,
2098 _("Display currently known threads.\n\
2099 Usage: info threads [-gid] [ID]...\n\
2100 -gid: Show global thread IDs.\n\
2101 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2102 Otherwise, all threads are displayed."));
2104 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2105 Use this command to switch between threads.\n\
2106 The new thread ID must be currently known."),
2107 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2109 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2110 _("Apply a command to a list of threads."),
2111 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2113 add_cmd ("all", class_run
, thread_apply_all_command
,
2115 Apply a command to all threads.\n\
2117 Usage: thread apply all [-ascending] <command>\n\
2118 -ascending: Call <command> for all threads in ascending order.\n\
2119 The default is descending order.\
2121 &thread_apply_list
);
2123 add_cmd ("name", class_run
, thread_name_command
,
2124 _("Set the current thread's name.\n\
2125 Usage: thread name [NAME]\n\
2126 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2128 add_cmd ("find", class_run
, thread_find_command
, _("\
2129 Find threads that match a regular expression.\n\
2130 Usage: thread find REGEXP\n\
2131 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2134 add_com_alias ("t", "thread", class_run
, 1);
2136 add_setshow_boolean_cmd ("thread-events", no_class
,
2137 &print_thread_events
, _("\
2138 Set printing of thread events (such as thread start and exit)."), _("\
2139 Show printing of thread events (such as thread start and exit)."), NULL
,
2141 show_print_thread_events
,
2142 &setprintlist
, &showprintlist
);
2144 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2145 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);