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_map
.clear();
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 /* A thread with this ptid should not exist yet. */
242 gdb_assert (inf
->thread_map
.find (ptid
) == inf
->thread_map
.end ());
244 inf
->thread_map
[ptid
] = tp
;
250 add_thread_silent (process_stratum_target
*targ
, ptid_t ptid
)
254 thread_info
*tp
= find_thread_ptid (targ
, ptid
);
256 /* Found an old thread with the same id. It has to be dead,
257 otherwise we wouldn't be adding a new thread with the same id.
258 The OS is reusing this id --- delete it, and recreate a new
261 /* In addition to deleting the thread, if this is the current
262 thread, then we need to take care that delete_thread doesn't
263 really delete the thread if it is inferior_ptid. Create a
264 new template thread in the list with an invalid ptid, switch
265 to it, delete the original thread, reset the new thread's
266 ptid, and switch to it. */
268 if (inferior_ptid
== ptid
)
270 thread_info
*new_thr
= new_thread (tp
->inf
, null_ptid
);
272 /* Make switch_to_thread not read from the thread. */
273 new_thr
->state
= THREAD_EXITED
;
274 switch_to_no_thread ();
276 /* Now we can delete it. */
279 /* Now reset its ptid, and reswitch inferior_ptid to it. */
280 new_thr
->ptid
= ptid
;
281 new_thr
->state
= THREAD_STOPPED
;
282 switch_to_thread (new_thr
);
284 gdb::observers::new_thread
.notify (new_thr
);
292 /* Just go ahead and delete it. */
296 inf
= find_inferior_ptid (targ
, ptid
);
298 tp
= new_thread (inf
, ptid
);
299 gdb::observers::new_thread
.notify (tp
);
305 add_thread_with_info (process_stratum_target
*targ
, ptid_t ptid
,
306 private_thread_info
*priv
)
308 thread_info
*result
= add_thread_silent (targ
, ptid
);
310 result
->priv
.reset (priv
);
312 if (print_thread_events
)
313 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
315 annotate_new_thread ();
320 add_thread (process_stratum_target
*targ
, ptid_t ptid
)
322 return add_thread_with_info (targ
, ptid
, NULL
);
325 private_thread_info::~private_thread_info () = default;
327 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
328 : ptid (ptid_
), inf (inf_
)
330 gdb_assert (inf_
!= NULL
);
332 this->global_num
= ++highest_thread_num
;
333 this->per_inf_num
= ++inf_
->highest_thread_num
;
335 /* Nothing to follow yet. */
336 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
337 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
338 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
341 thread_info::~thread_info ()
346 /* Returns true if THR is the current thread. */
349 is_current_thread (const thread_info
*thr
)
351 return thr
->inf
== current_inferior () && thr
->ptid
== inferior_ptid
;
354 /* See gdbthread.h. */
357 thread_info::deletable () const
359 /* If this is the current thread, or there's code out there that
360 relies on it existing (refcount > 0) we can't delete yet. */
361 return refcount () == 0 && !is_current_thread (this);
364 /* Add TP to the end of the step-over chain LIST_P. */
367 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
369 gdb_assert (tp
->step_over_next
== NULL
);
370 gdb_assert (tp
->step_over_prev
== NULL
);
375 tp
->step_over_prev
= tp
->step_over_next
= tp
;
379 struct thread_info
*head
= *list_p
;
380 struct thread_info
*tail
= head
->step_over_prev
;
382 tp
->step_over_prev
= tail
;
383 tp
->step_over_next
= head
;
384 head
->step_over_prev
= tp
;
385 tail
->step_over_next
= tp
;
389 /* Remove TP from step-over chain LIST_P. */
392 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
394 gdb_assert (tp
->step_over_next
!= NULL
);
395 gdb_assert (tp
->step_over_prev
!= NULL
);
399 if (tp
== tp
->step_over_next
)
402 *list_p
= tp
->step_over_next
;
405 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
406 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
407 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
410 /* See gdbthread.h. */
413 thread_step_over_chain_next (struct thread_info
*tp
)
415 struct thread_info
*next
= tp
->step_over_next
;
417 return (next
== step_over_queue_head
? NULL
: next
);
420 /* See gdbthread.h. */
423 thread_is_in_step_over_chain (struct thread_info
*tp
)
425 return (tp
->step_over_next
!= NULL
);
428 /* See gdbthread.h. */
431 thread_step_over_chain_enqueue (struct thread_info
*tp
)
433 step_over_chain_enqueue (&step_over_queue_head
, tp
);
436 /* See gdbthread.h. */
439 thread_step_over_chain_remove (struct thread_info
*tp
)
441 step_over_chain_remove (&step_over_queue_head
, tp
);
444 /* Delete the thread referenced by THR. If SILENT, don't notify
445 the observer of this exit.
447 THR must not be NULL or a failed assertion will be raised. */
450 delete_thread_1 (thread_info
*thr
, bool silent
, bool remove
)
452 gdb_assert (thr
!= nullptr);
454 set_thread_exited (thr
, silent
);
456 if (!thr
->deletable ())
458 /* Will be really deleted some other time. */
464 size_t nr_deleted
= thr
->inf
->thread_map
.erase(thr
->ptid
);
465 gdb_assert (nr_deleted
== 1);
471 /* Delete thread THREAD and notify of thread exit. If this is the
472 current thread, don't actually delete it, but tag it as exited and
473 do the notification. If this is the user selected thread, clear
477 delete_thread (thread_info
*thread
)
479 delete_thread_1 (thread
, false /* not silent */, true /* remove */);
483 delete_thread_noremove (thread_info
*thread
)
485 delete_thread_1 (thread
, false /* silent */, false /* don't remove */);
489 delete_thread_silent (thread_info
*thread
)
491 delete_thread_1 (thread
, true /* silent */, true /* remove */);
495 delete_thread_silent_noremove (thread_info
*thread
)
497 delete_thread_1 (thread
, true /* silent */, false /* don't remove */);
501 find_thread_global_id (int global_id
)
503 for (thread_info
*tp
: all_threads ())
504 if (tp
->global_num
== global_id
)
510 static struct thread_info
*
511 find_thread_id (struct inferior
*inf
, int thr_num
)
513 for (thread_info
*tp
: inf
->threads ())
514 if (tp
->per_inf_num
== thr_num
)
520 /* See gdbthread.h. */
523 find_thread_ptid (process_stratum_target
*targ
, ptid_t ptid
)
525 inferior
*inf
= find_inferior_ptid (targ
, ptid
);
528 return find_thread_ptid (inf
, ptid
);
531 /* See gdbthread.h. */
534 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
536 auto it
= inf
->thread_map
.find (ptid
);
537 if (it
!= inf
->thread_map
.end ())
543 /* See gdbthread.h. */
546 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
547 struct inferior
*inf
)
549 return target_thread_handle_to_thread_info (handle
.data (),
555 * Thread iterator function.
557 * Calls a callback function once for each thread, so long as
558 * the callback function returns false. If the callback function
559 * returns true, the iteration will end and the current thread
560 * will be returned. This can be useful for implementing a
561 * search for a thread with arbitrary attributes, or for applying
562 * some operation to every thread.
564 * FIXME: some of the existing functionality, such as
565 * "Thread apply all", might be rewritten using this functionality.
569 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
572 for (thread_info
*tp
: all_threads_safe ())
573 if ((*callback
) (tp
, data
))
579 /* See gdbthread.h. */
584 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
590 thread_count (process_stratum_target
*proc_target
)
592 auto rng
= all_threads (proc_target
);
593 return std::distance (rng
.begin (), rng
.end ());
596 /* Return the number of non-exited threads in the thread list. */
599 live_threads_count (void)
601 auto rng
= all_non_exited_threads ();
602 return std::distance (rng
.begin (), rng
.end ());
606 valid_global_thread_id (int global_id
)
608 for (thread_info
*tp
: all_threads ())
609 if (tp
->global_num
== global_id
)
616 in_thread_list (process_stratum_target
*targ
, ptid_t ptid
)
618 return find_thread_ptid (targ
, ptid
) != nullptr;
621 /* Finds the first thread of the inferior. */
624 first_thread_of_inferior (inferior
*inf
)
626 gdb_assert (!inf
->thread_map
.empty ());
628 auto compare_by_per_inf_num
= [] (const ptid_thread_map::value_type
&a
,
629 const ptid_thread_map::value_type
&b
)
631 return a
.second
->per_inf_num
< b
.second
->per_inf_num
;
633 auto it
= std::min_element (inf
->thread_map
.begin (), inf
->thread_map
.end (),
634 compare_by_per_inf_num
);
640 any_thread_of_inferior (inferior
*inf
)
642 gdb_assert (inf
->pid
!= 0);
644 /* Prefer the current thread. */
645 if (inf
== current_inferior ())
646 return inferior_thread ();
648 for (thread_info
*tp
: inf
->non_exited_threads ())
655 any_live_thread_of_inferior (inferior
*inf
)
657 struct thread_info
*curr_tp
= NULL
;
658 struct thread_info
*tp_executing
= NULL
;
660 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
662 /* Prefer the current thread if it's not executing. */
663 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
665 /* If the current thread is dead, forget it. If it's not
666 executing, use it. Otherwise, still choose it (below), but
667 only if no other non-executing thread is found. */
668 curr_tp
= inferior_thread ();
669 if (curr_tp
->state
== THREAD_EXITED
)
671 else if (!curr_tp
->executing
)
675 for (thread_info
*tp
: inf
->non_exited_threads ())
683 /* If both the current thread and all live threads are executing,
684 prefer the current thread. */
688 /* Otherwise, just return an executing thread, if any. */
692 /* Return true if TP is an active thread. */
694 thread_alive (thread_info
*tp
)
696 if (tp
->state
== THREAD_EXITED
)
699 /* Ensure we're looking at the right target stack. */
700 gdb_assert (tp
->inf
== current_inferior ());
702 return target_thread_alive (tp
->ptid
);
705 /* Switch to thread TP if it is alive. Returns true if successfully
706 switched, false otherwise. */
709 switch_to_thread_if_alive (thread_info
*thr
)
711 scoped_restore_current_thread restore_thread
;
713 /* Switch inferior first, so that we're looking at the right target
715 switch_to_inferior_no_thread (thr
->inf
);
717 if (thread_alive (thr
))
719 switch_to_thread (thr
);
720 restore_thread
.dont_restore ();
727 /* See gdbthreads.h. */
732 scoped_restore_current_thread restore_thread
;
734 for (thread_info
*tp
: all_threads_safe ())
736 switch_to_inferior_no_thread (tp
->inf
);
738 if (!thread_alive (tp
))
743 /* See gdbthreads.h. */
746 delete_exited_threads (void)
748 for (thread_info
*tp
: all_threads_safe ())
749 if (tp
->state
== THREAD_EXITED
)
753 /* Return true value if stack temporaries are enabled for the thread
757 thread_stack_temporaries_enabled_p (thread_info
*tp
)
762 return tp
->stack_temporaries_enabled
;
765 /* Push V on to the stack temporaries of the thread with id PTID. */
768 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
770 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
771 tp
->stack_temporaries
.push_back (v
);
774 /* Return true if VAL is among the stack temporaries of the thread
775 TP. Return false otherwise. */
778 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
780 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
781 for (value
*v
: tp
->stack_temporaries
)
788 /* Return the last of the stack temporaries for thread with id PTID.
789 Return NULL if there are no stack temporaries for the thread. */
792 get_last_thread_stack_temporary (thread_info
*tp
)
794 struct value
*lastval
= NULL
;
796 gdb_assert (tp
!= NULL
);
797 if (!tp
->stack_temporaries
.empty ())
798 lastval
= tp
->stack_temporaries
.back ();
804 thread_change_ptid (process_stratum_target
*targ
,
805 ptid_t old_ptid
, ptid_t new_ptid
)
807 struct inferior
*inf
;
808 struct thread_info
*tp
;
810 /* It can happen that what we knew as the target inferior id
811 changes. E.g, target remote may only discover the remote process
812 pid after adding the inferior to GDB's list. */
813 inf
= find_inferior_ptid (targ
, old_ptid
);
814 inf
->pid
= new_ptid
.pid ();
816 tp
= find_thread_ptid (inf
, old_ptid
);
817 gdb_assert (tp
!= nullptr);
819 int num_erased
= inf
->thread_map
.erase (old_ptid
);
820 gdb_assert (num_erased
== 1);
823 inf
->thread_map
[new_ptid
] = tp
;
825 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
828 /* See gdbthread.h. */
831 set_resumed (process_stratum_target
*targ
, ptid_t ptid
, bool resumed
)
833 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
834 tp
->resumed
= resumed
;
837 /* Helper for set_running, that marks one thread either running or
841 set_running_thread (struct thread_info
*tp
, int running
)
845 if (running
&& tp
->state
== THREAD_STOPPED
)
847 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
851 /* If the thread is now marked stopped, remove it from
852 the step-over queue, so that we don't try to resume
853 it until the user wants it to. */
854 if (tp
->step_over_next
!= NULL
)
855 thread_step_over_chain_remove (tp
);
861 /* See gdbthread.h. */
864 thread_info::set_running (bool running
)
866 if (set_running_thread (this, running
))
867 gdb::observers::target_resumed
.notify (this->ptid
);
871 set_running (process_stratum_target
*targ
, ptid_t ptid
, bool running
)
873 /* We try not to notify the observer if no thread has actually
874 changed the running state -- merely to reduce the number of
875 messages to the MI frontend. A frontend is supposed to handle
876 multiple *running notifications just fine. */
877 bool any_started
= false;
879 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
880 if (set_running_thread (tp
, running
))
884 gdb::observers::target_resumed
.notify (ptid
);
888 /* Helper for set_executing. Set's the thread's 'executing' field
889 from EXECUTING, and if EXECUTING is true also clears the thread's
893 set_executing_thread (thread_info
*thr
, bool executing
)
895 thr
->executing
= executing
;
897 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
901 set_executing (process_stratum_target
*targ
, ptid_t ptid
, bool executing
)
903 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
904 set_executing_thread (tp
, executing
);
906 /* It only takes one running thread to spawn more threads. */
908 targ
->threads_executing
= true;
909 /* Only clear the flag if the caller is telling us everything is
911 else if (minus_one_ptid
== ptid
)
912 targ
->threads_executing
= false;
915 /* See gdbthread.h. */
918 threads_are_executing (process_stratum_target
*target
)
920 return target
->threads_executing
;
924 set_stop_requested (process_stratum_target
*targ
, ptid_t ptid
, bool stop
)
926 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
927 tp
->stop_requested
= stop
;
929 /* Call the stop requested observer so other components of GDB can
930 react to this request. */
932 gdb::observers::thread_stop_requested
.notify (ptid
);
936 finish_thread_state (process_stratum_target
*targ
, ptid_t ptid
)
938 bool any_started
= false;
940 for (thread_info
*tp
: all_non_exited_threads (targ
, ptid
))
941 if (set_running_thread (tp
, tp
->executing
))
945 gdb::observers::target_resumed
.notify (ptid
);
948 /* See gdbthread.h. */
951 validate_registers_access (void)
953 /* No selected thread, no registers. */
954 if (inferior_ptid
== null_ptid
)
955 error (_("No thread selected."));
957 thread_info
*tp
= inferior_thread ();
959 /* Don't try to read from a dead thread. */
960 if (tp
->state
== THREAD_EXITED
)
961 error (_("The current thread has terminated"));
963 /* ... or from a spinning thread. FIXME: This isn't actually fully
964 correct. It'll allow an user-requested access (e.g., "print $pc"
965 at the prompt) when a thread is not executing for some internal
966 reason, but is marked running from the user's perspective. E.g.,
967 the thread is waiting for its turn in the step-over queue. */
969 error (_("Selected thread is running."));
972 /* See gdbthread.h. */
975 can_access_registers_thread (thread_info
*thread
)
977 /* No thread, no registers. */
981 /* Don't try to read from a dead thread. */
982 if (thread
->state
== THREAD_EXITED
)
985 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
986 if (thread
->executing
)
993 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
995 return (pc
>= thread
->control
.step_range_start
996 && pc
< thread
->control
.step_range_end
);
999 /* Helper for print_thread_info. Returns true if THR should be
1000 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1001 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1002 is true if REQUESTED_THREADS is list of global IDs, false if a list
1003 of per-inferior thread ids. If PID is not -1, only print THR if it
1004 is a thread from the process PID. Otherwise, threads from all
1005 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1006 and PID is not -1, then the thread is printed if it belongs to the
1007 specified process. Otherwise, an error is raised. */
1010 should_print_thread (const char *requested_threads
, int default_inf_num
,
1011 int global_ids
, int pid
, struct thread_info
*thr
)
1013 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1018 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1020 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1021 thr
->inf
->num
, thr
->per_inf_num
);
1026 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
1028 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1029 error (_("Requested thread not found in requested process"));
1033 if (thr
->state
== THREAD_EXITED
)
1039 /* Return the string to display in "info threads"'s "Target Id"
1043 thread_target_id_str (thread_info
*tp
)
1045 std::string target_id
= target_pid_to_str (tp
->ptid
);
1046 const char *extra_info
= target_extra_thread_info (tp
);
1047 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1049 if (extra_info
!= nullptr && name
!= nullptr)
1050 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1052 else if (extra_info
!= nullptr)
1053 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1054 else if (name
!= nullptr)
1055 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1060 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1061 whether REQUESTED_THREADS is a list of global or per-inferior
1065 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1066 int global_ids
, int pid
,
1067 int show_global_ids
)
1069 int default_inf_num
= current_inferior ()->num
;
1071 update_thread_list ();
1073 /* Whether we saw any thread. */
1074 bool any_thread
= false;
1075 /* Whether the current thread is exited. */
1076 bool current_exited
= false;
1078 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1079 ? inferior_thread () : NULL
);
1082 /* For backward compatibility, we make a list for MI. A table is
1083 preferable for the CLI, though, because it shows table
1085 gdb::optional
<ui_out_emit_list
> list_emitter
;
1086 gdb::optional
<ui_out_emit_table
> table_emitter
;
1088 /* We'll be switching threads temporarily below. */
1089 scoped_restore_current_thread restore_thread
;
1091 if (uiout
->is_mi_like_p ())
1092 list_emitter
.emplace (uiout
, "threads");
1096 /* The width of the "Target Id" column. Grown below to
1097 accommodate the largest entry. */
1098 size_t target_id_col_width
= 17;
1100 for (thread_info
*tp
: all_threads ())
1102 if (!should_print_thread (requested_threads
, default_inf_num
,
1103 global_ids
, pid
, tp
))
1106 if (!uiout
->is_mi_like_p ())
1108 /* Switch inferiors so we're looking at the right
1110 switch_to_inferior_no_thread (tp
->inf
);
1113 = std::max (target_id_col_width
,
1114 thread_target_id_str (tp
).size ());
1122 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1123 uiout
->message (_("No threads.\n"));
1125 uiout
->message (_("No threads match '%s'.\n"),
1130 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1131 n_threads
, "threads");
1133 uiout
->table_header (1, ui_left
, "current", "");
1134 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1135 if (show_global_ids
)
1136 uiout
->table_header (4, ui_left
, "id", "GId");
1137 uiout
->table_header (target_id_col_width
, ui_left
,
1138 "target-id", "Target Id");
1139 uiout
->table_header (1, ui_left
, "frame", "Frame");
1140 uiout
->table_body ();
1143 for (inferior
*inf
: all_inferiors ())
1145 /* Print the threads in per-inferior number order. */
1146 std::vector
<thread_info
*> threads_to_print
;
1148 for (thread_info
*tp
: inf
->threads ())
1149 threads_to_print
.push_back (tp
);
1151 std::sort (threads_to_print
.begin (), threads_to_print
.end (),
1152 [] (thread_info
*a
, thread_info
*b
)
1154 return a
->per_inf_num
< b
->per_inf_num
;
1157 for (thread_info
*tp
: threads_to_print
)
1162 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1163 current_exited
= true;
1165 if (!should_print_thread (requested_threads
, default_inf_num
,
1166 global_ids
, pid
, tp
))
1169 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1171 if (!uiout
->is_mi_like_p ())
1173 if (tp
== current_thread
)
1174 uiout
->field_string ("current", "*");
1176 uiout
->field_skip ("current");
1178 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1181 if (show_global_ids
|| uiout
->is_mi_like_p ())
1182 uiout
->field_signed ("id", tp
->global_num
);
1184 /* Switch to the thread (and inferior / target). */
1185 switch_to_thread (tp
);
1187 /* For the CLI, we stuff everything into the target-id field.
1188 This is a gross hack to make the output come out looking
1189 correct. The underlying problem here is that ui-out has no
1190 way to specify that a field's space allocation should be
1191 shared by several fields. For MI, we do the right thing
1194 if (uiout
->is_mi_like_p ())
1196 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1198 const char *extra_info
= target_extra_thread_info (tp
);
1199 if (extra_info
!= nullptr)
1200 uiout
->field_string ("details", extra_info
);
1202 const char *name
= (tp
->name
!= nullptr
1204 : target_thread_name (tp
));
1206 uiout
->field_string ("name", name
);
1210 uiout
->field_string ("target-id",
1211 thread_target_id_str (tp
).c_str ());
1214 if (tp
->state
== THREAD_RUNNING
)
1215 uiout
->text ("(running)\n");
1218 /* The switch above put us at the top of the stack (leaf
1220 print_stack_frame (get_selected_frame (NULL
),
1221 /* For MI output, print frame level. */
1222 uiout
->is_mi_like_p (),
1226 if (uiout
->is_mi_like_p ())
1228 const char *state
= "stopped";
1230 if (tp
->state
== THREAD_RUNNING
)
1232 uiout
->field_string ("state", state
);
1235 core
= target_core_of_thread (tp
->ptid
);
1236 if (uiout
->is_mi_like_p () && core
!= -1)
1237 uiout
->field_signed ("core", core
);
1241 /* This end scope restores the current thread and the frame
1242 selected before the "info threads" command, and it finishes the
1243 ui-out list or table. */
1246 if (pid
== -1 && requested_threads
== NULL
)
1248 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1249 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1251 if (inferior_ptid
!= null_ptid
&& current_exited
)
1252 uiout
->message ("\n\
1253 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1254 print_thread_id (inferior_thread ()));
1255 else if (any_thread
&& inferior_ptid
== null_ptid
)
1256 uiout
->message ("\n\
1257 No selected thread. See `help thread'.\n");
1261 /* See gdbthread.h. */
1264 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1267 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1270 /* The options for the "info threads" command. */
1272 struct info_threads_opts
1275 bool show_global_ids
= false;
1278 static const gdb::option::option_def info_threads_option_defs
[] = {
1280 gdb::option::flag_option_def
<info_threads_opts
> {
1282 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1283 N_("Show global thread IDs."),
1288 /* Create an option_def_group for the "info threads" options, with
1289 IT_OPTS as context. */
1291 static inline gdb::option::option_def_group
1292 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1294 return {{info_threads_option_defs
}, it_opts
};
1297 /* Implementation of the "info threads" command.
1299 Note: this has the drawback that it _really_ switches
1300 threads, which frees the frame cache. A no-side
1301 effects info-threads command would be nicer. */
1304 info_threads_command (const char *arg
, int from_tty
)
1306 info_threads_opts it_opts
;
1308 auto grp
= make_info_threads_options_def_group (&it_opts
);
1309 gdb::option::process_options
1310 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1312 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1315 /* Completer for the "info threads" command. */
1318 info_threads_command_completer (struct cmd_list_element
*ignore
,
1319 completion_tracker
&tracker
,
1320 const char *text
, const char *word_ignored
)
1322 const auto grp
= make_info_threads_options_def_group (nullptr);
1324 if (gdb::option::complete_options
1325 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1328 /* Convenience to let the user know what the option can accept. */
1331 gdb::option::complete_on_all_options (tracker
, grp
);
1332 /* Keep this "ID" in sync with what "help info threads"
1334 tracker
.add_completion (make_unique_xstrdup ("ID"));
1338 /* See gdbthread.h. */
1341 switch_to_thread_no_regs (struct thread_info
*thread
)
1343 struct inferior
*inf
= thread
->inf
;
1345 set_current_program_space (inf
->pspace
);
1346 set_current_inferior (inf
);
1348 inferior_ptid
= thread
->ptid
;
1351 /* See gdbthread.h. */
1354 switch_to_no_thread ()
1356 if (inferior_ptid
== null_ptid
)
1359 inferior_ptid
= null_ptid
;
1360 reinit_frame_cache ();
1363 /* See gdbthread.h. */
1366 switch_to_thread (thread_info
*thr
)
1368 gdb_assert (thr
!= NULL
);
1370 if (is_current_thread (thr
))
1373 switch_to_thread_no_regs (thr
);
1375 reinit_frame_cache ();
1378 /* See gdbsupport/common-gdbthread.h. */
1381 switch_to_thread (process_stratum_target
*proc_target
, ptid_t ptid
)
1383 thread_info
*thr
= find_thread_ptid (proc_target
, ptid
);
1384 switch_to_thread (thr
);
1388 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1390 struct frame_info
*frame
= NULL
;
1393 /* This means there was no selected frame. */
1394 if (frame_level
== -1)
1396 select_frame (NULL
);
1400 gdb_assert (frame_level
>= 0);
1402 /* Restore by level first, check if the frame id is the same as
1403 expected. If that fails, try restoring by frame id. If that
1404 fails, nothing to do, just warn the user. */
1406 count
= frame_level
;
1407 frame
= find_relative_frame (get_current_frame (), &count
);
1410 /* The frame ids must match - either both valid or both outer_frame_id.
1411 The latter case is not failsafe, but since it's highly unlikely
1412 the search by level finds the wrong frame, it's 99.9(9)% of
1413 the time (for all practical purposes) safe. */
1414 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1416 /* Cool, all is fine. */
1417 select_frame (frame
);
1421 frame
= frame_find_by_id (a_frame_id
);
1424 /* Cool, refound it. */
1425 select_frame (frame
);
1429 /* Nothing else to do, the frame layout really changed. Select the
1430 innermost stack frame. */
1431 select_frame (get_current_frame ());
1433 /* Warn the user. */
1434 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1436 warning (_("Couldn't restore frame #%d in "
1437 "current thread. Bottom (innermost) frame selected:"),
1439 /* For MI, we should probably have a notification about
1440 current frame change. But this error is not very
1441 likely, so don't bother for now. */
1442 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1447 scoped_restore_current_thread::restore ()
1449 /* If an entry of thread_info was previously selected, it won't be
1450 deleted because we've increased its refcount. The thread represented
1451 by this thread_info entry may have already exited (due to normal exit,
1452 detach, etc), so the thread_info.state is THREAD_EXITED. */
1453 if (m_thread
!= NULL
1454 /* If the previously selected thread belonged to a process that has
1455 in the mean time exited (or killed, detached, etc.), then don't revert
1456 back to it, but instead simply drop back to no thread selected. */
1458 switch_to_thread (m_thread
);
1461 switch_to_no_thread ();
1462 set_current_inferior (m_inf
);
1465 /* The running state of the originally selected thread may have
1466 changed, so we have to recheck it here. */
1467 if (inferior_ptid
!= null_ptid
1469 && m_thread
->state
== THREAD_STOPPED
1470 && target_has_registers
1472 && target_has_memory
)
1473 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1476 scoped_restore_current_thread::~scoped_restore_current_thread ()
1478 if (!m_dont_restore
)
1484 catch (const gdb_exception
&ex
)
1486 /* We're in a dtor, there's really nothing else we can do
1487 but swallow the exception. */
1491 if (m_thread
!= NULL
)
1492 m_thread
->decref ();
1496 scoped_restore_current_thread::scoped_restore_current_thread ()
1499 m_inf
= current_inferior ();
1501 if (inferior_ptid
!= null_ptid
)
1503 thread_info
*tp
= inferior_thread ();
1504 struct frame_info
*frame
;
1506 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1508 && target_has_registers
1510 && target_has_memory
)
1512 /* When processing internal events, there might not be a
1513 selected frame. If we naively call get_selected_frame
1514 here, then we can end up reading debuginfo for the
1515 current frame, but we don't generally need the debuginfo
1517 frame
= get_selected_frame_if_set ();
1522 m_selected_frame_id
= get_frame_id (frame
);
1523 m_selected_frame_level
= frame_relative_level (frame
);
1532 /* See gdbthread.h. */
1535 show_thread_that_caused_stop (void)
1537 return highest_thread_num
> 1;
1540 /* See gdbthread.h. */
1543 show_inferior_qualified_tids (void)
1545 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1548 /* See gdbthread.h. */
1551 print_thread_id (struct thread_info
*thr
)
1553 char *s
= get_print_cell ();
1555 if (show_inferior_qualified_tids ())
1556 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1558 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1562 /* Sort an array of struct thread_info pointers by thread ID (first by
1563 inferior number, and then by per-inferior thread number). Sorts in
1567 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1569 if (a
->inf
->num
!= b
->inf
->num
)
1570 return a
->inf
->num
< b
->inf
->num
;
1572 return (a
->per_inf_num
< b
->per_inf_num
);
1575 /* Sort an array of struct thread_info pointers by thread ID (first by
1576 inferior number, and then by per-inferior thread number). Sorts in
1577 descending order. */
1580 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1582 if (a
->inf
->num
!= b
->inf
->num
)
1583 return a
->inf
->num
> b
->inf
->num
;
1585 return (a
->per_inf_num
> b
->per_inf_num
);
1588 /* Switch to thread THR and execute CMD.
1589 FLAGS.QUIET controls the printing of the thread information.
1590 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1593 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1594 const qcs_flags
&flags
)
1596 switch_to_thread (thr
);
1599 std::string cmd_result
= execute_command_to_string
1600 (cmd
, from_tty
, gdb_stdout
->term_out ());
1601 if (!flags
.silent
|| cmd_result
.length () > 0)
1604 printf_filtered (_("\nThread %s (%s):\n"),
1605 print_thread_id (thr
),
1606 target_pid_to_str (inferior_ptid
).c_str ());
1607 printf_filtered ("%s", cmd_result
.c_str ());
1610 catch (const gdb_exception_error
&ex
)
1615 printf_filtered (_("\nThread %s (%s):\n"),
1616 print_thread_id (thr
),
1617 target_pid_to_str (inferior_ptid
).c_str ());
1619 printf_filtered ("%s\n", ex
.what ());
1626 /* Option definition of "thread apply"'s "-ascending" option. */
1628 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1631 Call COMMAND for all threads in ascending order.\n\
1632 The default is descending order."),
1635 /* The qcs command line flags for the "thread apply" commands. Keep
1636 this in sync with the "frame apply" commands. */
1638 using qcs_flag_option_def
1639 = gdb::option::flag_option_def
<qcs_flags
>;
1641 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1642 qcs_flag_option_def
{
1643 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1644 N_("Disables printing the thread information."),
1647 qcs_flag_option_def
{
1648 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1649 N_("Print any error raised by COMMAND and continue."),
1652 qcs_flag_option_def
{
1653 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1654 N_("Silently ignore any errors or empty output produced by COMMAND."),
1658 /* Create an option_def_group for the "thread apply all" options, with
1659 ASCENDING and FLAGS as context. */
1661 static inline std::array
<gdb::option::option_def_group
, 2>
1662 make_thread_apply_all_options_def_group (bool *ascending
,
1666 { {ascending_option_def
.def ()}, ascending
},
1667 { {thr_qcs_flags_option_defs
}, flags
},
1671 /* Create an option_def_group for the "thread apply" options, with
1672 FLAGS as context. */
1674 static inline gdb::option::option_def_group
1675 make_thread_apply_options_def_group (qcs_flags
*flags
)
1677 return {{thr_qcs_flags_option_defs
}, flags
};
1680 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1681 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1682 of two numbers separated by a hyphen. Examples:
1684 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1685 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1686 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1689 thread_apply_all_command (const char *cmd
, int from_tty
)
1691 bool ascending
= false;
1694 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1696 gdb::option::process_options
1697 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1699 validate_flags_qcs ("thread apply all", &flags
);
1701 if (cmd
== NULL
|| *cmd
== '\000')
1702 error (_("Please specify a command at the end of 'thread apply all'"));
1704 update_thread_list ();
1706 int tc
= live_threads_count ();
1709 /* Save a copy of the thread list and increment each thread's
1710 refcount while executing the command in the context of each
1711 thread, in case the command is one that wipes threads. E.g.,
1712 detach, kill, disconnect, etc., or even normally continuing
1713 over an inferior or thread exit. */
1714 std::vector
<thread_info
*> thr_list_cpy
;
1715 thr_list_cpy
.reserve (tc
);
1717 for (thread_info
*tp
: all_non_exited_threads ())
1718 thr_list_cpy
.push_back (tp
);
1719 gdb_assert (thr_list_cpy
.size () == tc
);
1721 /* Increment the refcounts, and restore them back on scope
1723 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1725 auto *sorter
= (ascending
1726 ? tp_array_compar_ascending
1727 : tp_array_compar_descending
);
1728 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1730 scoped_restore_current_thread restore_thread
;
1732 for (thread_info
*thr
: thr_list_cpy
)
1733 if (switch_to_thread_if_alive (thr
))
1734 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1738 /* Completer for "thread apply [ID list]". */
1741 thread_apply_command_completer (cmd_list_element
*ignore
,
1742 completion_tracker
&tracker
,
1743 const char *text
, const char * /*word*/)
1745 /* Don't leave this to complete_options because there's an early
1747 tracker
.set_use_custom_word_point (true);
1749 tid_range_parser parser
;
1750 parser
.init (text
, current_inferior ()->num
);
1754 while (!parser
.finished ())
1756 int inf_num
, thr_start
, thr_end
;
1758 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1761 if (parser
.in_star_range () || parser
.in_thread_range ())
1762 parser
.skip_range ();
1765 catch (const gdb_exception_error
&ex
)
1767 /* get_tid_range throws if it parses a negative number, for
1768 example. But a seemingly negative number may be the start of
1769 an option instead. */
1772 const char *cmd
= parser
.cur_tok ();
1776 /* No thread ID list yet. */
1780 /* Check if we're past a valid thread ID list already. */
1781 if (parser
.finished ()
1782 && cmd
> text
&& !isspace (cmd
[-1]))
1785 /* We're past the thread ID list, advance word point. */
1786 tracker
.advance_custom_word_point_by (cmd
- text
);
1789 const auto group
= make_thread_apply_options_def_group (nullptr);
1790 if (gdb::option::complete_options
1791 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1794 complete_nested_command_line (tracker
, text
);
1797 /* Completer for "thread apply all". */
1800 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1801 completion_tracker
&tracker
,
1802 const char *text
, const char *word
)
1804 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1806 if (gdb::option::complete_options
1807 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1810 complete_nested_command_line (tracker
, text
);
1813 /* Implementation of the "thread apply" command. */
1816 thread_apply_command (const char *tidlist
, int from_tty
)
1819 const char *cmd
= NULL
;
1820 tid_range_parser parser
;
1822 if (tidlist
== NULL
|| *tidlist
== '\000')
1823 error (_("Please specify a thread ID list"));
1825 parser
.init (tidlist
, current_inferior ()->num
);
1826 while (!parser
.finished ())
1828 int inf_num
, thr_start
, thr_end
;
1830 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1834 cmd
= parser
.cur_tok ();
1836 auto group
= make_thread_apply_options_def_group (&flags
);
1837 gdb::option::process_options
1838 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1840 validate_flags_qcs ("thread apply", &flags
);
1843 error (_("Please specify a command following the thread ID list"));
1845 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1846 invalid_thread_id_error (cmd
);
1848 scoped_restore_current_thread restore_thread
;
1850 parser
.init (tidlist
, current_inferior ()->num
);
1851 while (!parser
.finished ())
1853 struct thread_info
*tp
= NULL
;
1854 struct inferior
*inf
;
1855 int inf_num
, thr_num
;
1857 parser
.get_tid (&inf_num
, &thr_num
);
1858 inf
= find_inferior_id (inf_num
);
1860 tp
= find_thread_id (inf
, thr_num
);
1862 if (parser
.in_star_range ())
1866 warning (_("Unknown inferior %d"), inf_num
);
1867 parser
.skip_range ();
1871 /* No use looking for threads past the highest thread number
1872 the inferior ever had. */
1873 if (thr_num
>= inf
->highest_thread_num
)
1874 parser
.skip_range ();
1876 /* Be quiet about unknown threads numbers. */
1883 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1884 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1886 warning (_("Unknown thread %d"), thr_num
);
1890 if (!switch_to_thread_if_alive (tp
))
1892 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1896 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1901 /* Implementation of the "taas" command. */
1904 taas_command (const char *cmd
, int from_tty
)
1906 if (cmd
== NULL
|| *cmd
== '\0')
1907 error (_("Please specify a command to apply on all threads"));
1908 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1909 execute_command (expanded
.c_str (), from_tty
);
1912 /* Implementation of the "tfaas" command. */
1915 tfaas_command (const char *cmd
, int from_tty
)
1917 if (cmd
== NULL
|| *cmd
== '\0')
1918 error (_("Please specify a command to apply on all frames of all threads"));
1919 std::string expanded
1920 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1921 execute_command (expanded
.c_str (), from_tty
);
1924 /* Switch to the specified thread, or print the current thread. */
1927 thread_command (const char *tidstr
, int from_tty
)
1931 if (inferior_ptid
== null_ptid
)
1932 error (_("No thread selected"));
1934 if (target_has_stack
)
1936 struct thread_info
*tp
= inferior_thread ();
1938 if (tp
->state
== THREAD_EXITED
)
1939 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1940 print_thread_id (tp
),
1941 target_pid_to_str (inferior_ptid
).c_str ());
1943 printf_filtered (_("[Current thread is %s (%s)]\n"),
1944 print_thread_id (tp
),
1945 target_pid_to_str (inferior_ptid
).c_str ());
1948 error (_("No stack."));
1952 ptid_t previous_ptid
= inferior_ptid
;
1954 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1956 /* Print if the thread has not changed, otherwise an event will
1958 if (inferior_ptid
== previous_ptid
)
1960 print_selected_thread_frame (current_uiout
,
1961 USER_SELECTED_THREAD
1962 | USER_SELECTED_FRAME
);
1966 gdb::observers::user_selected_context_changed
.notify
1967 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1972 /* Implementation of `thread name'. */
1975 thread_name_command (const char *arg
, int from_tty
)
1977 struct thread_info
*info
;
1979 if (inferior_ptid
== null_ptid
)
1980 error (_("No thread selected"));
1982 arg
= skip_spaces (arg
);
1984 info
= inferior_thread ();
1986 info
->name
= arg
? xstrdup (arg
) : NULL
;
1989 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1992 thread_find_command (const char *arg
, int from_tty
)
1995 unsigned long match
= 0;
1997 if (arg
== NULL
|| *arg
== '\0')
1998 error (_("Command requires an argument."));
2000 tmp
= re_comp (arg
);
2002 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
2004 update_thread_list ();
2005 for (thread_info
*tp
: all_threads ())
2007 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
2009 printf_filtered (_("Thread %s has name '%s'\n"),
2010 print_thread_id (tp
), tp
->name
);
2014 tmp
= target_thread_name (tp
);
2015 if (tmp
!= NULL
&& re_exec (tmp
))
2017 printf_filtered (_("Thread %s has target name '%s'\n"),
2018 print_thread_id (tp
), tmp
);
2022 std::string name
= target_pid_to_str (tp
->ptid
);
2023 if (!name
.empty () && re_exec (name
.c_str ()))
2025 printf_filtered (_("Thread %s has target id '%s'\n"),
2026 print_thread_id (tp
), name
.c_str ());
2030 tmp
= target_extra_thread_info (tp
);
2031 if (tmp
!= NULL
&& re_exec (tmp
))
2033 printf_filtered (_("Thread %s has extra info '%s'\n"),
2034 print_thread_id (tp
), tmp
);
2039 printf_filtered (_("No threads match '%s'\n"), arg
);
2042 /* Print notices when new threads are attached and detached. */
2043 bool print_thread_events
= true;
2045 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2046 struct cmd_list_element
*c
, const char *value
)
2048 fprintf_filtered (file
,
2049 _("Printing of thread events is %s.\n"),
2053 /* See gdbthread.h. */
2056 thread_select (const char *tidstr
, thread_info
*tp
)
2058 if (!switch_to_thread_if_alive (tp
))
2059 error (_("Thread ID %s has terminated."), tidstr
);
2061 annotate_thread_changed ();
2063 /* Since the current thread may have changed, see if there is any
2064 exited thread we can now delete. */
2068 /* Print thread and frame switch command response. */
2071 print_selected_thread_frame (struct ui_out
*uiout
,
2072 user_selected_what selection
)
2074 struct thread_info
*tp
= inferior_thread ();
2076 if (selection
& USER_SELECTED_THREAD
)
2078 if (uiout
->is_mi_like_p ())
2080 uiout
->field_signed ("new-thread-id",
2081 inferior_thread ()->global_num
);
2085 uiout
->text ("[Switching to thread ");
2086 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2088 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2093 if (tp
->state
== THREAD_RUNNING
)
2095 if (selection
& USER_SELECTED_THREAD
)
2096 uiout
->text ("(running)\n");
2098 else if (selection
& USER_SELECTED_FRAME
)
2100 if (selection
& USER_SELECTED_THREAD
)
2103 if (has_stack_frames ())
2104 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2109 /* Update the 'threads_executing' global based on the threads we know
2110 about right now. This is used by infrun to tell whether we should
2111 pull events out of the current target. */
2114 update_threads_executing (void)
2116 process_stratum_target
*targ
= current_inferior ()->process_target ();
2121 targ
->threads_executing
= false;
2123 for (inferior
*inf
: all_non_exited_inferiors (targ
))
2125 if (!inf
->has_execution ())
2128 /* If the process has no threads, then it must be we have a
2129 process-exit event pending. */
2130 if (inf
->thread_map
.empty ())
2132 targ
->threads_executing
= true;
2136 for (thread_info
*tp
: inf
->non_exited_threads ())
2140 targ
->threads_executing
= true;
2148 update_thread_list (void)
2150 target_update_thread_list ();
2151 update_threads_executing ();
2154 /* Return a new value for the selected thread's id. Return a value of
2155 0 if no thread is selected. If GLOBAL is true, return the thread's
2156 global number. Otherwise return the per-inferior number. */
2158 static struct value
*
2159 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2163 if (inferior_ptid
== null_ptid
)
2167 thread_info
*tp
= inferior_thread ();
2169 int_val
= tp
->global_num
;
2171 int_val
= tp
->per_inf_num
;
2174 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2177 /* Return a new value for the selected thread's per-inferior thread
2178 number. Return a value of 0 if no thread is selected, or no
2181 static struct value
*
2182 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2183 struct internalvar
*var
,
2186 return thread_num_make_value_helper (gdbarch
, 0);
2189 /* Return a new value for the selected thread's global id. Return a
2190 value of 0 if no thread is selected, or no threads exist. */
2192 static struct value
*
2193 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2196 return thread_num_make_value_helper (gdbarch
, 1);
2199 /* Commands with a prefix of `thread'. */
2200 struct cmd_list_element
*thread_cmd_list
= NULL
;
2202 /* Implementation of `thread' variable. */
2204 static const struct internalvar_funcs thread_funcs
=
2206 thread_id_per_inf_num_make_value
,
2211 /* Implementation of `gthread' variable. */
2213 static const struct internalvar_funcs gthread_funcs
=
2215 global_thread_id_make_value
,
2221 _initialize_thread (void)
2223 static struct cmd_list_element
*thread_apply_list
= NULL
;
2224 cmd_list_element
*c
;
2226 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2228 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2230 static std::string info_threads_help
2231 = gdb::option::build_help (_("\
2232 Display currently known threads.\n\
2233 Usage: info threads [OPTION]... [ID]...\n\
2237 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2238 Otherwise, all threads are displayed."),
2241 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2242 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2244 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2245 Use this command to switch between threads.\n\
2246 The new thread ID must be currently known."),
2247 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2249 #define THREAD_APPLY_OPTION_HELP "\
2250 Prints per-inferior thread number and target system's thread id\n\
2251 followed by COMMAND output.\n\
2253 By default, an error raised during the execution of COMMAND\n\
2254 aborts \"thread apply\".\n\
2259 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2261 static std::string thread_apply_help
= gdb::option::build_help (_("\
2262 Apply a command to a list of threads.\n\
2263 Usage: thread apply ID... [OPTION]... COMMAND\n\
2264 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2265 THREAD_APPLY_OPTION_HELP
),
2268 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2269 thread_apply_help
.c_str (),
2270 &thread_apply_list
, "thread apply ", 1,
2272 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2274 const auto thread_apply_all_opts
2275 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2277 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2278 Apply a command to all threads.\n\
2280 Usage: thread apply all [OPTION]... COMMAND\n"
2281 THREAD_APPLY_OPTION_HELP
),
2282 thread_apply_all_opts
);
2284 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2285 thread_apply_all_help
.c_str (),
2286 &thread_apply_list
);
2287 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2289 c
= add_com ("taas", class_run
, taas_command
, _("\
2290 Apply a command to all threads (ignoring errors and empty output).\n\
2291 Usage: taas [OPTION]... COMMAND\n\
2292 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2293 See \"help thread apply all\" for available options."));
2294 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2296 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2297 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2298 Usage: tfaas [OPTION]... COMMAND\n\
2299 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2300 See \"help frame apply all\" for available options."));
2301 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2303 add_cmd ("name", class_run
, thread_name_command
,
2304 _("Set the current thread's name.\n\
2305 Usage: thread name [NAME]\n\
2306 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2308 add_cmd ("find", class_run
, thread_find_command
, _("\
2309 Find threads that match a regular expression.\n\
2310 Usage: thread find REGEXP\n\
2311 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2314 add_com_alias ("t", "thread", class_run
, 1);
2316 add_setshow_boolean_cmd ("thread-events", no_class
,
2317 &print_thread_events
, _("\
2318 Set printing of thread events (such as thread start and exit)."), _("\
2319 Show printing of thread events (such as thread start and exit)."), NULL
,
2321 show_print_thread_events
,
2322 &setprintlist
, &showprintlist
);
2324 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2325 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);