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 chain. */
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 /* Add TP to the end of the step-over chain LIST_P. */
343 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
345 gdb_assert (tp
->step_over_next
== NULL
);
346 gdb_assert (tp
->step_over_prev
== NULL
);
351 tp
->step_over_prev
= tp
->step_over_next
= tp
;
355 struct thread_info
*head
= *list_p
;
356 struct thread_info
*tail
= head
->step_over_prev
;
358 tp
->step_over_prev
= tail
;
359 tp
->step_over_next
= head
;
360 head
->step_over_prev
= tp
;
361 tail
->step_over_next
= tp
;
365 /* See gdbthread.h. */
368 thread_step_over_chain_remove (thread_info
**list_p
, thread_info
*tp
)
370 gdb_assert (tp
->step_over_next
!= NULL
);
371 gdb_assert (tp
->step_over_prev
!= NULL
);
375 if (tp
== tp
->step_over_next
)
378 *list_p
= tp
->step_over_next
;
381 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
382 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
383 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
386 /* See gdbthread.h. */
389 thread_step_over_chain_next (thread_info
*chain_head
, thread_info
*tp
)
391 thread_info
*next
= tp
->step_over_next
;
393 return next
== chain_head
? NULL
: next
;
396 /* See gdbthread.h. */
399 global_thread_step_over_chain_next (struct thread_info
*tp
)
401 return thread_step_over_chain_next (global_thread_step_over_chain_head
, tp
);
404 /* See gdbthread.h. */
407 thread_is_in_step_over_chain (struct thread_info
*tp
)
409 return (tp
->step_over_next
!= NULL
);
412 /* See gdbthread.h. */
415 thread_step_over_chain_length (thread_info
*tp
)
420 gdb_assert (thread_is_in_step_over_chain (tp
));
424 for (thread_info
*iter
= tp
->step_over_next
;
426 iter
= iter
->step_over_next
)
432 /* See gdbthread.h. */
435 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
437 infrun_debug_printf ("enqueueing thread %s in global step over chain",
438 target_pid_to_str (tp
->ptid
).c_str ());
440 step_over_chain_enqueue (&global_thread_step_over_chain_head
, tp
);
443 /* See gdbthread.h. */
446 global_thread_step_over_chain_enqueue_chain (thread_info
*chain_head
)
448 gdb_assert (chain_head
->step_over_next
!= nullptr);
449 gdb_assert (chain_head
->step_over_prev
!= nullptr);
451 if (global_thread_step_over_chain_head
== nullptr)
452 global_thread_step_over_chain_head
= chain_head
;
455 thread_info
*global_last
= global_thread_step_over_chain_head
->step_over_prev
;
456 thread_info
*chain_last
= chain_head
->step_over_prev
;
458 chain_last
->step_over_next
= global_thread_step_over_chain_head
;
459 global_last
->step_over_next
= chain_head
;
460 global_thread_step_over_chain_head
->step_over_prev
= chain_last
;
461 chain_head
->step_over_prev
= global_last
;
465 /* See gdbthread.h. */
468 global_thread_step_over_chain_remove (struct thread_info
*tp
)
470 infrun_debug_printf ("removing thread %s from global step over chain",
471 target_pid_to_str (tp
->ptid
).c_str ());
473 thread_step_over_chain_remove (&global_thread_step_over_chain_head
, tp
);
476 /* Delete the thread referenced by THR. If SILENT, don't notify
477 the observer of this exit.
479 THR must not be NULL or a failed assertion will be raised. */
482 delete_thread_1 (thread_info
*thr
, bool silent
)
484 gdb_assert (thr
!= nullptr);
486 struct thread_info
*tp
, *tpprev
= NULL
;
488 for (tp
= thr
->inf
->thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
495 set_thread_exited (tp
, silent
);
497 if (!tp
->deletable ())
499 /* Will be really deleted some other time. */
504 tpprev
->next
= tp
->next
;
506 tp
->inf
->thread_list
= tp
->next
;
511 /* See gdbthread.h. */
514 delete_thread (thread_info
*thread
)
516 delete_thread_1 (thread
, false /* not silent */);
520 delete_thread_silent (thread_info
*thread
)
522 delete_thread_1 (thread
, true /* silent */);
526 find_thread_global_id (int global_id
)
528 for (thread_info
*tp
: all_threads ())
529 if (tp
->global_num
== global_id
)
535 static struct thread_info
*
536 find_thread_id (struct inferior
*inf
, int thr_num
)
538 for (thread_info
*tp
: inf
->threads ())
539 if (tp
->per_inf_num
== thr_num
)
545 /* See gdbthread.h. */
548 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
550 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
553 return find_thread_ptid (inf
, ptid
);
556 /* See gdbthread.h. */
559 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
561 for (thread_info
*tp
: inf
->non_exited_threads ())
562 if (tp
->ptid
== ptid
)
568 /* See gdbthread.h. */
571 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
572 struct inferior
*inf
)
574 return target_thread_handle_to_thread_info (handle
.data (),
580 * Thread iterator function.
582 * Calls a callback function once for each thread, so long as
583 * the callback function returns false. If the callback function
584 * returns true, the iteration will end and the current thread
585 * will be returned. This can be useful for implementing a
586 * search for a thread with arbitrary attributes, or for applying
587 * some operation to every thread.
589 * FIXME: some of the existing functionality, such as
590 * "Thread apply all", might be rewritten using this functionality.
594 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
597 for (thread_info
*tp
: all_threads_safe ())
598 if ((*callback
) (tp
, data
))
604 /* See gdbthread.h. */
609 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
615 thread_count (process_stratum_target
*proc_target
)
617 auto rng
= all_threads (proc_target
);
618 return std::distance (rng
.begin (), rng
.end ());
621 /* Return the number of non-exited threads in the thread list. */
624 live_threads_count (void)
626 auto rng
= all_non_exited_threads ();
627 return std::distance (rng
.begin (), rng
.end ());
631 valid_global_thread_id (int global_id
)
633 for (thread_info
*tp
: all_threads ())
634 if (tp
->global_num
== global_id
)
641 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
643 return find_thread_ptid (targ
, ptid
) != nullptr;
646 /* Finds the first thread of the inferior. */
649 first_thread_of_inferior (inferior
*inf
)
651 return inf
->thread_list
;
655 any_thread_of_inferior (inferior
*inf
)
657 gdb_assert (inf
->pid
!= 0);
659 /* Prefer the current thread. */
660 if (inf
== current_inferior ())
661 return inferior_thread ();
663 for (thread_info
*tp
: inf
->non_exited_threads ())
670 any_live_thread_of_inferior (inferior
*inf
)
672 struct thread_info
*curr_tp
= NULL
;
673 struct thread_info
*tp_executing
= NULL
;
675 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
677 /* Prefer the current thread if it's not executing. */
678 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
680 /* If the current thread is dead, forget it. If it's not
681 executing, use it. Otherwise, still choose it (below), but
682 only if no other non-executing thread is found. */
683 curr_tp
= inferior_thread ();
684 if (curr_tp
->state
== THREAD_EXITED
)
686 else if (!curr_tp
->executing
)
690 for (thread_info
*tp
: inf
->non_exited_threads ())
698 /* If both the current thread and all live threads are executing,
699 prefer the current thread. */
703 /* Otherwise, just return an executing thread, if any. */
707 /* Return true if TP is an active thread. */
709 thread_alive (thread_info
*tp
)
711 if (tp
->state
== THREAD_EXITED
)
714 /* Ensure we're looking at the right target stack. */
715 gdb_assert (tp
->inf
== current_inferior ());
717 return target_thread_alive (tp
->ptid
);
720 /* Switch to thread TP if it is alive. Returns true if successfully
721 switched, false otherwise. */
724 switch_to_thread_if_alive (thread_info
*thr
)
726 scoped_restore_current_thread restore_thread
;
728 /* Switch inferior first, so that we're looking at the right target
730 switch_to_inferior_no_thread (thr
->inf
);
732 if (thread_alive (thr
))
734 switch_to_thread (thr
);
735 restore_thread
.dont_restore ();
742 /* See gdbthreads.h. */
747 scoped_restore_current_thread restore_thread
;
749 for (thread_info
*tp
: all_threads_safe ())
751 switch_to_inferior_no_thread (tp
->inf
);
753 if (!thread_alive (tp
))
758 /* See gdbthreads.h. */
761 delete_exited_threads (void)
763 for (thread_info
*tp
: all_threads_safe ())
764 if (tp
->state
== THREAD_EXITED
)
768 /* Return true value if stack temporaries are enabled for the thread
772 thread_stack_temporaries_enabled_p (thread_info
*tp
)
777 return tp
->stack_temporaries_enabled
;
780 /* Push V on to the stack temporaries of the thread with id PTID. */
783 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
785 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
786 tp
->stack_temporaries
.push_back (v
);
789 /* Return true if VAL is among the stack temporaries of the thread
790 TP. Return false otherwise. */
793 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
795 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
796 for (value
*v
: tp
->stack_temporaries
)
803 /* Return the last of the stack temporaries for thread with id PTID.
804 Return NULL if there are no stack temporaries for the thread. */
807 get_last_thread_stack_temporary (thread_info
*tp
)
809 struct value
*lastval
= NULL
;
811 gdb_assert (tp
!= NULL
);
812 if (!tp
->stack_temporaries
.empty ())
813 lastval
= tp
->stack_temporaries
.back ();
819 thread_change_ptid (process_stratum_target
*targ
,
820 ptid_t old_ptid
, ptid_t new_ptid
)
822 struct inferior
*inf
;
823 struct thread_info
*tp
;
825 /* It can happen that what we knew as the target inferior id
826 changes. E.g, target remote may only discover the remote process
827 pid after adding the inferior to GDB's list. */
828 inf
= find_inferior_ptid (targ
, old_ptid
);
829 inf
->pid
= new_ptid
.pid ();
831 tp
= find_thread_ptid (inf
, old_ptid
);
834 gdb::observers::thread_ptid_changed
.notify (targ
, old_ptid
, new_ptid
);
837 /* See gdbthread.h. */
840 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
842 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
843 tp
->resumed
= resumed
;
846 /* Helper for set_running, that marks one thread either running or
850 set_running_thread (struct thread_info
*tp
, bool running
)
852 bool started
= false;
854 if (running
&& tp
->state
== THREAD_STOPPED
)
856 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
860 /* If the thread is now marked stopped, remove it from
861 the step-over queue, so that we don't try to resume
862 it until the user wants it to. */
863 if (tp
->step_over_next
!= NULL
)
864 global_thread_step_over_chain_remove (tp
);
870 /* See gdbthread.h. */
873 thread_info::set_running (bool running
)
875 if (set_running_thread (this, running
))
876 gdb::observers::target_resumed
.notify (this->ptid
);
880 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
882 /* We try not to notify the observer if no thread has actually
883 changed the running state -- merely to reduce the number of
884 messages to the MI frontend. A frontend is supposed to handle
885 multiple *running notifications just fine. */
886 bool any_started
= false;
888 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
889 if (set_running_thread (tp
, running
))
893 gdb::observers::target_resumed
.notify (ptid
);
897 /* Helper for set_executing. Set's the thread's 'executing' field
898 from EXECUTING, and if EXECUTING is true also clears the thread's
902 set_executing_thread (thread_info
*thr
, bool executing
)
904 thr
->executing
= executing
;
906 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
910 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
912 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
913 set_executing_thread (tp
, executing
);
915 /* It only takes one running thread to spawn more threads. */
917 targ
->threads_executing
= true;
918 /* Only clear the flag if the caller is telling us everything is
920 else if (minus_one_ptid
== ptid
)
921 targ
->threads_executing
= false;
924 /* See gdbthread.h. */
927 threads_are_executing (process_stratum_target
*target
)
929 return target
->threads_executing
;
933 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
935 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
936 tp
->stop_requested
= stop
;
938 /* Call the stop requested observer so other components of GDB can
939 react to this request. */
941 gdb::observers::thread_stop_requested
.notify (ptid
);
945 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
947 bool any_started
= false;
949 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
950 if (set_running_thread (tp
, tp
->executing
))
954 gdb::observers::target_resumed
.notify (ptid
);
957 /* See gdbthread.h. */
960 validate_registers_access (void)
962 /* No selected thread, no registers. */
963 if (inferior_ptid
== null_ptid
)
964 error (_("No thread selected."));
966 thread_info
*tp
= inferior_thread ();
968 /* Don't try to read from a dead thread. */
969 if (tp
->state
== THREAD_EXITED
)
970 error (_("The current thread has terminated"));
972 /* ... or from a spinning thread. FIXME: This isn't actually fully
973 correct. It'll allow an user-requested access (e.g., "print $pc"
974 at the prompt) when a thread is not executing for some internal
975 reason, but is marked running from the user's perspective. E.g.,
976 the thread is waiting for its turn in the step-over queue. */
978 error (_("Selected thread is running."));
981 /* See gdbthread.h. */
984 can_access_registers_thread (thread_info
*thread
)
986 /* No thread, no registers. */
990 /* Don't try to read from a dead thread. */
991 if (thread
->state
== THREAD_EXITED
)
994 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
995 if (thread
->executing
)
1002 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1004 return (pc
>= thread
->control
.step_range_start
1005 && pc
< thread
->control
.step_range_end
);
1008 /* Helper for print_thread_info. Returns true if THR should be
1009 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1010 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1011 is true if REQUESTED_THREADS is list of global IDs, false if a list
1012 of per-inferior thread ids. If PID is not -1, only print THR if it
1013 is a thread from the process PID. Otherwise, threads from all
1014 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1015 and PID is not -1, then the thread is printed if it belongs to the
1016 specified process. Otherwise, an error is raised. */
1019 should_print_thread (const char *requested_threads
, int default_inf_num
,
1020 int global_ids
, int pid
, struct thread_info
*thr
)
1022 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1027 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1029 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1030 thr
->inf
->num
, thr
->per_inf_num
);
1035 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1037 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1038 error (_("Requested thread not found in requested process"));
1042 if (thr
->state
== THREAD_EXITED
)
1048 /* Return the string to display in "info threads"'s "Target Id"
1052 thread_target_id_str (thread_info
*tp
)
1054 std::string target_id
= target_pid_to_str (tp
->ptid
);
1055 const char *extra_info
= target_extra_thread_info (tp
);
1056 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1058 if (extra_info
!= nullptr && name
!= nullptr)
1059 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1061 else if (extra_info
!= nullptr)
1062 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1063 else if (name
!= nullptr)
1064 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1069 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1070 whether REQUESTED_THREADS is a list of global or per-inferior
1074 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1075 int global_ids
, int pid
,
1076 int show_global_ids
)
1078 int default_inf_num
= current_inferior ()->num
;
1080 update_thread_list ();
1082 /* Whether we saw any thread. */
1083 bool any_thread
= false;
1084 /* Whether the current thread is exited. */
1085 bool current_exited
= false;
1087 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1088 ? inferior_thread () : NULL
);
1091 /* For backward compatibility, we make a list for MI. A table is
1092 preferable for the CLI, though, because it shows table
1094 gdb::optional
<ui_out_emit_list
> list_emitter
;
1095 gdb::optional
<ui_out_emit_table
> table_emitter
;
1097 /* We'll be switching threads temporarily below. */
1098 scoped_restore_current_thread restore_thread
;
1100 if (uiout
->is_mi_like_p ())
1101 list_emitter
.emplace (uiout
, "threads");
1105 /* The width of the "Target Id" column. Grown below to
1106 accommodate the largest entry. */
1107 size_t target_id_col_width
= 17;
1109 for (thread_info
*tp
: all_threads ())
1111 if (!should_print_thread (requested_threads
, default_inf_num
,
1112 global_ids
, pid
, tp
))
1115 if (!uiout
->is_mi_like_p ())
1117 /* Switch inferiors so we're looking at the right
1119 switch_to_inferior_no_thread (tp
->inf
);
1122 = std::max (target_id_col_width
,
1123 thread_target_id_str (tp
).size ());
1131 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1132 uiout
->message (_("No threads.\n"));
1134 uiout
->message (_("No threads match '%s'.\n"),
1139 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1140 n_threads
, "threads");
1142 uiout
->table_header (1, ui_left
, "current", "");
1143 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1144 if (show_global_ids
)
1145 uiout
->table_header (4, ui_left
, "id", "GId");
1146 uiout
->table_header (target_id_col_width
, ui_left
,
1147 "target-id", "Target Id");
1148 uiout
->table_header (1, ui_left
, "frame", "Frame");
1149 uiout
->table_body ();
1152 for (inferior
*inf
: all_inferiors ())
1153 for (thread_info
*tp
: inf
->threads ())
1158 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1159 current_exited
= true;
1161 if (!should_print_thread (requested_threads
, default_inf_num
,
1162 global_ids
, pid
, tp
))
1165 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1167 if (!uiout
->is_mi_like_p ())
1169 if (tp
== current_thread
)
1170 uiout
->field_string ("current", "*");
1172 uiout
->field_skip ("current");
1174 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1177 if (show_global_ids
|| uiout
->is_mi_like_p ())
1178 uiout
->field_signed ("id", tp
->global_num
);
1180 /* Switch to the thread (and inferior / target). */
1181 switch_to_thread (tp
);
1183 /* For the CLI, we stuff everything into the target-id field.
1184 This is a gross hack to make the output come out looking
1185 correct. The underlying problem here is that ui-out has no
1186 way to specify that a field's space allocation should be
1187 shared by several fields. For MI, we do the right thing
1190 if (uiout
->is_mi_like_p ())
1192 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1194 const char *extra_info
= target_extra_thread_info (tp
);
1195 if (extra_info
!= nullptr)
1196 uiout
->field_string ("details", extra_info
);
1198 const char *name
= (tp
->name
!= nullptr
1200 : target_thread_name (tp
));
1202 uiout
->field_string ("name", name
);
1206 uiout
->field_string ("target-id",
1207 thread_target_id_str (tp
).c_str ());
1210 if (tp
->state
== THREAD_RUNNING
)
1211 uiout
->text ("(running)\n");
1214 /* The switch above put us at the top of the stack (leaf
1216 print_stack_frame (get_selected_frame (NULL
),
1217 /* For MI output, print frame level. */
1218 uiout
->is_mi_like_p (),
1222 if (uiout
->is_mi_like_p ())
1224 const char *state
= "stopped";
1226 if (tp
->state
== THREAD_RUNNING
)
1228 uiout
->field_string ("state", state
);
1231 core
= target_core_of_thread (tp
->ptid
);
1232 if (uiout
->is_mi_like_p () && core
!= -1)
1233 uiout
->field_signed ("core", core
);
1236 /* This end scope restores the current thread and the frame
1237 selected before the "info threads" command, and it finishes the
1238 ui-out list or table. */
1241 if (pid
== -1 && requested_threads
== NULL
)
1243 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1244 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1246 if (inferior_ptid
!= null_ptid
&& current_exited
)
1247 uiout
->message ("\n\
1248 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1249 print_thread_id (inferior_thread ()));
1250 else if (any_thread
&& inferior_ptid
== null_ptid
)
1251 uiout
->message ("\n\
1252 No selected thread. See `help thread'.\n");
1256 /* See gdbthread.h. */
1259 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1262 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1265 /* The options for the "info threads" command. */
1267 struct info_threads_opts
1270 bool show_global_ids
= false;
1273 static const gdb::option::option_def info_threads_option_defs
[] = {
1275 gdb::option::flag_option_def
<info_threads_opts
> {
1277 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1278 N_("Show global thread IDs."),
1283 /* Create an option_def_group for the "info threads" options, with
1284 IT_OPTS as context. */
1286 static inline gdb::option::option_def_group
1287 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1289 return {{info_threads_option_defs
}, it_opts
};
1292 /* Implementation of the "info threads" command.
1294 Note: this has the drawback that it _really_ switches
1295 threads, which frees the frame cache. A no-side
1296 effects info-threads command would be nicer. */
1299 info_threads_command (const char *arg
, int from_tty
)
1301 info_threads_opts it_opts
;
1303 auto grp
= make_info_threads_options_def_group (&it_opts
);
1304 gdb::option::process_options
1305 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1307 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1310 /* Completer for the "info threads" command. */
1313 info_threads_command_completer (struct cmd_list_element
*ignore
,
1314 completion_tracker
&tracker
,
1315 const char *text
, const char *word_ignored
)
1317 const auto grp
= make_info_threads_options_def_group (nullptr);
1319 if (gdb::option::complete_options
1320 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1323 /* Convenience to let the user know what the option can accept. */
1326 gdb::option::complete_on_all_options (tracker
, grp
);
1327 /* Keep this "ID" in sync with what "help info threads"
1329 tracker
.add_completion (make_unique_xstrdup ("ID"));
1333 /* See gdbthread.h. */
1336 switch_to_thread_no_regs (struct thread_info
*thread
)
1338 struct inferior
*inf
= thread
->inf
;
1340 set_current_program_space (inf
->pspace
);
1341 set_current_inferior (inf
);
1343 current_thread_
= thread
;
1344 inferior_ptid
= current_thread_
->ptid
;
1347 /* See gdbthread.h. */
1350 switch_to_no_thread ()
1352 if (current_thread_
== nullptr)
1355 current_thread_
= nullptr;
1356 inferior_ptid
= null_ptid
;
1357 reinit_frame_cache ();
1360 /* See gdbthread.h. */
1363 switch_to_thread (thread_info
*thr
)
1365 gdb_assert (thr
!= NULL
);
1367 if (is_current_thread (thr
))
1370 switch_to_thread_no_regs (thr
);
1372 reinit_frame_cache ();
1375 /* See gdbsupport/common-gdbthread.h. */
1378 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1380 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1381 switch_to_thread (thr
);
1387 scoped_restore_current_thread::restore ()
1389 /* If an entry of thread_info was previously selected, it won't be
1390 deleted because we've increased its refcount. The thread represented
1391 by this thread_info entry may have already exited (due to normal exit,
1392 detach, etc), so the thread_info.state is THREAD_EXITED. */
1393 if (m_thread
!= NULL
1394 /* If the previously selected thread belonged to a process that has
1395 in the mean time exited (or killed, detached, etc.), then don't revert
1396 back to it, but instead simply drop back to no thread selected. */
1398 switch_to_thread (m_thread
.get ());
1400 switch_to_inferior_no_thread (m_inf
.get ());
1402 /* The running state of the originally selected thread may have
1403 changed, so we have to recheck it here. */
1404 if (inferior_ptid
!= null_ptid
1406 && m_thread
->state
== THREAD_STOPPED
1407 && target_has_registers ()
1408 && target_has_stack ()
1409 && target_has_memory ())
1410 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1412 set_language (m_lang
);
1415 scoped_restore_current_thread::~scoped_restore_current_thread ()
1417 if (!m_dont_restore
)
1421 scoped_restore_current_thread::scoped_restore_current_thread ()
1423 m_inf
= inferior_ref::new_reference (current_inferior ());
1425 m_lang
= current_language
->la_language
;
1427 if (inferior_ptid
!= null_ptid
)
1429 m_thread
= thread_info_ref::new_reference (inferior_thread ());
1431 m_was_stopped
= m_thread
->state
== THREAD_STOPPED
;
1432 save_selected_frame (&m_selected_frame_id
, &m_selected_frame_level
);
1436 /* See gdbthread.h. */
1439 show_thread_that_caused_stop (void)
1441 return highest_thread_num
> 1;
1444 /* See gdbthread.h. */
1447 show_inferior_qualified_tids (void)
1449 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1452 /* See gdbthread.h. */
1455 print_thread_id (struct thread_info
*thr
)
1457 char *s
= get_print_cell ();
1459 if (show_inferior_qualified_tids ())
1460 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1462 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1466 /* Sort an array of struct thread_info pointers by thread ID (first by
1467 inferior number, and then by per-inferior thread number). Sorts in
1471 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1473 if (a
->inf
->num
!= b
->inf
->num
)
1474 return a
->inf
->num
< b
->inf
->num
;
1476 return (a
->per_inf_num
< b
->per_inf_num
);
1479 /* Sort an array of struct thread_info pointers by thread ID (first by
1480 inferior number, and then by per-inferior thread number). Sorts in
1481 descending order. */
1484 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1486 if (a
->inf
->num
!= b
->inf
->num
)
1487 return a
->inf
->num
> b
->inf
->num
;
1489 return (a
->per_inf_num
> b
->per_inf_num
);
1492 /* Switch to thread THR and execute CMD.
1493 FLAGS.QUIET controls the printing of the thread information.
1494 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1497 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1498 const qcs_flags
&flags
)
1500 switch_to_thread (thr
);
1502 /* The thread header is computed before running the command since
1503 the command can change the inferior, which is not permitted
1504 by thread_target_id_str. */
1505 std::string thr_header
=
1506 string_printf (_("\nThread %s (%s):\n"), print_thread_id (thr
),
1507 thread_target_id_str (thr
).c_str ());
1511 std::string cmd_result
= execute_command_to_string
1512 (cmd
, from_tty
, gdb_stdout
->term_out ());
1513 if (!flags
.silent
|| cmd_result
.length () > 0)
1516 printf_filtered ("%s", thr_header
.c_str ());
1517 printf_filtered ("%s", cmd_result
.c_str ());
1520 catch (const gdb_exception_error
&ex
)
1525 printf_filtered ("%s", thr_header
.c_str ());
1527 printf_filtered ("%s\n", ex
.what ());
1534 /* Option definition of "thread apply"'s "-ascending" option. */
1536 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1539 Call COMMAND for all threads in ascending order.\n\
1540 The default is descending order."),
1543 /* The qcs command line flags for the "thread apply" commands. Keep
1544 this in sync with the "frame apply" commands. */
1546 using qcs_flag_option_def
1547 = gdb::option::flag_option_def
<qcs_flags
>;
1549 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1550 qcs_flag_option_def
{
1551 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1552 N_("Disables printing the thread information."),
1555 qcs_flag_option_def
{
1556 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1557 N_("Print any error raised by COMMAND and continue."),
1560 qcs_flag_option_def
{
1561 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1562 N_("Silently ignore any errors or empty output produced by COMMAND."),
1566 /* Create an option_def_group for the "thread apply all" options, with
1567 ASCENDING and FLAGS as context. */
1569 static inline std::array
<gdb::option::option_def_group
, 2>
1570 make_thread_apply_all_options_def_group (bool *ascending
,
1574 { {ascending_option_def
.def ()}, ascending
},
1575 { {thr_qcs_flags_option_defs
}, flags
},
1579 /* Create an option_def_group for the "thread apply" options, with
1580 FLAGS as context. */
1582 static inline gdb::option::option_def_group
1583 make_thread_apply_options_def_group (qcs_flags
*flags
)
1585 return {{thr_qcs_flags_option_defs
}, flags
};
1588 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1589 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1590 of two numbers separated by a hyphen. Examples:
1592 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1593 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1594 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1597 thread_apply_all_command (const char *cmd
, int from_tty
)
1599 bool ascending
= false;
1602 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1604 gdb::option::process_options
1605 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1607 validate_flags_qcs ("thread apply all", &flags
);
1609 if (cmd
== NULL
|| *cmd
== '\000')
1610 error (_("Please specify a command at the end of 'thread apply all'"));
1612 update_thread_list ();
1614 int tc
= live_threads_count ();
1617 /* Save a copy of the thread list and increment each thread's
1618 refcount while executing the command in the context of each
1619 thread, in case the command is one that wipes threads. E.g.,
1620 detach, kill, disconnect, etc., or even normally continuing
1621 over an inferior or thread exit. */
1622 std::vector
<thread_info
*> thr_list_cpy
;
1623 thr_list_cpy
.reserve (tc
);
1625 for (thread_info
*tp
: all_non_exited_threads ())
1626 thr_list_cpy
.push_back (tp
);
1627 gdb_assert (thr_list_cpy
.size () == tc
);
1629 /* Increment the refcounts, and restore them back on scope
1631 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1633 auto *sorter
= (ascending
1634 ? tp_array_compar_ascending
1635 : tp_array_compar_descending
);
1636 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1638 scoped_restore_current_thread restore_thread
;
1640 for (thread_info
*thr
: thr_list_cpy
)
1641 if (switch_to_thread_if_alive (thr
))
1642 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1646 /* Completer for "thread apply [ID list]". */
1649 thread_apply_command_completer (cmd_list_element
*ignore
,
1650 completion_tracker
&tracker
,
1651 const char *text
, const char * /*word*/)
1653 /* Don't leave this to complete_options because there's an early
1655 tracker
.set_use_custom_word_point (true);
1657 tid_range_parser parser
;
1658 parser
.init (text
, current_inferior ()->num
);
1662 while (!parser
.finished ())
1664 int inf_num
, thr_start
, thr_end
;
1666 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1669 if (parser
.in_star_range () || parser
.in_thread_range ())
1670 parser
.skip_range ();
1673 catch (const gdb_exception_error
&ex
)
1675 /* get_tid_range throws if it parses a negative number, for
1676 example. But a seemingly negative number may be the start of
1677 an option instead. */
1680 const char *cmd
= parser
.cur_tok ();
1684 /* No thread ID list yet. */
1688 /* Check if we're past a valid thread ID list already. */
1689 if (parser
.finished ()
1690 && cmd
> text
&& !isspace (cmd
[-1]))
1693 /* We're past the thread ID list, advance word point. */
1694 tracker
.advance_custom_word_point_by (cmd
- text
);
1697 const auto group
= make_thread_apply_options_def_group (nullptr);
1698 if (gdb::option::complete_options
1699 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1702 complete_nested_command_line (tracker
, text
);
1705 /* Completer for "thread apply all". */
1708 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1709 completion_tracker
&tracker
,
1710 const char *text
, const char *word
)
1712 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1714 if (gdb::option::complete_options
1715 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1718 complete_nested_command_line (tracker
, text
);
1721 /* Implementation of the "thread apply" command. */
1724 thread_apply_command (const char *tidlist
, int from_tty
)
1727 const char *cmd
= NULL
;
1728 tid_range_parser parser
;
1730 if (tidlist
== NULL
|| *tidlist
== '\000')
1731 error (_("Please specify a thread ID list"));
1733 parser
.init (tidlist
, current_inferior ()->num
);
1734 while (!parser
.finished ())
1736 int inf_num
, thr_start
, thr_end
;
1738 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1742 cmd
= parser
.cur_tok ();
1744 auto group
= make_thread_apply_options_def_group (&flags
);
1745 gdb::option::process_options
1746 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1748 validate_flags_qcs ("thread apply", &flags
);
1751 error (_("Please specify a command following the thread ID list"));
1753 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1754 invalid_thread_id_error (cmd
);
1756 scoped_restore_current_thread restore_thread
;
1758 parser
.init (tidlist
, current_inferior ()->num
);
1759 while (!parser
.finished ())
1761 struct thread_info
*tp
= NULL
;
1762 struct inferior
*inf
;
1763 int inf_num
, thr_num
;
1765 parser
.get_tid (&inf_num
, &thr_num
);
1766 inf
= find_inferior_id (inf_num
);
1768 tp
= find_thread_id (inf
, thr_num
);
1770 if (parser
.in_star_range ())
1774 warning (_("Unknown inferior %d"), inf_num
);
1775 parser
.skip_range ();
1779 /* No use looking for threads past the highest thread number
1780 the inferior ever had. */
1781 if (thr_num
>= inf
->highest_thread_num
)
1782 parser
.skip_range ();
1784 /* Be quiet about unknown threads numbers. */
1791 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1792 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1794 warning (_("Unknown thread %d"), thr_num
);
1798 if (!switch_to_thread_if_alive (tp
))
1800 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1804 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1809 /* Implementation of the "taas" command. */
1812 taas_command (const char *cmd
, int from_tty
)
1814 if (cmd
== NULL
|| *cmd
== '\0')
1815 error (_("Please specify a command to apply on all threads"));
1816 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1817 execute_command (expanded
.c_str (), from_tty
);
1820 /* Implementation of the "tfaas" command. */
1823 tfaas_command (const char *cmd
, int from_tty
)
1825 if (cmd
== NULL
|| *cmd
== '\0')
1826 error (_("Please specify a command to apply on all frames of all threads"));
1827 std::string expanded
1828 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1829 execute_command (expanded
.c_str (), from_tty
);
1832 /* Switch to the specified thread, or print the current thread. */
1835 thread_command (const char *tidstr
, int from_tty
)
1839 if (inferior_ptid
== null_ptid
)
1840 error (_("No thread selected"));
1842 if (target_has_stack ())
1844 struct thread_info
*tp
= inferior_thread ();
1846 if (tp
->state
== THREAD_EXITED
)
1847 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1848 print_thread_id (tp
),
1849 target_pid_to_str (inferior_ptid
).c_str ());
1851 printf_filtered (_("[Current thread is %s (%s)]\n"),
1852 print_thread_id (tp
),
1853 target_pid_to_str (inferior_ptid
).c_str ());
1856 error (_("No stack."));
1860 ptid_t previous_ptid
= inferior_ptid
;
1862 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1864 /* Print if the thread has not changed, otherwise an event will
1866 if (inferior_ptid
== previous_ptid
)
1868 print_selected_thread_frame (current_uiout
,
1869 USER_SELECTED_THREAD
1870 | USER_SELECTED_FRAME
);
1874 gdb::observers::user_selected_context_changed
.notify
1875 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1880 /* Implementation of `thread name'. */
1883 thread_name_command (const char *arg
, int from_tty
)
1885 struct thread_info
*info
;
1887 if (inferior_ptid
== null_ptid
)
1888 error (_("No thread selected"));
1890 arg
= skip_spaces (arg
);
1892 info
= inferior_thread ();
1894 info
->name
= arg
? xstrdup (arg
) : NULL
;
1897 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1900 thread_find_command (const char *arg
, int from_tty
)
1903 unsigned long match
= 0;
1905 if (arg
== NULL
|| *arg
== '\0')
1906 error (_("Command requires an argument."));
1908 tmp
= re_comp (arg
);
1910 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1912 /* We're going to be switching threads. */
1913 scoped_restore_current_thread restore_thread
;
1915 update_thread_list ();
1917 for (thread_info
*tp
: all_threads ())
1919 switch_to_inferior_no_thread (tp
->inf
);
1921 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1923 printf_filtered (_("Thread %s has name '%s'\n"),
1924 print_thread_id (tp
), tp
->name
);
1928 tmp
= target_thread_name (tp
);
1929 if (tmp
!= NULL
&& re_exec (tmp
))
1931 printf_filtered (_("Thread %s has target name '%s'\n"),
1932 print_thread_id (tp
), tmp
);
1936 std::string name
= target_pid_to_str (tp
->ptid
);
1937 if (!name
.empty () && re_exec (name
.c_str ()))
1939 printf_filtered (_("Thread %s has target id '%s'\n"),
1940 print_thread_id (tp
), name
.c_str ());
1944 tmp
= target_extra_thread_info (tp
);
1945 if (tmp
!= NULL
&& re_exec (tmp
))
1947 printf_filtered (_("Thread %s has extra info '%s'\n"),
1948 print_thread_id (tp
), tmp
);
1953 printf_filtered (_("No threads match '%s'\n"), arg
);
1956 /* Print notices when new threads are attached and detached. */
1957 bool print_thread_events
= true;
1959 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1960 struct cmd_list_element
*c
, const char *value
)
1962 fprintf_filtered (file
,
1963 _("Printing of thread events is %s.\n"),
1967 /* See gdbthread.h. */
1970 thread_select (const char *tidstr
, thread_info
*tp
)
1972 if (!switch_to_thread_if_alive (tp
))
1973 error (_("Thread ID %s has terminated."), tidstr
);
1975 annotate_thread_changed ();
1977 /* Since the current thread may have changed, see if there is any
1978 exited thread we can now delete. */
1979 delete_exited_threads ();
1982 /* Print thread and frame switch command response. */
1985 print_selected_thread_frame (struct ui_out
*uiout
,
1986 user_selected_what selection
)
1988 struct thread_info
*tp
= inferior_thread ();
1990 if (selection
& USER_SELECTED_THREAD
)
1992 if (uiout
->is_mi_like_p ())
1994 uiout
->field_signed ("new-thread-id",
1995 inferior_thread ()->global_num
);
1999 uiout
->text ("[Switching to thread ");
2000 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2002 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2007 if (tp
->state
== THREAD_RUNNING
)
2009 if (selection
& USER_SELECTED_THREAD
)
2010 uiout
->text ("(running)\n");
2012 else if (selection
& USER_SELECTED_FRAME
)
2014 if (selection
& USER_SELECTED_THREAD
)
2017 if (has_stack_frames ())
2018 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2023 /* Update the 'threads_executing' global based on the threads we know
2024 about right now. This is used by infrun to tell whether we should
2025 pull events out of the current target. */
2028 update_threads_executing (void)
2030 process_stratum_target
*targ
= current_inferior ()->process_target ();
2035 targ
->threads_executing
= false;
2037 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2039 if (!inf
->has_execution ())
2042 /* If the process has no threads, then it must be we have a
2043 process-exit event pending. */
2044 if (inf
->thread_list
== NULL
)
2046 targ
->threads_executing
= true;
2050 for (thread_info
*tp
: inf
->non_exited_threads ())
2054 targ
->threads_executing
= true;
2062 update_thread_list (void)
2064 target_update_thread_list ();
2065 update_threads_executing ();
2068 /* Return a new value for the selected thread's id. Return a value of
2069 0 if no thread is selected. If GLOBAL is true, return the thread's
2070 global number. Otherwise return the per-inferior number. */
2072 static struct value
*
2073 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2077 if (inferior_ptid
== null_ptid
)
2081 thread_info
*tp
= inferior_thread ();
2083 int_val
= tp
->global_num
;
2085 int_val
= tp
->per_inf_num
;
2088 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2091 /* Return a new value for the selected thread's per-inferior thread
2092 number. Return a value of 0 if no thread is selected, or no
2095 static struct value
*
2096 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2097 struct internalvar
*var
,
2100 return thread_num_make_value_helper (gdbarch
, 0);
2103 /* Return a new value for the selected thread's global id. Return a
2104 value of 0 if no thread is selected, or no threads exist. */
2106 static struct value
*
2107 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2110 return thread_num_make_value_helper (gdbarch
, 1);
2113 /* Commands with a prefix of `thread'. */
2114 struct cmd_list_element
*thread_cmd_list
= NULL
;
2116 /* Implementation of `thread' variable. */
2118 static const struct internalvar_funcs thread_funcs
=
2120 thread_id_per_inf_num_make_value
,
2125 /* Implementation of `gthread' variable. */
2127 static const struct internalvar_funcs gthread_funcs
=
2129 global_thread_id_make_value
,
2134 void _initialize_thread ();
2136 _initialize_thread ()
2138 static struct cmd_list_element
*thread_apply_list
= NULL
;
2139 cmd_list_element
*c
;
2141 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2143 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2145 static std::string info_threads_help
2146 = gdb::option::build_help (_("\
2147 Display currently known threads.\n\
2148 Usage: info threads [OPTION]... [ID]...\n\
2149 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2150 Otherwise, all threads are displayed.\n\
2156 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2157 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2159 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2160 Use this command to switch between threads.\n\
2161 The new thread ID must be currently known."),
2162 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2164 #define THREAD_APPLY_OPTION_HELP "\
2165 Prints per-inferior thread number and target system's thread id\n\
2166 followed by COMMAND output.\n\
2168 By default, an error raised during the execution of COMMAND\n\
2169 aborts \"thread apply\".\n\
2174 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2176 static std::string thread_apply_help
= gdb::option::build_help (_("\
2177 Apply a command to a list of threads.\n\
2178 Usage: thread apply ID... [OPTION]... COMMAND\n\
2179 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2180 THREAD_APPLY_OPTION_HELP
),
2183 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2184 thread_apply_help
.c_str (),
2185 &thread_apply_list
, "thread apply ", 1,
2187 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2189 const auto thread_apply_all_opts
2190 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2192 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2193 Apply a command to all threads.\n\
2195 Usage: thread apply all [OPTION]... COMMAND\n"
2196 THREAD_APPLY_OPTION_HELP
),
2197 thread_apply_all_opts
);
2199 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2200 thread_apply_all_help
.c_str (),
2201 &thread_apply_list
);
2202 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2204 c
= add_com ("taas", class_run
, taas_command
, _("\
2205 Apply a command to all threads (ignoring errors and empty output).\n\
2206 Usage: taas [OPTION]... COMMAND\n\
2207 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2208 See \"help thread apply all\" for available options."));
2209 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2211 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2212 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2213 Usage: tfaas [OPTION]... COMMAND\n\
2214 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2215 See \"help frame apply all\" for available options."));
2216 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2218 add_cmd ("name", class_run
, thread_name_command
,
2219 _("Set the current thread's name.\n\
2220 Usage: thread name [NAME]\n\
2221 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2223 add_cmd ("find", class_run
, thread_find_command
, _("\
2224 Find threads that match a regular expression.\n\
2225 Usage: thread find REGEXP\n\
2226 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2229 add_com_alias ("t", "thread", class_run
, 1);
2231 add_setshow_boolean_cmd ("thread-events", no_class
,
2232 &print_thread_events
, _("\
2233 Set printing of thread events (such as thread start and exit)."), _("\
2234 Show printing of thread events (such as thread start and exit)."), NULL
,
2236 show_print_thread_events
,
2237 &setprintlist
, &showprintlist
);
2239 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2240 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);