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"
53 /* See gdbthread.h. */
55 bool debug_threads
= false;
57 /* Implement 'show debug threads'. */
60 show_debug_threads (struct ui_file
*file
, int from_tty
,
61 struct cmd_list_element
*c
, const char *value
)
63 gdb_printf (file
, _("Thread debugging is \"%s\".\n"), value
);
66 /* Definition of struct thread_info exported to gdbthread.h. */
68 /* Prototypes for local functions. */
70 static int highest_thread_num
;
72 /* The current/selected thread. */
73 static thread_info
*current_thread_
;
75 /* Returns true if THR is the current thread. */
78 is_current_thread (const thread_info
*thr
)
80 return thr
== current_thread_
;
84 inferior_thread (void)
86 gdb_assert (current_thread_
!= nullptr);
87 return current_thread_
;
90 /* Delete the breakpoint pointed at by BP_P, if there's one. */
93 delete_thread_breakpoint (struct breakpoint
**bp_p
)
97 delete_breakpoint (*bp_p
);
103 delete_step_resume_breakpoint (struct thread_info
*tp
)
106 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
110 delete_exception_resume_breakpoint (struct thread_info
*tp
)
113 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
116 /* See gdbthread.h. */
119 delete_single_step_breakpoints (struct thread_info
*tp
)
122 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
125 /* Delete the breakpoint pointed at by BP_P at the next stop, if
129 delete_at_next_stop (struct breakpoint
**bp
)
133 (*bp
)->disposition
= disp_del_at_next_stop
;
138 /* See gdbthread.h. */
141 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
143 return tp
->control
.single_step_breakpoints
!= NULL
;
146 /* See gdbthread.h. */
149 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
150 const address_space
*aspace
,
153 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
155 return (ss_bps
!= NULL
156 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
159 /* See gdbthread.h. */
162 thread_cancel_execution_command (struct thread_info
*thr
)
164 if (thr
->thread_fsm () != nullptr)
166 std::unique_ptr
<thread_fsm
> fsm
= thr
->release_thread_fsm ();
172 clear_thread_inferior_resources (struct thread_info
*tp
)
174 /* NOTE: this will take care of any left-over step_resume breakpoints,
175 but not any user-specified thread-specific breakpoints. We can not
176 delete the breakpoint straight-off, because the inferior might not
177 be stopped at the moment. */
178 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
179 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
180 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
182 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
184 bpstat_clear (&tp
->control
.stop_bpstat
);
186 btrace_teardown (tp
);
188 thread_cancel_execution_command (tp
);
190 clear_inline_frame_state (tp
);
193 /* See gdbthread.h. */
196 set_thread_exited (thread_info
*tp
, bool silent
)
198 /* Dead threads don't need to step-over. Remove from chain. */
199 if (thread_is_in_step_over_chain (tp
))
200 global_thread_step_over_chain_remove (tp
);
202 if (tp
->state
!= THREAD_EXITED
)
204 process_stratum_target
*proc_target
= tp
->inf
->process_target ();
206 /* Some targets unpush themselves from the inferior's target stack before
207 clearing the inferior's thread list (which marks all threads as exited,
208 and therefore leads to this function). In this case, the inferior's
209 process target will be nullptr when we arrive here.
211 See also the comment in inferior::unpush_target. */
212 if (proc_target
!= nullptr)
213 proc_target
->maybe_remove_resumed_with_pending_wait_status (tp
);
215 gdb::observers::thread_exit
.notify (tp
, silent
);
217 /* Tag it as exited. */
218 tp
->state
= THREAD_EXITED
;
220 /* Clear breakpoints, etc. associated with this thread. */
221 clear_thread_inferior_resources (tp
);
223 /* Remove from the ptid_t map. We don't want for
224 find_thread_ptid to find exited threads. Also, the target
225 may reuse the ptid for a new thread, and there can only be
226 one value per key; adding a new thread with the same ptid_t
227 would overwrite the exited thread's ptid entry. */
228 size_t nr_deleted
= tp
->inf
->ptid_thread_map
.erase (tp
->ptid
);
229 gdb_assert (nr_deleted
== 1);
234 init_thread_list (void)
236 highest_thread_num
= 0;
238 for (inferior
*inf
: all_inferiors ())
239 inf
->clear_thread_list (true);
242 /* Allocate a new thread of inferior INF with target id PTID and add
243 it to the thread list. */
245 static struct thread_info
*
246 new_thread (struct inferior
*inf
, ptid_t ptid
)
248 thread_info
*tp
= new thread_info (inf
, ptid
);
250 threads_debug_printf ("creating a new thread object, inferior %d, ptid %s",
251 inf
->num
, ptid
.to_string ().c_str ());
253 inf
->thread_list
.push_back (*tp
);
255 /* A thread with this ptid should not exist in the map yet. */
256 gdb_assert (inf
->ptid_thread_map
.find (ptid
) == inf
->ptid_thread_map
.end ());
258 inf
->ptid_thread_map
[ptid
] = tp
;
264 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
266 gdb_assert (targ
!= nullptr);
268 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
270 threads_debug_printf ("add thread to inferior %d, ptid %s, target %s",
271 inf
->num
, ptid
.to_string ().c_str (),
274 /* We may have an old thread with the same id in the thread list.
275 If we do, it must be dead, otherwise we wouldn't be adding a new
276 thread with the same id. The OS is reusing this id --- delete
277 the old thread, and create a new one. */
278 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
282 tp
= new_thread (inf
, ptid
);
283 gdb::observers::new_thread
.notify (tp
);
289 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
290 private_thread_info
*priv
)
292 thread_info
*result
= add_thread_silent (targ
, ptid
);
294 result
->priv
.reset (priv
);
296 if (print_thread_events
)
297 gdb_printf (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
299 annotate_new_thread ();
304 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
306 return add_thread_with_info (targ
, ptid
, NULL
);
309 private_thread_info::~private_thread_info () = default;
311 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
312 : ptid (ptid_
), inf (inf_
)
314 gdb_assert (inf_
!= NULL
);
316 this->global_num
= ++highest_thread_num
;
317 this->per_inf_num
= ++inf_
->highest_thread_num
;
319 /* Nothing to follow yet. */
320 this->pending_follow
.set_spurious ();
323 /* See gdbthread.h. */
325 thread_info::~thread_info ()
327 threads_debug_printf ("thread %s", this->ptid
.to_string ().c_str ());
330 /* See gdbthread.h. */
333 thread_info::deletable () const
335 /* If this is the current thread, or there's code out there that
336 relies on it existing (refcount > 0) we can't delete yet. */
337 return refcount () == 0 && !is_current_thread (this);
340 /* See gdbthread.h. */
343 thread_info::set_executing (bool executing
)
345 m_executing
= executing
;
347 this->clear_stop_pc ();
350 /* See gdbthread.h. */
353 thread_info::set_resumed (bool resumed
)
355 if (resumed
== m_resumed
)
358 process_stratum_target
*proc_target
= this->inf
->process_target ();
360 /* If we transition from resumed to not resumed, we might need to remove
361 the thread from the resumed threads with pending statuses list. */
363 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
367 /* If we transition from not resumed to resumed, we might need to add
368 the thread to the resumed threads with pending statuses list. */
370 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
373 /* See gdbthread.h. */
376 thread_info::set_pending_waitstatus (const target_waitstatus
&ws
)
378 gdb_assert (!this->has_pending_waitstatus ());
380 m_suspend
.waitstatus
= ws
;
381 m_suspend
.waitstatus_pending_p
= 1;
383 process_stratum_target
*proc_target
= this->inf
->process_target ();
384 proc_target
->maybe_add_resumed_with_pending_wait_status (this);
387 /* See gdbthread.h. */
390 thread_info::clear_pending_waitstatus ()
392 gdb_assert (this->has_pending_waitstatus ());
394 process_stratum_target
*proc_target
= this->inf
->process_target ();
395 proc_target
->maybe_remove_resumed_with_pending_wait_status (this);
397 m_suspend
.waitstatus_pending_p
= 0;
400 /* See gdbthread.h. */
403 thread_is_in_step_over_chain (struct thread_info
*tp
)
405 return tp
->step_over_list_node
.is_linked ();
408 /* See gdbthread.h. */
411 thread_step_over_chain_length (const thread_step_over_list
&l
)
415 for (const thread_info
&thread ATTRIBUTE_UNUSED
: l
)
421 /* See gdbthread.h. */
424 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
426 infrun_debug_printf ("enqueueing thread %s in global step over chain",
427 tp
->ptid
.to_string ().c_str ());
429 gdb_assert (!thread_is_in_step_over_chain (tp
));
430 global_thread_step_over_list
.push_back (*tp
);
433 /* See gdbthread.h. */
436 global_thread_step_over_chain_enqueue_chain (thread_step_over_list
&&list
)
438 global_thread_step_over_list
.splice (std::move (list
));
441 /* See gdbthread.h. */
444 global_thread_step_over_chain_remove (struct thread_info
*tp
)
446 infrun_debug_printf ("removing thread %s from global step over chain",
447 tp
->ptid
.to_string ().c_str ());
449 gdb_assert (thread_is_in_step_over_chain (tp
));
450 auto it
= global_thread_step_over_list
.iterator_to (*tp
);
451 global_thread_step_over_list
.erase (it
);
454 /* Delete the thread referenced by THR. If SILENT, don't notify
455 the observer of this exit.
457 THR must not be NULL or a failed assertion will be raised. */
460 delete_thread_1 (thread_info
*thr
, bool silent
)
462 gdb_assert (thr
!= nullptr);
464 threads_debug_printf ("deleting thread %s, silent = %d",
465 thr
->ptid
.to_string ().c_str (), silent
);
467 set_thread_exited (thr
, silent
);
469 if (!thr
->deletable ())
471 /* Will be really deleted some other time. */
475 auto it
= thr
->inf
->thread_list
.iterator_to (*thr
);
476 thr
->inf
->thread_list
.erase (it
);
481 /* See gdbthread.h. */
484 delete_thread (thread_info
*thread
)
486 delete_thread_1 (thread
, false /* not silent */);
490 delete_thread_silent (thread_info
*thread
)
492 delete_thread_1 (thread
, true /* silent */);
496 find_thread_global_id (int global_id
)
498 for (thread_info
*tp
: all_threads ())
499 if (tp
->global_num
== global_id
)
505 static struct thread_info
*
506 find_thread_id (struct inferior
*inf
, int thr_num
)
508 for (thread_info
*tp
: inf
->threads ())
509 if (tp
->per_inf_num
== thr_num
)
515 /* See gdbthread.h. */
518 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
520 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
523 return find_thread_ptid (inf
, ptid
);
526 /* See gdbthread.h. */
529 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
531 gdb_assert (inf
!= nullptr);
533 auto it
= inf
->ptid_thread_map
.find (ptid
);
534 if (it
!= inf
->ptid_thread_map
.end ())
540 /* See gdbthread.h. */
543 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
544 struct inferior
*inf
)
546 return target_thread_handle_to_thread_info (handle
.data (),
552 * Thread iterator function.
554 * Calls a callback function once for each thread, so long as
555 * the callback function returns false. If the callback function
556 * returns true, the iteration will end and the current thread
557 * will be returned. This can be useful for implementing a
558 * search for a thread with arbitrary attributes, or for applying
559 * some operation to every thread.
561 * FIXME: some of the existing functionality, such as
562 * "Thread apply all", might be rewritten using this functionality.
566 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
569 for (thread_info
*tp
: all_threads_safe ())
570 if ((*callback
) (tp
, data
))
576 /* See gdbthread.h. */
581 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
587 thread_count (process_stratum_target
*proc_target
)
589 auto rng
= all_threads (proc_target
);
590 return std::distance (rng
.begin (), rng
.end ());
593 /* Return the number of non-exited threads in the thread list. */
596 live_threads_count (void)
598 auto rng
= all_non_exited_threads ();
599 return std::distance (rng
.begin (), rng
.end ());
603 valid_global_thread_id (int global_id
)
605 for (thread_info
*tp
: all_threads ())
606 if (tp
->global_num
== global_id
)
613 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
615 return find_thread_ptid (targ
, ptid
) != nullptr;
618 /* Finds the first thread of the inferior. */
621 first_thread_of_inferior (inferior
*inf
)
623 if (inf
->thread_list
.empty ())
626 return &inf
->thread_list
.front ();
630 any_thread_of_inferior (inferior
*inf
)
632 gdb_assert (inf
->pid
!= 0);
634 /* Prefer the current thread, if there's one. */
635 if (inf
== current_inferior () && inferior_ptid
!= null_ptid
)
636 return inferior_thread ();
638 for (thread_info
*tp
: inf
->non_exited_threads ())
645 any_live_thread_of_inferior (inferior
*inf
)
647 struct thread_info
*curr_tp
= NULL
;
648 struct thread_info
*tp_executing
= NULL
;
650 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
652 /* Prefer the current thread if it's not executing. */
653 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
655 /* If the current thread is dead, forget it. If it's not
656 executing, use it. Otherwise, still choose it (below), but
657 only if no other non-executing thread is found. */
658 curr_tp
= inferior_thread ();
659 if (curr_tp
->state
== THREAD_EXITED
)
661 else if (!curr_tp
->executing ())
665 for (thread_info
*tp
: inf
->non_exited_threads ())
667 if (!tp
->executing ())
673 /* If both the current thread and all live threads are executing,
674 prefer the current thread. */
678 /* Otherwise, just return an executing thread, if any. */
682 /* Return true if TP is an active thread. */
684 thread_alive (thread_info
*tp
)
686 if (tp
->state
== THREAD_EXITED
)
689 /* Ensure we're looking at the right target stack. */
690 gdb_assert (tp
->inf
== current_inferior ());
692 return target_thread_alive (tp
->ptid
);
695 /* See gdbthreads.h. */
698 switch_to_thread_if_alive (thread_info
*thr
)
700 scoped_restore_current_thread restore_thread
;
702 /* Switch inferior first, so that we're looking at the right target
704 switch_to_inferior_no_thread (thr
->inf
);
706 if (thread_alive (thr
))
708 switch_to_thread (thr
);
709 restore_thread
.dont_restore ();
716 /* See gdbthreads.h. */
721 scoped_restore_current_thread restore_thread
;
723 for (thread_info
*tp
: all_threads_safe ())
725 switch_to_inferior_no_thread (tp
->inf
);
727 if (!thread_alive (tp
))
732 /* See gdbthreads.h. */
735 delete_exited_threads (void)
737 for (thread_info
*tp
: all_threads_safe ())
738 if (tp
->state
== THREAD_EXITED
)
742 /* Return true value if stack temporaries are enabled for the thread
746 thread_stack_temporaries_enabled_p (thread_info
*tp
)
751 return tp
->stack_temporaries_enabled
;
754 /* Push V on to the stack temporaries of the thread with id PTID. */
757 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
759 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
760 tp
->stack_temporaries
.push_back (v
);
763 /* Return true if VAL is among the stack temporaries of the thread
764 TP. Return false otherwise. */
767 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
769 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
770 for (value
*v
: tp
->stack_temporaries
)
777 /* Return the last of the stack temporaries for thread with id PTID.
778 Return NULL if there are no stack temporaries for the thread. */
781 get_last_thread_stack_temporary (thread_info
*tp
)
783 struct value
*lastval
= NULL
;
785 gdb_assert (tp
!= NULL
);
786 if (!tp
->stack_temporaries
.empty ())
787 lastval
= tp
->stack_temporaries
.back ();
793 thread_change_ptid (process_stratum_target
*targ
,
794 ptid_t old_ptid
, ptid_t new_ptid
)
796 struct inferior
*inf
;
797 struct thread_info
*tp
;
799 /* It can happen that what we knew as the target inferior id
800 changes. E.g, target remote may only discover the remote process
801 pid after adding the inferior to GDB's list. */
802 inf
= find_inferior_ptid (targ
, old_ptid
);
803 inf
->pid
= new_ptid
.pid ();
805 tp
= find_thread_ptid (inf
, old_ptid
);
806 gdb_assert (tp
!= nullptr);
808 int num_erased
= inf
->ptid_thread_map
.erase (old_ptid
);
809 gdb_assert (num_erased
== 1);
812 inf
->ptid_thread_map
[new_ptid
] = tp
;
814 gdb::observers::thread_ptid_changed
.notify (targ
, old_ptid
, new_ptid
);
817 /* See gdbthread.h. */
820 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
822 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
823 tp
->set_resumed (resumed
);
826 /* Helper for set_running, that marks one thread either running or
830 set_running_thread (struct thread_info
*tp
, bool running
)
832 bool started
= false;
834 if (running
&& tp
->state
== THREAD_STOPPED
)
836 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
838 threads_debug_printf ("thread: %s, running? %d%s",
839 tp
->ptid
.to_string ().c_str (), running
,
840 (started
? " (started)" : ""));
844 /* If the thread is now marked stopped, remove it from
845 the step-over queue, so that we don't try to resume
846 it until the user wants it to. */
847 if (thread_is_in_step_over_chain (tp
))
848 global_thread_step_over_chain_remove (tp
);
854 /* See gdbthread.h. */
857 thread_info::set_running (bool running
)
859 if (set_running_thread (this, running
))
860 gdb::observers::target_resumed
.notify (this->ptid
);
864 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
866 /* We try not to notify the observer if no thread has actually
867 changed the running state -- merely to reduce the number of
868 messages to the MI frontend. A frontend is supposed to handle
869 multiple *running notifications just fine. */
870 bool any_started
= false;
872 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
873 if (set_running_thread (tp
, running
))
877 gdb::observers::target_resumed
.notify (ptid
);
881 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
883 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
884 tp
->set_executing (executing
);
886 /* It only takes one running thread to spawn more threads. */
888 targ
->threads_executing
= true;
889 /* Only clear the flag if the caller is telling us everything is
891 else if (minus_one_ptid
== ptid
)
892 targ
->threads_executing
= false;
895 /* See gdbthread.h. */
898 threads_are_executing (process_stratum_target
*target
)
900 return target
->threads_executing
;
904 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
906 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
907 tp
->stop_requested
= stop
;
909 /* Call the stop requested observer so other components of GDB can
910 react to this request. */
912 gdb::observers::thread_stop_requested
.notify (ptid
);
916 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
918 bool any_started
= false;
920 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
921 if (set_running_thread (tp
, tp
->executing ()))
925 gdb::observers::target_resumed
.notify (ptid
);
928 /* See gdbthread.h. */
931 validate_registers_access (void)
933 /* No selected thread, no registers. */
934 if (inferior_ptid
== null_ptid
)
935 error (_("No thread selected."));
937 thread_info
*tp
= inferior_thread ();
939 /* Don't try to read from a dead thread. */
940 if (tp
->state
== THREAD_EXITED
)
941 error (_("The current thread has terminated"));
943 /* ... or from a spinning thread. FIXME: This isn't actually fully
944 correct. It'll allow an user-requested access (e.g., "print $pc"
945 at the prompt) when a thread is not executing for some internal
946 reason, but is marked running from the user's perspective. E.g.,
947 the thread is waiting for its turn in the step-over queue. */
948 if (tp
->executing ())
949 error (_("Selected thread is running."));
952 /* See gdbthread.h. */
955 can_access_registers_thread (thread_info
*thread
)
957 /* No thread, no registers. */
961 /* Don't try to read from a dead thread. */
962 if (thread
->state
== THREAD_EXITED
)
965 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
966 if (thread
->executing ())
973 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
975 return (pc
>= thread
->control
.step_range_start
976 && pc
< thread
->control
.step_range_end
);
979 /* Helper for print_thread_info. Returns true if THR should be
980 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
981 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
982 is true if REQUESTED_THREADS is list of global IDs, false if a list
983 of per-inferior thread ids. If PID is not -1, only print THR if it
984 is a thread from the process PID. Otherwise, threads from all
985 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
986 and PID is not -1, then the thread is printed if it belongs to the
987 specified process. Otherwise, an error is raised. */
990 should_print_thread (const char *requested_threads
, int default_inf_num
,
991 int global_ids
, int pid
, struct thread_info
*thr
)
993 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
998 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1000 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1001 thr
->inf
->num
, thr
->per_inf_num
);
1006 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1008 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1009 error (_("Requested thread not found in requested process"));
1013 if (thr
->state
== THREAD_EXITED
)
1019 /* Return the string to display in "info threads"'s "Target Id"
1023 thread_target_id_str (thread_info
*tp
)
1025 std::string target_id
= target_pid_to_str (tp
->ptid
);
1026 const char *extra_info
= target_extra_thread_info (tp
);
1027 const char *name
= thread_name (tp
);
1029 if (extra_info
!= nullptr && name
!= nullptr)
1030 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1032 else if (extra_info
!= nullptr)
1033 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1034 else if (name
!= nullptr)
1035 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1040 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1041 whether REQUESTED_THREADS is a list of global or per-inferior
1045 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1046 int global_ids
, int pid
,
1047 int show_global_ids
)
1049 int default_inf_num
= current_inferior ()->num
;
1051 update_thread_list ();
1053 /* Whether we saw any thread. */
1054 bool any_thread
= false;
1055 /* Whether the current thread is exited. */
1056 bool current_exited
= false;
1058 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1059 ? inferior_thread () : NULL
);
1062 /* For backward compatibility, we make a list for MI. A table is
1063 preferable for the CLI, though, because it shows table
1065 gdb::optional
<ui_out_emit_list
> list_emitter
;
1066 gdb::optional
<ui_out_emit_table
> table_emitter
;
1068 /* We'll be switching threads temporarily below. */
1069 scoped_restore_current_thread restore_thread
;
1071 if (uiout
->is_mi_like_p ())
1072 list_emitter
.emplace (uiout
, "threads");
1076 /* The width of the "Target Id" column. Grown below to
1077 accommodate the largest entry. */
1078 size_t target_id_col_width
= 17;
1080 for (thread_info
*tp
: all_threads ())
1082 if (!should_print_thread (requested_threads
, default_inf_num
,
1083 global_ids
, pid
, tp
))
1086 if (!uiout
->is_mi_like_p ())
1088 /* Switch inferiors so we're looking at the right
1090 switch_to_inferior_no_thread (tp
->inf
);
1093 = std::max (target_id_col_width
,
1094 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
= find_thread_ptid (proc_target
, 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 /* See gdbthread.h. */
1412 show_thread_that_caused_stop (void)
1414 return highest_thread_num
> 1;
1417 /* See gdbthread.h. */
1420 show_inferior_qualified_tids (void)
1422 auto inf
= inferior_list
.begin ();
1426 return inf
!= inferior_list
.end ();
1429 /* See gdbthread.h. */
1432 print_thread_id (struct thread_info
*thr
)
1434 gdb_assert (thr
!= nullptr);
1436 char *s
= get_print_cell ();
1438 if (show_inferior_qualified_tids ())
1439 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1441 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1445 /* Sort an array of struct thread_info pointers by thread ID (first by
1446 inferior number, and then by per-inferior thread number). Sorts in
1450 tp_array_compar_ascending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1452 if (a
->inf
->num
!= b
->inf
->num
)
1453 return a
->inf
->num
< b
->inf
->num
;
1455 return (a
->per_inf_num
< b
->per_inf_num
);
1458 /* Sort an array of struct thread_info pointers by thread ID (first by
1459 inferior number, and then by per-inferior thread number). Sorts in
1460 descending order. */
1463 tp_array_compar_descending (const thread_info_ref
&a
, const thread_info_ref
&b
)
1465 if (a
->inf
->num
!= b
->inf
->num
)
1466 return a
->inf
->num
> b
->inf
->num
;
1468 return (a
->per_inf_num
> b
->per_inf_num
);
1471 /* See gdbthread.h. */
1474 thread_try_catch_cmd (thread_info
*thr
, gdb::optional
<int> ada_task
,
1475 const char *cmd
, int from_tty
,
1476 const qcs_flags
&flags
)
1478 gdb_assert (is_current_thread (thr
));
1480 /* The thread header is computed before running the command since
1481 the command can change the inferior, which is not permitted
1482 by thread_target_id_str. */
1483 std::string thr_header
;
1484 if (ada_task
.has_value ())
1485 thr_header
= string_printf (_("\nTask ID %d:\n"), *ada_task
);
1487 thr_header
= string_printf (_("\nThread %s (%s):\n"),
1488 print_thread_id (thr
),
1489 thread_target_id_str (thr
).c_str ());
1493 std::string cmd_result
;
1494 execute_command_to_string
1495 (cmd_result
, cmd
, from_tty
, gdb_stdout
->term_out ());
1496 if (!flags
.silent
|| cmd_result
.length () > 0)
1499 gdb_printf ("%s", thr_header
.c_str ());
1500 gdb_printf ("%s", cmd_result
.c_str ());
1503 catch (const gdb_exception_error
&ex
)
1508 gdb_printf ("%s", thr_header
.c_str ());
1510 gdb_printf ("%s\n", ex
.what ());
1517 /* Option definition of "thread apply"'s "-ascending" option. */
1519 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1522 Call COMMAND for all threads in ascending order.\n\
1523 The default is descending order."),
1526 /* The qcs command line flags for the "thread apply" commands. Keep
1527 this in sync with the "frame apply" commands. */
1529 using qcs_flag_option_def
1530 = gdb::option::flag_option_def
<qcs_flags
>;
1532 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1533 qcs_flag_option_def
{
1534 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1535 N_("Disables printing the thread information."),
1538 qcs_flag_option_def
{
1539 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1540 N_("Print any error raised by COMMAND and continue."),
1543 qcs_flag_option_def
{
1544 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1545 N_("Silently ignore any errors or empty output produced by COMMAND."),
1549 /* Create an option_def_group for the "thread apply all" options, with
1550 ASCENDING and FLAGS as context. */
1552 static inline std::array
<gdb::option::option_def_group
, 2>
1553 make_thread_apply_all_options_def_group (bool *ascending
,
1557 { {ascending_option_def
.def ()}, ascending
},
1558 { {thr_qcs_flags_option_defs
}, flags
},
1562 /* Create an option_def_group for the "thread apply" options, with
1563 FLAGS as context. */
1565 static inline gdb::option::option_def_group
1566 make_thread_apply_options_def_group (qcs_flags
*flags
)
1568 return {{thr_qcs_flags_option_defs
}, flags
};
1571 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1572 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1573 of two numbers separated by a hyphen. Examples:
1575 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1576 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1577 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1580 thread_apply_all_command (const char *cmd
, int from_tty
)
1582 bool ascending
= false;
1585 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1587 gdb::option::process_options
1588 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1590 validate_flags_qcs ("thread apply all", &flags
);
1592 if (cmd
== NULL
|| *cmd
== '\000')
1593 error (_("Please specify a command at the end of 'thread apply all'"));
1595 update_thread_list ();
1597 int tc
= live_threads_count ();
1600 /* Save a copy of the thread list and increment each thread's
1601 refcount while executing the command in the context of each
1602 thread, in case the command is one that wipes threads. E.g.,
1603 detach, kill, disconnect, etc., or even normally continuing
1604 over an inferior or thread exit. */
1605 std::vector
<thread_info_ref
> thr_list_cpy
;
1606 thr_list_cpy
.reserve (tc
);
1608 for (thread_info
*tp
: all_non_exited_threads ())
1609 thr_list_cpy
.push_back (thread_info_ref::new_reference (tp
));
1610 gdb_assert (thr_list_cpy
.size () == tc
);
1612 auto *sorter
= (ascending
1613 ? tp_array_compar_ascending
1614 : tp_array_compar_descending
);
1615 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1617 scoped_restore_current_thread restore_thread
;
1619 for (thread_info_ref
&thr
: thr_list_cpy
)
1620 if (switch_to_thread_if_alive (thr
.get ()))
1621 thread_try_catch_cmd (thr
.get (), {}, cmd
, from_tty
, flags
);
1625 /* Completer for "thread apply [ID list]". */
1628 thread_apply_command_completer (cmd_list_element
*ignore
,
1629 completion_tracker
&tracker
,
1630 const char *text
, const char * /*word*/)
1632 /* Don't leave this to complete_options because there's an early
1634 tracker
.set_use_custom_word_point (true);
1636 tid_range_parser parser
;
1637 parser
.init (text
, current_inferior ()->num
);
1641 while (!parser
.finished ())
1643 int inf_num
, thr_start
, thr_end
;
1645 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1648 if (parser
.in_star_range () || parser
.in_thread_range ())
1649 parser
.skip_range ();
1652 catch (const gdb_exception_error
&ex
)
1654 /* get_tid_range throws if it parses a negative number, for
1655 example. But a seemingly negative number may be the start of
1656 an option instead. */
1659 const char *cmd
= parser
.cur_tok ();
1663 /* No thread ID list yet. */
1667 /* Check if we're past a valid thread ID list already. */
1668 if (parser
.finished ()
1669 && cmd
> text
&& !isspace (cmd
[-1]))
1672 /* We're past the thread ID list, advance word point. */
1673 tracker
.advance_custom_word_point_by (cmd
- text
);
1676 const auto group
= make_thread_apply_options_def_group (nullptr);
1677 if (gdb::option::complete_options
1678 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1681 complete_nested_command_line (tracker
, text
);
1684 /* Completer for "thread apply all". */
1687 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1688 completion_tracker
&tracker
,
1689 const char *text
, const char *word
)
1691 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1693 if (gdb::option::complete_options
1694 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1697 complete_nested_command_line (tracker
, text
);
1700 /* Implementation of the "thread apply" command. */
1703 thread_apply_command (const char *tidlist
, int from_tty
)
1706 const char *cmd
= NULL
;
1707 tid_range_parser parser
;
1709 if (tidlist
== NULL
|| *tidlist
== '\000')
1710 error (_("Please specify a thread ID list"));
1712 parser
.init (tidlist
, current_inferior ()->num
);
1713 while (!parser
.finished ())
1715 int inf_num
, thr_start
, thr_end
;
1717 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1721 cmd
= parser
.cur_tok ();
1723 auto group
= make_thread_apply_options_def_group (&flags
);
1724 gdb::option::process_options
1725 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1727 validate_flags_qcs ("thread apply", &flags
);
1730 error (_("Please specify a command following the thread ID list"));
1732 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1733 invalid_thread_id_error (cmd
);
1735 scoped_restore_current_thread restore_thread
;
1737 parser
.init (tidlist
, current_inferior ()->num
);
1738 while (!parser
.finished ())
1740 struct thread_info
*tp
= NULL
;
1741 struct inferior
*inf
;
1742 int inf_num
, thr_num
;
1744 parser
.get_tid (&inf_num
, &thr_num
);
1745 inf
= find_inferior_id (inf_num
);
1747 tp
= find_thread_id (inf
, thr_num
);
1749 if (parser
.in_star_range ())
1753 warning (_("Unknown inferior %d"), inf_num
);
1754 parser
.skip_range ();
1758 /* No use looking for threads past the highest thread number
1759 the inferior ever had. */
1760 if (thr_num
>= inf
->highest_thread_num
)
1761 parser
.skip_range ();
1763 /* Be quiet about unknown threads numbers. */
1770 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1771 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1773 warning (_("Unknown thread %d"), thr_num
);
1777 if (!switch_to_thread_if_alive (tp
))
1779 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1783 thread_try_catch_cmd (tp
, {}, cmd
, from_tty
, flags
);
1788 /* Implementation of the "taas" command. */
1791 taas_command (const char *cmd
, int from_tty
)
1793 if (cmd
== NULL
|| *cmd
== '\0')
1794 error (_("Please specify a command to apply on all threads"));
1795 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1796 execute_command (expanded
.c_str (), from_tty
);
1799 /* Implementation of the "tfaas" command. */
1802 tfaas_command (const char *cmd
, int from_tty
)
1804 if (cmd
== NULL
|| *cmd
== '\0')
1805 error (_("Please specify a command to apply on all frames of all threads"));
1806 std::string expanded
1807 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1808 execute_command (expanded
.c_str (), from_tty
);
1811 /* Switch to the specified thread, or print the current thread. */
1814 thread_command (const char *tidstr
, int from_tty
)
1818 if (inferior_ptid
== null_ptid
)
1819 error (_("No thread selected"));
1821 if (target_has_stack ())
1823 struct thread_info
*tp
= inferior_thread ();
1825 if (tp
->state
== THREAD_EXITED
)
1826 gdb_printf (_("[Current thread is %s (%s) (exited)]\n"),
1827 print_thread_id (tp
),
1828 target_pid_to_str (inferior_ptid
).c_str ());
1830 gdb_printf (_("[Current thread is %s (%s)]\n"),
1831 print_thread_id (tp
),
1832 target_pid_to_str (inferior_ptid
).c_str ());
1835 error (_("No stack."));
1839 ptid_t previous_ptid
= inferior_ptid
;
1841 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1843 /* Print if the thread has not changed, otherwise an event will
1845 if (inferior_ptid
== previous_ptid
)
1847 print_selected_thread_frame (current_uiout
,
1848 USER_SELECTED_THREAD
1849 | USER_SELECTED_FRAME
);
1853 gdb::observers::user_selected_context_changed
.notify
1854 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1859 /* Implementation of `thread name'. */
1862 thread_name_command (const char *arg
, int from_tty
)
1864 struct thread_info
*info
;
1866 if (inferior_ptid
== null_ptid
)
1867 error (_("No thread selected"));
1869 arg
= skip_spaces (arg
);
1871 info
= inferior_thread ();
1872 info
->set_name (arg
!= nullptr ? make_unique_xstrdup (arg
) : nullptr);
1875 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1878 thread_find_command (const char *arg
, int from_tty
)
1881 unsigned long match
= 0;
1883 if (arg
== NULL
|| *arg
== '\0')
1884 error (_("Command requires an argument."));
1886 tmp
= re_comp (arg
);
1888 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1890 /* We're going to be switching threads. */
1891 scoped_restore_current_thread restore_thread
;
1893 update_thread_list ();
1895 for (thread_info
*tp
: all_threads ())
1897 switch_to_inferior_no_thread (tp
->inf
);
1899 if (tp
->name () != nullptr && re_exec (tp
->name ()))
1901 gdb_printf (_("Thread %s has name '%s'\n"),
1902 print_thread_id (tp
), tp
->name ());
1906 tmp
= target_thread_name (tp
);
1907 if (tmp
!= NULL
&& re_exec (tmp
))
1909 gdb_printf (_("Thread %s has target name '%s'\n"),
1910 print_thread_id (tp
), tmp
);
1914 std::string name
= target_pid_to_str (tp
->ptid
);
1915 if (!name
.empty () && re_exec (name
.c_str ()))
1917 gdb_printf (_("Thread %s has target id '%s'\n"),
1918 print_thread_id (tp
), name
.c_str ());
1922 tmp
= target_extra_thread_info (tp
);
1923 if (tmp
!= NULL
&& re_exec (tmp
))
1925 gdb_printf (_("Thread %s has extra info '%s'\n"),
1926 print_thread_id (tp
), tmp
);
1931 gdb_printf (_("No threads match '%s'\n"), arg
);
1934 /* Print notices when new threads are attached and detached. */
1935 bool print_thread_events
= true;
1937 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1938 struct cmd_list_element
*c
, const char *value
)
1941 _("Printing of thread events is %s.\n"),
1945 /* See gdbthread.h. */
1948 thread_select (const char *tidstr
, thread_info
*tp
)
1950 if (!switch_to_thread_if_alive (tp
))
1951 error (_("Thread ID %s has terminated."), tidstr
);
1953 annotate_thread_changed ();
1955 /* Since the current thread may have changed, see if there is any
1956 exited thread we can now delete. */
1957 delete_exited_threads ();
1960 /* Print thread and frame switch command response. */
1963 print_selected_thread_frame (struct ui_out
*uiout
,
1964 user_selected_what selection
)
1966 struct thread_info
*tp
= inferior_thread ();
1968 if (selection
& USER_SELECTED_THREAD
)
1970 if (uiout
->is_mi_like_p ())
1972 uiout
->field_signed ("new-thread-id",
1973 inferior_thread ()->global_num
);
1977 uiout
->text ("[Switching to thread ");
1978 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
1980 uiout
->text (target_pid_to_str (inferior_ptid
));
1985 if (tp
->state
== THREAD_RUNNING
)
1987 if (selection
& USER_SELECTED_THREAD
)
1988 uiout
->text ("(running)\n");
1990 else if (selection
& USER_SELECTED_FRAME
)
1992 if (selection
& USER_SELECTED_THREAD
)
1995 if (has_stack_frames ())
1996 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2001 /* Update the 'threads_executing' global based on the threads we know
2002 about right now. This is used by infrun to tell whether we should
2003 pull events out of the current target. */
2006 update_threads_executing (void)
2008 process_stratum_target
*targ
= current_inferior ()->process_target ();
2013 targ
->threads_executing
= false;
2015 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2017 if (!inf
->has_execution ())
2020 /* If the process has no threads, then it must be we have a
2021 process-exit event pending. */
2022 if (inf
->thread_list
.empty ())
2024 targ
->threads_executing
= true;
2028 for (thread_info
*tp
: inf
->non_exited_threads ())
2030 if (tp
->executing ())
2032 targ
->threads_executing
= true;
2040 update_thread_list (void)
2042 target_update_thread_list ();
2043 update_threads_executing ();
2046 /* See gdbthread.h. */
2049 thread_name (thread_info
*thread
)
2051 /* Use the manually set name if there is one. */
2052 const char *name
= thread
->name ();
2053 if (name
!= nullptr)
2056 /* Otherwise, ask the target. Ensure we query the right target stack. */
2057 scoped_restore_current_thread restore_thread
;
2058 if (thread
->inf
!= current_inferior ())
2059 switch_to_inferior_no_thread (thread
->inf
);
2061 return target_thread_name (thread
);
2064 /* See gdbthread.h. */
2067 thread_state_string (enum thread_state state
)
2071 case THREAD_STOPPED
:
2074 case THREAD_RUNNING
:
2081 gdb_assert_not_reached ("unknown thread state");
2084 /* Return a new value for the selected thread's id. Return a value of
2085 0 if no thread is selected. If GLOBAL is true, return the thread's
2086 global number. Otherwise return the per-inferior number. */
2088 static struct value
*
2089 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2093 if (inferior_ptid
== null_ptid
)
2097 thread_info
*tp
= inferior_thread ();
2099 int_val
= tp
->global_num
;
2101 int_val
= tp
->per_inf_num
;
2104 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2107 /* Return a new value for the selected thread's per-inferior thread
2108 number. Return a value of 0 if no thread is selected, or no
2111 static struct value
*
2112 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2113 struct internalvar
*var
,
2116 return thread_num_make_value_helper (gdbarch
, 0);
2119 /* Return a new value for the selected thread's global id. Return a
2120 value of 0 if no thread is selected, or no threads exist. */
2122 static struct value
*
2123 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2126 return thread_num_make_value_helper (gdbarch
, 1);
2129 /* Return a new value for the number of non-exited threads in the current
2130 inferior. If there are no threads in the current inferior return a
2133 static struct value
*
2134 inferior_thread_count_make_value (struct gdbarch
*gdbarch
,
2135 struct internalvar
*var
, void *ignore
)
2139 if (inferior_ptid
!= null_ptid
)
2140 int_val
= current_inferior ()->non_exited_threads ().size ();
2142 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2145 /* Commands with a prefix of `thread'. */
2146 struct cmd_list_element
*thread_cmd_list
= NULL
;
2148 /* Implementation of `thread' variable. */
2150 static const struct internalvar_funcs thread_funcs
=
2152 thread_id_per_inf_num_make_value
,
2156 /* Implementation of `gthread' variable. */
2158 static const struct internalvar_funcs gthread_funcs
=
2160 global_thread_id_make_value
,
2164 /* Implementation of `_inferior_thread_count` convenience variable. */
2166 static const struct internalvar_funcs inferior_thread_count_funcs
=
2168 inferior_thread_count_make_value
,
2172 void _initialize_thread ();
2174 _initialize_thread ()
2176 static struct cmd_list_element
*thread_apply_list
= NULL
;
2177 cmd_list_element
*c
;
2179 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2181 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2183 static std::string info_threads_help
2184 = gdb::option::build_help (_("\
2185 Display currently known threads.\n\
2186 Usage: info threads [OPTION]... [ID]...\n\
2187 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2188 Otherwise, all threads are displayed.\n\
2194 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2195 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2197 cmd_list_element
*thread_cmd
2198 = add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2199 Use this command to switch between threads.\n\
2200 The new thread ID must be currently known."),
2201 &thread_cmd_list
, 1, &cmdlist
);
2203 add_com_alias ("t", thread_cmd
, class_run
, 1);
2205 #define THREAD_APPLY_OPTION_HELP "\
2206 Prints per-inferior thread number and target system's thread id\n\
2207 followed by COMMAND output.\n\
2209 By default, an error raised during the execution of COMMAND\n\
2210 aborts \"thread apply\".\n\
2215 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2217 static std::string thread_apply_help
= gdb::option::build_help (_("\
2218 Apply a command to a list of threads.\n\
2219 Usage: thread apply ID... [OPTION]... COMMAND\n\
2220 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2221 THREAD_APPLY_OPTION_HELP
),
2224 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2225 thread_apply_help
.c_str (),
2226 &thread_apply_list
, 1,
2228 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2230 const auto thread_apply_all_opts
2231 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2233 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2234 Apply a command to all threads.\n\
2236 Usage: thread apply all [OPTION]... COMMAND\n"
2237 THREAD_APPLY_OPTION_HELP
),
2238 thread_apply_all_opts
);
2240 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2241 thread_apply_all_help
.c_str (),
2242 &thread_apply_list
);
2243 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2245 c
= add_com ("taas", class_run
, taas_command
, _("\
2246 Apply a command to all threads (ignoring errors and empty output).\n\
2247 Usage: taas [OPTION]... COMMAND\n\
2248 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2249 See \"help thread apply all\" for available options."));
2250 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2252 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2253 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2254 Usage: tfaas [OPTION]... COMMAND\n\
2255 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2256 See \"help frame apply all\" for available options."));
2257 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2259 add_cmd ("name", class_run
, thread_name_command
,
2260 _("Set the current thread's name.\n\
2261 Usage: thread name [NAME]\n\
2262 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2264 add_cmd ("find", class_run
, thread_find_command
, _("\
2265 Find threads that match a regular expression.\n\
2266 Usage: thread find REGEXP\n\
2267 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2270 add_setshow_boolean_cmd ("thread-events", no_class
,
2271 &print_thread_events
, _("\
2272 Set printing of thread events (such as thread start and exit)."), _("\
2273 Show printing of thread events (such as thread start and exit)."), NULL
,
2275 show_print_thread_events
,
2276 &setprintlist
, &showprintlist
);
2278 add_setshow_boolean_cmd ("threads", class_maintenance
, &debug_threads
, _("\
2279 Set thread debugging."), _("\
2280 Show thread debugging."), _("\
2281 When on messages about thread creation and deletion are printed."),
2284 &setdebuglist
, &showdebuglist
);
2286 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2287 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);
2288 create_internalvar_type_lazy ("_inferior_thread_count",
2289 &inferior_thread_count_funcs
, NULL
);