1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 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 "continuations.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_command (char *tidstr
, int from_tty
);
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
));
163 clear_thread_inferior_resources (struct thread_info
*tp
)
165 /* NOTE: this will take care of any left-over step_resume breakpoints,
166 but not any user-specified thread-specific breakpoints. We can not
167 delete the breakpoint straight-off, because the inferior might not
168 be stopped at the moment. */
169 delete_at_next_stop (&tp
->control
.step_resume_breakpoint
);
170 delete_at_next_stop (&tp
->control
.exception_resume_breakpoint
);
171 delete_at_next_stop (&tp
->control
.single_step_breakpoints
);
173 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
175 bpstat_clear (&tp
->control
.stop_bpstat
);
177 btrace_teardown (tp
);
179 do_all_intermediate_continuations_thread (tp
, 1);
180 do_all_continuations_thread (tp
, 1);
184 free_thread (struct thread_info
*tp
)
188 if (tp
->private_dtor
)
189 tp
->private_dtor (tp
->private);
199 init_thread_list (void)
201 struct thread_info
*tp
, *tpnext
;
203 highest_thread_num
= 0;
208 for (tp
= thread_list
; tp
; tp
= tpnext
)
215 threads_executing
= 0;
218 /* Allocate a new thread with target id PTID and add it to the thread
221 static struct thread_info
*
222 new_thread (ptid_t ptid
)
224 struct thread_info
*tp
;
226 tp
= xcalloc (1, sizeof (*tp
));
229 tp
->num
= ++highest_thread_num
;
230 tp
->next
= thread_list
;
233 /* Nothing to follow yet. */
234 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
235 tp
->state
= THREAD_STOPPED
;
241 add_thread_silent (ptid_t ptid
)
243 struct thread_info
*tp
;
245 tp
= find_thread_ptid (ptid
);
247 /* Found an old thread with the same id. It has to be dead,
248 otherwise we wouldn't be adding a new thread with the same id.
249 The OS is reusing this id --- delete it, and recreate a new
252 /* In addition to deleting the thread, if this is the current
253 thread, then we need to take care that delete_thread doesn't
254 really delete the thread if it is inferior_ptid. Create a
255 new template thread in the list with an invalid ptid, switch
256 to it, delete the original thread, reset the new thread's
257 ptid, and switch to it. */
259 if (ptid_equal (inferior_ptid
, ptid
))
261 tp
= new_thread (null_ptid
);
263 /* Make switch_to_thread not read from the thread. */
264 tp
->state
= THREAD_EXITED
;
265 switch_to_thread (null_ptid
);
267 /* Now we can delete it. */
268 delete_thread (ptid
);
270 /* Now reset its ptid, and reswitch inferior_ptid to it. */
272 tp
->state
= THREAD_STOPPED
;
273 switch_to_thread (ptid
);
275 observer_notify_new_thread (tp
);
281 /* Just go ahead and delete it. */
282 delete_thread (ptid
);
285 tp
= new_thread (ptid
);
286 observer_notify_new_thread (tp
);
292 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
294 struct thread_info
*result
= add_thread_silent (ptid
);
296 result
->private = private;
298 if (print_thread_events
)
299 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
301 annotate_new_thread ();
306 add_thread (ptid_t ptid
)
308 return add_thread_with_info (ptid
, NULL
);
311 /* Delete thread PTID. If SILENT, don't notify the observer of this
314 delete_thread_1 (ptid_t ptid
, int silent
)
316 struct thread_info
*tp
, *tpprev
;
320 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
321 if (ptid_equal (tp
->ptid
, ptid
))
327 /* If this is the current thread, or there's code out there that
328 relies on it existing (refcount > 0) we can't delete yet. Mark
329 it as exited, and notify it. */
331 || ptid_equal (tp
->ptid
, inferior_ptid
))
333 if (tp
->state
!= THREAD_EXITED
)
335 observer_notify_thread_exit (tp
, silent
);
337 /* Tag it as exited. */
338 tp
->state
= THREAD_EXITED
;
340 /* Clear breakpoints, etc. associated with this thread. */
341 clear_thread_inferior_resources (tp
);
344 /* Will be really deleted some other time. */
348 /* Notify thread exit, but only if we haven't already. */
349 if (tp
->state
!= THREAD_EXITED
)
350 observer_notify_thread_exit (tp
, silent
);
352 /* Tag it as exited. */
353 tp
->state
= THREAD_EXITED
;
354 clear_thread_inferior_resources (tp
);
357 tpprev
->next
= tp
->next
;
359 thread_list
= tp
->next
;
364 /* Delete thread PTID and notify of thread exit. If this is
365 inferior_ptid, don't actually delete it, but tag it as exited and
366 do the notification. If PTID is the user selected thread, clear
369 delete_thread (ptid_t ptid
)
371 delete_thread_1 (ptid
, 0 /* not silent */);
375 delete_thread_silent (ptid_t ptid
)
377 delete_thread_1 (ptid
, 1 /* silent */);
381 find_thread_id (int num
)
383 struct thread_info
*tp
;
385 for (tp
= thread_list
; tp
; tp
= tp
->next
)
392 /* Find a thread_info by matching PTID. */
394 find_thread_ptid (ptid_t ptid
)
396 struct thread_info
*tp
;
398 for (tp
= thread_list
; tp
; tp
= tp
->next
)
399 if (ptid_equal (tp
->ptid
, ptid
))
406 * Thread iterator function.
408 * Calls a callback function once for each thread, so long as
409 * the callback function returns false. If the callback function
410 * returns true, the iteration will end and the current thread
411 * will be returned. This can be useful for implementing a
412 * search for a thread with arbitrary attributes, or for applying
413 * some operation to every thread.
415 * FIXME: some of the existing functionality, such as
416 * "Thread apply all", might be rewritten using this functionality.
420 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
423 struct thread_info
*tp
, *next
;
425 for (tp
= thread_list
; tp
; tp
= next
)
428 if ((*callback
) (tp
, data
))
439 struct thread_info
*tp
;
441 for (tp
= thread_list
; tp
; tp
= tp
->next
)
448 valid_thread_id (int num
)
450 struct thread_info
*tp
;
452 for (tp
= thread_list
; tp
; tp
= tp
->next
)
460 pid_to_thread_id (ptid_t ptid
)
462 struct thread_info
*tp
;
464 for (tp
= thread_list
; tp
; tp
= tp
->next
)
465 if (ptid_equal (tp
->ptid
, ptid
))
472 thread_id_to_pid (int num
)
474 struct thread_info
*thread
= find_thread_id (num
);
479 return pid_to_ptid (-1);
483 in_thread_list (ptid_t ptid
)
485 struct thread_info
*tp
;
487 for (tp
= thread_list
; tp
; tp
= tp
->next
)
488 if (ptid_equal (tp
->ptid
, ptid
))
491 return 0; /* Never heard of 'im. */
494 /* Finds the first thread of the inferior given by PID. If PID is -1,
495 return the first thread in the list. */
498 first_thread_of_process (int pid
)
500 struct thread_info
*tp
, *ret
= NULL
;
502 for (tp
= thread_list
; tp
; tp
= tp
->next
)
503 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
504 if (ret
== NULL
|| tp
->num
< ret
->num
)
511 any_thread_of_process (int pid
)
513 struct thread_info
*tp
;
515 gdb_assert (pid
!= 0);
517 /* Prefer the current thread. */
518 if (ptid_get_pid (inferior_ptid
) == pid
)
519 return inferior_thread ();
521 ALL_NON_EXITED_THREADS (tp
)
522 if (ptid_get_pid (tp
->ptid
) == pid
)
529 any_live_thread_of_process (int pid
)
531 struct thread_info
*curr_tp
= NULL
;
532 struct thread_info
*tp
;
533 struct thread_info
*tp_executing
= NULL
;
535 gdb_assert (pid
!= 0);
537 /* Prefer the current thread if it's not executing. */
538 if (ptid_get_pid (inferior_ptid
) == pid
)
540 /* If the current thread is dead, forget it. If it's not
541 executing, use it. Otherwise, still choose it (below), but
542 only if no other non-executing thread is found. */
543 curr_tp
= inferior_thread ();
544 if (curr_tp
->state
== THREAD_EXITED
)
546 else if (!curr_tp
->executing
)
550 ALL_NON_EXITED_THREADS (tp
)
551 if (ptid_get_pid (tp
->ptid
) == pid
)
559 /* If both the current thread and all live threads are executing,
560 prefer the current thread. */
564 /* Otherwise, just return an executing thread, if any. */
568 /* Print a list of thread ids currently known, and the total number of
569 threads. To be used from within catch_errors. */
571 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
573 struct thread_info
*tp
;
575 struct cleanup
*cleanup_chain
;
576 int current_thread
= -1;
578 update_thread_list ();
580 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
582 for (tp
= thread_list
; tp
; tp
= tp
->next
)
584 if (tp
->state
== THREAD_EXITED
)
587 if (ptid_equal (tp
->ptid
, inferior_ptid
))
588 current_thread
= tp
->num
;
591 ui_out_field_int (uiout
, "thread-id", tp
->num
);
594 do_cleanups (cleanup_chain
);
596 if (current_thread
!= -1)
597 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
598 ui_out_field_int (uiout
, "number-of-threads", num
);
602 /* Official gdblib interface function to get a list of thread ids and
605 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
607 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
608 error_message
, RETURN_MASK_ALL
) < 0)
613 /* Return true if TP is an active thread. */
615 thread_alive (struct thread_info
*tp
)
617 if (tp
->state
== THREAD_EXITED
)
619 if (!target_thread_alive (tp
->ptid
))
624 /* See gdbthreads.h. */
629 struct thread_info
*tp
, *next
;
631 for (tp
= thread_list
; tp
; tp
= next
)
634 if (!thread_alive (tp
))
635 delete_thread (tp
->ptid
);
640 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
642 struct inferior
*inf
;
643 struct thread_info
*tp
;
645 /* It can happen that what we knew as the target inferior id
646 changes. E.g, target remote may only discover the remote process
647 pid after adding the inferior to GDB's list. */
648 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
649 inf
->pid
= ptid_get_pid (new_ptid
);
651 tp
= find_thread_ptid (old_ptid
);
654 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
658 set_running (ptid_t ptid
, int running
)
660 struct thread_info
*tp
;
661 int all
= ptid_equal (ptid
, minus_one_ptid
);
663 /* We try not to notify the observer if no thread has actually changed
664 the running state -- merely to reduce the number of messages to
665 frontend. Frontend is supposed to handle multiple *running just fine. */
666 if (all
|| ptid_is_pid (ptid
))
670 for (tp
= thread_list
; tp
; tp
= tp
->next
)
671 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
673 if (tp
->state
== THREAD_EXITED
)
675 if (running
&& tp
->state
== THREAD_STOPPED
)
677 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
680 observer_notify_target_resumed (ptid
);
686 tp
= find_thread_ptid (ptid
);
688 gdb_assert (tp
->state
!= THREAD_EXITED
);
689 if (running
&& tp
->state
== THREAD_STOPPED
)
691 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
693 observer_notify_target_resumed (ptid
);
698 is_thread_state (ptid_t ptid
, enum thread_state state
)
700 struct thread_info
*tp
;
702 tp
= find_thread_ptid (ptid
);
704 return tp
->state
== state
;
708 is_stopped (ptid_t ptid
)
710 return is_thread_state (ptid
, THREAD_STOPPED
);
714 is_exited (ptid_t ptid
)
716 return is_thread_state (ptid
, THREAD_EXITED
);
720 is_running (ptid_t ptid
)
722 return is_thread_state (ptid
, THREAD_RUNNING
);
726 is_executing (ptid_t ptid
)
728 struct thread_info
*tp
;
730 tp
= find_thread_ptid (ptid
);
732 return tp
->executing
;
736 set_executing (ptid_t ptid
, int executing
)
738 struct thread_info
*tp
;
739 int all
= ptid_equal (ptid
, minus_one_ptid
);
741 if (all
|| ptid_is_pid (ptid
))
743 for (tp
= thread_list
; tp
; tp
= tp
->next
)
744 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
745 tp
->executing
= executing
;
749 tp
= find_thread_ptid (ptid
);
751 tp
->executing
= executing
;
754 /* It only takes one running thread to spawn more threads.*/
756 threads_executing
= 1;
757 /* Only clear the flag if the caller is telling us everything is
759 else if (ptid_equal (minus_one_ptid
, ptid
))
760 threads_executing
= 0;
763 /* See gdbthread.h. */
766 threads_are_executing (void)
768 return threads_executing
;
772 set_stop_requested (ptid_t ptid
, int stop
)
774 struct thread_info
*tp
;
775 int all
= ptid_equal (ptid
, minus_one_ptid
);
777 if (all
|| ptid_is_pid (ptid
))
779 for (tp
= thread_list
; tp
; tp
= tp
->next
)
780 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
781 tp
->stop_requested
= stop
;
785 tp
= find_thread_ptid (ptid
);
787 tp
->stop_requested
= stop
;
790 /* Call the stop requested observer so other components of GDB can
791 react to this request. */
793 observer_notify_thread_stop_requested (ptid
);
797 finish_thread_state (ptid_t ptid
)
799 struct thread_info
*tp
;
803 all
= ptid_equal (ptid
, minus_one_ptid
);
805 if (all
|| ptid_is_pid (ptid
))
807 for (tp
= thread_list
; tp
; tp
= tp
->next
)
809 if (tp
->state
== THREAD_EXITED
)
811 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
813 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
815 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
821 tp
= find_thread_ptid (ptid
);
823 if (tp
->state
!= THREAD_EXITED
)
825 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
827 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
832 observer_notify_target_resumed (ptid
);
836 finish_thread_state_cleanup (void *arg
)
838 ptid_t
*ptid_p
= arg
;
842 finish_thread_state (*ptid_p
);
846 pc_in_thread_step_range (CORE_ADDR pc
, struct thread_info
*thread
)
848 return (pc
>= thread
->control
.step_range_start
849 && pc
< thread
->control
.step_range_end
);
852 /* Prints the list of threads and their details on UIOUT.
853 This is a version of 'info_threads_command' suitable for
855 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
856 that should be printed. Otherwise, all threads are
858 If PID is not -1, only print threads from the process PID.
859 Otherwise, threads from all attached PIDs are printed.
860 If both REQUESTED_THREAD and PID are not -1, then the thread
861 is printed if it belongs to the specified process. Otherwise,
862 an error is raised. */
864 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
866 struct thread_info
*tp
;
868 struct cleanup
*old_chain
;
869 char *extra_info
, *name
, *target_id
;
870 int current_thread
= -1;
872 update_thread_list ();
873 current_ptid
= inferior_ptid
;
875 /* We'll be switching threads temporarily. */
876 old_chain
= make_cleanup_restore_current_thread ();
878 /* For backward compatibility, we make a list for MI. A table is
879 preferable for the CLI, though, because it shows table
881 if (ui_out_is_mi_like_p (uiout
))
882 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
887 for (tp
= thread_list
; tp
; tp
= tp
->next
)
889 if (!number_is_in_list (requested_threads
, tp
->num
))
892 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
895 if (tp
->state
== THREAD_EXITED
)
903 if (requested_threads
== NULL
|| *requested_threads
== '\0')
904 ui_out_message (uiout
, 0, _("No threads.\n"));
906 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
908 do_cleanups (old_chain
);
912 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
914 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
915 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
916 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
917 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
918 ui_out_table_body (uiout
);
921 for (tp
= thread_list
; tp
; tp
= tp
->next
)
923 struct cleanup
*chain2
;
926 if (!number_is_in_list (requested_threads
, tp
->num
))
929 if (pid
!= -1 && ptid_get_pid (tp
->ptid
) != pid
)
931 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
932 error (_("Requested thread not found in requested process"));
936 if (ptid_equal (tp
->ptid
, current_ptid
))
937 current_thread
= tp
->num
;
939 if (tp
->state
== THREAD_EXITED
)
942 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
944 if (ui_out_is_mi_like_p (uiout
))
947 if (ptid_equal (tp
->ptid
, current_ptid
))
948 ui_out_text (uiout
, "* ");
950 ui_out_text (uiout
, " ");
954 if (ptid_equal (tp
->ptid
, current_ptid
))
955 ui_out_field_string (uiout
, "current", "*");
957 ui_out_field_skip (uiout
, "current");
960 ui_out_field_int (uiout
, "id", tp
->num
);
962 /* For the CLI, we stuff everything into the target-id field.
963 This is a gross hack to make the output come out looking
964 correct. The underlying problem here is that ui-out has no
965 way to specify that a field's space allocation should be
966 shared by several fields. For MI, we do the right thing
969 target_id
= target_pid_to_str (tp
->ptid
);
970 extra_info
= target_extra_thread_info (tp
);
971 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
973 if (ui_out_is_mi_like_p (uiout
))
975 ui_out_field_string (uiout
, "target-id", target_id
);
977 ui_out_field_string (uiout
, "details", extra_info
);
979 ui_out_field_string (uiout
, "name", name
);
983 struct cleanup
*str_cleanup
;
986 if (extra_info
&& name
)
987 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
990 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
992 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
994 contents
= xstrdup (target_id
);
995 str_cleanup
= make_cleanup (xfree
, contents
);
997 ui_out_field_string (uiout
, "target-id", contents
);
998 do_cleanups (str_cleanup
);
1001 if (tp
->state
== THREAD_RUNNING
)
1002 ui_out_text (uiout
, "(running)\n");
1005 /* The switch below puts us at the top of the stack (leaf
1007 switch_to_thread (tp
->ptid
);
1008 print_stack_frame (get_selected_frame (NULL
),
1009 /* For MI output, print frame level. */
1010 ui_out_is_mi_like_p (uiout
),
1014 if (ui_out_is_mi_like_p (uiout
))
1016 char *state
= "stopped";
1018 if (tp
->state
== THREAD_RUNNING
)
1020 ui_out_field_string (uiout
, "state", state
);
1023 core
= target_core_of_thread (tp
->ptid
);
1024 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
1025 ui_out_field_int (uiout
, "core", core
);
1027 do_cleanups (chain2
);
1030 /* Restores the current thread and the frame selected before
1031 the "info threads" command. */
1032 do_cleanups (old_chain
);
1034 if (pid
== -1 && requested_threads
== NULL
)
1036 gdb_assert (current_thread
!= -1
1038 || ptid_equal (inferior_ptid
, null_ptid
));
1039 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
1040 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
1042 if (current_thread
!= -1 && is_exited (current_ptid
))
1043 ui_out_message (uiout
, 0, "\n\
1044 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1046 else if (thread_list
1047 && current_thread
== -1
1048 && ptid_equal (current_ptid
, null_ptid
))
1049 ui_out_message (uiout
, 0, "\n\
1050 No selected thread. See `help thread'.\n");
1054 /* Print information about currently known threads
1056 Optional ARG is a thread id, or list of thread ids.
1058 Note: this has the drawback that it _really_ switches
1059 threads, which frees the frame cache. A no-side
1060 effects info-threads command would be nicer. */
1063 info_threads_command (char *arg
, int from_tty
)
1065 print_thread_info (current_uiout
, arg
, -1);
1068 /* Switch from one thread to another. */
1071 switch_to_thread (ptid_t ptid
)
1073 /* Switch the program space as well, if we can infer it from the now
1074 current thread. Otherwise, it's up to the caller to select the
1076 if (!ptid_equal (ptid
, null_ptid
))
1078 struct inferior
*inf
;
1080 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1081 gdb_assert (inf
!= NULL
);
1082 set_current_program_space (inf
->pspace
);
1083 set_current_inferior (inf
);
1086 if (ptid_equal (ptid
, inferior_ptid
))
1089 inferior_ptid
= ptid
;
1090 reinit_frame_cache ();
1092 /* We don't check for is_stopped, because we're called at times
1093 while in the TARGET_RUNNING state, e.g., while handling an
1095 if (!ptid_equal (inferior_ptid
, null_ptid
)
1096 && !is_exited (ptid
)
1097 && !is_executing (ptid
))
1098 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1100 stop_pc
= ~(CORE_ADDR
) 0;
1104 restore_current_thread (ptid_t ptid
)
1106 switch_to_thread (ptid
);
1110 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1112 struct frame_info
*frame
= NULL
;
1115 /* This means there was no selected frame. */
1116 if (frame_level
== -1)
1118 select_frame (NULL
);
1122 gdb_assert (frame_level
>= 0);
1124 /* Restore by level first, check if the frame id is the same as
1125 expected. If that fails, try restoring by frame id. If that
1126 fails, nothing to do, just warn the user. */
1128 count
= frame_level
;
1129 frame
= find_relative_frame (get_current_frame (), &count
);
1132 /* The frame ids must match - either both valid or both outer_frame_id.
1133 The latter case is not failsafe, but since it's highly unlikely
1134 the search by level finds the wrong frame, it's 99.9(9)% of
1135 the time (for all practical purposes) safe. */
1136 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1138 /* Cool, all is fine. */
1139 select_frame (frame
);
1143 frame
= frame_find_by_id (a_frame_id
);
1146 /* Cool, refound it. */
1147 select_frame (frame
);
1151 /* Nothing else to do, the frame layout really changed. Select the
1152 innermost stack frame. */
1153 select_frame (get_current_frame ());
1155 /* Warn the user. */
1156 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1158 warning (_("Couldn't restore frame #%d in "
1159 "current thread. Bottom (innermost) frame selected:"),
1161 /* For MI, we should probably have a notification about
1162 current frame change. But this error is not very
1163 likely, so don't bother for now. */
1164 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1168 struct current_thread_cleanup
1170 ptid_t inferior_ptid
;
1171 struct frame_id selected_frame_id
;
1172 int selected_frame_level
;
1179 do_restore_current_thread_cleanup (void *arg
)
1181 struct thread_info
*tp
;
1182 struct current_thread_cleanup
*old
= arg
;
1184 tp
= find_thread_ptid (old
->inferior_ptid
);
1186 /* If the previously selected thread belonged to a process that has
1187 in the mean time been deleted (due to normal exit, detach, etc.),
1188 then don't revert back to it, but instead simply drop back to no
1191 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1192 restore_current_thread (old
->inferior_ptid
);
1195 restore_current_thread (null_ptid
);
1196 set_current_inferior (find_inferior_id (old
->inf_id
));
1199 /* The running state of the originally selected thread may have
1200 changed, so we have to recheck it here. */
1201 if (!ptid_equal (inferior_ptid
, null_ptid
)
1203 && is_stopped (inferior_ptid
)
1204 && target_has_registers
1206 && target_has_memory
)
1207 restore_selected_frame (old
->selected_frame_id
,
1208 old
->selected_frame_level
);
1212 restore_current_thread_cleanup_dtor (void *arg
)
1214 struct current_thread_cleanup
*old
= arg
;
1215 struct thread_info
*tp
;
1216 struct inferior
*inf
;
1218 tp
= find_thread_ptid (old
->inferior_ptid
);
1221 inf
= find_inferior_id (old
->inf_id
);
1223 inf
->removable
= old
->was_removable
;
1227 /* Set the thread reference count. */
1230 set_thread_refcount (void *data
)
1233 struct thread_array_cleanup
*ta_cleanup
= data
;
1235 for (k
= 0; k
!= ta_cleanup
->count
; k
++)
1236 ta_cleanup
->tp_array
[k
]->refcount
--;
1240 make_cleanup_restore_current_thread (void)
1242 struct thread_info
*tp
;
1243 struct frame_info
*frame
;
1244 struct current_thread_cleanup
*old
;
1246 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1247 old
->inferior_ptid
= inferior_ptid
;
1248 old
->inf_id
= current_inferior ()->num
;
1249 old
->was_removable
= current_inferior ()->removable
;
1251 if (!ptid_equal (inferior_ptid
, null_ptid
))
1253 old
->was_stopped
= is_stopped (inferior_ptid
);
1254 if (old
->was_stopped
1255 && target_has_registers
1257 && target_has_memory
)
1259 /* When processing internal events, there might not be a
1260 selected frame. If we naively call get_selected_frame
1261 here, then we can end up reading debuginfo for the
1262 current frame, but we don't generally need the debuginfo
1264 frame
= get_selected_frame_if_set ();
1269 old
->selected_frame_id
= get_frame_id (frame
);
1270 old
->selected_frame_level
= frame_relative_level (frame
);
1272 tp
= find_thread_ptid (inferior_ptid
);
1277 current_inferior ()->removable
= 0;
1279 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1280 restore_current_thread_cleanup_dtor
);
1283 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1284 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1285 of two numbers seperated by a hyphen. Examples:
1287 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1288 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1289 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1292 thread_apply_all_command (char *cmd
, int from_tty
)
1294 struct cleanup
*old_chain
;
1297 struct thread_array_cleanup ta_cleanup
;
1299 if (cmd
== NULL
|| *cmd
== '\000')
1300 error (_("Please specify a command following the thread ID list"));
1302 update_thread_list ();
1304 old_chain
= make_cleanup_restore_current_thread ();
1306 /* Save a copy of the command in case it is clobbered by
1308 saved_cmd
= xstrdup (cmd
);
1309 make_cleanup (xfree
, saved_cmd
);
1310 tc
= thread_count ();
1314 struct thread_info
**tp_array
;
1315 struct thread_info
*tp
;
1318 /* Save a copy of the thread_list in case we execute detach
1320 tp_array
= xmalloc (sizeof (struct thread_info
*) * tc
);
1321 make_cleanup (xfree
, tp_array
);
1322 ta_cleanup
.tp_array
= tp_array
;
1323 ta_cleanup
.count
= tc
;
1325 ALL_NON_EXITED_THREADS (tp
)
1332 make_cleanup (set_thread_refcount
, &ta_cleanup
);
1334 for (k
= 0; k
!= i
; k
++)
1335 if (thread_alive (tp_array
[k
]))
1337 switch_to_thread (tp_array
[k
]->ptid
);
1338 printf_filtered (_("\nThread %d (%s):\n"),
1340 target_pid_to_str (inferior_ptid
));
1341 execute_command (cmd
, from_tty
);
1343 /* Restore exact command used previously. */
1344 strcpy (cmd
, saved_cmd
);
1348 do_cleanups (old_chain
);
1352 thread_apply_command (char *tidlist
, int from_tty
)
1355 struct cleanup
*old_chain
;
1357 struct get_number_or_range_state state
;
1359 if (tidlist
== NULL
|| *tidlist
== '\000')
1360 error (_("Please specify a thread ID list"));
1362 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1365 error (_("Please specify a command following the thread ID list"));
1367 /* Save a copy of the command in case it is clobbered by
1369 saved_cmd
= xstrdup (cmd
);
1370 old_chain
= make_cleanup (xfree
, saved_cmd
);
1372 init_number_or_range (&state
, tidlist
);
1373 while (!state
.finished
&& state
.string
< cmd
)
1375 struct thread_info
*tp
;
1378 start
= get_number_or_range (&state
);
1380 make_cleanup_restore_current_thread ();
1382 tp
= find_thread_id (start
);
1385 warning (_("Unknown thread %d."), start
);
1386 else if (!thread_alive (tp
))
1387 warning (_("Thread %d has terminated."), start
);
1390 switch_to_thread (tp
->ptid
);
1392 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1393 target_pid_to_str (inferior_ptid
));
1394 execute_command (cmd
, from_tty
);
1396 /* Restore exact command used previously. */
1397 strcpy (cmd
, saved_cmd
);
1401 do_cleanups (old_chain
);
1404 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1405 if prefix of arg is `apply'. */
1408 thread_command (char *tidstr
, int from_tty
)
1412 if (ptid_equal (inferior_ptid
, null_ptid
))
1413 error (_("No thread selected"));
1415 if (target_has_stack
)
1417 if (is_exited (inferior_ptid
))
1418 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1419 pid_to_thread_id (inferior_ptid
),
1420 target_pid_to_str (inferior_ptid
));
1422 printf_filtered (_("[Current thread is %d (%s)]\n"),
1423 pid_to_thread_id (inferior_ptid
),
1424 target_pid_to_str (inferior_ptid
));
1427 error (_("No stack."));
1431 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1434 /* Implementation of `thread name'. */
1437 thread_name_command (char *arg
, int from_tty
)
1439 struct thread_info
*info
;
1441 if (ptid_equal (inferior_ptid
, null_ptid
))
1442 error (_("No thread selected"));
1444 arg
= skip_spaces (arg
);
1446 info
= inferior_thread ();
1448 info
->name
= arg
? xstrdup (arg
) : NULL
;
1451 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1454 thread_find_command (char *arg
, int from_tty
)
1456 struct thread_info
*tp
;
1458 unsigned long match
= 0;
1460 if (arg
== NULL
|| *arg
== '\0')
1461 error (_("Command requires an argument."));
1463 tmp
= re_comp (arg
);
1465 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1467 update_thread_list ();
1468 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1470 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1472 printf_filtered (_("Thread %d has name '%s'\n"),
1477 tmp
= target_thread_name (tp
);
1478 if (tmp
!= NULL
&& re_exec (tmp
))
1480 printf_filtered (_("Thread %d has target name '%s'\n"),
1485 tmp
= target_pid_to_str (tp
->ptid
);
1486 if (tmp
!= NULL
&& re_exec (tmp
))
1488 printf_filtered (_("Thread %d has target id '%s'\n"),
1493 tmp
= target_extra_thread_info (tp
);
1494 if (tmp
!= NULL
&& re_exec (tmp
))
1496 printf_filtered (_("Thread %d has extra info '%s'\n"),
1502 printf_filtered (_("No threads match '%s'\n"), arg
);
1505 /* Print notices when new threads are attached and detached. */
1506 int print_thread_events
= 1;
1508 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1509 struct cmd_list_element
*c
, const char *value
)
1511 fprintf_filtered (file
,
1512 _("Printing of thread events is %s.\n"),
1517 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1520 struct thread_info
*tp
;
1522 num
= value_as_long (parse_and_eval (tidstr
));
1524 tp
= find_thread_id (num
);
1527 error (_("Thread ID %d not known."), num
);
1529 if (!thread_alive (tp
))
1530 error (_("Thread ID %d has terminated."), num
);
1532 switch_to_thread (tp
->ptid
);
1534 annotate_thread_changed ();
1536 ui_out_text (uiout
, "[Switching to thread ");
1537 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1538 ui_out_text (uiout
, " (");
1539 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1540 ui_out_text (uiout
, ")]");
1542 /* Note that we can't reach this with an exited thread, due to the
1543 thread_alive check above. */
1544 if (tp
->state
== THREAD_RUNNING
)
1545 ui_out_text (uiout
, "(running)\n");
1548 ui_out_text (uiout
, "\n");
1549 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
1552 /* Since the current thread may have changed, see if there is any
1553 exited thread we can now delete. */
1560 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1562 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1563 error_message
, RETURN_MASK_ALL
) < 0)
1568 /* Update the 'threads_executing' global based on the threads we know
1572 update_threads_executing (void)
1574 struct thread_info
*tp
;
1576 threads_executing
= 0;
1577 ALL_NON_EXITED_THREADS (tp
)
1581 threads_executing
= 1;
1588 update_thread_list (void)
1590 target_update_thread_list ();
1591 update_threads_executing ();
1594 /* Return a new value for the selected thread's id. Return a value of 0 if
1595 no thread is selected, or no threads exist. */
1597 static struct value
*
1598 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1601 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1603 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1604 (tp
? tp
->num
: 0));
1607 /* Commands with a prefix of `thread'. */
1608 struct cmd_list_element
*thread_cmd_list
= NULL
;
1610 /* Implementation of `thread' variable. */
1612 static const struct internalvar_funcs thread_funcs
=
1614 thread_id_make_value
,
1620 _initialize_thread (void)
1622 static struct cmd_list_element
*thread_apply_list
= NULL
;
1624 add_info ("threads", info_threads_command
,
1625 _("Display currently known threads.\n\
1626 Usage: info threads [ID]...\n\
1627 Optional arguments are thread IDs with spaces between.\n\
1628 If no arguments, all threads are displayed."));
1630 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1631 Use this command to switch between threads.\n\
1632 The new thread ID must be currently known."),
1633 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1635 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1636 _("Apply a command to a list of threads."),
1637 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1639 add_cmd ("all", class_run
, thread_apply_all_command
,
1640 _("Apply a command to all threads."), &thread_apply_list
);
1642 add_cmd ("name", class_run
, thread_name_command
,
1643 _("Set the current thread's name.\n\
1644 Usage: thread name [NAME]\n\
1645 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1647 add_cmd ("find", class_run
, thread_find_command
, _("\
1648 Find threads that match a regular expression.\n\
1649 Usage: thread find REGEXP\n\
1650 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1654 add_com_alias ("t", "thread", class_run
, 1);
1656 add_setshow_boolean_cmd ("thread-events", no_class
,
1657 &print_thread_events
, _("\
1658 Set printing of thread events (such as thread start and exit)."), _("\
1659 Show printing of thread events (such as thread start and exit)."), NULL
,
1661 show_print_thread_events
,
1662 &setprintlist
, &showprintlist
);
1664 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);