1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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 /* RAII type used to increase / decrease the refcount of each thread
62 in a given list of threads. */
64 class scoped_inc_dec_ref
67 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
70 for (thread_info
*thr
: m_thrds
)
74 ~scoped_inc_dec_ref ()
76 for (thread_info
*thr
: m_thrds
)
81 const std::vector
<thread_info
*> &m_thrds
;
84 /* Returns true if THR is the current thread. */
87 is_current_thread (const thread_info
*thr
)
89 return thr
== current_thread_
;
93 inferior_thread (void)
95 gdb_assert (current_thread_
!= nullptr);
96 return current_thread_
;
99 /* Delete the breakpoint pointed at by BP_P, if there's one. */
102 delete_thread_breakpoint (struct breakpoint
**bp_p
)
106 delete_breakpoint (*bp_p
);
112 delete_step_resume_breakpoint (struct thread_info
*tp
)
115 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
119 delete_exception_resume_breakpoint (struct thread_info
*tp
)
122 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
125 /* See gdbthread.h. */
128 delete_single_step_breakpoints (struct thread_info
*tp
)
131 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
134 /* Delete the breakpoint pointed at by BP_P at the next stop, if
138 delete_at_next_stop (struct breakpoint
**bp
)
142 (*bp
)->disposition
= disp_del_at_next_stop
;
147 /* See gdbthread.h. */
150 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
152 return tp
->control
.single_step_breakpoints
!= NULL
;
155 /* See gdbthread.h. */
158 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
159 const address_space
*aspace
,
162 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
164 return (ss_bps
!= NULL
165 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
168 /* See gdbthread.h. */
171 thread_cancel_execution_command (struct thread_info
*thr
)
173 if (thr
->thread_fsm
!= NULL
)
175 thr
->thread_fsm
->clean_up (thr
);
176 delete thr
->thread_fsm
;
177 thr
->thread_fsm
= NULL
;
182 clear_thread_inferior_resources (struct thread_info
*tp
)
184 /* NOTE: this will take care of any left-over step_resume breakpoints,
185 but not any user-specified thread-specific breakpoints. We can not
186 delete the breakpoint straight-off, because the inferior might not
187 be stopped at the moment. */
188 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
189 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
190 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
192 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
194 bpstat_clear (&tp
->control
.stop_bpstat
);
196 btrace_teardown (tp
);
198 thread_cancel_execution_command (tp
);
200 clear_inline_frame_state (tp
);
203 /* Set the TP's state as exited. */
206 set_thread_exited (thread_info
*tp
, bool silent
)
208 /* Dead threads don't need to step-over. Remove from queue. */
209 if (tp
->step_over_next
!= NULL
)
210 global_thread_step_over_chain_remove (tp
);
212 if (tp
->state
!= THREAD_EXITED
)
214 gdb::observers::thread_exit
.notify (tp
, silent
);
216 /* Tag it as exited. */
217 tp
->state
= THREAD_EXITED
;
219 /* Clear breakpoints, etc. associated with this thread. */
220 clear_thread_inferior_resources (tp
);
225 init_thread_list (void)
227 highest_thread_num
= 0;
229 for (thread_info
*tp
: all_threads_safe ())
231 inferior
*inf
= tp
->inf
;
233 if (tp
->deletable ())
236 set_thread_exited (tp
, 1);
238 inf
->thread_list
= NULL
;
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 if (inf
->thread_list
== NULL
)
251 inf
->thread_list
= tp
;
254 struct thread_info
*last
;
256 for (last
= inf
->thread_list
; last
->next
!= NULL
; last
= last
->next
)
257 gdb_assert (ptid
!= last
->ptid
258 || last
->state
== THREAD_EXITED
);
260 gdb_assert (ptid
!= last
->ptid
261 || last
->state
== THREAD_EXITED
);
270 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
272 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
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 printf_unfiltered (_("[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 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
321 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
322 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
325 thread_info::~thread_info ()
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. */
342 thread_info::regcache ()
344 return get_thread_regcache (this);
347 /* See gdbthread.h. */
351 return this->regcache ()-> arch ();
354 /* Add TP to the end of the step-over chain LIST_P. */
357 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
359 gdb_assert (tp
->step_over_next
== NULL
);
360 gdb_assert (tp
->step_over_prev
== NULL
);
365 tp
->step_over_prev
= tp
->step_over_next
= tp
;
369 struct thread_info
*head
= *list_p
;
370 struct thread_info
*tail
= head
->step_over_prev
;
372 tp
->step_over_prev
= tail
;
373 tp
->step_over_next
= head
;
374 head
->step_over_prev
= tp
;
375 tail
->step_over_next
= tp
;
379 /* See gdbthread.h. */
382 thread_step_over_chain_remove (thread_info
**list_p
, thread_info
*tp
)
384 gdb_assert (tp
->step_over_next
!= NULL
);
385 gdb_assert (tp
->step_over_prev
!= NULL
);
389 if (tp
== tp
->step_over_next
)
392 *list_p
= tp
->step_over_next
;
395 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
396 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
397 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
400 /* See gdbthread.h. */
403 global_thread_step_over_chain_remove (thread_info
*tp
)
405 thread_step_over_chain_remove (&global_thread_step_over_chain_head
, tp
);
408 /* See gdbthread.h. */
411 thread_step_over_chain_next (thread_info
*chain_head
, thread_info
*tp
)
413 thread_info
*next
= tp
->step_over_next
;
415 return next
== chain_head
? NULL
: next
;
418 /* See gdbthread.h. */
421 global_thread_step_over_chain_next (thread_info
*tp
)
423 return thread_step_over_chain_next (global_thread_step_over_chain_head
, tp
);
426 /* See gdbthread.h. */
429 thread_is_in_step_over_chain (struct thread_info
*tp
)
431 return (tp
->step_over_next
!= NULL
);
434 /* See gdbthread.h. */
436 int thread_step_over_chain_length (thread_info
*tp
)
442 thread_info
*iter
= tp
->step_over_next
;
447 iter
= iter
->step_over_next
;
455 /* See gdbthread.h. */
458 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
461 fprintf_unfiltered (gdb_stdlog
, "enqueueing thread %ld in global step over chain\n", tp
->ptid
.lwp());
462 step_over_chain_enqueue (&global_thread_step_over_chain_head
, tp
);
465 /* Delete the thread referenced by THR. If SILENT, don't notify
466 the observer of this exit.
468 THR must not be NULL or a failed assertion will be raised. */
471 delete_thread_1 (thread_info
*thr
, bool silent
)
473 gdb_assert (thr
!= nullptr);
475 struct thread_info
*tp
, *tpprev
= NULL
;
477 for (tp
= thr
->inf
->thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
484 set_thread_exited (tp
, silent
);
486 if (!tp
->deletable ())
488 /* Will be really deleted some other time. */
493 tpprev
->next
= tp
->next
;
495 tp
->inf
->thread_list
= tp
->next
;
500 /* See gdbthread.h. */
503 delete_thread (thread_info
*thread
)
505 delete_thread_1 (thread
, false /* not silent */);
509 delete_thread_silent (thread_info
*thread
)
511 delete_thread_1 (thread
, true /* silent */);
515 find_thread_global_id (int global_id
)
517 for (thread_info
*tp
: all_threads ())
518 if (tp
->global_num
== global_id
)
524 static struct thread_info
*
525 find_thread_id (struct inferior
*inf
, int thr_num
)
527 for (thread_info
*tp
: inf
->threads ())
528 if (tp
->per_inf_num
== thr_num
)
534 /* See gdbthread.h. */
537 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
539 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
542 return find_thread_ptid (inf
, ptid
);
545 /* See gdbthread.h. */
548 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
550 for (thread_info
*tp
: inf
->non_exited_threads ())
551 if (tp
->ptid
== ptid
)
557 /* See gdbthread.h. */
560 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
561 struct inferior
*inf
)
563 return target_thread_handle_to_thread_info (handle
.data (),
569 * Thread iterator function.
571 * Calls a callback function once for each thread, so long as
572 * the callback function returns false. If the callback function
573 * returns true, the iteration will end and the current thread
574 * will be returned. This can be useful for implementing a
575 * search for a thread with arbitrary attributes, or for applying
576 * some operation to every thread.
578 * FIXME: some of the existing functionality, such as
579 * "Thread apply all", might be rewritten using this functionality.
583 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
586 for (thread_info
*tp
: all_threads_safe ())
587 if ((*callback
) (tp
, data
))
593 /* See gdbthread.h. */
598 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
604 thread_count (process_stratum_target
*proc_target
)
606 auto rng
= all_threads (proc_target
);
607 return std::distance (rng
.begin (), rng
.end ());
610 /* Return the number of non-exited threads in the thread list. */
613 live_threads_count (void)
615 auto rng
= all_non_exited_threads ();
616 return std::distance (rng
.begin (), rng
.end ());
620 valid_global_thread_id (int global_id
)
622 for (thread_info
*tp
: all_threads ())
623 if (tp
->global_num
== global_id
)
630 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
632 return find_thread_ptid (targ
, ptid
) != nullptr;
635 /* Finds the first thread of the inferior. */
638 first_thread_of_inferior (inferior
*inf
)
640 return inf
->thread_list
;
644 any_thread_of_inferior (inferior
*inf
)
646 gdb_assert (inf
->pid
!= 0);
648 /* Prefer the current thread. */
649 if (inf
== current_inferior ())
650 return inferior_thread ();
652 for (thread_info
*tp
: inf
->non_exited_threads ())
659 any_live_thread_of_inferior (inferior
*inf
)
661 struct thread_info
*curr_tp
= NULL
;
662 struct thread_info
*tp_executing
= NULL
;
664 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
666 /* Prefer the current thread if it's not executing. */
667 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
669 /* If the current thread is dead, forget it. If it's not
670 executing, use it. Otherwise, still choose it (below), but
671 only if no other non-executing thread is found. */
672 curr_tp
= inferior_thread ();
673 if (curr_tp
->state
== THREAD_EXITED
)
675 else if (!curr_tp
->executing
)
679 for (thread_info
*tp
: inf
->non_exited_threads ())
687 /* If both the current thread and all live threads are executing,
688 prefer the current thread. */
692 /* Otherwise, just return an executing thread, if any. */
696 /* Return true if TP is an active thread. */
698 thread_alive (thread_info
*tp
)
700 if (tp
->state
== THREAD_EXITED
)
703 /* Ensure we're looking at the right target stack. */
704 gdb_assert (tp
->inf
== current_inferior ());
706 return target_thread_alive (tp
->ptid
);
709 /* Switch to thread TP if it is alive. Returns true if successfully
710 switched, false otherwise. */
713 switch_to_thread_if_alive (thread_info
*thr
)
715 scoped_restore_current_thread restore_thread
;
717 /* Switch inferior first, so that we're looking at the right target
719 switch_to_inferior_no_thread (thr
->inf
);
721 if (thread_alive (thr
))
723 switch_to_thread (thr
);
724 restore_thread
.dont_restore ();
731 /* See gdbthreads.h. */
736 scoped_restore_current_thread restore_thread
;
738 for (thread_info
*tp
: all_threads_safe ())
740 switch_to_inferior_no_thread (tp
->inf
);
742 if (!thread_alive (tp
))
747 /* See gdbthreads.h. */
750 delete_exited_threads (void)
752 for (thread_info
*tp
: all_threads_safe ())
753 if (tp
->state
== THREAD_EXITED
)
757 /* Return true value if stack temporaries are enabled for the thread
761 thread_stack_temporaries_enabled_p (thread_info
*tp
)
766 return tp
->stack_temporaries_enabled
;
769 /* Push V on to the stack temporaries of the thread with id PTID. */
772 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
774 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
775 tp
->stack_temporaries
.push_back (v
);
778 /* Return true if VAL is among the stack temporaries of the thread
779 TP. Return false otherwise. */
782 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
784 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
785 for (value
*v
: tp
->stack_temporaries
)
792 /* Return the last of the stack temporaries for thread with id PTID.
793 Return NULL if there are no stack temporaries for the thread. */
796 get_last_thread_stack_temporary (thread_info
*tp
)
798 struct value
*lastval
= NULL
;
800 gdb_assert (tp
!= NULL
);
801 if (!tp
->stack_temporaries
.empty ())
802 lastval
= tp
->stack_temporaries
.back ();
808 thread_change_ptid (process_stratum_target
*targ
,
809 ptid_t old_ptid
, ptid_t new_ptid
)
811 struct inferior
*inf
;
812 struct thread_info
*tp
;
814 /* It can happen that what we knew as the target inferior id
815 changes. E.g, target remote may only discover the remote process
816 pid after adding the inferior to GDB's list. */
817 inf
= find_inferior_ptid (targ
, old_ptid
);
818 inf
->pid
= new_ptid
.pid ();
820 tp
= find_thread_ptid (inf
, old_ptid
);
823 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
826 /* See gdbthread.h. */
829 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
831 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
832 tp
->resumed
= resumed
;
835 /* Helper for set_running, that marks one thread either running or
839 set_running_thread (struct thread_info
*tp
, bool running
)
841 bool started
= false;
843 if (running
&& tp
->state
== THREAD_STOPPED
)
845 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
849 /* If the thread is now marked stopped, remove it from
850 the step-over queue, so that we don't try to resume
851 it until the user wants it to. */
852 if (tp
->step_over_next
!= NULL
)
853 global_thread_step_over_chain_remove (tp
);
859 /* See gdbthread.h. */
862 thread_info::set_running (bool running
)
864 if (set_running_thread (this, running
))
865 gdb::observers::target_resumed
.notify (this->ptid
);
869 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
871 /* We try not to notify the observer if no thread has actually
872 changed the running state -- merely to reduce the number of
873 messages to the MI frontend. A frontend is supposed to handle
874 multiple *running notifications just fine. */
875 bool any_started
= false;
877 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
878 if (set_running_thread (tp
, running
))
882 gdb::observers::target_resumed
.notify (ptid
);
886 /* Helper for set_executing. Set's the thread's 'executing' field
887 from EXECUTING, and if EXECUTING is true also clears the thread's
891 set_executing_thread (thread_info
*thr
, bool executing
)
893 thr
->executing
= executing
;
895 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
899 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
901 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
902 set_executing_thread (tp
, executing
);
904 /* It only takes one running thread to spawn more threads. */
906 targ
->threads_executing
= true;
907 /* Only clear the flag if the caller is telling us everything is
909 else if (minus_one_ptid
== ptid
)
910 targ
->threads_executing
= false;
913 /* See gdbthread.h. */
916 threads_are_executing (process_stratum_target
*target
)
918 return target
->threads_executing
;
922 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
924 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
925 tp
->stop_requested
= stop
;
927 /* Call the stop requested observer so other components of GDB can
928 react to this request. */
930 gdb::observers::thread_stop_requested
.notify (ptid
);
934 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
936 bool any_started
= false;
938 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
939 if (set_running_thread (tp
, tp
->executing
))
943 gdb::observers::target_resumed
.notify (ptid
);
946 /* See gdbthread.h. */
949 validate_registers_access (void)
951 /* No selected thread, no registers. */
952 if (inferior_ptid
== null_ptid
)
953 error (_("No thread selected."));
955 thread_info
*tp
= inferior_thread ();
957 /* Don't try to read from a dead thread. */
958 if (tp
->state
== THREAD_EXITED
)
959 error (_("The current thread has terminated"));
961 /* ... or from a spinning thread. FIXME: This isn't actually fully
962 correct. It'll allow an user-requested access (e.g., "print $pc"
963 at the prompt) when a thread is not executing for some internal
964 reason, but is marked running from the user's perspective. E.g.,
965 the thread is waiting for its turn in the step-over queue. */
967 error (_("Selected thread is running."));
970 /* See gdbthread.h. */
973 can_access_registers_thread (thread_info
*thread
)
975 /* No thread, no registers. */
979 /* Don't try to read from a dead thread. */
980 if (thread
->state
== THREAD_EXITED
)
983 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
984 if (thread
->executing
)
991 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
993 return (pc
>= thread
->control
.step_range_start
994 && pc
< thread
->control
.step_range_end
);
997 /* Helper for print_thread_info. Returns true if THR should be
998 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
999 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1000 is true if REQUESTED_THREADS is list of global IDs, false if a list
1001 of per-inferior thread ids. If PID is not -1, only print THR if it
1002 is a thread from the process PID. Otherwise, threads from all
1003 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1004 and PID is not -1, then the thread is printed if it belongs to the
1005 specified process. Otherwise, an error is raised. */
1008 should_print_thread (const char *requested_threads
, int default_inf_num
,
1009 int global_ids
, int pid
, struct thread_info
*thr
)
1011 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1016 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1018 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1019 thr
->inf
->num
, thr
->per_inf_num
);
1024 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1026 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1027 error (_("Requested thread not found in requested process"));
1031 if (thr
->state
== THREAD_EXITED
)
1037 /* Return the string to display in "info threads"'s "Target Id"
1041 thread_target_id_str (thread_info
*tp
)
1043 std::string target_id
= target_pid_to_str (tp
->ptid
);
1044 const char *extra_info
= target_extra_thread_info (tp
);
1045 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1047 if (extra_info
!= nullptr && name
!= nullptr)
1048 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1050 else if (extra_info
!= nullptr)
1051 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1052 else if (name
!= nullptr)
1053 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1058 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1059 whether REQUESTED_THREADS is a list of global or per-inferior
1063 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1064 int global_ids
, int pid
,
1065 int show_global_ids
)
1067 int default_inf_num
= current_inferior ()->num
;
1069 update_thread_list ();
1071 /* Whether we saw any thread. */
1072 bool any_thread
= false;
1073 /* Whether the current thread is exited. */
1074 bool current_exited
= false;
1076 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1077 ? inferior_thread () : NULL
);
1080 /* For backward compatibility, we make a list for MI. A table is
1081 preferable for the CLI, though, because it shows table
1083 gdb::optional
<ui_out_emit_list
> list_emitter
;
1084 gdb::optional
<ui_out_emit_table
> table_emitter
;
1086 /* We'll be switching threads temporarily below. */
1087 scoped_restore_current_thread restore_thread
;
1089 if (uiout
->is_mi_like_p ())
1090 list_emitter
.emplace (uiout
, "threads");
1094 /* The width of the "Target Id" column. Grown below to
1095 accommodate the largest entry. */
1096 size_t target_id_col_width
= 17;
1098 for (thread_info
*tp
: all_threads ())
1100 if (!should_print_thread (requested_threads
, default_inf_num
,
1101 global_ids
, pid
, tp
))
1104 if (!uiout
->is_mi_like_p ())
1106 /* Switch inferiors so we're looking at the right
1108 switch_to_inferior_no_thread (tp
->inf
);
1111 = std::max (target_id_col_width
,
1112 thread_target_id_str (tp
).size ());
1120 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1121 uiout
->message (_("No threads.\n"));
1123 uiout
->message (_("No threads match '%s'.\n"),
1128 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1129 n_threads
, "threads");
1131 uiout
->table_header (1, ui_left
, "current", "");
1132 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1133 if (show_global_ids
)
1134 uiout
->table_header (4, ui_left
, "id", "GId");
1135 uiout
->table_header (target_id_col_width
, ui_left
,
1136 "target-id", "Target Id");
1137 uiout
->table_header (1, ui_left
, "frame", "Frame");
1138 uiout
->table_body ();
1141 for (inferior
*inf
: all_inferiors ())
1142 for (thread_info
*tp
: inf
->threads ())
1147 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1148 current_exited
= true;
1150 if (!should_print_thread (requested_threads
, default_inf_num
,
1151 global_ids
, pid
, tp
))
1154 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1156 if (!uiout
->is_mi_like_p ())
1158 if (tp
== current_thread
)
1159 uiout
->field_string ("current", "*");
1161 uiout
->field_skip ("current");
1163 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1166 if (show_global_ids
|| uiout
->is_mi_like_p ())
1167 uiout
->field_signed ("id", tp
->global_num
);
1169 /* Switch to the thread (and inferior / target). */
1170 switch_to_thread (tp
);
1172 /* For the CLI, we stuff everything into the target-id field.
1173 This is a gross hack to make the output come out looking
1174 correct. The underlying problem here is that ui-out has no
1175 way to specify that a field's space allocation should be
1176 shared by several fields. For MI, we do the right thing
1179 if (uiout
->is_mi_like_p ())
1181 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1183 const char *extra_info
= target_extra_thread_info (tp
);
1184 if (extra_info
!= nullptr)
1185 uiout
->field_string ("details", extra_info
);
1187 const char *name
= (tp
->name
!= nullptr
1189 : target_thread_name (tp
));
1191 uiout
->field_string ("name", name
);
1195 uiout
->field_string ("target-id",
1196 thread_target_id_str (tp
).c_str ());
1199 if (tp
->state
== THREAD_RUNNING
)
1200 uiout
->text ("(running)\n");
1203 /* The switch above put us at the top of the stack (leaf
1205 print_stack_frame (get_selected_frame (NULL
),
1206 /* For MI output, print frame level. */
1207 uiout
->is_mi_like_p (),
1211 if (uiout
->is_mi_like_p ())
1213 const char *state
= "stopped";
1215 if (tp
->state
== THREAD_RUNNING
)
1217 uiout
->field_string ("state", state
);
1220 core
= target_core_of_thread (tp
->ptid
);
1221 if (uiout
->is_mi_like_p () && core
!= -1)
1222 uiout
->field_signed ("core", core
);
1225 /* This end scope restores the current thread and the frame
1226 selected before the "info threads" command, and it finishes the
1227 ui-out list or table. */
1230 if (pid
== -1 && requested_threads
== NULL
)
1232 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1233 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1235 if (inferior_ptid
!= null_ptid
&& current_exited
)
1236 uiout
->message ("\n\
1237 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1238 print_thread_id (inferior_thread ()));
1239 else if (any_thread
&& inferior_ptid
== null_ptid
)
1240 uiout
->message ("\n\
1241 No selected thread. See `help thread'.\n");
1245 /* See gdbthread.h. */
1248 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1251 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1254 /* The options for the "info threads" command. */
1256 struct info_threads_opts
1259 bool show_global_ids
= false;
1262 static const gdb::option::option_def info_threads_option_defs
[] = {
1264 gdb::option::flag_option_def
<info_threads_opts
> {
1266 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1267 N_("Show global thread IDs."),
1272 /* Create an option_def_group for the "info threads" options, with
1273 IT_OPTS as context. */
1275 static inline gdb::option::option_def_group
1276 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1278 return {{info_threads_option_defs
}, it_opts
};
1281 /* Implementation of the "info threads" command.
1283 Note: this has the drawback that it _really_ switches
1284 threads, which frees the frame cache. A no-side
1285 effects info-threads command would be nicer. */
1288 info_threads_command (const char *arg
, int from_tty
)
1290 info_threads_opts it_opts
;
1292 auto grp
= make_info_threads_options_def_group (&it_opts
);
1293 gdb::option::process_options
1294 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1296 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1299 /* Completer for the "info threads" command. */
1302 info_threads_command_completer (struct cmd_list_element
*ignore
,
1303 completion_tracker
&tracker
,
1304 const char *text
, const char *word_ignored
)
1306 const auto grp
= make_info_threads_options_def_group (nullptr);
1308 if (gdb::option::complete_options
1309 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1312 /* Convenience to let the user know what the option can accept. */
1315 gdb::option::complete_on_all_options (tracker
, grp
);
1316 /* Keep this "ID" in sync with what "help info threads"
1318 tracker
.add_completion (make_unique_xstrdup ("ID"));
1322 /* See gdbthread.h. */
1325 switch_to_thread_no_regs (struct thread_info
*thread
)
1327 struct inferior
*inf
= thread
->inf
;
1329 set_current_program_space (inf
->pspace
);
1330 set_current_inferior (inf
);
1332 current_thread_
= thread
;
1333 inferior_ptid
= current_thread_
->ptid
;
1336 /* See gdbthread.h. */
1339 switch_to_no_thread ()
1341 if (current_thread_
== nullptr)
1344 current_thread_
= nullptr;
1345 inferior_ptid
= null_ptid
;
1346 reinit_frame_cache ();
1349 /* See gdbthread.h. */
1352 switch_to_thread (thread_info
*thr
)
1354 gdb_assert (thr
!= NULL
);
1356 if (is_current_thread (thr
))
1359 switch_to_thread_no_regs (thr
);
1361 reinit_frame_cache ();
1364 /* See gdbsupport/common-gdbthread.h. */
1367 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1369 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1370 switch_to_thread (thr
);
1374 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1376 struct frame_info
*frame
= NULL
;
1379 /* This means there was no selected frame. */
1380 if (frame_level
== -1)
1382 select_frame (NULL
);
1386 gdb_assert (frame_level
>= 0);
1388 /* Restore by level first, check if the frame id is the same as
1389 expected. If that fails, try restoring by frame id. If that
1390 fails, nothing to do, just warn the user. */
1392 count
= frame_level
;
1393 frame
= find_relative_frame (get_current_frame (), &count
);
1396 /* The frame ids must match - either both valid or both outer_frame_id.
1397 The latter case is not failsafe, but since it's highly unlikely
1398 the search by level finds the wrong frame, it's 99.9(9)% of
1399 the time (for all practical purposes) safe. */
1400 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1402 /* Cool, all is fine. */
1403 select_frame (frame
);
1407 frame
= frame_find_by_id (a_frame_id
);
1410 /* Cool, refound it. */
1411 select_frame (frame
);
1415 /* Nothing else to do, the frame layout really changed. Select the
1416 innermost stack frame. */
1417 select_frame (get_current_frame ());
1419 /* Warn the user. */
1420 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1422 warning (_("Couldn't restore frame #%d in "
1423 "current thread. Bottom (innermost) frame selected:"),
1425 /* For MI, we should probably have a notification about
1426 current frame change. But this error is not very
1427 likely, so don't bother for now. */
1428 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1433 scoped_restore_current_thread::restore ()
1435 /* If an entry of thread_info was previously selected, it won't be
1436 deleted because we've increased its refcount. The thread represented
1437 by this thread_info entry may have already exited (due to normal exit,
1438 detach, etc), so the thread_info.state is THREAD_EXITED. */
1439 if (m_thread
!= NULL
1440 /* If the previously selected thread belonged to a process that has
1441 in the mean time exited (or killed, detached, etc.), then don't revert
1442 back to it, but instead simply drop back to no thread selected. */
1444 switch_to_thread (m_thread
);
1446 switch_to_inferior_no_thread (m_inf
);
1448 /* The running state of the originally selected thread may have
1449 changed, so we have to recheck it here. */
1450 if (inferior_ptid
!= null_ptid
1452 && m_thread
->state
== THREAD_STOPPED
1453 && target_has_registers
1455 && target_has_memory
)
1456 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1459 scoped_restore_current_thread::~scoped_restore_current_thread ()
1461 if (!m_dont_restore
)
1467 catch (const gdb_exception
&ex
)
1469 /* We're in a dtor, there's really nothing else we can do
1470 but swallow the exception. */
1474 if (m_thread
!= NULL
)
1475 m_thread
->decref ();
1479 scoped_restore_current_thread::scoped_restore_current_thread ()
1482 m_inf
= current_inferior ();
1484 if (inferior_ptid
!= null_ptid
)
1486 thread_info
*tp
= inferior_thread ();
1487 struct frame_info
*frame
;
1489 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1491 && target_has_registers
1493 && target_has_memory
)
1495 /* When processing internal events, there might not be a
1496 selected frame. If we naively call get_selected_frame
1497 here, then we can end up reading debuginfo for the
1498 current frame, but we don't generally need the debuginfo
1500 frame
= get_selected_frame_if_set ();
1507 m_selected_frame_id
= get_frame_id (frame
);
1508 m_selected_frame_level
= frame_relative_level (frame
);
1510 catch (const gdb_exception_error
&ex
)
1512 m_selected_frame_id
= null_frame_id
;
1513 m_selected_frame_level
= -1;
1523 /* See gdbthread.h. */
1526 show_thread_that_caused_stop (void)
1528 return highest_thread_num
> 1;
1531 /* See gdbthread.h. */
1534 show_inferior_qualified_tids (void)
1536 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1539 /* See gdbthread.h. */
1542 print_thread_id (struct thread_info
*thr
)
1544 char *s
= get_print_cell ();
1546 if (show_inferior_qualified_tids ())
1547 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1549 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1553 /* Sort an array of struct thread_info pointers by thread ID (first by
1554 inferior number, and then by per-inferior thread number). Sorts in
1558 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1560 if (a
->inf
->num
!= b
->inf
->num
)
1561 return a
->inf
->num
< b
->inf
->num
;
1563 return (a
->per_inf_num
< b
->per_inf_num
);
1566 /* Sort an array of struct thread_info pointers by thread ID (first by
1567 inferior number, and then by per-inferior thread number). Sorts in
1568 descending order. */
1571 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1573 if (a
->inf
->num
!= b
->inf
->num
)
1574 return a
->inf
->num
> b
->inf
->num
;
1576 return (a
->per_inf_num
> b
->per_inf_num
);
1579 /* Switch to thread THR and execute CMD.
1580 FLAGS.QUIET controls the printing of the thread information.
1581 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1584 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1585 const qcs_flags
&flags
)
1587 switch_to_thread (thr
);
1589 /* The thread header is computed before running the command since
1590 the command can change the inferior, which is not permitted
1591 by thread_target_id_str. */
1592 std::string thr_header
=
1593 string_printf (_("\nThread %s (%s):\n"), print_thread_id (thr
),
1594 thread_target_id_str (thr
).c_str ());
1598 std::string cmd_result
= execute_command_to_string
1599 (cmd
, from_tty
, gdb_stdout
->term_out ());
1600 if (!flags
.silent
|| cmd_result
.length () > 0)
1603 printf_filtered ("%s", thr_header
.c_str ());
1604 printf_filtered ("%s", cmd_result
.c_str ());
1607 catch (const gdb_exception_error
&ex
)
1612 printf_filtered ("%s", thr_header
.c_str ());
1614 printf_filtered ("%s\n", ex
.what ());
1621 /* Option definition of "thread apply"'s "-ascending" option. */
1623 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1626 Call COMMAND for all threads in ascending order.\n\
1627 The default is descending order."),
1630 /* The qcs command line flags for the "thread apply" commands. Keep
1631 this in sync with the "frame apply" commands. */
1633 using qcs_flag_option_def
1634 = gdb::option::flag_option_def
<qcs_flags
>;
1636 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1637 qcs_flag_option_def
{
1638 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1639 N_("Disables printing the thread information."),
1642 qcs_flag_option_def
{
1643 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1644 N_("Print any error raised by COMMAND and continue."),
1647 qcs_flag_option_def
{
1648 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1649 N_("Silently ignore any errors or empty output produced by COMMAND."),
1653 /* Create an option_def_group for the "thread apply all" options, with
1654 ASCENDING and FLAGS as context. */
1656 static inline std::array
<gdb::option::option_def_group
, 2>
1657 make_thread_apply_all_options_def_group (bool *ascending
,
1661 { {ascending_option_def
.def ()}, ascending
},
1662 { {thr_qcs_flags_option_defs
}, flags
},
1666 /* Create an option_def_group for the "thread apply" options, with
1667 FLAGS as context. */
1669 static inline gdb::option::option_def_group
1670 make_thread_apply_options_def_group (qcs_flags
*flags
)
1672 return {{thr_qcs_flags_option_defs
}, flags
};
1675 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1676 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1677 of two numbers separated by a hyphen. Examples:
1679 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1680 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1681 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1684 thread_apply_all_command (const char *cmd
, int from_tty
)
1686 bool ascending
= false;
1689 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1691 gdb::option::process_options
1692 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1694 validate_flags_qcs ("thread apply all", &flags
);
1696 if (cmd
== NULL
|| *cmd
== '\000')
1697 error (_("Please specify a command at the end of 'thread apply all'"));
1699 update_thread_list ();
1701 int tc
= live_threads_count ();
1704 /* Save a copy of the thread list and increment each thread's
1705 refcount while executing the command in the context of each
1706 thread, in case the command is one that wipes threads. E.g.,
1707 detach, kill, disconnect, etc., or even normally continuing
1708 over an inferior or thread exit. */
1709 std::vector
<thread_info
*> thr_list_cpy
;
1710 thr_list_cpy
.reserve (tc
);
1712 for (thread_info
*tp
: all_non_exited_threads ())
1713 thr_list_cpy
.push_back (tp
);
1714 gdb_assert (thr_list_cpy
.size () == tc
);
1716 /* Increment the refcounts, and restore them back on scope
1718 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1720 auto *sorter
= (ascending
1721 ? tp_array_compar_ascending
1722 : tp_array_compar_descending
);
1723 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1725 scoped_restore_current_thread restore_thread
;
1727 for (thread_info
*thr
: thr_list_cpy
)
1728 if (switch_to_thread_if_alive (thr
))
1729 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1733 /* Completer for "thread apply [ID list]". */
1736 thread_apply_command_completer (cmd_list_element
*ignore
,
1737 completion_tracker
&tracker
,
1738 const char *text
, const char * /*word*/)
1740 /* Don't leave this to complete_options because there's an early
1742 tracker
.set_use_custom_word_point (true);
1744 tid_range_parser parser
;
1745 parser
.init (text
, current_inferior ()->num
);
1749 while (!parser
.finished ())
1751 int inf_num
, thr_start
, thr_end
;
1753 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1756 if (parser
.in_star_range () || parser
.in_thread_range ())
1757 parser
.skip_range ();
1760 catch (const gdb_exception_error
&ex
)
1762 /* get_tid_range throws if it parses a negative number, for
1763 example. But a seemingly negative number may be the start of
1764 an option instead. */
1767 const char *cmd
= parser
.cur_tok ();
1771 /* No thread ID list yet. */
1775 /* Check if we're past a valid thread ID list already. */
1776 if (parser
.finished ()
1777 && cmd
> text
&& !isspace (cmd
[-1]))
1780 /* We're past the thread ID list, advance word point. */
1781 tracker
.advance_custom_word_point_by (cmd
- text
);
1784 const auto group
= make_thread_apply_options_def_group (nullptr);
1785 if (gdb::option::complete_options
1786 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1789 complete_nested_command_line (tracker
, text
);
1792 /* Completer for "thread apply all". */
1795 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1796 completion_tracker
&tracker
,
1797 const char *text
, const char *word
)
1799 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1801 if (gdb::option::complete_options
1802 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1805 complete_nested_command_line (tracker
, text
);
1808 /* Implementation of the "thread apply" command. */
1811 thread_apply_command (const char *tidlist
, int from_tty
)
1814 const char *cmd
= NULL
;
1815 tid_range_parser parser
;
1817 if (tidlist
== NULL
|| *tidlist
== '\000')
1818 error (_("Please specify a thread ID list"));
1820 parser
.init (tidlist
, current_inferior ()->num
);
1821 while (!parser
.finished ())
1823 int inf_num
, thr_start
, thr_end
;
1825 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1829 cmd
= parser
.cur_tok ();
1831 auto group
= make_thread_apply_options_def_group (&flags
);
1832 gdb::option::process_options
1833 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1835 validate_flags_qcs ("thread apply", &flags
);
1838 error (_("Please specify a command following the thread ID list"));
1840 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1841 invalid_thread_id_error (cmd
);
1843 scoped_restore_current_thread restore_thread
;
1845 parser
.init (tidlist
, current_inferior ()->num
);
1846 while (!parser
.finished ())
1848 struct thread_info
*tp
= NULL
;
1849 struct inferior
*inf
;
1850 int inf_num
, thr_num
;
1852 parser
.get_tid (&inf_num
, &thr_num
);
1853 inf
= find_inferior_id (inf_num
);
1855 tp
= find_thread_id (inf
, thr_num
);
1857 if (parser
.in_star_range ())
1861 warning (_("Unknown inferior %d"), inf_num
);
1862 parser
.skip_range ();
1866 /* No use looking for threads past the highest thread number
1867 the inferior ever had. */
1868 if (thr_num
>= inf
->highest_thread_num
)
1869 parser
.skip_range ();
1871 /* Be quiet about unknown threads numbers. */
1878 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1879 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1881 warning (_("Unknown thread %d"), thr_num
);
1885 if (!switch_to_thread_if_alive (tp
))
1887 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1891 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1896 /* Implementation of the "taas" command. */
1899 taas_command (const char *cmd
, int from_tty
)
1901 if (cmd
== NULL
|| *cmd
== '\0')
1902 error (_("Please specify a command to apply on all threads"));
1903 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1904 execute_command (expanded
.c_str (), from_tty
);
1907 /* Implementation of the "tfaas" command. */
1910 tfaas_command (const char *cmd
, int from_tty
)
1912 if (cmd
== NULL
|| *cmd
== '\0')
1913 error (_("Please specify a command to apply on all frames of all threads"));
1914 std::string expanded
1915 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1916 execute_command (expanded
.c_str (), from_tty
);
1919 /* Switch to the specified thread, or print the current thread. */
1922 thread_command (const char *tidstr
, int from_tty
)
1926 if (inferior_ptid
== null_ptid
)
1927 error (_("No thread selected"));
1929 if (target_has_stack
)
1931 struct thread_info
*tp
= inferior_thread ();
1933 if (tp
->state
== THREAD_EXITED
)
1934 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1935 print_thread_id (tp
),
1936 target_pid_to_str (inferior_ptid
).c_str ());
1938 printf_filtered (_("[Current thread is %s (%s)]\n"),
1939 print_thread_id (tp
),
1940 target_pid_to_str (inferior_ptid
).c_str ());
1943 error (_("No stack."));
1947 ptid_t previous_ptid
= inferior_ptid
;
1949 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1951 /* Print if the thread has not changed, otherwise an event will
1953 if (inferior_ptid
== previous_ptid
)
1955 print_selected_thread_frame (current_uiout
,
1956 USER_SELECTED_THREAD
1957 | USER_SELECTED_FRAME
);
1961 gdb::observers::user_selected_context_changed
.notify
1962 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1967 /* Implementation of `thread name'. */
1970 thread_name_command (const char *arg
, int from_tty
)
1972 struct thread_info
*info
;
1974 if (inferior_ptid
== null_ptid
)
1975 error (_("No thread selected"));
1977 arg
= skip_spaces (arg
);
1979 info
= inferior_thread ();
1981 info
->name
= arg
? xstrdup (arg
) : NULL
;
1984 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1987 thread_find_command (const char *arg
, int from_tty
)
1990 unsigned long match
= 0;
1992 if (arg
== NULL
|| *arg
== '\0')
1993 error (_("Command requires an argument."));
1995 tmp
= re_comp (arg
);
1997 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1999 update_thread_list ();
2000 for (thread_info
*tp
: all_threads ())
2002 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
2004 printf_filtered (_("Thread %s has name '%s'\n"),
2005 print_thread_id (tp
), tp
->name
);
2009 tmp
= target_thread_name (tp
);
2010 if (tmp
!= NULL
&& re_exec (tmp
))
2012 printf_filtered (_("Thread %s has target name '%s'\n"),
2013 print_thread_id (tp
), tmp
);
2017 std::string name
= target_pid_to_str (tp
->ptid
);
2018 if (!name
.empty () && re_exec (name
.c_str ()))
2020 printf_filtered (_("Thread %s has target id '%s'\n"),
2021 print_thread_id (tp
), name
.c_str ());
2025 tmp
= target_extra_thread_info (tp
);
2026 if (tmp
!= NULL
&& re_exec (tmp
))
2028 printf_filtered (_("Thread %s has extra info '%s'\n"),
2029 print_thread_id (tp
), tmp
);
2034 printf_filtered (_("No threads match '%s'\n"), arg
);
2037 /* Print notices when new threads are attached and detached. */
2038 bool print_thread_events
= true;
2040 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2041 struct cmd_list_element
*c
, const char *value
)
2043 fprintf_filtered (file
,
2044 _("Printing of thread events is %s.\n"),
2048 /* See gdbthread.h. */
2051 thread_select (const char *tidstr
, thread_info
*tp
)
2053 if (!switch_to_thread_if_alive (tp
))
2054 error (_("Thread ID %s has terminated."), tidstr
);
2056 annotate_thread_changed ();
2058 /* Since the current thread may have changed, see if there is any
2059 exited thread we can now delete. */
2060 delete_exited_threads ();
2063 /* Print thread and frame switch command response. */
2066 print_selected_thread_frame (struct ui_out
*uiout
,
2067 user_selected_what selection
)
2069 struct thread_info
*tp
= inferior_thread ();
2071 if (selection
& USER_SELECTED_THREAD
)
2073 if (uiout
->is_mi_like_p ())
2075 uiout
->field_signed ("new-thread-id",
2076 inferior_thread ()->global_num
);
2080 uiout
->text ("[Switching to thread ");
2081 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2083 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2088 if (tp
->state
== THREAD_RUNNING
)
2090 if (selection
& USER_SELECTED_THREAD
)
2091 uiout
->text ("(running)\n");
2093 else if (selection
& USER_SELECTED_FRAME
)
2095 if (selection
& USER_SELECTED_THREAD
)
2098 if (has_stack_frames ())
2099 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2104 /* Update the 'threads_executing' global based on the threads we know
2105 about right now. This is used by infrun to tell whether we should
2106 pull events out of the current target. */
2109 update_threads_executing (void)
2111 process_stratum_target
*targ
= current_inferior ()->process_target ();
2116 targ
->threads_executing
= false;
2118 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2120 if (!inf
->has_execution ())
2123 /* If the process has no threads, then it must be we have a
2124 process-exit event pending. */
2125 if (inf
->thread_list
== NULL
)
2127 targ
->threads_executing
= true;
2131 for (thread_info
*tp
: inf
->non_exited_threads ())
2135 targ
->threads_executing
= true;
2143 update_thread_list (void)
2145 target_update_thread_list ();
2146 update_threads_executing ();
2149 /* Return a new value for the selected thread's id. Return a value of
2150 0 if no thread is selected. If GLOBAL is true, return the thread's
2151 global number. Otherwise return the per-inferior number. */
2153 static struct value
*
2154 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2158 if (inferior_ptid
== null_ptid
)
2162 thread_info
*tp
= inferior_thread ();
2164 int_val
= tp
->global_num
;
2166 int_val
= tp
->per_inf_num
;
2169 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2172 /* Return a new value for the selected thread's per-inferior thread
2173 number. Return a value of 0 if no thread is selected, or no
2176 static struct value
*
2177 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2178 struct internalvar
*var
,
2181 return thread_num_make_value_helper (gdbarch
, 0);
2184 /* Return a new value for the selected thread's global id. Return a
2185 value of 0 if no thread is selected, or no threads exist. */
2187 static struct value
*
2188 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2191 return thread_num_make_value_helper (gdbarch
, 1);
2194 /* Commands with a prefix of `thread'. */
2195 struct cmd_list_element
*thread_cmd_list
= NULL
;
2197 /* Implementation of `thread' variable. */
2199 static const struct internalvar_funcs thread_funcs
=
2201 thread_id_per_inf_num_make_value
,
2206 /* Implementation of `gthread' variable. */
2208 static const struct internalvar_funcs gthread_funcs
=
2210 global_thread_id_make_value
,
2215 void _initialize_thread ();
2217 _initialize_thread ()
2219 static struct cmd_list_element
*thread_apply_list
= NULL
;
2220 cmd_list_element
*c
;
2222 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2224 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2226 static std::string info_threads_help
2227 = gdb::option::build_help (_("\
2228 Display currently known threads.\n\
2229 Usage: info threads [OPTION]... [ID]...\n\
2233 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2234 Otherwise, all threads are displayed."),
2237 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2238 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2240 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2241 Use this command to switch between threads.\n\
2242 The new thread ID must be currently known."),
2243 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2245 #define THREAD_APPLY_OPTION_HELP "\
2246 Prints per-inferior thread number and target system's thread id\n\
2247 followed by COMMAND output.\n\
2249 By default, an error raised during the execution of COMMAND\n\
2250 aborts \"thread apply\".\n\
2255 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2257 static std::string thread_apply_help
= gdb::option::build_help (_("\
2258 Apply a command to a list of threads.\n\
2259 Usage: thread apply ID... [OPTION]... COMMAND\n\
2260 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2261 THREAD_APPLY_OPTION_HELP
),
2264 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2265 thread_apply_help
.c_str (),
2266 &thread_apply_list
, "thread apply ", 1,
2268 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2270 const auto thread_apply_all_opts
2271 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2273 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2274 Apply a command to all threads.\n\
2276 Usage: thread apply all [OPTION]... COMMAND\n"
2277 THREAD_APPLY_OPTION_HELP
),
2278 thread_apply_all_opts
);
2280 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2281 thread_apply_all_help
.c_str (),
2282 &thread_apply_list
);
2283 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2285 c
= add_com ("taas", class_run
, taas_command
, _("\
2286 Apply a command to all threads (ignoring errors and empty output).\n\
2287 Usage: taas [OPTION]... COMMAND\n\
2288 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2289 See \"help thread apply all\" for available options."));
2290 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2292 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2293 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2294 Usage: tfaas [OPTION]... COMMAND\n\
2295 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2296 See \"help frame apply all\" for available options."));
2297 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2299 add_cmd ("name", class_run
, thread_name_command
,
2300 _("Set the current thread's name.\n\
2301 Usage: thread name [NAME]\n\
2302 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2304 add_cmd ("find", class_run
, thread_find_command
, _("\
2305 Find threads that match a regular expression.\n\
2306 Usage: thread find REGEXP\n\
2307 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2310 add_com_alias ("t", "thread", class_run
, 1);
2312 add_setshow_boolean_cmd ("thread-events", no_class
,
2313 &print_thread_events
, _("\
2314 Set printing of thread events (such as thread start and exit)."), _("\
2315 Show printing of thread events (such as thread start and exit)."), NULL
,
2317 show_print_thread_events
,
2318 &setprintlist
, &showprintlist
);
2320 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2321 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);