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 /* Definition of struct thread_info exported to gdbthread.h. */
50 /* Prototypes for exported functions. */
52 void _initialize_thread (void);
54 /* Prototypes for local functions. */
56 struct thread_info
*thread_list
= NULL
;
57 static int highest_thread_num
;
59 /* True if any thread is, or may be executing. We need to track this
60 separately because until we fully sync the thread list, we won't
61 know whether the target is fully stopped, even if we see stop
62 events for all known threads, because any of those threads may have
63 spawned new threads we haven't heard of yet. */
64 static int threads_executing
;
66 static void thread_apply_all_command (char *, int);
67 static int thread_alive (struct thread_info
*);
68 static void info_threads_command (char *, int);
69 static void thread_apply_command (char *, int);
70 static void restore_current_thread (ptid_t
);
72 /* Data to cleanup thread array. */
74 struct thread_array_cleanup
76 /* Array of thread pointers used to set
78 struct thread_info
**tp_array
;
80 /* Thread count in the array. */
86 inferior_thread (void)
88 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
93 /* Delete the breakpoint pointed at by BP_P, if there's one. */
96 delete_thread_breakpoint (struct breakpoint
**bp_p
)
100 delete_breakpoint (*bp_p
);
106 delete_step_resume_breakpoint (struct thread_info
*tp
)
109 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
113 delete_exception_resume_breakpoint (struct thread_info
*tp
)
116 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
119 /* See gdbthread.h. */
122 delete_single_step_breakpoints (struct thread_info
*tp
)
125 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
128 /* Delete the breakpoint pointed at by BP_P at the next stop, if
132 delete_at_next_stop (struct breakpoint
**bp
)
136 (*bp
)->disposition
= disp_del_at_next_stop
;
141 /* See gdbthread.h. */
144 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
146 return tp
->control
.single_step_breakpoints
!= NULL
;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
153 struct address_space
*aspace
,
156 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
158 return (ss_bps
!= NULL
159 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
162 /* See gdbthread.h. */
165 thread_cancel_execution_command (struct thread_info
*thr
)
167 if (thr
->thread_fsm
!= NULL
)
169 thread_fsm_clean_up (thr
->thread_fsm
, thr
);
170 thread_fsm_delete (thr
->thread_fsm
);
171 thr
->thread_fsm
= NULL
;
176 clear_thread_inferior_resources (struct thread_info
*tp
)
178 /* NOTE: this will take care of any left-over step_resume breakpoints,
179 but not any user-specified thread-specific breakpoints. We can not
180 delete the breakpoint straight-off, because the inferior might not
181 be stopped at the moment. */
182 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
183 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
184 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
186 delete_longjmp_breakpoint_at_next_stop (tp
->global_num
);
188 bpstat_clear (&tp
->control
.stop_bpstat
);
190 btrace_teardown (tp
);
192 thread_cancel_execution_command (tp
);
196 init_thread_list (void)
198 struct thread_info
*tp
, *tpnext
;
200 highest_thread_num
= 0;
205 for (tp
= thread_list
; tp
; tp
= tpnext
)
212 threads_executing
= 0;
215 /* Allocate a new thread of inferior INF with target id PTID and add
216 it to the thread list. */
218 static struct thread_info
*
219 new_thread (struct inferior
*inf
, ptid_t ptid
)
221 thread_info
*tp
= new thread_info (inf
, ptid
);
223 if (thread_list
== NULL
)
227 struct thread_info
*last
;
229 for (last
= thread_list
; last
->next
!= NULL
; last
= last
->next
)
238 add_thread_silent (ptid_t ptid
)
240 struct thread_info
*tp
;
241 struct inferior
*inf
= find_inferior_ptid (ptid
);
242 gdb_assert (inf
!= NULL
);
244 tp
= find_thread_ptid (ptid
);
246 /* Found an old thread with the same id. It has to be dead,
247 otherwise we wouldn't be adding a new thread with the same id.
248 The OS is reusing this id --- delete it, and recreate a new
251 /* In addition to deleting the thread, if this is the current
252 thread, then we need to take care that delete_thread doesn't
253 really delete the thread if it is inferior_ptid. Create a
254 new template thread in the list with an invalid ptid, switch
255 to it, delete the original thread, reset the new thread's
256 ptid, and switch to it. */
258 if (ptid_equal (inferior_ptid
, ptid
))
260 tp
= new_thread (inf
, null_ptid
);
262 /* Make switch_to_thread not read from the thread. */
263 tp
->state
= THREAD_EXITED
;
264 switch_to_thread (null_ptid
);
266 /* Now we can delete it. */
267 delete_thread (ptid
);
269 /* Now reset its ptid, and reswitch inferior_ptid to it. */
271 tp
->state
= THREAD_STOPPED
;
272 switch_to_thread (ptid
);
274 observer_notify_new_thread (tp
);
280 /* Just go ahead and delete it. */
281 delete_thread (ptid
);
284 tp
= new_thread (inf
, ptid
);
285 observer_notify_new_thread (tp
);
291 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
293 struct thread_info
*result
= add_thread_silent (ptid
);
297 if (print_thread_events
)
298 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
300 annotate_new_thread ();
305 add_thread (ptid_t ptid
)
307 return add_thread_with_info (ptid
, NULL
);
310 thread_info::thread_info (struct inferior
*inf_
, ptid_t ptid_
)
311 : ptid (ptid_
), inf (inf_
)
313 gdb_assert (inf_
!= NULL
);
315 this->global_num
= ++highest_thread_num
;
316 this->per_inf_num
= ++inf_
->highest_thread_num
;
318 /* Nothing to follow yet. */
319 memset (&this->pending_follow
, 0, sizeof (this->pending_follow
));
320 this->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
321 this->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
324 thread_info::~thread_info ()
328 if (this->private_dtor
)
329 this->private_dtor (this->priv
);
337 /* Add TP to the end of the step-over chain LIST_P. */
340 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
342 gdb_assert (tp
->step_over_next
== NULL
);
343 gdb_assert (tp
->step_over_prev
== NULL
);
348 tp
->step_over_prev
= tp
->step_over_next
= tp
;
352 struct thread_info
*head
= *list_p
;
353 struct thread_info
*tail
= head
->step_over_prev
;
355 tp
->step_over_prev
= tail
;
356 tp
->step_over_next
= head
;
357 head
->step_over_prev
= tp
;
358 tail
->step_over_next
= tp
;
362 /* Remove TP from step-over chain LIST_P. */
365 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
367 gdb_assert (tp
->step_over_next
!= NULL
);
368 gdb_assert (tp
->step_over_prev
!= NULL
);
372 if (tp
== tp
->step_over_next
)
375 *list_p
= tp
->step_over_next
;
378 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
379 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
380 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
383 /* See gdbthread.h. */
386 thread_step_over_chain_next (struct thread_info
*tp
)
388 struct thread_info
*next
= tp
->step_over_next
;
390 return (next
== step_over_queue_head
? NULL
: next
);
393 /* See gdbthread.h. */
396 thread_is_in_step_over_chain (struct thread_info
*tp
)
398 return (tp
->step_over_next
!= NULL
);
401 /* See gdbthread.h. */
404 thread_step_over_chain_enqueue (struct thread_info
*tp
)
406 step_over_chain_enqueue (&step_over_queue_head
, tp
);
409 /* See gdbthread.h. */
412 thread_step_over_chain_remove (struct thread_info
*tp
)
414 step_over_chain_remove (&step_over_queue_head
, tp
);
417 /* Delete thread PTID. If SILENT, don't notify the observer of this
420 delete_thread_1 (ptid_t ptid
, int silent
)
422 struct thread_info
*tp
, *tpprev
;
426 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
427 if (ptid_equal (tp
->ptid
, ptid
))
433 /* Dead threads don't need to step-over. Remove from queue. */
434 if (tp
->step_over_next
!= NULL
)
435 thread_step_over_chain_remove (tp
);
437 /* If this is the current thread, or there's code out there that
438 relies on it existing (refcount > 0) we can't delete yet. Mark
439 it as exited, and notify it. */
441 || ptid_equal (tp
->ptid
, inferior_ptid
))
443 if (tp
->state
!= THREAD_EXITED
)
445 observer_notify_thread_exit (tp
, silent
);
447 /* Tag it as exited. */
448 tp
->state
= THREAD_EXITED
;
450 /* Clear breakpoints, etc. associated with this thread. */
451 clear_thread_inferior_resources (tp
);
454 /* Will be really deleted some other time. */
458 /* Notify thread exit, but only if we haven't already. */
459 if (tp
->state
!= THREAD_EXITED
)
460 observer_notify_thread_exit (tp
, silent
);
462 /* Tag it as exited. */
463 tp
->state
= THREAD_EXITED
;
464 clear_thread_inferior_resources (tp
);
467 tpprev
->next
= tp
->next
;
469 thread_list
= tp
->next
;
474 /* Delete thread PTID and notify of thread exit. If this is
475 inferior_ptid, don't actually delete it, but tag it as exited and
476 do the notification. If PTID is the user selected thread, clear
479 delete_thread (ptid_t ptid
)
481 delete_thread_1 (ptid
, 0 /* not silent */);
485 delete_thread_silent (ptid_t ptid
)
487 delete_thread_1 (ptid
, 1 /* silent */);
491 find_thread_global_id (int global_id
)
493 struct thread_info
*tp
;
495 for (tp
= thread_list
; tp
; tp
= tp
->next
)
496 if (tp
->global_num
== global_id
)
502 static struct thread_info
*
503 find_thread_id (struct inferior
*inf
, int thr_num
)
505 struct thread_info
*tp
;
507 for (tp
= thread_list
; tp
; tp
= tp
->next
)
508 if (tp
->inf
== inf
&& tp
->per_inf_num
== thr_num
)
514 /* Find a thread_info by matching PTID. */
516 find_thread_ptid (ptid_t ptid
)
518 struct thread_info
*tp
;
520 for (tp
= thread_list
; tp
; tp
= tp
->next
)
521 if (ptid_equal (tp
->ptid
, ptid
))
528 * Thread iterator function.
530 * Calls a callback function once for each thread, so long as
531 * the callback function returns false. If the callback function
532 * returns true, the iteration will end and the current thread
533 * will be returned. This can be useful for implementing a
534 * search for a thread with arbitrary attributes, or for applying
535 * some operation to every thread.
537 * FIXME: some of the existing functionality, such as
538 * "Thread apply all", might be rewritten using this functionality.
542 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
545 struct thread_info
*tp
, *next
;
547 for (tp
= thread_list
; tp
; tp
= next
)
550 if ((*callback
) (tp
, data
))
561 struct thread_info
*tp
;
563 for (tp
= thread_list
; tp
; tp
= tp
->next
)
570 valid_global_thread_id (int global_id
)
572 struct thread_info
*tp
;
574 for (tp
= thread_list
; tp
; tp
= tp
->next
)
575 if (tp
->global_num
== global_id
)
582 ptid_to_global_thread_id (ptid_t ptid
)
584 struct thread_info
*tp
;
586 for (tp
= thread_list
; tp
; tp
= tp
->next
)
587 if (ptid_equal (tp
->ptid
, ptid
))
588 return tp
->global_num
;
594 global_thread_id_to_ptid (int global_id
)
596 struct thread_info
*thread
= find_thread_global_id (global_id
);
601 return minus_one_ptid
;
605 in_thread_list (ptid_t ptid
)
607 struct thread_info
*tp
;
609 for (tp
= thread_list
; tp
; tp
= tp
->next
)
610 if (ptid_equal (tp
->ptid
, ptid
))
613 return 0; /* Never heard of 'im. */
616 /* Finds the first thread of the inferior given by PID. If PID is -1,
617 return the first thread in the list. */
620 first_thread_of_process (int pid
)
622 struct thread_info
*tp
, *ret
= NULL
;
624 for (tp
= thread_list
; tp
; tp
= tp
->next
)
625 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
626 if (ret
== NULL
|| tp
->global_num
< ret
->global_num
)
633 any_thread_of_process (int pid
)
635 struct thread_info
*tp
;
637 gdb_assert (pid
!= 0);
639 /* Prefer the current thread. */
640 if (ptid_get_pid (inferior_ptid
) == pid
)
641 return inferior_thread ();
643 ALL_NON_EXITED_THREADS (tp
)
644 if (ptid_get_pid (tp
->ptid
) == pid
)
651 any_live_thread_of_process (int pid
)
653 struct thread_info
*curr_tp
= NULL
;
654 struct thread_info
*tp
;
655 struct thread_info
*tp_executing
= NULL
;
657 gdb_assert (pid
!= 0);
659 /* Prefer the current thread if it's not executing. */
660 if (ptid_get_pid (inferior_ptid
) == pid
)
662 /* If the current thread is dead, forget it. If it's not
663 executing, use it. Otherwise, still choose it (below), but
664 only if no other non-executing thread is found. */
665 curr_tp
= inferior_thread ();
666 if (curr_tp
->state
== THREAD_EXITED
)
668 else if (!curr_tp
->executing
)
672 ALL_NON_EXITED_THREADS (tp
)
673 if (ptid_get_pid (tp
->ptid
) == pid
)
681 /* If both the current thread and all live threads are executing,
682 prefer the current thread. */
686 /* Otherwise, just return an executing thread, if any. */
690 /* Print a list of thread ids currently known, and the total number of
691 threads. To be used from within catch_errors. */
693 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
695 struct thread_info
*tp
;
697 struct cleanup
*cleanup_chain
;
698 int current_thread
= -1;
700 update_thread_list ();
702 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
704 for (tp
= thread_list
; tp
; tp
= tp
->next
)
706 if (tp
->state
== THREAD_EXITED
)
709 if (ptid_equal (tp
->ptid
, inferior_ptid
))
710 current_thread
= tp
->global_num
;
713 uiout
->field_int ("thread-id", tp
->global_num
);
716 do_cleanups (cleanup_chain
);
718 if (current_thread
!= -1)
719 uiout
->field_int ("current-thread-id", current_thread
);
720 uiout
->field_int ("number-of-threads", num
);
724 /* Official gdblib interface function to get a list of thread ids and
727 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
729 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
730 error_message
, RETURN_MASK_ALL
) < 0)
735 /* Return true if TP is an active thread. */
737 thread_alive (struct thread_info
*tp
)
739 if (tp
->state
== THREAD_EXITED
)
741 if (!target_thread_alive (tp
->ptid
))
746 /* See gdbthreads.h. */
751 struct thread_info
*tp
, *tmp
;
753 ALL_THREADS_SAFE (tp
, tmp
)
755 if (!thread_alive (tp
))
756 delete_thread (tp
->ptid
);
760 /* See gdbthreads.h. */
763 delete_exited_threads (void)
765 struct thread_info
*tp
, *tmp
;
767 ALL_THREADS_SAFE (tp
, tmp
)
769 if (tp
->state
== THREAD_EXITED
)
770 delete_thread (tp
->ptid
);
774 /* Disable storing stack temporaries for the thread whose id is
778 disable_thread_stack_temporaries (void *data
)
780 ptid_t
*pd
= (ptid_t
*) data
;
781 struct thread_info
*tp
= find_thread_ptid (*pd
);
785 tp
->stack_temporaries_enabled
= 0;
786 VEC_free (value_ptr
, tp
->stack_temporaries
);
792 /* Enable storing stack temporaries for thread with id PTID and return a
793 cleanup which can disable and clear the stack temporaries. */
796 enable_thread_stack_temporaries (ptid_t ptid
)
798 struct thread_info
*tp
= find_thread_ptid (ptid
);
802 gdb_assert (tp
!= NULL
);
804 tp
->stack_temporaries_enabled
= 1;
805 tp
->stack_temporaries
= NULL
;
806 data
= XNEW (ptid_t
);
808 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
813 /* Return non-zero value if stack temporaies are enabled for the thread
817 thread_stack_temporaries_enabled_p (ptid_t ptid
)
819 struct thread_info
*tp
= find_thread_ptid (ptid
);
824 return tp
->stack_temporaries_enabled
;
827 /* Push V on to the stack temporaries of the thread with id PTID. */
830 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
832 struct thread_info
*tp
= find_thread_ptid (ptid
);
834 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
835 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
838 /* Return 1 if VAL is among the stack temporaries of the thread
839 with id PTID. Return 0 otherwise. */
842 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
844 struct thread_info
*tp
= find_thread_ptid (ptid
);
846 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
847 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
852 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
860 /* Return the last of the stack temporaries for thread with id PTID.
861 Return NULL if there are no stack temporaries for the thread. */
864 get_last_thread_stack_temporary (ptid_t ptid
)
866 struct value
*lastval
= NULL
;
867 struct thread_info
*tp
= find_thread_ptid (ptid
);
869 gdb_assert (tp
!= NULL
);
870 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
871 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
877 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
879 struct inferior
*inf
;
880 struct thread_info
*tp
;
882 /* It can happen that what we knew as the target inferior id
883 changes. E.g, target remote may only discover the remote process
884 pid after adding the inferior to GDB's list. */
885 inf
= find_inferior_ptid (old_ptid
);
886 inf
->pid
= ptid_get_pid (new_ptid
);
888 tp
= find_thread_ptid (old_ptid
);
891 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
894 /* See gdbthread.h. */
897 set_resumed (ptid_t ptid
, int resumed
)
899 struct thread_info
*tp
;
900 int all
= ptid_equal (ptid
, minus_one_ptid
);
902 if (all
|| ptid_is_pid (ptid
))
904 for (tp
= thread_list
; tp
; tp
= tp
->next
)
905 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
906 tp
->resumed
= resumed
;
910 tp
= find_thread_ptid (ptid
);
911 gdb_assert (tp
!= NULL
);
912 tp
->resumed
= resumed
;
916 /* Helper for set_running, that marks one thread either running or
920 set_running_thread (struct thread_info
*tp
, int running
)
924 if (running
&& tp
->state
== THREAD_STOPPED
)
926 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
930 /* If the thread is now marked stopped, remove it from
931 the step-over queue, so that we don't try to resume
932 it until the user wants it to. */
933 if (tp
->step_over_next
!= NULL
)
934 thread_step_over_chain_remove (tp
);
941 set_running (ptid_t ptid
, int running
)
943 struct thread_info
*tp
;
944 int all
= ptid_equal (ptid
, minus_one_ptid
);
947 /* We try not to notify the observer if no thread has actually changed
948 the running state -- merely to reduce the number of messages to
949 frontend. Frontend is supposed to handle multiple *running just fine. */
950 if (all
|| ptid_is_pid (ptid
))
952 for (tp
= thread_list
; tp
; tp
= tp
->next
)
953 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
955 if (tp
->state
== THREAD_EXITED
)
958 if (set_running_thread (tp
, running
))
964 tp
= find_thread_ptid (ptid
);
965 gdb_assert (tp
!= NULL
);
966 gdb_assert (tp
->state
!= THREAD_EXITED
);
967 if (set_running_thread (tp
, running
))
971 observer_notify_target_resumed (ptid
);
975 is_thread_state (ptid_t ptid
, enum thread_state state
)
977 struct thread_info
*tp
;
979 tp
= find_thread_ptid (ptid
);
981 return tp
->state
== state
;
985 is_stopped (ptid_t ptid
)
987 return is_thread_state (ptid
, THREAD_STOPPED
);
991 is_exited (ptid_t ptid
)
993 return is_thread_state (ptid
, THREAD_EXITED
);
997 is_running (ptid_t ptid
)
999 return is_thread_state (ptid
, THREAD_RUNNING
);
1003 is_executing (ptid_t ptid
)
1005 struct thread_info
*tp
;
1007 tp
= find_thread_ptid (ptid
);
1009 return tp
->executing
;
1013 set_executing (ptid_t ptid
, int executing
)
1015 struct thread_info
*tp
;
1016 int all
= ptid_equal (ptid
, minus_one_ptid
);
1018 if (all
|| ptid_is_pid (ptid
))
1020 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1021 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1022 tp
->executing
= executing
;
1026 tp
= find_thread_ptid (ptid
);
1028 tp
->executing
= executing
;
1031 /* It only takes one running thread to spawn more threads.*/
1033 threads_executing
= 1;
1034 /* Only clear the flag if the caller is telling us everything is
1036 else if (ptid_equal (minus_one_ptid
, ptid
))
1037 threads_executing
= 0;
1040 /* See gdbthread.h. */
1043 threads_are_executing (void)
1045 return threads_executing
;
1049 set_stop_requested (ptid_t ptid
, int stop
)
1051 struct thread_info
*tp
;
1052 int all
= ptid_equal (ptid
, minus_one_ptid
);
1054 if (all
|| ptid_is_pid (ptid
))
1056 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1057 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1058 tp
->stop_requested
= stop
;
1062 tp
= find_thread_ptid (ptid
);
1064 tp
->stop_requested
= stop
;
1067 /* Call the stop requested observer so other components of GDB can
1068 react to this request. */
1070 observer_notify_thread_stop_requested (ptid
);
1074 finish_thread_state (ptid_t ptid
)
1076 struct thread_info
*tp
;
1078 int any_started
= 0;
1080 all
= ptid_equal (ptid
, minus_one_ptid
);
1082 if (all
|| ptid_is_pid (ptid
))
1084 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1086 if (tp
->state
== THREAD_EXITED
)
1088 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1090 if (set_running_thread (tp
, tp
->executing
))
1097 tp
= find_thread_ptid (ptid
);
1099 if (tp
->state
!= THREAD_EXITED
)
1101 if (set_running_thread (tp
, tp
->executing
))
1107 observer_notify_target_resumed (ptid
);
1111 finish_thread_state_cleanup (void *arg
)
1113 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1117 finish_thread_state (*ptid_p
);
1120 /* See gdbthread.h. */
1123 validate_registers_access (void)
1125 /* No selected thread, no registers. */
1126 if (ptid_equal (inferior_ptid
, null_ptid
))
1127 error (_("No thread selected."));
1129 /* Don't try to read from a dead thread. */
1130 if (is_exited (inferior_ptid
))
1131 error (_("The current thread has terminated"));
1133 /* ... or from a spinning thread. FIXME: This isn't actually fully
1134 correct. It'll allow an user-requested access (e.g., "print $pc"
1135 at the prompt) when a thread is not executing for some internal
1136 reason, but is marked running from the user's perspective. E.g.,
1137 the thread is waiting for its turn in the step-over queue. */
1138 if (is_executing (inferior_ptid
))
1139 error (_("Selected thread is running."));
1142 /* See gdbthread.h. */
1145 can_access_registers_ptid (ptid_t ptid
)
1147 /* No thread, no registers. */
1148 if (ptid_equal (ptid
, null_ptid
))
1151 /* Don't try to read from a dead thread. */
1152 if (is_exited (ptid
))
1155 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
1156 if (is_executing (ptid
))
1163 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1165 return (pc
>= thread
->control
.step_range_start
1166 && pc
< thread
->control
.step_range_end
);
1169 /* Helper for print_thread_info. Returns true if THR should be
1170 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
1171 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
1172 is true if REQUESTED_THREADS is list of global IDs, false if a list
1173 of per-inferior thread ids. If PID is not -1, only print THR if it
1174 is a thread from the process PID. Otherwise, threads from all
1175 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
1176 and PID is not -1, then the thread is printed if it belongs to the
1177 specified process. Otherwise, an error is raised. */
1180 should_print_thread (const char *requested_threads
, int default_inf_num
,
1181 int global_ids
, int pid
, struct thread_info
*thr
)
1183 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1188 in_list
= number_is_in_list (requested_threads
, thr
->global_num
);
1190 in_list
= tid_is_in_list (requested_threads
, default_inf_num
,
1191 thr
->inf
->num
, thr
->per_inf_num
);
1196 if (pid
!= -1 && ptid_get_pid (thr
->ptid
) != pid
)
1198 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1199 error (_("Requested thread not found in requested process"));
1203 if (thr
->state
== THREAD_EXITED
)
1209 /* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1210 whether REQUESTED_THREADS is a list of global or per-inferior
1214 print_thread_info_1 (struct ui_out
*uiout
, char *requested_threads
,
1215 int global_ids
, int pid
,
1216 int show_global_ids
)
1218 struct thread_info
*tp
;
1219 ptid_t current_ptid
;
1220 struct cleanup
*old_chain
;
1221 const char *extra_info
, *name
, *target_id
;
1222 struct inferior
*inf
;
1223 int default_inf_num
= current_inferior ()->num
;
1225 update_thread_list ();
1226 current_ptid
= inferior_ptid
;
1228 /* We'll be switching threads temporarily. */
1229 old_chain
= make_cleanup_restore_current_thread ();
1231 /* For backward compatibility, we make a list for MI. A table is
1232 preferable for the CLI, though, because it shows table
1234 if (uiout
->is_mi_like_p ())
1235 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
1240 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1242 if (!should_print_thread (requested_threads
, default_inf_num
,
1243 global_ids
, pid
, tp
))
1251 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1252 uiout
->message (_("No threads.\n"));
1254 uiout
->message (_("No threads match '%s'.\n"),
1256 do_cleanups (old_chain
);
1260 if (show_global_ids
|| uiout
->is_mi_like_p ())
1261 make_cleanup_ui_out_table_begin_end (uiout
, 5, n_threads
, "threads");
1263 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
1265 uiout
->table_header (1, ui_left
, "current", "");
1267 if (!uiout
->is_mi_like_p ())
1268 uiout
->table_header (4, ui_left
, "id-in-tg", "Id");
1269 if (show_global_ids
|| uiout
->is_mi_like_p ())
1270 uiout
->table_header (4, ui_left
, "id", "GId");
1271 uiout
->table_header (17, ui_left
, "target-id", "Target Id");
1272 uiout
->table_header (1, ui_left
, "frame", "Frame");
1273 uiout
->table_body ();
1276 ALL_THREADS_BY_INFERIOR (inf
, tp
)
1278 struct cleanup
*chain2
;
1281 if (!should_print_thread (requested_threads
, default_inf_num
,
1282 global_ids
, pid
, tp
))
1285 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1287 if (uiout
->is_mi_like_p ())
1289 /* Compatibility. */
1290 if (ptid_equal (tp
->ptid
, current_ptid
))
1297 if (ptid_equal (tp
->ptid
, current_ptid
))
1298 uiout
->field_string ("current", "*");
1300 uiout
->field_skip ("current");
1303 if (!uiout
->is_mi_like_p ())
1304 uiout
->field_string ("id-in-tg", print_thread_id (tp
));
1306 if (show_global_ids
|| uiout
->is_mi_like_p ())
1307 uiout
->field_int ("id", tp
->global_num
);
1309 /* For the CLI, we stuff everything into the target-id field.
1310 This is a gross hack to make the output come out looking
1311 correct. The underlying problem here is that ui-out has no
1312 way to specify that a field's space allocation should be
1313 shared by several fields. For MI, we do the right thing
1316 target_id
= target_pid_to_str (tp
->ptid
);
1317 extra_info
= target_extra_thread_info (tp
);
1318 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1320 if (uiout
->is_mi_like_p ())
1322 uiout
->field_string ("target-id", target_id
);
1324 uiout
->field_string ("details", extra_info
);
1326 uiout
->field_string ("name", name
);
1330 struct cleanup
*str_cleanup
;
1333 if (extra_info
&& name
)
1334 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
1336 else if (extra_info
)
1337 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
1339 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
1341 contents
= xstrdup (target_id
);
1342 str_cleanup
= make_cleanup (xfree
, contents
);
1344 uiout
->field_string ("target-id", contents
);
1345 do_cleanups (str_cleanup
);
1348 if (tp
->state
== THREAD_RUNNING
)
1349 uiout
->text ("(running)\n");
1352 /* The switch below puts us at the top of the stack (leaf
1354 switch_to_thread (tp
->ptid
);
1355 print_stack_frame (get_selected_frame (NULL
),
1356 /* For MI output, print frame level. */
1357 uiout
->is_mi_like_p (),
1361 if (uiout
->is_mi_like_p ())
1363 char *state
= "stopped";
1365 if (tp
->state
== THREAD_RUNNING
)
1367 uiout
->field_string ("state", state
);
1370 core
= target_core_of_thread (tp
->ptid
);
1371 if (uiout
->is_mi_like_p () && core
!= -1)
1372 uiout
->field_int ("core", core
);
1374 do_cleanups (chain2
);
1377 /* Restores the current thread and the frame selected before
1378 the "info threads" command. */
1379 do_cleanups (old_chain
);
1381 if (pid
== -1 && requested_threads
== NULL
)
1383 if (uiout
->is_mi_like_p ()
1384 && !ptid_equal (inferior_ptid
, null_ptid
))
1386 int num
= ptid_to_global_thread_id (inferior_ptid
);
1388 gdb_assert (num
!= 0);
1389 uiout
->field_int ("current-thread-id", num
);
1392 if (!ptid_equal (inferior_ptid
, null_ptid
) && is_exited (inferior_ptid
))
1393 uiout
->message ("\n\
1394 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1395 print_thread_id (inferior_thread ()));
1396 else if (thread_list
!= NULL
1397 && ptid_equal (inferior_ptid
, null_ptid
))
1398 uiout
->message ("\n\
1399 No selected thread. See `help thread'.\n");
1403 /* See gdbthread.h. */
1406 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1408 print_thread_info_1 (uiout
, requested_threads
, 1, pid
, 0);
1411 /* Implementation of the "info threads" command.
1413 Note: this has the drawback that it _really_ switches
1414 threads, which frees the frame cache. A no-side
1415 effects info-threads command would be nicer. */
1418 info_threads_command (char *arg
, int from_tty
)
1420 int show_global_ids
= 0;
1423 && check_for_argument (&arg
, "-gid", sizeof ("-gid") - 1))
1425 arg
= skip_spaces (arg
);
1426 show_global_ids
= 1;
1429 print_thread_info_1 (current_uiout
, arg
, 0, -1, show_global_ids
);
1432 /* See gdbthread.h. */
1435 switch_to_thread_no_regs (struct thread_info
*thread
)
1437 struct inferior
*inf
;
1439 inf
= find_inferior_ptid (thread
->ptid
);
1440 gdb_assert (inf
!= NULL
);
1441 set_current_program_space (inf
->pspace
);
1442 set_current_inferior (inf
);
1444 inferior_ptid
= thread
->ptid
;
1445 stop_pc
= ~(CORE_ADDR
) 0;
1448 /* Switch from one thread to another. */
1451 switch_to_thread (ptid_t ptid
)
1453 /* Switch the program space as well, if we can infer it from the now
1454 current thread. Otherwise, it's up to the caller to select the
1456 if (!ptid_equal (ptid
, null_ptid
))
1458 struct inferior
*inf
;
1460 inf
= find_inferior_ptid (ptid
);
1461 gdb_assert (inf
!= NULL
);
1462 set_current_program_space (inf
->pspace
);
1463 set_current_inferior (inf
);
1466 if (ptid_equal (ptid
, inferior_ptid
))
1469 inferior_ptid
= ptid
;
1470 reinit_frame_cache ();
1472 /* We don't check for is_stopped, because we're called at times
1473 while in the TARGET_RUNNING state, e.g., while handling an
1475 if (!ptid_equal (inferior_ptid
, null_ptid
)
1476 && !is_exited (ptid
)
1477 && !is_executing (ptid
))
1478 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1480 stop_pc
= ~(CORE_ADDR
) 0;
1484 restore_current_thread (ptid_t ptid
)
1486 switch_to_thread (ptid
);
1490 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1492 struct frame_info
*frame
= NULL
;
1495 /* This means there was no selected frame. */
1496 if (frame_level
== -1)
1498 select_frame (NULL
);
1502 gdb_assert (frame_level
>= 0);
1504 /* Restore by level first, check if the frame id is the same as
1505 expected. If that fails, try restoring by frame id. If that
1506 fails, nothing to do, just warn the user. */
1508 count
= frame_level
;
1509 frame
= find_relative_frame (get_current_frame (), &count
);
1512 /* The frame ids must match - either both valid or both outer_frame_id.
1513 The latter case is not failsafe, but since it's highly unlikely
1514 the search by level finds the wrong frame, it's 99.9(9)% of
1515 the time (for all practical purposes) safe. */
1516 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1518 /* Cool, all is fine. */
1519 select_frame (frame
);
1523 frame
= frame_find_by_id (a_frame_id
);
1526 /* Cool, refound it. */
1527 select_frame (frame
);
1531 /* Nothing else to do, the frame layout really changed. Select the
1532 innermost stack frame. */
1533 select_frame (get_current_frame ());
1535 /* Warn the user. */
1536 if (frame_level
> 0 && !current_uiout
->is_mi_like_p ())
1538 warning (_("Couldn't restore frame #%d in "
1539 "current thread. Bottom (innermost) frame selected:"),
1541 /* For MI, we should probably have a notification about
1542 current frame change. But this error is not very
1543 likely, so don't bother for now. */
1544 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1548 /* Data used by the cleanup installed by
1549 'make_cleanup_restore_current_thread'. */
1551 struct current_thread_cleanup
1553 /* Next in list of currently installed 'struct
1554 current_thread_cleanup' cleanups. See
1555 'current_thread_cleanup_chain' below. */
1556 struct current_thread_cleanup
*next
;
1558 ptid_t inferior_ptid
;
1559 struct frame_id selected_frame_id
;
1560 int selected_frame_level
;
1566 /* A chain of currently installed 'struct current_thread_cleanup'
1567 cleanups. Restoring the previously selected thread looks up the
1568 old thread in the thread list by ptid. If the thread changes ptid,
1569 we need to update the cleanup's thread structure so the look up
1571 static struct current_thread_cleanup
*current_thread_cleanup_chain
;
1573 /* A thread_ptid_changed observer. Update all currently installed
1574 current_thread_cleanup cleanups that want to switch back to
1575 OLD_PTID to switch back to NEW_PTID instead. */
1578 restore_current_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
1580 struct current_thread_cleanup
*it
;
1582 for (it
= current_thread_cleanup_chain
; it
!= NULL
; it
= it
->next
)
1584 if (ptid_equal (it
->inferior_ptid
, old_ptid
))
1585 it
->inferior_ptid
= new_ptid
;
1590 do_restore_current_thread_cleanup (void *arg
)
1592 struct thread_info
*tp
;
1593 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1595 tp
= find_thread_ptid (old
->inferior_ptid
);
1597 /* If the previously selected thread belonged to a process that has
1598 in the mean time been deleted (due to normal exit, detach, etc.),
1599 then don't revert back to it, but instead simply drop back to no
1602 && find_inferior_ptid (tp
->ptid
) != NULL
)
1603 restore_current_thread (old
->inferior_ptid
);
1606 restore_current_thread (null_ptid
);
1607 set_current_inferior (find_inferior_id (old
->inf_id
));
1610 /* The running state of the originally selected thread may have
1611 changed, so we have to recheck it here. */
1612 if (!ptid_equal (inferior_ptid
, null_ptid
)
1614 && is_stopped (inferior_ptid
)
1615 && target_has_registers
1617 && target_has_memory
)
1618 restore_selected_frame (old
->selected_frame_id
,
1619 old
->selected_frame_level
);
1623 restore_current_thread_cleanup_dtor (void *arg
)
1625 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1626 struct thread_info
*tp
;
1627 struct inferior
*inf
;
1629 current_thread_cleanup_chain
= current_thread_cleanup_chain
->next
;
1631 tp
= find_thread_ptid (old
->inferior_ptid
);
1634 inf
= find_inferior_id (old
->inf_id
);
1636 inf
->removable
= old
->was_removable
;
1640 /* Set the thread reference count. */
1643 set_thread_refcount (void *data
)
1646 struct thread_array_cleanup
*ta_cleanup
1647 = (struct thread_array_cleanup
*) data
;
1649 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1650 ta_cleanup
->tp_array
[k
]->refcount
--;
1654 make_cleanup_restore_current_thread (void)
1656 struct current_thread_cleanup
*old
= XNEW (struct current_thread_cleanup
);
1658 old
->inferior_ptid
= inferior_ptid
;
1659 old
->inf_id
= current_inferior ()->num
;
1660 old
->was_removable
= current_inferior ()->removable
;
1662 old
->next
= current_thread_cleanup_chain
;
1663 current_thread_cleanup_chain
= old
;
1665 if (!ptid_equal (inferior_ptid
, null_ptid
))
1667 struct frame_info
*frame
;
1669 old
->was_stopped
= is_stopped (inferior_ptid
);
1670 if (old
->was_stopped
1671 && target_has_registers
1673 && target_has_memory
)
1675 /* When processing internal events, there might not be a
1676 selected frame. If we naively call get_selected_frame
1677 here, then we can end up reading debuginfo for the
1678 current frame, but we don't generally need the debuginfo
1680 frame
= get_selected_frame_if_set ();
1685 old
->selected_frame_id
= get_frame_id (frame
);
1686 old
->selected_frame_level
= frame_relative_level (frame
);
1688 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1694 current_inferior ()->removable
= 0;
1696 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1697 restore_current_thread_cleanup_dtor
);
1700 /* See gdbthread.h. */
1703 show_thread_that_caused_stop (void)
1705 return highest_thread_num
> 1;
1708 /* See gdbthread.h. */
1711 show_inferior_qualified_tids (void)
1713 return (inferior_list
->next
!= NULL
|| inferior_list
->num
!= 1);
1716 /* See gdbthread.h. */
1719 print_thread_id (struct thread_info
*thr
)
1721 char *s
= get_print_cell ();
1723 if (show_inferior_qualified_tids ())
1724 xsnprintf (s
, PRINT_CELL_SIZE
, "%d.%d", thr
->inf
->num
, thr
->per_inf_num
);
1726 xsnprintf (s
, PRINT_CELL_SIZE
, "%d", thr
->per_inf_num
);
1730 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1731 descending order. */
1733 static int tp_array_compar_ascending
;
1735 /* Sort an array for struct thread_info pointers by thread ID (first
1736 by inferior number, and then by per-inferior thread number). The
1737 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1740 tp_array_compar (const void *ap_voidp
, const void *bp_voidp
)
1742 const struct thread_info
*a
= *(const struct thread_info
* const *) ap_voidp
;
1743 const struct thread_info
*b
= *(const struct thread_info
* const *) bp_voidp
;
1745 if (a
->inf
->num
!= b
->inf
->num
)
1747 return (((a
->inf
->num
> b
->inf
->num
) - (a
->inf
->num
< b
->inf
->num
))
1748 * (tp_array_compar_ascending
? +1 : -1));
1751 return (((a
->per_inf_num
> b
->per_inf_num
)
1752 - (a
->per_inf_num
< b
->per_inf_num
))
1753 * (tp_array_compar_ascending
? +1 : -1));
1756 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1757 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1758 of two numbers seperated by a hyphen. Examples:
1760 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1761 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1762 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1765 thread_apply_all_command (char *cmd
, int from_tty
)
1767 struct cleanup
*old_chain
;
1770 struct thread_array_cleanup ta_cleanup
;
1772 tp_array_compar_ascending
= 0;
1774 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1776 cmd
= skip_spaces (cmd
);
1777 tp_array_compar_ascending
= 1;
1780 if (cmd
== NULL
|| *cmd
== '\000')
1781 error (_("Please specify a command following the thread ID list"));
1783 update_thread_list ();
1785 old_chain
= make_cleanup_restore_current_thread ();
1787 /* Save a copy of the command in case it is clobbered by
1789 saved_cmd
= xstrdup (cmd
);
1790 make_cleanup (xfree
, saved_cmd
);
1792 /* Note this includes exited threads. */
1793 tc
= thread_count ();
1796 struct thread_info
**tp_array
;
1797 struct thread_info
*tp
;
1800 /* Save a copy of the thread_list in case we execute detach
1802 tp_array
= XNEWVEC (struct thread_info
*, tc
);
1803 make_cleanup (xfree
, tp_array
);
1805 ALL_NON_EXITED_THREADS (tp
)
1811 /* Because we skipped exited threads, we may end up with fewer
1812 threads in the array than the total count of threads. */
1813 gdb_assert (i
<= tc
);
1816 qsort (tp_array
, i
, sizeof (*tp_array
), tp_array_compar
);
1818 ta_cleanup
.tp_array
= tp_array
;
1819 ta_cleanup
.count
= i
;
1820 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1822 for (k
= 0; k
!= i
; k
++)
1823 if (thread_alive (tp_array
[k
]))
1825 switch_to_thread (tp_array
[k
]->ptid
);
1826 printf_filtered (_("\nThread %s (%s):\n"),
1827 print_thread_id (tp_array
[k
]),
1828 target_pid_to_str (inferior_ptid
));
1829 execute_command (cmd
, from_tty
);
1831 /* Restore exact command used previously. */
1832 strcpy (cmd
, saved_cmd
);
1836 do_cleanups (old_chain
);
1839 /* Implementation of the "thread apply" command. */
1842 thread_apply_command (char *tidlist
, int from_tty
)
1845 struct cleanup
*old_chain
;
1847 tid_range_parser parser
;
1849 if (tidlist
== NULL
|| *tidlist
== '\000')
1850 error (_("Please specify a thread ID list"));
1852 parser
.init (tidlist
, current_inferior ()->num
);
1853 while (!parser
.finished ())
1855 int inf_num
, thr_start
, thr_end
;
1857 if (!parser
.get_tid_range (&inf_num
, &thr_start
, &thr_end
))
1859 cmd
= (char *) parser
.cur_tok ();
1865 error (_("Please specify a command following the thread ID list"));
1867 if (tidlist
== cmd
|| !isalpha (cmd
[0]))
1868 invalid_thread_id_error (cmd
);
1870 /* Save a copy of the command in case it is clobbered by
1872 saved_cmd
= xstrdup (cmd
);
1873 old_chain
= make_cleanup (xfree
, saved_cmd
);
1875 make_cleanup_restore_current_thread ();
1877 parser
.init (tidlist
, current_inferior ()->num
);
1878 while (!parser
.finished () && parser
.cur_tok () < cmd
)
1880 struct thread_info
*tp
= NULL
;
1881 struct inferior
*inf
;
1882 int inf_num
, thr_num
;
1884 parser
.get_tid (&inf_num
, &thr_num
);
1885 inf
= find_inferior_id (inf_num
);
1887 tp
= find_thread_id (inf
, thr_num
);
1889 if (parser
.in_star_range ())
1893 warning (_("Unknown inferior %d"), inf_num
);
1894 parser
.skip_range ();
1898 /* No use looking for threads past the highest thread number
1899 the inferior ever had. */
1900 if (thr_num
>= inf
->highest_thread_num
)
1901 parser
.skip_range ();
1903 /* Be quiet about unknown threads numbers. */
1910 if (show_inferior_qualified_tids () || parser
.tid_is_qualified ())
1911 warning (_("Unknown thread %d.%d"), inf_num
, thr_num
);
1913 warning (_("Unknown thread %d"), thr_num
);
1917 if (!thread_alive (tp
))
1919 warning (_("Thread %s has terminated."), print_thread_id (tp
));
1923 switch_to_thread (tp
->ptid
);
1925 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp
),
1926 target_pid_to_str (inferior_ptid
));
1927 execute_command (cmd
, from_tty
);
1929 /* Restore exact command used previously. */
1930 strcpy (cmd
, saved_cmd
);
1933 do_cleanups (old_chain
);
1936 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1937 if prefix of arg is `apply'. */
1940 thread_command (char *tidstr
, int from_tty
)
1944 if (ptid_equal (inferior_ptid
, null_ptid
))
1945 error (_("No thread selected"));
1947 if (target_has_stack
)
1949 struct thread_info
*tp
= inferior_thread ();
1951 if (is_exited (inferior_ptid
))
1952 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1953 print_thread_id (tp
),
1954 target_pid_to_str (inferior_ptid
));
1956 printf_filtered (_("[Current thread is %s (%s)]\n"),
1957 print_thread_id (tp
),
1958 target_pid_to_str (inferior_ptid
));
1961 error (_("No stack."));
1965 ptid_t previous_ptid
= inferior_ptid
;
1968 result
= gdb_thread_select (current_uiout
, tidstr
, NULL
);
1970 /* If thread switch did not succeed don't notify or print. */
1971 if (result
== GDB_RC_FAIL
)
1974 /* Print if the thread has not changed, otherwise an event will be sent. */
1975 if (ptid_equal (inferior_ptid
, previous_ptid
))
1977 print_selected_thread_frame (current_uiout
,
1978 USER_SELECTED_THREAD
1979 | USER_SELECTED_FRAME
);
1983 observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
1984 | USER_SELECTED_FRAME
);
1989 /* Implementation of `thread name'. */
1992 thread_name_command (char *arg
, int from_tty
)
1994 struct thread_info
*info
;
1996 if (ptid_equal (inferior_ptid
, null_ptid
))
1997 error (_("No thread selected"));
1999 arg
= skip_spaces (arg
);
2001 info
= inferior_thread ();
2003 info
->name
= arg
? xstrdup (arg
) : NULL
;
2006 /* Find thread ids with a name, target pid, or extra info matching ARG. */
2009 thread_find_command (char *arg
, int from_tty
)
2011 struct thread_info
*tp
;
2013 unsigned long match
= 0;
2015 if (arg
== NULL
|| *arg
== '\0')
2016 error (_("Command requires an argument."));
2018 tmp
= re_comp (arg
);
2020 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
2022 update_thread_list ();
2023 for (tp
= thread_list
; tp
; tp
= tp
->next
)
2025 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
2027 printf_filtered (_("Thread %s has name '%s'\n"),
2028 print_thread_id (tp
), tp
->name
);
2032 tmp
= target_thread_name (tp
);
2033 if (tmp
!= NULL
&& re_exec (tmp
))
2035 printf_filtered (_("Thread %s has target name '%s'\n"),
2036 print_thread_id (tp
), tmp
);
2040 tmp
= target_pid_to_str (tp
->ptid
);
2041 if (tmp
!= NULL
&& re_exec (tmp
))
2043 printf_filtered (_("Thread %s has target id '%s'\n"),
2044 print_thread_id (tp
), tmp
);
2048 tmp
= target_extra_thread_info (tp
);
2049 if (tmp
!= NULL
&& re_exec (tmp
))
2051 printf_filtered (_("Thread %s has extra info '%s'\n"),
2052 print_thread_id (tp
), tmp
);
2057 printf_filtered (_("No threads match '%s'\n"), arg
);
2060 /* Print notices when new threads are attached and detached. */
2061 int print_thread_events
= 1;
2063 show_print_thread_events (struct ui_file
*file
, int from_tty
,
2064 struct cmd_list_element
*c
, const char *value
)
2066 fprintf_filtered (file
,
2067 _("Printing of thread events is %s.\n"),
2072 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr_v
)
2074 const char *tidstr
= (const char *) tidstr_v
;
2075 struct thread_info
*tp
;
2077 if (uiout
->is_mi_like_p ())
2079 int num
= value_as_long (parse_and_eval (tidstr
));
2081 tp
= find_thread_global_id (num
);
2083 error (_("Thread ID %d not known."), num
);
2087 tp
= parse_thread_id (tidstr
, NULL
);
2088 gdb_assert (tp
!= NULL
);
2091 if (!thread_alive (tp
))
2092 error (_("Thread ID %s has terminated."), tidstr
);
2094 switch_to_thread (tp
->ptid
);
2096 annotate_thread_changed ();
2098 /* Since the current thread may have changed, see if there is any
2099 exited thread we can now delete. */
2105 /* Print thread and frame switch command response. */
2108 print_selected_thread_frame (struct ui_out
*uiout
,
2109 user_selected_what selection
)
2111 struct thread_info
*tp
= inferior_thread ();
2112 struct inferior
*inf
= current_inferior ();
2114 if (selection
& USER_SELECTED_THREAD
)
2116 if (uiout
->is_mi_like_p ())
2118 uiout
->field_int ("new-thread-id",
2119 inferior_thread ()->global_num
);
2123 uiout
->text ("[Switching to thread ");
2124 uiout
->field_string ("new-thread-id", print_thread_id (tp
));
2126 uiout
->text (target_pid_to_str (inferior_ptid
));
2131 if (tp
->state
== THREAD_RUNNING
)
2133 if (selection
& USER_SELECTED_THREAD
)
2134 uiout
->text ("(running)\n");
2136 else if (selection
& USER_SELECTED_FRAME
)
2138 if (selection
& USER_SELECTED_THREAD
)
2141 if (has_stack_frames ())
2142 print_stack_frame_to_uiout (uiout
, get_selected_frame (NULL
),
2148 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
2150 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
2151 error_message
, RETURN_MASK_ALL
) < 0)
2156 /* Update the 'threads_executing' global based on the threads we know
2160 update_threads_executing (void)
2162 struct thread_info
*tp
;
2164 threads_executing
= 0;
2165 ALL_NON_EXITED_THREADS (tp
)
2169 threads_executing
= 1;
2176 update_thread_list (void)
2178 target_update_thread_list ();
2179 update_threads_executing ();
2182 /* Return a new value for the selected thread's id. Return a value of
2183 0 if no thread is selected. If GLOBAL is true, return the thread's
2184 global number. Otherwise return the per-inferior number. */
2186 static struct value
*
2187 thread_num_make_value_helper (struct gdbarch
*gdbarch
, int global
)
2189 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
2195 int_val
= tp
->global_num
;
2197 int_val
= tp
->per_inf_num
;
2199 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, int_val
);
2202 /* Return a new value for the selected thread's per-inferior thread
2203 number. Return a value of 0 if no thread is selected, or no
2206 static struct value
*
2207 thread_id_per_inf_num_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2210 return thread_num_make_value_helper (gdbarch
, 0);
2213 /* Return a new value for the selected thread's global id. Return a
2214 value of 0 if no thread is selected, or no threads exist. */
2216 static struct value
*
2217 global_thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
2220 return thread_num_make_value_helper (gdbarch
, 1);
2223 /* Commands with a prefix of `thread'. */
2224 struct cmd_list_element
*thread_cmd_list
= NULL
;
2226 /* Implementation of `thread' variable. */
2228 static const struct internalvar_funcs thread_funcs
=
2230 thread_id_per_inf_num_make_value
,
2235 /* Implementation of `gthread' variable. */
2237 static const struct internalvar_funcs gthread_funcs
=
2239 global_thread_id_make_value
,
2245 _initialize_thread (void)
2247 static struct cmd_list_element
*thread_apply_list
= NULL
;
2249 add_info ("threads", info_threads_command
,
2250 _("Display currently known threads.\n\
2251 Usage: info threads [-gid] [ID]...\n\
2252 -gid: Show global thread IDs.\n\
2253 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2254 Otherwise, all threads are displayed."));
2256 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
2257 Use this command to switch between threads.\n\
2258 The new thread ID must be currently known."),
2259 &thread_cmd_list
, "thread ", 1, &cmdlist
);
2261 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
2262 _("Apply a command to a list of threads."),
2263 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
2265 add_cmd ("all", class_run
, thread_apply_all_command
,
2267 Apply a command to all threads.\n\
2269 Usage: thread apply all [-ascending] <command>\n\
2270 -ascending: Call <command> for all threads in ascending order.\n\
2271 The default is descending order.\
2273 &thread_apply_list
);
2275 add_cmd ("name", class_run
, thread_name_command
,
2276 _("Set the current thread's name.\n\
2277 Usage: thread name [NAME]\n\
2278 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
2280 add_cmd ("find", class_run
, thread_find_command
, _("\
2281 Find threads that match a regular expression.\n\
2282 Usage: thread find REGEXP\n\
2283 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2286 add_com_alias ("t", "thread", class_run
, 1);
2288 add_setshow_boolean_cmd ("thread-events", no_class
,
2289 &print_thread_events
, _("\
2290 Set printing of thread events (such as thread start and exit)."), _("\
2291 Show printing of thread events (such as thread start and exit)."), NULL
,
2293 show_print_thread_events
,
2294 &setprintlist
, &showprintlist
);
2296 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2297 create_internalvar_type_lazy ("_gthread", >hread_funcs
, NULL
);
2299 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed
);