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/>. */
27 #include "gdbsupport/environ.h"
30 #include "gdbthread.h"
37 #include <sys/types.h>
40 #include "observable.h"
42 #include "cli/cli-decode.h"
43 #include "cli/cli-option.h"
44 #include "gdbsupport/gdb_regex.h"
45 #include "cli/cli-utils.h"
46 #include "thread-fsm.h"
47 #include "tid-parse.h"
49 #include "gdbsupport/gdb_optional.h"
50 #include "inline-frame.h"
54 /* See gdbthread.h. */
56 bool debug_threads
= false;
58 /* Implement 'show debug threads'. */
61 show_debug_threads (struct ui_file
*file
, int from_tty
,
62 struct cmd_list_element
*c
, const char *value
)
64 gdb_printf (file
, _("Thread debugging is \"%s\".\n"), value
);
67 /* Definition of struct thread_info exported to gdbthread.h. */
69 /* Prototypes for local functions. */
71 static int highest_thread_num
;
73 /* The current/selected thread. */
74 static thread_info
*current_thread_
;
76 /* Returns true if THR is the current thread. */
79 is_current_thread (const thread_info
*thr
)
81 return thr
== current_thread_
;
85 inferior_thread (void)
87 gdb_assert (current_thread_
!= nullptr);
88 return current_thread_
;
91 /* Delete the breakpoint pointed at by BP_P, if there's one. */
94 delete_thread_breakpoint (struct breakpoint
**bp_p
)
98 delete_breakpoint (*bp_p
);
104 delete_step_resume_breakpoint (struct thread_info
*tp
)
107 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
111 delete_exception_resume_breakpoint (struct thread_info
*tp
)
114 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
117 /* See gdbthread.h. */
120 delete_single_step_breakpoints (struct thread_info
*tp
)
123 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
126 /* Delete the breakpoint pointed at by BP_P at the next stop, if
130 delete_at_next_stop (struct breakpoint
**bp
)
134 (*bp
)->disposition
= disp_del_at_next_stop
;
139 /* See gdbthread.h. */
142 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
144 return tp
->control
.single_step_breakpoints
!= NULL
;
147 /* See gdbthread.h. */
150 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
151 const address_space
*aspace
,
154 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
156 return (ss_bps
!= NULL
157 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
160 /* See gdbthread.h. */
163 thread_cancel_execution_command (struct thread_info
*thr
)
165 if (thr
->thread_fsm () != nullptr)
167 std::unique_ptr
<thread_fsm
> fsm
= thr
->release_thread_fsm ();
173 clear_thread_inferior_resources (struct thread_info
*tp
)
175 /* NOTE: this will take care of any left-over step_resume breakpoints,
176 but not any user-specified thread-specific breakpoints. We can not
177 delete the breakpoint straight-off, because the inferior might not
178 be stopped at the moment. */
179 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
180 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
181 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
183 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
185 bpstat_clear (&tp
->control
.stop_bpstat
);
187 btrace_teardown (tp
);
189 thread_cancel_execution_command (tp
);
191 clear_inline_frame_state (tp
);
194 /* Notify interpreters and observers that thread T has exited. */
197 notify_thread_exited (thread_info
*t
, int silent
)
199 interps_notify_thread_exited (t
, silent
);
200 gdb::observers::thread_exit
.notify (t
, silent
);
203 /* See gdbthread.h. */
206 set_thread_exited (thread_info
*tp
, bool silent
)
208 /* Dead threads don't need to step-over. Remove from chain. */
209 if (thread_is_in_step_over_chain (tp
))
210 global_thread_step_over_chain_remove (tp
);
212 if (tp
->state
!= THREAD_EXITED
)
214 process_stratum_target
*proc_target
= tp
->inf
->process_target ();
216 /* Some targets unpush themselves from the inferior's target stack before
217 clearing the inferior's thread list (which marks all threads as exited,
218 and therefore leads to this function). In this case, the inferior's
219 process target will be nullptr when we arrive here.
221 See also the comment in inferior::unpush_target. */
222 if (proc_target
!= nullptr)
223 proc_target
->maybe_remove_resumed_with_pending_wait_status (tp
);
225 notify_thread_exited (tp
, silent
);
227 /* Tag it as exited. */
228 tp
->state
= THREAD_EXITED
;
230 /* Clear breakpoints, etc. associated with this thread. */
231 clear_thread_inferior_resources (tp
);
233 /* Remove from the ptid_t map. We don't want for
234 inferior::find_thread to find exited threads. Also, the target
235 may reuse the ptid for a new thread, and there can only be
236 one value per key; adding a new thread with the same ptid_t
237 would overwrite the exited thread's ptid entry. */
238 size_t nr_deleted
= tp
->inf
->ptid_thread_map
.erase (tp
->ptid
);
239 gdb_assert (nr_deleted
== 1);
244 init_thread_list (void)
246 highest_thread_num
= 0;
248 for (inferior
*inf
: all_inferiors ())
249 inf
->clear_thread_list ();
252 /* Allocate a new thread of inferior INF with target id PTID and add
253 it to the thread list. */
255 static struct thread_info
*
256 new_thread (struct inferior
*inf
, ptid_t ptid
)
258 thread_info
*tp
= new thread_info (inf
, ptid
);
260 threads_debug_printf ("creating a new thread object, inferior %d, ptid %s",
261 inf
->num
, ptid
.to_string ().c_str ());
263 inf
->thread_list
.push_back (*tp
);
265 /* A thread with this ptid should not exist in the map yet. */
266 gdb_assert (inf
->ptid_thread_map
.find (ptid
) == inf
->ptid_thread_map
.end ());
268 inf
->ptid_thread_map
[ptid
] = tp
;
273 /* Notify interpreters and observers that thread T has been created. */
276 notify_new_thread (thread_info
*t
)
278 interps_notify_new_thread (t
);
279 gdb::observers::new_thread
.notify (t
);
283 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
285 gdb_assert (targ
!= nullptr);
287 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
289 threads_debug_printf ("add thread to inferior %d, ptid %s, target %s",
290 inf
->num
, ptid
.to_string ().c_str (),
293 /* We may have an old thread with the same id in the thread list.
294 If we do, it must be dead, otherwise we wouldn't be adding a new
295 thread with the same id. The OS is reusing this id --- delete
296 the old thread, and create a new one. */
297 thread_info
*tp
= inf
->find_thread (ptid
);
301 tp
= new_thread (inf
, ptid
);
302 notify_new_thread (tp
);
308 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
309 private_thread_info_up priv
)
311 thread_info
*result
= add_thread_silent (targ
, ptid
);
313 result
->priv
= std::move (priv
);
315 if (print_thread_events
)
316 gdb_printf (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
318 annotate_new_thread ();
323 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
325 return add_thread_with_info (targ
, ptid
, NULL
);
328 private_thread_info::~private_thread_info () = default;
330 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
331 : ptid (ptid_
), inf (inf_
)
333 gdb_assert (inf_
!= NULL
);
335 this->global_num
= ++highest_thread_num
;
336 this->per_inf_num
= ++inf_
->highest_thread_num
;
338 /* Nothing to follow yet. */
339 this->pending_follow
.set_spurious ();
342 /* See gdbthread.h. */
344 thread_info::~thread_info ()
346 threads_debug_printf ("thread %s", this->ptid
.to_string ().c_str ());
349 /* See gdbthread.h. */
352 thread_info::deletable () const
354 /* If this is the current thread, or there's code out there that
355 relies on it existing (refcount > 0) we can't delete yet. */
356 return refcount () == 0 && !is_current_thread (this);
359 /* See gdbthread.h. */
362 thread_info::set_executing (bool executing
)
364 m_executing
= executing
;
366 this->clear_stop_pc ();
369 /* See gdbthread.h. */
372 thread_info::set_resumed (bool resumed
)
374 if (resumed
== m_resumed
)
377 process_stratum_target
*proc_target
= this->inf
->process_target ();
379 /* If we transition from resumed to not resumed, we might need to remove
380 the thread from the resumed threads with pending statuses list. */
382 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
386 /* If we transition from not resumed to resumed, we might need to add
387 the thread to the resumed threads with pending statuses list. */
389 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
392 /* See gdbthread.h. */
395 thread_info::set_pending_waitstatus (const target_waitstatus
&ws
)
397 gdb_assert (!this->has_pending_waitstatus ());
399 m_suspend
.waitstatus
= ws
;
400 m_suspend
.waitstatus_pending_p
= 1;
402 process_stratum_target
*proc_target
= this->inf
->process_target ();
403 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
406 /* See gdbthread.h. */
409 thread_info::clear_pending_waitstatus ()
411 gdb_assert (this->has_pending_waitstatus ());
413 process_stratum_target
*proc_target
= this->inf
->process_target ();
414 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
416 m_suspend
.waitstatus_pending_p
= 0;
419 /* See gdbthread.h. */
422 thread_is_in_step_over_chain (struct thread_info
*tp
)
424 return tp
->step_over_list_node
.is_linked ();
427 /* See gdbthread.h. */
430 thread_step_over_chain_length (const thread_step_over_list
&l
)
434 for (const thread_info
&thread ATTRIBUTE_UNUSED
: l
)
440 /* See gdbthread.h. */
443 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
445 infrun_debug_printf ("enqueueing thread %s in global step over chain",
446 tp
->ptid
.to_string ().c_str ());
448 gdb_assert (!thread_is_in_step_over_chain (tp
));
449 global_thread_step_over_list
.push_back (*tp
);
452 /* See gdbthread.h. */
455 global_thread_step_over_chain_enqueue_chain (thread_step_over_list
&&list
)
457 global_thread_step_over_list
.splice (std::move (list
));
460 /* See gdbthread.h. */
463 global_thread_step_over_chain_remove (struct thread_info
*tp
)
465 infrun_debug_printf ("removing thread %s from global step over chain",
466 tp
->ptid
.to_string ().c_str ());
468 gdb_assert (thread_is_in_step_over_chain (tp
));
469 auto it
= global_thread_step_over_list
.iterator_to (*tp
);
470 global_thread_step_over_list
.erase (it
);
473 /* Delete the thread referenced by THR. If SILENT, don't notify
474 the observer of this exit.
476 THR must not be NULL or a failed assertion will be raised. */
479 delete_thread_1 (thread_info
*thr
, bool silent
)
481 gdb_assert (thr
!= nullptr);
483 threads_debug_printf ("deleting thread %s, silent = %d",
484 thr
->ptid
.to_string ().c_str (), silent
);
486 set_thread_exited (thr
, silent
);
488 if (!thr
->deletable ())
490 /* Will be really deleted some other time. */
494 auto it
= thr
->inf
->thread_list
.iterator_to (*thr
);
495 thr
->inf
->thread_list
.erase (it
);
500 /* See gdbthread.h. */
503 delete_thread (thread_info
*thread
)
505 delete_thread_1 (thread
, false /* not silent */);
509 delete_thread_silent (thread_info
*thread
)
511 delete_thread_1 (thread
, true /* silent */);
515 find_thread_global_id (int global_id
)
517 for (thread_info
*tp
: all_threads ())
518 if (tp
->global_num
== global_id
)
524 static struct thread_info
*
525 find_thread_id (struct inferior
*inf
, int thr_num
)
527 for (thread_info
*tp
: inf
->threads ())
528 if (tp
->per_inf_num
== thr_num
)
534 /* See gdbthread.h. */
537 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
538 struct inferior
*inf
)
540 return target_thread_handle_to_thread_info (handle
.data (),
546 * Thread iterator function.
548 * Calls a callback function once for each thread, so long as
549 * the callback function returns false. If the callback function
550 * returns true, the iteration will end and the current thread
551 * will be returned. This can be useful for implementing a
552 * search for a thread with arbitrary attributes, or for applying
553 * some operation to every thread.
555 * FIXME: some of the existing functionality, such as
556 * "Thread apply all", might be rewritten using this functionality.
560 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
563 for (thread_info
*tp
: all_threads_safe ())
564 if ((*callback
) (tp
, data
))
570 /* See gdbthread.h. */
575 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
581 thread_count (process_stratum_target
*proc_target
)
583 auto rng
= all_threads (proc_target
);
584 return std::distance (rng
.begin (), rng
.end ());
587 /* Return the number of non-exited threads in the thread list. */
590 live_threads_count (void)
592 auto rng
= all_non_exited_threads ();
593 return std::distance (rng
.begin (), rng
.end ());
597 valid_global_thread_id (int global_id
)
599 for (thread_info
*tp
: all_threads ())
600 if (tp
->global_num
== global_id
)
607 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
609 return targ
->find_thread (ptid
) != nullptr;
612 /* Finds the first thread of the inferior. */
615 first_thread_of_inferior (inferior
*inf
)
617 if (inf
->thread_list
.empty ())
620 return &inf
->thread_list
.front ();
624 any_thread_of_inferior (inferior
*inf
)
626 gdb_assert (inf
->pid
!= 0);
628 /* Prefer the current thread, if there's one. */
629 if (inf
== current_inferior () && inferior_ptid
!= null_ptid
)
630 return inferior_thread ();
632 for (thread_info
*tp
: inf
->non_exited_threads ())
639 any_live_thread_of_inferior (inferior
*inf
)
641 struct thread_info
*curr_tp
= NULL
;
642 struct thread_info
*tp_executing
= NULL
;
644 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
646 /* Prefer the current thread if it's not executing. */
647 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
649 /* If the current thread is dead, forget it. If it's not
650 executing, use it. Otherwise, still choose it (below), but
651 only if no other non-executing thread is found. */
652 curr_tp
= inferior_thread ();
653 if (curr_tp
->state
== THREAD_EXITED
)
655 else if (!curr_tp
->executing ())
659 for (thread_info
*tp
: inf
->non_exited_threads ())
661 if (!tp
->executing ())
667 /* If both the current thread and all live threads are executing,
668 prefer the current thread. */
672 /* Otherwise, just return an executing thread, if any. */
676 /* Return true if TP is an active thread. */
678 thread_alive (thread_info
*tp
)
680 if (tp
->state
== THREAD_EXITED
)
683 /* Ensure we're looking at the right target stack. */
684 gdb_assert (tp
->inf
== current_inferior ());
686 return target_thread_alive (tp
->ptid
);
689 /* See gdbthreads.h. */
692 switch_to_thread_if_alive (thread_info
*thr
)
694 scoped_restore_current_thread restore_thread
;
696 /* Switch inferior first, so that we're looking at the right target
698 switch_to_inferior_no_thread (thr
->inf
);
700 if (thread_alive (thr
))
702 switch_to_thread (thr
);
703 restore_thread
.dont_restore ();
710 /* See gdbthreads.h. */
715 scoped_restore_current_thread restore_thread
;
717 for (thread_info
*tp
: all_threads_safe ())
719 switch_to_inferior_no_thread (tp
->inf
);
721 if (!thread_alive (tp
))
726 /* See gdbthreads.h. */
729 delete_exited_threads (void)
731 for (thread_info
*tp
: all_threads_safe ())
732 if (tp
->state
== THREAD_EXITED
)
736 /* Return true value if stack temporaries are enabled for the thread
740 thread_stack_temporaries_enabled_p (thread_info
*tp
)
745 return tp
->stack_temporaries_enabled
;
748 /* Push V on to the stack temporaries of the thread with id PTID. */
751 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
753 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
754 tp
->stack_temporaries
.push_back (v
);
757 /* Return true if VAL is among the stack temporaries of the thread
758 TP. Return false otherwise. */
761 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
763 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
764 for (value
*v
: tp
->stack_temporaries
)
771 /* Return the last of the stack temporaries for thread with id PTID.
772 Return NULL if there are no stack temporaries for the thread. */
775 get_last_thread_stack_temporary (thread_info
*tp
)
777 struct value
*lastval
= NULL
;
779 gdb_assert (tp
!= NULL
);
780 if (!tp
->stack_temporaries
.empty ())
781 lastval
= tp
->stack_temporaries
.back ();
787 thread_change_ptid (process_stratum_target
*targ
,
788 ptid_t old_ptid
, ptid_t new_ptid
)
790 struct inferior
*inf
;
791 struct thread_info
*tp
;
793 /* It can happen that what we knew as the target inferior id
794 changes. E.g, target remote may only discover the remote process
795 pid after adding the inferior to GDB's list. */
796 inf
= find_inferior_ptid (targ
, old_ptid
);
797 inf
->pid
= new_ptid
.pid ();
799 tp
= inf
->find_thread (old_ptid
);
800 gdb_assert (tp
!= nullptr);
802 int num_erased
= inf
->ptid_thread_map
.erase (old_ptid
);
803 gdb_assert (num_erased
== 1);
806 inf
->ptid_thread_map
[new_ptid
] = tp
;
808 gdb::observers::thread_ptid_changed
.notify (targ
, old_ptid
, new_ptid
);
811 /* See gdbthread.h. */
814 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
816 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
817 tp
->set_resumed (resumed
);
820 /* Helper for set_running, that marks one thread either running or
824 set_running_thread (struct thread_info
*tp
, bool running
)
826 bool started
= false;
828 if (running
&& tp
->state
== THREAD_STOPPED
)
830 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
832 threads_debug_printf ("thread: %s, running? %d%s",
833 tp
->ptid
.to_string ().c_str (), running
,
834 (started
? " (started)" : ""));
838 /* If the thread is now marked stopped, remove it from
839 the step-over queue, so that we don't try to resume
840 it until the user wants it to. */
841 if (thread_is_in_step_over_chain (tp
))
842 global_thread_step_over_chain_remove (tp
);
848 /* Notify interpreters and observers that the target was resumed. */
851 notify_target_resumed (ptid_t ptid
)
853 interps_notify_target_resumed (ptid
);
854 gdb::observers::target_resumed
.notify (ptid
);
857 /* See gdbthread.h. */
860 thread_info::set_running (bool running
)
862 if (set_running_thread (this, running
))
863 notify_target_resumed (this->ptid
);
867 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
869 /* We try not to notify the observer if no thread has actually
870 changed the running state -- merely to reduce the number of
871 messages to the MI frontend. A frontend is supposed to handle
872 multiple *running notifications just fine. */
873 bool any_started
= false;
875 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
876 if (set_running_thread (tp
, running
))
880 notify_target_resumed (ptid
);
884 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
886 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
887 tp
->set_executing (executing
);
889 /* It only takes one running thread to spawn more threads. */
891 targ
->threads_executing
= true;
892 /* Only clear the flag if the caller is telling us everything is
894 else if (minus_one_ptid
== ptid
)
895 targ
->threads_executing
= false;
898 /* See gdbthread.h. */
901 threads_are_executing (process_stratum_target
*target
)
903 return target
->threads_executing
;
907 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
909 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
910 tp
->stop_requested
= stop
;
912 /* Call the stop requested observer so other components of GDB can
913 react to this request. */
915 gdb::observers::thread_stop_requested
.notify (ptid
);
919 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
921 bool any_started
= false;
923 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
924 if (set_running_thread (tp
, tp
->executing ()))
928 notify_target_resumed (ptid
);
931 /* See gdbthread.h. */
934 validate_registers_access (void)
936 /* No selected thread, no registers. */
937 if (inferior_ptid
== null_ptid
)
938 error (_("No thread selected."));
940 thread_info
*tp
= inferior_thread ();
942 /* Don't try to read from a dead thread. */
943 if (tp
->state
== THREAD_EXITED
)
944 error (_("The current thread has terminated"));
946 /* ... or from a spinning thread. FIXME: This isn't actually fully
947 correct. It'll allow an user-requested access (e.g., "print $pc"
948 at the prompt) when a thread is not executing for some internal
949 reason, but is marked running from the user's perspective. E.g.,
950 the thread is waiting for its turn in the step-over queue. */
951 if (tp
->executing ())
952 error (_("Selected thread is running."));
955 /* See gdbthread.h. */
958 can_access_registers_thread (thread_info
*thread
)
960 /* No thread, no registers. */
964 /* Don't try to read from a dead thread. */
965 if (thread
->state
== THREAD_EXITED
)
968 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
969 if (thread
->executing ())
976 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
978 return (pc
>= thread
->control
.step_range_start
979 && pc
< thread
->control
.step_range_end
);
982 /* Helper for print_thread_info. Returns true if THR should be
983 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
984 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
985 is true if REQUESTED_THREADS is list of global IDs, false if a list
986 of per-inferior thread ids. If PID is not -1, only print THR if it
987 is a thread from the process PID. Otherwise, threads from all
988 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
989 and PID is not -1, then the thread is printed if it belongs to the
990 specified process. Otherwise, an error is raised. */
993 should_print_thread (const char *requested_threads
, int default_inf_num
,
994 int global_ids
, int pid
, struct thread_info
*thr
)
996 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1001 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1003 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1004 thr
->inf
->num
, thr
->per_inf_num
);
1009 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1011 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1012 error (_("Requested thread not found in requested process"));
1016 if (thr
->state
== THREAD_EXITED
)
1022 /* Return the string to display in "info threads"'s "Target Id"
1026 thread_target_id_str (thread_info
*tp
)
1028 std::string target_id
= target_pid_to_str (tp
->ptid
);
1029 const char *extra_info
= target_extra_thread_info (tp
);
1030 const char *name
= thread_name (tp
);
1032 if (extra_info
!= nullptr && name
!= nullptr)
1033 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1035 else if (extra_info
!= nullptr)
1036 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1037 else if (name
!= nullptr)
1038 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1043 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1044 whether REQUESTED_THREADS is a list of global or per-inferior
1048 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1049 int global_ids
, int pid
,
1050 int show_global_ids
)
1052 int default_inf_num
= current_inferior ()->num
;
1054 update_thread_list ();
1056 /* Whether we saw any thread. */
1057 bool any_thread
= false;
1058 /* Whether the current thread is exited. */
1059 bool current_exited
= false;
1061 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1062 ? inferior_thread () : NULL
);
1065 /* For backward compatibility, we make a list for MI. A table is
1066 preferable for the CLI, though, because it shows table
1068 gdb::optional
<ui_out_emit_list
> list_emitter
;
1069 gdb::optional
<ui_out_emit_table
> table_emitter
;
1071 /* We'll be switching threads temporarily below. */
1072 scoped_restore_current_thread restore_thread
;
1074 if (uiout
->is_mi_like_p ())
1075 list_emitter
.emplace (uiout
, "threads");
1079 /* The width of the "Target Id" column. Grown below to
1080 accommodate the largest entry. */
1081 size_t target_id_col_width
= 17;
1083 for (thread_info
*tp
: all_threads ())
1085 if (!should_print_thread (requested_threads
, default_inf_num
,
1086 global_ids
, pid
, tp
))
1089 /* Switch inferiors so we're looking at the right
1091 switch_to_inferior_no_thread (tp
->inf
);
1094 = std::max (target_id_col_width
,
1095 thread_target_id_str (tp
).size ());
1102 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1103 uiout
->message (_("No threads.\n"));
1105 uiout
->message (_("No threads match '%s'.\n"),
1110 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1111 n_threads
, "threads");
1113 uiout
->table_header (1, ui_left
, "current", "");
1114 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1115 if (show_global_ids
)
1116 uiout
->table_header (4, ui_left
, "id", "GId");
1117 uiout
->table_header (target_id_col_width
, ui_left
,
1118 "target-id", "Target Id");
1119 uiout
->table_header (1, ui_left
, "frame", "Frame");
1120 uiout
->table_body ();
1123 for (inferior
*inf
: all_inferiors ())
1124 for (thread_info
*tp
: inf
->threads ())
1129 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1130 current_exited
= true;
1132 if (!should_print_thread (requested_threads
, default_inf_num
,
1133 global_ids
, pid
, tp
))
1136 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1138 if (!uiout
->is_mi_like_p ())
1140 if (tp
== current_thread
)
1141 uiout
->field_string ("current", "*");
1143 uiout
->field_skip ("current");
1145 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1148 if (show_global_ids
|| uiout
->is_mi_like_p ())
1149 uiout
->field_signed ("id", tp
->global_num
);
1151 /* Switch to the thread (and inferior / target). */
1152 switch_to_thread (tp
);
1154 /* For the CLI, we stuff everything into the target-id field.
1155 This is a gross hack to make the output come out looking
1156 correct. The underlying problem here is that ui-out has no
1157 way to specify that a field's space allocation should be
1158 shared by several fields. For MI, we do the right thing
1161 if (uiout
->is_mi_like_p ())
1163 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1165 const char *extra_info
= target_extra_thread_info (tp
);
1166 if (extra_info
!= nullptr)
1167 uiout
->field_string ("details", extra_info
);
1169 const char *name
= thread_name (tp
);
1171 uiout
->field_string ("name", name
);
1175 uiout
->field_string ("target-id", thread_target_id_str (tp
));
1178 if (tp
->state
== THREAD_RUNNING
)
1179 uiout
->text ("(running)\n");
1182 /* The switch above put us at the top of the stack (leaf
1184 print_stack_frame (get_selected_frame (NULL
),
1185 /* For MI output, print frame level. */
1186 uiout
->is_mi_like_p (),
1190 if (uiout
->is_mi_like_p ())
1192 const char *state
= "stopped";
1194 if (tp
->state
== THREAD_RUNNING
)
1196 uiout
->field_string ("state", state
);
1199 core
= target_core_of_thread (tp
->ptid
);
1200 if (uiout
->is_mi_like_p () && core
!= -1)
1201 uiout
->field_signed ("core", core
);
1204 /* This end scope restores the current thread and the frame
1205 selected before the "info threads" command, and it finishes the
1206 ui-out list or table. */
1209 if (pid
== -1 && requested_threads
== NULL
)
1211 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1212 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1214 if (inferior_ptid
!= null_ptid
&& current_exited
)
1215 uiout
->message ("\n\
1216 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1217 print_thread_id (inferior_thread ()));
1218 else if (any_thread
&& inferior_ptid
== null_ptid
)
1219 uiout
->message ("\n\
1220 No selected thread. See `help thread'.\n");
1224 /* See gdbthread.h. */
1227 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1230 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1233 /* The options for the "info threads" command. */
1235 struct info_threads_opts
1238 bool show_global_ids
= false;
1241 static const gdb::option::option_def info_threads_option_defs
[] = {
1243 gdb::option::flag_option_def
<info_threads_opts
> {
1245 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1246 N_("Show global thread IDs."),
1251 /* Create an option_def_group for the "info threads" options, with
1252 IT_OPTS as context. */
1254 static inline gdb::option::option_def_group
1255 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1257 return {{info_threads_option_defs
}, it_opts
};
1260 /* Implementation of the "info threads" command.
1262 Note: this has the drawback that it _really_ switches
1263 threads, which frees the frame cache. A no-side
1264 effects info-threads command would be nicer. */
1267 info_threads_command (const char *arg
, int from_tty
)
1269 info_threads_opts it_opts
;
1271 auto grp
= make_info_threads_options_def_group (&it_opts
);
1272 gdb::option::process_options
1273 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1275 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1278 /* Completer for the "info threads" command. */
1281 info_threads_command_completer (struct cmd_list_element
*ignore
,
1282 completion_tracker
&tracker
,
1283 const char *text
, const char *word_ignored
)
1285 const auto grp
= make_info_threads_options_def_group (nullptr);
1287 if (gdb::option::complete_options
1288 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1291 /* Convenience to let the user know what the option can accept. */
1294 gdb::option::complete_on_all_options (tracker
, grp
);
1295 /* Keep this "ID" in sync with what "help info threads"
1297 tracker
.add_completion (make_unique_xstrdup ("ID"));
1301 /* See gdbthread.h. */
1304 switch_to_thread_no_regs (struct thread_info
*thread
)
1306 gdb_assert (thread
!= nullptr);
1307 threads_debug_printf ("thread = %s", thread
->ptid
.to_string ().c_str ());
1309 struct inferior
*inf
= thread
->inf
;
1311 set_current_program_space (inf
->pspace
);
1312 set_current_inferior (inf
);
1314 current_thread_
= thread
;
1315 inferior_ptid
= current_thread_
->ptid
;
1318 /* See gdbthread.h. */
1321 switch_to_no_thread ()
1323 if (current_thread_
== nullptr)
1326 threads_debug_printf ("thread = NONE");
1328 current_thread_
= nullptr;
1329 inferior_ptid
= null_ptid
;
1330 reinit_frame_cache ();
1333 /* See gdbthread.h. */
1336 switch_to_thread (thread_info
*thr
)
1338 gdb_assert (thr
!= NULL
);
1340 if (is_current_thread (thr
))
1343 switch_to_thread_no_regs (thr
);
1345 reinit_frame_cache ();
1348 /* See gdbsupport/common-gdbthread.h. */
1351 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1353 thread_info
*thr
= proc_target
->find_thread (ptid
);
1354 switch_to_thread (thr
);
1360 scoped_restore_current_thread::restore ()
1362 /* If an entry of thread_info was previously selected, it won't be
1363 deleted because we've increased its refcount. The thread represented
1364 by this thread_info entry may have already exited (due to normal exit,
1365 detach, etc), so the thread_info.state is THREAD_EXITED. */
1366 if (m_thread
!= NULL
1367 /* If the previously selected thread belonged to a process that has
1368 in the mean time exited (or killed, detached, etc.), then don't revert
1369 back to it, but instead simply drop back to no thread selected. */
1371 switch_to_thread (m_thread
.get ());
1373 switch_to_inferior_no_thread (m_inf
.get ());
1375 /* The running state of the originally selected thread may have
1376 changed, so we have to recheck it here. */
1377 if (inferior_ptid
!= null_ptid
1379 && m_thread
->state
== THREAD_STOPPED
1380 && target_has_registers ()
1381 && target_has_stack ()
1382 && target_has_memory ())
1383 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1385 set_language (m_lang
);
1388 scoped_restore_current_thread::~scoped_restore_current_thread ()
1390 if (!m_dont_restore
)
1394 scoped_restore_current_thread::scoped_restore_current_thread ()
1396 m_inf
= inferior_ref::new_reference (current_inferior ());
1398 m_lang
= current_language
->la_language
;
1400 if (inferior_ptid
!= null_ptid
)
1402 m_thread
= thread_info_ref::new_reference (inferior_thread ());
1404 m_was_stopped
= m_thread
->state
== THREAD_STOPPED
;
1405 save_selected_frame (&m_selected_frame_id
, &m_selected_frame_level
);
1409 scoped_restore_current_thread::scoped_restore_current_thread
1410 (scoped_restore_current_thread
&&rhs
)
1411 : m_dont_restore (std::move (rhs
.m_dont_restore
)),
1412 m_thread (std::move (rhs
.m_thread
)),
1413 m_inf (std::move (rhs
.m_inf
)),
1414 m_selected_frame_id (std::move (rhs
.m_selected_frame_id
)),
1415 m_selected_frame_level (std::move (rhs
.m_selected_frame_level
)),
1416 m_was_stopped (std::move (rhs
.m_was_stopped
)),
1417 m_lang (std::move (rhs
.m_lang
))
1419 /* Deactivate the rhs. */
1420 rhs
.m_dont_restore
= true;
1423 /* See gdbthread.h. */
1426 show_thread_that_caused_stop (void)
1428 return highest_thread_num
> 1;
1431 /* See gdbthread.h. */
1434 show_inferior_qualified_tids (void)
1436 auto inf
= inferior_list
.begin ();
1440 return inf
!= inferior_list
.end ();
1443 /* See gdbthread.h. */
1446 print_thread_id (struct thread_info
*thr
)
1448 if (show_inferior_qualified_tids ())
1449 return print_full_thread_id (thr
);
1451 char *s
= get_print_cell ();
1453 gdb_assert (thr
!= nullptr);
1454 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1458 /* See gdbthread.h. */
1461 print_full_thread_id (struct thread_info
*thr
)
1463 char *s
= get_print_cell ();
1465 gdb_assert (thr
!= nullptr);
1466 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1470 /* Sort an array of struct thread_info pointers by thread ID (first by
1471 inferior number, and then by per-inferior thread number). Sorts in
1475 tp_array_compar_ascending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1477 if (a
->inf
->num
!= b
->inf
->num
)
1478 return a
->inf
->num
< b
->inf
->num
;
1480 return (a
->per_inf_num
< b
->per_inf_num
);
1483 /* Sort an array of struct thread_info pointers by thread ID (first by
1484 inferior number, and then by per-inferior thread number). Sorts in
1485 descending order. */
1488 tp_array_compar_descending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1490 if (a
->inf
->num
!= b
->inf
->num
)
1491 return a
->inf
->num
> b
->inf
->num
;
1493 return (a
->per_inf_num
> b
->per_inf_num
);
1496 /* See gdbthread.h. */
1499 thread_try_catch_cmd (thread_info
*thr
, gdb::optional
<int> ada_task
,
1500 const char *cmd
, int from_tty
,
1501 const qcs_flags
&flags
)
1503 gdb_assert (is_current_thread (thr
));
1505 /* The thread header is computed before running the command since
1506 the command can change the inferior, which is not permitted
1507 by thread_target_id_str. */
1508 std::string thr_header
;
1509 if (ada_task
.has_value ())
1510 thr_header
= string_printf (_("\nTask ID %d:\n"), *ada_task
);
1512 thr_header
= string_printf (_("\nThread %s (%s):\n"),
1513 print_thread_id (thr
),
1514 thread_target_id_str (thr
).c_str ());
1518 std::string cmd_result
;
1519 execute_command_to_string
1520 (cmd_result
, cmd
, from_tty
, gdb_stdout
->term_out ());
1521 if (!flags
.silent
|| cmd_result
.length () > 0)
1524 gdb_printf ("%s", thr_header
.c_str ());
1525 gdb_printf ("%s", cmd_result
.c_str ());
1528 catch (const gdb_exception_error
&ex
)
1533 gdb_printf ("%s", thr_header
.c_str ());
1535 gdb_printf ("%s\n", ex
.what ());
1542 /* Option definition of "thread apply"'s "-ascending" option. */
1544 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1547 Call COMMAND for all threads in ascending order.\n\
1548 The default is descending order."),
1551 /* The qcs command line flags for the "thread apply" commands. Keep
1552 this in sync with the "frame apply" commands. */
1554 using qcs_flag_option_def
1555 = gdb::option::flag_option_def
<qcs_flags
>;
1557 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1558 qcs_flag_option_def
{
1559 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1560 N_("Disables printing the thread information."),
1563 qcs_flag_option_def
{
1564 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1565 N_("Print any error raised by COMMAND and continue."),
1568 qcs_flag_option_def
{
1569 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1570 N_("Silently ignore any errors or empty output produced by COMMAND."),
1574 /* Create an option_def_group for the "thread apply all" options, with
1575 ASCENDING and FLAGS as context. */
1577 static inline std::array
<gdb::option::option_def_group
, 2>
1578 make_thread_apply_all_options_def_group (bool *ascending
,
1582 { {ascending_option_def
.def ()}, ascending
},
1583 { {thr_qcs_flags_option_defs
}, flags
},
1587 /* Create an option_def_group for the "thread apply" options, with
1588 FLAGS as context. */
1590 static inline gdb::option::option_def_group
1591 make_thread_apply_options_def_group (qcs_flags
*flags
)
1593 return {{thr_qcs_flags_option_defs
}, flags
};
1596 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1597 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1598 of two numbers separated by a hyphen. Examples:
1600 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1601 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1602 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1605 thread_apply_all_command (const char *cmd
, int from_tty
)
1607 bool ascending
= false;
1610 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1612 gdb::option::process_options
1613 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1615 validate_flags_qcs ("thread apply all", &flags
);
1617 if (cmd
== NULL
|| *cmd
== '\000')
1618 error (_("Please specify a command at the end of 'thread apply all'"));
1620 update_thread_list ();
1622 int tc
= live_threads_count ();
1625 /* Save a copy of the thread list and increment each thread's
1626 refcount while executing the command in the context of each
1627 thread, in case the command is one that wipes threads. E.g.,
1628 detach, kill, disconnect, etc., or even normally continuing
1629 over an inferior or thread exit. */
1630 std::vector
<thread_info_ref
> thr_list_cpy
;
1631 thr_list_cpy
.reserve (tc
);
1633 for (thread_info
*tp
: all_non_exited_threads ())
1634 thr_list_cpy
.push_back (thread_info_ref::new_reference (tp
));
1635 gdb_assert (thr_list_cpy
.size () == tc
);
1637 auto *sorter
= (ascending
1638 ? tp_array_compar_ascending
1639 : tp_array_compar_descending
);
1640 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1642 scoped_restore_current_thread restore_thread
;
1644 for (thread_info_ref
&thr
: thr_list_cpy
)
1645 if (switch_to_thread_if_alive (thr
.get ()))
1646 thread_try_catch_cmd (thr
.get (), {}, cmd
, from_tty
, flags
);
1650 /* Completer for "thread apply [ID list]". */
1653 thread_apply_command_completer (cmd_list_element
*ignore
,
1654 completion_tracker
&tracker
,
1655 const char *text
, const char * /*word*/)
1657 /* Don't leave this to complete_options because there's an early
1659 tracker
.set_use_custom_word_point (true);
1661 tid_range_parser parser
;
1662 parser
.init (text
, current_inferior ()->num
);
1666 while (!parser
.finished ())
1668 int inf_num
, thr_start
, thr_end
;
1670 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1673 if (parser
.in_star_range () || parser
.in_thread_range ())
1674 parser
.skip_range ();
1677 catch (const gdb_exception_error
&ex
)
1679 /* get_tid_range throws if it parses a negative number, for
1680 example. But a seemingly negative number may be the start of
1681 an option instead. */
1684 const char *cmd
= parser
.cur_tok ();
1688 /* No thread ID list yet. */
1692 /* Check if we're past a valid thread ID list already. */
1693 if (parser
.finished ()
1694 && cmd
> text
&& !isspace (cmd
[-1]))
1697 /* We're past the thread ID list, advance word point. */
1698 tracker
.advance_custom_word_point_by (cmd
- text
);
1701 const auto group
= make_thread_apply_options_def_group (nullptr);
1702 if (gdb::option::complete_options
1703 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1706 complete_nested_command_line (tracker
, text
);
1709 /* Completer for "thread apply all". */
1712 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1713 completion_tracker
&tracker
,
1714 const char *text
, const char *word
)
1716 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1718 if (gdb::option::complete_options
1719 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1722 complete_nested_command_line (tracker
, text
);
1725 /* Implementation of the "thread apply" command. */
1728 thread_apply_command (const char *tidlist
, int from_tty
)
1731 const char *cmd
= NULL
;
1732 tid_range_parser parser
;
1734 if (tidlist
== NULL
|| *tidlist
== '\000')
1735 error (_("Please specify a thread ID list"));
1737 parser
.init (tidlist
, current_inferior ()->num
);
1738 while (!parser
.finished ())
1740 int inf_num
, thr_start
, thr_end
;
1742 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1746 cmd
= parser
.cur_tok ();
1748 auto group
= make_thread_apply_options_def_group (&flags
);
1749 gdb::option::process_options
1750 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1752 validate_flags_qcs ("thread apply", &flags
);
1755 error (_("Please specify a command following the thread ID list"));
1757 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1758 invalid_thread_id_error (cmd
);
1760 scoped_restore_current_thread restore_thread
;
1762 parser
.init (tidlist
, current_inferior ()->num
);
1763 while (!parser
.finished ())
1765 struct thread_info
*tp
= NULL
;
1766 struct inferior
*inf
;
1767 int inf_num
, thr_num
;
1769 parser
.get_tid (&inf_num
, &thr_num
);
1770 inf
= find_inferior_id (inf_num
);
1772 tp
= find_thread_id (inf
, thr_num
);
1774 if (parser
.in_star_range ())
1778 warning (_("Unknown inferior %d"), inf_num
);
1779 parser
.skip_range ();
1783 /* No use looking for threads past the highest thread number
1784 the inferior ever had. */
1785 if (thr_num
>= inf
->highest_thread_num
)
1786 parser
.skip_range ();
1788 /* Be quiet about unknown threads numbers. */
1795 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1796 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1798 warning (_("Unknown thread %d"), thr_num
);
1802 if (!switch_to_thread_if_alive (tp
))
1804 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1808 thread_try_catch_cmd (tp
, {}, cmd
, from_tty
, flags
);
1813 /* Implementation of the "taas" command. */
1816 taas_command (const char *cmd
, int from_tty
)
1818 if (cmd
== NULL
|| *cmd
== '\0')
1819 error (_("Please specify a command to apply on all threads"));
1820 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1821 execute_command (expanded
.c_str (), from_tty
);
1824 /* Implementation of the "tfaas" command. */
1827 tfaas_command (const char *cmd
, int from_tty
)
1829 if (cmd
== NULL
|| *cmd
== '\0')
1830 error (_("Please specify a command to apply on all frames of all threads"));
1831 std::string expanded
1832 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1833 execute_command (expanded
.c_str (), from_tty
);
1836 /* Switch to the specified thread, or print the current thread. */
1839 thread_command (const char *tidstr
, int from_tty
)
1843 if (inferior_ptid
== null_ptid
)
1844 error (_("No thread selected"));
1846 if (target_has_stack ())
1848 struct thread_info
*tp
= inferior_thread ();
1850 if (tp
->state
== THREAD_EXITED
)
1851 gdb_printf (_("[Current thread is %s (%s) (exited)]\n"),
1852 print_thread_id (tp
),
1853 target_pid_to_str (inferior_ptid
).c_str ());
1855 gdb_printf (_("[Current thread is %s (%s)]\n"),
1856 print_thread_id (tp
),
1857 target_pid_to_str (inferior_ptid
).c_str ());
1860 error (_("No stack."));
1864 ptid_t previous_ptid
= inferior_ptid
;
1866 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1868 /* Print if the thread has not changed, otherwise an event will
1870 if (inferior_ptid
== previous_ptid
)
1872 print_selected_thread_frame (current_uiout
,
1873 USER_SELECTED_THREAD
1874 | USER_SELECTED_FRAME
);
1877 notify_user_selected_context_changed
1878 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1882 /* Implementation of `thread name'. */
1885 thread_name_command (const char *arg
, int from_tty
)
1887 struct thread_info
*info
;
1889 if (inferior_ptid
== null_ptid
)
1890 error (_("No thread selected"));
1892 arg
= skip_spaces (arg
);
1894 info
= inferior_thread ();
1895 info
->set_name (arg
!= nullptr ? make_unique_xstrdup (arg
) : nullptr);
1898 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1901 thread_find_command (const char *arg
, int from_tty
)
1904 unsigned long match
= 0;
1906 if (arg
== NULL
|| *arg
== '\0')
1907 error (_("Command requires an argument."));
1909 tmp
= re_comp (arg
);
1911 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1913 /* We're going to be switching threads. */
1914 scoped_restore_current_thread restore_thread
;
1916 update_thread_list ();
1918 for (thread_info
*tp
: all_threads ())
1920 switch_to_inferior_no_thread (tp
->inf
);
1922 if (tp
->name () != nullptr && re_exec (tp
->name ()))
1924 gdb_printf (_("Thread %s has name '%s'\n"),
1925 print_thread_id (tp
), tp
->name ());
1929 tmp
= target_thread_name (tp
);
1930 if (tmp
!= NULL
&& re_exec (tmp
))
1932 gdb_printf (_("Thread %s has target name '%s'\n"),
1933 print_thread_id (tp
), tmp
);
1937 std::string name
= target_pid_to_str (tp
->ptid
);
1938 if (!name
.empty () && re_exec (name
.c_str ()))
1940 gdb_printf (_("Thread %s has target id '%s'\n"),
1941 print_thread_id (tp
), name
.c_str ());
1945 tmp
= target_extra_thread_info (tp
);
1946 if (tmp
!= NULL
&& re_exec (tmp
))
1948 gdb_printf (_("Thread %s has extra info '%s'\n"),
1949 print_thread_id (tp
), tmp
);
1954 gdb_printf (_("No threads match '%s'\n"), arg
);
1957 /* Print notices when new threads are attached and detached. */
1958 bool print_thread_events
= true;
1960 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1961 struct cmd_list_element
*c
, const char *value
)
1964 _("Printing of thread events is %s.\n"),
1968 /* See gdbthread.h. */
1971 thread_select (const char *tidstr
, thread_info
*tp
)
1973 if (!switch_to_thread_if_alive (tp
))
1974 error (_("Thread ID %s has terminated."), tidstr
);
1976 annotate_thread_changed ();
1978 /* Since the current thread may have changed, see if there is any
1979 exited thread we can now delete. */
1980 delete_exited_threads ();
1983 /* Print thread and frame switch command response. */
1986 print_selected_thread_frame (struct ui_out
*uiout
,
1987 user_selected_what selection
)
1989 struct thread_info
*tp
= inferior_thread ();
1991 if (selection
& USER_SELECTED_THREAD
)
1993 if (uiout
->is_mi_like_p ())
1995 uiout
->field_signed ("new-thread-id",
1996 inferior_thread ()->global_num
);
2000 uiout
->text ("[Switching to thread ");
2001 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2003 uiout
->text (target_pid_to_str (inferior_ptid
));
2008 if (tp
->state
== THREAD_RUNNING
)
2010 if (selection
& USER_SELECTED_THREAD
)
2011 uiout
->text ("(running)\n");
2013 else if (selection
& USER_SELECTED_FRAME
)
2015 if (selection
& USER_SELECTED_THREAD
)
2018 if (has_stack_frames ())
2019 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2024 /* Update the 'threads_executing' global based on the threads we know
2025 about right now. This is used by infrun to tell whether we should
2026 pull events out of the current target. */
2029 update_threads_executing (void)
2031 process_stratum_target
*targ
= current_inferior ()->process_target ();
2036 targ
->threads_executing
= false;
2038 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2040 if (!inf
->has_execution ())
2043 /* If the process has no threads, then it must be we have a
2044 process-exit event pending. */
2045 if (inf
->thread_list
.empty ())
2047 targ
->threads_executing
= true;
2051 for (thread_info
*tp
: inf
->non_exited_threads ())
2053 if (tp
->executing ())
2055 targ
->threads_executing
= true;
2063 update_thread_list (void)
2065 target_update_thread_list ();
2066 update_threads_executing ();
2069 /* See gdbthread.h. */
2072 thread_name (thread_info
*thread
)
2074 /* Use the manually set name if there is one. */
2075 const char *name
= thread
->name ();
2076 if (name
!= nullptr)
2079 /* Otherwise, ask the target. Ensure we query the right target stack. */
2080 scoped_restore_current_thread restore_thread
;
2081 if (thread
->inf
!= current_inferior ())
2082 switch_to_inferior_no_thread (thread
->inf
);
2084 return target_thread_name (thread
);
2087 /* See gdbthread.h. */
2090 thread_state_string (enum thread_state state
)
2094 case THREAD_STOPPED
:
2097 case THREAD_RUNNING
:
2104 gdb_assert_not_reached ("unknown thread state");
2107 /* Return a new value for the selected thread's id. Return a value of
2108 0 if no thread is selected. If GLOBAL is true, return the thread's
2109 global number. Otherwise return the per-inferior number. */
2111 static struct value
*
2112 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2116 if (inferior_ptid
== null_ptid
)
2120 thread_info
*tp
= inferior_thread ();
2122 int_val
= tp
->global_num
;
2124 int_val
= tp
->per_inf_num
;
2127 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2130 /* Return a new value for the selected thread's per-inferior thread
2131 number. Return a value of 0 if no thread is selected, or no
2134 static struct value
*
2135 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2136 struct internalvar
*var
,
2139 return thread_num_make_value_helper (gdbarch
, 0);
2142 /* Return a new value for the selected thread's global id. Return a
2143 value of 0 if no thread is selected, or no threads exist. */
2145 static struct value
*
2146 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2149 return thread_num_make_value_helper (gdbarch
, 1);
2152 /* Return a new value for the number of non-exited threads in the current
2153 inferior. If there are no threads in the current inferior return a
2156 static struct value
*
2157 inferior_thread_count_make_value (struct gdbarch
*gdbarch
,
2158 struct internalvar
*var
, void *ignore
)
2162 if (inferior_ptid
!= null_ptid
)
2163 int_val
= current_inferior ()->non_exited_threads ().size ();
2165 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2168 /* Commands with a prefix of `thread'. */
2169 struct cmd_list_element
*thread_cmd_list
= NULL
;
2171 /* Implementation of `thread' variable. */
2173 static const struct internalvar_funcs thread_funcs
=
2175 thread_id_per_inf_num_make_value
,
2179 /* Implementation of `gthread' variable. */
2181 static const struct internalvar_funcs gthread_funcs
=
2183 global_thread_id_make_value
,
2187 /* Implementation of `_inferior_thread_count` convenience variable. */
2189 static const struct internalvar_funcs inferior_thread_count_funcs
=
2191 inferior_thread_count_make_value
,
2195 void _initialize_thread ();
2197 _initialize_thread ()
2199 static struct cmd_list_element
*thread_apply_list
= NULL
;
2200 cmd_list_element
*c
;
2202 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2204 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2206 static std::string info_threads_help
2207 = gdb::option::build_help (_("\
2208 Display currently known threads.\n\
2209 Usage: info threads [OPTION]... [ID]...\n\
2210 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2211 Otherwise, all threads are displayed.\n\
2217 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2218 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2220 cmd_list_element
*thread_cmd
2221 = add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2222 Use this command to switch between threads.\n\
2223 The new thread ID must be currently known."),
2224 &thread_cmd_list
, 1, &cmdlist
);
2226 add_com_alias ("t", thread_cmd
, class_run
, 1);
2228 #define THREAD_APPLY_OPTION_HELP "\
2229 Prints per-inferior thread number and target system's thread id\n\
2230 followed by COMMAND output.\n\
2232 By default, an error raised during the execution of COMMAND\n\
2233 aborts \"thread apply\".\n\
2238 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2240 static std::string thread_apply_help
= gdb::option::build_help (_("\
2241 Apply a command to a list of threads.\n\
2242 Usage: thread apply ID... [OPTION]... COMMAND\n\
2243 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2244 THREAD_APPLY_OPTION_HELP
),
2247 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2248 thread_apply_help
.c_str (),
2249 &thread_apply_list
, 1,
2251 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2253 const auto thread_apply_all_opts
2254 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2256 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2257 Apply a command to all threads.\n\
2259 Usage: thread apply all [OPTION]... COMMAND\n"
2260 THREAD_APPLY_OPTION_HELP
),
2261 thread_apply_all_opts
);
2263 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2264 thread_apply_all_help
.c_str (),
2265 &thread_apply_list
);
2266 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2268 c
= add_com ("taas", class_run
, taas_command
, _("\
2269 Apply a command to all threads (ignoring errors and empty output).\n\
2270 Usage: taas [OPTION]... COMMAND\n\
2271 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2272 See \"help thread apply all\" for available options."));
2273 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2275 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2276 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2277 Usage: tfaas [OPTION]... COMMAND\n\
2278 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2279 See \"help frame apply all\" for available options."));
2280 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2282 add_cmd ("name", class_run
, thread_name_command
,
2283 _("Set the current thread's name.\n\
2284 Usage: thread name [NAME]\n\
2285 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2287 add_cmd ("find", class_run
, thread_find_command
, _("\
2288 Find threads that match a regular expression.\n\
2289 Usage: thread find REGEXP\n\
2290 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2293 add_setshow_boolean_cmd ("thread-events", no_class
,
2294 &print_thread_events
, _("\
2295 Set printing of thread events (such as thread start and exit)."), _("\
2296 Show printing of thread events (such as thread start and exit)."), NULL
,
2298 show_print_thread_events
,
2299 &setprintlist
, &showprintlist
);
2301 add_setshow_boolean_cmd ("threads", class_maintenance
, &debug_threads
, _("\
2302 Set thread debugging."), _("\
2303 Show thread debugging."), _("\
2304 When on messages about thread creation and deletion are printed."),
2307 &setdebuglist
, &showdebuglist
);
2309 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2310 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);
2311 create_internalvar_type_lazy ("_inferior_thread_count",
2312 &inferior_thread_count_funcs
, NULL
);