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 /* True if any thread is, or may be executing. We need to track this
59 separately because until we fully sync the thread list, we won't
60 know whether the target is fully stopped, even if we see stop
61 events for all known threads, because any of those threads may have
62 spawned new threads we haven't heard of yet. */
63 static int threads_executing
;
65 /* RAII type used to increase / decrease the refcount of each thread
66 in a given list of threads. */
68 class scoped_inc_dec_ref
71 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
74 for (thread_info
*thr
: m_thrds
)
78 ~scoped_inc_dec_ref ()
80 for (thread_info
*thr
: m_thrds
)
85 const std::vector
<thread_info
*> &m_thrds
;
90 inferior_thread (void)
92 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
97 /* Delete the breakpoint pointed at by BP_P, if there's one. */
100 delete_thread_breakpoint (struct breakpoint
**bp_p
)
104 delete_breakpoint (*bp_p
);
110 delete_step_resume_breakpoint (struct thread_info
*tp
)
113 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
117 delete_exception_resume_breakpoint (struct thread_info
*tp
)
120 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
123 /* See gdbthread.h. */
126 delete_single_step_breakpoints (struct thread_info
*tp
)
129 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
132 /* Delete the breakpoint pointed at by BP_P at the next stop, if
136 delete_at_next_stop (struct breakpoint
**bp
)
140 (*bp
)->disposition
= disp_del_at_next_stop
;
145 /* See gdbthread.h. */
148 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
150 return tp
->control
.single_step_breakpoints
!= NULL
;
153 /* See gdbthread.h. */
156 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
157 const address_space
*aspace
,
160 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
162 return (ss_bps
!= NULL
163 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
166 /* See gdbthread.h. */
169 thread_cancel_execution_command (struct thread_info
*thr
)
171 if (thr
->thread_fsm
!= NULL
)
173 thr
->thread_fsm
->clean_up (thr
);
174 delete thr
->thread_fsm
;
175 thr
->thread_fsm
= NULL
;
180 clear_thread_inferior_resources (struct thread_info
*tp
)
182 /* NOTE: this will take care of any left-over step_resume breakpoints,
183 but not any user-specified thread-specific breakpoints. We can not
184 delete the breakpoint straight-off, because the inferior might not
185 be stopped at the moment. */
186 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
187 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
188 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
190 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
192 bpstat_clear (&tp
->control
.stop_bpstat
);
194 btrace_teardown (tp
);
196 thread_cancel_execution_command (tp
);
198 clear_inline_frame_state (tp
->ptid
);
201 /* Set the TP's state as exited. */
204 set_thread_exited (thread_info
*tp
, int silent
)
206 /* Dead threads don't need to step-over. Remove from queue. */
207 if (tp
->step_over_next
!= NULL
)
208 thread_step_over_chain_remove (tp
);
210 if (tp
->state
!= THREAD_EXITED
)
212 gdb::observers::thread_exit
.notify (tp
, silent
);
214 /* Tag it as exited. */
215 tp
->state
= THREAD_EXITED
;
217 /* Clear breakpoints, etc. associated with this thread. */
218 clear_thread_inferior_resources (tp
);
223 init_thread_list (void)
225 highest_thread_num
= 0;
227 for (thread_info
*tp
: all_threads_safe ())
229 inferior
*inf
= tp
->inf
;
231 if (tp
->deletable ())
234 set_thread_exited (tp
, 1);
236 inf
->thread_list
= NULL
;
240 /* Allocate a new thread of inferior INF with target id PTID and add
241 it to the thread list. */
243 static struct thread_info
*
244 new_thread (struct inferior
*inf
, ptid_t ptid
)
246 thread_info
*tp
= new thread_info (inf
, ptid
);
248 if (inf
->thread_list
== NULL
)
249 inf
->thread_list
= tp
;
252 struct thread_info
*last
;
254 for (last
= inf
->thread_list
; last
->next
!= NULL
; last
= last
->next
)
263 add_thread_silent (ptid_t ptid
)
265 struct inferior
*inf
= find_inferior_ptid (ptid
);
266 gdb_assert (inf
!= NULL
);
268 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
270 /* Found an old thread with the same id. It has to be dead,
271 otherwise we wouldn't be adding a new thread with the same id.
272 The OS is reusing this id --- delete it, and recreate a new
275 /* In addition to deleting the thread, if this is the current
276 thread, then we need to take care that delete_thread doesn't
277 really delete the thread if it is inferior_ptid. Create a
278 new template thread in the list with an invalid ptid, switch
279 to it, delete the original thread, reset the new thread's
280 ptid, and switch to it. */
282 if (inferior_ptid
== ptid
)
284 thread_info
*new_thr
= new_thread (inf
, null_ptid
);
286 /* Make switch_to_thread not read from the thread. */
287 new_thr
->state
= THREAD_EXITED
;
288 switch_to_no_thread ();
290 /* Now we can delete it. */
293 /* Now reset its ptid, and reswitch inferior_ptid to it. */
294 new_thr
->ptid
= ptid
;
295 new_thr
->state
= THREAD_STOPPED
;
296 switch_to_thread (new_thr
);
298 gdb::observers::new_thread
.notify (new_thr
);
304 /* Just go ahead and delete it. */
308 tp
= new_thread (inf
, ptid
);
309 gdb::observers::new_thread
.notify (tp
);
315 add_thread_with_info (ptid_t ptid
, private_thread_info
*priv
)
317 struct thread_info
*result
= add_thread_silent (ptid
);
319 result
->priv
.reset (priv
);
321 if (print_thread_events
)
322 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
324 annotate_new_thread ();
329 add_thread (ptid_t ptid
)
331 return add_thread_with_info (ptid
, NULL
);
334 private_thread_info::~private_thread_info () = default;
336 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
337 : ptid (ptid_
), inf (inf_
)
339 gdb_assert (inf_
!= NULL
);
341 this->global_num
= ++highest_thread_num
;
342 this->per_inf_num
= ++inf_
->highest_thread_num
;
344 /* Nothing to follow yet. */
345 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
346 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
347 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
350 thread_info::~thread_info ()
355 /* See gdbthread.h. */
358 thread_info::deletable () const
360 /* If this is the current thread, or there's code out there that
361 relies on it existing (refcount > 0) we can't delete yet. */
362 return refcount () == 0 && ptid
!= inferior_ptid
;
365 /* Add TP to the end of the step-over chain LIST_P. */
368 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
370 gdb_assert (tp
->step_over_next
== NULL
);
371 gdb_assert (tp
->step_over_prev
== NULL
);
376 tp
->step_over_prev
= tp
->step_over_next
= tp
;
380 struct thread_info
*head
= *list_p
;
381 struct thread_info
*tail
= head
->step_over_prev
;
383 tp
->step_over_prev
= tail
;
384 tp
->step_over_next
= head
;
385 head
->step_over_prev
= tp
;
386 tail
->step_over_next
= tp
;
390 /* Remove TP from step-over chain LIST_P. */
393 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
395 gdb_assert (tp
->step_over_next
!= NULL
);
396 gdb_assert (tp
->step_over_prev
!= NULL
);
400 if (tp
== tp
->step_over_next
)
403 *list_p
= tp
->step_over_next
;
406 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
407 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
408 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
411 /* See gdbthread.h. */
414 thread_step_over_chain_next (struct thread_info
*tp
)
416 struct thread_info
*next
= tp
->step_over_next
;
418 return (next
== step_over_queue_head
? NULL
: next
);
421 /* See gdbthread.h. */
424 thread_is_in_step_over_chain (struct thread_info
*tp
)
426 return (tp
->step_over_next
!= NULL
);
429 /* See gdbthread.h. */
432 thread_step_over_chain_enqueue (struct thread_info
*tp
)
434 step_over_chain_enqueue (&step_over_queue_head
, tp
);
437 /* See gdbthread.h. */
440 thread_step_over_chain_remove (struct thread_info
*tp
)
442 step_over_chain_remove (&step_over_queue_head
, tp
);
445 /* Delete the thread referenced by THR. If SILENT, don't notify
446 the observer of this exit.
448 THR must not be NULL or a failed assertion will be raised. */
451 delete_thread_1 (thread_info
*thr
, bool silent
)
453 gdb_assert (thr
!= nullptr);
455 struct thread_info
*tp
, *tpprev
= NULL
;
457 for (tp
= thr
->inf
->thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
464 set_thread_exited (tp
, silent
);
466 if (!tp
->deletable ())
468 /* Will be really deleted some other time. */
473 tpprev
->next
= tp
->next
;
475 tp
->inf
->thread_list
= tp
->next
;
480 /* Delete thread THREAD and notify of thread exit. If this is the
481 current thread, don't actually delete it, but tag it as exited and
482 do the notification. If this is the user selected thread, clear
486 delete_thread (thread_info
*thread
)
488 delete_thread_1 (thread
, false /* not silent */);
492 delete_thread_silent (thread_info
*thread
)
494 delete_thread_1 (thread
, true /* silent */);
498 find_thread_global_id (int global_id
)
500 for (thread_info
*tp
: all_threads ())
501 if (tp
->global_num
== global_id
)
507 static struct thread_info
*
508 find_thread_id (struct inferior
*inf
, int thr_num
)
510 for (thread_info
*tp
: inf
->threads ())
511 if (tp
->per_inf_num
== thr_num
)
517 /* Find a thread_info by matching PTID. */
520 find_thread_ptid (ptid_t ptid
)
522 inferior
*inf
= find_inferior_ptid (ptid
);
525 return find_thread_ptid (inf
, ptid
);
528 /* See gdbthread.h. */
531 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
533 for (thread_info
*tp
: inf
->threads ())
534 if (tp
->ptid
== ptid
)
540 /* See gdbthread.h. */
543 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
544 struct inferior
*inf
)
546 return target_thread_handle_to_thread_info (handle
.data (),
552 * Thread iterator function.
554 * Calls a callback function once for each thread, so long as
555 * the callback function returns false. If the callback function
556 * returns true, the iteration will end and the current thread
557 * will be returned. This can be useful for implementing a
558 * search for a thread with arbitrary attributes, or for applying
559 * some operation to every thread.
561 * FIXME: some of the existing functionality, such as
562 * "Thread apply all", might be rewritten using this functionality.
566 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
569 for (thread_info
*tp
: all_threads_safe ())
570 if ((*callback
) (tp
, data
))
576 /* See gdbthread.h. */
581 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
589 auto rng
= all_threads ();
590 return std::distance (rng
.begin (), rng
.end ());
593 /* Return the number of non-exited threads in the thread list. */
596 live_threads_count (void)
598 auto rng
= all_non_exited_threads ();
599 return std::distance (rng
.begin (), rng
.end ());
603 valid_global_thread_id (int global_id
)
605 for (thread_info
*tp
: all_threads ())
606 if (tp
->global_num
== global_id
)
613 in_thread_list (ptid_t ptid
)
615 return find_thread_ptid (ptid
) != nullptr;
618 /* Finds the first thread of the inferior. */
621 first_thread_of_inferior (inferior
*inf
)
623 return inf
->thread_list
;
627 any_thread_of_inferior (inferior
*inf
)
629 gdb_assert (inf
->pid
!= 0);
631 /* Prefer the current thread. */
632 if (inf
== current_inferior ())
633 return inferior_thread ();
635 for (thread_info
*tp
: inf
->non_exited_threads ())
642 any_live_thread_of_inferior (inferior
*inf
)
644 struct thread_info
*curr_tp
= NULL
;
645 struct thread_info
*tp_executing
= NULL
;
647 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
649 /* Prefer the current thread if it's not executing. */
650 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
652 /* If the current thread is dead, forget it. If it's not
653 executing, use it. Otherwise, still choose it (below), but
654 only if no other non-executing thread is found. */
655 curr_tp
= inferior_thread ();
656 if (curr_tp
->state
== THREAD_EXITED
)
658 else if (!curr_tp
->executing
)
662 for (thread_info
*tp
: inf
->non_exited_threads ())
670 /* If both the current thread and all live threads are executing,
671 prefer the current thread. */
675 /* Otherwise, just return an executing thread, if any. */
679 /* Return true if TP is an active thread. */
681 thread_alive (thread_info
*tp
)
683 if (tp
->state
== THREAD_EXITED
)
686 /* Ensure we're looking at the right target stack. */
687 gdb_assert (tp
->inf
== current_inferior ());
689 return target_thread_alive (tp
->ptid
);
692 /* Switch to thread TP if it is alive. Returns true if successfully
693 switched, false otherwise. */
696 switch_to_thread_if_alive (thread_info
*thr
)
698 scoped_restore_current_thread restore_thread
;
700 /* Switch inferior first, so that we're looking at the right target
702 switch_to_inferior_no_thread (thr
->inf
);
704 if (thread_alive (thr
))
706 switch_to_thread (thr
);
707 restore_thread
.dont_restore ();
714 /* See gdbthreads.h. */
719 scoped_restore_current_thread restore_thread
;
721 for (thread_info
*tp
: all_threads_safe ())
723 switch_to_inferior_no_thread (tp
->inf
);
725 if (!thread_alive (tp
))
730 /* See gdbthreads.h. */
733 delete_exited_threads (void)
735 for (thread_info
*tp
: all_threads_safe ())
736 if (tp
->state
== THREAD_EXITED
)
740 /* Return true value if stack temporaries are enabled for the thread
744 thread_stack_temporaries_enabled_p (thread_info
*tp
)
749 return tp
->stack_temporaries_enabled
;
752 /* Push V on to the stack temporaries of the thread with id PTID. */
755 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
757 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
758 tp
->stack_temporaries
.push_back (v
);
761 /* Return true if VAL is among the stack temporaries of the thread
762 TP. Return false otherwise. */
765 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
767 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
768 for (value
*v
: tp
->stack_temporaries
)
775 /* Return the last of the stack temporaries for thread with id PTID.
776 Return NULL if there are no stack temporaries for the thread. */
779 get_last_thread_stack_temporary (thread_info
*tp
)
781 struct value
*lastval
= NULL
;
783 gdb_assert (tp
!= NULL
);
784 if (!tp
->stack_temporaries
.empty ())
785 lastval
= tp
->stack_temporaries
.back ();
791 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
793 struct inferior
*inf
;
794 struct thread_info
*tp
;
796 /* It can happen that what we knew as the target inferior id
797 changes. E.g, target remote may only discover the remote process
798 pid after adding the inferior to GDB's list. */
799 inf
= find_inferior_ptid (old_ptid
);
800 inf
->pid
= new_ptid
.pid ();
802 tp
= find_thread_ptid (inf
, old_ptid
);
805 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
808 /* See gdbthread.h. */
811 set_resumed (ptid_t ptid
, int resumed
)
813 for (thread_info
*tp
: all_non_exited_threads (ptid
))
814 tp
->resumed
= resumed
;
817 /* Helper for set_running, that marks one thread either running or
821 set_running_thread (struct thread_info
*tp
, int running
)
825 if (running
&& tp
->state
== THREAD_STOPPED
)
827 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
831 /* If the thread is now marked stopped, remove it from
832 the step-over queue, so that we don't try to resume
833 it until the user wants it to. */
834 if (tp
->step_over_next
!= NULL
)
835 thread_step_over_chain_remove (tp
);
841 /* See gdbthread.h. */
844 thread_info::set_running (bool running
)
846 if (set_running_thread (this, running
))
847 gdb::observers::target_resumed
.notify (this->ptid
);
851 set_running (ptid_t ptid
, int running
)
853 /* We try not to notify the observer if no thread has actually
854 changed the running state -- merely to reduce the number of
855 messages to the MI frontend. A frontend is supposed to handle
856 multiple *running notifications just fine. */
857 bool any_started
= false;
859 for (thread_info
*tp
: all_non_exited_threads (ptid
))
860 if (set_running_thread (tp
, running
))
864 gdb::observers::target_resumed
.notify (ptid
);
868 /* Helper for set_executing. Set's the thread's 'executing' field
869 from EXECUTING, and if EXECUTING is true also clears the thread's
873 set_executing_thread (thread_info
*thr
, bool executing
)
875 thr
->executing
= executing
;
877 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
881 set_executing (ptid_t ptid
, int executing
)
883 for (thread_info
*tp
: all_non_exited_threads (ptid
))
884 set_executing_thread (tp
, executing
);
886 /* It only takes one running thread to spawn more threads. */
888 threads_executing
= 1;
889 /* Only clear the flag if the caller is telling us everything is
891 else if (minus_one_ptid
== ptid
)
892 threads_executing
= 0;
895 /* See gdbthread.h. */
898 threads_are_executing (void)
900 return threads_executing
;
904 set_stop_requested (ptid_t ptid
, int stop
)
906 for (thread_info
*tp
: all_non_exited_threads (ptid
))
907 tp
->stop_requested
= stop
;
909 /* Call the stop requested observer so other components of GDB can
910 react to this request. */
912 gdb::observers::thread_stop_requested
.notify (ptid
);
916 finish_thread_state (ptid_t ptid
)
918 bool any_started
= false;
920 for (thread_info
*tp
: all_non_exited_threads (ptid
))
921 if (set_running_thread (tp
, tp
->executing
))
925 gdb::observers::target_resumed
.notify (ptid
);
928 /* See gdbthread.h. */
931 validate_registers_access (void)
933 /* No selected thread, no registers. */
934 if (inferior_ptid
== null_ptid
)
935 error (_("No thread selected."));
937 thread_info
*tp
= inferior_thread ();
939 /* Don't try to read from a dead thread. */
940 if (tp
->state
== THREAD_EXITED
)
941 error (_("The current thread has terminated"));
943 /* ... or from a spinning thread. FIXME: This isn't actually fully
944 correct. It'll allow an user-requested access (e.g., "print $pc"
945 at the prompt) when a thread is not executing for some internal
946 reason, but is marked running from the user's perspective. E.g.,
947 the thread is waiting for its turn in the step-over queue. */
949 error (_("Selected thread is running."));
952 /* See gdbthread.h. */
955 can_access_registers_thread (thread_info
*thread
)
957 /* No thread, no registers. */
961 /* Don't try to read from a dead thread. */
962 if (thread
->state
== THREAD_EXITED
)
965 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
966 if (thread
->executing
)
973 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
975 return (pc
>= thread
->control
.step_range_start
976 && pc
< thread
->control
.step_range_end
);
979 /* Helper for print_thread_info. Returns true if THR should be
980 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
981 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
982 is true if REQUESTED_THREADS is list of global IDs, false if a list
983 of per-inferior thread ids. If PID is not -1, only print THR if it
984 is a thread from the process PID. Otherwise, threads from all
985 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
986 and PID is not -1, then the thread is printed if it belongs to the
987 specified process. Otherwise, an error is raised. */
990 should_print_thread (const char *requested_threads
, int default_inf_num
,
991 int global_ids
, int pid
, struct thread_info
*thr
)
993 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
998 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1000 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1001 thr
->inf
->num
, thr
->per_inf_num
);
1006 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1008 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1009 error (_("Requested thread not found in requested process"));
1013 if (thr
->state
== THREAD_EXITED
)
1019 /* Return the string to display in "info threads"'s "Target Id"
1023 thread_target_id_str (thread_info
*tp
)
1025 std::string target_id
= target_pid_to_str (tp
->ptid
);
1026 const char *extra_info
= target_extra_thread_info (tp
);
1027 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1029 if (extra_info
!= nullptr && name
!= nullptr)
1030 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1032 else if (extra_info
!= nullptr)
1033 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1034 else if (name
!= nullptr)
1035 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1040 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1041 whether REQUESTED_THREADS is a list of global or per-inferior
1045 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1046 int global_ids
, int pid
,
1047 int show_global_ids
)
1049 int default_inf_num
= current_inferior ()->num
;
1051 update_thread_list ();
1053 /* Whether we saw any thread. */
1054 bool any_thread
= false;
1055 /* Whether the current thread is exited. */
1056 bool current_exited
= false;
1058 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1059 ? inferior_thread () : NULL
);
1062 /* For backward compatibility, we make a list for MI. A table is
1063 preferable for the CLI, though, because it shows table
1065 gdb::optional
<ui_out_emit_list
> list_emitter
;
1066 gdb::optional
<ui_out_emit_table
> table_emitter
;
1068 /* We'll be switching threads temporarily below. */
1069 scoped_restore_current_thread restore_thread
;
1071 if (uiout
->is_mi_like_p ())
1072 list_emitter
.emplace (uiout
, "threads");
1076 /* The width of the "Target Id" column. Grown below to
1077 accommodate the largest entry. */
1078 size_t target_id_col_width
= 17;
1080 for (thread_info
*tp
: all_threads ())
1082 if (!should_print_thread (requested_threads
, default_inf_num
,
1083 global_ids
, pid
, tp
))
1086 if (!uiout
->is_mi_like_p ())
1088 /* Switch inferiors so we're looking at the right
1090 switch_to_inferior_no_thread (tp
->inf
);
1093 = std::max (target_id_col_width
,
1094 thread_target_id_str (tp
).size ());
1102 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1103 uiout
->message (_("No threads.\n"));
1105 uiout
->message (_("No threads match '%s'.\n"),
1110 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1111 n_threads
, "threads");
1113 uiout
->table_header (1, ui_left
, "current", "");
1114 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1115 if (show_global_ids
)
1116 uiout
->table_header (4, ui_left
, "id", "GId");
1117 uiout
->table_header (target_id_col_width
, ui_left
,
1118 "target-id", "Target Id");
1119 uiout
->table_header (1, ui_left
, "frame", "Frame");
1120 uiout
->table_body ();
1123 for (inferior
*inf
: all_inferiors ())
1124 for (thread_info
*tp
: inf
->threads ())
1129 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1130 current_exited
= true;
1132 if (!should_print_thread (requested_threads
, default_inf_num
,
1133 global_ids
, pid
, tp
))
1136 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1138 if (!uiout
->is_mi_like_p ())
1140 if (tp
== current_thread
)
1141 uiout
->field_string ("current", "*");
1143 uiout
->field_skip ("current");
1145 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1148 if (show_global_ids
|| uiout
->is_mi_like_p ())
1149 uiout
->field_signed ("id", tp
->global_num
);
1151 /* Switch to the thread (and inferior / target). */
1152 switch_to_thread (tp
);
1154 /* For the CLI, we stuff everything into the target-id field.
1155 This is a gross hack to make the output come out looking
1156 correct. The underlying problem here is that ui-out has no
1157 way to specify that a field's space allocation should be
1158 shared by several fields. For MI, we do the right thing
1161 if (uiout
->is_mi_like_p ())
1163 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1165 const char *extra_info
= target_extra_thread_info (tp
);
1166 if (extra_info
!= nullptr)
1167 uiout
->field_string ("details", extra_info
);
1169 const char *name
= (tp
->name
!= nullptr
1171 : target_thread_name (tp
));
1173 uiout
->field_string ("name", name
);
1177 uiout
->field_string ("target-id",
1178 thread_target_id_str (tp
).c_str ());
1181 if (tp
->state
== THREAD_RUNNING
)
1182 uiout
->text ("(running)\n");
1185 /* The switch above put us at the top of the stack (leaf
1187 print_stack_frame (get_selected_frame (NULL
),
1188 /* For MI output, print frame level. */
1189 uiout
->is_mi_like_p (),
1193 if (uiout
->is_mi_like_p ())
1195 const char *state
= "stopped";
1197 if (tp
->state
== THREAD_RUNNING
)
1199 uiout
->field_string ("state", state
);
1202 core
= target_core_of_thread (tp
->ptid
);
1203 if (uiout
->is_mi_like_p () && core
!= -1)
1204 uiout
->field_signed ("core", core
);
1207 /* This end scope restores the current thread and the frame
1208 selected before the "info threads" command, and it finishes the
1209 ui-out list or table. */
1212 if (pid
== -1 && requested_threads
== NULL
)
1214 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1215 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1217 if (inferior_ptid
!= null_ptid
&& current_exited
)
1218 uiout
->message ("\n\
1219 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1220 print_thread_id (inferior_thread ()));
1221 else if (any_thread
&& inferior_ptid
== null_ptid
)
1222 uiout
->message ("\n\
1223 No selected thread. See `help thread'.\n");
1227 /* See gdbthread.h. */
1230 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1233 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1236 /* The options for the "info threads" command. */
1238 struct info_threads_opts
1241 bool show_global_ids
= false;
1244 static const gdb::option::option_def info_threads_option_defs
[] = {
1246 gdb::option::flag_option_def
<info_threads_opts
> {
1248 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1249 N_("Show global thread IDs."),
1254 /* Create an option_def_group for the "info threads" options, with
1255 IT_OPTS as context. */
1257 static inline gdb::option::option_def_group
1258 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1260 return {{info_threads_option_defs
}, it_opts
};
1263 /* Implementation of the "info threads" command.
1265 Note: this has the drawback that it _really_ switches
1266 threads, which frees the frame cache. A no-side
1267 effects info-threads command would be nicer. */
1270 info_threads_command (const char *arg
, int from_tty
)
1272 info_threads_opts it_opts
;
1274 auto grp
= make_info_threads_options_def_group (&it_opts
);
1275 gdb::option::process_options
1276 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1278 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1281 /* Completer for the "info threads" command. */
1284 info_threads_command_completer (struct cmd_list_element
*ignore
,
1285 completion_tracker
&tracker
,
1286 const char *text
, const char *word_ignored
)
1288 const auto grp
= make_info_threads_options_def_group (nullptr);
1290 if (gdb::option::complete_options
1291 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1294 /* Convenience to let the user know what the option can accept. */
1297 gdb::option::complete_on_all_options (tracker
, grp
);
1298 /* Keep this "ID" in sync with what "help info threads"
1300 tracker
.add_completion (make_unique_xstrdup ("ID"));
1304 /* See gdbthread.h. */
1307 switch_to_thread_no_regs (struct thread_info
*thread
)
1309 struct inferior
*inf
= thread
->inf
;
1311 set_current_program_space (inf
->pspace
);
1312 set_current_inferior (inf
);
1314 inferior_ptid
= thread
->ptid
;
1317 /* See gdbthread.h. */
1320 switch_to_no_thread ()
1322 if (inferior_ptid
== null_ptid
)
1325 inferior_ptid
= null_ptid
;
1326 reinit_frame_cache ();
1329 /* See gdbthread.h. */
1332 switch_to_thread (thread_info
*thr
)
1334 gdb_assert (thr
!= NULL
);
1336 if (inferior_ptid
== thr
->ptid
)
1339 switch_to_thread_no_regs (thr
);
1341 reinit_frame_cache ();
1344 /* See gdbsupport/common-gdbthread.h. */
1347 switch_to_thread (ptid_t ptid
)
1349 thread_info
*thr
= find_thread_ptid (ptid
);
1350 switch_to_thread (thr
);
1354 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1356 struct frame_info
*frame
= NULL
;
1359 /* This means there was no selected frame. */
1360 if (frame_level
== -1)
1362 select_frame (NULL
);
1366 gdb_assert (frame_level
>= 0);
1368 /* Restore by level first, check if the frame id is the same as
1369 expected. If that fails, try restoring by frame id. If that
1370 fails, nothing to do, just warn the user. */
1372 count
= frame_level
;
1373 frame
= find_relative_frame (get_current_frame (), &count
);
1376 /* The frame ids must match - either both valid or both outer_frame_id.
1377 The latter case is not failsafe, but since it's highly unlikely
1378 the search by level finds the wrong frame, it's 99.9(9)% of
1379 the time (for all practical purposes) safe. */
1380 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1382 /* Cool, all is fine. */
1383 select_frame (frame
);
1387 frame
= frame_find_by_id (a_frame_id
);
1390 /* Cool, refound it. */
1391 select_frame (frame
);
1395 /* Nothing else to do, the frame layout really changed. Select the
1396 innermost stack frame. */
1397 select_frame (get_current_frame ());
1399 /* Warn the user. */
1400 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1402 warning (_("Couldn't restore frame #%d in "
1403 "current thread. Bottom (innermost) frame selected:"),
1405 /* For MI, we should probably have a notification about
1406 current frame change. But this error is not very
1407 likely, so don't bother for now. */
1408 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1413 scoped_restore_current_thread::restore ()
1415 /* If an entry of thread_info was previously selected, it won't be
1416 deleted because we've increased its refcount. The thread represented
1417 by this thread_info entry may have already exited (due to normal exit,
1418 detach, etc), so the thread_info.state is THREAD_EXITED. */
1419 if (m_thread
!= NULL
1420 /* If the previously selected thread belonged to a process that has
1421 in the mean time exited (or killed, detached, etc.), then don't revert
1422 back to it, but instead simply drop back to no thread selected. */
1424 switch_to_thread (m_thread
);
1427 switch_to_no_thread ();
1428 set_current_inferior (m_inf
);
1431 /* The running state of the originally selected thread may have
1432 changed, so we have to recheck it here. */
1433 if (inferior_ptid
!= null_ptid
1435 && m_thread
->state
== THREAD_STOPPED
1436 && target_has_registers
1438 && target_has_memory
)
1439 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1442 scoped_restore_current_thread::~scoped_restore_current_thread ()
1444 if (!m_dont_restore
)
1450 catch (const gdb_exception
&ex
)
1452 /* We're in a dtor, there's really nothing else we can do
1453 but swallow the exception. */
1457 if (m_thread
!= NULL
)
1458 m_thread
->decref ();
1462 scoped_restore_current_thread::scoped_restore_current_thread ()
1465 m_inf
= current_inferior ();
1467 if (inferior_ptid
!= null_ptid
)
1469 thread_info
*tp
= inferior_thread ();
1470 struct frame_info
*frame
;
1472 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1474 && target_has_registers
1476 && target_has_memory
)
1478 /* When processing internal events, there might not be a
1479 selected frame. If we naively call get_selected_frame
1480 here, then we can end up reading debuginfo for the
1481 current frame, but we don't generally need the debuginfo
1483 frame
= get_selected_frame_if_set ();
1488 m_selected_frame_id
= get_frame_id (frame
);
1489 m_selected_frame_level
= frame_relative_level (frame
);
1498 /* See gdbthread.h. */
1501 show_thread_that_caused_stop (void)
1503 return highest_thread_num
> 1;
1506 /* See gdbthread.h. */
1509 show_inferior_qualified_tids (void)
1511 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1514 /* See gdbthread.h. */
1517 print_thread_id (struct thread_info
*thr
)
1519 char *s
= get_print_cell ();
1521 if (show_inferior_qualified_tids ())
1522 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1524 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1528 /* Sort an array of struct thread_info pointers by thread ID (first by
1529 inferior number, and then by per-inferior thread number). Sorts in
1533 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1535 if (a
->inf
->num
!= b
->inf
->num
)
1536 return a
->inf
->num
< b
->inf
->num
;
1538 return (a
->per_inf_num
< b
->per_inf_num
);
1541 /* Sort an array of struct thread_info pointers by thread ID (first by
1542 inferior number, and then by per-inferior thread number). Sorts in
1543 descending order. */
1546 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1548 if (a
->inf
->num
!= b
->inf
->num
)
1549 return a
->inf
->num
> b
->inf
->num
;
1551 return (a
->per_inf_num
> b
->per_inf_num
);
1554 /* Switch to thread THR and execute CMD.
1555 FLAGS.QUIET controls the printing of the thread information.
1556 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1559 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1560 const qcs_flags
&flags
)
1562 switch_to_thread (thr
);
1565 std::string cmd_result
= execute_command_to_string
1566 (cmd
, from_tty
, gdb_stdout
->term_out ());
1567 if (!flags
.silent
|| cmd_result
.length () > 0)
1570 printf_filtered (_("\nThread %s (%s):\n"),
1571 print_thread_id (thr
),
1572 target_pid_to_str (inferior_ptid
).c_str ());
1573 printf_filtered ("%s", cmd_result
.c_str ());
1576 catch (const gdb_exception_error
&ex
)
1581 printf_filtered (_("\nThread %s (%s):\n"),
1582 print_thread_id (thr
),
1583 target_pid_to_str (inferior_ptid
).c_str ());
1585 printf_filtered ("%s\n", ex
.what ());
1592 /* Option definition of "thread apply"'s "-ascending" option. */
1594 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1597 Call COMMAND for all threads in ascending order.\n\
1598 The default is descending order."),
1601 /* The qcs command line flags for the "thread apply" commands. Keep
1602 this in sync with the "frame apply" commands. */
1604 using qcs_flag_option_def
1605 = gdb::option::flag_option_def
<qcs_flags
>;
1607 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1608 qcs_flag_option_def
{
1609 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1610 N_("Disables printing the thread information."),
1613 qcs_flag_option_def
{
1614 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1615 N_("Print any error raised by COMMAND and continue."),
1618 qcs_flag_option_def
{
1619 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1620 N_("Silently ignore any errors or empty output produced by COMMAND."),
1624 /* Create an option_def_group for the "thread apply all" options, with
1625 ASCENDING and FLAGS as context. */
1627 static inline std::array
<gdb::option::option_def_group
, 2>
1628 make_thread_apply_all_options_def_group (bool *ascending
,
1632 { {ascending_option_def
.def ()}, ascending
},
1633 { {thr_qcs_flags_option_defs
}, flags
},
1637 /* Create an option_def_group for the "thread apply" options, with
1638 FLAGS as context. */
1640 static inline gdb::option::option_def_group
1641 make_thread_apply_options_def_group (qcs_flags
*flags
)
1643 return {{thr_qcs_flags_option_defs
}, flags
};
1646 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1647 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1648 of two numbers separated by a hyphen. Examples:
1650 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1651 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1652 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1655 thread_apply_all_command (const char *cmd
, int from_tty
)
1657 bool ascending
= false;
1660 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1662 gdb::option::process_options
1663 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1665 validate_flags_qcs ("thread apply all", &flags
);
1667 if (cmd
== NULL
|| *cmd
== '\000')
1668 error (_("Please specify a command at the end of 'thread apply all'"));
1670 update_thread_list ();
1672 int tc
= live_threads_count ();
1675 /* Save a copy of the thread list and increment each thread's
1676 refcount while executing the command in the context of each
1677 thread, in case the command is one that wipes threads. E.g.,
1678 detach, kill, disconnect, etc., or even normally continuing
1679 over an inferior or thread exit. */
1680 std::vector
<thread_info
*> thr_list_cpy
;
1681 thr_list_cpy
.reserve (tc
);
1683 for (thread_info
*tp
: all_non_exited_threads ())
1684 thr_list_cpy
.push_back (tp
);
1685 gdb_assert (thr_list_cpy
.size () == tc
);
1687 /* Increment the refcounts, and restore them back on scope
1689 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1691 auto *sorter
= (ascending
1692 ? tp_array_compar_ascending
1693 : tp_array_compar_descending
);
1694 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1696 scoped_restore_current_thread restore_thread
;
1698 for (thread_info
*thr
: thr_list_cpy
)
1699 if (switch_to_thread_if_alive (thr
))
1700 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1704 /* Completer for "thread apply [ID list]". */
1707 thread_apply_command_completer (cmd_list_element
*ignore
,
1708 completion_tracker
&tracker
,
1709 const char *text
, const char * /*word*/)
1711 /* Don't leave this to complete_options because there's an early
1713 tracker
.set_use_custom_word_point (true);
1715 tid_range_parser parser
;
1716 parser
.init (text
, current_inferior ()->num
);
1720 while (!parser
.finished ())
1722 int inf_num
, thr_start
, thr_end
;
1724 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1727 if (parser
.in_star_range () || parser
.in_thread_range ())
1728 parser
.skip_range ();
1731 catch (const gdb_exception_error
&ex
)
1733 /* get_tid_range throws if it parses a negative number, for
1734 example. But a seemingly negative number may be the start of
1735 an option instead. */
1738 const char *cmd
= parser
.cur_tok ();
1742 /* No thread ID list yet. */
1746 /* Check if we're past a valid thread ID list already. */
1747 if (parser
.finished ()
1748 && cmd
> text
&& !isspace (cmd
[-1]))
1751 /* We're past the thread ID list, advance word point. */
1752 tracker
.advance_custom_word_point_by (cmd
- text
);
1755 const auto group
= make_thread_apply_options_def_group (nullptr);
1756 if (gdb::option::complete_options
1757 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1760 complete_nested_command_line (tracker
, text
);
1763 /* Completer for "thread apply all". */
1766 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1767 completion_tracker
&tracker
,
1768 const char *text
, const char *word
)
1770 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1772 if (gdb::option::complete_options
1773 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1776 complete_nested_command_line (tracker
, text
);
1779 /* Implementation of the "thread apply" command. */
1782 thread_apply_command (const char *tidlist
, int from_tty
)
1785 const char *cmd
= NULL
;
1786 tid_range_parser parser
;
1788 if (tidlist
== NULL
|| *tidlist
== '\000')
1789 error (_("Please specify a thread ID list"));
1791 parser
.init (tidlist
, current_inferior ()->num
);
1792 while (!parser
.finished ())
1794 int inf_num
, thr_start
, thr_end
;
1796 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1800 cmd
= parser
.cur_tok ();
1802 auto group
= make_thread_apply_options_def_group (&flags
);
1803 gdb::option::process_options
1804 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1806 validate_flags_qcs ("thread apply", &flags
);
1809 error (_("Please specify a command following the thread ID list"));
1811 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1812 invalid_thread_id_error (cmd
);
1814 scoped_restore_current_thread restore_thread
;
1816 parser
.init (tidlist
, current_inferior ()->num
);
1817 while (!parser
.finished ())
1819 struct thread_info
*tp
= NULL
;
1820 struct inferior
*inf
;
1821 int inf_num
, thr_num
;
1823 parser
.get_tid (&inf_num
, &thr_num
);
1824 inf
= find_inferior_id (inf_num
);
1826 tp
= find_thread_id (inf
, thr_num
);
1828 if (parser
.in_star_range ())
1832 warning (_("Unknown inferior %d"), inf_num
);
1833 parser
.skip_range ();
1837 /* No use looking for threads past the highest thread number
1838 the inferior ever had. */
1839 if (thr_num
>= inf
->highest_thread_num
)
1840 parser
.skip_range ();
1842 /* Be quiet about unknown threads numbers. */
1849 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1850 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1852 warning (_("Unknown thread %d"), thr_num
);
1856 if (!switch_to_thread_if_alive (tp
))
1858 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1862 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1867 /* Implementation of the "taas" command. */
1870 taas_command (const char *cmd
, int from_tty
)
1872 if (cmd
== NULL
|| *cmd
== '\0')
1873 error (_("Please specify a command to apply on all threads"));
1874 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1875 execute_command (expanded
.c_str (), from_tty
);
1878 /* Implementation of the "tfaas" command. */
1881 tfaas_command (const char *cmd
, int from_tty
)
1883 if (cmd
== NULL
|| *cmd
== '\0')
1884 error (_("Please specify a command to apply on all frames of all threads"));
1885 std::string expanded
1886 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1887 execute_command (expanded
.c_str (), from_tty
);
1890 /* Switch to the specified thread, or print the current thread. */
1893 thread_command (const char *tidstr
, int from_tty
)
1897 if (inferior_ptid
== null_ptid
)
1898 error (_("No thread selected"));
1900 if (target_has_stack
)
1902 struct thread_info
*tp
= inferior_thread ();
1904 if (tp
->state
== THREAD_EXITED
)
1905 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1906 print_thread_id (tp
),
1907 target_pid_to_str (inferior_ptid
).c_str ());
1909 printf_filtered (_("[Current thread is %s (%s)]\n"),
1910 print_thread_id (tp
),
1911 target_pid_to_str (inferior_ptid
).c_str ());
1914 error (_("No stack."));
1918 ptid_t previous_ptid
= inferior_ptid
;
1920 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1922 /* Print if the thread has not changed, otherwise an event will
1924 if (inferior_ptid
== previous_ptid
)
1926 print_selected_thread_frame (current_uiout
,
1927 USER_SELECTED_THREAD
1928 | USER_SELECTED_FRAME
);
1932 gdb::observers::user_selected_context_changed
.notify
1933 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1938 /* Implementation of `thread name'. */
1941 thread_name_command (const char *arg
, int from_tty
)
1943 struct thread_info
*info
;
1945 if (inferior_ptid
== null_ptid
)
1946 error (_("No thread selected"));
1948 arg
= skip_spaces (arg
);
1950 info
= inferior_thread ();
1952 info
->name
= arg
? xstrdup (arg
) : NULL
;
1955 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1958 thread_find_command (const char *arg
, int from_tty
)
1961 unsigned long match
= 0;
1963 if (arg
== NULL
|| *arg
== '\0')
1964 error (_("Command requires an argument."));
1966 tmp
= re_comp (arg
);
1968 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1970 update_thread_list ();
1971 for (thread_info
*tp
: all_threads ())
1973 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1975 printf_filtered (_("Thread %s has name '%s'\n"),
1976 print_thread_id (tp
), tp
->name
);
1980 tmp
= target_thread_name (tp
);
1981 if (tmp
!= NULL
&& re_exec (tmp
))
1983 printf_filtered (_("Thread %s has target name '%s'\n"),
1984 print_thread_id (tp
), tmp
);
1988 std::string name
= target_pid_to_str (tp
->ptid
);
1989 if (!name
.empty () && re_exec (name
.c_str ()))
1991 printf_filtered (_("Thread %s has target id '%s'\n"),
1992 print_thread_id (tp
), name
.c_str ());
1996 tmp
= target_extra_thread_info (tp
);
1997 if (tmp
!= NULL
&& re_exec (tmp
))
1999 printf_filtered (_("Thread %s has extra info '%s'\n"),
2000 print_thread_id (tp
), tmp
);
2005 printf_filtered (_("No threads match '%s'\n"), arg
);
2008 /* Print notices when new threads are attached and detached. */
2009 bool print_thread_events
= true;
2011 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2012 struct cmd_list_element
*c
, const char *value
)
2014 fprintf_filtered (file
,
2015 _("Printing of thread events is %s.\n"),
2019 /* See gdbthread.h. */
2022 thread_select (const char *tidstr
, thread_info
*tp
)
2024 if (!switch_to_thread_if_alive (tp
))
2025 error (_("Thread ID %s has terminated."), tidstr
);
2027 annotate_thread_changed ();
2029 /* Since the current thread may have changed, see if there is any
2030 exited thread we can now delete. */
2034 /* Print thread and frame switch command response. */
2037 print_selected_thread_frame (struct ui_out
*uiout
,
2038 user_selected_what selection
)
2040 struct thread_info
*tp
= inferior_thread ();
2042 if (selection
& USER_SELECTED_THREAD
)
2044 if (uiout
->is_mi_like_p ())
2046 uiout
->field_signed ("new-thread-id",
2047 inferior_thread ()->global_num
);
2051 uiout
->text ("[Switching to thread ");
2052 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2054 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2059 if (tp
->state
== THREAD_RUNNING
)
2061 if (selection
& USER_SELECTED_THREAD
)
2062 uiout
->text ("(running)\n");
2064 else if (selection
& USER_SELECTED_FRAME
)
2066 if (selection
& USER_SELECTED_THREAD
)
2069 if (has_stack_frames ())
2070 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2075 /* Update the 'threads_executing' global based on the threads we know
2079 update_threads_executing (void)
2081 threads_executing
= 0;
2082 for (thread_info
*tp
: all_non_exited_threads ())
2086 threads_executing
= 1;
2093 update_thread_list (void)
2095 target_update_thread_list ();
2096 update_threads_executing ();
2099 /* Return a new value for the selected thread's id. Return a value of
2100 0 if no thread is selected. If GLOBAL is true, return the thread's
2101 global number. Otherwise return the per-inferior number. */
2103 static struct value
*
2104 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2108 if (inferior_ptid
== null_ptid
)
2112 thread_info
*tp
= inferior_thread ();
2114 int_val
= tp
->global_num
;
2116 int_val
= tp
->per_inf_num
;
2119 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2122 /* Return a new value for the selected thread's per-inferior thread
2123 number. Return a value of 0 if no thread is selected, or no
2126 static struct value
*
2127 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2128 struct internalvar
*var
,
2131 return thread_num_make_value_helper (gdbarch
, 0);
2134 /* Return a new value for the selected thread's global id. Return a
2135 value of 0 if no thread is selected, or no threads exist. */
2137 static struct value
*
2138 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2141 return thread_num_make_value_helper (gdbarch
, 1);
2144 /* Commands with a prefix of `thread'. */
2145 struct cmd_list_element
*thread_cmd_list
= NULL
;
2147 /* Implementation of `thread' variable. */
2149 static const struct internalvar_funcs thread_funcs
=
2151 thread_id_per_inf_num_make_value
,
2156 /* Implementation of `gthread' variable. */
2158 static const struct internalvar_funcs gthread_funcs
=
2160 global_thread_id_make_value
,
2166 _initialize_thread (void)
2168 static struct cmd_list_element
*thread_apply_list
= NULL
;
2169 cmd_list_element
*c
;
2171 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2173 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2175 static std::string info_threads_help
2176 = gdb::option::build_help (_("\
2177 Display currently known threads.\n\
2178 Usage: info threads [OPTION]... [ID]...\n\
2182 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2183 Otherwise, all threads are displayed."),
2186 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2187 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2189 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2190 Use this command to switch between threads.\n\
2191 The new thread ID must be currently known."),
2192 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2194 #define THREAD_APPLY_OPTION_HELP "\
2195 Prints per-inferior thread number and target system's thread id\n\
2196 followed by COMMAND output.\n\
2198 By default, an error raised during the execution of COMMAND\n\
2199 aborts \"thread apply\".\n\
2204 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2206 static std::string thread_apply_help
= gdb::option::build_help (_("\
2207 Apply a command to a list of threads.\n\
2208 Usage: thread apply ID... [OPTION]... COMMAND\n\
2209 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2210 THREAD_APPLY_OPTION_HELP
),
2213 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2214 thread_apply_help
.c_str (),
2215 &thread_apply_list
, "thread apply ", 1,
2217 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2219 const auto thread_apply_all_opts
2220 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2222 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2223 Apply a command to all threads.\n\
2225 Usage: thread apply all [OPTION]... COMMAND\n"
2226 THREAD_APPLY_OPTION_HELP
),
2227 thread_apply_all_opts
);
2229 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2230 thread_apply_all_help
.c_str (),
2231 &thread_apply_list
);
2232 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2234 c
= add_com ("taas", class_run
, taas_command
, _("\
2235 Apply a command to all threads (ignoring errors and empty output).\n\
2236 Usage: taas [OPTION]... COMMAND\n\
2237 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2238 See \"help thread apply all\" for available options."));
2239 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2241 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2242 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2243 Usage: tfaas [OPTION]... COMMAND\n\
2244 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2245 See \"help frame apply all\" for available options."));
2246 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2248 add_cmd ("name", class_run
, thread_name_command
,
2249 _("Set the current thread's name.\n\
2250 Usage: thread name [NAME]\n\
2251 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2253 add_cmd ("find", class_run
, thread_find_command
, _("\
2254 Find threads that match a regular expression.\n\
2255 Usage: thread find REGEXP\n\
2256 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2259 add_com_alias ("t", "thread", class_run
, 1);
2261 add_setshow_boolean_cmd ("thread-events", no_class
,
2262 &print_thread_events
, _("\
2263 Set printing of thread events (such as thread start and exit)."), _("\
2264 Show printing of thread events (such as thread start and exit)."), NULL
,
2266 show_print_thread_events
,
2267 &setprintlist
, &showprintlist
);
2269 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2270 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);