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 static int thread_alive (struct thread_info
*);
67 /* RAII type used to increase / decrease the refcount of each thread
68 in a given list of threads. */
70 class scoped_inc_dec_ref
73 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
76 for (thread_info
*thr
: m_thrds
)
80 ~scoped_inc_dec_ref ()
82 for (thread_info
*thr
: m_thrds
)
87 const std::vector
<thread_info
*> &m_thrds
;
92 inferior_thread (void)
94 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
99 /* Delete the breakpoint pointed at by BP_P, if there's one. */
102 delete_thread_breakpoint (struct breakpoint
**bp_p
)
106 delete_breakpoint (*bp_p
);
112 delete_step_resume_breakpoint (struct thread_info
*tp
)
115 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
119 delete_exception_resume_breakpoint (struct thread_info
*tp
)
122 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
125 /* See gdbthread.h. */
128 delete_single_step_breakpoints (struct thread_info
*tp
)
131 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
134 /* Delete the breakpoint pointed at by BP_P at the next stop, if
138 delete_at_next_stop (struct breakpoint
**bp
)
142 (*bp
)->disposition
= disp_del_at_next_stop
;
147 /* See gdbthread.h. */
150 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
152 return tp
->control
.single_step_breakpoints
!= NULL
;
155 /* See gdbthread.h. */
158 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
159 const address_space
*aspace
,
162 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
164 return (ss_bps
!= NULL
165 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
168 /* See gdbthread.h. */
171 thread_cancel_execution_command (struct thread_info
*thr
)
173 if (thr
->thread_fsm
!= NULL
)
175 thr
->thread_fsm
->clean_up (thr
);
176 delete thr
->thread_fsm
;
177 thr
->thread_fsm
= NULL
;
182 clear_thread_inferior_resources (struct thread_info
*tp
)
184 /* NOTE: this will take care of any left-over step_resume breakpoints,
185 but not any user-specified thread-specific breakpoints. We can not
186 delete the breakpoint straight-off, because the inferior might not
187 be stopped at the moment. */
188 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
189 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
190 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
192 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
194 bpstat_clear (&tp
->control
.stop_bpstat
);
196 btrace_teardown (tp
);
198 thread_cancel_execution_command (tp
);
200 clear_inline_frame_state (tp
->ptid
);
203 /* Set the TP's state as exited. */
206 set_thread_exited (thread_info
*tp
, int silent
)
208 /* Dead threads don't need to step-over. Remove from queue. */
209 if (tp
->step_over_next
!= NULL
)
210 global_thread_step_over_chain_remove (tp
);
212 if (tp
->state
!= THREAD_EXITED
)
214 gdb::observers::thread_exit
.notify (tp
, silent
);
216 /* Tag it as exited. */
217 tp
->state
= THREAD_EXITED
;
219 /* Clear breakpoints, etc. associated with this thread. */
220 clear_thread_inferior_resources (tp
);
225 init_thread_list (void)
227 highest_thread_num
= 0;
229 for (thread_info
*tp
: all_threads_safe ())
231 inferior
*inf
= tp
->inf
;
233 if (tp
->deletable ())
236 set_thread_exited (tp
, 1);
238 inf
->thread_map
.clear();
242 /* Allocate a new thread of inferior INF with target id PTID and add
243 it to the thread list. */
245 static struct thread_info
*
246 new_thread (struct inferior
*inf
, ptid_t ptid
)
248 thread_info
*tp
= new thread_info (inf
, ptid
);
250 /* A thread with this ptid should not exist yet. */
251 gdb_assert (inf
->thread_map
.find (ptid
) == inf
->thread_map
.end ());
253 inf
->thread_map
[ptid
] = tp
;
259 add_thread_silent (ptid_t ptid
)
261 struct inferior
*inf
= find_inferior_ptid (ptid
);
262 gdb_assert (inf
!= NULL
);
264 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
266 /* Found an old thread with the same id. It has to be dead,
267 otherwise we wouldn't be adding a new thread with the same id.
268 The OS is reusing this id --- delete it, and recreate a new
271 /* In addition to deleting the thread, if this is the current
272 thread, then we need to take care that delete_thread doesn't
273 really delete the thread if it is inferior_ptid. Create a
274 new template thread in the list with an invalid ptid, switch
275 to it, delete the original thread, reset the new thread's
276 ptid, and switch to it. */
278 if (inferior_ptid
== ptid
)
280 thread_info
*new_thr
= new_thread (inf
, null_ptid
);
282 /* Make switch_to_thread not read from the thread. */
283 new_thr
->state
= THREAD_EXITED
;
284 switch_to_no_thread ();
286 /* Now we can delete it. */
289 /* Now reset its ptid, and reswitch inferior_ptid to it. */
290 new_thr
->ptid
= ptid
;
291 new_thr
->state
= THREAD_STOPPED
;
292 switch_to_thread (new_thr
);
294 gdb::observers::new_thread
.notify (new_thr
);
300 /* Just go ahead and delete it. */
304 tp
= new_thread (inf
, ptid
);
305 gdb::observers::new_thread
.notify (tp
);
311 add_thread_with_info (ptid_t ptid
, private_thread_info
*priv
)
313 struct thread_info
*result
= add_thread_silent (ptid
);
315 result
->priv
.reset (priv
);
317 if (print_thread_events
)
318 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
320 annotate_new_thread ();
325 add_thread (ptid_t ptid
)
327 return add_thread_with_info (ptid
, NULL
);
330 private_thread_info::~private_thread_info () = default;
332 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
333 : ptid (ptid_
), inf (inf_
)
335 gdb_assert (inf_
!= NULL
);
337 this->global_num
= ++highest_thread_num
;
338 this->per_inf_num
= ++inf_
->highest_thread_num
;
340 /* Nothing to follow yet. */
341 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
342 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
343 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
346 thread_info::~thread_info ()
351 /* See gdbthread.h. */
354 thread_info::deletable () const
356 /* If this is the current thread, or there's code out there that
357 relies on it existing (refcount > 0) we can't delete yet. */
358 return refcount () == 0 && ptid
!= inferior_ptid
;
361 /* See gdbthread.h. */
363 thread_info::regcache ()
365 return get_thread_regcache (this);
368 /* See gdbthread.h. */
372 return this->regcache ()-> arch ();
375 /* Add TP to the end of the step-over chain LIST_P. */
378 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
380 gdb_assert (tp
->step_over_next
== NULL
);
381 gdb_assert (tp
->step_over_prev
== NULL
);
386 tp
->step_over_prev
= tp
->step_over_next
= tp
;
390 struct thread_info
*head
= *list_p
;
391 struct thread_info
*tail
= head
->step_over_prev
;
393 tp
->step_over_prev
= tail
;
394 tp
->step_over_next
= head
;
395 head
->step_over_prev
= tp
;
396 tail
->step_over_next
= tp
;
400 /* See gdbthread.h. */
403 thread_step_over_chain_remove (thread_info
**list_p
, thread_info
*tp
)
405 gdb_assert (tp
->step_over_next
!= NULL
);
406 gdb_assert (tp
->step_over_prev
!= NULL
);
410 if (tp
== tp
->step_over_next
)
413 *list_p
= tp
->step_over_next
;
416 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
417 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
418 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
421 /* See gdbthread.h. */
424 global_thread_step_over_chain_remove (thread_info
*tp
)
426 thread_step_over_chain_remove (&global_thread_step_over_chain_head
, tp
);
429 /* See gdbthread.h. */
432 thread_step_over_chain_next (thread_info
*chain_head
, thread_info
*tp
)
434 thread_info
*next
= tp
->step_over_next
;
436 return next
== chain_head
? NULL
: next
;
439 /* See gdbthread.h. */
442 global_thread_step_over_chain_next (thread_info
*tp
)
444 return thread_step_over_chain_next (global_thread_step_over_chain_head
, tp
);
447 /* See gdbthread.h. */
450 thread_is_in_step_over_chain (struct thread_info
*tp
)
452 return (tp
->step_over_next
!= NULL
);
455 /* See gdbthread.h. */
457 int thread_step_over_chain_length (thread_info
*tp
)
463 thread_info
*iter
= tp
->step_over_next
;
468 iter
= iter
->step_over_next
;
476 /* See gdbthread.h. */
479 global_thread_step_over_chain_enqueue (struct thread_info
*tp
)
482 fprintf_unfiltered (gdb_stdlog
, "enqueueing thread %ld in global step over chain\n", tp
->ptid
.lwp());
483 step_over_chain_enqueue (&global_thread_step_over_chain_head
, tp
);
486 /* Delete the thread referenced by THR. If SILENT, don't notify
487 the observer of this exit.
489 THR must not be NULL or a failed assertion will be raised. */
492 delete_thread_1 (thread_info
*thr
, bool silent
, bool remove
)
494 gdb_assert (thr
!= nullptr);
496 set_thread_exited (thr
, silent
);
498 if (!thr
->deletable ())
500 /* Will be really deleted some other time. */
506 size_t nr_deleted
= thr
->inf
->thread_map
.erase(thr
->ptid
);
507 gdb_assert (nr_deleted
== 1);
513 /* Delete thread THREAD and notify of thread exit. If this is the
514 current thread, don't actually delete it, but tag it as exited and
515 do the notification. If this is the user selected thread, clear
519 delete_thread (thread_info
*thread
)
521 delete_thread_1 (thread
, false /* not silent */, true /* remove */);
525 delete_thread_noremove (thread_info
*thread
)
527 delete_thread_1 (thread
, false /* silent */, false /* don't remove */);
531 delete_thread_silent (thread_info
*thread
)
533 delete_thread_1 (thread
, true /* silent */, true /* remove */);
537 delete_thread_silent_noremove (thread_info
*thread
)
539 delete_thread_1 (thread
, true /* silent */, false /* don't remove */);
543 find_thread_global_id (int global_id
)
545 for (thread_info
*tp
: all_threads ())
546 if (tp
->global_num
== global_id
)
552 static struct thread_info
*
553 find_thread_id (struct inferior
*inf
, int thr_num
)
555 for (thread_info
*tp
: inf
->threads ())
556 if (tp
->per_inf_num
== thr_num
)
562 /* Find a thread_info by matching PTID. */
565 find_thread_ptid (ptid_t ptid
)
567 inferior
*inf
= find_inferior_ptid (ptid
);
570 return find_thread_ptid (inf
, ptid
);
573 /* See gdbthread.h. */
576 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
578 auto it
= inf
->thread_map
.find (ptid
);
579 if (it
!= inf
->thread_map
.end ())
585 /* See gdbthread.h. */
588 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
589 struct inferior
*inf
)
591 return target_thread_handle_to_thread_info (handle
.data (),
597 * Thread iterator function.
599 * Calls a callback function once for each thread, so long as
600 * the callback function returns false. If the callback function
601 * returns true, the iteration will end and the current thread
602 * will be returned. This can be useful for implementing a
603 * search for a thread with arbitrary attributes, or for applying
604 * some operation to every thread.
606 * FIXME: some of the existing functionality, such as
607 * "Thread apply all", might be rewritten using this functionality.
611 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
614 for (thread_info
*tp
: all_threads_safe ())
615 if ((*callback
) (tp
, data
))
621 /* See gdbthread.h. */
626 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
634 auto rng
= all_threads ();
635 return std::distance (rng
.begin (), rng
.end ());
638 /* Return the number of non-exited threads in the thread list. */
641 live_threads_count (void)
643 auto rng
= all_non_exited_threads ();
644 return std::distance (rng
.begin (), rng
.end ());
648 valid_global_thread_id (int global_id
)
650 for (thread_info
*tp
: all_threads ())
651 if (tp
->global_num
== global_id
)
658 in_thread_list (ptid_t ptid
)
660 return find_thread_ptid (ptid
) != nullptr;
663 /* Finds the first thread of the inferior. */
666 first_thread_of_inferior (inferior
*inf
)
668 gdb_assert (!inf
->thread_map
.empty ());
670 auto compare_by_per_inf_num
= [] (const ptid_thread_map::value_type
&a
,
671 const ptid_thread_map::value_type
&b
)
673 return a
.second
->per_inf_num
< b
.second
->per_inf_num
;
675 auto it
= std::min_element (inf
->thread_map
.begin (), inf
->thread_map
.end (),
676 compare_by_per_inf_num
);
682 any_thread_of_inferior (inferior
*inf
)
684 gdb_assert (inf
->pid
!= 0);
686 /* Prefer the current thread. */
687 if (inf
== current_inferior ())
688 return inferior_thread ();
690 for (thread_info
*tp
: inf
->non_exited_threads ())
697 any_live_thread_of_inferior (inferior
*inf
)
699 struct thread_info
*curr_tp
= NULL
;
700 struct thread_info
*tp_executing
= NULL
;
702 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
704 /* Prefer the current thread if it's not executing. */
705 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
707 /* If the current thread is dead, forget it. If it's not
708 executing, use it. Otherwise, still choose it (below), but
709 only if no other non-executing thread is found. */
710 curr_tp
= inferior_thread ();
711 if (curr_tp
->state
== THREAD_EXITED
)
713 else if (!curr_tp
->executing
)
717 for (thread_info
*tp
: inf
->non_exited_threads ())
725 /* If both the current thread and all live threads are executing,
726 prefer the current thread. */
730 /* Otherwise, just return an executing thread, if any. */
734 /* Return true if TP is an active thread. */
736 thread_alive (struct thread_info
*tp
)
738 if (tp
->state
== THREAD_EXITED
)
740 if (!target_thread_alive (tp
->ptid
))
745 /* See gdbthreads.h. */
750 for (thread_info
*tp
: all_threads_safe ())
751 if (!thread_alive (tp
))
755 /* See gdbthreads.h. */
758 delete_exited_threads (void)
760 for (thread_info
*tp
: all_threads_safe ())
761 if (tp
->state
== THREAD_EXITED
)
765 /* Return true value if stack temporaries are enabled for the thread
769 thread_stack_temporaries_enabled_p (thread_info
*tp
)
774 return tp
->stack_temporaries_enabled
;
777 /* Push V on to the stack temporaries of the thread with id PTID. */
780 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
782 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
783 tp
->stack_temporaries
.push_back (v
);
786 /* Return true if VAL is among the stack temporaries of the thread
787 TP. Return false otherwise. */
790 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
792 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
793 for (value
*v
: tp
->stack_temporaries
)
800 /* Return the last of the stack temporaries for thread with id PTID.
801 Return NULL if there are no stack temporaries for the thread. */
804 get_last_thread_stack_temporary (thread_info
*tp
)
806 struct value
*lastval
= NULL
;
808 gdb_assert (tp
!= NULL
);
809 if (!tp
->stack_temporaries
.empty ())
810 lastval
= tp
->stack_temporaries
.back ();
816 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
818 struct inferior
*inf
;
819 struct thread_info
*tp
;
821 /* It can happen that what we knew as the target inferior id
822 changes. E.g, target remote may only discover the remote process
823 pid after adding the inferior to GDB's list. */
824 inf
= find_inferior_ptid (old_ptid
);
825 inf
->pid
= new_ptid
.pid ();
827 tp
= find_thread_ptid (inf
, old_ptid
);
828 gdb_assert (tp
!= nullptr);
830 int num_erased
= inf
->thread_map
.erase (old_ptid
);
831 gdb_assert (num_erased
== 1);
834 inf
->thread_map
[new_ptid
] = tp
;
836 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
839 /* See gdbthread.h. */
842 set_resumed (ptid_t ptid
, int resumed
)
844 for (thread_info
*tp
: all_non_exited_threads (ptid
))
845 tp
->resumed
= resumed
;
848 /* Helper for set_running, that marks one thread either running or
852 set_running_thread (struct thread_info
*tp
, int running
)
856 if (running
&& tp
->state
== THREAD_STOPPED
)
858 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
862 /* If the thread is now marked stopped, remove it from
863 the step-over queue, so that we don't try to resume
864 it until the user wants it to. */
865 if (tp
->step_over_next
!= NULL
)
866 global_thread_step_over_chain_remove (tp
);
872 /* See gdbthread.h. */
875 thread_info::set_running (bool running
)
877 if (set_running_thread (this, running
))
878 gdb::observers::target_resumed
.notify (this->ptid
);
882 set_running (ptid_t ptid
, int running
)
884 /* We try not to notify the observer if no thread has actually
885 changed the running state -- merely to reduce the number of
886 messages to the MI frontend. A frontend is supposed to handle
887 multiple *running notifications just fine. */
888 bool any_started
= false;
890 for (thread_info
*tp
: all_non_exited_threads (ptid
))
891 if (set_running_thread (tp
, running
))
895 gdb::observers::target_resumed
.notify (ptid
);
899 /* Helper for set_executing. Set's the thread's 'executing' field
900 from EXECUTING, and if EXECUTING is true also clears the thread's
904 set_executing_thread (thread_info
*thr
, bool executing
)
906 thr
->executing
= executing
;
908 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
912 set_executing (ptid_t ptid
, int executing
)
914 for (thread_info
*tp
: all_non_exited_threads (ptid
))
915 set_executing_thread (tp
, executing
);
917 /* It only takes one running thread to spawn more threads. */
919 threads_executing
= 1;
920 /* Only clear the flag if the caller is telling us everything is
922 else if (minus_one_ptid
== ptid
)
923 threads_executing
= 0;
926 /* See gdbthread.h. */
929 threads_are_executing (void)
931 return threads_executing
;
935 set_stop_requested (ptid_t ptid
, int stop
)
937 for (thread_info
*tp
: all_non_exited_threads (ptid
))
938 tp
->stop_requested
= stop
;
940 /* Call the stop requested observer so other components of GDB can
941 react to this request. */
943 gdb::observers::thread_stop_requested
.notify (ptid
);
947 finish_thread_state (ptid_t ptid
)
949 bool any_started
= false;
951 for (thread_info
*tp
: all_non_exited_threads (ptid
))
952 if (set_running_thread (tp
, tp
->executing
))
956 gdb::observers::target_resumed
.notify (ptid
);
959 /* See gdbthread.h. */
962 validate_registers_access (void)
964 /* No selected thread, no registers. */
965 if (inferior_ptid
== null_ptid
)
966 error (_("No thread selected."));
968 thread_info
*tp
= inferior_thread ();
970 /* Don't try to read from a dead thread. */
971 if (tp
->state
== THREAD_EXITED
)
972 error (_("The current thread has terminated"));
974 /* ... or from a spinning thread. FIXME: This isn't actually fully
975 correct. It'll allow an user-requested access (e.g., "print $pc"
976 at the prompt) when a thread is not executing for some internal
977 reason, but is marked running from the user's perspective. E.g.,
978 the thread is waiting for its turn in the step-over queue. */
980 error (_("Selected thread is running."));
983 /* See gdbthread.h. */
986 can_access_registers_thread (thread_info
*thread
)
988 /* No thread, no registers. */
992 /* Don't try to read from a dead thread. */
993 if (thread
->state
== THREAD_EXITED
)
996 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
997 if (thread
->executing
)
1004 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1006 return (pc
>= thread
->control
.step_range_start
1007 && pc
< thread
->control
.step_range_end
);
1010 /* Helper for print_thread_info. Returns true if THR should be
1011 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1012 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1013 is true if REQUESTED_THREADS is list of global IDs, false if a list
1014 of per-inferior thread ids. If PID is not -1, only print THR if it
1015 is a thread from the process PID. Otherwise, threads from all
1016 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1017 and PID is not -1, then the thread is printed if it belongs to the
1018 specified process. Otherwise, an error is raised. */
1021 should_print_thread (const char *requested_threads
, int default_inf_num
,
1022 int global_ids
, int pid
, struct thread_info
*thr
)
1024 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1029 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1031 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1032 thr
->inf
->num
, thr
->per_inf_num
);
1037 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1039 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1040 error (_("Requested thread not found in requested process"));
1044 if (thr
->state
== THREAD_EXITED
)
1050 /* Return the string to display in "info threads"'s "Target Id"
1054 thread_target_id_str (thread_info
*tp
)
1056 std::string target_id
= target_pid_to_str (tp
->ptid
);
1057 const char *extra_info
= target_extra_thread_info (tp
);
1058 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1060 if (extra_info
!= nullptr && name
!= nullptr)
1061 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1063 else if (extra_info
!= nullptr)
1064 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1065 else if (name
!= nullptr)
1066 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1071 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1072 whether REQUESTED_THREADS is a list of global or per-inferior
1076 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1077 int global_ids
, int pid
,
1078 int show_global_ids
)
1080 int default_inf_num
= current_inferior ()->num
;
1082 update_thread_list ();
1084 /* Whether we saw any thread. */
1085 bool any_thread
= false;
1086 /* Whether the current thread is exited. */
1087 bool current_exited
= false;
1089 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1090 ? inferior_thread () : NULL
);
1093 /* For backward compatibility, we make a list for MI. A table is
1094 preferable for the CLI, though, because it shows table
1096 gdb::optional
<ui_out_emit_list
> list_emitter
;
1097 gdb::optional
<ui_out_emit_table
> table_emitter
;
1099 if (uiout
->is_mi_like_p ())
1100 list_emitter
.emplace (uiout
, "threads");
1104 /* The width of the "Target Id" column. Grown below to
1105 accommodate the largest entry. */
1106 size_t target_id_col_width
= 17;
1108 for (thread_info
*tp
: all_threads ())
1110 if (!should_print_thread (requested_threads
, default_inf_num
,
1111 global_ids
, pid
, tp
))
1114 if (!uiout
->is_mi_like_p ())
1117 = std::max (target_id_col_width
,
1118 thread_target_id_str (tp
).size ());
1126 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1127 uiout
->message (_("No threads.\n"));
1129 uiout
->message (_("No threads match '%s'.\n"),
1134 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1135 n_threads
, "threads");
1137 uiout
->table_header (1, ui_left
, "current", "");
1138 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1139 if (show_global_ids
)
1140 uiout
->table_header (4, ui_left
, "id", "GId");
1141 uiout
->table_header (target_id_col_width
, ui_left
,
1142 "target-id", "Target Id");
1143 uiout
->table_header (1, ui_left
, "frame", "Frame");
1144 uiout
->table_body ();
1147 /* We'll be switching threads temporarily. */
1148 scoped_restore_current_thread restore_thread
;
1150 for (inferior
*inf
: all_inferiors ())
1152 /* Print the threads in per-inferior number order. */
1153 std::vector
<thread_info
*> threads_to_print
;
1155 for (thread_info
*tp
: inf
->threads ())
1156 threads_to_print
.push_back (tp
);
1158 std::sort (threads_to_print
.begin (), threads_to_print
.end (),
1159 [] (thread_info
*a
, thread_info
*b
)
1161 return a
->per_inf_num
< b
->per_inf_num
;
1164 for (thread_info
*tp
: threads_to_print
)
1169 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1170 current_exited
= true;
1172 if (!should_print_thread (requested_threads
, default_inf_num
,
1173 global_ids
, pid
, tp
))
1176 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1178 if (!uiout
->is_mi_like_p ())
1180 if (tp
== current_thread
)
1181 uiout
->field_string ("current", "*");
1183 uiout
->field_skip ("current");
1185 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1188 if (show_global_ids
|| uiout
->is_mi_like_p ())
1189 uiout
->field_signed ("id", tp
->global_num
);
1191 /* For the CLI, we stuff everything into the target-id field.
1192 This is a gross hack to make the output come out looking
1193 correct. The underlying problem here is that ui-out has no
1194 way to specify that a field's space allocation should be
1195 shared by several fields. For MI, we do the right thing
1198 if (uiout
->is_mi_like_p ())
1200 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1202 const char *extra_info
= target_extra_thread_info (tp
);
1203 if (extra_info
!= nullptr)
1204 uiout
->field_string ("details", extra_info
);
1206 const char *name
= (tp
->name
!= nullptr
1208 : target_thread_name (tp
));
1210 uiout
->field_string ("name", name
);
1214 uiout
->field_string ("target-id",
1215 thread_target_id_str (tp
).c_str ());
1218 if (tp
->state
== THREAD_RUNNING
)
1219 uiout
->text ("(running)\n");
1222 /* The switch below puts us at the top of the stack (leaf
1224 switch_to_thread (tp
);
1225 print_stack_frame (get_selected_frame (NULL
),
1226 /* For MI output, print frame level. */
1227 uiout
->is_mi_like_p (),
1231 if (uiout
->is_mi_like_p ())
1233 const char *state
= "stopped";
1235 if (tp
->state
== THREAD_RUNNING
)
1237 uiout
->field_string ("state", state
);
1240 core
= target_core_of_thread (tp
->ptid
);
1241 if (uiout
->is_mi_like_p () && core
!= -1)
1242 uiout
->field_signed ("core", core
);
1246 /* This end scope restores the current thread and the frame
1247 selected before the "info threads" command, and it finishes the
1248 ui-out list or table. */
1251 if (pid
== -1 && requested_threads
== NULL
)
1253 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1254 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1256 if (inferior_ptid
!= null_ptid
&& current_exited
)
1257 uiout
->message ("\n\
1258 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1259 print_thread_id (inferior_thread ()));
1260 else if (any_thread
&& inferior_ptid
== null_ptid
)
1261 uiout
->message ("\n\
1262 No selected thread. See `help thread'.\n");
1266 /* See gdbthread.h. */
1269 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1272 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1275 /* The options for the "info threads" command. */
1277 struct info_threads_opts
1280 bool show_global_ids
= false;
1283 static const gdb::option::option_def info_threads_option_defs
[] = {
1285 gdb::option::flag_option_def
<info_threads_opts
> {
1287 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1288 N_("Show global thread IDs."),
1293 /* Create an option_def_group for the "info threads" options, with
1294 IT_OPTS as context. */
1296 static inline gdb::option::option_def_group
1297 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1299 return {{info_threads_option_defs
}, it_opts
};
1302 /* Implementation of the "info threads" command.
1304 Note: this has the drawback that it _really_ switches
1305 threads, which frees the frame cache. A no-side
1306 effects info-threads command would be nicer. */
1309 info_threads_command (const char *arg
, int from_tty
)
1311 info_threads_opts it_opts
;
1313 auto grp
= make_info_threads_options_def_group (&it_opts
);
1314 gdb::option::process_options
1315 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1317 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1320 /* Completer for the "info threads" command. */
1323 info_threads_command_completer (struct cmd_list_element
*ignore
,
1324 completion_tracker
&tracker
,
1325 const char *text
, const char *word_ignored
)
1327 const auto grp
= make_info_threads_options_def_group (nullptr);
1329 if (gdb::option::complete_options
1330 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1333 /* Convenience to let the user know what the option can accept. */
1336 gdb::option::complete_on_all_options (tracker
, grp
);
1337 /* Keep this "ID" in sync with what "help info threads"
1339 tracker
.add_completion (make_unique_xstrdup ("ID"));
1343 /* See gdbthread.h. */
1346 switch_to_thread_no_regs (struct thread_info
*thread
)
1348 struct inferior
*inf
= thread
->inf
;
1350 set_current_program_space (inf
->pspace
);
1351 set_current_inferior (inf
);
1353 inferior_ptid
= thread
->ptid
;
1356 /* See gdbthread.h. */
1359 switch_to_no_thread ()
1361 if (inferior_ptid
== null_ptid
)
1364 inferior_ptid
= null_ptid
;
1365 reinit_frame_cache ();
1368 /* See gdbthread.h. */
1371 switch_to_thread (thread_info
*thr
)
1373 gdb_assert (thr
!= NULL
);
1375 if (inferior_ptid
== thr
->ptid
)
1378 switch_to_thread_no_regs (thr
);
1380 reinit_frame_cache ();
1383 /* See gdbsupport/common-gdbthread.h. */
1386 switch_to_thread (ptid_t ptid
)
1388 thread_info
*thr
= find_thread_ptid (ptid
);
1389 switch_to_thread (thr
);
1393 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1395 struct frame_info
*frame
= NULL
;
1398 /* This means there was no selected frame. */
1399 if (frame_level
== -1)
1401 select_frame (NULL
);
1405 gdb_assert (frame_level
>= 0);
1407 /* Restore by level first, check if the frame id is the same as
1408 expected. If that fails, try restoring by frame id. If that
1409 fails, nothing to do, just warn the user. */
1411 count
= frame_level
;
1412 frame
= find_relative_frame (get_current_frame (), &count
);
1415 /* The frame ids must match - either both valid or both outer_frame_id.
1416 The latter case is not failsafe, but since it's highly unlikely
1417 the search by level finds the wrong frame, it's 99.9(9)% of
1418 the time (for all practical purposes) safe. */
1419 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1421 /* Cool, all is fine. */
1422 select_frame (frame
);
1426 frame
= frame_find_by_id (a_frame_id
);
1429 /* Cool, refound it. */
1430 select_frame (frame
);
1434 /* Nothing else to do, the frame layout really changed. Select the
1435 innermost stack frame. */
1436 select_frame (get_current_frame ());
1438 /* Warn the user. */
1439 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1441 warning (_("Couldn't restore frame #%d in "
1442 "current thread. Bottom (innermost) frame selected:"),
1444 /* For MI, we should probably have a notification about
1445 current frame change. But this error is not very
1446 likely, so don't bother for now. */
1447 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1451 scoped_restore_current_thread::~scoped_restore_current_thread ()
1453 /* If an entry of thread_info was previously selected, it won't be
1454 deleted because we've increased its refcount. The thread represented
1455 by this thread_info entry may have already exited (due to normal exit,
1456 detach, etc), so the thread_info.state is THREAD_EXITED. */
1457 if (m_thread
!= NULL
1458 /* If the previously selected thread belonged to a process that has
1459 in the mean time exited (or killed, detached, etc.), then don't revert
1460 back to it, but instead simply drop back to no thread selected. */
1462 switch_to_thread (m_thread
);
1465 switch_to_no_thread ();
1466 set_current_inferior (m_inf
);
1469 /* The running state of the originally selected thread may have
1470 changed, so we have to recheck it here. */
1471 if (inferior_ptid
!= null_ptid
1473 && m_thread
->state
== THREAD_STOPPED
1474 && target_has_registers
1476 && target_has_memory
)
1477 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1479 if (m_thread
!= NULL
)
1480 m_thread
->decref ();
1484 scoped_restore_current_thread::scoped_restore_current_thread ()
1487 m_inf
= current_inferior ();
1489 if (inferior_ptid
!= null_ptid
)
1491 thread_info
*tp
= inferior_thread ();
1492 struct frame_info
*frame
;
1494 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1496 && target_has_registers
1498 && target_has_memory
)
1500 /* When processing internal events, there might not be a
1501 selected frame. If we naively call get_selected_frame
1502 here, then we can end up reading debuginfo for the
1503 current frame, but we don't generally need the debuginfo
1505 frame
= get_selected_frame_if_set ();
1510 m_selected_frame_id
= get_frame_id (frame
);
1511 m_selected_frame_level
= frame_relative_level (frame
);
1520 /* See gdbthread.h. */
1523 show_thread_that_caused_stop (void)
1525 return highest_thread_num
> 1;
1528 /* See gdbthread.h. */
1531 show_inferior_qualified_tids (void)
1533 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1536 /* See gdbthread.h. */
1539 print_thread_id (struct thread_info
*thr
)
1541 char *s
= get_print_cell ();
1543 if (show_inferior_qualified_tids ())
1544 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1546 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1550 /* Sort an array of struct thread_info pointers by thread ID (first by
1551 inferior number, and then by per-inferior thread number). Sorts in
1555 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1557 if (a
->inf
->num
!= b
->inf
->num
)
1558 return a
->inf
->num
< b
->inf
->num
;
1560 return (a
->per_inf_num
< b
->per_inf_num
);
1563 /* Sort an array of struct thread_info pointers by thread ID (first by
1564 inferior number, and then by per-inferior thread number). Sorts in
1565 descending order. */
1568 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1570 if (a
->inf
->num
!= b
->inf
->num
)
1571 return a
->inf
->num
> b
->inf
->num
;
1573 return (a
->per_inf_num
> b
->per_inf_num
);
1576 /* Switch to thread THR and execute CMD.
1577 FLAGS.QUIET controls the printing of the thread information.
1578 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1581 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1582 const qcs_flags
&flags
)
1584 switch_to_thread (thr
);
1587 std::string cmd_result
= execute_command_to_string
1588 (cmd
, from_tty
, gdb_stdout
->term_out ());
1589 if (!flags
.silent
|| cmd_result
.length () > 0)
1592 printf_filtered (_("\nThread %s (%s):\n"),
1593 print_thread_id (thr
),
1594 target_pid_to_str (inferior_ptid
).c_str ());
1595 printf_filtered ("%s", cmd_result
.c_str ());
1598 catch (const gdb_exception_error
&ex
)
1603 printf_filtered (_("\nThread %s (%s):\n"),
1604 print_thread_id (thr
),
1605 target_pid_to_str (inferior_ptid
).c_str ());
1607 printf_filtered ("%s\n", ex
.what ());
1614 /* Option definition of "thread apply"'s "-ascending" option. */
1616 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1619 Call COMMAND for all threads in ascending order.\n\
1620 The default is descending order."),
1623 /* The qcs command line flags for the "thread apply" commands. Keep
1624 this in sync with the "frame apply" commands. */
1626 using qcs_flag_option_def
1627 = gdb::option::flag_option_def
<qcs_flags
>;
1629 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1630 qcs_flag_option_def
{
1631 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1632 N_("Disables printing the thread information."),
1635 qcs_flag_option_def
{
1636 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1637 N_("Print any error raised by COMMAND and continue."),
1640 qcs_flag_option_def
{
1641 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1642 N_("Silently ignore any errors or empty output produced by COMMAND."),
1646 /* Create an option_def_group for the "thread apply all" options, with
1647 ASCENDING and FLAGS as context. */
1649 static inline std::array
<gdb::option::option_def_group
, 2>
1650 make_thread_apply_all_options_def_group (bool *ascending
,
1654 { {ascending_option_def
.def ()}, ascending
},
1655 { {thr_qcs_flags_option_defs
}, flags
},
1659 /* Create an option_def_group for the "thread apply" options, with
1660 FLAGS as context. */
1662 static inline gdb::option::option_def_group
1663 make_thread_apply_options_def_group (qcs_flags
*flags
)
1665 return {{thr_qcs_flags_option_defs
}, flags
};
1668 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1669 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1670 of two numbers separated by a hyphen. Examples:
1672 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1673 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1674 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1677 thread_apply_all_command (const char *cmd
, int from_tty
)
1679 bool ascending
= false;
1682 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1684 gdb::option::process_options
1685 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1687 validate_flags_qcs ("thread apply all", &flags
);
1689 if (cmd
== NULL
|| *cmd
== '\000')
1690 error (_("Please specify a command at the end of 'thread apply all'"));
1692 update_thread_list ();
1694 int tc
= live_threads_count ();
1697 /* Save a copy of the thread list and increment each thread's
1698 refcount while executing the command in the context of each
1699 thread, in case the command is one that wipes threads. E.g.,
1700 detach, kill, disconnect, etc., or even normally continuing
1701 over an inferior or thread exit. */
1702 std::vector
<thread_info
*> thr_list_cpy
;
1703 thr_list_cpy
.reserve (tc
);
1705 for (thread_info
*tp
: all_non_exited_threads ())
1706 thr_list_cpy
.push_back (tp
);
1707 gdb_assert (thr_list_cpy
.size () == tc
);
1709 /* Increment the refcounts, and restore them back on scope
1711 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1713 auto *sorter
= (ascending
1714 ? tp_array_compar_ascending
1715 : tp_array_compar_descending
);
1716 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1718 scoped_restore_current_thread restore_thread
;
1720 for (thread_info
*thr
: thr_list_cpy
)
1721 if (thread_alive (thr
))
1722 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1726 /* Completer for "thread apply [ID list]". */
1729 thread_apply_command_completer (cmd_list_element
*ignore
,
1730 completion_tracker
&tracker
,
1731 const char *text
, const char * /*word*/)
1733 /* Don't leave this to complete_options because there's an early
1735 tracker
.set_use_custom_word_point (true);
1737 tid_range_parser parser
;
1738 parser
.init (text
, current_inferior ()->num
);
1742 while (!parser
.finished ())
1744 int inf_num
, thr_start
, thr_end
;
1746 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1749 if (parser
.in_star_range () || parser
.in_thread_range ())
1750 parser
.skip_range ();
1753 catch (const gdb_exception_error
&ex
)
1755 /* get_tid_range throws if it parses a negative number, for
1756 example. But a seemingly negative number may be the start of
1757 an option instead. */
1760 const char *cmd
= parser
.cur_tok ();
1764 /* No thread ID list yet. */
1768 /* Check if we're past a valid thread ID list already. */
1769 if (parser
.finished ()
1770 && cmd
> text
&& !isspace (cmd
[-1]))
1773 /* We're past the thread ID list, advance word point. */
1774 tracker
.advance_custom_word_point_by (cmd
- text
);
1777 const auto group
= make_thread_apply_options_def_group (nullptr);
1778 if (gdb::option::complete_options
1779 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1782 complete_nested_command_line (tracker
, text
);
1785 /* Completer for "thread apply all". */
1788 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1789 completion_tracker
&tracker
,
1790 const char *text
, const char *word
)
1792 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1794 if (gdb::option::complete_options
1795 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1798 complete_nested_command_line (tracker
, text
);
1801 /* Implementation of the "thread apply" command. */
1804 thread_apply_command (const char *tidlist
, int from_tty
)
1807 const char *cmd
= NULL
;
1808 tid_range_parser parser
;
1810 if (tidlist
== NULL
|| *tidlist
== '\000')
1811 error (_("Please specify a thread ID list"));
1813 parser
.init (tidlist
, current_inferior ()->num
);
1814 while (!parser
.finished ())
1816 int inf_num
, thr_start
, thr_end
;
1818 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1822 cmd
= parser
.cur_tok ();
1824 auto group
= make_thread_apply_options_def_group (&flags
);
1825 gdb::option::process_options
1826 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1828 validate_flags_qcs ("thread apply", &flags
);
1831 error (_("Please specify a command following the thread ID list"));
1833 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1834 invalid_thread_id_error (cmd
);
1836 scoped_restore_current_thread restore_thread
;
1838 parser
.init (tidlist
, current_inferior ()->num
);
1839 while (!parser
.finished ())
1841 struct thread_info
*tp
= NULL
;
1842 struct inferior
*inf
;
1843 int inf_num
, thr_num
;
1845 parser
.get_tid (&inf_num
, &thr_num
);
1846 inf
= find_inferior_id (inf_num
);
1848 tp
= find_thread_id (inf
, thr_num
);
1850 if (parser
.in_star_range ())
1854 warning (_("Unknown inferior %d"), inf_num
);
1855 parser
.skip_range ();
1859 /* No use looking for threads past the highest thread number
1860 the inferior ever had. */
1861 if (thr_num
>= inf
->highest_thread_num
)
1862 parser
.skip_range ();
1864 /* Be quiet about unknown threads numbers. */
1871 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1872 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1874 warning (_("Unknown thread %d"), thr_num
);
1878 if (!thread_alive (tp
))
1880 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1884 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1889 /* Implementation of the "taas" command. */
1892 taas_command (const char *cmd
, int from_tty
)
1894 if (cmd
== NULL
|| *cmd
== '\0')
1895 error (_("Please specify a command to apply on all threads"));
1896 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1897 execute_command (expanded
.c_str (), from_tty
);
1900 /* Implementation of the "tfaas" command. */
1903 tfaas_command (const char *cmd
, int from_tty
)
1905 if (cmd
== NULL
|| *cmd
== '\0')
1906 error (_("Please specify a command to apply on all frames of all threads"));
1907 std::string expanded
1908 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1909 execute_command (expanded
.c_str (), from_tty
);
1912 /* Switch to the specified thread, or print the current thread. */
1915 thread_command (const char *tidstr
, int from_tty
)
1919 if (inferior_ptid
== null_ptid
)
1920 error (_("No thread selected"));
1922 if (target_has_stack
)
1924 struct thread_info
*tp
= inferior_thread ();
1926 if (tp
->state
== THREAD_EXITED
)
1927 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1928 print_thread_id (tp
),
1929 target_pid_to_str (inferior_ptid
).c_str ());
1931 printf_filtered (_("[Current thread is %s (%s)]\n"),
1932 print_thread_id (tp
),
1933 target_pid_to_str (inferior_ptid
).c_str ());
1936 error (_("No stack."));
1940 ptid_t previous_ptid
= inferior_ptid
;
1942 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1944 /* Print if the thread has not changed, otherwise an event will
1946 if (inferior_ptid
== previous_ptid
)
1948 print_selected_thread_frame (current_uiout
,
1949 USER_SELECTED_THREAD
1950 | USER_SELECTED_FRAME
);
1954 gdb::observers::user_selected_context_changed
.notify
1955 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1960 /* Implementation of `thread name'. */
1963 thread_name_command (const char *arg
, int from_tty
)
1965 struct thread_info
*info
;
1967 if (inferior_ptid
== null_ptid
)
1968 error (_("No thread selected"));
1970 arg
= skip_spaces (arg
);
1972 info
= inferior_thread ();
1974 info
->name
= arg
? xstrdup (arg
) : NULL
;
1977 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1980 thread_find_command (const char *arg
, int from_tty
)
1983 unsigned long match
= 0;
1985 if (arg
== NULL
|| *arg
== '\0')
1986 error (_("Command requires an argument."));
1988 tmp
= re_comp (arg
);
1990 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1992 update_thread_list ();
1993 for (thread_info
*tp
: all_threads ())
1995 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1997 printf_filtered (_("Thread %s has name '%s'\n"),
1998 print_thread_id (tp
), tp
->name
);
2002 tmp
= target_thread_name (tp
);
2003 if (tmp
!= NULL
&& re_exec (tmp
))
2005 printf_filtered (_("Thread %s has target name '%s'\n"),
2006 print_thread_id (tp
), tmp
);
2010 std::string name
= target_pid_to_str (tp
->ptid
);
2011 if (!name
.empty () && re_exec (name
.c_str ()))
2013 printf_filtered (_("Thread %s has target id '%s'\n"),
2014 print_thread_id (tp
), name
.c_str ());
2018 tmp
= target_extra_thread_info (tp
);
2019 if (tmp
!= NULL
&& re_exec (tmp
))
2021 printf_filtered (_("Thread %s has extra info '%s'\n"),
2022 print_thread_id (tp
), tmp
);
2027 printf_filtered (_("No threads match '%s'\n"), arg
);
2030 /* Print notices when new threads are attached and detached. */
2031 bool print_thread_events
= true;
2033 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2034 struct cmd_list_element
*c
, const char *value
)
2036 fprintf_filtered (file
,
2037 _("Printing of thread events is %s.\n"),
2041 /* See gdbthread.h. */
2044 thread_select (const char *tidstr
, thread_info
*tp
)
2046 if (!thread_alive (tp
))
2047 error (_("Thread ID %s has terminated."), tidstr
);
2049 switch_to_thread (tp
);
2051 annotate_thread_changed ();
2053 /* Since the current thread may have changed, see if there is any
2054 exited thread we can now delete. */
2058 /* Print thread and frame switch command response. */
2061 print_selected_thread_frame (struct ui_out
*uiout
,
2062 user_selected_what selection
)
2064 struct thread_info
*tp
= inferior_thread ();
2066 if (selection
& USER_SELECTED_THREAD
)
2068 if (uiout
->is_mi_like_p ())
2070 uiout
->field_signed ("new-thread-id",
2071 inferior_thread ()->global_num
);
2075 uiout
->text ("[Switching to thread ");
2076 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2078 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2083 if (tp
->state
== THREAD_RUNNING
)
2085 if (selection
& USER_SELECTED_THREAD
)
2086 uiout
->text ("(running)\n");
2088 else if (selection
& USER_SELECTED_FRAME
)
2090 if (selection
& USER_SELECTED_THREAD
)
2093 if (has_stack_frames ())
2094 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2099 /* Update the 'threads_executing' global based on the threads we know
2103 update_threads_executing (void)
2105 threads_executing
= 0;
2106 for (thread_info
*tp
: all_non_exited_threads ())
2110 threads_executing
= 1;
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
,
2190 _initialize_thread (void)
2192 static struct cmd_list_element
*thread_apply_list
= NULL
;
2193 cmd_list_element
*c
;
2195 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2197 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2199 static std::string info_threads_help
2200 = gdb::option::build_help (_("\
2201 Display currently known threads.\n\
2202 Usage: info threads [OPTION]... [ID]...\n\
2206 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2207 Otherwise, all threads are displayed."),
2210 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2211 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2213 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2214 Use this command to switch between threads.\n\
2215 The new thread ID must be currently known."),
2216 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2218 #define THREAD_APPLY_OPTION_HELP "\
2219 Prints per-inferior thread number and target system's thread id\n\
2220 followed by COMMAND output.\n\
2222 By default, an error raised during the execution of COMMAND\n\
2223 aborts \"thread apply\".\n\
2228 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2230 static std::string thread_apply_help
= gdb::option::build_help (_("\
2231 Apply a command to a list of threads.\n\
2232 Usage: thread apply ID... [OPTION]... COMMAND\n\
2233 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2234 THREAD_APPLY_OPTION_HELP
),
2237 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2238 thread_apply_help
.c_str (),
2239 &thread_apply_list
, "thread apply ", 1,
2241 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2243 const auto thread_apply_all_opts
2244 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2246 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2247 Apply a command to all threads.\n\
2249 Usage: thread apply all [OPTION]... COMMAND\n"
2250 THREAD_APPLY_OPTION_HELP
),
2251 thread_apply_all_opts
);
2253 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2254 thread_apply_all_help
.c_str (),
2255 &thread_apply_list
);
2256 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2258 c
= add_com ("taas", class_run
, taas_command
, _("\
2259 Apply a command to all threads (ignoring errors and empty output).\n\
2260 Usage: taas [OPTION]... COMMAND\n\
2261 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2262 See \"help thread apply all\" for available options."));
2263 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2265 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2266 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2267 Usage: tfaas [OPTION]... COMMAND\n\
2268 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2269 See \"help frame apply all\" for available options."));
2270 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2272 add_cmd ("name", class_run
, thread_name_command
,
2273 _("Set the current thread's name.\n\
2274 Usage: thread name [NAME]\n\
2275 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2277 add_cmd ("find", class_run
, thread_find_command
, _("\
2278 Find threads that match a regular expression.\n\
2279 Usage: thread find REGEXP\n\
2280 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2283 add_com_alias ("t", "thread", class_run
, 1);
2285 add_setshow_boolean_cmd ("thread-events", no_class
,
2286 &print_thread_events
, _("\
2287 Set printing of thread events (such as thread start and exit)."), _("\
2288 Show printing of thread events (such as thread start and exit)."), NULL
,
2290 show_print_thread_events
,
2291 &setprintlist
, &showprintlist
);
2293 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2294 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);