1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdbthread.h"
37 #include <sys/types.h>
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46 #include "tid-parse.h"
48 #include "common/gdb_optional.h"
50 /* Definition of struct thread_info exported to gdbthread.h. */
52 /* Prototypes for local functions. */
54 struct thread_info
*thread_list
= NULL
;
55 static int highest_thread_num
;
57 /* True if any thread is, or may be executing. We need to track this
58 separately because until we fully sync the thread list, we won't
59 know whether the target is fully stopped, even if we see stop
60 events for all known threads, because any of those threads may have
61 spawned new threads we haven't heard of yet. */
62 static int threads_executing
;
64 static void thread_apply_all_command (char *, int);
65 static int thread_alive (struct thread_info
*);
66 static void info_threads_command (char *, int);
67 static void thread_apply_command (char *, int);
69 /* RAII type used to increase / decrease the refcount of each thread
70 in a given list of threads. */
72 class scoped_inc_dec_ref
75 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
78 for (thread_info
*thr
: m_thrds
)
82 ~scoped_inc_dec_ref ()
84 for (thread_info
*thr
: m_thrds
)
89 const std::vector
<thread_info
*> &m_thrds
;
94 inferior_thread (void)
96 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
101 /* Delete the breakpoint pointed at by BP_P, if there's one. */
104 delete_thread_breakpoint (struct breakpoint
**bp_p
)
108 delete_breakpoint (*bp_p
);
114 delete_step_resume_breakpoint (struct thread_info
*tp
)
117 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
121 delete_exception_resume_breakpoint (struct thread_info
*tp
)
124 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
127 /* See gdbthread.h. */
130 delete_single_step_breakpoints (struct thread_info
*tp
)
133 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
136 /* Delete the breakpoint pointed at by BP_P at the next stop, if
140 delete_at_next_stop (struct breakpoint
**bp
)
144 (*bp
)->disposition
= disp_del_at_next_stop
;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
154 return tp
->control
.single_step_breakpoints
!= NULL
;
157 /* See gdbthread.h. */
160 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
161 struct address_space
*aspace
,
164 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
166 return (ss_bps
!= NULL
167 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
170 /* See gdbthread.h. */
173 thread_cancel_execution_command (struct thread_info
*thr
)
175 if (thr
->thread_fsm
!= NULL
)
177 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
178 thread_fsm_delete (thr
->thread_fsm
);
179 thr
->thread_fsm
= NULL
;
184 clear_thread_inferior_resources (struct thread_info
*tp
)
186 /* NOTE: this will take care of any left-over step_resume breakpoints,
187 but not any user-specified thread-specific breakpoints. We can not
188 delete the breakpoint straight-off, because the inferior might not
189 be stopped at the moment. */
190 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
191 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
192 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
194 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
196 bpstat_clear (&tp
->control
.stop_bpstat
);
198 btrace_teardown (tp
);
200 thread_cancel_execution_command (tp
);
203 /* Set the TP's state as exited. */
206 set_thread_exited (thread_info
*tp
, int silent
)
208 /* Dead threads don't need to step-over. Remove from queue. */
209 if (tp
->step_over_next
!= NULL
)
210 thread_step_over_chain_remove (tp
);
212 if (tp
->state
!= THREAD_EXITED
)
214 observer_notify_thread_exit (tp
, silent
);
216 /* Tag it as exited. */
217 tp
->state
= THREAD_EXITED
;
219 /* Clear breakpoints, etc. associated with this thread. */
220 clear_thread_inferior_resources (tp
);
225 init_thread_list (void)
227 struct thread_info
*tp
, *tpnext
;
229 highest_thread_num
= 0;
234 for (tp
= thread_list
; tp
; tp
= tpnext
)
237 if (tp
->deletable ())
240 set_thread_exited (tp
, 1);
244 threads_executing
= 0;
247 /* Allocate a new thread of inferior INF with target id PTID and add
248 it to the thread list. */
250 static struct thread_info
*
251 new_thread (struct inferior
*inf
, ptid_t ptid
)
253 thread_info
*tp
= new thread_info (inf
, ptid
);
255 if (thread_list
== NULL
)
259 struct thread_info
*last
;
261 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
270 add_thread_silent (ptid_t ptid
)
272 struct thread_info
*tp
;
273 struct inferior
*inf
= find_inferior_ptid (ptid
);
274 gdb_assert (inf
!= NULL
);
276 tp
= find_thread_ptid (ptid
);
278 /* Found an old thread with the same id. It has to be dead,
279 otherwise we wouldn't be adding a new thread with the same id.
280 The OS is reusing this id --- delete it, and recreate a new
283 /* In addition to deleting the thread, if this is the current
284 thread, then we need to take care that delete_thread doesn't
285 really delete the thread if it is inferior_ptid. Create a
286 new template thread in the list with an invalid ptid, switch
287 to it, delete the original thread, reset the new thread's
288 ptid, and switch to it. */
290 if (inferior_ptid
== ptid
)
292 tp
= new_thread (inf
, null_ptid
);
294 /* Make switch_to_thread not read from the thread. */
295 tp
->state
= THREAD_EXITED
;
296 switch_to_thread (null_ptid
);
298 /* Now we can delete it. */
299 delete_thread (ptid
);
301 /* Now reset its ptid, and reswitch inferior_ptid to it. */
303 tp
->state
= THREAD_STOPPED
;
304 switch_to_thread (ptid
);
306 observer_notify_new_thread (tp
);
312 /* Just go ahead and delete it. */
313 delete_thread (ptid
);
316 tp
= new_thread (inf
, ptid
);
317 observer_notify_new_thread (tp
);
323 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
325 struct thread_info
*result
= add_thread_silent (ptid
);
329 if (print_thread_events
)
330 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
332 annotate_new_thread ();
337 add_thread (ptid_t ptid
)
339 return add_thread_with_info (ptid
, NULL
);
342 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
343 : ptid (ptid_
), inf (inf_
)
345 gdb_assert (inf_
!= NULL
);
347 this->global_num
= ++highest_thread_num
;
348 this->per_inf_num
= ++inf_
->highest_thread_num
;
350 /* Nothing to follow yet. */
351 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
352 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
353 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
356 thread_info::~thread_info ()
360 if (this->private_dtor
)
361 this->private_dtor (this->priv
);
369 /* Add TP to the end of the step-over chain LIST_P. */
372 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
374 gdb_assert (tp
->step_over_next
== NULL
);
375 gdb_assert (tp
->step_over_prev
== NULL
);
380 tp
->step_over_prev
= tp
->step_over_next
= tp
;
384 struct thread_info
*head
= *list_p
;
385 struct thread_info
*tail
= head
->step_over_prev
;
387 tp
->step_over_prev
= tail
;
388 tp
->step_over_next
= head
;
389 head
->step_over_prev
= tp
;
390 tail
->step_over_next
= tp
;
394 /* Remove TP from step-over chain LIST_P. */
397 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
399 gdb_assert (tp
->step_over_next
!= NULL
);
400 gdb_assert (tp
->step_over_prev
!= NULL
);
404 if (tp
== tp
->step_over_next
)
407 *list_p
= tp
->step_over_next
;
410 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
411 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
412 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
415 /* See gdbthread.h. */
418 thread_step_over_chain_next (struct thread_info
*tp
)
420 struct thread_info
*next
= tp
->step_over_next
;
422 return (next
== step_over_queue_head
? NULL
: next
);
425 /* See gdbthread.h. */
428 thread_is_in_step_over_chain (struct thread_info
*tp
)
430 return (tp
->step_over_next
!= NULL
);
433 /* See gdbthread.h. */
436 thread_step_over_chain_enqueue (struct thread_info
*tp
)
438 step_over_chain_enqueue (&step_over_queue_head
, tp
);
441 /* See gdbthread.h. */
444 thread_step_over_chain_remove (struct thread_info
*tp
)
446 step_over_chain_remove (&step_over_queue_head
, tp
);
449 /* Delete thread PTID. If SILENT, don't notify the observer of this
452 delete_thread_1 (ptid_t ptid
, int silent
)
454 struct thread_info
*tp
, *tpprev
;
458 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
459 if (tp
->ptid
== ptid
)
465 set_thread_exited (tp
, silent
);
467 if (!tp
->deletable ())
469 /* Will be really deleted some other time. */
474 tpprev
->next
= tp
->next
;
476 thread_list
= tp
->next
;
481 /* Delete thread PTID and notify of thread exit. If this is
482 inferior_ptid, don't actually delete it, but tag it as exited and
483 do the notification. If PTID is the user selected thread, clear
486 delete_thread (ptid_t ptid
)
488 delete_thread_1 (ptid
, 0 /* not silent */);
492 delete_thread_silent (ptid_t ptid
)
494 delete_thread_1 (ptid
, 1 /* silent */);
498 find_thread_global_id (int global_id
)
500 struct thread_info
*tp
;
502 for (tp
= thread_list
; tp
; tp
= tp
->next
)
503 if (tp
->global_num
== global_id
)
509 static struct thread_info
*
510 find_thread_id (struct inferior
*inf
, int thr_num
)
512 struct thread_info
*tp
;
514 for (tp
= thread_list
; tp
; tp
= tp
->next
)
515 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
521 /* Find a thread_info by matching PTID. */
524 find_thread_ptid (ptid_t ptid
)
526 struct thread_info
*tp
;
528 for (tp
= thread_list
; tp
; tp
= tp
->next
)
529 if (tp
->ptid
== ptid
)
535 /* See gdbthread.h. */
538 find_thread_by_handle (struct value
*thread_handle
, struct inferior
*inf
)
540 return target_thread_handle_to_thread_info
541 (value_contents_all (thread_handle
),
542 TYPE_LENGTH (value_type (thread_handle
)),
547 * Thread iterator function.
549 * Calls a callback function once for each thread, so long as
550 * the callback function returns false. If the callback function
551 * returns true, the iteration will end and the current thread
552 * will be returned. This can be useful for implementing a
553 * search for a thread with arbitrary attributes, or for applying
554 * some operation to every thread.
556 * FIXME: some of the existing functionality, such as
557 * "Thread apply all", might be rewritten using this functionality.
561 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
564 struct thread_info
*tp
, *next
;
566 for (tp
= thread_list
; tp
; tp
= next
)
569 if ((*callback
) (tp
, data
))
580 struct thread_info
*tp
;
582 for (tp
= thread_list
; tp
; tp
= tp
->next
)
588 /* Return the number of non-exited threads in the thread list. */
591 live_threads_count (void)
594 struct thread_info
*tp
;
596 ALL_NON_EXITED_THREADS (tp
)
603 valid_global_thread_id (int global_id
)
605 struct thread_info
*tp
;
607 for (tp
= thread_list
; tp
; tp
= tp
->next
)
608 if (tp
->global_num
== global_id
)
615 ptid_to_global_thread_id (ptid_t ptid
)
617 struct thread_info
*tp
;
619 for (tp
= thread_list
; tp
; tp
= tp
->next
)
620 if (tp
->ptid
== ptid
)
621 return tp
->global_num
;
627 global_thread_id_to_ptid (int global_id
)
629 struct thread_info
*thread
= find_thread_global_id (global_id
);
634 return minus_one_ptid
;
638 in_thread_list (ptid_t ptid
)
640 struct thread_info
*tp
;
642 for (tp
= thread_list
; tp
; tp
= tp
->next
)
643 if (tp
->ptid
== ptid
)
646 return 0; /* Never heard of 'im. */
649 /* Finds the first thread of the inferior given by PID. If PID is -1,
650 return the first thread in the list. */
653 first_thread_of_process (int pid
)
655 struct thread_info
*tp
, *ret
= NULL
;
657 for (tp
= thread_list
; tp
; tp
= tp
->next
)
658 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
659 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
666 any_thread_of_process (int pid
)
668 struct thread_info
*tp
;
670 gdb_assert (pid
!= 0);
672 /* Prefer the current thread. */
673 if (ptid_get_pid (inferior_ptid
) == pid
)
674 return inferior_thread ();
676 ALL_NON_EXITED_THREADS (tp
)
677 if (ptid_get_pid (tp
->ptid
) == pid
)
684 any_live_thread_of_process (int pid
)
686 struct thread_info
*curr_tp
= NULL
;
687 struct thread_info
*tp
;
688 struct thread_info
*tp_executing
= NULL
;
690 gdb_assert (pid
!= 0);
692 /* Prefer the current thread if it's not executing. */
693 if (ptid_get_pid (inferior_ptid
) == pid
)
695 /* If the current thread is dead, forget it. If it's not
696 executing, use it. Otherwise, still choose it (below), but
697 only if no other non-executing thread is found. */
698 curr_tp
= inferior_thread ();
699 if (curr_tp
->state
== THREAD_EXITED
)
701 else if (!curr_tp
->executing
)
705 ALL_NON_EXITED_THREADS (tp
)
706 if (ptid_get_pid (tp
->ptid
) == pid
)
714 /* If both the current thread and all live threads are executing,
715 prefer the current thread. */
719 /* Otherwise, just return an executing thread, if any. */
723 /* Print a list of thread ids currently known, and the total number of
724 threads. To be used from within catch_errors. */
726 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
728 struct thread_info
*tp
;
730 int current_thread
= -1;
732 update_thread_list ();
735 ui_out_emit_tuple
tuple_emitter (uiout
, "thread-ids");
737 for (tp
= thread_list
; tp
; tp
= tp
->next
)
739 if (tp
->state
== THREAD_EXITED
)
742 if (tp
->ptid
== inferior_ptid
)
743 current_thread
= tp
->global_num
;
746 uiout
->field_int ("thread-id", tp
->global_num
);
750 if (current_thread
!= -1)
751 uiout
->field_int ("current-thread-id", current_thread
);
752 uiout
->field_int ("number-of-threads", num
);
756 /* Official gdblib interface function to get a list of thread ids and
759 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
761 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
762 error_message
, RETURN_MASK_ALL
) < 0)
767 /* Return true if TP is an active thread. */
769 thread_alive (struct thread_info
*tp
)
771 if (tp
->state
== THREAD_EXITED
)
773 if (!target_thread_alive (tp
->ptid
))
778 /* See gdbthreads.h. */
783 struct thread_info
*tp
, *tmp
;
785 ALL_THREADS_SAFE (tp
, tmp
)
787 if (!thread_alive (tp
))
788 delete_thread (tp
->ptid
);
792 /* See gdbthreads.h. */
795 delete_exited_threads (void)
797 struct thread_info
*tp
, *tmp
;
799 ALL_THREADS_SAFE (tp
, tmp
)
801 if (tp
->state
== THREAD_EXITED
)
802 delete_thread (tp
->ptid
);
806 /* Disable storing stack temporaries for the thread whose id is
810 disable_thread_stack_temporaries (void *data
)
812 ptid_t
*pd
= (ptid_t
*) data
;
813 struct thread_info
*tp
= find_thread_ptid (*pd
);
817 tp
->stack_temporaries_enabled
= 0;
818 VEC_free (value_ptr
, tp
->stack_temporaries
);
824 /* Enable storing stack temporaries for thread with id PTID and return a
825 cleanup which can disable and clear the stack temporaries. */
828 enable_thread_stack_temporaries (ptid_t ptid
)
830 struct thread_info
*tp
= find_thread_ptid (ptid
);
834 gdb_assert (tp
!= NULL
);
836 tp
->stack_temporaries_enabled
= 1;
837 tp
->stack_temporaries
= NULL
;
838 data
= XNEW (ptid_t
);
840 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
845 /* Return non-zero value if stack temporaies are enabled for the thread
849 thread_stack_temporaries_enabled_p (ptid_t ptid
)
851 struct thread_info
*tp
= find_thread_ptid (ptid
);
856 return tp
->stack_temporaries_enabled
;
859 /* Push V on to the stack temporaries of the thread with id PTID. */
862 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
864 struct thread_info
*tp
= find_thread_ptid (ptid
);
866 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
867 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
870 /* Return 1 if VAL is among the stack temporaries of the thread
871 with id PTID. Return 0 otherwise. */
874 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
876 struct thread_info
*tp
= find_thread_ptid (ptid
);
878 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
879 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
884 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
892 /* Return the last of the stack temporaries for thread with id PTID.
893 Return NULL if there are no stack temporaries for the thread. */
896 get_last_thread_stack_temporary (ptid_t ptid
)
898 struct value
*lastval
= NULL
;
899 struct thread_info
*tp
= find_thread_ptid (ptid
);
901 gdb_assert (tp
!= NULL
);
902 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
903 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
909 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
911 struct inferior
*inf
;
912 struct thread_info
*tp
;
914 /* It can happen that what we knew as the target inferior id
915 changes. E.g, target remote may only discover the remote process
916 pid after adding the inferior to GDB's list. */
917 inf
= find_inferior_ptid (old_ptid
);
918 inf
->pid
= ptid_get_pid (new_ptid
);
920 tp
= find_thread_ptid (old_ptid
);
923 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
926 /* See gdbthread.h. */
929 set_resumed (ptid_t ptid
, int resumed
)
931 struct thread_info
*tp
;
932 int all
= ptid
== minus_one_ptid
;
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
))
938 tp
->resumed
= resumed
;
942 tp
= find_thread_ptid (ptid
);
943 gdb_assert (tp
!= NULL
);
944 tp
->resumed
= resumed
;
948 /* Helper for set_running, that marks one thread either running or
952 set_running_thread (struct thread_info
*tp
, int running
)
956 if (running
&& tp
->state
== THREAD_STOPPED
)
958 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
962 /* If the thread is now marked stopped, remove it from
963 the step-over queue, so that we don't try to resume
964 it until the user wants it to. */
965 if (tp
->step_over_next
!= NULL
)
966 thread_step_over_chain_remove (tp
);
973 set_running (ptid_t ptid
, int running
)
975 struct thread_info
*tp
;
976 int all
= ptid
== minus_one_ptid
;
979 /* We try not to notify the observer if no thread has actually changed
980 the running state -- merely to reduce the number of messages to
981 frontend. Frontend is supposed to handle multiple *running just fine. */
982 if (all
|| ptid_is_pid (ptid
))
984 for (tp
= thread_list
; tp
; tp
= tp
->next
)
985 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
987 if (tp
->state
== THREAD_EXITED
)
990 if (set_running_thread (tp
, running
))
996 tp
= find_thread_ptid (ptid
);
997 gdb_assert (tp
!= NULL
);
998 gdb_assert (tp
->state
!= THREAD_EXITED
);
999 if (set_running_thread (tp
, running
))
1003 observer_notify_target_resumed (ptid
);
1007 is_thread_state (ptid_t ptid
, enum thread_state state
)
1009 struct thread_info
*tp
;
1011 tp
= find_thread_ptid (ptid
);
1013 return tp
->state
== state
;
1017 is_stopped (ptid_t ptid
)
1019 return is_thread_state (ptid
, THREAD_STOPPED
);
1023 is_exited (ptid_t ptid
)
1025 return is_thread_state (ptid
, THREAD_EXITED
);
1029 is_running (ptid_t ptid
)
1031 return is_thread_state (ptid
, THREAD_RUNNING
);
1035 is_executing (ptid_t ptid
)
1037 struct thread_info
*tp
;
1039 tp
= find_thread_ptid (ptid
);
1041 return tp
->executing
;
1045 set_executing (ptid_t ptid
, int executing
)
1047 struct thread_info
*tp
;
1048 int all
= ptid
== minus_one_ptid
;
1050 if (all
|| ptid_is_pid (ptid
))
1052 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1053 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1054 tp
->executing
= executing
;
1058 tp
= find_thread_ptid (ptid
);
1060 tp
->executing
= executing
;
1063 /* It only takes one running thread to spawn more threads.*/
1065 threads_executing
= 1;
1066 /* Only clear the flag if the caller is telling us everything is
1068 else if (minus_one_ptid
== ptid
)
1069 threads_executing
= 0;
1072 /* See gdbthread.h. */
1075 threads_are_executing (void)
1077 return threads_executing
;
1081 set_stop_requested (ptid_t ptid
, int stop
)
1083 struct thread_info
*tp
;
1084 int all
= ptid
== minus_one_ptid
;
1086 if (all
|| ptid_is_pid (ptid
))
1088 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1089 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1090 tp
->stop_requested
= stop
;
1094 tp
= find_thread_ptid (ptid
);
1096 tp
->stop_requested
= stop
;
1099 /* Call the stop requested observer so other components of GDB can
1100 react to this request. */
1102 observer_notify_thread_stop_requested (ptid
);
1106 finish_thread_state (ptid_t ptid
)
1108 struct thread_info
*tp
;
1110 int any_started
= 0;
1112 all
= ptid
== minus_one_ptid
;
1114 if (all
|| ptid_is_pid (ptid
))
1116 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1118 if (tp
->state
== THREAD_EXITED
)
1120 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1122 if (set_running_thread (tp
, tp
->executing
))
1129 tp
= find_thread_ptid (ptid
);
1131 if (tp
->state
!= THREAD_EXITED
)
1133 if (set_running_thread (tp
, tp
->executing
))
1139 observer_notify_target_resumed (ptid
);
1143 finish_thread_state_cleanup (void *arg
)
1145 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1149 finish_thread_state (*ptid_p
);
1152 /* See gdbthread.h. */
1155 validate_registers_access (void)
1157 /* No selected thread, no registers. */
1158 if (inferior_ptid
== null_ptid
)
1159 error (_("No thread selected."));
1161 /* Don't try to read from a dead thread. */
1162 if (is_exited (inferior_ptid
))
1163 error (_("The current thread has terminated"));
1165 /* ... or from a spinning thread. FIXME: This isn't actually fully
1166 correct. It'll allow an user-requested access (e.g., "print $pc"
1167 at the prompt) when a thread is not executing for some internal
1168 reason, but is marked running from the user's perspective. E.g.,
1169 the thread is waiting for its turn in the step-over queue. */
1170 if (is_executing (inferior_ptid
))
1171 error (_("Selected thread is running."));
1174 /* See gdbthread.h. */
1177 can_access_registers_ptid (ptid_t ptid
)
1179 /* No thread, no registers. */
1180 if (ptid
== null_ptid
)
1183 /* Don't try to read from a dead thread. */
1184 if (is_exited (ptid
))
1187 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1188 if (is_executing (ptid
))
1195 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1197 return (pc
>= thread
->control
.step_range_start
1198 && pc
< thread
->control
.step_range_end
);
1201 /* Helper for print_thread_info. Returns true if THR should be
1202 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1203 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1204 is true if REQUESTED_THREADS is list of global IDs, false if a list
1205 of per-inferior thread ids. If PID is not -1, only print THR if it
1206 is a thread from the process PID. Otherwise, threads from all
1207 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1208 and PID is not -1, then the thread is printed if it belongs to the
1209 specified process. Otherwise, an error is raised. */
1212 should_print_thread (const char *requested_threads
, int default_inf_num
,
1213 int global_ids
, int pid
, struct thread_info
*thr
)
1215 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1220 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1222 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1223 thr
->inf
->num
, thr
->per_inf_num
);
1228 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1230 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1231 error (_("Requested thread not found in requested process"));
1235 if (thr
->state
== THREAD_EXITED
)
1241 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1242 whether REQUESTED_THREADS is a list of global or per-inferior
1246 print_thread_info_1 (struct ui_out
*uiout
, char *requested_threads
,
1247 int global_ids
, int pid
,
1248 int show_global_ids
)
1250 struct thread_info
*tp
;
1251 ptid_t current_ptid
;
1252 const char *extra_info
, *name
, *target_id
;
1253 struct inferior
*inf
;
1254 int default_inf_num
= current_inferior ()->num
;
1256 update_thread_list ();
1257 current_ptid
= inferior_ptid
;
1260 /* For backward compatibility, we make a list for MI. A table is
1261 preferable for the CLI, though, because it shows table
1263 gdb::optional
<ui_out_emit_list
> list_emitter
;
1264 gdb::optional
<ui_out_emit_table
> table_emitter
;
1266 if (uiout
->is_mi_like_p ())
1267 list_emitter
.emplace (uiout
, "threads");
1272 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1274 if (!should_print_thread (requested_threads
, default_inf_num
,
1275 global_ids
, pid
, tp
))
1283 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1284 uiout
->message (_("No threads.\n"));
1286 uiout
->message (_("No threads match '%s'.\n"),
1291 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1292 n_threads
, "threads");
1294 uiout
->table_header (1, ui_left
, "current", "");
1295 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1296 if (show_global_ids
)
1297 uiout
->table_header (4, ui_left
, "id", "GId");
1298 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1299 uiout
->table_header (1, ui_left
, "frame", "Frame");
1300 uiout
->table_body ();
1303 /* We'll be switching threads temporarily. */
1304 scoped_restore_current_thread restore_thread
;
1306 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1310 if (!should_print_thread (requested_threads
, default_inf_num
,
1311 global_ids
, pid
, tp
))
1314 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1316 if (!uiout
->is_mi_like_p ())
1318 if (tp
->ptid
== current_ptid
)
1319 uiout
->field_string ("current", "*");
1321 uiout
->field_skip ("current");
1323 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1326 if (show_global_ids
|| uiout
->is_mi_like_p ())
1327 uiout
->field_int ("id", tp
->global_num
);
1329 /* For the CLI, we stuff everything into the target-id field.
1330 This is a gross hack to make the output come out looking
1331 correct. The underlying problem here is that ui-out has no
1332 way to specify that a field's space allocation should be
1333 shared by several fields. For MI, we do the right thing
1336 target_id
= target_pid_to_str (tp
->ptid
);
1337 extra_info
= target_extra_thread_info (tp
);
1338 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1340 if (uiout
->is_mi_like_p ())
1342 uiout
->field_string ("target-id", target_id
);
1344 uiout
->field_string ("details", extra_info
);
1346 uiout
->field_string ("name", name
);
1350 std::string contents
;
1352 if (extra_info
&& name
)
1353 contents
= string_printf ("%s \"%s\" (%s)", target_id
,
1355 else if (extra_info
)
1356 contents
= string_printf ("%s (%s)", target_id
, extra_info
);
1358 contents
= string_printf ("%s \"%s\"", target_id
, name
);
1360 contents
= target_id
;
1362 uiout
->field_string ("target-id", contents
.c_str ());
1365 if (tp
->state
== THREAD_RUNNING
)
1366 uiout
->text ("(running)\n");
1369 /* The switch below puts us at the top of the stack (leaf
1371 switch_to_thread (tp
->ptid
);
1372 print_stack_frame (get_selected_frame (NULL
),
1373 /* For MI output, print frame level. */
1374 uiout
->is_mi_like_p (),
1378 if (uiout
->is_mi_like_p ())
1380 const char *state
= "stopped";
1382 if (tp
->state
== THREAD_RUNNING
)
1384 uiout
->field_string ("state", state
);
1387 core
= target_core_of_thread (tp
->ptid
);
1388 if (uiout
->is_mi_like_p () && core
!= -1)
1389 uiout
->field_int ("core", core
);
1392 /* This end scope restores the current thread and the frame
1393 selected before the "info threads" command, and it finishes the
1394 ui-out list or table. */
1397 if (pid
== -1 && requested_threads
== NULL
)
1399 if (uiout
->is_mi_like_p ()
1400 && inferior_ptid
!= null_ptid
)
1402 int num
= ptid_to_global_thread_id (inferior_ptid
);
1404 gdb_assert (num
!= 0);
1405 uiout
->field_int ("current-thread-id", num
);
1408 if (inferior_ptid
!= null_ptid
&& is_exited (inferior_ptid
))
1409 uiout
->message ("\n\
1410 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1411 print_thread_id (inferior_thread ()));
1412 else if (thread_list
!= NULL
&& inferior_ptid
== null_ptid
)
1413 uiout
->message ("\n\
1414 No selected thread. See `help thread'.\n");
1418 /* See gdbthread.h. */
1421 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1423 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1426 /* Implementation of the "info threads" command.
1428 Note: this has the drawback that it _really_ switches
1429 threads, which frees the frame cache. A no-side
1430 effects info-threads command would be nicer. */
1433 info_threads_command (char *arg
, int from_tty
)
1435 int show_global_ids
= 0;
1438 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1440 arg
= skip_spaces (arg
);
1441 show_global_ids
= 1;
1444 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1447 /* See gdbthread.h. */
1450 switch_to_thread_no_regs (struct thread_info
*thread
)
1452 struct inferior
*inf
= thread
->inf
;
1454 set_current_program_space (inf
->pspace
);
1455 set_current_inferior (inf
);
1457 inferior_ptid
= thread
->ptid
;
1458 stop_pc
= ~(CORE_ADDR
) 0;
1461 /* Switch to no thread selected. */
1464 switch_to_no_thread ()
1466 if (inferior_ptid
== null_ptid
)
1469 inferior_ptid
= null_ptid
;
1470 reinit_frame_cache ();
1471 stop_pc
= ~(CORE_ADDR
) 0;
1474 /* Switch from one thread to another. */
1477 switch_to_thread (thread_info
*thr
)
1479 gdb_assert (thr
!= NULL
);
1481 if (inferior_ptid
== thr
->ptid
)
1484 switch_to_thread_no_regs (thr
);
1486 reinit_frame_cache ();
1488 /* We don't check for is_stopped, because we're called at times
1489 while in the TARGET_RUNNING state, e.g., while handling an
1491 if (thr
->state
!= THREAD_EXITED
1493 stop_pc
= regcache_read_pc (get_thread_regcache (thr
->ptid
));
1496 /* See gdbthread.h. */
1499 switch_to_thread (ptid_t ptid
)
1501 if (ptid
== null_ptid
)
1502 switch_to_no_thread ();
1504 switch_to_thread (find_thread_ptid (ptid
));
1508 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1510 struct frame_info
*frame
= NULL
;
1513 /* This means there was no selected frame. */
1514 if (frame_level
== -1)
1516 select_frame (NULL
);
1520 gdb_assert (frame_level
>= 0);
1522 /* Restore by level first, check if the frame id is the same as
1523 expected. If that fails, try restoring by frame id. If that
1524 fails, nothing to do, just warn the user. */
1526 count
= frame_level
;
1527 frame
= find_relative_frame (get_current_frame (), &count
);
1530 /* The frame ids must match - either both valid or both outer_frame_id.
1531 The latter case is not failsafe, but since it's highly unlikely
1532 the search by level finds the wrong frame, it's 99.9(9)% of
1533 the time (for all practical purposes) safe. */
1534 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1536 /* Cool, all is fine. */
1537 select_frame (frame
);
1541 frame
= frame_find_by_id (a_frame_id
);
1544 /* Cool, refound it. */
1545 select_frame (frame
);
1549 /* Nothing else to do, the frame layout really changed. Select the
1550 innermost stack frame. */
1551 select_frame (get_current_frame ());
1553 /* Warn the user. */
1554 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1556 warning (_("Couldn't restore frame #%d in "
1557 "current thread. Bottom (innermost) frame selected:"),
1559 /* For MI, we should probably have a notification about
1560 current frame change. But this error is not very
1561 likely, so don't bother for now. */
1562 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1566 scoped_restore_current_thread::~scoped_restore_current_thread ()
1568 /* If an entry of thread_info was previously selected, it won't be
1569 deleted because we've increased its refcount. The thread represented
1570 by this thread_info entry may have already exited (due to normal exit,
1571 detach, etc), so the thread_info.state is THREAD_EXITED. */
1572 if (m_thread
!= NULL
1573 /* If the previously selected thread belonged to a process that has
1574 in the mean time exited (or killed, detached, etc.), then don't revert
1575 back to it, but instead simply drop back to no thread selected. */
1577 switch_to_thread (m_thread
);
1580 switch_to_no_thread ();
1581 set_current_inferior (m_inf
);
1584 /* The running state of the originally selected thread may have
1585 changed, so we have to recheck it here. */
1586 if (inferior_ptid
!= null_ptid
1588 && is_stopped (inferior_ptid
)
1589 && target_has_registers
1591 && target_has_memory
)
1592 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1594 if (m_thread
!= NULL
)
1595 m_thread
->decref ();
1599 scoped_restore_current_thread::scoped_restore_current_thread ()
1602 m_inf
= current_inferior ();
1604 if (inferior_ptid
!= null_ptid
)
1606 thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1607 struct frame_info
*frame
;
1609 gdb_assert (tp
!= NULL
);
1611 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1613 && target_has_registers
1615 && target_has_memory
)
1617 /* When processing internal events, there might not be a
1618 selected frame. If we naively call get_selected_frame
1619 here, then we can end up reading debuginfo for the
1620 current frame, but we don't generally need the debuginfo
1622 frame
= get_selected_frame_if_set ();
1627 m_selected_frame_id
= get_frame_id (frame
);
1628 m_selected_frame_level
= frame_relative_level (frame
);
1637 /* See gdbthread.h. */
1640 show_thread_that_caused_stop (void)
1642 return highest_thread_num
> 1;
1645 /* See gdbthread.h. */
1648 show_inferior_qualified_tids (void)
1650 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1653 /* See gdbthread.h. */
1656 print_thread_id (struct thread_info
*thr
)
1658 char *s
= get_print_cell ();
1660 if (show_inferior_qualified_tids ())
1661 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1663 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1667 /* If true, tp_array_compar should sort in ascending order, otherwise
1668 in descending order. */
1670 static bool tp_array_compar_ascending
;
1672 /* Sort an array for struct thread_info pointers by thread ID (first
1673 by inferior number, and then by per-inferior thread number). The
1674 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1677 tp_array_compar (const thread_info
*a
, const thread_info
*b
)
1679 if (a
->inf
->num
!= b
->inf
->num
)
1681 if (tp_array_compar_ascending
)
1682 return a
->inf
->num
< b
->inf
->num
;
1684 return a
->inf
->num
> b
->inf
->num
;
1687 if (tp_array_compar_ascending
)
1688 return (a
->per_inf_num
< b
->per_inf_num
);
1690 return (a
->per_inf_num
> b
->per_inf_num
);
1693 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1694 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1695 of two numbers seperated by a hyphen. Examples:
1697 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1698 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1699 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1702 thread_apply_all_command (char *cmd
, int from_tty
)
1704 tp_array_compar_ascending
= false;
1706 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1708 cmd
= skip_spaces (cmd
);
1709 tp_array_compar_ascending
= true;
1712 if (cmd
== NULL
|| *cmd
== '\000')
1713 error (_("Please specify a command following the thread ID list"));
1715 update_thread_list ();
1717 /* Save a copy of the command in case it is clobbered by
1719 std::string saved_cmd
= cmd
;
1721 int tc
= live_threads_count ();
1724 /* Save a copy of the thread list and increment each thread's
1725 refcount while executing the command in the context of each
1726 thread, in case the command is one that wipes threads. E.g.,
1727 detach, kill, disconnect, etc., or even normally continuing
1728 over an inferior or thread exit. */
1729 std::vector
<thread_info
*> thr_list_cpy
;
1730 thr_list_cpy
.reserve (tc
);
1735 ALL_NON_EXITED_THREADS (tp
)
1737 thr_list_cpy
.push_back (tp
);
1740 gdb_assert (thr_list_cpy
.size () == tc
);
1743 /* Increment the refcounts, and restore them back on scope
1745 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1747 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), tp_array_compar
);
1749 scoped_restore_current_thread restore_thread
;
1751 for (thread_info
*thr
: thr_list_cpy
)
1752 if (thread_alive (thr
))
1754 switch_to_thread (thr
->ptid
);
1755 printf_filtered (_("\nThread %s (%s):\n"),
1756 print_thread_id (thr
),
1757 target_pid_to_str (inferior_ptid
));
1758 execute_command (cmd
, from_tty
);
1760 /* Restore exact command used previously. */
1761 strcpy (cmd
, saved_cmd
.c_str ());
1766 /* Implementation of the "thread apply" command. */
1769 thread_apply_command (char *tidlist
, int from_tty
)
1772 tid_range_parser parser
;
1774 if (tidlist
== NULL
|| *tidlist
== '\000')
1775 error (_("Please specify a thread ID list"));
1777 parser
.init (tidlist
, current_inferior ()->num
);
1778 while (!parser
.finished ())
1780 int inf_num
, thr_start
, thr_end
;
1782 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1784 cmd
= (char *) parser
.cur_tok ();
1790 error (_("Please specify a command following the thread ID list"));
1792 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1793 invalid_thread_id_error (cmd
);
1795 /* Save a copy of the command in case it is clobbered by
1797 std::string saved_cmd
= cmd
;
1799 scoped_restore_current_thread restore_thread
;
1801 parser
.init (tidlist
, current_inferior ()->num
);
1802 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1804 struct thread_info
*tp
= NULL
;
1805 struct inferior
*inf
;
1806 int inf_num
, thr_num
;
1808 parser
.get_tid (&inf_num
, &thr_num
);
1809 inf
= find_inferior_id (inf_num
);
1811 tp
= find_thread_id (inf
, thr_num
);
1813 if (parser
.in_star_range ())
1817 warning (_("Unknown inferior %d"), inf_num
);
1818 parser
.skip_range ();
1822 /* No use looking for threads past the highest thread number
1823 the inferior ever had. */
1824 if (thr_num
>= inf
->highest_thread_num
)
1825 parser
.skip_range ();
1827 /* Be quiet about unknown threads numbers. */
1834 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1835 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1837 warning (_("Unknown thread %d"), thr_num
);
1841 if (!thread_alive (tp
))
1843 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1847 switch_to_thread (tp
->ptid
);
1849 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1850 target_pid_to_str (inferior_ptid
));
1851 execute_command (cmd
, from_tty
);
1853 /* Restore exact command used previously. */
1854 strcpy (cmd
, saved_cmd
.c_str ());
1858 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1859 if prefix of arg is `apply'. */
1862 thread_command (char *tidstr
, int from_tty
)
1866 if (inferior_ptid
== null_ptid
)
1867 error (_("No thread selected"));
1869 if (target_has_stack
)
1871 struct thread_info
*tp
= inferior_thread ();
1873 if (is_exited (inferior_ptid
))
1874 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1875 print_thread_id (tp
),
1876 target_pid_to_str (inferior_ptid
));
1878 printf_filtered (_("[Current thread is %s (%s)]\n"),
1879 print_thread_id (tp
),
1880 target_pid_to_str (inferior_ptid
));
1883 error (_("No stack."));
1887 ptid_t previous_ptid
= inferior_ptid
;
1890 result
= gdb_thread_select (current_uiout
, tidstr
, NULL
);
1892 /* If thread switch did not succeed don't notify or print. */
1893 if (result
== GDB_RC_FAIL
)
1896 /* Print if the thread has not changed, otherwise an event will
1898 if (inferior_ptid
== previous_ptid
)
1900 print_selected_thread_frame (current_uiout
,
1901 USER_SELECTED_THREAD
1902 | USER_SELECTED_FRAME
);
1906 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1907 | USER_SELECTED_FRAME
);
1912 /* Implementation of `thread name'. */
1915 thread_name_command (const char *arg
, int from_tty
)
1917 struct thread_info
*info
;
1919 if (inferior_ptid
== null_ptid
)
1920 error (_("No thread selected"));
1922 arg
= skip_spaces (arg
);
1924 info
= inferior_thread ();
1926 info
->name
= arg
? xstrdup (arg
) : NULL
;
1929 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1932 thread_find_command (const char *arg
, int from_tty
)
1934 struct thread_info
*tp
;
1936 unsigned long match
= 0;
1938 if (arg
== NULL
|| *arg
== '\0')
1939 error (_("Command requires an argument."));
1941 tmp
= re_comp (arg
);
1943 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1945 update_thread_list ();
1946 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1948 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1950 printf_filtered (_("Thread %s has name '%s'\n"),
1951 print_thread_id (tp
), tp
->name
);
1955 tmp
= target_thread_name (tp
);
1956 if (tmp
!= NULL
&& re_exec (tmp
))
1958 printf_filtered (_("Thread %s has target name '%s'\n"),
1959 print_thread_id (tp
), tmp
);
1963 tmp
= target_pid_to_str (tp
->ptid
);
1964 if (tmp
!= NULL
&& re_exec (tmp
))
1966 printf_filtered (_("Thread %s has target id '%s'\n"),
1967 print_thread_id (tp
), tmp
);
1971 tmp
= target_extra_thread_info (tp
);
1972 if (tmp
!= NULL
&& re_exec (tmp
))
1974 printf_filtered (_("Thread %s has extra info '%s'\n"),
1975 print_thread_id (tp
), tmp
);
1980 printf_filtered (_("No threads match '%s'\n"), arg
);
1983 /* Print notices when new threads are attached and detached. */
1984 int print_thread_events
= 1;
1986 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1987 struct cmd_list_element
*c
, const char *value
)
1989 fprintf_filtered (file
,
1990 _("Printing of thread events is %s.\n"),
1995 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr_v
)
1997 const char *tidstr
= (const char *) tidstr_v
;
1998 struct thread_info
*tp
;
2000 if (uiout
->is_mi_like_p ())
2002 int num
= value_as_long (parse_and_eval (tidstr
));
2004 tp
= find_thread_global_id (num
);
2006 error (_("Thread ID %d not known."), num
);
2010 tp
= parse_thread_id (tidstr
, NULL
);
2011 gdb_assert (tp
!= NULL
);
2014 if (!thread_alive (tp
))
2015 error (_("Thread ID %s has terminated."), tidstr
);
2017 switch_to_thread (tp
->ptid
);
2019 annotate_thread_changed ();
2021 /* Since the current thread may have changed, see if there is any
2022 exited thread we can now delete. */
2028 /* Print thread and frame switch command response. */
2031 print_selected_thread_frame (struct ui_out
*uiout
,
2032 user_selected_what selection
)
2034 struct thread_info
*tp
= inferior_thread ();
2035 struct inferior
*inf
= current_inferior ();
2037 if (selection
& USER_SELECTED_THREAD
)
2039 if (uiout
->is_mi_like_p ())
2041 uiout
->field_int ("new-thread-id",
2042 inferior_thread ()->global_num
);
2046 uiout
->text ("[Switching to thread ");
2047 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2049 uiout
->text (target_pid_to_str (inferior_ptid
));
2054 if (tp
->state
== THREAD_RUNNING
)
2056 if (selection
& USER_SELECTED_THREAD
)
2057 uiout
->text ("(running)\n");
2059 else if (selection
& USER_SELECTED_FRAME
)
2061 if (selection
& USER_SELECTED_THREAD
)
2064 if (has_stack_frames ())
2065 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2071 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
2073 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
2074 error_message
, RETURN_MASK_ALL
) < 0)
2079 /* Update the 'threads_executing' global based on the threads we know
2083 update_threads_executing (void)
2085 struct thread_info
*tp
;
2087 threads_executing
= 0;
2088 ALL_NON_EXITED_THREADS (tp
)
2092 threads_executing
= 1;
2099 update_thread_list (void)
2101 target_update_thread_list ();
2102 update_threads_executing ();
2105 /* Return a new value for the selected thread's id. Return a value of
2106 0 if no thread is selected. If GLOBAL is true, return the thread's
2107 global number. Otherwise return the per-inferior number. */
2109 static struct value
*
2110 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2112 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2118 int_val
= tp
->global_num
;
2120 int_val
= tp
->per_inf_num
;
2122 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2125 /* Return a new value for the selected thread's per-inferior thread
2126 number. Return a value of 0 if no thread is selected, or no
2129 static struct value
*
2130 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2131 struct internalvar
*var
,
2134 return thread_num_make_value_helper (gdbarch
, 0);
2137 /* Return a new value for the selected thread's global id. Return a
2138 value of 0 if no thread is selected, or no threads exist. */
2140 static struct value
*
2141 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2144 return thread_num_make_value_helper (gdbarch
, 1);
2147 /* Commands with a prefix of `thread'. */
2148 struct cmd_list_element
*thread_cmd_list
= NULL
;
2150 /* Implementation of `thread' variable. */
2152 static const struct internalvar_funcs thread_funcs
=
2154 thread_id_per_inf_num_make_value
,
2159 /* Implementation of `gthread' variable. */
2161 static const struct internalvar_funcs gthread_funcs
=
2163 global_thread_id_make_value
,
2169 _initialize_thread (void)
2171 static struct cmd_list_element
*thread_apply_list
= NULL
;
2173 add_info ("threads", info_threads_command
,
2174 _("Display currently known threads.\n\
2175 Usage: info threads [-gid] [ID]...\n\
2176 -gid: Show global thread IDs.\n\
2177 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2178 Otherwise, all threads are displayed."));
2180 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2181 Use this command to switch between threads.\n\
2182 The new thread ID must be currently known."),
2183 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2185 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2186 _("Apply a command to a list of threads."),
2187 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2189 add_cmd ("all", class_run
, thread_apply_all_command
,
2191 Apply a command to all threads.\n\
2193 Usage: thread apply all [-ascending] <command>\n\
2194 -ascending: Call <command> for all threads in ascending order.\n\
2195 The default is descending order.\
2197 &thread_apply_list
);
2199 add_cmd ("name", class_run
, thread_name_command
,
2200 _("Set the current thread's name.\n\
2201 Usage: thread name [NAME]\n\
2202 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2204 add_cmd ("find", class_run
, thread_find_command
, _("\
2205 Find threads that match a regular expression.\n\
2206 Usage: thread find REGEXP\n\
2207 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2210 add_com_alias ("t", "thread", class_run
, 1);
2212 add_setshow_boolean_cmd ("thread-events", no_class
,
2213 &print_thread_events
, _("\
2214 Set printing of thread events (such as thread start and exit)."), _("\
2215 Show printing of thread events (such as thread start and exit)."), NULL
,
2217 show_print_thread_events
,
2218 &setprintlist
, &showprintlist
);
2220 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2221 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);