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 thread_step_over_chain_remove (tp
);
212 if (tp
->state
!= THREAD_EXITED
)
214 gdb::observers::thread_exit
.notify (tp
, silent
);
216 /* Tag it as exited. */
217 tp
->state
= THREAD_EXITED
;
219 /* Clear breakpoints, etc. associated with this thread. */
220 clear_thread_inferior_resources (tp
);
225 init_thread_list (void)
227 highest_thread_num
= 0;
229 for (thread_info
*tp
: all_threads_safe ())
231 inferior
*inf
= tp
->inf
;
233 if (tp
->deletable ())
236 set_thread_exited (tp
, 1);
238 inf
->thread_list
= NULL
;
242 /* Allocate a new thread of inferior INF with target id PTID and add
243 it to the thread list. */
245 static struct thread_info
*
246 new_thread (struct inferior
*inf
, ptid_t ptid
)
248 thread_info
*tp
= new thread_info (inf
, ptid
);
250 if (inf
->thread_list
== NULL
)
251 inf
->thread_list
= tp
;
254 struct thread_info
*last
;
256 for (last
= inf
->thread_list
; last
->next
!= NULL
; last
= last
->next
)
265 add_thread_silent (ptid_t ptid
)
267 struct inferior
*inf
= find_inferior_ptid (ptid
);
268 gdb_assert (inf
!= NULL
);
270 thread_info
*tp
= find_thread_ptid (inf
, ptid
);
272 /* Found an old thread with the same id. It has to be dead,
273 otherwise we wouldn't be adding a new thread with the same id.
274 The OS is reusing this id --- delete it, and recreate a new
277 /* In addition to deleting the thread, if this is the current
278 thread, then we need to take care that delete_thread doesn't
279 really delete the thread if it is inferior_ptid. Create a
280 new template thread in the list with an invalid ptid, switch
281 to it, delete the original thread, reset the new thread's
282 ptid, and switch to it. */
284 if (inferior_ptid
== ptid
)
286 thread_info
*new_thr
= new_thread (inf
, null_ptid
);
288 /* Make switch_to_thread not read from the thread. */
289 new_thr
->state
= THREAD_EXITED
;
290 switch_to_no_thread ();
292 /* Now we can delete it. */
295 /* Now reset its ptid, and reswitch inferior_ptid to it. */
296 new_thr
->ptid
= ptid
;
297 new_thr
->state
= THREAD_STOPPED
;
298 switch_to_thread (new_thr
);
300 gdb::observers::new_thread
.notify (new_thr
);
306 /* Just go ahead and delete it. */
310 tp
= new_thread (inf
, ptid
);
311 gdb::observers::new_thread
.notify (tp
);
317 add_thread_with_info (ptid_t ptid
, private_thread_info
*priv
)
319 struct thread_info
*result
= add_thread_silent (ptid
);
321 result
->priv
.reset (priv
);
323 if (print_thread_events
)
324 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
).c_str ());
326 annotate_new_thread ();
331 add_thread (ptid_t ptid
)
333 return add_thread_with_info (ptid
, NULL
);
336 private_thread_info::~private_thread_info () = default;
338 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
339 : ptid (ptid_
), inf (inf_
)
341 gdb_assert (inf_
!= NULL
);
343 this->global_num
= ++highest_thread_num
;
344 this->per_inf_num
= ++inf_
->highest_thread_num
;
346 /* Nothing to follow yet. */
347 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
348 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
349 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
352 thread_info::~thread_info ()
357 /* See gdbthread.h. */
360 thread_info::deletable () const
362 /* If this is the current thread, or there's code out there that
363 relies on it existing (refcount > 0) we can't delete yet. */
364 return refcount () == 0 && ptid
!= inferior_ptid
;
367 /* Add TP to the end of the step-over chain LIST_P. */
370 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
372 gdb_assert (tp
->step_over_next
== NULL
);
373 gdb_assert (tp
->step_over_prev
== NULL
);
378 tp
->step_over_prev
= tp
->step_over_next
= tp
;
382 struct thread_info
*head
= *list_p
;
383 struct thread_info
*tail
= head
->step_over_prev
;
385 tp
->step_over_prev
= tail
;
386 tp
->step_over_next
= head
;
387 head
->step_over_prev
= tp
;
388 tail
->step_over_next
= tp
;
392 /* Remove TP from step-over chain LIST_P. */
395 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
397 gdb_assert (tp
->step_over_next
!= NULL
);
398 gdb_assert (tp
->step_over_prev
!= NULL
);
402 if (tp
== tp
->step_over_next
)
405 *list_p
= tp
->step_over_next
;
408 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
409 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
410 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
413 /* See gdbthread.h. */
416 thread_step_over_chain_next (struct thread_info
*tp
)
418 struct thread_info
*next
= tp
->step_over_next
;
420 return (next
== step_over_queue_head
? NULL
: next
);
423 /* See gdbthread.h. */
426 thread_is_in_step_over_chain (struct thread_info
*tp
)
428 return (tp
->step_over_next
!= NULL
);
431 /* See gdbthread.h. */
434 thread_step_over_chain_enqueue (struct thread_info
*tp
)
436 step_over_chain_enqueue (&step_over_queue_head
, tp
);
439 /* See gdbthread.h. */
442 thread_step_over_chain_remove (struct thread_info
*tp
)
444 step_over_chain_remove (&step_over_queue_head
, tp
);
447 /* Delete the thread referenced by THR. If SILENT, don't notify
448 the observer of this exit.
450 THR must not be NULL or a failed assertion will be raised. */
453 delete_thread_1 (thread_info
*thr
, bool silent
)
455 gdb_assert (thr
!= nullptr);
457 struct thread_info
*tp
, *tpprev
= NULL
;
459 for (tp
= thr
->inf
->thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
466 set_thread_exited (tp
, silent
);
468 if (!tp
->deletable ())
470 /* Will be really deleted some other time. */
475 tpprev
->next
= tp
->next
;
477 tp
->inf
->thread_list
= tp
->next
;
482 /* Delete thread THREAD and notify of thread exit. If this is the
483 current thread, don't actually delete it, but tag it as exited and
484 do the notification. If this is the user selected thread, clear
488 delete_thread (thread_info
*thread
)
490 delete_thread_1 (thread
, false /* not silent */);
494 delete_thread_silent (thread_info
*thread
)
496 delete_thread_1 (thread
, true /* silent */);
500 find_thread_global_id (int global_id
)
502 for (thread_info
*tp
: all_threads ())
503 if (tp
->global_num
== global_id
)
509 static struct thread_info
*
510 find_thread_id (struct inferior
*inf
, int thr_num
)
512 for (thread_info
*tp
: inf
->threads ())
513 if (tp
->per_inf_num
== thr_num
)
519 /* Find a thread_info by matching PTID. */
522 find_thread_ptid (ptid_t ptid
)
524 inferior
*inf
= find_inferior_ptid (ptid
);
527 return find_thread_ptid (inf
, ptid
);
530 /* See gdbthread.h. */
533 find_thread_ptid (inferior
*inf
, ptid_t ptid
)
535 for (thread_info
*tp
: inf
->threads ())
536 if (tp
->ptid
== ptid
)
542 /* See gdbthread.h. */
545 find_thread_by_handle (gdb::array_view
<const gdb_byte
> handle
,
546 struct inferior
*inf
)
548 return target_thread_handle_to_thread_info (handle
.data (),
554 * Thread iterator function.
556 * Calls a callback function once for each thread, so long as
557 * the callback function returns false. If the callback function
558 * returns true, the iteration will end and the current thread
559 * will be returned. This can be useful for implementing a
560 * search for a thread with arbitrary attributes, or for applying
561 * some operation to every thread.
563 * FIXME: some of the existing functionality, such as
564 * "Thread apply all", might be rewritten using this functionality.
568 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
571 for (thread_info
*tp
: all_threads_safe ())
572 if ((*callback
) (tp
, data
))
578 /* See gdbthread.h. */
583 for (thread_info
*tp ATTRIBUTE_UNUSED
: all_threads ())
591 auto rng
= all_threads ();
592 return std::distance (rng
.begin (), rng
.end ());
595 /* Return the number of non-exited threads in the thread list. */
598 live_threads_count (void)
600 auto rng
= all_non_exited_threads ();
601 return std::distance (rng
.begin (), rng
.end ());
605 valid_global_thread_id (int global_id
)
607 for (thread_info
*tp
: all_threads ())
608 if (tp
->global_num
== global_id
)
615 in_thread_list (ptid_t ptid
)
617 return find_thread_ptid (ptid
) != nullptr;
620 /* Finds the first thread of the inferior. */
623 first_thread_of_inferior (inferior
*inf
)
625 return inf
->thread_list
;
629 any_thread_of_inferior (inferior
*inf
)
631 gdb_assert (inf
->pid
!= 0);
633 /* Prefer the current thread. */
634 if (inf
== current_inferior ())
635 return inferior_thread ();
637 for (thread_info
*tp
: inf
->non_exited_threads ())
644 any_live_thread_of_inferior (inferior
*inf
)
646 struct thread_info
*curr_tp
= NULL
;
647 struct thread_info
*tp_executing
= NULL
;
649 gdb_assert (inf
!= NULL
&& inf
->pid
!= 0);
651 /* Prefer the current thread if it's not executing. */
652 if (inferior_ptid
!= null_ptid
&& current_inferior () == inf
)
654 /* If the current thread is dead, forget it. If it's not
655 executing, use it. Otherwise, still choose it (below), but
656 only if no other non-executing thread is found. */
657 curr_tp
= inferior_thread ();
658 if (curr_tp
->state
== THREAD_EXITED
)
660 else if (!curr_tp
->executing
)
664 for (thread_info
*tp
: inf
->non_exited_threads ())
672 /* If both the current thread and all live threads are executing,
673 prefer the current thread. */
677 /* Otherwise, just return an executing thread, if any. */
681 /* Return true if TP is an active thread. */
683 thread_alive (struct thread_info
*tp
)
685 if (tp
->state
== THREAD_EXITED
)
687 if (!target_thread_alive (tp
->ptid
))
692 /* See gdbthreads.h. */
697 for (thread_info
*tp
: all_threads_safe ())
698 if (!thread_alive (tp
))
702 /* See gdbthreads.h. */
705 delete_exited_threads (void)
707 for (thread_info
*tp
: all_threads_safe ())
708 if (tp
->state
== THREAD_EXITED
)
712 /* Return true value if stack temporaries are enabled for the thread
716 thread_stack_temporaries_enabled_p (thread_info
*tp
)
721 return tp
->stack_temporaries_enabled
;
724 /* Push V on to the stack temporaries of the thread with id PTID. */
727 push_thread_stack_temporary (thread_info
*tp
, struct value
*v
)
729 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
730 tp
->stack_temporaries
.push_back (v
);
733 /* Return true if VAL is among the stack temporaries of the thread
734 TP. Return false otherwise. */
737 value_in_thread_stack_temporaries (struct value
*val
, thread_info
*tp
)
739 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
740 for (value
*v
: tp
->stack_temporaries
)
747 /* Return the last of the stack temporaries for thread with id PTID.
748 Return NULL if there are no stack temporaries for the thread. */
751 get_last_thread_stack_temporary (thread_info
*tp
)
753 struct value
*lastval
= NULL
;
755 gdb_assert (tp
!= NULL
);
756 if (!tp
->stack_temporaries
.empty ())
757 lastval
= tp
->stack_temporaries
.back ();
763 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
765 struct inferior
*inf
;
766 struct thread_info
*tp
;
768 /* It can happen that what we knew as the target inferior id
769 changes. E.g, target remote may only discover the remote process
770 pid after adding the inferior to GDB's list. */
771 inf
= find_inferior_ptid (old_ptid
);
772 inf
->pid
= new_ptid
.pid ();
774 tp
= find_thread_ptid (inf
, old_ptid
);
777 gdb::observers::thread_ptid_changed
.notify (old_ptid
, new_ptid
);
780 /* See gdbthread.h. */
783 set_resumed (ptid_t ptid
, int resumed
)
785 for (thread_info
*tp
: all_non_exited_threads (ptid
))
786 tp
->resumed
= resumed
;
789 /* Helper for set_running, that marks one thread either running or
793 set_running_thread (struct thread_info
*tp
, int running
)
797 if (running
&& tp
->state
== THREAD_STOPPED
)
799 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
803 /* If the thread is now marked stopped, remove it from
804 the step-over queue, so that we don't try to resume
805 it until the user wants it to. */
806 if (tp
->step_over_next
!= NULL
)
807 thread_step_over_chain_remove (tp
);
813 /* See gdbthread.h. */
816 thread_info::set_running (bool running
)
818 if (set_running_thread (this, running
))
819 gdb::observers::target_resumed
.notify (this->ptid
);
823 set_running (ptid_t ptid
, int running
)
825 /* We try not to notify the observer if no thread has actually
826 changed the running state -- merely to reduce the number of
827 messages to the MI frontend. A frontend is supposed to handle
828 multiple *running notifications just fine. */
829 bool any_started
= false;
831 for (thread_info
*tp
: all_non_exited_threads (ptid
))
832 if (set_running_thread (tp
, running
))
836 gdb::observers::target_resumed
.notify (ptid
);
840 /* Helper for set_executing. Set's the thread's 'executing' field
841 from EXECUTING, and if EXECUTING is true also clears the thread's
845 set_executing_thread (thread_info
*thr
, bool executing
)
847 thr
->executing
= executing
;
849 thr
->suspend
.stop_pc
= ~(CORE_ADDR
) 0;
853 set_executing (ptid_t ptid
, int executing
)
855 for (thread_info
*tp
: all_non_exited_threads (ptid
))
856 set_executing_thread (tp
, executing
);
858 /* It only takes one running thread to spawn more threads. */
860 threads_executing
= 1;
861 /* Only clear the flag if the caller is telling us everything is
863 else if (minus_one_ptid
== ptid
)
864 threads_executing
= 0;
867 /* See gdbthread.h. */
870 threads_are_executing (void)
872 return threads_executing
;
876 set_stop_requested (ptid_t ptid
, int stop
)
878 for (thread_info
*tp
: all_non_exited_threads (ptid
))
879 tp
->stop_requested
= stop
;
881 /* Call the stop requested observer so other components of GDB can
882 react to this request. */
884 gdb::observers::thread_stop_requested
.notify (ptid
);
888 finish_thread_state (ptid_t ptid
)
890 bool any_started
= false;
892 for (thread_info
*tp
: all_non_exited_threads (ptid
))
893 if (set_running_thread (tp
, tp
->executing
))
897 gdb::observers::target_resumed
.notify (ptid
);
900 /* See gdbthread.h. */
903 validate_registers_access (void)
905 /* No selected thread, no registers. */
906 if (inferior_ptid
== null_ptid
)
907 error (_("No thread selected."));
909 thread_info
*tp
= inferior_thread ();
911 /* Don't try to read from a dead thread. */
912 if (tp
->state
== THREAD_EXITED
)
913 error (_("The current thread has terminated"));
915 /* ... or from a spinning thread. FIXME: This isn't actually fully
916 correct. It'll allow an user-requested access (e.g., "print $pc"
917 at the prompt) when a thread is not executing for some internal
918 reason, but is marked running from the user's perspective. E.g.,
919 the thread is waiting for its turn in the step-over queue. */
921 error (_("Selected thread is running."));
924 /* See gdbthread.h. */
927 can_access_registers_thread (thread_info
*thread
)
929 /* No thread, no registers. */
933 /* Don't try to read from a dead thread. */
934 if (thread
->state
== THREAD_EXITED
)
937 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
938 if (thread
->executing
)
945 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
947 return (pc
>= thread
->control
.step_range_start
948 && pc
< thread
->control
.step_range_end
);
951 /* Helper for print_thread_info. Returns true if THR should be
952 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
953 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
954 is true if REQUESTED_THREADS is list of global IDs, false if a list
955 of per-inferior thread ids. If PID is not -1, only print THR if it
956 is a thread from the process PID. Otherwise, threads from all
957 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
958 and PID is not -1, then the thread is printed if it belongs to the
959 specified process. Otherwise, an error is raised. */
962 should_print_thread (const char *requested_threads
, int default_inf_num
,
963 int global_ids
, int pid
, struct thread_info
*thr
)
965 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
970 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
972 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
973 thr
->inf
->num
, thr
->per_inf_num
);
978 if (pid
!= -1 && thr
->ptid
.pid () != pid
)
980 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
981 error (_("Requested thread not found in requested process"));
985 if (thr
->state
== THREAD_EXITED
)
991 /* Return the string to display in "info threads"'s "Target Id"
995 thread_target_id_str (thread_info
*tp
)
997 std::string target_id
= target_pid_to_str (tp
->ptid
);
998 const char *extra_info
= target_extra_thread_info (tp
);
999 const char *name
= tp
->name
!= nullptr ? tp
->name
: target_thread_name (tp
);
1001 if (extra_info
!= nullptr && name
!= nullptr)
1002 return string_printf ("%s \"%s\" (%s)", target_id
.c_str (), name
,
1004 else if (extra_info
!= nullptr)
1005 return string_printf ("%s (%s)", target_id
.c_str (), extra_info
);
1006 else if (name
!= nullptr)
1007 return string_printf ("%s \"%s\"", target_id
.c_str (), name
);
1012 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1013 whether REQUESTED_THREADS is a list of global or per-inferior
1017 print_thread_info_1 (struct ui_out
*uiout
, const char *requested_threads
,
1018 int global_ids
, int pid
,
1019 int show_global_ids
)
1021 int default_inf_num
= current_inferior ()->num
;
1023 update_thread_list ();
1025 /* Whether we saw any thread. */
1026 bool any_thread
= false;
1027 /* Whether the current thread is exited. */
1028 bool current_exited
= false;
1030 thread_info
*current_thread
= (inferior_ptid
!= null_ptid
1031 ? inferior_thread () : NULL
);
1034 /* For backward compatibility, we make a list for MI. A table is
1035 preferable for the CLI, though, because it shows table
1037 gdb::optional
<ui_out_emit_list
> list_emitter
;
1038 gdb::optional
<ui_out_emit_table
> table_emitter
;
1040 if (uiout
->is_mi_like_p ())
1041 list_emitter
.emplace (uiout
, "threads");
1045 /* The width of the "Target Id" column. Grown below to
1046 accommodate the largest entry. */
1047 size_t target_id_col_width
= 17;
1049 for (thread_info
*tp
: all_threads ())
1051 if (!should_print_thread (requested_threads
, default_inf_num
,
1052 global_ids
, pid
, tp
))
1055 if (!uiout
->is_mi_like_p ())
1058 = std::max (target_id_col_width
,
1059 thread_target_id_str (tp
).size ());
1067 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1068 uiout
->message (_("No threads.\n"));
1070 uiout
->message (_("No threads match '%s'.\n"),
1075 table_emitter
.emplace (uiout
, show_global_ids
? 5 : 4,
1076 n_threads
, "threads");
1078 uiout
->table_header (1, ui_left
, "current", "");
1079 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1080 if (show_global_ids
)
1081 uiout
->table_header (4, ui_left
, "id", "GId");
1082 uiout
->table_header (target_id_col_width
, ui_left
,
1083 "target-id", "Target Id");
1084 uiout
->table_header (1, ui_left
, "frame", "Frame");
1085 uiout
->table_body ();
1088 /* We'll be switching threads temporarily. */
1089 scoped_restore_current_thread restore_thread
;
1091 for (inferior
*inf
: all_inferiors ())
1092 for (thread_info
*tp
: inf
->threads ())
1097 if (tp
== current_thread
&& tp
->state
== THREAD_EXITED
)
1098 current_exited
= true;
1100 if (!should_print_thread (requested_threads
, default_inf_num
,
1101 global_ids
, pid
, tp
))
1104 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1106 if (!uiout
->is_mi_like_p ())
1108 if (tp
== current_thread
)
1109 uiout
->field_string ("current", "*");
1111 uiout
->field_skip ("current");
1113 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1116 if (show_global_ids
|| uiout
->is_mi_like_p ())
1117 uiout
->field_signed ("id", tp
->global_num
);
1119 /* For the CLI, we stuff everything into the target-id field.
1120 This is a gross hack to make the output come out looking
1121 correct. The underlying problem here is that ui-out has no
1122 way to specify that a field's space allocation should be
1123 shared by several fields. For MI, we do the right thing
1126 if (uiout
->is_mi_like_p ())
1128 uiout
->field_string ("target-id", target_pid_to_str (tp
->ptid
));
1130 const char *extra_info
= target_extra_thread_info (tp
);
1131 if (extra_info
!= nullptr)
1132 uiout
->field_string ("details", extra_info
);
1134 const char *name
= (tp
->name
!= nullptr
1136 : target_thread_name (tp
));
1138 uiout
->field_string ("name", name
);
1142 uiout
->field_string ("target-id",
1143 thread_target_id_str (tp
).c_str ());
1146 if (tp
->state
== THREAD_RUNNING
)
1147 uiout
->text ("(running)\n");
1150 /* The switch below puts us at the top of the stack (leaf
1152 switch_to_thread (tp
);
1153 print_stack_frame (get_selected_frame (NULL
),
1154 /* For MI output, print frame level. */
1155 uiout
->is_mi_like_p (),
1159 if (uiout
->is_mi_like_p ())
1161 const char *state
= "stopped";
1163 if (tp
->state
== THREAD_RUNNING
)
1165 uiout
->field_string ("state", state
);
1168 core
= target_core_of_thread (tp
->ptid
);
1169 if (uiout
->is_mi_like_p () && core
!= -1)
1170 uiout
->field_signed ("core", core
);
1173 /* This end scope restores the current thread and the frame
1174 selected before the "info threads" command, and it finishes the
1175 ui-out list or table. */
1178 if (pid
== -1 && requested_threads
== NULL
)
1180 if (uiout
->is_mi_like_p () && inferior_ptid
!= null_ptid
)
1181 uiout
->field_signed ("current-thread-id", current_thread
->global_num
);
1183 if (inferior_ptid
!= null_ptid
&& current_exited
)
1184 uiout
->message ("\n\
1185 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1186 print_thread_id (inferior_thread ()));
1187 else if (any_thread
&& inferior_ptid
== null_ptid
)
1188 uiout
->message ("\n\
1189 No selected thread. See `help thread'.\n");
1193 /* See gdbthread.h. */
1196 print_thread_info (struct ui_out
*uiout
, const char *requested_threads
,
1199 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1202 /* The options for the "info threads" command. */
1204 struct info_threads_opts
1207 bool show_global_ids
= false;
1210 static const gdb::option::option_def info_threads_option_defs
[] = {
1212 gdb::option::flag_option_def
<info_threads_opts
> {
1214 [] (info_threads_opts
*opts
) { return &opts
->show_global_ids
; },
1215 N_("Show global thread IDs."),
1220 /* Create an option_def_group for the "info threads" options, with
1221 IT_OPTS as context. */
1223 static inline gdb::option::option_def_group
1224 make_info_threads_options_def_group (info_threads_opts
*it_opts
)
1226 return {{info_threads_option_defs
}, it_opts
};
1229 /* Implementation of the "info threads" command.
1231 Note: this has the drawback that it _really_ switches
1232 threads, which frees the frame cache. A no-side
1233 effects info-threads command would be nicer. */
1236 info_threads_command (const char *arg
, int from_tty
)
1238 info_threads_opts it_opts
;
1240 auto grp
= make_info_threads_options_def_group (&it_opts
);
1241 gdb::option::process_options
1242 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1244 print_thread_info_1 (current_uiout
, arg
, 0, -1, it_opts
.show_global_ids
);
1247 /* Completer for the "info threads" command. */
1250 info_threads_command_completer (struct cmd_list_element
*ignore
,
1251 completion_tracker
&tracker
,
1252 const char *text
, const char *word_ignored
)
1254 const auto grp
= make_info_threads_options_def_group (nullptr);
1256 if (gdb::option::complete_options
1257 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1260 /* Convenience to let the user know what the option can accept. */
1263 gdb::option::complete_on_all_options (tracker
, grp
);
1264 /* Keep this "ID" in sync with what "help info threads"
1266 tracker
.add_completion (make_unique_xstrdup ("ID"));
1270 /* See gdbthread.h. */
1273 switch_to_thread_no_regs (struct thread_info
*thread
)
1275 struct inferior
*inf
= thread
->inf
;
1277 set_current_program_space (inf
->pspace
);
1278 set_current_inferior (inf
);
1280 inferior_ptid
= thread
->ptid
;
1283 /* See gdbthread.h. */
1286 switch_to_no_thread ()
1288 if (inferior_ptid
== null_ptid
)
1291 inferior_ptid
= null_ptid
;
1292 reinit_frame_cache ();
1295 /* See gdbthread.h. */
1298 switch_to_thread (thread_info
*thr
)
1300 gdb_assert (thr
!= NULL
);
1302 if (inferior_ptid
== thr
->ptid
)
1305 switch_to_thread_no_regs (thr
);
1307 reinit_frame_cache ();
1310 /* See gdbsupport/common-gdbthread.h. */
1313 switch_to_thread (ptid_t ptid
)
1315 thread_info
*thr
= find_thread_ptid (ptid
);
1316 switch_to_thread (thr
);
1320 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1322 struct frame_info
*frame
= NULL
;
1325 /* This means there was no selected frame. */
1326 if (frame_level
== -1)
1328 select_frame (NULL
);
1332 gdb_assert (frame_level
>= 0);
1334 /* Restore by level first, check if the frame id is the same as
1335 expected. If that fails, try restoring by frame id. If that
1336 fails, nothing to do, just warn the user. */
1338 count
= frame_level
;
1339 frame
= find_relative_frame (get_current_frame (), &count
);
1342 /* The frame ids must match - either both valid or both outer_frame_id.
1343 The latter case is not failsafe, but since it's highly unlikely
1344 the search by level finds the wrong frame, it's 99.9(9)% of
1345 the time (for all practical purposes) safe. */
1346 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1348 /* Cool, all is fine. */
1349 select_frame (frame
);
1353 frame
= frame_find_by_id (a_frame_id
);
1356 /* Cool, refound it. */
1357 select_frame (frame
);
1361 /* Nothing else to do, the frame layout really changed. Select the
1362 innermost stack frame. */
1363 select_frame (get_current_frame ());
1365 /* Warn the user. */
1366 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1368 warning (_("Couldn't restore frame #%d in "
1369 "current thread. Bottom (innermost) frame selected:"),
1371 /* For MI, we should probably have a notification about
1372 current frame change. But this error is not very
1373 likely, so don't bother for now. */
1374 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1379 scoped_restore_current_thread::restore ()
1381 /* If an entry of thread_info was previously selected, it won't be
1382 deleted because we've increased its refcount. The thread represented
1383 by this thread_info entry may have already exited (due to normal exit,
1384 detach, etc), so the thread_info.state is THREAD_EXITED. */
1385 if (m_thread
!= NULL
1386 /* If the previously selected thread belonged to a process that has
1387 in the mean time exited (or killed, detached, etc.), then don't revert
1388 back to it, but instead simply drop back to no thread selected. */
1390 switch_to_thread (m_thread
);
1393 switch_to_no_thread ();
1394 set_current_inferior (m_inf
);
1397 /* The running state of the originally selected thread may have
1398 changed, so we have to recheck it here. */
1399 if (inferior_ptid
!= null_ptid
1401 && m_thread
->state
== THREAD_STOPPED
1402 && target_has_registers
1404 && target_has_memory
)
1405 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1408 scoped_restore_current_thread::~scoped_restore_current_thread ()
1410 if (!m_dont_restore
)
1416 catch (const gdb_exception
&ex
)
1418 /* We're in a dtor, there's really nothing else we can do
1419 but swallow the exception. */
1423 if (m_thread
!= NULL
)
1424 m_thread
->decref ();
1428 scoped_restore_current_thread::scoped_restore_current_thread ()
1431 m_inf
= current_inferior ();
1433 if (inferior_ptid
!= null_ptid
)
1435 thread_info
*tp
= inferior_thread ();
1436 struct frame_info
*frame
;
1438 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1440 && target_has_registers
1442 && target_has_memory
)
1444 /* When processing internal events, there might not be a
1445 selected frame. If we naively call get_selected_frame
1446 here, then we can end up reading debuginfo for the
1447 current frame, but we don't generally need the debuginfo
1449 frame
= get_selected_frame_if_set ();
1454 m_selected_frame_id
= get_frame_id (frame
);
1455 m_selected_frame_level
= frame_relative_level (frame
);
1464 /* See gdbthread.h. */
1467 show_thread_that_caused_stop (void)
1469 return highest_thread_num
> 1;
1472 /* See gdbthread.h. */
1475 show_inferior_qualified_tids (void)
1477 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1480 /* See gdbthread.h. */
1483 print_thread_id (struct thread_info
*thr
)
1485 char *s
= get_print_cell ();
1487 if (show_inferior_qualified_tids ())
1488 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1490 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1494 /* Sort an array of struct thread_info pointers by thread ID (first by
1495 inferior number, and then by per-inferior thread number). Sorts in
1499 tp_array_compar_ascending (const thread_info
*a
, const thread_info
*b
)
1501 if (a
->inf
->num
!= b
->inf
->num
)
1502 return a
->inf
->num
< b
->inf
->num
;
1504 return (a
->per_inf_num
< b
->per_inf_num
);
1507 /* Sort an array of struct thread_info pointers by thread ID (first by
1508 inferior number, and then by per-inferior thread number). Sorts in
1509 descending order. */
1512 tp_array_compar_descending (const thread_info
*a
, const thread_info
*b
)
1514 if (a
->inf
->num
!= b
->inf
->num
)
1515 return a
->inf
->num
> b
->inf
->num
;
1517 return (a
->per_inf_num
> b
->per_inf_num
);
1520 /* Switch to thread THR and execute CMD.
1521 FLAGS.QUIET controls the printing of the thread information.
1522 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1525 thr_try_catch_cmd (thread_info
*thr
, const char *cmd
, int from_tty
,
1526 const qcs_flags
&flags
)
1528 switch_to_thread (thr
);
1531 std::string cmd_result
= execute_command_to_string
1532 (cmd
, from_tty
, gdb_stdout
->term_out ());
1533 if (!flags
.silent
|| cmd_result
.length () > 0)
1536 printf_filtered (_("\nThread %s (%s):\n"),
1537 print_thread_id (thr
),
1538 target_pid_to_str (inferior_ptid
).c_str ());
1539 printf_filtered ("%s", cmd_result
.c_str ());
1542 catch (const gdb_exception_error
&ex
)
1547 printf_filtered (_("\nThread %s (%s):\n"),
1548 print_thread_id (thr
),
1549 target_pid_to_str (inferior_ptid
).c_str ());
1551 printf_filtered ("%s\n", ex
.what ());
1558 /* Option definition of "thread apply"'s "-ascending" option. */
1560 static const gdb::option::flag_option_def
<> ascending_option_def
= {
1563 Call COMMAND for all threads in ascending order.\n\
1564 The default is descending order."),
1567 /* The qcs command line flags for the "thread apply" commands. Keep
1568 this in sync with the "frame apply" commands. */
1570 using qcs_flag_option_def
1571 = gdb::option::flag_option_def
<qcs_flags
>;
1573 static const gdb::option::option_def thr_qcs_flags_option_defs
[] = {
1574 qcs_flag_option_def
{
1575 "q", [] (qcs_flags
*opt
) { return &opt
->quiet
; },
1576 N_("Disables printing the thread information."),
1579 qcs_flag_option_def
{
1580 "c", [] (qcs_flags
*opt
) { return &opt
->cont
; },
1581 N_("Print any error raised by COMMAND and continue."),
1584 qcs_flag_option_def
{
1585 "s", [] (qcs_flags
*opt
) { return &opt
->silent
; },
1586 N_("Silently ignore any errors or empty output produced by COMMAND."),
1590 /* Create an option_def_group for the "thread apply all" options, with
1591 ASCENDING and FLAGS as context. */
1593 static inline std::array
<gdb::option::option_def_group
, 2>
1594 make_thread_apply_all_options_def_group (bool *ascending
,
1598 { {ascending_option_def
.def ()}, ascending
},
1599 { {thr_qcs_flags_option_defs
}, flags
},
1603 /* Create an option_def_group for the "thread apply" options, with
1604 FLAGS as context. */
1606 static inline gdb::option::option_def_group
1607 make_thread_apply_options_def_group (qcs_flags
*flags
)
1609 return {{thr_qcs_flags_option_defs
}, flags
};
1612 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1613 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1614 of two numbers separated by a hyphen. Examples:
1616 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1617 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1618 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
1621 thread_apply_all_command (const char *cmd
, int from_tty
)
1623 bool ascending
= false;
1626 auto group
= make_thread_apply_all_options_def_group (&ascending
,
1628 gdb::option::process_options
1629 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1631 validate_flags_qcs ("thread apply all", &flags
);
1633 if (cmd
== NULL
|| *cmd
== '\000')
1634 error (_("Please specify a command at the end of 'thread apply all'"));
1636 update_thread_list ();
1638 int tc
= live_threads_count ();
1641 /* Save a copy of the thread list and increment each thread's
1642 refcount while executing the command in the context of each
1643 thread, in case the command is one that wipes threads. E.g.,
1644 detach, kill, disconnect, etc., or even normally continuing
1645 over an inferior or thread exit. */
1646 std::vector
<thread_info
*> thr_list_cpy
;
1647 thr_list_cpy
.reserve (tc
);
1649 for (thread_info
*tp
: all_non_exited_threads ())
1650 thr_list_cpy
.push_back (tp
);
1651 gdb_assert (thr_list_cpy
.size () == tc
);
1653 /* Increment the refcounts, and restore them back on scope
1655 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1657 auto *sorter
= (ascending
1658 ? tp_array_compar_ascending
1659 : tp_array_compar_descending
);
1660 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), sorter
);
1662 scoped_restore_current_thread restore_thread
;
1664 for (thread_info
*thr
: thr_list_cpy
)
1665 if (thread_alive (thr
))
1666 thr_try_catch_cmd (thr
, cmd
, from_tty
, flags
);
1670 /* Completer for "thread apply [ID list]". */
1673 thread_apply_command_completer (cmd_list_element
*ignore
,
1674 completion_tracker
&tracker
,
1675 const char *text
, const char * /*word*/)
1677 /* Don't leave this to complete_options because there's an early
1679 tracker
.set_use_custom_word_point (true);
1681 tid_range_parser parser
;
1682 parser
.init (text
, current_inferior ()->num
);
1686 while (!parser
.finished ())
1688 int inf_num
, thr_start
, thr_end
;
1690 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1693 if (parser
.in_star_range () || parser
.in_thread_range ())
1694 parser
.skip_range ();
1697 catch (const gdb_exception_error
&ex
)
1699 /* get_tid_range throws if it parses a negative number, for
1700 example. But a seemingly negative number may be the start of
1701 an option instead. */
1704 const char *cmd
= parser
.cur_tok ();
1708 /* No thread ID list yet. */
1712 /* Check if we're past a valid thread ID list already. */
1713 if (parser
.finished ()
1714 && cmd
> text
&& !isspace (cmd
[-1]))
1717 /* We're past the thread ID list, advance word point. */
1718 tracker
.advance_custom_word_point_by (cmd
- text
);
1721 const auto group
= make_thread_apply_options_def_group (nullptr);
1722 if (gdb::option::complete_options
1723 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1726 complete_nested_command_line (tracker
, text
);
1729 /* Completer for "thread apply all". */
1732 thread_apply_all_command_completer (cmd_list_element
*ignore
,
1733 completion_tracker
&tracker
,
1734 const char *text
, const char *word
)
1736 const auto group
= make_thread_apply_all_options_def_group (nullptr,
1738 if (gdb::option::complete_options
1739 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
))
1742 complete_nested_command_line (tracker
, text
);
1745 /* Implementation of the "thread apply" command. */
1748 thread_apply_command (const char *tidlist
, int from_tty
)
1751 const char *cmd
= NULL
;
1752 tid_range_parser parser
;
1754 if (tidlist
== NULL
|| *tidlist
== '\000')
1755 error (_("Please specify a thread ID list"));
1757 parser
.init (tidlist
, current_inferior ()->num
);
1758 while (!parser
.finished ())
1760 int inf_num
, thr_start
, thr_end
;
1762 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1766 cmd
= parser
.cur_tok ();
1768 auto group
= make_thread_apply_options_def_group (&flags
);
1769 gdb::option::process_options
1770 (&cmd
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1772 validate_flags_qcs ("thread apply", &flags
);
1775 error (_("Please specify a command following the thread ID list"));
1777 if (tidlist
== cmd
|| isdigit (cmd
[0]))
1778 invalid_thread_id_error (cmd
);
1780 scoped_restore_current_thread restore_thread
;
1782 parser
.init (tidlist
, current_inferior ()->num
);
1783 while (!parser
.finished ())
1785 struct thread_info
*tp
= NULL
;
1786 struct inferior
*inf
;
1787 int inf_num
, thr_num
;
1789 parser
.get_tid (&inf_num
, &thr_num
);
1790 inf
= find_inferior_id (inf_num
);
1792 tp
= find_thread_id (inf
, thr_num
);
1794 if (parser
.in_star_range ())
1798 warning (_("Unknown inferior %d"), inf_num
);
1799 parser
.skip_range ();
1803 /* No use looking for threads past the highest thread number
1804 the inferior ever had. */
1805 if (thr_num
>= inf
->highest_thread_num
)
1806 parser
.skip_range ();
1808 /* Be quiet about unknown threads numbers. */
1815 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1816 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1818 warning (_("Unknown thread %d"), thr_num
);
1822 if (!thread_alive (tp
))
1824 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1828 thr_try_catch_cmd (tp
, cmd
, from_tty
, flags
);
1833 /* Implementation of the "taas" command. */
1836 taas_command (const char *cmd
, int from_tty
)
1838 if (cmd
== NULL
|| *cmd
== '\0')
1839 error (_("Please specify a command to apply on all threads"));
1840 std::string expanded
= std::string ("thread apply all -s ") + cmd
;
1841 execute_command (expanded
.c_str (), from_tty
);
1844 /* Implementation of the "tfaas" command. */
1847 tfaas_command (const char *cmd
, int from_tty
)
1849 if (cmd
== NULL
|| *cmd
== '\0')
1850 error (_("Please specify a command to apply on all frames of all threads"));
1851 std::string expanded
1852 = std::string ("thread apply all -s -- frame apply all -s ") + cmd
;
1853 execute_command (expanded
.c_str (), from_tty
);
1856 /* Switch to the specified thread, or print the current thread. */
1859 thread_command (const char *tidstr
, int from_tty
)
1863 if (inferior_ptid
== null_ptid
)
1864 error (_("No thread selected"));
1866 if (target_has_stack
)
1868 struct thread_info
*tp
= inferior_thread ();
1870 if (tp
->state
== THREAD_EXITED
)
1871 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1872 print_thread_id (tp
),
1873 target_pid_to_str (inferior_ptid
).c_str ());
1875 printf_filtered (_("[Current thread is %s (%s)]\n"),
1876 print_thread_id (tp
),
1877 target_pid_to_str (inferior_ptid
).c_str ());
1880 error (_("No stack."));
1884 ptid_t previous_ptid
= inferior_ptid
;
1886 thread_select (tidstr
, parse_thread_id (tidstr
, NULL
));
1888 /* Print if the thread has not changed, otherwise an event will
1890 if (inferior_ptid
== previous_ptid
)
1892 print_selected_thread_frame (current_uiout
,
1893 USER_SELECTED_THREAD
1894 | USER_SELECTED_FRAME
);
1898 gdb::observers::user_selected_context_changed
.notify
1899 (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
);
1904 /* Implementation of `thread name'. */
1907 thread_name_command (const char *arg
, int from_tty
)
1909 struct thread_info
*info
;
1911 if (inferior_ptid
== null_ptid
)
1912 error (_("No thread selected"));
1914 arg
= skip_spaces (arg
);
1916 info
= inferior_thread ();
1918 info
->name
= arg
? xstrdup (arg
) : NULL
;
1921 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1924 thread_find_command (const char *arg
, int from_tty
)
1927 unsigned long match
= 0;
1929 if (arg
== NULL
|| *arg
== '\0')
1930 error (_("Command requires an argument."));
1932 tmp
= re_comp (arg
);
1934 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1936 update_thread_list ();
1937 for (thread_info
*tp
: all_threads ())
1939 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1941 printf_filtered (_("Thread %s has name '%s'\n"),
1942 print_thread_id (tp
), tp
->name
);
1946 tmp
= target_thread_name (tp
);
1947 if (tmp
!= NULL
&& re_exec (tmp
))
1949 printf_filtered (_("Thread %s has target name '%s'\n"),
1950 print_thread_id (tp
), tmp
);
1954 std::string name
= target_pid_to_str (tp
->ptid
);
1955 if (!name
.empty () && re_exec (name
.c_str ()))
1957 printf_filtered (_("Thread %s has target id '%s'\n"),
1958 print_thread_id (tp
), name
.c_str ());
1962 tmp
= target_extra_thread_info (tp
);
1963 if (tmp
!= NULL
&& re_exec (tmp
))
1965 printf_filtered (_("Thread %s has extra info '%s'\n"),
1966 print_thread_id (tp
), tmp
);
1971 printf_filtered (_("No threads match '%s'\n"), arg
);
1974 /* Print notices when new threads are attached and detached. */
1975 bool print_thread_events
= true;
1977 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1978 struct cmd_list_element
*c
, const char *value
)
1980 fprintf_filtered (file
,
1981 _("Printing of thread events is %s.\n"),
1985 /* See gdbthread.h. */
1988 thread_select (const char *tidstr
, thread_info
*tp
)
1990 if (!thread_alive (tp
))
1991 error (_("Thread ID %s has terminated."), tidstr
);
1993 switch_to_thread (tp
);
1995 annotate_thread_changed ();
1997 /* Since the current thread may have changed, see if there is any
1998 exited thread we can now delete. */
2002 /* Print thread and frame switch command response. */
2005 print_selected_thread_frame (struct ui_out
*uiout
,
2006 user_selected_what selection
)
2008 struct thread_info
*tp
= inferior_thread ();
2010 if (selection
& USER_SELECTED_THREAD
)
2012 if (uiout
->is_mi_like_p ())
2014 uiout
->field_signed ("new-thread-id",
2015 inferior_thread ()->global_num
);
2019 uiout
->text ("[Switching to thread ");
2020 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2022 uiout
->text (target_pid_to_str (inferior_ptid
).c_str ());
2027 if (tp
->state
== THREAD_RUNNING
)
2029 if (selection
& USER_SELECTED_THREAD
)
2030 uiout
->text ("(running)\n");
2032 else if (selection
& USER_SELECTED_FRAME
)
2034 if (selection
& USER_SELECTED_THREAD
)
2037 if (has_stack_frames ())
2038 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2043 /* Update the 'threads_executing' global based on the threads we know
2047 update_threads_executing (void)
2049 threads_executing
= 0;
2050 for (thread_info
*tp
: all_non_exited_threads ())
2054 threads_executing
= 1;
2061 update_thread_list (void)
2063 target_update_thread_list ();
2064 update_threads_executing ();
2067 /* Return a new value for the selected thread's id. Return a value of
2068 0 if no thread is selected. If GLOBAL is true, return the thread's
2069 global number. Otherwise return the per-inferior number. */
2071 static struct value
*
2072 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2076 if (inferior_ptid
== null_ptid
)
2080 thread_info
*tp
= inferior_thread ();
2082 int_val
= tp
->global_num
;
2084 int_val
= tp
->per_inf_num
;
2087 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2090 /* Return a new value for the selected thread's per-inferior thread
2091 number. Return a value of 0 if no thread is selected, or no
2094 static struct value
*
2095 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2096 struct internalvar
*var
,
2099 return thread_num_make_value_helper (gdbarch
, 0);
2102 /* Return a new value for the selected thread's global id. Return a
2103 value of 0 if no thread is selected, or no threads exist. */
2105 static struct value
*
2106 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2109 return thread_num_make_value_helper (gdbarch
, 1);
2112 /* Commands with a prefix of `thread'. */
2113 struct cmd_list_element
*thread_cmd_list
= NULL
;
2115 /* Implementation of `thread' variable. */
2117 static const struct internalvar_funcs thread_funcs
=
2119 thread_id_per_inf_num_make_value
,
2124 /* Implementation of `gthread' variable. */
2126 static const struct internalvar_funcs gthread_funcs
=
2128 global_thread_id_make_value
,
2134 _initialize_thread (void)
2136 static struct cmd_list_element
*thread_apply_list
= NULL
;
2137 cmd_list_element
*c
;
2139 const auto info_threads_opts
= make_info_threads_options_def_group (nullptr);
2141 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2143 static std::string info_threads_help
2144 = gdb::option::build_help (_("\
2145 Display currently known threads.\n\
2146 Usage: info threads [OPTION]... [ID]...\n\
2150 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2151 Otherwise, all threads are displayed."),
2154 c
= add_info ("threads", info_threads_command
, info_threads_help
.c_str ());
2155 set_cmd_completer_handle_brkchars (c
, info_threads_command_completer
);
2157 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2158 Use this command to switch between threads.\n\
2159 The new thread ID must be currently known."),
2160 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2162 #define THREAD_APPLY_OPTION_HELP "\
2163 Prints per-inferior thread number and target system's thread id\n\
2164 followed by COMMAND output.\n\
2166 By default, an error raised during the execution of COMMAND\n\
2167 aborts \"thread apply\".\n\
2172 const auto thread_apply_opts
= make_thread_apply_options_def_group (nullptr);
2174 static std::string thread_apply_help
= gdb::option::build_help (_("\
2175 Apply a command to a list of threads.\n\
2176 Usage: thread apply ID... [OPTION]... COMMAND\n\
2177 ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
2178 THREAD_APPLY_OPTION_HELP
),
2181 c
= add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2182 thread_apply_help
.c_str (),
2183 &thread_apply_list
, "thread apply ", 1,
2185 set_cmd_completer_handle_brkchars (c
, thread_apply_command_completer
);
2187 const auto thread_apply_all_opts
2188 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2190 static std::string thread_apply_all_help
= gdb::option::build_help (_("\
2191 Apply a command to all threads.\n\
2193 Usage: thread apply all [OPTION]... COMMAND\n"
2194 THREAD_APPLY_OPTION_HELP
),
2195 thread_apply_all_opts
);
2197 c
= add_cmd ("all", class_run
, thread_apply_all_command
,
2198 thread_apply_all_help
.c_str (),
2199 &thread_apply_list
);
2200 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2202 c
= add_com ("taas", class_run
, taas_command
, _("\
2203 Apply a command to all threads (ignoring errors and empty output).\n\
2204 Usage: taas [OPTION]... COMMAND\n\
2205 shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2206 See \"help thread apply all\" for available options."));
2207 set_cmd_completer_handle_brkchars (c
, thread_apply_all_command_completer
);
2209 c
= add_com ("tfaas", class_run
, tfaas_command
, _("\
2210 Apply a command to all frames of all threads (ignoring errors and empty output).\n\
2211 Usage: tfaas [OPTION]... COMMAND\n\
2212 shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2213 See \"help frame apply all\" for available options."));
2214 set_cmd_completer_handle_brkchars (c
, frame_apply_all_cmd_completer
);
2216 add_cmd ("name", class_run
, thread_name_command
,
2217 _("Set the current thread's name.\n\
2218 Usage: thread name [NAME]\n\
2219 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2221 add_cmd ("find", class_run
, thread_find_command
, _("\
2222 Find threads that match a regular expression.\n\
2223 Usage: thread find REGEXP\n\
2224 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2227 add_com_alias ("t", "thread", class_run
, 1);
2229 add_setshow_boolean_cmd ("thread-events", no_class
,
2230 &print_thread_events
, _("\
2231 Set printing of thread events (such as thread start and exit)."), _("\
2232 Show printing of thread events (such as thread start and exit)."), NULL
,
2234 show_print_thread_events
,
2235 &setprintlist
, &showprintlist
);
2237 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2238 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);