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 /* RAII type used to increase / decrease the refcount of each thread
59 in a given list of threads. */
61 class scoped_inc_dec_ref
64 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
67 for (thread_info
*thr
: m_thrds
)
71 ~scoped_inc_dec_ref ()
73 for (thread_info
*thr
: m_thrds
)
78 const std::vector
<thread_info
*> &m_thrds
;
83 inferior_thread (void)
85 struct thread_info
*tp
= find_thread_ptid (current_inferior (), inferior_ptid
);
90 /* Delete the breakpoint pointed at by BP_P, if there's one. */
93 delete_thread_breakpoint (struct breakpoint
**bp_p
)
97 delete_breakpoint (*bp_p
);
103 delete_step_resume_breakpoint (struct thread_info
*tp
)
106 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
110 delete_exception_resume_breakpoint (struct thread_info
*tp
)
113 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
116 /* See gdbthread.h. */
119 delete_single_step_breakpoints (struct thread_info
*tp
)
122 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
125 /* Delete the breakpoint pointed at by BP_P at the next stop, if
129 delete_at_next_stop (struct breakpoint
**bp
)
133 (*bp
)->disposition
= disp_del_at_next_stop
;
138 /* See gdbthread.h. */
141 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
143 return tp
->control
.single_step_breakpoints
!= NULL
;
146 /* See gdbthread.h. */
149 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
150 const address_space
*aspace
,
153 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
155 return (ss_bps
!= NULL
156 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
159 /* See gdbthread.h. */
162 thread_cancel_execution_command (struct thread_info
*thr
)
164 if (thr
->thread_fsm
!= NULL
)
166 thr
->thread_fsm
->clean_up (thr
);
167 delete thr
->thread_fsm
;
168 thr
->thread_fsm
= NULL
;
173 clear_thread_inferior_resources (struct thread_info
*tp
)
175 /* NOTE: this will take care of any left-over step_resume breakpoints,
176 but not any user-specified thread-specific breakpoints. We can not
177 delete the breakpoint straight-off, because the inferior might not
178 be stopped at the moment. */
179 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
180 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
181 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
183 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
185 bpstat_clear (&tp
->control
.stop_bpstat
);
187 btrace_teardown (tp
);
189 thread_cancel_execution_command (tp
);
191 clear_inline_frame_state (tp
);
194 /* Set the TP's state as exited. */
197 set_thread_exited (thread_info
*tp
, int silent
)
199 /* Dead threads don't need to step-over. Remove from queue. */
200 if (tp
->step_over_next
!= NULL
)
201 thread_step_over_chain_remove (tp
);
203 if (tp
->state
!= THREAD_EXITED
)
205 gdb::observers::thread_exit
.notify (tp
, silent
);
207 /* Tag it as exited. */
208 tp
->state
= THREAD_EXITED
;
210 /* Clear breakpoints, etc. associated with this thread. */
211 clear_thread_inferior_resources (tp
);
216 init_thread_list (void)
218 highest_thread_num
= 0;
220 for (thread_info
*tp
: all_threads_safe ())
222 inferior
*inf
= tp
->inf
;
224 if (tp
->deletable ())
227 set_thread_exited (tp
, 1);
229 inf
->thread_list
= NULL
;
233 /* Allocate a new thread of inferior INF with target id PTID and add
234 it to the thread list. */
236 static struct thread_info
*
237 new_thread (struct inferior
*inf
, ptid_t ptid
)
239 thread_info
*tp
= new thread_info (inf
, ptid
);
241 if (inf
->thread_list
== NULL
)
242 inf
->thread_list
= tp
;
245 struct thread_info
*last
;
247 for (last
= inf
->thread_list
; last
->next
!= NULL
; last
= last
->next
)
256 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
260 thread_info
*tp
= find_thread_ptid (targ
, ptid
);
262 /* Found an old thread with the same id. It has to be dead,
263 otherwise we wouldn't be adding a new thread with the same id.
264 The OS is reusing this id --- delete it, and recreate a new
267 /* In addition to deleting the thread, if this is the current
268 thread, then we need to take care that delete_thread doesn't
269 really delete the thread if it is inferior_ptid. Create a
270 new template thread in the list with an invalid ptid, switch
271 to it, delete the original thread, reset the new thread's
272 ptid, and switch to it. */
274 if (inferior_ptid
== ptid
)
276 thread_info
*new_thr
= new_thread (tp
->inf
, null_ptid
);
278 /* Make switch_to_thread not read from the thread. */
279 new_thr
->state
= THREAD_EXITED
;
280 switch_to_no_thread ();
282 /* Now we can delete it. */
285 /* Now reset its ptid, and reswitch inferior_ptid to it. */
286 new_thr
->ptid
= ptid
;
287 new_thr
->state
= THREAD_STOPPED
;
288 switch_to_thread (new_thr
);
290 gdb::observers::new_thread
.notify (new_thr
);
298 /* Just go ahead and delete it. */
302 inf
= find_inferior_ptid (targ
, ptid
);
304 tp
= new_thread (inf
, ptid
);
305 gdb::observers::new_thread
.notify (tp
);
311 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
312 private_thread_info
*priv
)
314 thread_info
*result
= add_thread_silent (targ
, ptid
);
316 result
->priv
.reset (priv
);
318 if (print_thread_events
)
319 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
321 annotate_new_thread ();
326 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
328 return add_thread_with_info (targ
, ptid
, NULL
);
331 private_thread_info::~private_thread_info () = default;
333 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
334 : ptid (ptid_
), inf (inf_
)
336 gdb_assert (inf_
!= NULL
);
338 this->global_num
= ++highest_thread_num
;
339 this->per_inf_num
= ++inf_
->highest_thread_num
;
341 /* Nothing to follow yet. */
342 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
343 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
344 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
347 thread_info::~thread_info ()
352 /* Returns true if THR is the current thread. */
355 is_current_thread (const thread_info
*thr
)
357 return thr
->inf
== current_inferior () && thr
->ptid
== inferior_ptid
;
360 /* See gdbthread.h. */
363 thread_info::deletable () const
365 /* If this is the current thread, or there's code out there that
366 relies on it existing (refcount > 0) we can't delete yet. */
367 return refcount () == 0 && !is_current_thread (this);
370 /* Add TP to the end of the step-over chain LIST_P. */
373 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
375 gdb_assert (tp
->step_over_next
== NULL
);
376 gdb_assert (tp
->step_over_prev
== NULL
);
381 tp
->step_over_prev
= tp
->step_over_next
= tp
;
385 struct thread_info
*head
= *list_p
;
386 struct thread_info
*tail
= head
->step_over_prev
;
388 tp
->step_over_prev
= tail
;
389 tp
->step_over_next
= head
;
390 head
->step_over_prev
= tp
;
391 tail
->step_over_next
= tp
;
395 /* Remove TP from step-over chain LIST_P. */
398 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
400 gdb_assert (tp
->step_over_next
!= NULL
);
401 gdb_assert (tp
->step_over_prev
!= NULL
);
405 if (tp
== tp
->step_over_next
)
408 *list_p
= tp
->step_over_next
;
411 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
412 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
413 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
416 /* See gdbthread.h. */
419 thread_step_over_chain_next (struct thread_info
*tp
)
421 struct thread_info
*next
= tp
->step_over_next
;
423 return (next
== step_over_queue_head
? NULL
: next
);
426 /* See gdbthread.h. */
429 thread_is_in_step_over_chain (struct thread_info
*tp
)
431 return (tp
->step_over_next
!= NULL
);
434 /* See gdbthread.h. */
437 thread_step_over_chain_enqueue (struct thread_info
*tp
)
439 step_over_chain_enqueue (&step_over_queue_head
, tp
);
442 /* See gdbthread.h. */
445 thread_step_over_chain_remove (struct thread_info
*tp
)
447 step_over_chain_remove (&step_over_queue_head
, tp
);
450 /* Delete the thread referenced by THR. If SILENT, don't notify
451 the observer of this exit.
453 THR must not be NULL or a failed assertion will be raised. */
456 delete_thread_1 (thread_info
*thr
, bool silent
)
458 gdb_assert (thr
!= nullptr);
460 struct thread_info
*tp
, *tpprev
= NULL
;
462 for (tp
= thr
->inf
->thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
469 set_thread_exited (tp
, silent
);
471 if (!tp
->deletable ())
473 /* Will be really deleted some other time. */
478 tpprev
->next
= tp
->next
;
480 tp
->inf
->thread_list
= tp
->next
;
485 /* Delete thread THREAD and notify of thread exit. If this is the
486 current thread, don't actually delete it, but tag it as exited and
487 do the notification. If this is the user selected thread, clear
491 delete_thread (thread_info
*thread
)
493 delete_thread_1 (thread
, false /* not silent */);
497 delete_thread_silent (thread_info
*thread
)
499 delete_thread_1 (thread
, true /* silent */);
503 find_thread_global_id (int global_id
)
505 for (thread_info
*tp
: all_threads ())
506 if (tp
->global_num
== global_id
)
512 static struct thread_info
*
513 find_thread_id (struct inferior
*inf
, int thr_num
)
515 for (thread_info
*tp
: inf
->threads ())
516 if (tp
->per_inf_num
== thr_num
)
522 /* See gdbthread.h. */
525 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
527 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
530 return find_thread_ptid (inf
, ptid
);
533 /* See gdbthread.h. */
536 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
538 for (thread_info
*tp
: inf
->threads ())
539 if (tp
->ptid
== ptid
)
545 /* See gdbthread.h. */
548 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
549 struct inferior
*inf
)
551 return target_thread_handle_to_thread_info (handle
.data (),
557 * Thread iterator function.
559 * Calls a callback function once for each thread, so long as
560 * the callback function returns false. If the callback function
561 * returns true, the iteration will end and the current thread
562 * will be returned. This can be useful for implementing a
563 * search for a thread with arbitrary attributes, or for applying
564 * some operation to every thread.
566 * FIXME: some of the existing functionality, such as
567 * "Thread apply all", might be rewritten using this functionality.
571 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
574 for (thread_info
*tp
: all_threads_safe ())
575 if ((*callback
) (tp
, data
))
581 /* See gdbthread.h. */
586 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
592 thread_count (process_stratum_target
*proc_target
)
594 auto rng
= all_threads (proc_target
);
595 return std::distance (rng
.begin (), rng
.end ());
598 /* Return the number of non-exited threads in the thread list. */
601 live_threads_count (void)
603 auto rng
= all_non_exited_threads ();
604 return std::distance (rng
.begin (), rng
.end ());
608 valid_global_thread_id (int global_id
)
610 for (thread_info
*tp
: all_threads ())
611 if (tp
->global_num
== global_id
)
618 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
620 return find_thread_ptid (targ
, ptid
) != nullptr;
623 /* Finds the first thread of the inferior. */
626 first_thread_of_inferior (inferior
*inf
)
628 return inf
->thread_list
;
632 any_thread_of_inferior (inferior
*inf
)
634 gdb_assert (inf
->pid
!= 0);
636 /* Prefer the current thread. */
637 if (inf
== current_inferior ())
638 return inferior_thread ();
640 for (thread_info
*tp
: inf
->non_exited_threads ())
647 any_live_thread_of_inferior (inferior
*inf
)
649 struct thread_info
*curr_tp
= NULL
;
650 struct thread_info
*tp_executing
= NULL
;
652 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
654 /* Prefer the current thread if it's not executing. */
655 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
657 /* If the current thread is dead, forget it. If it's not
658 executing, use it. Otherwise, still choose it (below), but
659 only if no other non-executing thread is found. */
660 curr_tp
= inferior_thread ();
661 if (curr_tp
->state
== THREAD_EXITED
)
663 else if (!curr_tp
->executing
)
667 for (thread_info
*tp
: inf
->non_exited_threads ())
675 /* If both the current thread and all live threads are executing,
676 prefer the current thread. */
680 /* Otherwise, just return an executing thread, if any. */
684 /* Return true if TP is an active thread. */
686 thread_alive (thread_info
*tp
)
688 if (tp
->state
== THREAD_EXITED
)
691 /* Ensure we're looking at the right target stack. */
692 gdb_assert (tp
->inf
== current_inferior ());
694 return target_thread_alive (tp
->ptid
);
697 /* Switch to thread TP if it is alive. Returns true if successfully
698 switched, false otherwise. */
701 switch_to_thread_if_alive (thread_info
*thr
)
703 scoped_restore_current_thread restore_thread
;
705 /* Switch inferior first, so that we're looking at the right target
707 switch_to_inferior_no_thread (thr
->inf
);
709 if (thread_alive (thr
))
711 switch_to_thread (thr
);
712 restore_thread
.dont_restore ();
719 /* See gdbthreads.h. */
724 scoped_restore_current_thread restore_thread
;
726 for (thread_info
*tp
: all_threads_safe ())
728 switch_to_inferior_no_thread (tp
->inf
);
730 if (!thread_alive (tp
))
735 /* See gdbthreads.h. */
738 delete_exited_threads (void)
740 for (thread_info
*tp
: all_threads_safe ())
741 if (tp
->state
== THREAD_EXITED
)
745 /* Return true value if stack temporaries are enabled for the thread
749 thread_stack_temporaries_enabled_p (thread_info
*tp
)
754 return tp
->stack_temporaries_enabled
;
757 /* Push V on to the stack temporaries of the thread with id PTID. */
760 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
762 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
763 tp
->stack_temporaries
.push_back (v
);
766 /* Return true if VAL is among the stack temporaries of the thread
767 TP. Return false otherwise. */
770 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
772 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
773 for (value
*v
: tp
->stack_temporaries
)
780 /* Return the last of the stack temporaries for thread with id PTID.
781 Return NULL if there are no stack temporaries for the thread. */
784 get_last_thread_stack_temporary (thread_info
*tp
)
786 struct value
*lastval
= NULL
;
788 gdb_assert (tp
!= NULL
);
789 if (!tp
->stack_temporaries
.empty ())
790 lastval
= tp
->stack_temporaries
.back ();
796 thread_change_ptid (process_stratum_target
*targ
,
797 ptid_t old_ptid
, ptid_t new_ptid
)
799 struct inferior
*inf
;
800 struct thread_info
*tp
;
802 /* It can happen that what we knew as the target inferior id
803 changes. E.g, target remote may only discover the remote process
804 pid after adding the inferior to GDB's list. */
805 inf
= find_inferior_ptid (targ
, old_ptid
);
806 inf
->pid
= new_ptid
.pid ();
808 tp
= find_thread_ptid (inf
, old_ptid
);
811 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
814 /* See gdbthread.h. */
817 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
819 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
820 tp
->resumed
= resumed
;
823 /* Helper for set_running, that marks one thread either running or
827 set_running_thread (struct thread_info
*tp
, bool running
)
829 bool started
= false;
831 if (running
&& tp
->state
== THREAD_STOPPED
)
833 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
837 /* If the thread is now marked stopped, remove it from
838 the step-over queue, so that we don't try to resume
839 it until the user wants it to. */
840 if (tp
->step_over_next
!= NULL
)
841 thread_step_over_chain_remove (tp
);
847 /* See gdbthread.h. */
850 thread_info::set_running (bool running
)
852 if (set_running_thread (this, running
))
853 gdb::observers::target_resumed
.notify (this->ptid
);
857 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
859 /* We try not to notify the observer if no thread has actually
860 changed the running state -- merely to reduce the number of
861 messages to the MI frontend. A frontend is supposed to handle
862 multiple *running notifications just fine. */
863 bool any_started
= false;
865 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
866 if (set_running_thread (tp
, running
))
870 gdb::observers::target_resumed
.notify (ptid
);
874 /* Helper for set_executing. Set's the thread's 'executing' field
875 from EXECUTING, and if EXECUTING is true also clears the thread's
879 set_executing_thread (thread_info
*thr
, bool executing
)
881 thr
->executing
= executing
;
883 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
887 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
889 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
890 set_executing_thread (tp
, executing
);
892 /* It only takes one running thread to spawn more threads. */
894 targ
->threads_executing
= true;
895 /* Only clear the flag if the caller is telling us everything is
897 else if (minus_one_ptid
== ptid
)
898 targ
->threads_executing
= false;
901 /* See gdbthread.h. */
904 threads_are_executing (process_stratum_target
*target
)
906 return target
->threads_executing
;
910 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
912 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
913 tp
->stop_requested
= stop
;
915 /* Call the stop requested observer so other components of GDB can
916 react to this request. */
918 gdb::observers::thread_stop_requested
.notify (ptid
);
922 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
924 bool any_started
= false;
926 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
927 if (set_running_thread (tp
, tp
->executing
))
931 gdb::observers::target_resumed
.notify (ptid
);
934 /* See gdbthread.h. */
937 validate_registers_access (void)
939 /* No selected thread, no registers. */
940 if (inferior_ptid
== null_ptid
)
941 error (_("No thread selected."));
943 thread_info
*tp
= inferior_thread ();
945 /* Don't try to read from a dead thread. */
946 if (tp
->state
== THREAD_EXITED
)
947 error (_("The current thread has terminated"));
949 /* ... or from a spinning thread. FIXME: This isn't actually fully
950 correct. It'll allow an user-requested access (e.g., "print $pc"
951 at the prompt) when a thread is not executing for some internal
952 reason, but is marked running from the user's perspective. E.g.,
953 the thread is waiting for its turn in the step-over queue. */
955 error (_("Selected thread is running."));
958 /* See gdbthread.h. */
961 can_access_registers_thread (thread_info
*thread
)
963 /* No thread, no registers. */
967 /* Don't try to read from a dead thread. */
968 if (thread
->state
== THREAD_EXITED
)
971 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
972 if (thread
->executing
)
979 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
981 return (pc
>= thread
->control
.step_range_start
982 && pc
< thread
->control
.step_range_end
);
985 /* Helper for print_thread_info. Returns true if THR should be
986 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
987 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
988 is true if REQUESTED_THREADS is list of global IDs, false if a list
989 of per-inferior thread ids. If PID is not -1, only print THR if it
990 is a thread from the process PID. Otherwise, threads from all
991 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
992 and PID is not -1, then the thread is printed if it belongs to the
993 specified process. Otherwise, an error is raised. */
996 should_print_thread (const char *requested_threads
, int default_inf_num
,
997 int global_ids
, int pid
, struct thread_info
*thr
)
999 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1004 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1006 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1007 thr
->inf
->num
, thr
->per_inf_num
);
1012 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1014 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1015 error (_("Requested thread not found in requested process"));
1019 if (thr
->state
== THREAD_EXITED
)
1025 /* Return the string to display in "info threads"'s "Target Id"
1029 thread_target_id_str (thread_info
*tp
)
1031 std::string target_id
= target_pid_to_str (tp
->ptid
);
1032 const char *extra_info
= target_extra_thread_info (tp
);
1033 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1035 if (extra_info
!= nullptr && name
!= nullptr)
1036 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1038 else if (extra_info
!= nullptr)
1039 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1040 else if (name
!= nullptr)
1041 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1046 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1047 whether REQUESTED_THREADS is a list of global or per-inferior
1051 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1052 int global_ids
, int pid
,
1053 int show_global_ids
)
1055 int default_inf_num
= current_inferior ()->num
;
1057 update_thread_list ();
1059 /* Whether we saw any thread. */
1060 bool any_thread
= false;
1061 /* Whether the current thread is exited. */
1062 bool current_exited
= false;
1064 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1065 ? inferior_thread () : NULL
);
1068 /* For backward compatibility, we make a list for MI. A table is
1069 preferable for the CLI, though, because it shows table
1071 gdb::optional
<ui_out_emit_list
> list_emitter
;
1072 gdb::optional
<ui_out_emit_table
> table_emitter
;
1074 /* We'll be switching threads temporarily below. */
1075 scoped_restore_current_thread restore_thread
;
1077 if (uiout
->is_mi_like_p ())
1078 list_emitter
.emplace (uiout
, "threads");
1082 /* The width of the "Target Id" column. Grown below to
1083 accommodate the largest entry. */
1084 size_t target_id_col_width
= 17;
1086 for (thread_info
*tp
: all_threads ())
1088 if (!should_print_thread (requested_threads
, default_inf_num
,
1089 global_ids
, pid
, tp
))
1092 if (!uiout
->is_mi_like_p ())
1094 /* Switch inferiors so we're looking at the right
1096 switch_to_inferior_no_thread (tp
->inf
);
1099 = std::max (target_id_col_width
,
1100 thread_target_id_str (tp
).size ());
1108 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1109 uiout
->message (_("No threads.\n"));
1111 uiout
->message (_("No threads match '%s'.\n"),
1116 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1117 n_threads
, "threads");
1119 uiout
->table_header (1, ui_left
, "current", "");
1120 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1121 if (show_global_ids
)
1122 uiout
->table_header (4, ui_left
, "id", "GId");
1123 uiout
->table_header (target_id_col_width
, ui_left
,
1124 "target-id", "Target Id");
1125 uiout
->table_header (1, ui_left
, "frame", "Frame");
1126 uiout
->table_body ();
1129 for (inferior
*inf
: all_inferiors ())
1130 for (thread_info
*tp
: inf
->threads ())
1135 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1136 current_exited
= true;
1138 if (!should_print_thread (requested_threads
, default_inf_num
,
1139 global_ids
, pid
, tp
))
1142 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1144 if (!uiout
->is_mi_like_p ())
1146 if (tp
== current_thread
)
1147 uiout
->field_string ("current", "*");
1149 uiout
->field_skip ("current");
1151 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1154 if (show_global_ids
|| uiout
->is_mi_like_p ())
1155 uiout
->field_signed ("id", tp
->global_num
);
1157 /* Switch to the thread (and inferior / target). */
1158 switch_to_thread (tp
);
1160 /* For the CLI, we stuff everything into the target-id field.
1161 This is a gross hack to make the output come out looking
1162 correct. The underlying problem here is that ui-out has no
1163 way to specify that a field's space allocation should be
1164 shared by several fields. For MI, we do the right thing
1167 if (uiout
->is_mi_like_p ())
1169 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1171 const char *extra_info
= target_extra_thread_info (tp
);
1172 if (extra_info
!= nullptr)
1173 uiout
->field_string ("details", extra_info
);
1175 const char *name
= (tp
->name
!= nullptr
1177 : target_thread_name (tp
));
1179 uiout
->field_string ("name", name
);
1183 uiout
->field_string ("target-id",
1184 thread_target_id_str (tp
).c_str ());
1187 if (tp
->state
== THREAD_RUNNING
)
1188 uiout
->text ("(running)\n");
1191 /* The switch above put us at the top of the stack (leaf
1193 print_stack_frame (get_selected_frame (NULL
),
1194 /* For MI output, print frame level. */
1195 uiout
->is_mi_like_p (),
1199 if (uiout
->is_mi_like_p ())
1201 const char *state
= "stopped";
1203 if (tp
->state
== THREAD_RUNNING
)
1205 uiout
->field_string ("state", state
);
1208 core
= target_core_of_thread (tp
->ptid
);
1209 if (uiout
->is_mi_like_p () && core
!= -1)
1210 uiout
->field_signed ("core", core
);
1213 /* This end scope restores the current thread and the frame
1214 selected before the "info threads" command, and it finishes the
1215 ui-out list or table. */
1218 if (pid
== -1 && requested_threads
== NULL
)
1220 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1221 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1223 if (inferior_ptid
!= null_ptid
&& current_exited
)
1224 uiout
->message ("\n\
1225 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1226 print_thread_id (inferior_thread ()));
1227 else if (any_thread
&& inferior_ptid
== null_ptid
)
1228 uiout
->message ("\n\
1229 No selected thread. See `help thread'.\n");
1233 /* See gdbthread.h. */
1236 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1239 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1242 /* The options for the "info threads" command. */
1244 struct info_threads_opts
1247 bool show_global_ids
= false;
1250 static const gdb::option::option_def info_threads_option_defs
[] = {
1252 gdb::option::flag_option_def
<info_threads_opts
> {
1254 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1255 N_("Show global thread IDs."),
1260 /* Create an option_def_group for the "info threads" options, with
1261 IT_OPTS as context. */
1263 static inline gdb::option::option_def_group
1264 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1266 return {{info_threads_option_defs
}, it_opts
};
1269 /* Implementation of the "info threads" command.
1271 Note: this has the drawback that it _really_ switches
1272 threads, which frees the frame cache. A no-side
1273 effects info-threads command would be nicer. */
1276 info_threads_command (const char *arg
, int from_tty
)
1278 info_threads_opts it_opts
;
1280 auto grp
= make_info_threads_options_def_group (&it_opts
);
1281 gdb::option::process_options
1282 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1284 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1287 /* Completer for the "info threads" command. */
1290 info_threads_command_completer (struct cmd_list_element
*ignore
,
1291 completion_tracker
&tracker
,
1292 const char *text
, const char *word_ignored
)
1294 const auto grp
= make_info_threads_options_def_group (nullptr);
1296 if (gdb::option::complete_options
1297 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1300 /* Convenience to let the user know what the option can accept. */
1303 gdb::option::complete_on_all_options (tracker
, grp
);
1304 /* Keep this "ID" in sync with what "help info threads"
1306 tracker
.add_completion (make_unique_xstrdup ("ID"));
1310 /* See gdbthread.h. */
1313 switch_to_thread_no_regs (struct thread_info
*thread
)
1315 struct inferior
*inf
= thread
->inf
;
1317 set_current_program_space (inf
->pspace
);
1318 set_current_inferior (inf
);
1320 inferior_ptid
= thread
->ptid
;
1323 /* See gdbthread.h. */
1326 switch_to_no_thread ()
1328 if (inferior_ptid
== null_ptid
)
1331 inferior_ptid
= null_ptid
;
1332 reinit_frame_cache ();
1335 /* See gdbthread.h. */
1338 switch_to_thread (thread_info
*thr
)
1340 gdb_assert (thr
!= NULL
);
1342 if (is_current_thread (thr
))
1345 switch_to_thread_no_regs (thr
);
1347 reinit_frame_cache ();
1350 /* See gdbsupport/common-gdbthread.h. */
1353 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1355 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1356 switch_to_thread (thr
);
1360 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1362 struct frame_info
*frame
= NULL
;
1365 /* This means there was no selected frame. */
1366 if (frame_level
== -1)
1368 select_frame (NULL
);
1372 gdb_assert (frame_level
>= 0);
1374 /* Restore by level first, check if the frame id is the same as
1375 expected. If that fails, try restoring by frame id. If that
1376 fails, nothing to do, just warn the user. */
1378 count
= frame_level
;
1379 frame
= find_relative_frame (get_current_frame (), &count
);
1382 /* The frame ids must match - either both valid or both outer_frame_id.
1383 The latter case is not failsafe, but since it's highly unlikely
1384 the search by level finds the wrong frame, it's 99.9(9)% of
1385 the time (for all practical purposes) safe. */
1386 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1388 /* Cool, all is fine. */
1389 select_frame (frame
);
1393 frame
= frame_find_by_id (a_frame_id
);
1396 /* Cool, refound it. */
1397 select_frame (frame
);
1401 /* Nothing else to do, the frame layout really changed. Select the
1402 innermost stack frame. */
1403 select_frame (get_current_frame ());
1405 /* Warn the user. */
1406 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1408 warning (_("Couldn't restore frame #%d in "
1409 "current thread. Bottom (innermost) frame selected:"),
1411 /* For MI, we should probably have a notification about
1412 current frame change. But this error is not very
1413 likely, so don't bother for now. */
1414 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1419 scoped_restore_current_thread::restore ()
1421 /* If an entry of thread_info was previously selected, it won't be
1422 deleted because we've increased its refcount. The thread represented
1423 by this thread_info entry may have already exited (due to normal exit,
1424 detach, etc), so the thread_info.state is THREAD_EXITED. */
1425 if (m_thread
!= NULL
1426 /* If the previously selected thread belonged to a process that has
1427 in the mean time exited (or killed, detached, etc.), then don't revert
1428 back to it, but instead simply drop back to no thread selected. */
1430 switch_to_thread (m_thread
);
1432 switch_to_inferior_no_thread (m_inf
);
1434 /* The running state of the originally selected thread may have
1435 changed, so we have to recheck it here. */
1436 if (inferior_ptid
!= null_ptid
1438 && m_thread
->state
== THREAD_STOPPED
1439 && target_has_registers
1441 && target_has_memory
)
1442 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1445 scoped_restore_current_thread::~scoped_restore_current_thread ()
1447 if (!m_dont_restore
)
1453 catch (const gdb_exception
&ex
)
1455 /* We're in a dtor, there's really nothing else we can do
1456 but swallow the exception. */
1460 if (m_thread
!= NULL
)
1461 m_thread
->decref ();
1465 scoped_restore_current_thread::scoped_restore_current_thread ()
1468 m_inf
= current_inferior ();
1470 if (inferior_ptid
!= null_ptid
)
1472 thread_info
*tp
= inferior_thread ();
1473 struct frame_info
*frame
;
1475 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1477 && target_has_registers
1479 && target_has_memory
)
1481 /* When processing internal events, there might not be a
1482 selected frame. If we naively call get_selected_frame
1483 here, then we can end up reading debuginfo for the
1484 current frame, but we don't generally need the debuginfo
1486 frame
= get_selected_frame_if_set ();
1491 m_selected_frame_id
= get_frame_id (frame
);
1492 m_selected_frame_level
= frame_relative_level (frame
);
1501 /* See gdbthread.h. */
1504 show_thread_that_caused_stop (void)
1506 return highest_thread_num
> 1;
1509 /* See gdbthread.h. */
1512 show_inferior_qualified_tids (void)
1514 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1517 /* See gdbthread.h. */
1520 print_thread_id (struct thread_info
*thr
)
1522 char *s
= get_print_cell ();
1524 if (show_inferior_qualified_tids ())
1525 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1527 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1531 /* Sort an array of struct thread_info pointers by thread ID (first by
1532 inferior number, and then by per-inferior thread number). Sorts in
1536 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1538 if (a
->inf
->num
!= b
->inf
->num
)
1539 return a
->inf
->num
< b
->inf
->num
;
1541 return (a
->per_inf_num
< b
->per_inf_num
);
1544 /* Sort an array of struct thread_info pointers by thread ID (first by
1545 inferior number, and then by per-inferior thread number). Sorts in
1546 descending order. */
1549 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1551 if (a
->inf
->num
!= b
->inf
->num
)
1552 return a
->inf
->num
> b
->inf
->num
;
1554 return (a
->per_inf_num
> b
->per_inf_num
);
1557 /* Switch to thread THR and execute CMD.
1558 FLAGS.QUIET controls the printing of the thread information.
1559 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1562 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1563 const qcs_flags
&flags
)
1565 switch_to_thread (thr
);
1568 std::string cmd_result
= execute_command_to_string
1569 (cmd
, from_tty
, gdb_stdout
->term_out ());
1570 if (!flags
.silent
|| cmd_result
.length () > 0)
1573 printf_filtered (_("\nThread %s (%s):\n"),
1574 print_thread_id (thr
),
1575 target_pid_to_str (inferior_ptid
).c_str ());
1576 printf_filtered ("%s", cmd_result
.c_str ());
1579 catch (const gdb_exception_error
&ex
)
1584 printf_filtered (_("\nThread %s (%s):\n"),
1585 print_thread_id (thr
),
1586 target_pid_to_str (inferior_ptid
).c_str ());
1588 printf_filtered ("%s\n", ex
.what ());
1595 /* Option definition of "thread apply"'s "-ascending" option. */
1597 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1600 Call COMMAND for all threads in ascending order.\n\
1601 The default is descending order."),
1604 /* The qcs command line flags for the "thread apply" commands. Keep
1605 this in sync with the "frame apply" commands. */
1607 using qcs_flag_option_def
1608 = gdb::option::flag_option_def
<qcs_flags
>;
1610 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1611 qcs_flag_option_def
{
1612 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1613 N_("Disables printing the thread information."),
1616 qcs_flag_option_def
{
1617 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1618 N_("Print any error raised by COMMAND and continue."),
1621 qcs_flag_option_def
{
1622 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1623 N_("Silently ignore any errors or empty output produced by COMMAND."),
1627 /* Create an option_def_group for the "thread apply all" options, with
1628 ASCENDING and FLAGS as context. */
1630 static inline std::array
<gdb::option::option_def_group
, 2>
1631 make_thread_apply_all_options_def_group (bool *ascending
,
1635 { {ascending_option_def
.def ()}, ascending
},
1636 { {thr_qcs_flags_option_defs
}, flags
},
1640 /* Create an option_def_group for the "thread apply" options, with
1641 FLAGS as context. */
1643 static inline gdb::option::option_def_group
1644 make_thread_apply_options_def_group (qcs_flags
*flags
)
1646 return {{thr_qcs_flags_option_defs
}, flags
};
1649 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1650 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1651 of two numbers separated by a hyphen. Examples:
1653 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1654 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1655 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1658 thread_apply_all_command (const char *cmd
, int from_tty
)
1660 bool ascending
= false;
1663 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1665 gdb::option::process_options
1666 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1668 validate_flags_qcs ("thread apply all", &flags
);
1670 if (cmd
== NULL
|| *cmd
== '\000')
1671 error (_("Please specify a command at the end of 'thread apply all'"));
1673 update_thread_list ();
1675 int tc
= live_threads_count ();
1678 /* Save a copy of the thread list and increment each thread's
1679 refcount while executing the command in the context of each
1680 thread, in case the command is one that wipes threads. E.g.,
1681 detach, kill, disconnect, etc., or even normally continuing
1682 over an inferior or thread exit. */
1683 std::vector
<thread_info
*> thr_list_cpy
;
1684 thr_list_cpy
.reserve (tc
);
1686 for (thread_info
*tp
: all_non_exited_threads ())
1687 thr_list_cpy
.push_back (tp
);
1688 gdb_assert (thr_list_cpy
.size () == tc
);
1690 /* Increment the refcounts, and restore them back on scope
1692 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1694 auto *sorter
= (ascending
1695 ? tp_array_compar_ascending
1696 : tp_array_compar_descending
);
1697 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1699 scoped_restore_current_thread restore_thread
;
1701 for (thread_info
*thr
: thr_list_cpy
)
1702 if (switch_to_thread_if_alive (thr
))
1703 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1707 /* Completer for "thread apply [ID list]". */
1710 thread_apply_command_completer (cmd_list_element
*ignore
,
1711 completion_tracker
&tracker
,
1712 const char *text
, const char * /*word*/)
1714 /* Don't leave this to complete_options because there's an early
1716 tracker
.set_use_custom_word_point (true);
1718 tid_range_parser parser
;
1719 parser
.init (text
, current_inferior ()->num
);
1723 while (!parser
.finished ())
1725 int inf_num
, thr_start
, thr_end
;
1727 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1730 if (parser
.in_star_range () || parser
.in_thread_range ())
1731 parser
.skip_range ();
1734 catch (const gdb_exception_error
&ex
)
1736 /* get_tid_range throws if it parses a negative number, for
1737 example. But a seemingly negative number may be the start of
1738 an option instead. */
1741 const char *cmd
= parser
.cur_tok ();
1745 /* No thread ID list yet. */
1749 /* Check if we're past a valid thread ID list already. */
1750 if (parser
.finished ()
1751 && cmd
> text
&& !isspace (cmd
[-1]))
1754 /* We're past the thread ID list, advance word point. */
1755 tracker
.advance_custom_word_point_by (cmd
- text
);
1758 const auto group
= make_thread_apply_options_def_group (nullptr);
1759 if (gdb::option::complete_options
1760 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1763 complete_nested_command_line (tracker
, text
);
1766 /* Completer for "thread apply all". */
1769 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1770 completion_tracker
&tracker
,
1771 const char *text
, const char *word
)
1773 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1775 if (gdb::option::complete_options
1776 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1779 complete_nested_command_line (tracker
, text
);
1782 /* Implementation of the "thread apply" command. */
1785 thread_apply_command (const char *tidlist
, int from_tty
)
1788 const char *cmd
= NULL
;
1789 tid_range_parser parser
;
1791 if (tidlist
== NULL
|| *tidlist
== '\000')
1792 error (_("Please specify a thread ID list"));
1794 parser
.init (tidlist
, current_inferior ()->num
);
1795 while (!parser
.finished ())
1797 int inf_num
, thr_start
, thr_end
;
1799 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1803 cmd
= parser
.cur_tok ();
1805 auto group
= make_thread_apply_options_def_group (&flags
);
1806 gdb::option::process_options
1807 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1809 validate_flags_qcs ("thread apply", &flags
);
1812 error (_("Please specify a command following the thread ID list"));
1814 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1815 invalid_thread_id_error (cmd
);
1817 scoped_restore_current_thread restore_thread
;
1819 parser
.init (tidlist
, current_inferior ()->num
);
1820 while (!parser
.finished ())
1822 struct thread_info
*tp
= NULL
;
1823 struct inferior
*inf
;
1824 int inf_num
, thr_num
;
1826 parser
.get_tid (&inf_num
, &thr_num
);
1827 inf
= find_inferior_id (inf_num
);
1829 tp
= find_thread_id (inf
, thr_num
);
1831 if (parser
.in_star_range ())
1835 warning (_("Unknown inferior %d"), inf_num
);
1836 parser
.skip_range ();
1840 /* No use looking for threads past the highest thread number
1841 the inferior ever had. */
1842 if (thr_num
>= inf
->highest_thread_num
)
1843 parser
.skip_range ();
1845 /* Be quiet about unknown threads numbers. */
1852 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1853 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1855 warning (_("Unknown thread %d"), thr_num
);
1859 if (!switch_to_thread_if_alive (tp
))
1861 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1865 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1870 /* Implementation of the "taas" command. */
1873 taas_command (const char *cmd
, int from_tty
)
1875 if (cmd
== NULL
|| *cmd
== '\0')
1876 error (_("Please specify a command to apply on all threads"));
1877 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1878 execute_command (expanded
.c_str (), from_tty
);
1881 /* Implementation of the "tfaas" command. */
1884 tfaas_command (const char *cmd
, int from_tty
)
1886 if (cmd
== NULL
|| *cmd
== '\0')
1887 error (_("Please specify a command to apply on all frames of all threads"));
1888 std::string expanded
1889 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1890 execute_command (expanded
.c_str (), from_tty
);
1893 /* Switch to the specified thread, or print the current thread. */
1896 thread_command (const char *tidstr
, int from_tty
)
1900 if (inferior_ptid
== null_ptid
)
1901 error (_("No thread selected"));
1903 if (target_has_stack
)
1905 struct thread_info
*tp
= inferior_thread ();
1907 if (tp
->state
== THREAD_EXITED
)
1908 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1909 print_thread_id (tp
),
1910 target_pid_to_str (inferior_ptid
).c_str ());
1912 printf_filtered (_("[Current thread is %s (%s)]\n"),
1913 print_thread_id (tp
),
1914 target_pid_to_str (inferior_ptid
).c_str ());
1917 error (_("No stack."));
1921 ptid_t previous_ptid
= inferior_ptid
;
1923 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1925 /* Print if the thread has not changed, otherwise an event will
1927 if (inferior_ptid
== previous_ptid
)
1929 print_selected_thread_frame (current_uiout
,
1930 USER_SELECTED_THREAD
1931 | USER_SELECTED_FRAME
);
1935 gdb::observers::user_selected_context_changed
.notify
1936 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1941 /* Implementation of `thread name'. */
1944 thread_name_command (const char *arg
, int from_tty
)
1946 struct thread_info
*info
;
1948 if (inferior_ptid
== null_ptid
)
1949 error (_("No thread selected"));
1951 arg
= skip_spaces (arg
);
1953 info
= inferior_thread ();
1955 info
->name
= arg
? xstrdup (arg
) : NULL
;
1958 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1961 thread_find_command (const char *arg
, int from_tty
)
1964 unsigned long match
= 0;
1966 if (arg
== NULL
|| *arg
== '\0')
1967 error (_("Command requires an argument."));
1969 tmp
= re_comp (arg
);
1971 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1973 update_thread_list ();
1974 for (thread_info
*tp
: all_threads ())
1976 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1978 printf_filtered (_("Thread %s has name '%s'\n"),
1979 print_thread_id (tp
), tp
->name
);
1983 tmp
= target_thread_name (tp
);
1984 if (tmp
!= NULL
&& re_exec (tmp
))
1986 printf_filtered (_("Thread %s has target name '%s'\n"),
1987 print_thread_id (tp
), tmp
);
1991 std::string name
= target_pid_to_str (tp
->ptid
);
1992 if (!name
.empty () && re_exec (name
.c_str ()))
1994 printf_filtered (_("Thread %s has target id '%s'\n"),
1995 print_thread_id (tp
), name
.c_str ());
1999 tmp
= target_extra_thread_info (tp
);
2000 if (tmp
!= NULL
&& re_exec (tmp
))
2002 printf_filtered (_("Thread %s has extra info '%s'\n"),
2003 print_thread_id (tp
), tmp
);
2008 printf_filtered (_("No threads match '%s'\n"), arg
);
2011 /* Print notices when new threads are attached and detached. */
2012 bool print_thread_events
= true;
2014 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2015 struct cmd_list_element
*c
, const char *value
)
2017 fprintf_filtered (file
,
2018 _("Printing of thread events is %s.\n"),
2022 /* See gdbthread.h. */
2025 thread_select (const char *tidstr
, thread_info
*tp
)
2027 if (!switch_to_thread_if_alive (tp
))
2028 error (_("Thread ID %s has terminated."), tidstr
);
2030 annotate_thread_changed ();
2032 /* Since the current thread may have changed, see if there is any
2033 exited thread we can now delete. */
2037 /* Print thread and frame switch command response. */
2040 print_selected_thread_frame (struct ui_out
*uiout
,
2041 user_selected_what selection
)
2043 struct thread_info
*tp
= inferior_thread ();
2045 if (selection
& USER_SELECTED_THREAD
)
2047 if (uiout
->is_mi_like_p ())
2049 uiout
->field_signed ("new-thread-id",
2050 inferior_thread ()->global_num
);
2054 uiout
->text ("[Switching to thread ");
2055 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2057 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2062 if (tp
->state
== THREAD_RUNNING
)
2064 if (selection
& USER_SELECTED_THREAD
)
2065 uiout
->text ("(running)\n");
2067 else if (selection
& USER_SELECTED_FRAME
)
2069 if (selection
& USER_SELECTED_THREAD
)
2072 if (has_stack_frames ())
2073 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2078 /* Update the 'threads_executing' global based on the threads we know
2079 about right now. This is used by infrun to tell whether we should
2080 pull events out of the current target. */
2083 update_threads_executing (void)
2085 process_stratum_target
*targ
= current_inferior ()->process_target ();
2090 targ
->threads_executing
= false;
2092 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2094 if (!inf
->has_execution ())
2097 /* If the process has no threads, then it must be we have a
2098 process-exit event pending. */
2099 if (inf
->thread_list
== NULL
)
2101 targ
->threads_executing
= true;
2105 for (thread_info
*tp
: inf
->non_exited_threads ())
2109 targ
->threads_executing
= true;
2117 update_thread_list (void)
2119 target_update_thread_list ();
2120 update_threads_executing ();
2123 /* Return a new value for the selected thread's id. Return a value of
2124 0 if no thread is selected. If GLOBAL is true, return the thread's
2125 global number. Otherwise return the per-inferior number. */
2127 static struct value
*
2128 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2132 if (inferior_ptid
== null_ptid
)
2136 thread_info
*tp
= inferior_thread ();
2138 int_val
= tp
->global_num
;
2140 int_val
= tp
->per_inf_num
;
2143 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2146 /* Return a new value for the selected thread's per-inferior thread
2147 number. Return a value of 0 if no thread is selected, or no
2150 static struct value
*
2151 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2152 struct internalvar
*var
,
2155 return thread_num_make_value_helper (gdbarch
, 0);
2158 /* Return a new value for the selected thread's global id. Return a
2159 value of 0 if no thread is selected, or no threads exist. */
2161 static struct value
*
2162 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2165 return thread_num_make_value_helper (gdbarch
, 1);
2168 /* Commands with a prefix of `thread'. */
2169 struct cmd_list_element
*thread_cmd_list
= NULL
;
2171 /* Implementation of `thread' variable. */
2173 static const struct internalvar_funcs thread_funcs
=
2175 thread_id_per_inf_num_make_value
,
2180 /* Implementation of `gthread' variable. */
2182 static const struct internalvar_funcs gthread_funcs
=
2184 global_thread_id_make_value
,
2189 void _initialize_thread ();
2191 _initialize_thread ()
2193 static struct cmd_list_element
*thread_apply_list
= NULL
;
2194 cmd_list_element
*c
;
2196 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2198 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2200 static std::string info_threads_help
2201 = gdb::option::build_help (_("\
2202 Display currently known threads.\n\
2203 Usage: info threads [OPTION]... [ID]...\n\
2207 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2208 Otherwise, all threads are displayed."),
2211 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2212 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2214 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2215 Use this command to switch between threads.\n\
2216 The new thread ID must be currently known."),
2217 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2219 #define THREAD_APPLY_OPTION_HELP "\
2220 Prints per-inferior thread number and target system's thread id\n\
2221 followed by COMMAND output.\n\
2223 By default, an error raised during the execution of COMMAND\n\
2224 aborts \"thread apply\".\n\
2229 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2231 static std::string thread_apply_help
= gdb::option::build_help (_("\
2232 Apply a command to a list of threads.\n\
2233 Usage: thread apply ID... [OPTION]... COMMAND\n\
2234 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2235 THREAD_APPLY_OPTION_HELP
),
2238 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2239 thread_apply_help
.c_str (),
2240 &thread_apply_list
, "thread apply ", 1,
2242 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2244 const auto thread_apply_all_opts
2245 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2247 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2248 Apply a command to all threads.\n\
2250 Usage: thread apply all [OPTION]... COMMAND\n"
2251 THREAD_APPLY_OPTION_HELP
),
2252 thread_apply_all_opts
);
2254 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2255 thread_apply_all_help
.c_str (),
2256 &thread_apply_list
);
2257 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2259 c
= add_com ("taas", class_run
, taas_command
, _("\
2260 Apply a command to all threads (ignoring errors and empty output).\n\
2261 Usage: taas [OPTION]... COMMAND\n\
2262 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2263 See \"help thread apply all\" for available options."));
2264 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2266 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2267 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2268 Usage: tfaas [OPTION]... COMMAND\n\
2269 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2270 See \"help frame apply all\" for available options."));
2271 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2273 add_cmd ("name", class_run
, thread_name_command
,
2274 _("Set the current thread's name.\n\
2275 Usage: thread name [NAME]\n\
2276 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2278 add_cmd ("find", class_run
, thread_find_command
, _("\
2279 Find threads that match a regular expression.\n\
2280 Usage: thread find REGEXP\n\
2281 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2284 add_com_alias ("t", "thread", class_run
, 1);
2286 add_setshow_boolean_cmd ("thread-events", no_class
,
2287 &print_thread_events
, _("\
2288 Set printing of thread events (such as thread start and exit)."), _("\
2289 Show printing of thread events (such as thread start and exit)."), NULL
,
2291 show_print_thread_events
,
2292 &setprintlist
, &showprintlist
);
2294 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2295 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);