1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2016 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"
47 /* Definition of struct thread_info exported to gdbthread.h. */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 /* True if any thread is, or may be executing. We need to track this
59 separately because until we fully sync the thread list, we won't
60 know whether the target is fully stopped, even if we see stop
61 events for all known threads, because any of those threads may have
62 spawned new threads we haven't heard of yet. */
63 static int threads_executing
;
65 static void thread_apply_all_command (char *, int);
66 static int thread_alive (struct thread_info
*);
67 static void info_threads_command (char *, int);
68 static void thread_apply_command (char *, int);
69 static void restore_current_thread (ptid_t
);
71 /* Data to cleanup thread array. */
73 struct thread_array_cleanup
75 /* Array of thread pointers used to set
77 struct thread_info
**tp_array
;
79 /* Thread count in the array. */
85 inferior_thread (void)
87 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
92 /* Delete the breakpoint pointed at by BP_P, if there's one. */
95 delete_thread_breakpoint (struct breakpoint
**bp_p
)
99 delete_breakpoint (*bp_p
);
105 delete_step_resume_breakpoint (struct thread_info
*tp
)
108 delete_thread_breakpoint (&tp
->control
.step_resume_breakpoint
);
112 delete_exception_resume_breakpoint (struct thread_info
*tp
)
115 delete_thread_breakpoint (&tp
->control
.exception_resume_breakpoint
);
118 /* See gdbthread.h. */
121 delete_single_step_breakpoints (struct thread_info
*tp
)
124 delete_thread_breakpoint (&tp
->control
.single_step_breakpoints
);
127 /* Delete the breakpoint pointed at by BP_P at the next stop, if
131 delete_at_next_stop (struct breakpoint
**bp
)
135 (*bp
)->disposition
= disp_del_at_next_stop
;
140 /* See gdbthread.h. */
143 thread_has_single_step_breakpoints_set (struct thread_info
*tp
)
145 return tp
->control
.single_step_breakpoints
!= NULL
;
148 /* See gdbthread.h. */
151 thread_has_single_step_breakpoint_here (struct thread_info
*tp
,
152 struct address_space
*aspace
,
155 struct breakpoint
*ss_bps
= tp
->control
.single_step_breakpoints
;
157 return (ss_bps
!= NULL
158 && breakpoint_has_location_inserted_here (ss_bps
, aspace
, addr
));
161 /* See gdbthread.h. */
164 thread_cancel_execution_command (struct thread_info
*thr
)
166 if (thr
->thread_fsm
!= NULL
)
168 thread_fsm_clean_up (thr
->thread_fsm
);
169 thread_fsm_delete (thr
->thread_fsm
);
170 thr
->thread_fsm
= NULL
;
175 clear_thread_inferior_resources (struct thread_info
*tp
)
177 /* NOTE: this will take care of any left-over step_resume breakpoints,
178 but not any user-specified thread-specific breakpoints. We can not
179 delete the breakpoint straight-off, because the inferior might not
180 be stopped at the moment. */
181 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
182 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
183 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
185 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
187 bpstat_clear (&tp
->control
.stop_bpstat
);
189 btrace_teardown (tp
);
191 thread_cancel_execution_command (tp
);
195 free_thread (struct thread_info
*tp
)
199 if (tp
->private_dtor
)
200 tp
->private_dtor (tp
->priv
);
210 init_thread_list (void)
212 struct thread_info
*tp
, *tpnext
;
214 highest_thread_num
= 0;
219 for (tp
= thread_list
; tp
; tp
= tpnext
)
226 threads_executing
= 0;
229 /* Allocate a new thread with target id PTID and add it to the thread
232 static struct thread_info
*
233 new_thread (ptid_t ptid
)
235 struct thread_info
*tp
= XCNEW (struct thread_info
);
238 tp
->num
= ++highest_thread_num
;
239 tp
->next
= thread_list
;
242 /* Nothing to follow yet. */
243 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
244 tp
->state
= THREAD_STOPPED
;
245 tp
->suspend
.waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
251 add_thread_silent (ptid_t ptid
)
253 struct thread_info
*tp
;
255 tp
= find_thread_ptid (ptid
);
257 /* Found an old thread with the same id. It has to be dead,
258 otherwise we wouldn't be adding a new thread with the same id.
259 The OS is reusing this id --- delete it, and recreate a new
262 /* In addition to deleting the thread, if this is the current
263 thread, then we need to take care that delete_thread doesn't
264 really delete the thread if it is inferior_ptid. Create a
265 new template thread in the list with an invalid ptid, switch
266 to it, delete the original thread, reset the new thread's
267 ptid, and switch to it. */
269 if (ptid_equal (inferior_ptid
, ptid
))
271 tp
= new_thread (null_ptid
);
273 /* Make switch_to_thread not read from the thread. */
274 tp
->state
= THREAD_EXITED
;
275 switch_to_thread (null_ptid
);
277 /* Now we can delete it. */
278 delete_thread (ptid
);
280 /* Now reset its ptid, and reswitch inferior_ptid to it. */
282 tp
->state
= THREAD_STOPPED
;
283 switch_to_thread (ptid
);
285 observer_notify_new_thread (tp
);
291 /* Just go ahead and delete it. */
292 delete_thread (ptid
);
295 tp
= new_thread (ptid
);
296 observer_notify_new_thread (tp
);
302 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*priv
)
304 struct thread_info
*result
= add_thread_silent (ptid
);
308 if (print_thread_events
)
309 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
311 annotate_new_thread ();
316 add_thread (ptid_t ptid
)
318 return add_thread_with_info (ptid
, NULL
);
321 /* Add TP to the end of the step-over chain LIST_P. */
324 step_over_chain_enqueue (struct thread_info
**list_p
, struct thread_info
*tp
)
326 gdb_assert (tp
->step_over_next
== NULL
);
327 gdb_assert (tp
->step_over_prev
== NULL
);
332 tp
->step_over_prev
= tp
->step_over_next
= tp
;
336 struct thread_info
*head
= *list_p
;
337 struct thread_info
*tail
= head
->step_over_prev
;
339 tp
->step_over_prev
= tail
;
340 tp
->step_over_next
= head
;
341 head
->step_over_prev
= tp
;
342 tail
->step_over_next
= tp
;
346 /* Remove TP from step-over chain LIST_P. */
349 step_over_chain_remove (struct thread_info
**list_p
, struct thread_info
*tp
)
351 gdb_assert (tp
->step_over_next
!= NULL
);
352 gdb_assert (tp
->step_over_prev
!= NULL
);
356 if (tp
== tp
->step_over_next
)
359 *list_p
= tp
->step_over_next
;
362 tp
->step_over_prev
->step_over_next
= tp
->step_over_next
;
363 tp
->step_over_next
->step_over_prev
= tp
->step_over_prev
;
364 tp
->step_over_prev
= tp
->step_over_next
= NULL
;
367 /* See gdbthread.h. */
370 thread_step_over_chain_next (struct thread_info
*tp
)
372 struct thread_info
*next
= tp
->step_over_next
;
374 return (next
== step_over_queue_head
? NULL
: next
);
377 /* See gdbthread.h. */
380 thread_is_in_step_over_chain (struct thread_info
*tp
)
382 return (tp
->step_over_next
!= NULL
);
385 /* See gdbthread.h. */
388 thread_step_over_chain_enqueue (struct thread_info
*tp
)
390 step_over_chain_enqueue (&step_over_queue_head
, tp
);
393 /* See gdbthread.h. */
396 thread_step_over_chain_remove (struct thread_info
*tp
)
398 step_over_chain_remove (&step_over_queue_head
, tp
);
401 /* Delete thread PTID. If SILENT, don't notify the observer of this
404 delete_thread_1 (ptid_t ptid
, int silent
)
406 struct thread_info
*tp
, *tpprev
;
410 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
411 if (ptid_equal (tp
->ptid
, ptid
))
417 /* Dead threads don't need to step-over. Remove from queue. */
418 if (tp
->step_over_next
!= NULL
)
419 thread_step_over_chain_remove (tp
);
421 /* If this is the current thread, or there's code out there that
422 relies on it existing (refcount > 0) we can't delete yet. Mark
423 it as exited, and notify it. */
425 || ptid_equal (tp
->ptid
, inferior_ptid
))
427 if (tp
->state
!= THREAD_EXITED
)
429 observer_notify_thread_exit (tp
, silent
);
431 /* Tag it as exited. */
432 tp
->state
= THREAD_EXITED
;
434 /* Clear breakpoints, etc. associated with this thread. */
435 clear_thread_inferior_resources (tp
);
438 /* Will be really deleted some other time. */
442 /* Notify thread exit, but only if we haven't already. */
443 if (tp
->state
!= THREAD_EXITED
)
444 observer_notify_thread_exit (tp
, silent
);
446 /* Tag it as exited. */
447 tp
->state
= THREAD_EXITED
;
448 clear_thread_inferior_resources (tp
);
451 tpprev
->next
= tp
->next
;
453 thread_list
= tp
->next
;
458 /* Delete thread PTID and notify of thread exit. If this is
459 inferior_ptid, don't actually delete it, but tag it as exited and
460 do the notification. If PTID is the user selected thread, clear
463 delete_thread (ptid_t ptid
)
465 delete_thread_1 (ptid
, 0 /* not silent */);
469 delete_thread_silent (ptid_t ptid
)
471 delete_thread_1 (ptid
, 1 /* silent */);
475 find_thread_id (int num
)
477 struct thread_info
*tp
;
479 for (tp
= thread_list
; tp
; tp
= tp
->next
)
486 /* Find a thread_info by matching PTID. */
488 find_thread_ptid (ptid_t ptid
)
490 struct thread_info
*tp
;
492 for (tp
= thread_list
; tp
; tp
= tp
->next
)
493 if (ptid_equal (tp
->ptid
, ptid
))
500 * Thread iterator function.
502 * Calls a callback function once for each thread, so long as
503 * the callback function returns false. If the callback function
504 * returns true, the iteration will end and the current thread
505 * will be returned. This can be useful for implementing a
506 * search for a thread with arbitrary attributes, or for applying
507 * some operation to every thread.
509 * FIXME: some of the existing functionality, such as
510 * "Thread apply all", might be rewritten using this functionality.
514 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
517 struct thread_info
*tp
, *next
;
519 for (tp
= thread_list
; tp
; tp
= next
)
522 if ((*callback
) (tp
, data
))
533 struct thread_info
*tp
;
535 for (tp
= thread_list
; tp
; tp
= tp
->next
)
542 valid_thread_id (int num
)
544 struct thread_info
*tp
;
546 for (tp
= thread_list
; tp
; tp
= tp
->next
)
554 pid_to_thread_id (ptid_t ptid
)
556 struct thread_info
*tp
;
558 for (tp
= thread_list
; tp
; tp
= tp
->next
)
559 if (ptid_equal (tp
->ptid
, ptid
))
566 thread_id_to_pid (int num
)
568 struct thread_info
*thread
= find_thread_id (num
);
573 return pid_to_ptid (-1);
577 in_thread_list (ptid_t ptid
)
579 struct thread_info
*tp
;
581 for (tp
= thread_list
; tp
; tp
= tp
->next
)
582 if (ptid_equal (tp
->ptid
, ptid
))
585 return 0; /* Never heard of 'im. */
588 /* Finds the first thread of the inferior given by PID. If PID is -1,
589 return the first thread in the list. */
592 first_thread_of_process (int pid
)
594 struct thread_info
*tp
, *ret
= NULL
;
596 for (tp
= thread_list
; tp
; tp
= tp
->next
)
597 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
598 if (ret
== NULL
|| tp
->num
< ret
->num
)
605 any_thread_of_process (int pid
)
607 struct thread_info
*tp
;
609 gdb_assert (pid
!= 0);
611 /* Prefer the current thread. */
612 if (ptid_get_pid (inferior_ptid
) == pid
)
613 return inferior_thread ();
615 ALL_NON_EXITED_THREADS (tp
)
616 if (ptid_get_pid (tp
->ptid
) == pid
)
623 any_live_thread_of_process (int pid
)
625 struct thread_info
*curr_tp
= NULL
;
626 struct thread_info
*tp
;
627 struct thread_info
*tp_executing
= NULL
;
629 gdb_assert (pid
!= 0);
631 /* Prefer the current thread if it's not executing. */
632 if (ptid_get_pid (inferior_ptid
) == pid
)
634 /* If the current thread is dead, forget it. If it's not
635 executing, use it. Otherwise, still choose it (below), but
636 only if no other non-executing thread is found. */
637 curr_tp
= inferior_thread ();
638 if (curr_tp
->state
== THREAD_EXITED
)
640 else if (!curr_tp
->executing
)
644 ALL_NON_EXITED_THREADS (tp
)
645 if (ptid_get_pid (tp
->ptid
) == pid
)
653 /* If both the current thread and all live threads are executing,
654 prefer the current thread. */
658 /* Otherwise, just return an executing thread, if any. */
662 /* Print a list of thread ids currently known, and the total number of
663 threads. To be used from within catch_errors. */
665 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
667 struct thread_info
*tp
;
669 struct cleanup
*cleanup_chain
;
670 int current_thread
= -1;
672 update_thread_list ();
674 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
676 for (tp
= thread_list
; tp
; tp
= tp
->next
)
678 if (tp
->state
== THREAD_EXITED
)
681 if (ptid_equal (tp
->ptid
, inferior_ptid
))
682 current_thread
= tp
->num
;
685 ui_out_field_int (uiout
, "thread-id", tp
->num
);
688 do_cleanups (cleanup_chain
);
690 if (current_thread
!= -1)
691 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
692 ui_out_field_int (uiout
, "number-of-threads", num
);
696 /* Official gdblib interface function to get a list of thread ids and
699 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
701 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
702 error_message
, RETURN_MASK_ALL
) < 0)
707 /* Return true if TP is an active thread. */
709 thread_alive (struct thread_info
*tp
)
711 if (tp
->state
== THREAD_EXITED
)
713 if (!target_thread_alive (tp
->ptid
))
718 /* See gdbthreads.h. */
723 struct thread_info
*tp
, *tmp
;
725 ALL_THREADS_SAFE (tp
, tmp
)
727 if (!thread_alive (tp
))
728 delete_thread (tp
->ptid
);
732 /* See gdbthreads.h. */
735 delete_exited_threads (void)
737 struct thread_info
*tp
, *tmp
;
739 ALL_THREADS_SAFE (tp
, tmp
)
741 if (tp
->state
== THREAD_EXITED
)
742 delete_thread (tp
->ptid
);
746 /* Disable storing stack temporaries for the thread whose id is
750 disable_thread_stack_temporaries (void *data
)
752 ptid_t
*pd
= (ptid_t
*) data
;
753 struct thread_info
*tp
= find_thread_ptid (*pd
);
757 tp
->stack_temporaries_enabled
= 0;
758 VEC_free (value_ptr
, tp
->stack_temporaries
);
764 /* Enable storing stack temporaries for thread with id PTID and return a
765 cleanup which can disable and clear the stack temporaries. */
768 enable_thread_stack_temporaries (ptid_t ptid
)
770 struct thread_info
*tp
= find_thread_ptid (ptid
);
774 gdb_assert (tp
!= NULL
);
776 tp
->stack_temporaries_enabled
= 1;
777 tp
->stack_temporaries
= NULL
;
778 data
= XNEW (ptid_t
);
780 c
= make_cleanup (disable_thread_stack_temporaries
, data
);
785 /* Return non-zero value if stack temporaies are enabled for the thread
789 thread_stack_temporaries_enabled_p (ptid_t ptid
)
791 struct thread_info
*tp
= find_thread_ptid (ptid
);
796 return tp
->stack_temporaries_enabled
;
799 /* Push V on to the stack temporaries of the thread with id PTID. */
802 push_thread_stack_temporary (ptid_t ptid
, struct value
*v
)
804 struct thread_info
*tp
= find_thread_ptid (ptid
);
806 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
807 VEC_safe_push (value_ptr
, tp
->stack_temporaries
, v
);
810 /* Return 1 if VAL is among the stack temporaries of the thread
811 with id PTID. Return 0 otherwise. */
814 value_in_thread_stack_temporaries (struct value
*val
, ptid_t ptid
)
816 struct thread_info
*tp
= find_thread_ptid (ptid
);
818 gdb_assert (tp
!= NULL
&& tp
->stack_temporaries_enabled
);
819 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
824 for (i
= 0; VEC_iterate (value_ptr
, tp
->stack_temporaries
, i
, v
); i
++)
832 /* Return the last of the stack temporaries for thread with id PTID.
833 Return NULL if there are no stack temporaries for the thread. */
836 get_last_thread_stack_temporary (ptid_t ptid
)
838 struct value
*lastval
= NULL
;
839 struct thread_info
*tp
= find_thread_ptid (ptid
);
841 gdb_assert (tp
!= NULL
);
842 if (!VEC_empty (value_ptr
, tp
->stack_temporaries
))
843 lastval
= VEC_last (value_ptr
, tp
->stack_temporaries
);
849 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
851 struct inferior
*inf
;
852 struct thread_info
*tp
;
854 /* It can happen that what we knew as the target inferior id
855 changes. E.g, target remote may only discover the remote process
856 pid after adding the inferior to GDB's list. */
857 inf
= find_inferior_ptid (old_ptid
);
858 inf
->pid
= ptid_get_pid (new_ptid
);
860 tp
= find_thread_ptid (old_ptid
);
863 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
866 /* See gdbthread.h. */
869 set_resumed (ptid_t ptid
, int resumed
)
871 struct thread_info
*tp
;
872 int all
= ptid_equal (ptid
, minus_one_ptid
);
874 if (all
|| ptid_is_pid (ptid
))
876 for (tp
= thread_list
; tp
; tp
= tp
->next
)
877 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
878 tp
->resumed
= resumed
;
882 tp
= find_thread_ptid (ptid
);
883 gdb_assert (tp
!= NULL
);
884 tp
->resumed
= resumed
;
888 /* Helper for set_running, that marks one thread either running or
892 set_running_thread (struct thread_info
*tp
, int running
)
896 if (running
&& tp
->state
== THREAD_STOPPED
)
898 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
902 /* If the thread is now marked stopped, remove it from
903 the step-over queue, so that we don't try to resume
904 it until the user wants it to. */
905 if (tp
->step_over_next
!= NULL
)
906 thread_step_over_chain_remove (tp
);
913 set_running (ptid_t ptid
, int running
)
915 struct thread_info
*tp
;
916 int all
= ptid_equal (ptid
, minus_one_ptid
);
919 /* We try not to notify the observer if no thread has actually changed
920 the running state -- merely to reduce the number of messages to
921 frontend. Frontend is supposed to handle multiple *running just fine. */
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
))
927 if (tp
->state
== THREAD_EXITED
)
930 if (set_running_thread (tp
, running
))
936 tp
= find_thread_ptid (ptid
);
937 gdb_assert (tp
!= NULL
);
938 gdb_assert (tp
->state
!= THREAD_EXITED
);
939 if (set_running_thread (tp
, running
))
943 observer_notify_target_resumed (ptid
);
947 is_thread_state (ptid_t ptid
, enum thread_state state
)
949 struct thread_info
*tp
;
951 tp
= find_thread_ptid (ptid
);
953 return tp
->state
== state
;
957 is_stopped (ptid_t ptid
)
959 return is_thread_state (ptid
, THREAD_STOPPED
);
963 is_exited (ptid_t ptid
)
965 return is_thread_state (ptid
, THREAD_EXITED
);
969 is_running (ptid_t ptid
)
971 return is_thread_state (ptid
, THREAD_RUNNING
);
975 is_executing (ptid_t ptid
)
977 struct thread_info
*tp
;
979 tp
= find_thread_ptid (ptid
);
981 return tp
->executing
;
985 set_executing (ptid_t ptid
, int executing
)
987 struct thread_info
*tp
;
988 int all
= ptid_equal (ptid
, minus_one_ptid
);
990 if (all
|| ptid_is_pid (ptid
))
992 for (tp
= thread_list
; tp
; tp
= tp
->next
)
993 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
994 tp
->executing
= executing
;
998 tp
= find_thread_ptid (ptid
);
1000 tp
->executing
= executing
;
1003 /* It only takes one running thread to spawn more threads.*/
1005 threads_executing
= 1;
1006 /* Only clear the flag if the caller is telling us everything is
1008 else if (ptid_equal (minus_one_ptid
, ptid
))
1009 threads_executing
= 0;
1012 /* See gdbthread.h. */
1015 threads_are_executing (void)
1017 return threads_executing
;
1021 set_stop_requested (ptid_t ptid
, int stop
)
1023 struct thread_info
*tp
;
1024 int all
= ptid_equal (ptid
, minus_one_ptid
);
1026 if (all
|| ptid_is_pid (ptid
))
1028 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1029 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
1030 tp
->stop_requested
= stop
;
1034 tp
= find_thread_ptid (ptid
);
1036 tp
->stop_requested
= stop
;
1039 /* Call the stop requested observer so other components of GDB can
1040 react to this request. */
1042 observer_notify_thread_stop_requested (ptid
);
1046 finish_thread_state (ptid_t ptid
)
1048 struct thread_info
*tp
;
1050 int any_started
= 0;
1052 all
= ptid_equal (ptid
, minus_one_ptid
);
1054 if (all
|| ptid_is_pid (ptid
))
1056 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1058 if (tp
->state
== THREAD_EXITED
)
1060 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
1062 if (set_running_thread (tp
, tp
->executing
))
1069 tp
= find_thread_ptid (ptid
);
1071 if (tp
->state
!= THREAD_EXITED
)
1073 if (set_running_thread (tp
, tp
->executing
))
1079 observer_notify_target_resumed (ptid
);
1083 finish_thread_state_cleanup (void *arg
)
1085 ptid_t
*ptid_p
= (ptid_t
*) arg
;
1089 finish_thread_state (*ptid_p
);
1093 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
1095 return (pc
>= thread
->control
.step_range_start
1096 && pc
< thread
->control
.step_range_end
);
1099 /* Prints the list of threads and their details on UIOUT.
1100 This is a version of 'info_threads_command' suitable for
1102 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1103 that should be printed. Otherwise, all threads are
1105 If PID is not -1, only print threads from the process PID.
1106 Otherwise, threads from all attached PIDs are printed.
1107 If both REQUESTED_THREAD and PID are not -1, then the thread
1108 is printed if it belongs to the specified process. Otherwise,
1109 an error is raised. */
1111 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
1113 struct thread_info
*tp
;
1114 ptid_t current_ptid
;
1115 struct cleanup
*old_chain
;
1116 const char *extra_info
, *name
, *target_id
;
1117 int current_thread
= -1;
1119 update_thread_list ();
1120 current_ptid
= inferior_ptid
;
1122 /* We'll be switching threads temporarily. */
1123 old_chain
= make_cleanup_restore_current_thread ();
1125 /* For backward compatibility, we make a list for MI. A table is
1126 preferable for the CLI, though, because it shows table
1128 if (ui_out_is_mi_like_p (uiout
))
1129 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
1134 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1136 if (!number_is_in_list (requested_threads
, tp
->num
))
1139 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
1142 if (tp
->state
== THREAD_EXITED
)
1150 if (requested_threads
== NULL
|| *requested_threads
== '\0')
1151 ui_out_message (uiout
, 0, _("No threads.\n"));
1153 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
1155 do_cleanups (old_chain
);
1159 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
1161 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
1162 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
1163 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
1164 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
1165 ui_out_table_body (uiout
);
1168 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1170 struct cleanup
*chain2
;
1173 if (!number_is_in_list (requested_threads
, tp
->num
))
1176 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
1178 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
1179 error (_("Requested thread not found in requested process"));
1183 if (ptid_equal (tp
->ptid
, current_ptid
))
1184 current_thread
= tp
->num
;
1186 if (tp
->state
== THREAD_EXITED
)
1189 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
1191 if (ui_out_is_mi_like_p (uiout
))
1193 /* Compatibility. */
1194 if (ptid_equal (tp
->ptid
, current_ptid
))
1195 ui_out_text (uiout
, "* ");
1197 ui_out_text (uiout
, " ");
1201 if (ptid_equal (tp
->ptid
, current_ptid
))
1202 ui_out_field_string (uiout
, "current", "*");
1204 ui_out_field_skip (uiout
, "current");
1207 ui_out_field_int (uiout
, "id", tp
->num
);
1209 /* For the CLI, we stuff everything into the target-id field.
1210 This is a gross hack to make the output come out looking
1211 correct. The underlying problem here is that ui-out has no
1212 way to specify that a field's space allocation should be
1213 shared by several fields. For MI, we do the right thing
1216 target_id
= target_pid_to_str (tp
->ptid
);
1217 extra_info
= target_extra_thread_info (tp
);
1218 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
1220 if (ui_out_is_mi_like_p (uiout
))
1222 ui_out_field_string (uiout
, "target-id", target_id
);
1224 ui_out_field_string (uiout
, "details", extra_info
);
1226 ui_out_field_string (uiout
, "name", name
);
1230 struct cleanup
*str_cleanup
;
1233 if (extra_info
&& name
)
1234 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
1236 else if (extra_info
)
1237 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
1239 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
1241 contents
= xstrdup (target_id
);
1242 str_cleanup
= make_cleanup (xfree
, contents
);
1244 ui_out_field_string (uiout
, "target-id", contents
);
1245 do_cleanups (str_cleanup
);
1248 if (tp
->state
== THREAD_RUNNING
)
1249 ui_out_text (uiout
, "(running)\n");
1252 /* The switch below puts us at the top of the stack (leaf
1254 switch_to_thread (tp
->ptid
);
1255 print_stack_frame (get_selected_frame (NULL
),
1256 /* For MI output, print frame level. */
1257 ui_out_is_mi_like_p (uiout
),
1261 if (ui_out_is_mi_like_p (uiout
))
1263 char *state
= "stopped";
1265 if (tp
->state
== THREAD_RUNNING
)
1267 ui_out_field_string (uiout
, "state", state
);
1270 core
= target_core_of_thread (tp
->ptid
);
1271 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
1272 ui_out_field_int (uiout
, "core", core
);
1274 do_cleanups (chain2
);
1277 /* Restores the current thread and the frame selected before
1278 the "info threads" command. */
1279 do_cleanups (old_chain
);
1281 if (pid
== -1 && requested_threads
== NULL
)
1283 gdb_assert (current_thread
!= -1
1285 || ptid_equal (inferior_ptid
, null_ptid
));
1286 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
1287 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
1289 if (current_thread
!= -1 && is_exited (current_ptid
))
1290 ui_out_message (uiout
, 0, "\n\
1291 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1293 else if (thread_list
1294 && current_thread
== -1
1295 && ptid_equal (current_ptid
, null_ptid
))
1296 ui_out_message (uiout
, 0, "\n\
1297 No selected thread. See `help thread'.\n");
1301 /* Print information about currently known threads
1303 Optional ARG is a thread id, or list of thread ids.
1305 Note: this has the drawback that it _really_ switches
1306 threads, which frees the frame cache. A no-side
1307 effects info-threads command would be nicer. */
1310 info_threads_command (char *arg
, int from_tty
)
1312 print_thread_info (current_uiout
, arg
, -1);
1315 /* See gdbthread.h. */
1318 switch_to_thread_no_regs (struct thread_info
*thread
)
1320 struct inferior
*inf
;
1322 inf
= find_inferior_ptid (thread
->ptid
);
1323 gdb_assert (inf
!= NULL
);
1324 set_current_program_space (inf
->pspace
);
1325 set_current_inferior (inf
);
1327 inferior_ptid
= thread
->ptid
;
1328 stop_pc
= ~(CORE_ADDR
) 0;
1331 /* Switch from one thread to another. */
1334 switch_to_thread (ptid_t ptid
)
1336 /* Switch the program space as well, if we can infer it from the now
1337 current thread. Otherwise, it's up to the caller to select the
1339 if (!ptid_equal (ptid
, null_ptid
))
1341 struct inferior
*inf
;
1343 inf
= find_inferior_ptid (ptid
);
1344 gdb_assert (inf
!= NULL
);
1345 set_current_program_space (inf
->pspace
);
1346 set_current_inferior (inf
);
1349 if (ptid_equal (ptid
, inferior_ptid
))
1352 inferior_ptid
= ptid
;
1353 reinit_frame_cache ();
1355 /* We don't check for is_stopped, because we're called at times
1356 while in the TARGET_RUNNING state, e.g., while handling an
1358 if (!ptid_equal (inferior_ptid
, null_ptid
)
1359 && !is_exited (ptid
)
1360 && !is_executing (ptid
))
1361 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1363 stop_pc
= ~(CORE_ADDR
) 0;
1367 restore_current_thread (ptid_t ptid
)
1369 switch_to_thread (ptid
);
1373 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1375 struct frame_info
*frame
= NULL
;
1378 /* This means there was no selected frame. */
1379 if (frame_level
== -1)
1381 select_frame (NULL
);
1385 gdb_assert (frame_level
>= 0);
1387 /* Restore by level first, check if the frame id is the same as
1388 expected. If that fails, try restoring by frame id. If that
1389 fails, nothing to do, just warn the user. */
1391 count
= frame_level
;
1392 frame
= find_relative_frame (get_current_frame (), &count
);
1395 /* The frame ids must match - either both valid or both outer_frame_id.
1396 The latter case is not failsafe, but since it's highly unlikely
1397 the search by level finds the wrong frame, it's 99.9(9)% of
1398 the time (for all practical purposes) safe. */
1399 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1401 /* Cool, all is fine. */
1402 select_frame (frame
);
1406 frame
= frame_find_by_id (a_frame_id
);
1409 /* Cool, refound it. */
1410 select_frame (frame
);
1414 /* Nothing else to do, the frame layout really changed. Select the
1415 innermost stack frame. */
1416 select_frame (get_current_frame ());
1418 /* Warn the user. */
1419 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1421 warning (_("Couldn't restore frame #%d in "
1422 "current thread. Bottom (innermost) frame selected:"),
1424 /* For MI, we should probably have a notification about
1425 current frame change. But this error is not very
1426 likely, so don't bother for now. */
1427 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1431 /* Data used by the cleanup installed by
1432 'make_cleanup_restore_current_thread'. */
1434 struct current_thread_cleanup
1436 /* Next in list of currently installed 'struct
1437 current_thread_cleanup' cleanups. See
1438 'current_thread_cleanup_chain' below. */
1439 struct current_thread_cleanup
*next
;
1441 ptid_t inferior_ptid
;
1442 struct frame_id selected_frame_id
;
1443 int selected_frame_level
;
1449 /* A chain of currently installed 'struct current_thread_cleanup'
1450 cleanups. Restoring the previously selected thread looks up the
1451 old thread in the thread list by ptid. If the thread changes ptid,
1452 we need to update the cleanup's thread structure so the look up
1454 static struct current_thread_cleanup
*current_thread_cleanup_chain
;
1456 /* A thread_ptid_changed observer. Update all currently installed
1457 current_thread_cleanup cleanups that want to switch back to
1458 OLD_PTID to switch back to NEW_PTID instead. */
1461 restore_current_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
1463 struct current_thread_cleanup
*it
;
1465 for (it
= current_thread_cleanup_chain
; it
!= NULL
; it
= it
->next
)
1467 if (ptid_equal (it
->inferior_ptid
, old_ptid
))
1468 it
->inferior_ptid
= new_ptid
;
1473 do_restore_current_thread_cleanup (void *arg
)
1475 struct thread_info
*tp
;
1476 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1478 tp
= find_thread_ptid (old
->inferior_ptid
);
1480 /* If the previously selected thread belonged to a process that has
1481 in the mean time been deleted (due to normal exit, detach, etc.),
1482 then don't revert back to it, but instead simply drop back to no
1485 && find_inferior_ptid (tp
->ptid
) != NULL
)
1486 restore_current_thread (old
->inferior_ptid
);
1489 restore_current_thread (null_ptid
);
1490 set_current_inferior (find_inferior_id (old
->inf_id
));
1493 /* The running state of the originally selected thread may have
1494 changed, so we have to recheck it here. */
1495 if (!ptid_equal (inferior_ptid
, null_ptid
)
1497 && is_stopped (inferior_ptid
)
1498 && target_has_registers
1500 && target_has_memory
)
1501 restore_selected_frame (old
->selected_frame_id
,
1502 old
->selected_frame_level
);
1506 restore_current_thread_cleanup_dtor (void *arg
)
1508 struct current_thread_cleanup
*old
= (struct current_thread_cleanup
*) arg
;
1509 struct thread_info
*tp
;
1510 struct inferior
*inf
;
1512 current_thread_cleanup_chain
= current_thread_cleanup_chain
->next
;
1514 tp
= find_thread_ptid (old
->inferior_ptid
);
1517 inf
= find_inferior_id (old
->inf_id
);
1519 inf
->removable
= old
->was_removable
;
1523 /* Set the thread reference count. */
1526 set_thread_refcount (void *data
)
1529 struct thread_array_cleanup
*ta_cleanup
1530 = (struct thread_array_cleanup
*) data
;
1532 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1533 ta_cleanup
->tp_array
[k
]->refcount
--;
1537 make_cleanup_restore_current_thread (void)
1539 struct thread_info
*tp
;
1540 struct frame_info
*frame
;
1541 struct current_thread_cleanup
*old
= XNEW (struct current_thread_cleanup
);
1543 old
->inferior_ptid
= inferior_ptid
;
1544 old
->inf_id
= current_inferior ()->num
;
1545 old
->was_removable
= current_inferior ()->removable
;
1547 old
->next
= current_thread_cleanup_chain
;
1548 current_thread_cleanup_chain
= old
;
1550 if (!ptid_equal (inferior_ptid
, null_ptid
))
1552 old
->was_stopped
= is_stopped (inferior_ptid
);
1553 if (old
->was_stopped
1554 && target_has_registers
1556 && target_has_memory
)
1558 /* When processing internal events, there might not be a
1559 selected frame. If we naively call get_selected_frame
1560 here, then we can end up reading debuginfo for the
1561 current frame, but we don't generally need the debuginfo
1563 frame
= get_selected_frame_if_set ();
1568 old
->selected_frame_id
= get_frame_id (frame
);
1569 old
->selected_frame_level
= frame_relative_level (frame
);
1571 tp
= find_thread_ptid (inferior_ptid
);
1576 current_inferior ()->removable
= 0;
1578 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1579 restore_current_thread_cleanup_dtor
);
1582 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1583 descending order. */
1585 static int tp_array_compar_ascending
;
1587 /* Sort an array for struct thread_info pointers by their NUM, order is
1588 determined by TP_ARRAY_COMPAR_ASCENDING. */
1591 tp_array_compar (const void *ap_voidp
, const void *bp_voidp
)
1593 const struct thread_info
*const *ap
1594 = (const struct thread_info
* const*) ap_voidp
;
1595 const struct thread_info
*const *bp
1596 = (const struct thread_info
* const*) bp_voidp
;
1598 return ((((*ap
)->num
> (*bp
)->num
) - ((*ap
)->num
< (*bp
)->num
))
1599 * (tp_array_compar_ascending
? +1 : -1));
1602 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1603 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1604 of two numbers seperated by a hyphen. Examples:
1606 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1607 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1608 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1611 thread_apply_all_command (char *cmd
, int from_tty
)
1613 struct cleanup
*old_chain
;
1616 struct thread_array_cleanup ta_cleanup
;
1618 tp_array_compar_ascending
= 0;
1620 && check_for_argument (&cmd
, "-ascending", strlen ("-ascending")))
1622 cmd
= skip_spaces (cmd
);
1623 tp_array_compar_ascending
= 1;
1626 if (cmd
== NULL
|| *cmd
== '\000')
1627 error (_("Please specify a command following the thread ID list"));
1629 update_thread_list ();
1631 old_chain
= make_cleanup_restore_current_thread ();
1633 /* Save a copy of the command in case it is clobbered by
1635 saved_cmd
= xstrdup (cmd
);
1636 make_cleanup (xfree
, saved_cmd
);
1638 /* Note this includes exited threads. */
1639 tc
= thread_count ();
1642 struct thread_info
**tp_array
;
1643 struct thread_info
*tp
;
1646 /* Save a copy of the thread_list in case we execute detach
1648 tp_array
= XNEWVEC (struct thread_info
*, tc
);
1649 make_cleanup (xfree
, tp_array
);
1651 ALL_NON_EXITED_THREADS (tp
)
1657 /* Because we skipped exited threads, we may end up with fewer
1658 threads in the array than the total count of threads. */
1659 gdb_assert (i
<= tc
);
1662 qsort (tp_array
, i
, sizeof (*tp_array
), tp_array_compar
);
1664 ta_cleanup
.tp_array
= tp_array
;
1665 ta_cleanup
.count
= i
;
1666 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1668 for (k
= 0; k
!= i
; k
++)
1669 if (thread_alive (tp_array
[k
]))
1671 switch_to_thread (tp_array
[k
]->ptid
);
1672 printf_filtered (_("\nThread %d (%s):\n"),
1674 target_pid_to_str (inferior_ptid
));
1675 execute_command (cmd
, from_tty
);
1677 /* Restore exact command used previously. */
1678 strcpy (cmd
, saved_cmd
);
1682 do_cleanups (old_chain
);
1686 thread_apply_command (char *tidlist
, int from_tty
)
1689 struct cleanup
*old_chain
;
1691 struct get_number_or_range_state state
;
1693 if (tidlist
== NULL
|| *tidlist
== '\000')
1694 error (_("Please specify a thread ID list"));
1696 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1699 error (_("Please specify a command following the thread ID list"));
1701 /* Save a copy of the command in case it is clobbered by
1703 saved_cmd
= xstrdup (cmd
);
1704 old_chain
= make_cleanup (xfree
, saved_cmd
);
1706 init_number_or_range (&state
, tidlist
);
1707 while (!state
.finished
&& state
.string
< cmd
)
1709 struct thread_info
*tp
;
1712 start
= get_number_or_range (&state
);
1714 make_cleanup_restore_current_thread ();
1716 tp
= find_thread_id (start
);
1719 warning (_("Unknown thread %d."), start
);
1720 else if (!thread_alive (tp
))
1721 warning (_("Thread %d has terminated."), start
);
1724 switch_to_thread (tp
->ptid
);
1726 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1727 target_pid_to_str (inferior_ptid
));
1728 execute_command (cmd
, from_tty
);
1730 /* Restore exact command used previously. */
1731 strcpy (cmd
, saved_cmd
);
1735 do_cleanups (old_chain
);
1738 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1739 if prefix of arg is `apply'. */
1742 thread_command (char *tidstr
, int from_tty
)
1746 if (ptid_equal (inferior_ptid
, null_ptid
))
1747 error (_("No thread selected"));
1749 if (target_has_stack
)
1751 if (is_exited (inferior_ptid
))
1752 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1753 pid_to_thread_id (inferior_ptid
),
1754 target_pid_to_str (inferior_ptid
));
1756 printf_filtered (_("[Current thread is %d (%s)]\n"),
1757 pid_to_thread_id (inferior_ptid
),
1758 target_pid_to_str (inferior_ptid
));
1761 error (_("No stack."));
1765 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1768 /* Implementation of `thread name'. */
1771 thread_name_command (char *arg
, int from_tty
)
1773 struct thread_info
*info
;
1775 if (ptid_equal (inferior_ptid
, null_ptid
))
1776 error (_("No thread selected"));
1778 arg
= skip_spaces (arg
);
1780 info
= inferior_thread ();
1782 info
->name
= arg
? xstrdup (arg
) : NULL
;
1785 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1788 thread_find_command (char *arg
, int from_tty
)
1790 struct thread_info
*tp
;
1792 unsigned long match
= 0;
1794 if (arg
== NULL
|| *arg
== '\0')
1795 error (_("Command requires an argument."));
1797 tmp
= re_comp (arg
);
1799 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1801 update_thread_list ();
1802 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1804 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1806 printf_filtered (_("Thread %d has name '%s'\n"),
1811 tmp
= target_thread_name (tp
);
1812 if (tmp
!= NULL
&& re_exec (tmp
))
1814 printf_filtered (_("Thread %d has target name '%s'\n"),
1819 tmp
= target_pid_to_str (tp
->ptid
);
1820 if (tmp
!= NULL
&& re_exec (tmp
))
1822 printf_filtered (_("Thread %d has target id '%s'\n"),
1827 tmp
= target_extra_thread_info (tp
);
1828 if (tmp
!= NULL
&& re_exec (tmp
))
1830 printf_filtered (_("Thread %d has extra info '%s'\n"),
1836 printf_filtered (_("No threads match '%s'\n"), arg
);
1839 /* Print notices when new threads are attached and detached. */
1840 int print_thread_events
= 1;
1842 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1843 struct cmd_list_element
*c
, const char *value
)
1845 fprintf_filtered (file
,
1846 _("Printing of thread events is %s.\n"),
1851 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1854 struct thread_info
*tp
;
1856 num
= value_as_long (parse_and_eval ((const char *) tidstr
));
1858 tp
= find_thread_id (num
);
1861 error (_("Thread ID %d not known."), num
);
1863 if (!thread_alive (tp
))
1864 error (_("Thread ID %d has terminated."), num
);
1866 switch_to_thread (tp
->ptid
);
1868 annotate_thread_changed ();
1870 ui_out_text (uiout
, "[Switching to thread ");
1871 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1872 ui_out_text (uiout
, " (");
1873 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1874 ui_out_text (uiout
, ")]");
1876 /* Note that we can't reach this with an exited thread, due to the
1877 thread_alive check above. */
1878 if (tp
->state
== THREAD_RUNNING
)
1879 ui_out_text (uiout
, "(running)\n");
1882 ui_out_text (uiout
, "\n");
1883 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1886 /* Since the current thread may have changed, see if there is any
1887 exited thread we can now delete. */
1894 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1896 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1897 error_message
, RETURN_MASK_ALL
) < 0)
1902 /* Update the 'threads_executing' global based on the threads we know
1906 update_threads_executing (void)
1908 struct thread_info
*tp
;
1910 threads_executing
= 0;
1911 ALL_NON_EXITED_THREADS (tp
)
1915 threads_executing
= 1;
1922 update_thread_list (void)
1924 target_update_thread_list ();
1925 update_threads_executing ();
1928 /* Return a new value for the selected thread's id. Return a value of 0 if
1929 no thread is selected, or no threads exist. */
1931 static struct value
*
1932 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1935 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1937 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1938 (tp
? tp
->num
: 0));
1941 /* Commands with a prefix of `thread'. */
1942 struct cmd_list_element
*thread_cmd_list
= NULL
;
1944 /* Implementation of `thread' variable. */
1946 static const struct internalvar_funcs thread_funcs
=
1948 thread_id_make_value
,
1954 _initialize_thread (void)
1956 static struct cmd_list_element
*thread_apply_list
= NULL
;
1958 add_info ("threads", info_threads_command
,
1959 _("Display currently known threads.\n\
1960 Usage: info threads [ID]...\n\
1961 Optional arguments are thread IDs with spaces between.\n\
1962 If no arguments, all threads are displayed."));
1964 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1965 Use this command to switch between threads.\n\
1966 The new thread ID must be currently known."),
1967 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1969 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1970 _("Apply a command to a list of threads."),
1971 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1973 add_cmd ("all", class_run
, thread_apply_all_command
,
1975 Apply a command to all threads.\n\
1977 Usage: thread apply all [-ascending] <command>\n\
1978 -ascending: Call <command> for all threads in ascending order.\n\
1979 The default is descending order.\
1981 &thread_apply_list
);
1983 add_cmd ("name", class_run
, thread_name_command
,
1984 _("Set the current thread's name.\n\
1985 Usage: thread name [NAME]\n\
1986 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1988 add_cmd ("find", class_run
, thread_find_command
, _("\
1989 Find threads that match a regular expression.\n\
1990 Usage: thread find REGEXP\n\
1991 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1994 add_com_alias ("t", "thread", class_run
, 1);
1996 add_setshow_boolean_cmd ("thread-events", no_class
,
1997 &print_thread_events
, _("\
1998 Set printing of thread events (such as thread start and exit)."), _("\
1999 Show printing of thread events (such as thread start and exit)."), NULL
,
2001 show_print_thread_events
,
2002 &setprintlist
, &showprintlist
);
2004 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);
2006 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed
);