1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 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 "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 /* Definition of struct thread_info exported to gdbthread.h. */
54 /* Prototypes for local functions. */
56 static int highest_thread_num
;
58 /* The current/selected thread. */
59 static thread_info
*current_thread_
;
61 /* Returns true if THR is the current thread. */
64 is_current_thread (const thread_info
*thr
)
66 return thr
== current_thread_
;
70 inferior_thread (void)
72 gdb_assert (current_thread_
!= nullptr);
73 return current_thread_
;
76 /* Delete the breakpoint pointed at by BP_P, if there's one. */
79 delete_thread_breakpoint (struct breakpoint
**bp_p
)
83 delete_breakpoint (*bp_p
);
89 delete_step_resume_breakpoint (struct thread_info
*tp
)
92 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
96 delete_exception_resume_breakpoint (struct thread_info
*tp
)
99 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
102 /* See gdbthread.h. */
105 delete_single_step_breakpoints (struct thread_info
*tp
)
108 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
111 /* Delete the breakpoint pointed at by BP_P at the next stop, if
115 delete_at_next_stop (struct breakpoint
**bp
)
119 (*bp
)->disposition
= disp_del_at_next_stop
;
124 /* See gdbthread.h. */
127 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
129 return tp
->control
.single_step_breakpoints
!= NULL
;
132 /* See gdbthread.h. */
135 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
136 const address_space
*aspace
,
139 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
141 return (ss_bps
!= NULL
142 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
145 /* See gdbthread.h. */
148 thread_cancel_execution_command (struct thread_info
*thr
)
150 if (thr
->thread_fsm
!= NULL
)
152 thr
->thread_fsm
->clean_up (thr
);
153 delete thr
->thread_fsm
;
154 thr
->thread_fsm
= NULL
;
159 clear_thread_inferior_resources (struct thread_info
*tp
)
161 /* NOTE: this will take care of any left-over step_resume breakpoints,
162 but not any user-specified thread-specific breakpoints. We can not
163 delete the breakpoint straight-off, because the inferior might not
164 be stopped at the moment. */
165 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
166 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
167 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
169 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
171 bpstat_clear (&tp
->control
.stop_bpstat
);
173 btrace_teardown (tp
);
175 thread_cancel_execution_command (tp
);
177 clear_inline_frame_state (tp
);
180 /* See gdbthread.h. */
183 set_thread_exited (thread_info
*tp
, bool silent
)
185 /* Dead threads don't need to step-over. Remove from chain. */
186 if (thread_is_in_step_over_chain (tp
))
187 global_thread_step_over_chain_remove (tp
);
189 if (tp
->state
!= THREAD_EXITED
)
191 process_stratum_target
*proc_target
= tp
->inf
->process_target ();
193 /* Some targets unpush themselves from the inferior's target stack before
194 clearing the inferior's thread list (which marks all threads as exited,
195 and therefore leads to this function). In this case, the inferior's
196 process target will be nullptr when we arrive here.
198 See also the comment in inferior::unpush_target. */
199 if (proc_target
!= nullptr)
200 proc_target
->maybe_remove_resumed_with_pending_wait_status (tp
);
202 gdb::observers::thread_exit
.notify (tp
, silent
);
204 /* Tag it as exited. */
205 tp
->state
= THREAD_EXITED
;
207 /* Clear breakpoints, etc. associated with this thread. */
208 clear_thread_inferior_resources (tp
);
210 /* Remove from the ptid_t map. We don't want for
211 find_thread_ptid to find exited threads. Also, the target
212 may reuse the ptid for a new thread, and there can only be
213 one value per key; adding a new thread with the same ptid_t
214 would overwrite the exited thread's ptid entry. */
215 size_t nr_deleted
= tp
->inf
->ptid_thread_map
.erase (tp
->ptid
);
216 gdb_assert (nr_deleted
== 1);
221 init_thread_list (void)
223 highest_thread_num
= 0;
225 for (inferior
*inf
: all_inferiors ())
226 inf
->clear_thread_list (true);
229 /* Allocate a new thread of inferior INF with target id PTID and add
230 it to the thread list. */
232 static struct thread_info
*
233 new_thread (struct inferior
*inf
, ptid_t ptid
)
235 thread_info
*tp
= new thread_info (inf
, ptid
);
237 inf
->thread_list
.push_back (*tp
);
239 /* A thread with this ptid should not exist in the map yet. */
240 gdb_assert (inf
->ptid_thread_map
.find (ptid
) == inf
->ptid_thread_map
.end ());
242 inf
->ptid_thread_map
[ptid
] = tp
;
248 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
250 gdb_assert (targ
!= nullptr);
252 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
254 /* We may have an old thread with the same id in the thread list.
255 If we do, it must be dead, otherwise we wouldn't be adding a new
256 thread with the same id. The OS is reusing this id --- delete
257 the old thread, and create a new one. */
258 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
262 tp
= new_thread (inf
, ptid
);
263 gdb::observers::new_thread
.notify (tp
);
269 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
270 private_thread_info
*priv
)
272 thread_info
*result
= add_thread_silent (targ
, ptid
);
274 result
->priv
.reset (priv
);
276 if (print_thread_events
)
277 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
279 annotate_new_thread ();
284 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
286 return add_thread_with_info (targ
, ptid
, NULL
);
289 private_thread_info::~private_thread_info () = default;
291 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
292 : ptid (ptid_
), inf (inf_
)
294 gdb_assert (inf_
!= NULL
);
296 this->global_num
= ++highest_thread_num
;
297 this->per_inf_num
= ++inf_
->highest_thread_num
;
299 /* Nothing to follow yet. */
300 this->pending_follow
.set_spurious ();
303 /* See gdbthread.h. */
306 thread_info::deletable () const
308 /* If this is the current thread, or there's code out there that
309 relies on it existing (refcount > 0) we can't delete yet. */
310 return refcount () == 0 && !is_current_thread (this);
313 /* See gdbthread.h. */
316 thread_info::set_executing (bool executing
)
318 m_executing
= executing
;
320 this->clear_stop_pc ();
323 /* See gdbthread.h. */
326 thread_info::set_resumed (bool resumed
)
328 if (resumed
== m_resumed
)
331 process_stratum_target
*proc_target
= this->inf
->process_target ();
333 /* If we transition from resumed to not resumed, we might need to remove
334 the thread from the resumed threads with pending statuses list. */
336 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
340 /* If we transition from not resumed to resumed, we might need to add
341 the thread to the resumed threads with pending statuses list. */
343 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
346 /* See gdbthread.h. */
349 thread_info::set_pending_waitstatus (const target_waitstatus
&ws
)
351 gdb_assert (!this->has_pending_waitstatus ());
353 m_suspend
.waitstatus
= ws
;
354 m_suspend
.waitstatus_pending_p
= 1;
356 process_stratum_target
*proc_target
= this->inf
->process_target ();
357 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
360 /* See gdbthread.h. */
363 thread_info::clear_pending_waitstatus ()
365 gdb_assert (this->has_pending_waitstatus ());
367 process_stratum_target
*proc_target
= this->inf
->process_target ();
368 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
370 m_suspend
.waitstatus_pending_p
= 0;
373 /* See gdbthread.h. */
376 thread_is_in_step_over_chain (struct thread_info
*tp
)
378 return tp
->step_over_list_node
.is_linked ();
381 /* See gdbthread.h. */
384 thread_step_over_chain_length (const thread_step_over_list
&l
)
388 for (const thread_info
&thread ATTRIBUTE_UNUSED
: l
)
394 /* See gdbthread.h. */
397 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
399 infrun_debug_printf ("enqueueing thread %s in global step over chain",
400 target_pid_to_str (tp
->ptid
).c_str ());
402 gdb_assert (!thread_is_in_step_over_chain (tp
));
403 global_thread_step_over_list
.push_back (*tp
);
406 /* See gdbthread.h. */
409 global_thread_step_over_chain_enqueue_chain (thread_step_over_list
&&list
)
411 global_thread_step_over_list
.splice (std::move (list
));
414 /* See gdbthread.h. */
417 global_thread_step_over_chain_remove (struct thread_info
*tp
)
419 infrun_debug_printf ("removing thread %s from global step over chain",
420 target_pid_to_str (tp
->ptid
).c_str ());
422 gdb_assert (thread_is_in_step_over_chain (tp
));
423 auto it
= global_thread_step_over_list
.iterator_to (*tp
);
424 global_thread_step_over_list
.erase (it
);
427 /* Delete the thread referenced by THR. If SILENT, don't notify
428 the observer of this exit.
430 THR must not be NULL or a failed assertion will be raised. */
433 delete_thread_1 (thread_info
*thr
, bool silent
)
435 gdb_assert (thr
!= nullptr);
437 set_thread_exited (thr
, silent
);
439 if (!thr
->deletable ())
441 /* Will be really deleted some other time. */
445 auto it
= thr
->inf
->thread_list
.iterator_to (*thr
);
446 thr
->inf
->thread_list
.erase (it
);
451 /* See gdbthread.h. */
454 delete_thread (thread_info
*thread
)
456 delete_thread_1 (thread
, false /* not silent */);
460 delete_thread_silent (thread_info
*thread
)
462 delete_thread_1 (thread
, true /* silent */);
466 find_thread_global_id (int global_id
)
468 for (thread_info
*tp
: all_threads ())
469 if (tp
->global_num
== global_id
)
475 static struct thread_info
*
476 find_thread_id (struct inferior
*inf
, int thr_num
)
478 for (thread_info
*tp
: inf
->threads ())
479 if (tp
->per_inf_num
== thr_num
)
485 /* See gdbthread.h. */
488 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
490 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
493 return find_thread_ptid (inf
, ptid
);
496 /* See gdbthread.h. */
499 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
501 gdb_assert (inf
!= nullptr);
503 auto it
= inf
->ptid_thread_map
.find (ptid
);
504 if (it
!= inf
->ptid_thread_map
.end ())
510 /* See gdbthread.h. */
513 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
514 struct inferior
*inf
)
516 return target_thread_handle_to_thread_info (handle
.data (),
522 * Thread iterator function.
524 * Calls a callback function once for each thread, so long as
525 * the callback function returns false. If the callback function
526 * returns true, the iteration will end and the current thread
527 * will be returned. This can be useful for implementing a
528 * search for a thread with arbitrary attributes, or for applying
529 * some operation to every thread.
531 * FIXME: some of the existing functionality, such as
532 * "Thread apply all", might be rewritten using this functionality.
536 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
539 for (thread_info
*tp
: all_threads_safe ())
540 if ((*callback
) (tp
, data
))
546 /* See gdbthread.h. */
551 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
557 thread_count (process_stratum_target
*proc_target
)
559 auto rng
= all_threads (proc_target
);
560 return std::distance (rng
.begin (), rng
.end ());
563 /* Return the number of non-exited threads in the thread list. */
566 live_threads_count (void)
568 auto rng
= all_non_exited_threads ();
569 return std::distance (rng
.begin (), rng
.end ());
573 valid_global_thread_id (int global_id
)
575 for (thread_info
*tp
: all_threads ())
576 if (tp
->global_num
== global_id
)
583 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
585 return find_thread_ptid (targ
, ptid
) != nullptr;
588 /* Finds the first thread of the inferior. */
591 first_thread_of_inferior (inferior
*inf
)
593 if (inf
->thread_list
.empty ())
596 return &inf
->thread_list
.front ();
600 any_thread_of_inferior (inferior
*inf
)
602 gdb_assert (inf
->pid
!= 0);
604 /* Prefer the current thread, if there's one. */
605 if (inf
== current_inferior () && inferior_ptid
!= null_ptid
)
606 return inferior_thread ();
608 for (thread_info
*tp
: inf
->non_exited_threads ())
615 any_live_thread_of_inferior (inferior
*inf
)
617 struct thread_info
*curr_tp
= NULL
;
618 struct thread_info
*tp_executing
= NULL
;
620 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
622 /* Prefer the current thread if it's not executing. */
623 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
625 /* If the current thread is dead, forget it. If it's not
626 executing, use it. Otherwise, still choose it (below), but
627 only if no other non-executing thread is found. */
628 curr_tp
= inferior_thread ();
629 if (curr_tp
->state
== THREAD_EXITED
)
631 else if (!curr_tp
->executing ())
635 for (thread_info
*tp
: inf
->non_exited_threads ())
637 if (!tp
->executing ())
643 /* If both the current thread and all live threads are executing,
644 prefer the current thread. */
648 /* Otherwise, just return an executing thread, if any. */
652 /* Return true if TP is an active thread. */
654 thread_alive (thread_info
*tp
)
656 if (tp
->state
== THREAD_EXITED
)
659 /* Ensure we're looking at the right target stack. */
660 gdb_assert (tp
->inf
== current_inferior ());
662 return target_thread_alive (tp
->ptid
);
665 /* Switch to thread TP if it is alive. Returns true if successfully
666 switched, false otherwise. */
669 switch_to_thread_if_alive (thread_info
*thr
)
671 scoped_restore_current_thread restore_thread
;
673 /* Switch inferior first, so that we're looking at the right target
675 switch_to_inferior_no_thread (thr
->inf
);
677 if (thread_alive (thr
))
679 switch_to_thread (thr
);
680 restore_thread
.dont_restore ();
687 /* See gdbthreads.h. */
692 scoped_restore_current_thread restore_thread
;
694 for (thread_info
*tp
: all_threads_safe ())
696 switch_to_inferior_no_thread (tp
->inf
);
698 if (!thread_alive (tp
))
703 /* See gdbthreads.h. */
706 delete_exited_threads (void)
708 for (thread_info
*tp
: all_threads_safe ())
709 if (tp
->state
== THREAD_EXITED
)
713 /* Return true value if stack temporaries are enabled for the thread
717 thread_stack_temporaries_enabled_p (thread_info
*tp
)
722 return tp
->stack_temporaries_enabled
;
725 /* Push V on to the stack temporaries of the thread with id PTID. */
728 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
730 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
731 tp
->stack_temporaries
.push_back (v
);
734 /* Return true if VAL is among the stack temporaries of the thread
735 TP. Return false otherwise. */
738 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
740 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
741 for (value
*v
: tp
->stack_temporaries
)
748 /* Return the last of the stack temporaries for thread with id PTID.
749 Return NULL if there are no stack temporaries for the thread. */
752 get_last_thread_stack_temporary (thread_info
*tp
)
754 struct value
*lastval
= NULL
;
756 gdb_assert (tp
!= NULL
);
757 if (!tp
->stack_temporaries
.empty ())
758 lastval
= tp
->stack_temporaries
.back ();
764 thread_change_ptid (process_stratum_target
*targ
,
765 ptid_t old_ptid
, ptid_t new_ptid
)
767 struct inferior
*inf
;
768 struct thread_info
*tp
;
770 /* It can happen that what we knew as the target inferior id
771 changes. E.g, target remote may only discover the remote process
772 pid after adding the inferior to GDB's list. */
773 inf
= find_inferior_ptid (targ
, old_ptid
);
774 inf
->pid
= new_ptid
.pid ();
776 tp
= find_thread_ptid (inf
, old_ptid
);
777 gdb_assert (tp
!= nullptr);
779 int num_erased
= inf
->ptid_thread_map
.erase (old_ptid
);
780 gdb_assert (num_erased
== 1);
783 inf
->ptid_thread_map
[new_ptid
] = tp
;
785 gdb::observers::thread_ptid_changed
.notify (targ
, old_ptid
, new_ptid
);
788 /* See gdbthread.h. */
791 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
793 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
794 tp
->set_resumed (resumed
);
797 /* Helper for set_running, that marks one thread either running or
801 set_running_thread (struct thread_info
*tp
, bool running
)
803 bool started
= false;
805 if (running
&& tp
->state
== THREAD_STOPPED
)
807 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
811 /* If the thread is now marked stopped, remove it from
812 the step-over queue, so that we don't try to resume
813 it until the user wants it to. */
814 if (thread_is_in_step_over_chain (tp
))
815 global_thread_step_over_chain_remove (tp
);
821 /* See gdbthread.h. */
824 thread_info::set_running (bool running
)
826 if (set_running_thread (this, running
))
827 gdb::observers::target_resumed
.notify (this->ptid
);
831 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
833 /* We try not to notify the observer if no thread has actually
834 changed the running state -- merely to reduce the number of
835 messages to the MI frontend. A frontend is supposed to handle
836 multiple *running notifications just fine. */
837 bool any_started
= false;
839 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
840 if (set_running_thread (tp
, running
))
844 gdb::observers::target_resumed
.notify (ptid
);
848 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
850 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
851 tp
->set_executing (executing
);
853 /* It only takes one running thread to spawn more threads. */
855 targ
->threads_executing
= true;
856 /* Only clear the flag if the caller is telling us everything is
858 else if (minus_one_ptid
== ptid
)
859 targ
->threads_executing
= false;
862 /* See gdbthread.h. */
865 threads_are_executing (process_stratum_target
*target
)
867 return target
->threads_executing
;
871 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
873 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
874 tp
->stop_requested
= stop
;
876 /* Call the stop requested observer so other components of GDB can
877 react to this request. */
879 gdb::observers::thread_stop_requested
.notify (ptid
);
883 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
885 bool any_started
= false;
887 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
888 if (set_running_thread (tp
, tp
->executing ()))
892 gdb::observers::target_resumed
.notify (ptid
);
895 /* See gdbthread.h. */
898 validate_registers_access (void)
900 /* No selected thread, no registers. */
901 if (inferior_ptid
== null_ptid
)
902 error (_("No thread selected."));
904 thread_info
*tp
= inferior_thread ();
906 /* Don't try to read from a dead thread. */
907 if (tp
->state
== THREAD_EXITED
)
908 error (_("The current thread has terminated"));
910 /* ... or from a spinning thread. FIXME: This isn't actually fully
911 correct. It'll allow an user-requested access (e.g., "print $pc"
912 at the prompt) when a thread is not executing for some internal
913 reason, but is marked running from the user's perspective. E.g.,
914 the thread is waiting for its turn in the step-over queue. */
915 if (tp
->executing ())
916 error (_("Selected thread is running."));
919 /* See gdbthread.h. */
922 can_access_registers_thread (thread_info
*thread
)
924 /* No thread, no registers. */
928 /* Don't try to read from a dead thread. */
929 if (thread
->state
== THREAD_EXITED
)
932 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
933 if (thread
->executing ())
940 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
942 return (pc
>= thread
->control
.step_range_start
943 && pc
< thread
->control
.step_range_end
);
946 /* Helper for print_thread_info. Returns true if THR should be
947 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
948 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
949 is true if REQUESTED_THREADS is list of global IDs, false if a list
950 of per-inferior thread ids. If PID is not -1, only print THR if it
951 is a thread from the process PID. Otherwise, threads from all
952 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
953 and PID is not -1, then the thread is printed if it belongs to the
954 specified process. Otherwise, an error is raised. */
957 should_print_thread (const char *requested_threads
, int default_inf_num
,
958 int global_ids
, int pid
, struct thread_info
*thr
)
960 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
965 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
967 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
968 thr
->inf
->num
, thr
->per_inf_num
);
973 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
975 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
976 error (_("Requested thread not found in requested process"));
980 if (thr
->state
== THREAD_EXITED
)
986 /* Return the string to display in "info threads"'s "Target Id"
990 thread_target_id_str (thread_info
*tp
)
992 std::string target_id
= target_pid_to_str (tp
->ptid
);
993 const char *extra_info
= target_extra_thread_info (tp
);
994 const char *name
= thread_name (tp
);
996 if (extra_info
!= nullptr && name
!= nullptr)
997 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
999 else if (extra_info
!= nullptr)
1000 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1001 else if (name
!= nullptr)
1002 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1007 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1008 whether REQUESTED_THREADS is a list of global or per-inferior
1012 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1013 int global_ids
, int pid
,
1014 int show_global_ids
)
1016 int default_inf_num
= current_inferior ()->num
;
1018 update_thread_list ();
1020 /* Whether we saw any thread. */
1021 bool any_thread
= false;
1022 /* Whether the current thread is exited. */
1023 bool current_exited
= false;
1025 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1026 ? inferior_thread () : NULL
);
1029 /* For backward compatibility, we make a list for MI. A table is
1030 preferable for the CLI, though, because it shows table
1032 gdb::optional
<ui_out_emit_list
> list_emitter
;
1033 gdb::optional
<ui_out_emit_table
> table_emitter
;
1035 /* We'll be switching threads temporarily below. */
1036 scoped_restore_current_thread restore_thread
;
1038 if (uiout
->is_mi_like_p ())
1039 list_emitter
.emplace (uiout
, "threads");
1043 /* The width of the "Target Id" column. Grown below to
1044 accommodate the largest entry. */
1045 size_t target_id_col_width
= 17;
1047 for (thread_info
*tp
: all_threads ())
1049 if (!should_print_thread (requested_threads
, default_inf_num
,
1050 global_ids
, pid
, tp
))
1053 if (!uiout
->is_mi_like_p ())
1055 /* Switch inferiors so we're looking at the right
1057 switch_to_inferior_no_thread (tp
->inf
);
1060 = std::max (target_id_col_width
,
1061 thread_target_id_str (tp
).size ());
1069 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1070 uiout
->message (_("No threads.\n"));
1072 uiout
->message (_("No threads match '%s'.\n"),
1077 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1078 n_threads
, "threads");
1080 uiout
->table_header (1, ui_left
, "current", "");
1081 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1082 if (show_global_ids
)
1083 uiout
->table_header (4, ui_left
, "id", "GId");
1084 uiout
->table_header (target_id_col_width
, ui_left
,
1085 "target-id", "Target Id");
1086 uiout
->table_header (1, ui_left
, "frame", "Frame");
1087 uiout
->table_body ();
1090 for (inferior
*inf
: all_inferiors ())
1091 for (thread_info
*tp
: inf
->threads ())
1096 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1097 current_exited
= true;
1099 if (!should_print_thread (requested_threads
, default_inf_num
,
1100 global_ids
, pid
, tp
))
1103 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1105 if (!uiout
->is_mi_like_p ())
1107 if (tp
== current_thread
)
1108 uiout
->field_string ("current", "*");
1110 uiout
->field_skip ("current");
1112 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1115 if (show_global_ids
|| uiout
->is_mi_like_p ())
1116 uiout
->field_signed ("id", tp
->global_num
);
1118 /* Switch to the thread (and inferior / target). */
1119 switch_to_thread (tp
);
1121 /* For the CLI, we stuff everything into the target-id field.
1122 This is a gross hack to make the output come out looking
1123 correct. The underlying problem here is that ui-out has no
1124 way to specify that a field's space allocation should be
1125 shared by several fields. For MI, we do the right thing
1128 if (uiout
->is_mi_like_p ())
1130 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1132 const char *extra_info
= target_extra_thread_info (tp
);
1133 if (extra_info
!= nullptr)
1134 uiout
->field_string ("details", extra_info
);
1136 const char *name
= thread_name (tp
);
1138 uiout
->field_string ("name", name
);
1142 uiout
->field_string ("target-id", thread_target_id_str (tp
));
1145 if (tp
->state
== THREAD_RUNNING
)
1146 uiout
->text ("(running)\n");
1149 /* The switch above put us at the top of the stack (leaf
1151 print_stack_frame (get_selected_frame (NULL
),
1152 /* For MI output, print frame level. */
1153 uiout
->is_mi_like_p (),
1157 if (uiout
->is_mi_like_p ())
1159 const char *state
= "stopped";
1161 if (tp
->state
== THREAD_RUNNING
)
1163 uiout
->field_string ("state", state
);
1166 core
= target_core_of_thread (tp
->ptid
);
1167 if (uiout
->is_mi_like_p () && core
!= -1)
1168 uiout
->field_signed ("core", core
);
1171 /* This end scope restores the current thread and the frame
1172 selected before the "info threads" command, and it finishes the
1173 ui-out list or table. */
1176 if (pid
== -1 && requested_threads
== NULL
)
1178 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1179 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1181 if (inferior_ptid
!= null_ptid
&& current_exited
)
1182 uiout
->message ("\n\
1183 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1184 print_thread_id (inferior_thread ()));
1185 else if (any_thread
&& inferior_ptid
== null_ptid
)
1186 uiout
->message ("\n\
1187 No selected thread. See `help thread'.\n");
1191 /* See gdbthread.h. */
1194 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1197 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1200 /* The options for the "info threads" command. */
1202 struct info_threads_opts
1205 bool show_global_ids
= false;
1208 static const gdb::option::option_def info_threads_option_defs
[] = {
1210 gdb::option::flag_option_def
<info_threads_opts
> {
1212 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1213 N_("Show global thread IDs."),
1218 /* Create an option_def_group for the "info threads" options, with
1219 IT_OPTS as context. */
1221 static inline gdb::option::option_def_group
1222 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1224 return {{info_threads_option_defs
}, it_opts
};
1227 /* Implementation of the "info threads" command.
1229 Note: this has the drawback that it _really_ switches
1230 threads, which frees the frame cache. A no-side
1231 effects info-threads command would be nicer. */
1234 info_threads_command (const char *arg
, int from_tty
)
1236 info_threads_opts it_opts
;
1238 auto grp
= make_info_threads_options_def_group (&it_opts
);
1239 gdb::option::process_options
1240 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1242 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1245 /* Completer for the "info threads" command. */
1248 info_threads_command_completer (struct cmd_list_element
*ignore
,
1249 completion_tracker
&tracker
,
1250 const char *text
, const char *word_ignored
)
1252 const auto grp
= make_info_threads_options_def_group (nullptr);
1254 if (gdb::option::complete_options
1255 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1258 /* Convenience to let the user know what the option can accept. */
1261 gdb::option::complete_on_all_options (tracker
, grp
);
1262 /* Keep this "ID" in sync with what "help info threads"
1264 tracker
.add_completion (make_unique_xstrdup ("ID"));
1268 /* See gdbthread.h. */
1271 switch_to_thread_no_regs (struct thread_info
*thread
)
1273 struct inferior
*inf
= thread
->inf
;
1275 set_current_program_space (inf
->pspace
);
1276 set_current_inferior (inf
);
1278 current_thread_
= thread
;
1279 inferior_ptid
= current_thread_
->ptid
;
1282 /* See gdbthread.h. */
1285 switch_to_no_thread ()
1287 if (current_thread_
== nullptr)
1290 current_thread_
= nullptr;
1291 inferior_ptid
= null_ptid
;
1292 reinit_frame_cache ();
1295 /* See gdbthread.h. */
1298 switch_to_thread (thread_info
*thr
)
1300 gdb_assert (thr
!= NULL
);
1302 if (is_current_thread (thr
))
1305 switch_to_thread_no_regs (thr
);
1307 reinit_frame_cache ();
1310 /* See gdbsupport/common-gdbthread.h. */
1313 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1315 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1316 switch_to_thread (thr
);
1322 scoped_restore_current_thread::restore ()
1324 /* If an entry of thread_info was previously selected, it won't be
1325 deleted because we've increased its refcount. The thread represented
1326 by this thread_info entry may have already exited (due to normal exit,
1327 detach, etc), so the thread_info.state is THREAD_EXITED. */
1328 if (m_thread
!= NULL
1329 /* If the previously selected thread belonged to a process that has
1330 in the mean time exited (or killed, detached, etc.), then don't revert
1331 back to it, but instead simply drop back to no thread selected. */
1333 switch_to_thread (m_thread
.get ());
1335 switch_to_inferior_no_thread (m_inf
.get ());
1337 /* The running state of the originally selected thread may have
1338 changed, so we have to recheck it here. */
1339 if (inferior_ptid
!= null_ptid
1341 && m_thread
->state
== THREAD_STOPPED
1342 && target_has_registers ()
1343 && target_has_stack ()
1344 && target_has_memory ())
1345 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1347 set_language (m_lang
);
1350 scoped_restore_current_thread::~scoped_restore_current_thread ()
1352 if (!m_dont_restore
)
1356 scoped_restore_current_thread::scoped_restore_current_thread ()
1358 m_inf
= inferior_ref::new_reference (current_inferior ());
1360 m_lang
= current_language
->la_language
;
1362 if (inferior_ptid
!= null_ptid
)
1364 m_thread
= thread_info_ref::new_reference (inferior_thread ());
1366 m_was_stopped
= m_thread
->state
== THREAD_STOPPED
;
1367 save_selected_frame (&m_selected_frame_id
, &m_selected_frame_level
);
1371 /* See gdbthread.h. */
1374 show_thread_that_caused_stop (void)
1376 return highest_thread_num
> 1;
1379 /* See gdbthread.h. */
1382 show_inferior_qualified_tids (void)
1384 auto inf
= inferior_list
.begin ();
1388 return inf
!= inferior_list
.end ();
1391 /* See gdbthread.h. */
1394 print_thread_id (struct thread_info
*thr
)
1396 char *s
= get_print_cell ();
1398 if (show_inferior_qualified_tids ())
1399 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1401 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1405 /* Sort an array of struct thread_info pointers by thread ID (first by
1406 inferior number, and then by per-inferior thread number). Sorts in
1410 tp_array_compar_ascending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1412 if (a
->inf
->num
!= b
->inf
->num
)
1413 return a
->inf
->num
< b
->inf
->num
;
1415 return (a
->per_inf_num
< b
->per_inf_num
);
1418 /* Sort an array of struct thread_info pointers by thread ID (first by
1419 inferior number, and then by per-inferior thread number). Sorts in
1420 descending order. */
1423 tp_array_compar_descending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1425 if (a
->inf
->num
!= b
->inf
->num
)
1426 return a
->inf
->num
> b
->inf
->num
;
1428 return (a
->per_inf_num
> b
->per_inf_num
);
1431 /* Assuming that THR is the current thread, execute CMD.
1432 FLAGS.QUIET controls the printing of the thread information.
1433 FLAGS.CONT and FLAGS.SILENT control how to handle errors. Can throw an
1434 exception if !FLAGS.SILENT and !FLAGS.CONT and CMD fails. */
1437 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1438 const qcs_flags
&flags
)
1440 gdb_assert (is_current_thread (thr
));
1442 /* The thread header is computed before running the command since
1443 the command can change the inferior, which is not permitted
1444 by thread_target_id_str. */
1445 std::string thr_header
=
1446 string_printf (_("\nThread %s (%s):\n"), print_thread_id (thr
),
1447 thread_target_id_str (thr
).c_str ());
1451 std::string cmd_result
;
1452 execute_command_to_string
1453 (cmd_result
, cmd
, from_tty
, gdb_stdout
->term_out ());
1454 if (!flags
.silent
|| cmd_result
.length () > 0)
1457 printf_filtered ("%s", thr_header
.c_str ());
1458 printf_filtered ("%s", cmd_result
.c_str ());
1461 catch (const gdb_exception_error
&ex
)
1466 printf_filtered ("%s", thr_header
.c_str ());
1468 printf_filtered ("%s\n", ex
.what ());
1475 /* Option definition of "thread apply"'s "-ascending" option. */
1477 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1480 Call COMMAND for all threads in ascending order.\n\
1481 The default is descending order."),
1484 /* The qcs command line flags for the "thread apply" commands. Keep
1485 this in sync with the "frame apply" commands. */
1487 using qcs_flag_option_def
1488 = gdb::option::flag_option_def
<qcs_flags
>;
1490 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1491 qcs_flag_option_def
{
1492 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1493 N_("Disables printing the thread information."),
1496 qcs_flag_option_def
{
1497 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1498 N_("Print any error raised by COMMAND and continue."),
1501 qcs_flag_option_def
{
1502 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1503 N_("Silently ignore any errors or empty output produced by COMMAND."),
1507 /* Create an option_def_group for the "thread apply all" options, with
1508 ASCENDING and FLAGS as context. */
1510 static inline std::array
<gdb::option::option_def_group
, 2>
1511 make_thread_apply_all_options_def_group (bool *ascending
,
1515 { {ascending_option_def
.def ()}, ascending
},
1516 { {thr_qcs_flags_option_defs
}, flags
},
1520 /* Create an option_def_group for the "thread apply" options, with
1521 FLAGS as context. */
1523 static inline gdb::option::option_def_group
1524 make_thread_apply_options_def_group (qcs_flags
*flags
)
1526 return {{thr_qcs_flags_option_defs
}, flags
};
1529 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1530 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1531 of two numbers separated by a hyphen. Examples:
1533 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1534 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1535 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1538 thread_apply_all_command (const char *cmd
, int from_tty
)
1540 bool ascending
= false;
1543 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1545 gdb::option::process_options
1546 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1548 validate_flags_qcs ("thread apply all", &flags
);
1550 if (cmd
== NULL
|| *cmd
== '\000')
1551 error (_("Please specify a command at the end of 'thread apply all'"));
1553 update_thread_list ();
1555 int tc
= live_threads_count ();
1558 /* Save a copy of the thread list and increment each thread's
1559 refcount while executing the command in the context of each
1560 thread, in case the command is one that wipes threads. E.g.,
1561 detach, kill, disconnect, etc., or even normally continuing
1562 over an inferior or thread exit. */
1563 std::vector
<thread_info_ref
> thr_list_cpy
;
1564 thr_list_cpy
.reserve (tc
);
1566 for (thread_info
*tp
: all_non_exited_threads ())
1567 thr_list_cpy
.push_back (thread_info_ref::new_reference (tp
));
1568 gdb_assert (thr_list_cpy
.size () == tc
);
1570 auto *sorter
= (ascending
1571 ? tp_array_compar_ascending
1572 : tp_array_compar_descending
);
1573 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1575 scoped_restore_current_thread restore_thread
;
1577 for (thread_info_ref
&thr
: thr_list_cpy
)
1578 if (switch_to_thread_if_alive (thr
.get ()))
1579 thr_try_catch_cmd (thr
.get (), cmd
, from_tty
, flags
);
1583 /* Completer for "thread apply [ID list]". */
1586 thread_apply_command_completer (cmd_list_element
*ignore
,
1587 completion_tracker
&tracker
,
1588 const char *text
, const char * /*word*/)
1590 /* Don't leave this to complete_options because there's an early
1592 tracker
.set_use_custom_word_point (true);
1594 tid_range_parser parser
;
1595 parser
.init (text
, current_inferior ()->num
);
1599 while (!parser
.finished ())
1601 int inf_num
, thr_start
, thr_end
;
1603 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1606 if (parser
.in_star_range () || parser
.in_thread_range ())
1607 parser
.skip_range ();
1610 catch (const gdb_exception_error
&ex
)
1612 /* get_tid_range throws if it parses a negative number, for
1613 example. But a seemingly negative number may be the start of
1614 an option instead. */
1617 const char *cmd
= parser
.cur_tok ();
1621 /* No thread ID list yet. */
1625 /* Check if we're past a valid thread ID list already. */
1626 if (parser
.finished ()
1627 && cmd
> text
&& !isspace (cmd
[-1]))
1630 /* We're past the thread ID list, advance word point. */
1631 tracker
.advance_custom_word_point_by (cmd
- text
);
1634 const auto group
= make_thread_apply_options_def_group (nullptr);
1635 if (gdb::option::complete_options
1636 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1639 complete_nested_command_line (tracker
, text
);
1642 /* Completer for "thread apply all". */
1645 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1646 completion_tracker
&tracker
,
1647 const char *text
, const char *word
)
1649 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1651 if (gdb::option::complete_options
1652 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1655 complete_nested_command_line (tracker
, text
);
1658 /* Implementation of the "thread apply" command. */
1661 thread_apply_command (const char *tidlist
, int from_tty
)
1664 const char *cmd
= NULL
;
1665 tid_range_parser parser
;
1667 if (tidlist
== NULL
|| *tidlist
== '\000')
1668 error (_("Please specify a thread ID list"));
1670 parser
.init (tidlist
, current_inferior ()->num
);
1671 while (!parser
.finished ())
1673 int inf_num
, thr_start
, thr_end
;
1675 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1679 cmd
= parser
.cur_tok ();
1681 auto group
= make_thread_apply_options_def_group (&flags
);
1682 gdb::option::process_options
1683 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1685 validate_flags_qcs ("thread apply", &flags
);
1688 error (_("Please specify a command following the thread ID list"));
1690 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1691 invalid_thread_id_error (cmd
);
1693 scoped_restore_current_thread restore_thread
;
1695 parser
.init (tidlist
, current_inferior ()->num
);
1696 while (!parser
.finished ())
1698 struct thread_info
*tp
= NULL
;
1699 struct inferior
*inf
;
1700 int inf_num
, thr_num
;
1702 parser
.get_tid (&inf_num
, &thr_num
);
1703 inf
= find_inferior_id (inf_num
);
1705 tp
= find_thread_id (inf
, thr_num
);
1707 if (parser
.in_star_range ())
1711 warning (_("Unknown inferior %d"), inf_num
);
1712 parser
.skip_range ();
1716 /* No use looking for threads past the highest thread number
1717 the inferior ever had. */
1718 if (thr_num
>= inf
->highest_thread_num
)
1719 parser
.skip_range ();
1721 /* Be quiet about unknown threads numbers. */
1728 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1729 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1731 warning (_("Unknown thread %d"), thr_num
);
1735 if (!switch_to_thread_if_alive (tp
))
1737 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1741 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1746 /* Implementation of the "taas" command. */
1749 taas_command (const char *cmd
, int from_tty
)
1751 if (cmd
== NULL
|| *cmd
== '\0')
1752 error (_("Please specify a command to apply on all threads"));
1753 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1754 execute_command (expanded
.c_str (), from_tty
);
1757 /* Implementation of the "tfaas" command. */
1760 tfaas_command (const char *cmd
, int from_tty
)
1762 if (cmd
== NULL
|| *cmd
== '\0')
1763 error (_("Please specify a command to apply on all frames of all threads"));
1764 std::string expanded
1765 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1766 execute_command (expanded
.c_str (), from_tty
);
1769 /* Switch to the specified thread, or print the current thread. */
1772 thread_command (const char *tidstr
, int from_tty
)
1776 if (inferior_ptid
== null_ptid
)
1777 error (_("No thread selected"));
1779 if (target_has_stack ())
1781 struct thread_info
*tp
= inferior_thread ();
1783 if (tp
->state
== THREAD_EXITED
)
1784 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1785 print_thread_id (tp
),
1786 target_pid_to_str (inferior_ptid
).c_str ());
1788 printf_filtered (_("[Current thread is %s (%s)]\n"),
1789 print_thread_id (tp
),
1790 target_pid_to_str (inferior_ptid
).c_str ());
1793 error (_("No stack."));
1797 ptid_t previous_ptid
= inferior_ptid
;
1799 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1801 /* Print if the thread has not changed, otherwise an event will
1803 if (inferior_ptid
== previous_ptid
)
1805 print_selected_thread_frame (current_uiout
,
1806 USER_SELECTED_THREAD
1807 | USER_SELECTED_FRAME
);
1811 gdb::observers::user_selected_context_changed
.notify
1812 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1817 /* Implementation of `thread name'. */
1820 thread_name_command (const char *arg
, int from_tty
)
1822 struct thread_info
*info
;
1824 if (inferior_ptid
== null_ptid
)
1825 error (_("No thread selected"));
1827 arg
= skip_spaces (arg
);
1829 info
= inferior_thread ();
1830 info
->set_name (arg
!= nullptr ? make_unique_xstrdup (arg
) : nullptr);
1833 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1836 thread_find_command (const char *arg
, int from_tty
)
1839 unsigned long match
= 0;
1841 if (arg
== NULL
|| *arg
== '\0')
1842 error (_("Command requires an argument."));
1844 tmp
= re_comp (arg
);
1846 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1848 /* We're going to be switching threads. */
1849 scoped_restore_current_thread restore_thread
;
1851 update_thread_list ();
1853 for (thread_info
*tp
: all_threads ())
1855 switch_to_inferior_no_thread (tp
->inf
);
1857 if (tp
->name () != nullptr && re_exec (tp
->name ()))
1859 printf_filtered (_("Thread %s has name '%s'\n"),
1860 print_thread_id (tp
), tp
->name ());
1864 tmp
= target_thread_name (tp
);
1865 if (tmp
!= NULL
&& re_exec (tmp
))
1867 printf_filtered (_("Thread %s has target name '%s'\n"),
1868 print_thread_id (tp
), tmp
);
1872 std::string name
= target_pid_to_str (tp
->ptid
);
1873 if (!name
.empty () && re_exec (name
.c_str ()))
1875 printf_filtered (_("Thread %s has target id '%s'\n"),
1876 print_thread_id (tp
), name
.c_str ());
1880 tmp
= target_extra_thread_info (tp
);
1881 if (tmp
!= NULL
&& re_exec (tmp
))
1883 printf_filtered (_("Thread %s has extra info '%s'\n"),
1884 print_thread_id (tp
), tmp
);
1889 printf_filtered (_("No threads match '%s'\n"), arg
);
1892 /* Print notices when new threads are attached and detached. */
1893 bool print_thread_events
= true;
1895 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1896 struct cmd_list_element
*c
, const char *value
)
1898 fprintf_filtered (file
,
1899 _("Printing of thread events is %s.\n"),
1903 /* See gdbthread.h. */
1906 thread_select (const char *tidstr
, thread_info
*tp
)
1908 if (!switch_to_thread_if_alive (tp
))
1909 error (_("Thread ID %s has terminated."), tidstr
);
1911 annotate_thread_changed ();
1913 /* Since the current thread may have changed, see if there is any
1914 exited thread we can now delete. */
1915 delete_exited_threads ();
1918 /* Print thread and frame switch command response. */
1921 print_selected_thread_frame (struct ui_out
*uiout
,
1922 user_selected_what selection
)
1924 struct thread_info
*tp
= inferior_thread ();
1926 if (selection
& USER_SELECTED_THREAD
)
1928 if (uiout
->is_mi_like_p ())
1930 uiout
->field_signed ("new-thread-id",
1931 inferior_thread ()->global_num
);
1935 uiout
->text ("[Switching to thread ");
1936 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
1938 uiout
->text (target_pid_to_str (inferior_ptid
));
1943 if (tp
->state
== THREAD_RUNNING
)
1945 if (selection
& USER_SELECTED_THREAD
)
1946 uiout
->text ("(running)\n");
1948 else if (selection
& USER_SELECTED_FRAME
)
1950 if (selection
& USER_SELECTED_THREAD
)
1953 if (has_stack_frames ())
1954 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
1959 /* Update the 'threads_executing' global based on the threads we know
1960 about right now. This is used by infrun to tell whether we should
1961 pull events out of the current target. */
1964 update_threads_executing (void)
1966 process_stratum_target
*targ
= current_inferior ()->process_target ();
1971 targ
->threads_executing
= false;
1973 for (inferior
*inf
: all_non_exited_inferiors (targ
))
1975 if (!inf
->has_execution ())
1978 /* If the process has no threads, then it must be we have a
1979 process-exit event pending. */
1980 if (inf
->thread_list
.empty ())
1982 targ
->threads_executing
= true;
1986 for (thread_info
*tp
: inf
->non_exited_threads ())
1988 if (tp
->executing ())
1990 targ
->threads_executing
= true;
1998 update_thread_list (void)
2000 target_update_thread_list ();
2001 update_threads_executing ();
2004 /* See gdbthread.h. */
2007 thread_name (thread_info
*thread
)
2009 /* Use the manually set name if there is one. */
2010 const char *name
= thread
->name ();
2011 if (name
!= nullptr)
2014 /* Otherwise, ask the target. Ensure we query the right target stack. */
2015 scoped_restore_current_thread restore_thread
;
2016 if (thread
->inf
!= current_inferior ())
2017 switch_to_inferior_no_thread (thread
->inf
);
2019 return target_thread_name (thread
);
2022 /* Return a new value for the selected thread's id. Return a value of
2023 0 if no thread is selected. If GLOBAL is true, return the thread's
2024 global number. Otherwise return the per-inferior number. */
2026 static struct value
*
2027 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2031 if (inferior_ptid
== null_ptid
)
2035 thread_info
*tp
= inferior_thread ();
2037 int_val
= tp
->global_num
;
2039 int_val
= tp
->per_inf_num
;
2042 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2045 /* Return a new value for the selected thread's per-inferior thread
2046 number. Return a value of 0 if no thread is selected, or no
2049 static struct value
*
2050 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2051 struct internalvar
*var
,
2054 return thread_num_make_value_helper (gdbarch
, 0);
2057 /* Return a new value for the selected thread's global id. Return a
2058 value of 0 if no thread is selected, or no threads exist. */
2060 static struct value
*
2061 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2064 return thread_num_make_value_helper (gdbarch
, 1);
2067 /* Commands with a prefix of `thread'. */
2068 struct cmd_list_element
*thread_cmd_list
= NULL
;
2070 /* Implementation of `thread' variable. */
2072 static const struct internalvar_funcs thread_funcs
=
2074 thread_id_per_inf_num_make_value
,
2079 /* Implementation of `gthread' variable. */
2081 static const struct internalvar_funcs gthread_funcs
=
2083 global_thread_id_make_value
,
2088 void _initialize_thread ();
2090 _initialize_thread ()
2092 static struct cmd_list_element
*thread_apply_list
= NULL
;
2093 cmd_list_element
*c
;
2095 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2097 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2099 static std::string info_threads_help
2100 = gdb::option::build_help (_("\
2101 Display currently known threads.\n\
2102 Usage: info threads [OPTION]... [ID]...\n\
2103 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2104 Otherwise, all threads are displayed.\n\
2110 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2111 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2113 cmd_list_element
*thread_cmd
2114 = add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2115 Use this command to switch between threads.\n\
2116 The new thread ID must be currently known."),
2117 &thread_cmd_list
, 1, &cmdlist
);
2119 add_com_alias ("t", thread_cmd
, class_run
, 1);
2121 #define THREAD_APPLY_OPTION_HELP "\
2122 Prints per-inferior thread number and target system's thread id\n\
2123 followed by COMMAND output.\n\
2125 By default, an error raised during the execution of COMMAND\n\
2126 aborts \"thread apply\".\n\
2131 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2133 static std::string thread_apply_help
= gdb::option::build_help (_("\
2134 Apply a command to a list of threads.\n\
2135 Usage: thread apply ID... [OPTION]... COMMAND\n\
2136 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2137 THREAD_APPLY_OPTION_HELP
),
2140 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2141 thread_apply_help
.c_str (),
2142 &thread_apply_list
, 1,
2144 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2146 const auto thread_apply_all_opts
2147 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2149 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2150 Apply a command to all threads.\n\
2152 Usage: thread apply all [OPTION]... COMMAND\n"
2153 THREAD_APPLY_OPTION_HELP
),
2154 thread_apply_all_opts
);
2156 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2157 thread_apply_all_help
.c_str (),
2158 &thread_apply_list
);
2159 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2161 c
= add_com ("taas", class_run
, taas_command
, _("\
2162 Apply a command to all threads (ignoring errors and empty output).\n\
2163 Usage: taas [OPTION]... COMMAND\n\
2164 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2165 See \"help thread apply all\" for available options."));
2166 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2168 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2169 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2170 Usage: tfaas [OPTION]... COMMAND\n\
2171 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2172 See \"help frame apply all\" for available options."));
2173 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2175 add_cmd ("name", class_run
, thread_name_command
,
2176 _("Set the current thread's name.\n\
2177 Usage: thread name [NAME]\n\
2178 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2180 add_cmd ("find", class_run
, thread_find_command
, _("\
2181 Find threads that match a regular expression.\n\
2182 Usage: thread find REGEXP\n\
2183 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2186 add_setshow_boolean_cmd ("thread-events", no_class
,
2187 &print_thread_events
, _("\
2188 Set printing of thread events (such as thread start and exit)."), _("\
2189 Show printing of thread events (such as thread start and exit)."), NULL
,
2191 show_print_thread_events
,
2192 &setprintlist
, &showprintlist
);
2194 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2195 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);