1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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/>. */
29 #include "gdbthread.h"
37 #include <sys/types.h>
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46 #include "tid-parse.h"
48 #include "common/gdb_optional.h"
50 /* Definition of struct thread_info exported to gdbthread.h. */
52 /* Prototypes for local functions. */
54 struct thread_info
*thread_list
= NULL
;
55 static int highest_thread_num
;
57 /* True if any thread is, or may be executing. We need to track this
58 separately because until we fully sync the thread list, we won't
59 know whether the target is fully stopped, even if we see stop
60 events for all known threads, because any of those threads may have
61 spawned new threads we haven't heard of yet. */
62 static int threads_executing
;
64 static void thread_apply_all_command (char *, int);
65 static int thread_alive (struct thread_info
*);
66 static void info_threads_command (char *, int);
67 static void thread_apply_command (char *, int);
69 /* RAII type used to increase / decrease the refcount of each thread
70 in a given list of threads. */
72 class scoped_inc_dec_ref
75 explicit scoped_inc_dec_ref (const std::vector
<thread_info
*> &thrds
)
78 for (thread_info
*thr
: m_thrds
)
82 ~scoped_inc_dec_ref ()
84 for (thread_info
*thr
: m_thrds
)
89 const std::vector
<thread_info
*> &m_thrds
;
94 inferior_thread (void)
96 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
101 /* Delete the breakpoint pointed at by BP_P, if there's one. */
104 delete_thread_breakpoint (struct breakpoint
**bp_p
)
108 delete_breakpoint (*bp_p
);
114 delete_step_resume_breakpoint (struct thread_info
*tp
)
117 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
121 delete_exception_resume_breakpoint (struct thread_info
*tp
)
124 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
127 /* See gdbthread.h. */
130 delete_single_step_breakpoints (struct thread_info
*tp
)
133 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
136 /* Delete the breakpoint pointed at by BP_P at the next stop, if
140 delete_at_next_stop (struct breakpoint
**bp
)
144 (*bp
)->disposition
= disp_del_at_next_stop
;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
154 return tp
->control
.single_step_breakpoints
!= NULL
;
157 /* See gdbthread.h. */
160 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
161 struct address_space
*aspace
,
164 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
166 return (ss_bps
!= NULL
167 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
170 /* See gdbthread.h. */
173 thread_cancel_execution_command (struct thread_info
*thr
)
175 if (thr
->thread_fsm
!= NULL
)
177 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
178 thread_fsm_delete (thr
->thread_fsm
);
179 thr
->thread_fsm
= NULL
;
184 clear_thread_inferior_resources (struct thread_info
*tp
)
186 /* NOTE: this will take care of any left-over step_resume breakpoints,
187 but not any user-specified thread-specific breakpoints. We can not
188 delete the breakpoint straight-off, because the inferior might not
189 be stopped at the moment. */
190 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
191 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
192 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
194 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
196 bpstat_clear (&tp
->control
.stop_bpstat
);
198 btrace_teardown (tp
);
200 thread_cancel_execution_command (tp
);
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 observer_notify_thread_exit (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 struct thread_info
*tp
, *tpnext
;
229 highest_thread_num
= 0;
234 for (tp
= thread_list
; tp
; tp
= tpnext
)
237 if (tp
->deletable ())
240 set_thread_exited (tp
, 1);
244 threads_executing
= 0;
247 /* Allocate a new thread of inferior INF with target id PTID and add
248 it to the thread list. */
250 static struct thread_info
*
251 new_thread (struct inferior
*inf
, ptid_t ptid
)
253 thread_info
*tp
= new thread_info (inf
, ptid
);
255 if (thread_list
== NULL
)
259 struct thread_info
*last
;
261 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
270 add_thread_silent (ptid_t ptid
)
272 struct thread_info
*tp
;
273 struct inferior
*inf
= find_inferior_ptid (ptid
);
274 gdb_assert (inf
!= NULL
);
276 tp
= find_thread_ptid (ptid
);
278 /* Found an old thread with the same id. It has to be dead,
279 otherwise we wouldn't be adding a new thread with the same id.
280 The OS is reusing this id --- delete it, and recreate a new
283 /* In addition to deleting the thread, if this is the current
284 thread, then we need to take care that delete_thread doesn't
285 really delete the thread if it is inferior_ptid. Create a
286 new template thread in the list with an invalid ptid, switch
287 to it, delete the original thread, reset the new thread's
288 ptid, and switch to it. */
290 if (inferior_ptid
== ptid
)
292 tp
= new_thread (inf
, null_ptid
);
294 /* Make switch_to_thread not read from the thread. */
295 tp
->state
= THREAD_EXITED
;
296 switch_to_thread (null_ptid
);
298 /* Now we can delete it. */
299 delete_thread (ptid
);
301 /* Now reset its ptid, and reswitch inferior_ptid to it. */
303 tp
->state
= THREAD_STOPPED
;
304 switch_to_thread (ptid
);
306 observer_notify_new_thread (tp
);
312 /* Just go ahead and delete it. */
313 delete_thread (ptid
);
316 tp
= new_thread (inf
, ptid
);
317 observer_notify_new_thread (tp
);
323 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
325 struct thread_info
*result
= add_thread_silent (ptid
);
329 if (print_thread_events
)
330 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
332 annotate_new_thread ();
337 add_thread (ptid_t ptid
)
339 return add_thread_with_info (ptid
, NULL
);
342 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
343 : ptid (ptid_
), inf (inf_
)
345 gdb_assert (inf_
!= NULL
);
347 this->global_num
= ++highest_thread_num
;
348 this->per_inf_num
= ++inf_
->highest_thread_num
;
350 /* Nothing to follow yet. */
351 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
352 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
353 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
356 thread_info::~thread_info ()
360 if (this->private_dtor
)
361 this->private_dtor (this->priv
);
369 /* Add TP to the end of the step-over chain LIST_P. */
372 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
374 gdb_assert (tp
->step_over_next
== NULL
);
375 gdb_assert (tp
->step_over_prev
== NULL
);
380 tp
->step_over_prev
= tp
->step_over_next
= tp
;
384 struct thread_info
*head
= *list_p
;
385 struct thread_info
*tail
= head
->step_over_prev
;
387 tp
->step_over_prev
= tail
;
388 tp
->step_over_next
= head
;
389 head
->step_over_prev
= tp
;
390 tail
->step_over_next
= tp
;
394 /* Remove TP from step-over chain LIST_P. */
397 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
399 gdb_assert (tp
->step_over_next
!= NULL
);
400 gdb_assert (tp
->step_over_prev
!= NULL
);
404 if (tp
== tp
->step_over_next
)
407 *list_p
= tp
->step_over_next
;
410 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
411 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
412 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
415 /* See gdbthread.h. */
418 thread_step_over_chain_next (struct thread_info
*tp
)
420 struct thread_info
*next
= tp
->step_over_next
;
422 return (next
== step_over_queue_head
? NULL
: next
);
425 /* See gdbthread.h. */
428 thread_is_in_step_over_chain (struct thread_info
*tp
)
430 return (tp
->step_over_next
!= NULL
);
433 /* See gdbthread.h. */
436 thread_step_over_chain_enqueue (struct thread_info
*tp
)
438 step_over_chain_enqueue (&step_over_queue_head
, tp
);
441 /* See gdbthread.h. */
444 thread_step_over_chain_remove (struct thread_info
*tp
)
446 step_over_chain_remove (&step_over_queue_head
, tp
);
449 /* Delete thread PTID. If SILENT, don't notify the observer of this
452 delete_thread_1 (ptid_t ptid
, int silent
)
454 struct thread_info
*tp
, *tpprev
;
458 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
459 if (tp
->ptid
== ptid
)
465 set_thread_exited (tp
, silent
);
467 if (!tp
->deletable ())
469 /* Will be really deleted some other time. */
474 tpprev
->next
= tp
->next
;
476 thread_list
= tp
->next
;
481 /* Delete thread PTID and notify of thread exit. If this is
482 inferior_ptid, don't actually delete it, but tag it as exited and
483 do the notification. If PTID is the user selected thread, clear
486 delete_thread (ptid_t ptid
)
488 delete_thread_1 (ptid
, 0 /* not silent */);
492 delete_thread_silent (ptid_t ptid
)
494 delete_thread_1 (ptid
, 1 /* silent */);
498 find_thread_global_id (int global_id
)
500 struct thread_info
*tp
;
502 for (tp
= thread_list
; tp
; tp
= tp
->next
)
503 if (tp
->global_num
== global_id
)
509 static struct thread_info
*
510 find_thread_id (struct inferior
*inf
, int thr_num
)
512 struct thread_info
*tp
;
514 for (tp
= thread_list
; tp
; tp
= tp
->next
)
515 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
521 /* Find a thread_info by matching PTID. */
523 find_thread_ptid (ptid_t ptid
)
525 struct thread_info
*tp
;
527 for (tp
= thread_list
; tp
; tp
= tp
->next
)
528 if (tp
->ptid
== ptid
)
535 * Thread iterator function.
537 * Calls a callback function once for each thread, so long as
538 * the callback function returns false. If the callback function
539 * returns true, the iteration will end and the current thread
540 * will be returned. This can be useful for implementing a
541 * search for a thread with arbitrary attributes, or for applying
542 * some operation to every thread.
544 * FIXME: some of the existing functionality, such as
545 * "Thread apply all", might be rewritten using this functionality.
549 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
552 struct thread_info
*tp
, *next
;
554 for (tp
= thread_list
; tp
; tp
= next
)
557 if ((*callback
) (tp
, data
))
568 struct thread_info
*tp
;
570 for (tp
= thread_list
; tp
; tp
= tp
->next
)
576 /* Return the number of non-exited threads in the thread list. */
579 live_threads_count (void)
582 struct thread_info
*tp
;
584 ALL_NON_EXITED_THREADS (tp
)
591 valid_global_thread_id (int global_id
)
593 struct thread_info
*tp
;
595 for (tp
= thread_list
; tp
; tp
= tp
->next
)
596 if (tp
->global_num
== global_id
)
603 ptid_to_global_thread_id (ptid_t ptid
)
605 struct thread_info
*tp
;
607 for (tp
= thread_list
; tp
; tp
= tp
->next
)
608 if (tp
->ptid
== ptid
)
609 return tp
->global_num
;
615 global_thread_id_to_ptid (int global_id
)
617 struct thread_info
*thread
= find_thread_global_id (global_id
);
622 return minus_one_ptid
;
626 in_thread_list (ptid_t ptid
)
628 struct thread_info
*tp
;
630 for (tp
= thread_list
; tp
; tp
= tp
->next
)
631 if (tp
->ptid
== ptid
)
634 return 0; /* Never heard of 'im. */
637 /* Finds the first thread of the inferior given by PID. If PID is -1,
638 return the first thread in the list. */
641 first_thread_of_process (int pid
)
643 struct thread_info
*tp
, *ret
= NULL
;
645 for (tp
= thread_list
; tp
; tp
= tp
->next
)
646 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
647 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
654 any_thread_of_process (int pid
)
656 struct thread_info
*tp
;
658 gdb_assert (pid
!= 0);
660 /* Prefer the current thread. */
661 if (ptid_get_pid (inferior_ptid
) == pid
)
662 return inferior_thread ();
664 ALL_NON_EXITED_THREADS (tp
)
665 if (ptid_get_pid (tp
->ptid
) == pid
)
672 any_live_thread_of_process (int pid
)
674 struct thread_info
*curr_tp
= NULL
;
675 struct thread_info
*tp
;
676 struct thread_info
*tp_executing
= NULL
;
678 gdb_assert (pid
!= 0);
680 /* Prefer the current thread if it's not executing. */
681 if (ptid_get_pid (inferior_ptid
) == pid
)
683 /* If the current thread is dead, forget it. If it's not
684 executing, use it. Otherwise, still choose it (below), but
685 only if no other non-executing thread is found. */
686 curr_tp
= inferior_thread ();
687 if (curr_tp
->state
== THREAD_EXITED
)
689 else if (!curr_tp
->executing
)
693 ALL_NON_EXITED_THREADS (tp
)
694 if (ptid_get_pid (tp
->ptid
) == pid
)
702 /* If both the current thread and all live threads are executing,
703 prefer the current thread. */
707 /* Otherwise, just return an executing thread, if any. */
711 /* Print a list of thread ids currently known, and the total number of
712 threads. To be used from within catch_errors. */
714 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
716 struct thread_info
*tp
;
718 int current_thread
= -1;
720 update_thread_list ();
723 ui_out_emit_tuple
tuple_emitter (uiout
, "thread-ids");
725 for (tp
= thread_list
; tp
; tp
= tp
->next
)
727 if (tp
->state
== THREAD_EXITED
)
730 if (tp
->ptid
== inferior_ptid
)
731 current_thread
= tp
->global_num
;
734 uiout
->field_int ("thread-id", tp
->global_num
);
738 if (current_thread
!= -1)
739 uiout
->field_int ("current-thread-id", current_thread
);
740 uiout
->field_int ("number-of-threads", num
);
744 /* Official gdblib interface function to get a list of thread ids and
747 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
749 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
750 error_message
, RETURN_MASK_ALL
) < 0)
755 /* Return true if TP is an active thread. */
757 thread_alive (struct thread_info
*tp
)
759 if (tp
->state
== THREAD_EXITED
)
761 if (!target_thread_alive (tp
->ptid
))
766 /* See gdbthreads.h. */
771 struct thread_info
*tp
, *tmp
;
773 ALL_THREADS_SAFE (tp
, tmp
)
775 if (!thread_alive (tp
))
776 delete_thread (tp
->ptid
);
780 /* See gdbthreads.h. */
783 delete_exited_threads (void)
785 struct thread_info
*tp
, *tmp
;
787 ALL_THREADS_SAFE (tp
, tmp
)
789 if (tp
->state
== THREAD_EXITED
)
790 delete_thread (tp
->ptid
);
794 /* Disable storing stack temporaries for the thread whose id is
798 disable_thread_stack_temporaries (void *data
)
800 ptid_t
*pd
= (ptid_t
*) data
;
801 struct thread_info
*tp
= find_thread_ptid (*pd
);
805 tp
->stack_temporaries_enabled
= 0;
806 VEC_free (value_ptr
, tp
->stack_temporaries
);
812 /* Enable storing stack temporaries for thread with id PTID and return a
813 cleanup which can disable and clear the stack temporaries. */
816 enable_thread_stack_temporaries (ptid_t ptid
)
818 struct thread_info
*tp
= find_thread_ptid (ptid
);
822 gdb_assert (tp
!= NULL
);
824 tp
->stack_temporaries_enabled
= 1;
825 tp
->stack_temporaries
= NULL
;
826 data
= XNEW (ptid_t
);
828 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
833 /* Return non-zero value if stack temporaies are enabled for the thread
837 thread_stack_temporaries_enabled_p (ptid_t ptid
)
839 struct thread_info
*tp
= find_thread_ptid (ptid
);
844 return tp
->stack_temporaries_enabled
;
847 /* Push V on to the stack temporaries of the thread with id PTID. */
850 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
852 struct thread_info
*tp
= find_thread_ptid (ptid
);
854 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
855 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
858 /* Return 1 if VAL is among the stack temporaries of the thread
859 with id PTID. Return 0 otherwise. */
862 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
864 struct thread_info
*tp
= find_thread_ptid (ptid
);
866 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
867 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
872 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
880 /* Return the last of the stack temporaries for thread with id PTID.
881 Return NULL if there are no stack temporaries for the thread. */
884 get_last_thread_stack_temporary (ptid_t ptid
)
886 struct value
*lastval
= NULL
;
887 struct thread_info
*tp
= find_thread_ptid (ptid
);
889 gdb_assert (tp
!= NULL
);
890 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
891 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
897 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
899 struct inferior
*inf
;
900 struct thread_info
*tp
;
902 /* It can happen that what we knew as the target inferior id
903 changes. E.g, target remote may only discover the remote process
904 pid after adding the inferior to GDB's list. */
905 inf
= find_inferior_ptid (old_ptid
);
906 inf
->pid
= ptid_get_pid (new_ptid
);
908 tp
= find_thread_ptid (old_ptid
);
911 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
914 /* See gdbthread.h. */
917 set_resumed (ptid_t ptid
, int resumed
)
919 struct thread_info
*tp
;
920 int all
= ptid
== minus_one_ptid
;
922 if (all
|| ptid_is_pid (ptid
))
924 for (tp
= thread_list
; tp
; tp
= tp
->next
)
925 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
926 tp
->resumed
= resumed
;
930 tp
= find_thread_ptid (ptid
);
931 gdb_assert (tp
!= NULL
);
932 tp
->resumed
= resumed
;
936 /* Helper for set_running, that marks one thread either running or
940 set_running_thread (struct thread_info
*tp
, int running
)
944 if (running
&& tp
->state
== THREAD_STOPPED
)
946 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
950 /* If the thread is now marked stopped, remove it from
951 the step-over queue, so that we don't try to resume
952 it until the user wants it to. */
953 if (tp
->step_over_next
!= NULL
)
954 thread_step_over_chain_remove (tp
);
961 set_running (ptid_t ptid
, int running
)
963 struct thread_info
*tp
;
964 int all
= ptid
== minus_one_ptid
;
967 /* We try not to notify the observer if no thread has actually changed
968 the running state -- merely to reduce the number of messages to
969 frontend. Frontend is supposed to handle multiple *running just fine. */
970 if (all
|| ptid_is_pid (ptid
))
972 for (tp
= thread_list
; tp
; tp
= tp
->next
)
973 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
975 if (tp
->state
== THREAD_EXITED
)
978 if (set_running_thread (tp
, running
))
984 tp
= find_thread_ptid (ptid
);
985 gdb_assert (tp
!= NULL
);
986 gdb_assert (tp
->state
!= THREAD_EXITED
);
987 if (set_running_thread (tp
, running
))
991 observer_notify_target_resumed (ptid
);
995 is_thread_state (ptid_t ptid
, enum thread_state state
)
997 struct thread_info
*tp
;
999 tp
= find_thread_ptid (ptid
);
1001 return tp
->state
== state
;
1005 is_stopped (ptid_t ptid
)
1007 return is_thread_state (ptid
, THREAD_STOPPED
);
1011 is_exited (ptid_t ptid
)
1013 return is_thread_state (ptid
, THREAD_EXITED
);
1017 is_running (ptid_t ptid
)
1019 return is_thread_state (ptid
, THREAD_RUNNING
);
1023 is_executing (ptid_t ptid
)
1025 struct thread_info
*tp
;
1027 tp
= find_thread_ptid (ptid
);
1029 return tp
->executing
;
1033 set_executing (ptid_t ptid
, int executing
)
1035 struct thread_info
*tp
;
1036 int all
= ptid
== minus_one_ptid
;
1038 if (all
|| ptid_is_pid (ptid
))
1040 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1041 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1042 tp
->executing
= executing
;
1046 tp
= find_thread_ptid (ptid
);
1048 tp
->executing
= executing
;
1051 /* It only takes one running thread to spawn more threads.*/
1053 threads_executing
= 1;
1054 /* Only clear the flag if the caller is telling us everything is
1056 else if (minus_one_ptid
== ptid
)
1057 threads_executing
= 0;
1060 /* See gdbthread.h. */
1063 threads_are_executing (void)
1065 return threads_executing
;
1069 set_stop_requested (ptid_t ptid
, int stop
)
1071 struct thread_info
*tp
;
1072 int all
= ptid
== minus_one_ptid
;
1074 if (all
|| ptid_is_pid (ptid
))
1076 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1077 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1078 tp
->stop_requested
= stop
;
1082 tp
= find_thread_ptid (ptid
);
1084 tp
->stop_requested
= stop
;
1087 /* Call the stop requested observer so other components of GDB can
1088 react to this request. */
1090 observer_notify_thread_stop_requested (ptid
);
1094 finish_thread_state (ptid_t ptid
)
1096 struct thread_info
*tp
;
1098 int any_started
= 0;
1100 all
= ptid
== minus_one_ptid
;
1102 if (all
|| ptid_is_pid (ptid
))
1104 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1106 if (tp
->state
== THREAD_EXITED
)
1108 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1110 if (set_running_thread (tp
, tp
->executing
))
1117 tp
= find_thread_ptid (ptid
);
1119 if (tp
->state
!= THREAD_EXITED
)
1121 if (set_running_thread (tp
, tp
->executing
))
1127 observer_notify_target_resumed (ptid
);
1131 finish_thread_state_cleanup (void *arg
)
1133 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1137 finish_thread_state (*ptid_p
);
1140 /* See gdbthread.h. */
1143 validate_registers_access (void)
1145 /* No selected thread, no registers. */
1146 if (inferior_ptid
== null_ptid
)
1147 error (_("No thread selected."));
1149 /* Don't try to read from a dead thread. */
1150 if (is_exited (inferior_ptid
))
1151 error (_("The current thread has terminated"));
1153 /* ... or from a spinning thread. FIXME: This isn't actually fully
1154 correct. It'll allow an user-requested access (e.g., "print $pc"
1155 at the prompt) when a thread is not executing for some internal
1156 reason, but is marked running from the user's perspective. E.g.,
1157 the thread is waiting for its turn in the step-over queue. */
1158 if (is_executing (inferior_ptid
))
1159 error (_("Selected thread is running."));
1162 /* See gdbthread.h. */
1165 can_access_registers_ptid (ptid_t ptid
)
1167 /* No thread, no registers. */
1168 if (ptid
== null_ptid
)
1171 /* Don't try to read from a dead thread. */
1172 if (is_exited (ptid
))
1175 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1176 if (is_executing (ptid
))
1183 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1185 return (pc
>= thread
->control
.step_range_start
1186 && pc
< thread
->control
.step_range_end
);
1189 /* Helper for print_thread_info. Returns true if THR should be
1190 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1191 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1192 is true if REQUESTED_THREADS is list of global IDs, false if a list
1193 of per-inferior thread ids. If PID is not -1, only print THR if it
1194 is a thread from the process PID. Otherwise, threads from all
1195 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1196 and PID is not -1, then the thread is printed if it belongs to the
1197 specified process. Otherwise, an error is raised. */
1200 should_print_thread (const char *requested_threads
, int default_inf_num
,
1201 int global_ids
, int pid
, struct thread_info
*thr
)
1203 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1208 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1210 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1211 thr
->inf
->num
, thr
->per_inf_num
);
1216 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1218 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1219 error (_("Requested thread not found in requested process"));
1223 if (thr
->state
== THREAD_EXITED
)
1229 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1230 whether REQUESTED_THREADS is a list of global or per-inferior
1234 print_thread_info_1 (struct ui_out
*uiout
, char *requested_threads
,
1235 int global_ids
, int pid
,
1236 int show_global_ids
)
1238 struct thread_info
*tp
;
1239 ptid_t current_ptid
;
1240 const char *extra_info
, *name
, *target_id
;
1241 struct inferior
*inf
;
1242 int default_inf_num
= current_inferior ()->num
;
1244 update_thread_list ();
1245 current_ptid
= inferior_ptid
;
1248 /* For backward compatibility, we make a list for MI. A table is
1249 preferable for the CLI, though, because it shows table
1251 gdb::optional
<ui_out_emit_list
> list_emitter
;
1252 gdb::optional
<ui_out_emit_table
> table_emitter
;
1254 if (uiout
->is_mi_like_p ())
1255 list_emitter
.emplace (uiout
, "threads");
1260 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1262 if (!should_print_thread (requested_threads
, default_inf_num
,
1263 global_ids
, pid
, tp
))
1271 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1272 uiout
->message (_("No threads.\n"));
1274 uiout
->message (_("No threads match '%s'.\n"),
1279 table_emitter
.emplace (uiout
,
1280 (show_global_ids
|| uiout
->is_mi_like_p ())
1282 n_threads
, "threads");
1284 uiout
->table_header (1, ui_left
, "current", "");
1286 if (!uiout
->is_mi_like_p ())
1287 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1288 if (show_global_ids
|| uiout
->is_mi_like_p ())
1289 uiout
->table_header (4, ui_left
, "id", "GId");
1290 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1291 uiout
->table_header (1, ui_left
, "frame", "Frame");
1292 uiout
->table_body ();
1295 /* We'll be switching threads temporarily. */
1296 scoped_restore_current_thread restore_thread
;
1298 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1302 if (!should_print_thread (requested_threads
, default_inf_num
,
1303 global_ids
, pid
, tp
))
1306 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
1308 if (!uiout
->is_mi_like_p ())
1310 if (tp
->ptid
== current_ptid
)
1311 uiout
->field_string ("current", "*");
1313 uiout
->field_skip ("current");
1316 if (!uiout
->is_mi_like_p ())
1317 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1319 if (show_global_ids
|| uiout
->is_mi_like_p ())
1320 uiout
->field_int ("id", tp
->global_num
);
1322 /* For the CLI, we stuff everything into the target-id field.
1323 This is a gross hack to make the output come out looking
1324 correct. The underlying problem here is that ui-out has no
1325 way to specify that a field's space allocation should be
1326 shared by several fields. For MI, we do the right thing
1329 target_id
= target_pid_to_str (tp
->ptid
);
1330 extra_info
= target_extra_thread_info (tp
);
1331 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1333 if (uiout
->is_mi_like_p ())
1335 uiout
->field_string ("target-id", target_id
);
1337 uiout
->field_string ("details", extra_info
);
1339 uiout
->field_string ("name", name
);
1343 std::string contents
;
1345 if (extra_info
&& name
)
1346 contents
= string_printf ("%s \"%s\" (%s)", target_id
,
1348 else if (extra_info
)
1349 contents
= string_printf ("%s (%s)", target_id
, extra_info
);
1351 contents
= string_printf ("%s \"%s\"", target_id
, name
);
1353 contents
= target_id
;
1355 uiout
->field_string ("target-id", contents
.c_str ());
1358 if (tp
->state
== THREAD_RUNNING
)
1359 uiout
->text ("(running)\n");
1362 /* The switch below puts us at the top of the stack (leaf
1364 switch_to_thread (tp
->ptid
);
1365 print_stack_frame (get_selected_frame (NULL
),
1366 /* For MI output, print frame level. */
1367 uiout
->is_mi_like_p (),
1371 if (uiout
->is_mi_like_p ())
1373 const char *state
= "stopped";
1375 if (tp
->state
== THREAD_RUNNING
)
1377 uiout
->field_string ("state", state
);
1380 core
= target_core_of_thread (tp
->ptid
);
1381 if (uiout
->is_mi_like_p () && core
!= -1)
1382 uiout
->field_int ("core", core
);
1385 /* This end scope restores the current thread and the frame
1386 selected before the "info threads" command, and it finishes the
1387 ui-out list or table. */
1390 if (pid
== -1 && requested_threads
== NULL
)
1392 if (uiout
->is_mi_like_p ()
1393 && inferior_ptid
!= null_ptid
)
1395 int num
= ptid_to_global_thread_id (inferior_ptid
);
1397 gdb_assert (num
!= 0);
1398 uiout
->field_int ("current-thread-id", num
);
1401 if (inferior_ptid
!= null_ptid
&& is_exited (inferior_ptid
))
1402 uiout
->message ("\n\
1403 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1404 print_thread_id (inferior_thread ()));
1405 else if (thread_list
!= NULL
&& inferior_ptid
== null_ptid
)
1406 uiout
->message ("\n\
1407 No selected thread. See `help thread'.\n");
1411 /* See gdbthread.h. */
1414 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1416 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1419 /* Implementation of the "info threads" command.
1421 Note: this has the drawback that it _really_ switches
1422 threads, which frees the frame cache. A no-side
1423 effects info-threads command would be nicer. */
1426 info_threads_command (char *arg
, int from_tty
)
1428 int show_global_ids
= 0;
1431 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1433 arg
= skip_spaces (arg
);
1434 show_global_ids
= 1;
1437 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1440 /* See gdbthread.h. */
1443 switch_to_thread_no_regs (struct thread_info
*thread
)
1445 struct inferior
*inf
= thread
->inf
;
1447 set_current_program_space (inf
->pspace
);
1448 set_current_inferior (inf
);
1450 inferior_ptid
= thread
->ptid
;
1451 stop_pc
= ~(CORE_ADDR
) 0;
1454 /* Switch to no thread selected. */
1457 switch_to_no_thread ()
1459 if (inferior_ptid
== null_ptid
)
1462 inferior_ptid
= null_ptid
;
1463 reinit_frame_cache ();
1464 stop_pc
= ~(CORE_ADDR
) 0;
1467 /* Switch from one thread to another. */
1470 switch_to_thread (thread_info
*thr
)
1472 gdb_assert (thr
!= NULL
);
1474 if (inferior_ptid
== thr
->ptid
)
1477 switch_to_thread_no_regs (thr
);
1479 reinit_frame_cache ();
1481 /* We don't check for is_stopped, because we're called at times
1482 while in the TARGET_RUNNING state, e.g., while handling an
1484 if (thr
->state
!= THREAD_EXITED
1486 stop_pc
= regcache_read_pc (get_thread_regcache (thr
->ptid
));
1489 /* See gdbthread.h. */
1492 switch_to_thread (ptid_t ptid
)
1494 if (ptid
== null_ptid
)
1495 switch_to_no_thread ();
1497 switch_to_thread (find_thread_ptid (ptid
));
1501 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1503 struct frame_info
*frame
= NULL
;
1506 /* This means there was no selected frame. */
1507 if (frame_level
== -1)
1509 select_frame (NULL
);
1513 gdb_assert (frame_level
>= 0);
1515 /* Restore by level first, check if the frame id is the same as
1516 expected. If that fails, try restoring by frame id. If that
1517 fails, nothing to do, just warn the user. */
1519 count
= frame_level
;
1520 frame
= find_relative_frame (get_current_frame (), &count
);
1523 /* The frame ids must match - either both valid or both outer_frame_id.
1524 The latter case is not failsafe, but since it's highly unlikely
1525 the search by level finds the wrong frame, it's 99.9(9)% of
1526 the time (for all practical purposes) safe. */
1527 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1529 /* Cool, all is fine. */
1530 select_frame (frame
);
1534 frame
= frame_find_by_id (a_frame_id
);
1537 /* Cool, refound it. */
1538 select_frame (frame
);
1542 /* Nothing else to do, the frame layout really changed. Select the
1543 innermost stack frame. */
1544 select_frame (get_current_frame ());
1546 /* Warn the user. */
1547 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1549 warning (_("Couldn't restore frame #%d in "
1550 "current thread. Bottom (innermost) frame selected:"),
1552 /* For MI, we should probably have a notification about
1553 current frame change. But this error is not very
1554 likely, so don't bother for now. */
1555 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1559 scoped_restore_current_thread::~scoped_restore_current_thread ()
1561 /* If an entry of thread_info was previously selected, it won't be
1562 deleted because we've increased its refcount. The thread represented
1563 by this thread_info entry may have already exited (due to normal exit,
1564 detach, etc), so the thread_info.state is THREAD_EXITED. */
1565 if (m_thread
!= NULL
1566 /* If the previously selected thread belonged to a process that has
1567 in the mean time exited (or killed, detached, etc.), then don't revert
1568 back to it, but instead simply drop back to no thread selected. */
1570 switch_to_thread (m_thread
);
1573 switch_to_no_thread ();
1574 set_current_inferior (m_inf
);
1577 /* The running state of the originally selected thread may have
1578 changed, so we have to recheck it here. */
1579 if (inferior_ptid
!= null_ptid
1581 && is_stopped (inferior_ptid
)
1582 && target_has_registers
1584 && target_has_memory
)
1585 restore_selected_frame (m_selected_frame_id
, m_selected_frame_level
);
1587 if (m_thread
!= NULL
)
1588 m_thread
->decref ();
1592 scoped_restore_current_thread::scoped_restore_current_thread ()
1595 m_inf
= current_inferior ();
1597 if (inferior_ptid
!= null_ptid
)
1599 thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1600 struct frame_info
*frame
;
1602 gdb_assert (tp
!= NULL
);
1604 m_was_stopped
= tp
->state
== THREAD_STOPPED
;
1606 && target_has_registers
1608 && target_has_memory
)
1610 /* When processing internal events, there might not be a
1611 selected frame. If we naively call get_selected_frame
1612 here, then we can end up reading debuginfo for the
1613 current frame, but we don't generally need the debuginfo
1615 frame
= get_selected_frame_if_set ();
1620 m_selected_frame_id
= get_frame_id (frame
);
1621 m_selected_frame_level
= frame_relative_level (frame
);
1630 /* See gdbthread.h. */
1633 show_thread_that_caused_stop (void)
1635 return highest_thread_num
> 1;
1638 /* See gdbthread.h. */
1641 show_inferior_qualified_tids (void)
1643 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1646 /* See gdbthread.h. */
1649 print_thread_id (struct thread_info
*thr
)
1651 char *s
= get_print_cell ();
1653 if (show_inferior_qualified_tids ())
1654 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1656 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1660 /* If true, tp_array_compar should sort in ascending order, otherwise
1661 in descending order. */
1663 static bool tp_array_compar_ascending
;
1665 /* Sort an array for struct thread_info pointers by thread ID (first
1666 by inferior number, and then by per-inferior thread number). The
1667 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1670 tp_array_compar (const thread_info
*a
, const thread_info
*b
)
1672 if (a
->inf
->num
!= b
->inf
->num
)
1674 if (tp_array_compar_ascending
)
1675 return a
->inf
->num
< b
->inf
->num
;
1677 return a
->inf
->num
> b
->inf
->num
;
1680 if (tp_array_compar_ascending
)
1681 return (a
->per_inf_num
< b
->per_inf_num
);
1683 return (a
->per_inf_num
> b
->per_inf_num
);
1686 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1687 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1688 of two numbers seperated by a hyphen. Examples:
1690 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1691 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1692 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1695 thread_apply_all_command (char *cmd
, int from_tty
)
1697 tp_array_compar_ascending
= false;
1699 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1701 cmd
= skip_spaces (cmd
);
1702 tp_array_compar_ascending
= true;
1705 if (cmd
== NULL
|| *cmd
== '\000')
1706 error (_("Please specify a command following the thread ID list"));
1708 update_thread_list ();
1710 /* Save a copy of the command in case it is clobbered by
1712 std::string saved_cmd
= cmd
;
1714 int tc
= live_threads_count ();
1717 /* Save a copy of the thread list and increment each thread's
1718 refcount while executing the command in the context of each
1719 thread, in case the command is one that wipes threads. E.g.,
1720 detach, kill, disconnect, etc., or even normally continuing
1721 over an inferior or thread exit. */
1722 std::vector
<thread_info
*> thr_list_cpy
;
1723 thr_list_cpy
.reserve (tc
);
1728 ALL_NON_EXITED_THREADS (tp
)
1730 thr_list_cpy
.push_back (tp
);
1733 gdb_assert (thr_list_cpy
.size () == tc
);
1736 /* Increment the refcounts, and restore them back on scope
1738 scoped_inc_dec_ref
inc_dec_ref (thr_list_cpy
);
1740 std::sort (thr_list_cpy
.begin (), thr_list_cpy
.end (), tp_array_compar
);
1742 scoped_restore_current_thread restore_thread
;
1744 for (thread_info
*thr
: thr_list_cpy
)
1745 if (thread_alive (thr
))
1747 switch_to_thread (thr
->ptid
);
1748 printf_filtered (_("\nThread %s (%s):\n"),
1749 print_thread_id (thr
),
1750 target_pid_to_str (inferior_ptid
));
1751 execute_command (cmd
, from_tty
);
1753 /* Restore exact command used previously. */
1754 strcpy (cmd
, saved_cmd
.c_str ());
1759 /* Implementation of the "thread apply" command. */
1762 thread_apply_command (char *tidlist
, int from_tty
)
1765 tid_range_parser parser
;
1767 if (tidlist
== NULL
|| *tidlist
== '\000')
1768 error (_("Please specify a thread ID list"));
1770 parser
.init (tidlist
, current_inferior ()->num
);
1771 while (!parser
.finished ())
1773 int inf_num
, thr_start
, thr_end
;
1775 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1777 cmd
= (char *) parser
.cur_tok ();
1783 error (_("Please specify a command following the thread ID list"));
1785 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1786 invalid_thread_id_error (cmd
);
1788 /* Save a copy of the command in case it is clobbered by
1790 std::string saved_cmd
= cmd
;
1792 scoped_restore_current_thread restore_thread
;
1794 parser
.init (tidlist
, current_inferior ()->num
);
1795 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1797 struct thread_info
*tp
= NULL
;
1798 struct inferior
*inf
;
1799 int inf_num
, thr_num
;
1801 parser
.get_tid (&inf_num
, &thr_num
);
1802 inf
= find_inferior_id (inf_num
);
1804 tp
= find_thread_id (inf
, thr_num
);
1806 if (parser
.in_star_range ())
1810 warning (_("Unknown inferior %d"), inf_num
);
1811 parser
.skip_range ();
1815 /* No use looking for threads past the highest thread number
1816 the inferior ever had. */
1817 if (thr_num
>= inf
->highest_thread_num
)
1818 parser
.skip_range ();
1820 /* Be quiet about unknown threads numbers. */
1827 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1828 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1830 warning (_("Unknown thread %d"), thr_num
);
1834 if (!thread_alive (tp
))
1836 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1840 switch_to_thread (tp
->ptid
);
1842 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1843 target_pid_to_str (inferior_ptid
));
1844 execute_command (cmd
, from_tty
);
1846 /* Restore exact command used previously. */
1847 strcpy (cmd
, saved_cmd
.c_str ());
1851 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1852 if prefix of arg is `apply'. */
1855 thread_command (char *tidstr
, int from_tty
)
1859 if (inferior_ptid
== null_ptid
)
1860 error (_("No thread selected"));
1862 if (target_has_stack
)
1864 struct thread_info
*tp
= inferior_thread ();
1866 if (is_exited (inferior_ptid
))
1867 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1868 print_thread_id (tp
),
1869 target_pid_to_str (inferior_ptid
));
1871 printf_filtered (_("[Current thread is %s (%s)]\n"),
1872 print_thread_id (tp
),
1873 target_pid_to_str (inferior_ptid
));
1876 error (_("No stack."));
1880 ptid_t previous_ptid
= inferior_ptid
;
1883 result
= gdb_thread_select (current_uiout
, tidstr
, NULL
);
1885 /* If thread switch did not succeed don't notify or print. */
1886 if (result
== GDB_RC_FAIL
)
1889 /* Print if the thread has not changed, otherwise an event will
1891 if (inferior_ptid
== previous_ptid
)
1893 print_selected_thread_frame (current_uiout
,
1894 USER_SELECTED_THREAD
1895 | USER_SELECTED_FRAME
);
1899 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1900 | USER_SELECTED_FRAME
);
1905 /* Implementation of `thread name'. */
1908 thread_name_command (char *arg
, int from_tty
)
1910 struct thread_info
*info
;
1912 if (inferior_ptid
== null_ptid
)
1913 error (_("No thread selected"));
1915 arg
= skip_spaces (arg
);
1917 info
= inferior_thread ();
1919 info
->name
= arg
? xstrdup (arg
) : NULL
;
1922 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1925 thread_find_command (char *arg
, int from_tty
)
1927 struct thread_info
*tp
;
1929 unsigned long match
= 0;
1931 if (arg
== NULL
|| *arg
== '\0')
1932 error (_("Command requires an argument."));
1934 tmp
= re_comp (arg
);
1936 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1938 update_thread_list ();
1939 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1941 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1943 printf_filtered (_("Thread %s has name '%s'\n"),
1944 print_thread_id (tp
), tp
->name
);
1948 tmp
= target_thread_name (tp
);
1949 if (tmp
!= NULL
&& re_exec (tmp
))
1951 printf_filtered (_("Thread %s has target name '%s'\n"),
1952 print_thread_id (tp
), tmp
);
1956 tmp
= target_pid_to_str (tp
->ptid
);
1957 if (tmp
!= NULL
&& re_exec (tmp
))
1959 printf_filtered (_("Thread %s has target id '%s'\n"),
1960 print_thread_id (tp
), tmp
);
1964 tmp
= target_extra_thread_info (tp
);
1965 if (tmp
!= NULL
&& re_exec (tmp
))
1967 printf_filtered (_("Thread %s has extra info '%s'\n"),
1968 print_thread_id (tp
), tmp
);
1973 printf_filtered (_("No threads match '%s'\n"), arg
);
1976 /* Print notices when new threads are attached and detached. */
1977 int print_thread_events
= 1;
1979 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1980 struct cmd_list_element
*c
, const char *value
)
1982 fprintf_filtered (file
,
1983 _("Printing of thread events is %s.\n"),
1988 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr_v
)
1990 const char *tidstr
= (const char *) tidstr_v
;
1991 struct thread_info
*tp
;
1993 if (uiout
->is_mi_like_p ())
1995 int num
= value_as_long (parse_and_eval (tidstr
));
1997 tp
= find_thread_global_id (num
);
1999 error (_("Thread ID %d not known."), num
);
2003 tp
= parse_thread_id (tidstr
, NULL
);
2004 gdb_assert (tp
!= NULL
);
2007 if (!thread_alive (tp
))
2008 error (_("Thread ID %s has terminated."), tidstr
);
2010 switch_to_thread (tp
->ptid
);
2012 annotate_thread_changed ();
2014 /* Since the current thread may have changed, see if there is any
2015 exited thread we can now delete. */
2021 /* Print thread and frame switch command response. */
2024 print_selected_thread_frame (struct ui_out
*uiout
,
2025 user_selected_what selection
)
2027 struct thread_info
*tp
= inferior_thread ();
2028 struct inferior
*inf
= current_inferior ();
2030 if (selection
& USER_SELECTED_THREAD
)
2032 if (uiout
->is_mi_like_p ())
2034 uiout
->field_int ("new-thread-id",
2035 inferior_thread ()->global_num
);
2039 uiout
->text ("[Switching to thread ");
2040 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2042 uiout
->text (target_pid_to_str (inferior_ptid
));
2047 if (tp
->state
== THREAD_RUNNING
)
2049 if (selection
& USER_SELECTED_THREAD
)
2050 uiout
->text ("(running)\n");
2052 else if (selection
& USER_SELECTED_FRAME
)
2054 if (selection
& USER_SELECTED_THREAD
)
2057 if (has_stack_frames ())
2058 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2064 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
2066 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
2067 error_message
, RETURN_MASK_ALL
) < 0)
2072 /* Update the 'threads_executing' global based on the threads we know
2076 update_threads_executing (void)
2078 struct thread_info
*tp
;
2080 threads_executing
= 0;
2081 ALL_NON_EXITED_THREADS (tp
)
2085 threads_executing
= 1;
2092 update_thread_list (void)
2094 target_update_thread_list ();
2095 update_threads_executing ();
2098 /* Return a new value for the selected thread's id. Return a value of
2099 0 if no thread is selected. If GLOBAL is true, return the thread's
2100 global number. Otherwise return the per-inferior number. */
2102 static struct value
*
2103 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2105 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2111 int_val
= tp
->global_num
;
2113 int_val
= tp
->per_inf_num
;
2115 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2118 /* Return a new value for the selected thread's per-inferior thread
2119 number. Return a value of 0 if no thread is selected, or no
2122 static struct value
*
2123 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
,
2124 struct internalvar
*var
,
2127 return thread_num_make_value_helper (gdbarch
, 0);
2130 /* Return a new value for the selected thread's global id. Return a
2131 value of 0 if no thread is selected, or no threads exist. */
2133 static struct value
*
2134 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2137 return thread_num_make_value_helper (gdbarch
, 1);
2140 /* Commands with a prefix of `thread'. */
2141 struct cmd_list_element
*thread_cmd_list
= NULL
;
2143 /* Implementation of `thread' variable. */
2145 static const struct internalvar_funcs thread_funcs
=
2147 thread_id_per_inf_num_make_value
,
2152 /* Implementation of `gthread' variable. */
2154 static const struct internalvar_funcs gthread_funcs
=
2156 global_thread_id_make_value
,
2162 _initialize_thread (void)
2164 static struct cmd_list_element
*thread_apply_list
= NULL
;
2166 add_info ("threads", info_threads_command
,
2167 _("Display currently known threads.\n\
2168 Usage: info threads [-gid] [ID]...\n\
2169 -gid: Show global thread IDs.\n\
2170 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2171 Otherwise, all threads are displayed."));
2173 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2174 Use this command to switch between threads.\n\
2175 The new thread ID must be currently known."),
2176 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2178 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2179 _("Apply a command to a list of threads."),
2180 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2182 add_cmd ("all", class_run
, thread_apply_all_command
,
2184 Apply a command to all threads.\n\
2186 Usage: thread apply all [-ascending] <command>\n\
2187 -ascending: Call <command> for all threads in ascending order.\n\
2188 The default is descending order.\
2190 &thread_apply_list
);
2192 add_cmd ("name", class_run
, thread_name_command
,
2193 _("Set the current thread's name.\n\
2194 Usage: thread name [NAME]\n\
2195 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2197 add_cmd ("find", class_run
, thread_find_command
, _("\
2198 Find threads that match a regular expression.\n\
2199 Usage: thread find REGEXP\n\
2200 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2203 add_com_alias ("t", "thread", class_run
, 1);
2205 add_setshow_boolean_cmd ("thread-events", no_class
,
2206 &print_thread_events
, _("\
2207 Set printing of thread events (such as thread start and exit)."), _("\
2208 Show printing of thread events (such as thread start and exit)."), NULL
,
2210 show_print_thread_events
,
2211 &setprintlist
, &showprintlist
);
2213 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2214 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);