1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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/>. */
26 #include "gdbsupport/environ.h"
29 #include "gdbthread.h"
36 #include <sys/types.h>
39 #include "observable.h"
41 #include "cli/cli-decode.h"
42 #include "cli/cli-option.h"
43 #include "gdbsupport/gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46 #include "tid-parse.h"
48 #include "gdbsupport/gdb_optional.h"
49 #include "inline-frame.h"
52 /* See gdbthread.h. */
54 bool debug_threads
= false;
56 /* Implement 'show debug threads'. */
59 show_debug_threads (struct ui_file
*file
, int from_tty
,
60 struct cmd_list_element
*c
, const char *value
)
62 gdb_printf (file
, _("Thread debugging is \"%s\".\n"), value
);
65 /* Definition of struct thread_info exported to gdbthread.h. */
67 /* Prototypes for local functions. */
69 static int highest_thread_num
;
71 /* The current/selected thread. */
72 static thread_info
*current_thread_
;
74 /* Returns true if THR is the current thread. */
77 is_current_thread (const thread_info
*thr
)
79 return thr
== current_thread_
;
83 inferior_thread (void)
85 gdb_assert (current_thread_
!= nullptr);
86 return current_thread_
;
89 /* Delete the breakpoint pointed at by BP_P, if there's one. */
92 delete_thread_breakpoint (struct breakpoint
**bp_p
)
96 delete_breakpoint (*bp_p
);
102 delete_step_resume_breakpoint (struct thread_info
*tp
)
105 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
109 delete_exception_resume_breakpoint (struct thread_info
*tp
)
112 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
115 /* See gdbthread.h. */
118 delete_single_step_breakpoints (struct thread_info
*tp
)
121 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
124 /* Delete the breakpoint pointed at by BP_P at the next stop, if
128 delete_at_next_stop (struct breakpoint
**bp
)
132 (*bp
)->disposition
= disp_del_at_next_stop
;
137 /* See gdbthread.h. */
140 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
142 return tp
->control
.single_step_breakpoints
!= NULL
;
145 /* See gdbthread.h. */
148 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
149 const address_space
*aspace
,
152 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
154 return (ss_bps
!= NULL
155 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
158 /* See gdbthread.h. */
161 thread_cancel_execution_command (struct thread_info
*thr
)
163 if (thr
->thread_fsm () != nullptr)
165 std::unique_ptr
<thread_fsm
> fsm
= thr
->release_thread_fsm ();
171 clear_thread_inferior_resources (struct thread_info
*tp
)
173 /* NOTE: this will take care of any left-over step_resume breakpoints,
174 but not any user-specified thread-specific breakpoints. We can not
175 delete the breakpoint straight-off, because the inferior might not
176 be stopped at the moment. */
177 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
178 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
179 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
181 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
183 bpstat_clear (&tp
->control
.stop_bpstat
);
185 btrace_teardown (tp
);
187 thread_cancel_execution_command (tp
);
189 clear_inline_frame_state (tp
);
192 /* See gdbthread.h. */
195 set_thread_exited (thread_info
*tp
, bool silent
)
197 /* Dead threads don't need to step-over. Remove from chain. */
198 if (thread_is_in_step_over_chain (tp
))
199 global_thread_step_over_chain_remove (tp
);
201 if (tp
->state
!= THREAD_EXITED
)
203 process_stratum_target
*proc_target
= tp
->inf
->process_target ();
205 /* Some targets unpush themselves from the inferior's target stack before
206 clearing the inferior's thread list (which marks all threads as exited,
207 and therefore leads to this function). In this case, the inferior's
208 process target will be nullptr when we arrive here.
210 See also the comment in inferior::unpush_target. */
211 if (proc_target
!= nullptr)
212 proc_target
->maybe_remove_resumed_with_pending_wait_status (tp
);
214 gdb::observers::thread_exit
.notify (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
);
222 /* Remove from the ptid_t map. We don't want for
223 find_thread_ptid to find exited threads. Also, the target
224 may reuse the ptid for a new thread, and there can only be
225 one value per key; adding a new thread with the same ptid_t
226 would overwrite the exited thread's ptid entry. */
227 size_t nr_deleted
= tp
->inf
->ptid_thread_map
.erase (tp
->ptid
);
228 gdb_assert (nr_deleted
== 1);
233 init_thread_list (void)
235 highest_thread_num
= 0;
237 for (inferior
*inf
: all_inferiors ())
238 inf
->clear_thread_list (true);
241 /* Allocate a new thread of inferior INF with target id PTID and add
242 it to the thread list. */
244 static struct thread_info
*
245 new_thread (struct inferior
*inf
, ptid_t ptid
)
247 thread_info
*tp
= new thread_info (inf
, ptid
);
249 threads_debug_printf ("creating a new thread object, inferior %d, ptid %s",
250 inf
->num
, ptid
.to_string ().c_str ());
252 inf
->thread_list
.push_back (*tp
);
254 /* A thread with this ptid should not exist in the map yet. */
255 gdb_assert (inf
->ptid_thread_map
.find (ptid
) == inf
->ptid_thread_map
.end ());
257 inf
->ptid_thread_map
[ptid
] = tp
;
263 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
265 gdb_assert (targ
!= nullptr);
267 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
269 threads_debug_printf ("add thread to inferior %d, ptid %s, target %s",
270 inf
->num
, ptid
.to_string ().c_str (),
273 /* We may have an old thread with the same id in the thread list.
274 If we do, it must be dead, otherwise we wouldn't be adding a new
275 thread with the same id. The OS is reusing this id --- delete
276 the old thread, and create a new one. */
277 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
281 tp
= new_thread (inf
, ptid
);
282 gdb::observers::new_thread
.notify (tp
);
288 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
289 private_thread_info
*priv
)
291 thread_info
*result
= add_thread_silent (targ
, ptid
);
293 result
->priv
.reset (priv
);
295 if (print_thread_events
)
296 gdb_printf (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
298 annotate_new_thread ();
303 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
305 return add_thread_with_info (targ
, ptid
, NULL
);
308 private_thread_info::~private_thread_info () = default;
310 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
311 : ptid (ptid_
), inf (inf_
)
313 gdb_assert (inf_
!= NULL
);
315 this->global_num
= ++highest_thread_num
;
316 this->per_inf_num
= ++inf_
->highest_thread_num
;
318 /* Nothing to follow yet. */
319 this->pending_follow
.set_spurious ();
322 /* See gdbthread.h. */
324 thread_info::~thread_info ()
326 threads_debug_printf ("thread %s", this->ptid
.to_string ().c_str ());
329 /* See gdbthread.h. */
332 thread_info::deletable () const
334 /* If this is the current thread, or there's code out there that
335 relies on it existing (refcount > 0) we can't delete yet. */
336 return refcount () == 0 && !is_current_thread (this);
339 /* See gdbthread.h. */
342 thread_info::set_executing (bool executing
)
344 m_executing
= executing
;
346 this->clear_stop_pc ();
349 /* See gdbthread.h. */
352 thread_info::set_resumed (bool resumed
)
354 if (resumed
== m_resumed
)
357 process_stratum_target
*proc_target
= this->inf
->process_target ();
359 /* If we transition from resumed to not resumed, we might need to remove
360 the thread from the resumed threads with pending statuses list. */
362 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
366 /* If we transition from not resumed to resumed, we might need to add
367 the thread to the resumed threads with pending statuses list. */
369 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
372 /* See gdbthread.h. */
375 thread_info::set_pending_waitstatus (const target_waitstatus
&ws
)
377 gdb_assert (!this->has_pending_waitstatus ());
379 m_suspend
.waitstatus
= ws
;
380 m_suspend
.waitstatus_pending_p
= 1;
382 process_stratum_target
*proc_target
= this->inf
->process_target ();
383 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
386 /* See gdbthread.h. */
389 thread_info::clear_pending_waitstatus ()
391 gdb_assert (this->has_pending_waitstatus ());
393 process_stratum_target
*proc_target
= this->inf
->process_target ();
394 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
396 m_suspend
.waitstatus_pending_p
= 0;
399 /* See gdbthread.h. */
402 thread_is_in_step_over_chain (struct thread_info
*tp
)
404 return tp
->step_over_list_node
.is_linked ();
407 /* See gdbthread.h. */
410 thread_step_over_chain_length (const thread_step_over_list
&l
)
414 for (const thread_info
&thread ATTRIBUTE_UNUSED
: l
)
420 /* See gdbthread.h. */
423 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
425 infrun_debug_printf ("enqueueing thread %s in global step over chain",
426 tp
->ptid
.to_string ().c_str ());
428 gdb_assert (!thread_is_in_step_over_chain (tp
));
429 global_thread_step_over_list
.push_back (*tp
);
432 /* See gdbthread.h. */
435 global_thread_step_over_chain_enqueue_chain (thread_step_over_list
&&list
)
437 global_thread_step_over_list
.splice (std::move (list
));
440 /* See gdbthread.h. */
443 global_thread_step_over_chain_remove (struct thread_info
*tp
)
445 infrun_debug_printf ("removing thread %s from global step over chain",
446 tp
->ptid
.to_string ().c_str ());
448 gdb_assert (thread_is_in_step_over_chain (tp
));
449 auto it
= global_thread_step_over_list
.iterator_to (*tp
);
450 global_thread_step_over_list
.erase (it
);
453 /* Delete the thread referenced by THR. If SILENT, don't notify
454 the observer of this exit.
456 THR must not be NULL or a failed assertion will be raised. */
459 delete_thread_1 (thread_info
*thr
, bool silent
)
461 gdb_assert (thr
!= nullptr);
463 threads_debug_printf ("deleting thread %s, silent = %d",
464 thr
->ptid
.to_string ().c_str (), silent
);
466 set_thread_exited (thr
, silent
);
468 if (!thr
->deletable ())
470 /* Will be really deleted some other time. */
474 auto it
= thr
->inf
->thread_list
.iterator_to (*thr
);
475 thr
->inf
->thread_list
.erase (it
);
480 /* See gdbthread.h. */
483 delete_thread (thread_info
*thread
)
485 delete_thread_1 (thread
, false /* not silent */);
489 delete_thread_silent (thread_info
*thread
)
491 delete_thread_1 (thread
, true /* silent */);
495 find_thread_global_id (int global_id
)
497 for (thread_info
*tp
: all_threads ())
498 if (tp
->global_num
== global_id
)
504 static struct thread_info
*
505 find_thread_id (struct inferior
*inf
, int thr_num
)
507 for (thread_info
*tp
: inf
->threads ())
508 if (tp
->per_inf_num
== thr_num
)
514 /* See gdbthread.h. */
517 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
519 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
522 return find_thread_ptid (inf
, ptid
);
525 /* See gdbthread.h. */
528 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
530 gdb_assert (inf
!= nullptr);
532 auto it
= inf
->ptid_thread_map
.find (ptid
);
533 if (it
!= inf
->ptid_thread_map
.end ())
539 /* See gdbthread.h. */
542 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
543 struct inferior
*inf
)
545 return target_thread_handle_to_thread_info (handle
.data (),
551 * Thread iterator function.
553 * Calls a callback function once for each thread, so long as
554 * the callback function returns false. If the callback function
555 * returns true, the iteration will end and the current thread
556 * will be returned. This can be useful for implementing a
557 * search for a thread with arbitrary attributes, or for applying
558 * some operation to every thread.
560 * FIXME: some of the existing functionality, such as
561 * "Thread apply all", might be rewritten using this functionality.
565 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
568 for (thread_info
*tp
: all_threads_safe ())
569 if ((*callback
) (tp
, data
))
575 /* See gdbthread.h. */
580 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
586 thread_count (process_stratum_target
*proc_target
)
588 auto rng
= all_threads (proc_target
);
589 return std::distance (rng
.begin (), rng
.end ());
592 /* Return the number of non-exited threads in the thread list. */
595 live_threads_count (void)
597 auto rng
= all_non_exited_threads ();
598 return std::distance (rng
.begin (), rng
.end ());
602 valid_global_thread_id (int global_id
)
604 for (thread_info
*tp
: all_threads ())
605 if (tp
->global_num
== global_id
)
612 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
614 return find_thread_ptid (targ
, ptid
) != nullptr;
617 /* Finds the first thread of the inferior. */
620 first_thread_of_inferior (inferior
*inf
)
622 if (inf
->thread_list
.empty ())
625 return &inf
->thread_list
.front ();
629 any_thread_of_inferior (inferior
*inf
)
631 gdb_assert (inf
->pid
!= 0);
633 /* Prefer the current thread, if there's one. */
634 if (inf
== current_inferior () && inferior_ptid
!= null_ptid
)
635 return inferior_thread ();
637 for (thread_info
*tp
: inf
->non_exited_threads ())
644 any_live_thread_of_inferior (inferior
*inf
)
646 struct thread_info
*curr_tp
= NULL
;
647 struct thread_info
*tp_executing
= NULL
;
649 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
651 /* Prefer the current thread if it's not executing. */
652 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
654 /* If the current thread is dead, forget it. If it's not
655 executing, use it. Otherwise, still choose it (below), but
656 only if no other non-executing thread is found. */
657 curr_tp
= inferior_thread ();
658 if (curr_tp
->state
== THREAD_EXITED
)
660 else if (!curr_tp
->executing ())
664 for (thread_info
*tp
: inf
->non_exited_threads ())
666 if (!tp
->executing ())
672 /* If both the current thread and all live threads are executing,
673 prefer the current thread. */
677 /* Otherwise, just return an executing thread, if any. */
681 /* Return true if TP is an active thread. */
683 thread_alive (thread_info
*tp
)
685 if (tp
->state
== THREAD_EXITED
)
688 /* Ensure we're looking at the right target stack. */
689 gdb_assert (tp
->inf
== current_inferior ());
691 return target_thread_alive (tp
->ptid
);
694 /* See gdbthreads.h. */
697 switch_to_thread_if_alive (thread_info
*thr
)
699 scoped_restore_current_thread restore_thread
;
701 /* Switch inferior first, so that we're looking at the right target
703 switch_to_inferior_no_thread (thr
->inf
);
705 if (thread_alive (thr
))
707 switch_to_thread (thr
);
708 restore_thread
.dont_restore ();
715 /* See gdbthreads.h. */
720 scoped_restore_current_thread restore_thread
;
722 for (thread_info
*tp
: all_threads_safe ())
724 switch_to_inferior_no_thread (tp
->inf
);
726 if (!thread_alive (tp
))
731 /* See gdbthreads.h. */
734 delete_exited_threads (void)
736 for (thread_info
*tp
: all_threads_safe ())
737 if (tp
->state
== THREAD_EXITED
)
741 /* Return true value if stack temporaries are enabled for the thread
745 thread_stack_temporaries_enabled_p (thread_info
*tp
)
750 return tp
->stack_temporaries_enabled
;
753 /* Push V on to the stack temporaries of the thread with id PTID. */
756 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
758 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
759 tp
->stack_temporaries
.push_back (v
);
762 /* Return true if VAL is among the stack temporaries of the thread
763 TP. Return false otherwise. */
766 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
768 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
769 for (value
*v
: tp
->stack_temporaries
)
776 /* Return the last of the stack temporaries for thread with id PTID.
777 Return NULL if there are no stack temporaries for the thread. */
780 get_last_thread_stack_temporary (thread_info
*tp
)
782 struct value
*lastval
= NULL
;
784 gdb_assert (tp
!= NULL
);
785 if (!tp
->stack_temporaries
.empty ())
786 lastval
= tp
->stack_temporaries
.back ();
792 thread_change_ptid (process_stratum_target
*targ
,
793 ptid_t old_ptid
, ptid_t new_ptid
)
795 struct inferior
*inf
;
796 struct thread_info
*tp
;
798 /* It can happen that what we knew as the target inferior id
799 changes. E.g, target remote may only discover the remote process
800 pid after adding the inferior to GDB's list. */
801 inf
= find_inferior_ptid (targ
, old_ptid
);
802 inf
->pid
= new_ptid
.pid ();
804 tp
= find_thread_ptid (inf
, old_ptid
);
805 gdb_assert (tp
!= nullptr);
807 int num_erased
= inf
->ptid_thread_map
.erase (old_ptid
);
808 gdb_assert (num_erased
== 1);
811 inf
->ptid_thread_map
[new_ptid
] = tp
;
813 gdb::observers::thread_ptid_changed
.notify (targ
, old_ptid
, new_ptid
);
816 /* See gdbthread.h. */
819 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
821 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
822 tp
->set_resumed (resumed
);
825 /* Helper for set_running, that marks one thread either running or
829 set_running_thread (struct thread_info
*tp
, bool running
)
831 bool started
= false;
833 if (running
&& tp
->state
== THREAD_STOPPED
)
835 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
837 threads_debug_printf ("thread: %s, running? %d%s",
838 tp
->ptid
.to_string ().c_str (), running
,
839 (started
? " (started)" : ""));
843 /* If the thread is now marked stopped, remove it from
844 the step-over queue, so that we don't try to resume
845 it until the user wants it to. */
846 if (thread_is_in_step_over_chain (tp
))
847 global_thread_step_over_chain_remove (tp
);
853 /* See gdbthread.h. */
856 thread_info::set_running (bool running
)
858 if (set_running_thread (this, running
))
859 gdb::observers::target_resumed
.notify (this->ptid
);
863 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
865 /* We try not to notify the observer if no thread has actually
866 changed the running state -- merely to reduce the number of
867 messages to the MI frontend. A frontend is supposed to handle
868 multiple *running notifications just fine. */
869 bool any_started
= false;
871 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
872 if (set_running_thread (tp
, running
))
876 gdb::observers::target_resumed
.notify (ptid
);
880 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
882 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
883 tp
->set_executing (executing
);
885 /* It only takes one running thread to spawn more threads. */
887 targ
->threads_executing
= true;
888 /* Only clear the flag if the caller is telling us everything is
890 else if (minus_one_ptid
== ptid
)
891 targ
->threads_executing
= false;
894 /* See gdbthread.h. */
897 threads_are_executing (process_stratum_target
*target
)
899 return target
->threads_executing
;
903 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
905 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
906 tp
->stop_requested
= stop
;
908 /* Call the stop requested observer so other components of GDB can
909 react to this request. */
911 gdb::observers::thread_stop_requested
.notify (ptid
);
915 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
917 bool any_started
= false;
919 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
920 if (set_running_thread (tp
, tp
->executing ()))
924 gdb::observers::target_resumed
.notify (ptid
);
927 /* See gdbthread.h. */
930 validate_registers_access (void)
932 /* No selected thread, no registers. */
933 if (inferior_ptid
== null_ptid
)
934 error (_("No thread selected."));
936 thread_info
*tp
= inferior_thread ();
938 /* Don't try to read from a dead thread. */
939 if (tp
->state
== THREAD_EXITED
)
940 error (_("The current thread has terminated"));
942 /* ... or from a spinning thread. FIXME: This isn't actually fully
943 correct. It'll allow an user-requested access (e.g., "print $pc"
944 at the prompt) when a thread is not executing for some internal
945 reason, but is marked running from the user's perspective. E.g.,
946 the thread is waiting for its turn in the step-over queue. */
947 if (tp
->executing ())
948 error (_("Selected thread is running."));
951 /* See gdbthread.h. */
954 can_access_registers_thread (thread_info
*thread
)
956 /* No thread, no registers. */
960 /* Don't try to read from a dead thread. */
961 if (thread
->state
== THREAD_EXITED
)
964 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
965 if (thread
->executing ())
972 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
974 return (pc
>= thread
->control
.step_range_start
975 && pc
< thread
->control
.step_range_end
);
978 /* Helper for print_thread_info. Returns true if THR should be
979 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
980 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
981 is true if REQUESTED_THREADS is list of global IDs, false if a list
982 of per-inferior thread ids. If PID is not -1, only print THR if it
983 is a thread from the process PID. Otherwise, threads from all
984 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
985 and PID is not -1, then the thread is printed if it belongs to the
986 specified process. Otherwise, an error is raised. */
989 should_print_thread (const char *requested_threads
, int default_inf_num
,
990 int global_ids
, int pid
, struct thread_info
*thr
)
992 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
997 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
999 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1000 thr
->inf
->num
, thr
->per_inf_num
);
1005 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1007 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1008 error (_("Requested thread not found in requested process"));
1012 if (thr
->state
== THREAD_EXITED
)
1018 /* Return the string to display in "info threads"'s "Target Id"
1022 thread_target_id_str (thread_info
*tp
)
1024 std::string target_id
= target_pid_to_str (tp
->ptid
);
1025 const char *extra_info
= target_extra_thread_info (tp
);
1026 const char *name
= thread_name (tp
);
1028 if (extra_info
!= nullptr && name
!= nullptr)
1029 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1031 else if (extra_info
!= nullptr)
1032 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1033 else if (name
!= nullptr)
1034 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1039 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1040 whether REQUESTED_THREADS is a list of global or per-inferior
1044 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1045 int global_ids
, int pid
,
1046 int show_global_ids
)
1048 int default_inf_num
= current_inferior ()->num
;
1050 update_thread_list ();
1052 /* Whether we saw any thread. */
1053 bool any_thread
= false;
1054 /* Whether the current thread is exited. */
1055 bool current_exited
= false;
1057 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1058 ? inferior_thread () : NULL
);
1061 /* For backward compatibility, we make a list for MI. A table is
1062 preferable for the CLI, though, because it shows table
1064 gdb::optional
<ui_out_emit_list
> list_emitter
;
1065 gdb::optional
<ui_out_emit_table
> table_emitter
;
1067 /* We'll be switching threads temporarily below. */
1068 scoped_restore_current_thread restore_thread
;
1070 if (uiout
->is_mi_like_p ())
1071 list_emitter
.emplace (uiout
, "threads");
1075 /* The width of the "Target Id" column. Grown below to
1076 accommodate the largest entry. */
1077 size_t target_id_col_width
= 17;
1079 for (thread_info
*tp
: all_threads ())
1081 if (!should_print_thread (requested_threads
, default_inf_num
,
1082 global_ids
, pid
, tp
))
1085 if (!uiout
->is_mi_like_p ())
1087 /* Switch inferiors so we're looking at the right
1089 switch_to_inferior_no_thread (tp
->inf
);
1092 = std::max (target_id_col_width
,
1093 thread_target_id_str (tp
).size ());
1101 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1102 uiout
->message (_("No threads.\n"));
1104 uiout
->message (_("No threads match '%s'.\n"),
1109 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1110 n_threads
, "threads");
1112 uiout
->table_header (1, ui_left
, "current", "");
1113 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1114 if (show_global_ids
)
1115 uiout
->table_header (4, ui_left
, "id", "GId");
1116 uiout
->table_header (target_id_col_width
, ui_left
,
1117 "target-id", "Target Id");
1118 uiout
->table_header (1, ui_left
, "frame", "Frame");
1119 uiout
->table_body ();
1122 for (inferior
*inf
: all_inferiors ())
1123 for (thread_info
*tp
: inf
->threads ())
1128 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1129 current_exited
= true;
1131 if (!should_print_thread (requested_threads
, default_inf_num
,
1132 global_ids
, pid
, tp
))
1135 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1137 if (!uiout
->is_mi_like_p ())
1139 if (tp
== current_thread
)
1140 uiout
->field_string ("current", "*");
1142 uiout
->field_skip ("current");
1144 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1147 if (show_global_ids
|| uiout
->is_mi_like_p ())
1148 uiout
->field_signed ("id", tp
->global_num
);
1150 /* Switch to the thread (and inferior / target). */
1151 switch_to_thread (tp
);
1153 /* For the CLI, we stuff everything into the target-id field.
1154 This is a gross hack to make the output come out looking
1155 correct. The underlying problem here is that ui-out has no
1156 way to specify that a field's space allocation should be
1157 shared by several fields. For MI, we do the right thing
1160 if (uiout
->is_mi_like_p ())
1162 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1164 const char *extra_info
= target_extra_thread_info (tp
);
1165 if (extra_info
!= nullptr)
1166 uiout
->field_string ("details", extra_info
);
1168 const char *name
= thread_name (tp
);
1170 uiout
->field_string ("name", name
);
1174 uiout
->field_string ("target-id", thread_target_id_str (tp
));
1177 if (tp
->state
== THREAD_RUNNING
)
1178 uiout
->text ("(running)\n");
1181 /* The switch above put us at the top of the stack (leaf
1183 print_stack_frame (get_selected_frame (NULL
),
1184 /* For MI output, print frame level. */
1185 uiout
->is_mi_like_p (),
1189 if (uiout
->is_mi_like_p ())
1191 const char *state
= "stopped";
1193 if (tp
->state
== THREAD_RUNNING
)
1195 uiout
->field_string ("state", state
);
1198 core
= target_core_of_thread (tp
->ptid
);
1199 if (uiout
->is_mi_like_p () && core
!= -1)
1200 uiout
->field_signed ("core", core
);
1203 /* This end scope restores the current thread and the frame
1204 selected before the "info threads" command, and it finishes the
1205 ui-out list or table. */
1208 if (pid
== -1 && requested_threads
== NULL
)
1210 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1211 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1213 if (inferior_ptid
!= null_ptid
&& current_exited
)
1214 uiout
->message ("\n\
1215 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1216 print_thread_id (inferior_thread ()));
1217 else if (any_thread
&& inferior_ptid
== null_ptid
)
1218 uiout
->message ("\n\
1219 No selected thread. See `help thread'.\n");
1223 /* See gdbthread.h. */
1226 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1229 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1232 /* The options for the "info threads" command. */
1234 struct info_threads_opts
1237 bool show_global_ids
= false;
1240 static const gdb::option::option_def info_threads_option_defs
[] = {
1242 gdb::option::flag_option_def
<info_threads_opts
> {
1244 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1245 N_("Show global thread IDs."),
1250 /* Create an option_def_group for the "info threads" options, with
1251 IT_OPTS as context. */
1253 static inline gdb::option::option_def_group
1254 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1256 return {{info_threads_option_defs
}, it_opts
};
1259 /* Implementation of the "info threads" command.
1261 Note: this has the drawback that it _really_ switches
1262 threads, which frees the frame cache. A no-side
1263 effects info-threads command would be nicer. */
1266 info_threads_command (const char *arg
, int from_tty
)
1268 info_threads_opts it_opts
;
1270 auto grp
= make_info_threads_options_def_group (&it_opts
);
1271 gdb::option::process_options
1272 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1274 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1277 /* Completer for the "info threads" command. */
1280 info_threads_command_completer (struct cmd_list_element
*ignore
,
1281 completion_tracker
&tracker
,
1282 const char *text
, const char *word_ignored
)
1284 const auto grp
= make_info_threads_options_def_group (nullptr);
1286 if (gdb::option::complete_options
1287 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1290 /* Convenience to let the user know what the option can accept. */
1293 gdb::option::complete_on_all_options (tracker
, grp
);
1294 /* Keep this "ID" in sync with what "help info threads"
1296 tracker
.add_completion (make_unique_xstrdup ("ID"));
1300 /* See gdbthread.h. */
1303 switch_to_thread_no_regs (struct thread_info
*thread
)
1305 gdb_assert (thread
!= nullptr);
1306 threads_debug_printf ("thread = %s", thread
->ptid
.to_string ().c_str ());
1308 struct inferior
*inf
= thread
->inf
;
1310 set_current_program_space (inf
->pspace
);
1311 set_current_inferior (inf
);
1313 current_thread_
= thread
;
1314 inferior_ptid
= current_thread_
->ptid
;
1317 /* See gdbthread.h. */
1320 switch_to_no_thread ()
1322 if (current_thread_
== nullptr)
1325 threads_debug_printf ("thread = NONE");
1327 current_thread_
= nullptr;
1328 inferior_ptid
= null_ptid
;
1329 reinit_frame_cache ();
1332 /* See gdbthread.h. */
1335 switch_to_thread (thread_info
*thr
)
1337 gdb_assert (thr
!= NULL
);
1339 if (is_current_thread (thr
))
1342 switch_to_thread_no_regs (thr
);
1344 reinit_frame_cache ();
1347 /* See gdbsupport/common-gdbthread.h. */
1350 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1352 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1353 switch_to_thread (thr
);
1359 scoped_restore_current_thread::restore ()
1361 /* If an entry of thread_info was previously selected, it won't be
1362 deleted because we've increased its refcount. The thread represented
1363 by this thread_info entry may have already exited (due to normal exit,
1364 detach, etc), so the thread_info.state is THREAD_EXITED. */
1365 if (m_thread
!= NULL
1366 /* If the previously selected thread belonged to a process that has
1367 in the mean time exited (or killed, detached, etc.), then don't revert
1368 back to it, but instead simply drop back to no thread selected. */
1370 switch_to_thread (m_thread
.get ());
1372 switch_to_inferior_no_thread (m_inf
.get ());
1374 /* The running state of the originally selected thread may have
1375 changed, so we have to recheck it here. */
1376 if (inferior_ptid
!= null_ptid
1378 && m_thread
->state
== THREAD_STOPPED
1379 && target_has_registers ()
1380 && target_has_stack ()
1381 && target_has_memory ())
1382 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1384 set_language (m_lang
);
1387 scoped_restore_current_thread::~scoped_restore_current_thread ()
1389 if (!m_dont_restore
)
1393 scoped_restore_current_thread::scoped_restore_current_thread ()
1395 m_inf
= inferior_ref::new_reference (current_inferior ());
1397 m_lang
= current_language
->la_language
;
1399 if (inferior_ptid
!= null_ptid
)
1401 m_thread
= thread_info_ref::new_reference (inferior_thread ());
1403 m_was_stopped
= m_thread
->state
== THREAD_STOPPED
;
1404 save_selected_frame (&m_selected_frame_id
, &m_selected_frame_level
);
1408 /* See gdbthread.h. */
1411 show_thread_that_caused_stop (void)
1413 return highest_thread_num
> 1;
1416 /* See gdbthread.h. */
1419 show_inferior_qualified_tids (void)
1421 auto inf
= inferior_list
.begin ();
1425 return inf
!= inferior_list
.end ();
1428 /* See gdbthread.h. */
1431 print_thread_id (struct thread_info
*thr
)
1433 char *s
= get_print_cell ();
1435 if (show_inferior_qualified_tids ())
1436 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1438 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1442 /* Sort an array of struct thread_info pointers by thread ID (first by
1443 inferior number, and then by per-inferior thread number). Sorts in
1447 tp_array_compar_ascending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1449 if (a
->inf
->num
!= b
->inf
->num
)
1450 return a
->inf
->num
< b
->inf
->num
;
1452 return (a
->per_inf_num
< b
->per_inf_num
);
1455 /* Sort an array of struct thread_info pointers by thread ID (first by
1456 inferior number, and then by per-inferior thread number). Sorts in
1457 descending order. */
1460 tp_array_compar_descending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1462 if (a
->inf
->num
!= b
->inf
->num
)
1463 return a
->inf
->num
> b
->inf
->num
;
1465 return (a
->per_inf_num
> b
->per_inf_num
);
1468 /* See gdbthread.h. */
1471 thread_try_catch_cmd (thread_info
*thr
, gdb::optional
<int> ada_task
,
1472 const char *cmd
, int from_tty
,
1473 const qcs_flags
&flags
)
1475 gdb_assert (is_current_thread (thr
));
1477 /* The thread header is computed before running the command since
1478 the command can change the inferior, which is not permitted
1479 by thread_target_id_str. */
1480 std::string thr_header
;
1481 if (ada_task
.has_value ())
1482 thr_header
= string_printf (_("\nTask ID %d:\n"), *ada_task
);
1484 thr_header
= string_printf (_("\nThread %s (%s):\n"),
1485 print_thread_id (thr
),
1486 thread_target_id_str (thr
).c_str ());
1490 std::string cmd_result
;
1491 execute_command_to_string
1492 (cmd_result
, cmd
, from_tty
, gdb_stdout
->term_out ());
1493 if (!flags
.silent
|| cmd_result
.length () > 0)
1496 gdb_printf ("%s", thr_header
.c_str ());
1497 gdb_printf ("%s", cmd_result
.c_str ());
1500 catch (const gdb_exception_error
&ex
)
1505 gdb_printf ("%s", thr_header
.c_str ());
1507 gdb_printf ("%s\n", ex
.what ());
1514 /* Option definition of "thread apply"'s "-ascending" option. */
1516 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1519 Call COMMAND for all threads in ascending order.\n\
1520 The default is descending order."),
1523 /* The qcs command line flags for the "thread apply" commands. Keep
1524 this in sync with the "frame apply" commands. */
1526 using qcs_flag_option_def
1527 = gdb::option::flag_option_def
<qcs_flags
>;
1529 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1530 qcs_flag_option_def
{
1531 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1532 N_("Disables printing the thread information."),
1535 qcs_flag_option_def
{
1536 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1537 N_("Print any error raised by COMMAND and continue."),
1540 qcs_flag_option_def
{
1541 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1542 N_("Silently ignore any errors or empty output produced by COMMAND."),
1546 /* Create an option_def_group for the "thread apply all" options, with
1547 ASCENDING and FLAGS as context. */
1549 static inline std::array
<gdb::option::option_def_group
, 2>
1550 make_thread_apply_all_options_def_group (bool *ascending
,
1554 { {ascending_option_def
.def ()}, ascending
},
1555 { {thr_qcs_flags_option_defs
}, flags
},
1559 /* Create an option_def_group for the "thread apply" options, with
1560 FLAGS as context. */
1562 static inline gdb::option::option_def_group
1563 make_thread_apply_options_def_group (qcs_flags
*flags
)
1565 return {{thr_qcs_flags_option_defs
}, flags
};
1568 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1569 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1570 of two numbers separated by a hyphen. Examples:
1572 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1573 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1574 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1577 thread_apply_all_command (const char *cmd
, int from_tty
)
1579 bool ascending
= false;
1582 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1584 gdb::option::process_options
1585 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1587 validate_flags_qcs ("thread apply all", &flags
);
1589 if (cmd
== NULL
|| *cmd
== '\000')
1590 error (_("Please specify a command at the end of 'thread apply all'"));
1592 update_thread_list ();
1594 int tc
= live_threads_count ();
1597 /* Save a copy of the thread list and increment each thread's
1598 refcount while executing the command in the context of each
1599 thread, in case the command is one that wipes threads. E.g.,
1600 detach, kill, disconnect, etc., or even normally continuing
1601 over an inferior or thread exit. */
1602 std::vector
<thread_info_ref
> thr_list_cpy
;
1603 thr_list_cpy
.reserve (tc
);
1605 for (thread_info
*tp
: all_non_exited_threads ())
1606 thr_list_cpy
.push_back (thread_info_ref::new_reference (tp
));
1607 gdb_assert (thr_list_cpy
.size () == tc
);
1609 auto *sorter
= (ascending
1610 ? tp_array_compar_ascending
1611 : tp_array_compar_descending
);
1612 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1614 scoped_restore_current_thread restore_thread
;
1616 for (thread_info_ref
&thr
: thr_list_cpy
)
1617 if (switch_to_thread_if_alive (thr
.get ()))
1618 thread_try_catch_cmd (thr
.get (), {}, cmd
, from_tty
, flags
);
1622 /* Completer for "thread apply [ID list]". */
1625 thread_apply_command_completer (cmd_list_element
*ignore
,
1626 completion_tracker
&tracker
,
1627 const char *text
, const char * /*word*/)
1629 /* Don't leave this to complete_options because there's an early
1631 tracker
.set_use_custom_word_point (true);
1633 tid_range_parser parser
;
1634 parser
.init (text
, current_inferior ()->num
);
1638 while (!parser
.finished ())
1640 int inf_num
, thr_start
, thr_end
;
1642 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1645 if (parser
.in_star_range () || parser
.in_thread_range ())
1646 parser
.skip_range ();
1649 catch (const gdb_exception_error
&ex
)
1651 /* get_tid_range throws if it parses a negative number, for
1652 example. But a seemingly negative number may be the start of
1653 an option instead. */
1656 const char *cmd
= parser
.cur_tok ();
1660 /* No thread ID list yet. */
1664 /* Check if we're past a valid thread ID list already. */
1665 if (parser
.finished ()
1666 && cmd
> text
&& !isspace (cmd
[-1]))
1669 /* We're past the thread ID list, advance word point. */
1670 tracker
.advance_custom_word_point_by (cmd
- text
);
1673 const auto group
= make_thread_apply_options_def_group (nullptr);
1674 if (gdb::option::complete_options
1675 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1678 complete_nested_command_line (tracker
, text
);
1681 /* Completer for "thread apply all". */
1684 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1685 completion_tracker
&tracker
,
1686 const char *text
, const char *word
)
1688 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1690 if (gdb::option::complete_options
1691 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1694 complete_nested_command_line (tracker
, text
);
1697 /* Implementation of the "thread apply" command. */
1700 thread_apply_command (const char *tidlist
, int from_tty
)
1703 const char *cmd
= NULL
;
1704 tid_range_parser parser
;
1706 if (tidlist
== NULL
|| *tidlist
== '\000')
1707 error (_("Please specify a thread ID list"));
1709 parser
.init (tidlist
, current_inferior ()->num
);
1710 while (!parser
.finished ())
1712 int inf_num
, thr_start
, thr_end
;
1714 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1718 cmd
= parser
.cur_tok ();
1720 auto group
= make_thread_apply_options_def_group (&flags
);
1721 gdb::option::process_options
1722 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1724 validate_flags_qcs ("thread apply", &flags
);
1727 error (_("Please specify a command following the thread ID list"));
1729 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1730 invalid_thread_id_error (cmd
);
1732 scoped_restore_current_thread restore_thread
;
1734 parser
.init (tidlist
, current_inferior ()->num
);
1735 while (!parser
.finished ())
1737 struct thread_info
*tp
= NULL
;
1738 struct inferior
*inf
;
1739 int inf_num
, thr_num
;
1741 parser
.get_tid (&inf_num
, &thr_num
);
1742 inf
= find_inferior_id (inf_num
);
1744 tp
= find_thread_id (inf
, thr_num
);
1746 if (parser
.in_star_range ())
1750 warning (_("Unknown inferior %d"), inf_num
);
1751 parser
.skip_range ();
1755 /* No use looking for threads past the highest thread number
1756 the inferior ever had. */
1757 if (thr_num
>= inf
->highest_thread_num
)
1758 parser
.skip_range ();
1760 /* Be quiet about unknown threads numbers. */
1767 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1768 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1770 warning (_("Unknown thread %d"), thr_num
);
1774 if (!switch_to_thread_if_alive (tp
))
1776 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1780 thread_try_catch_cmd (tp
, {}, cmd
, from_tty
, flags
);
1785 /* Implementation of the "taas" command. */
1788 taas_command (const char *cmd
, int from_tty
)
1790 if (cmd
== NULL
|| *cmd
== '\0')
1791 error (_("Please specify a command to apply on all threads"));
1792 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1793 execute_command (expanded
.c_str (), from_tty
);
1796 /* Implementation of the "tfaas" command. */
1799 tfaas_command (const char *cmd
, int from_tty
)
1801 if (cmd
== NULL
|| *cmd
== '\0')
1802 error (_("Please specify a command to apply on all frames of all threads"));
1803 std::string expanded
1804 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1805 execute_command (expanded
.c_str (), from_tty
);
1808 /* Switch to the specified thread, or print the current thread. */
1811 thread_command (const char *tidstr
, int from_tty
)
1815 if (inferior_ptid
== null_ptid
)
1816 error (_("No thread selected"));
1818 if (target_has_stack ())
1820 struct thread_info
*tp
= inferior_thread ();
1822 if (tp
->state
== THREAD_EXITED
)
1823 gdb_printf (_("[Current thread is %s (%s) (exited)]\n"),
1824 print_thread_id (tp
),
1825 target_pid_to_str (inferior_ptid
).c_str ());
1827 gdb_printf (_("[Current thread is %s (%s)]\n"),
1828 print_thread_id (tp
),
1829 target_pid_to_str (inferior_ptid
).c_str ());
1832 error (_("No stack."));
1836 ptid_t previous_ptid
= inferior_ptid
;
1838 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1840 /* Print if the thread has not changed, otherwise an event will
1842 if (inferior_ptid
== previous_ptid
)
1844 print_selected_thread_frame (current_uiout
,
1845 USER_SELECTED_THREAD
1846 | USER_SELECTED_FRAME
);
1850 gdb::observers::user_selected_context_changed
.notify
1851 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1856 /* Implementation of `thread name'. */
1859 thread_name_command (const char *arg
, int from_tty
)
1861 struct thread_info
*info
;
1863 if (inferior_ptid
== null_ptid
)
1864 error (_("No thread selected"));
1866 arg
= skip_spaces (arg
);
1868 info
= inferior_thread ();
1869 info
->set_name (arg
!= nullptr ? make_unique_xstrdup (arg
) : nullptr);
1872 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1875 thread_find_command (const char *arg
, int from_tty
)
1878 unsigned long match
= 0;
1880 if (arg
== NULL
|| *arg
== '\0')
1881 error (_("Command requires an argument."));
1883 tmp
= re_comp (arg
);
1885 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1887 /* We're going to be switching threads. */
1888 scoped_restore_current_thread restore_thread
;
1890 update_thread_list ();
1892 for (thread_info
*tp
: all_threads ())
1894 switch_to_inferior_no_thread (tp
->inf
);
1896 if (tp
->name () != nullptr && re_exec (tp
->name ()))
1898 gdb_printf (_("Thread %s has name '%s'\n"),
1899 print_thread_id (tp
), tp
->name ());
1903 tmp
= target_thread_name (tp
);
1904 if (tmp
!= NULL
&& re_exec (tmp
))
1906 gdb_printf (_("Thread %s has target name '%s'\n"),
1907 print_thread_id (tp
), tmp
);
1911 std::string name
= target_pid_to_str (tp
->ptid
);
1912 if (!name
.empty () && re_exec (name
.c_str ()))
1914 gdb_printf (_("Thread %s has target id '%s'\n"),
1915 print_thread_id (tp
), name
.c_str ());
1919 tmp
= target_extra_thread_info (tp
);
1920 if (tmp
!= NULL
&& re_exec (tmp
))
1922 gdb_printf (_("Thread %s has extra info '%s'\n"),
1923 print_thread_id (tp
), tmp
);
1928 gdb_printf (_("No threads match '%s'\n"), arg
);
1931 /* Print notices when new threads are attached and detached. */
1932 bool print_thread_events
= true;
1934 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1935 struct cmd_list_element
*c
, const char *value
)
1938 _("Printing of thread events is %s.\n"),
1942 /* See gdbthread.h. */
1945 thread_select (const char *tidstr
, thread_info
*tp
)
1947 if (!switch_to_thread_if_alive (tp
))
1948 error (_("Thread ID %s has terminated."), tidstr
);
1950 annotate_thread_changed ();
1952 /* Since the current thread may have changed, see if there is any
1953 exited thread we can now delete. */
1954 delete_exited_threads ();
1957 /* Print thread and frame switch command response. */
1960 print_selected_thread_frame (struct ui_out
*uiout
,
1961 user_selected_what selection
)
1963 struct thread_info
*tp
= inferior_thread ();
1965 if (selection
& USER_SELECTED_THREAD
)
1967 if (uiout
->is_mi_like_p ())
1969 uiout
->field_signed ("new-thread-id",
1970 inferior_thread ()->global_num
);
1974 uiout
->text ("[Switching to thread ");
1975 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
1977 uiout
->text (target_pid_to_str (inferior_ptid
));
1982 if (tp
->state
== THREAD_RUNNING
)
1984 if (selection
& USER_SELECTED_THREAD
)
1985 uiout
->text ("(running)\n");
1987 else if (selection
& USER_SELECTED_FRAME
)
1989 if (selection
& USER_SELECTED_THREAD
)
1992 if (has_stack_frames ())
1993 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
1998 /* Update the 'threads_executing' global based on the threads we know
1999 about right now. This is used by infrun to tell whether we should
2000 pull events out of the current target. */
2003 update_threads_executing (void)
2005 process_stratum_target
*targ
= current_inferior ()->process_target ();
2010 targ
->threads_executing
= false;
2012 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2014 if (!inf
->has_execution ())
2017 /* If the process has no threads, then it must be we have a
2018 process-exit event pending. */
2019 if (inf
->thread_list
.empty ())
2021 targ
->threads_executing
= true;
2025 for (thread_info
*tp
: inf
->non_exited_threads ())
2027 if (tp
->executing ())
2029 targ
->threads_executing
= true;
2037 update_thread_list (void)
2039 target_update_thread_list ();
2040 update_threads_executing ();
2043 /* See gdbthread.h. */
2046 thread_name (thread_info
*thread
)
2048 /* Use the manually set name if there is one. */
2049 const char *name
= thread
->name ();
2050 if (name
!= nullptr)
2053 /* Otherwise, ask the target. Ensure we query the right target stack. */
2054 scoped_restore_current_thread restore_thread
;
2055 if (thread
->inf
!= current_inferior ())
2056 switch_to_inferior_no_thread (thread
->inf
);
2058 return target_thread_name (thread
);
2061 /* See gdbthread.h. */
2064 thread_state_string (enum thread_state state
)
2068 case THREAD_STOPPED
:
2071 case THREAD_RUNNING
:
2078 gdb_assert_not_reached ("unknown thread state");
2081 /* Return a new value for the selected thread's id. Return a value of
2082 0 if no thread is selected. If GLOBAL is true, return the thread's
2083 global number. Otherwise return the per-inferior number. */
2085 static struct value
*
2086 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2090 if (inferior_ptid
== null_ptid
)
2094 thread_info
*tp
= inferior_thread ();
2096 int_val
= tp
->global_num
;
2098 int_val
= tp
->per_inf_num
;
2101 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2104 /* Return a new value for the selected thread's per-inferior thread
2105 number. Return a value of 0 if no thread is selected, or no
2108 static struct value
*
2109 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2110 struct internalvar
*var
,
2113 return thread_num_make_value_helper (gdbarch
, 0);
2116 /* Return a new value for the selected thread's global id. Return a
2117 value of 0 if no thread is selected, or no threads exist. */
2119 static struct value
*
2120 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2123 return thread_num_make_value_helper (gdbarch
, 1);
2126 /* Return a new value for the number of non-exited threads in the current
2127 inferior. If there are no threads in the current inferior return a
2130 static struct value
*
2131 inferior_thread_count_make_value (struct gdbarch
*gdbarch
,
2132 struct internalvar
*var
, void *ignore
)
2136 if (inferior_ptid
!= null_ptid
)
2137 int_val
= current_inferior ()->non_exited_threads ().size ();
2139 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2142 /* Commands with a prefix of `thread'. */
2143 struct cmd_list_element
*thread_cmd_list
= NULL
;
2145 /* Implementation of `thread' variable. */
2147 static const struct internalvar_funcs thread_funcs
=
2149 thread_id_per_inf_num_make_value
,
2153 /* Implementation of `gthread' variable. */
2155 static const struct internalvar_funcs gthread_funcs
=
2157 global_thread_id_make_value
,
2161 /* Implementation of `_inferior_thread_count` convenience variable. */
2163 static const struct internalvar_funcs inferior_thread_count_funcs
=
2165 inferior_thread_count_make_value
,
2169 void _initialize_thread ();
2171 _initialize_thread ()
2173 static struct cmd_list_element
*thread_apply_list
= NULL
;
2174 cmd_list_element
*c
;
2176 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2178 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2180 static std::string info_threads_help
2181 = gdb::option::build_help (_("\
2182 Display currently known threads.\n\
2183 Usage: info threads [OPTION]... [ID]...\n\
2184 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2185 Otherwise, all threads are displayed.\n\
2191 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2192 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2194 cmd_list_element
*thread_cmd
2195 = add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2196 Use this command to switch between threads.\n\
2197 The new thread ID must be currently known."),
2198 &thread_cmd_list
, 1, &cmdlist
);
2200 add_com_alias ("t", thread_cmd
, class_run
, 1);
2202 #define THREAD_APPLY_OPTION_HELP "\
2203 Prints per-inferior thread number and target system's thread id\n\
2204 followed by COMMAND output.\n\
2206 By default, an error raised during the execution of COMMAND\n\
2207 aborts \"thread apply\".\n\
2212 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2214 static std::string thread_apply_help
= gdb::option::build_help (_("\
2215 Apply a command to a list of threads.\n\
2216 Usage: thread apply ID... [OPTION]... COMMAND\n\
2217 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2218 THREAD_APPLY_OPTION_HELP
),
2221 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2222 thread_apply_help
.c_str (),
2223 &thread_apply_list
, 1,
2225 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2227 const auto thread_apply_all_opts
2228 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2230 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2231 Apply a command to all threads.\n\
2233 Usage: thread apply all [OPTION]... COMMAND\n"
2234 THREAD_APPLY_OPTION_HELP
),
2235 thread_apply_all_opts
);
2237 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2238 thread_apply_all_help
.c_str (),
2239 &thread_apply_list
);
2240 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2242 c
= add_com ("taas", class_run
, taas_command
, _("\
2243 Apply a command to all threads (ignoring errors and empty output).\n\
2244 Usage: taas [OPTION]... COMMAND\n\
2245 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2246 See \"help thread apply all\" for available options."));
2247 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2249 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2250 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2251 Usage: tfaas [OPTION]... COMMAND\n\
2252 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2253 See \"help frame apply all\" for available options."));
2254 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2256 add_cmd ("name", class_run
, thread_name_command
,
2257 _("Set the current thread's name.\n\
2258 Usage: thread name [NAME]\n\
2259 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2261 add_cmd ("find", class_run
, thread_find_command
, _("\
2262 Find threads that match a regular expression.\n\
2263 Usage: thread find REGEXP\n\
2264 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2267 add_setshow_boolean_cmd ("thread-events", no_class
,
2268 &print_thread_events
, _("\
2269 Set printing of thread events (such as thread start and exit)."), _("\
2270 Show printing of thread events (such as thread start and exit)."), NULL
,
2272 show_print_thread_events
,
2273 &setprintlist
, &showprintlist
);
2275 add_setshow_boolean_cmd ("threads", class_maintenance
, &debug_threads
, _("\
2276 Set thread debugging."), _("\
2277 Show thread debugging."), _("\
2278 When on messages about thread creation and deletion are printed."),
2281 &setdebuglist
, &showdebuglist
);
2283 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2284 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);
2285 create_internalvar_type_lazy ("_inferior_thread_count",
2286 &inferior_thread_count_funcs
, NULL
);