1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "gdbthread.h"
32 #include "exceptions.h"
37 #include "gdb_string.h"
40 #include <sys/types.h>
45 #include "cli/cli-decode.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 static struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 static void thread_command (char *tidstr
, int from_tty
);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info
*);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t
);
64 static void prune_threads (void);
66 /* Frontend view of the thread state. Possible extensions: stepping,
67 finishing, until(ling),... */
76 inferior_thread (void)
78 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
84 delete_step_resume_breakpoint (struct thread_info
*tp
)
86 if (tp
&& tp
->control
.step_resume_breakpoint
)
88 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
89 tp
->control
.step_resume_breakpoint
= NULL
;
94 delete_exception_resume_breakpoint (struct thread_info
*tp
)
96 if (tp
&& tp
->control
.exception_resume_breakpoint
)
98 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
99 tp
->control
.exception_resume_breakpoint
= NULL
;
104 clear_thread_inferior_resources (struct thread_info
*tp
)
106 /* NOTE: this will take care of any left-over step_resume breakpoints,
107 but not any user-specified thread-specific breakpoints. We can not
108 delete the breakpoint straight-off, because the inferior might not
109 be stopped at the moment. */
110 if (tp
->control
.step_resume_breakpoint
)
112 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
113 tp
->control
.step_resume_breakpoint
= NULL
;
116 if (tp
->control
.exception_resume_breakpoint
)
118 tp
->control
.exception_resume_breakpoint
->disposition
119 = disp_del_at_next_stop
;
120 tp
->control
.exception_resume_breakpoint
= NULL
;
123 bpstat_clear (&tp
->control
.stop_bpstat
);
125 discard_all_intermediate_continuations_thread (tp
);
126 discard_all_continuations_thread (tp
);
128 delete_longjmp_breakpoint (tp
->num
);
132 free_thread (struct thread_info
*tp
)
134 clear_thread_inferior_resources (tp
);
138 if (tp
->private_dtor
)
139 tp
->private_dtor (tp
->private);
149 init_thread_list (void)
151 struct thread_info
*tp
, *tpnext
;
153 highest_thread_num
= 0;
158 for (tp
= thread_list
; tp
; tp
= tpnext
)
167 /* Allocate a new thread with target id PTID and add it to the thread
170 static struct thread_info
*
171 new_thread (ptid_t ptid
)
173 struct thread_info
*tp
;
175 tp
= xcalloc (1, sizeof (*tp
));
178 tp
->num
= ++highest_thread_num
;
179 tp
->next
= thread_list
;
182 /* Nothing to follow yet. */
183 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
184 tp
->state_
= THREAD_STOPPED
;
190 add_thread_silent (ptid_t ptid
)
192 struct thread_info
*tp
;
194 tp
= find_thread_ptid (ptid
);
196 /* Found an old thread with the same id. It has to be dead,
197 otherwise we wouldn't be adding a new thread with the same id.
198 The OS is reusing this id --- delete it, and recreate a new
201 /* In addition to deleting the thread, if this is the current
202 thread, then we need to take care that delete_thread doesn't
203 really delete the thread if it is inferior_ptid. Create a
204 new template thread in the list with an invalid ptid, switch
205 to it, delete the original thread, reset the new thread's
206 ptid, and switch to it. */
208 if (ptid_equal (inferior_ptid
, ptid
))
210 tp
= new_thread (null_ptid
);
212 /* Make switch_to_thread not read from the thread. */
213 tp
->state_
= THREAD_EXITED
;
214 switch_to_thread (null_ptid
);
216 /* Now we can delete it. */
217 delete_thread (ptid
);
219 /* Now reset its ptid, and reswitch inferior_ptid to it. */
221 tp
->state_
= THREAD_STOPPED
;
222 switch_to_thread (ptid
);
224 observer_notify_new_thread (tp
);
230 /* Just go ahead and delete it. */
231 delete_thread (ptid
);
234 tp
= new_thread (ptid
);
235 observer_notify_new_thread (tp
);
241 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
243 struct thread_info
*result
= add_thread_silent (ptid
);
245 result
->private = private;
247 if (print_thread_events
)
248 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
250 annotate_new_thread ();
255 add_thread (ptid_t ptid
)
257 return add_thread_with_info (ptid
, NULL
);
260 /* Delete thread PTID. If SILENT, don't notify the observer of this
263 delete_thread_1 (ptid_t ptid
, int silent
)
265 struct thread_info
*tp
, *tpprev
;
269 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
270 if (ptid_equal (tp
->ptid
, ptid
))
276 /* If this is the current thread, or there's code out there that
277 relies on it existing (refcount > 0) we can't delete yet. Mark
278 it as exited, and notify it. */
280 || ptid_equal (tp
->ptid
, inferior_ptid
))
282 if (tp
->state_
!= THREAD_EXITED
)
284 observer_notify_thread_exit (tp
, silent
);
286 /* Tag it as exited. */
287 tp
->state_
= THREAD_EXITED
;
289 /* Clear breakpoints, etc. associated with this thread. */
290 clear_thread_inferior_resources (tp
);
293 /* Will be really deleted some other time. */
298 tpprev
->next
= tp
->next
;
300 thread_list
= tp
->next
;
302 /* Notify thread exit, but only if we haven't already. */
303 if (tp
->state_
!= THREAD_EXITED
)
304 observer_notify_thread_exit (tp
, silent
);
309 /* Delete thread PTID and notify of thread exit. If this is
310 inferior_ptid, don't actually delete it, but tag it as exited and
311 do the notification. If PTID is the user selected thread, clear
314 delete_thread (ptid_t ptid
)
316 delete_thread_1 (ptid
, 0 /* not silent */);
320 delete_thread_silent (ptid_t ptid
)
322 delete_thread_1 (ptid
, 1 /* silent */);
326 find_thread_id (int num
)
328 struct thread_info
*tp
;
330 for (tp
= thread_list
; tp
; tp
= tp
->next
)
337 /* Find a thread_info by matching PTID. */
339 find_thread_ptid (ptid_t ptid
)
341 struct thread_info
*tp
;
343 for (tp
= thread_list
; tp
; tp
= tp
->next
)
344 if (ptid_equal (tp
->ptid
, ptid
))
351 * Thread iterator function.
353 * Calls a callback function once for each thread, so long as
354 * the callback function returns false. If the callback function
355 * returns true, the iteration will end and the current thread
356 * will be returned. This can be useful for implementing a
357 * search for a thread with arbitrary attributes, or for applying
358 * some operation to every thread.
360 * FIXME: some of the existing functionality, such as
361 * "Thread apply all", might be rewritten using this functionality.
365 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
368 struct thread_info
*tp
, *next
;
370 for (tp
= thread_list
; tp
; tp
= next
)
373 if ((*callback
) (tp
, data
))
384 struct thread_info
*tp
;
386 for (tp
= thread_list
; tp
; tp
= tp
->next
)
393 valid_thread_id (int num
)
395 struct thread_info
*tp
;
397 for (tp
= thread_list
; tp
; tp
= tp
->next
)
405 pid_to_thread_id (ptid_t ptid
)
407 struct thread_info
*tp
;
409 for (tp
= thread_list
; tp
; tp
= tp
->next
)
410 if (ptid_equal (tp
->ptid
, ptid
))
417 thread_id_to_pid (int num
)
419 struct thread_info
*thread
= find_thread_id (num
);
424 return pid_to_ptid (-1);
428 in_thread_list (ptid_t ptid
)
430 struct thread_info
*tp
;
432 for (tp
= thread_list
; tp
; tp
= tp
->next
)
433 if (ptid_equal (tp
->ptid
, ptid
))
436 return 0; /* Never heard of 'im. */
439 /* Finds the first thread of the inferior given by PID. If PID is -1,
440 return the first thread in the list. */
443 first_thread_of_process (int pid
)
445 struct thread_info
*tp
, *ret
= NULL
;
447 for (tp
= thread_list
; tp
; tp
= tp
->next
)
448 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
449 if (ret
== NULL
|| tp
->num
< ret
->num
)
456 any_thread_of_process (int pid
)
458 struct thread_info
*tp
;
460 for (tp
= thread_list
; tp
; tp
= tp
->next
)
461 if (ptid_get_pid (tp
->ptid
) == pid
)
468 any_live_thread_of_process (int pid
)
470 struct thread_info
*tp
;
471 struct thread_info
*tp_running
= NULL
;
473 for (tp
= thread_list
; tp
; tp
= tp
->next
)
474 if (ptid_get_pid (tp
->ptid
) == pid
)
476 if (tp
->state_
== THREAD_STOPPED
)
478 else if (tp
->state_
== THREAD_RUNNING
)
485 /* Print a list of thread ids currently known, and the total number of
486 threads. To be used from within catch_errors. */
488 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
490 struct thread_info
*tp
;
492 struct cleanup
*cleanup_chain
;
493 int current_thread
= -1;
495 update_thread_list ();
497 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
499 for (tp
= thread_list
; tp
; tp
= tp
->next
)
501 if (tp
->state_
== THREAD_EXITED
)
504 if (ptid_equal (tp
->ptid
, inferior_ptid
))
505 current_thread
= tp
->num
;
508 ui_out_field_int (uiout
, "thread-id", tp
->num
);
511 do_cleanups (cleanup_chain
);
513 if (current_thread
!= -1)
514 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
515 ui_out_field_int (uiout
, "number-of-threads", num
);
519 /* Official gdblib interface function to get a list of thread ids and
522 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
524 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
525 error_message
, RETURN_MASK_ALL
) < 0)
530 /* Return true if TP is an active thread. */
532 thread_alive (struct thread_info
*tp
)
534 if (tp
->state_
== THREAD_EXITED
)
536 if (!target_thread_alive (tp
->ptid
))
544 struct thread_info
*tp
, *next
;
546 for (tp
= thread_list
; tp
; tp
= next
)
549 if (!thread_alive (tp
))
550 delete_thread (tp
->ptid
);
555 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
557 struct inferior
*inf
;
558 struct thread_info
*tp
;
560 /* It can happen that what we knew as the target inferior id
561 changes. E.g, target remote may only discover the remote process
562 pid after adding the inferior to GDB's list. */
563 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
564 inf
->pid
= ptid_get_pid (new_ptid
);
566 tp
= find_thread_ptid (old_ptid
);
569 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
573 set_running (ptid_t ptid
, int running
)
575 struct thread_info
*tp
;
576 int all
= ptid_equal (ptid
, minus_one_ptid
);
578 /* We try not to notify the observer if no thread has actually changed
579 the running state -- merely to reduce the number of messages to
580 frontend. Frontend is supposed to handle multiple *running just fine. */
581 if (all
|| ptid_is_pid (ptid
))
585 for (tp
= thread_list
; tp
; tp
= tp
->next
)
586 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
588 if (tp
->state_
== THREAD_EXITED
)
590 if (running
&& tp
->state_
== THREAD_STOPPED
)
592 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
595 observer_notify_target_resumed (ptid
);
601 tp
= find_thread_ptid (ptid
);
603 gdb_assert (tp
->state_
!= THREAD_EXITED
);
604 if (running
&& tp
->state_
== THREAD_STOPPED
)
606 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
608 observer_notify_target_resumed (ptid
);
613 is_thread_state (ptid_t ptid
, enum thread_state state
)
615 struct thread_info
*tp
;
617 tp
= find_thread_ptid (ptid
);
619 return tp
->state_
== state
;
623 is_stopped (ptid_t ptid
)
625 return is_thread_state (ptid
, THREAD_STOPPED
);
629 is_exited (ptid_t ptid
)
631 return is_thread_state (ptid
, THREAD_EXITED
);
635 is_running (ptid_t ptid
)
637 return is_thread_state (ptid
, THREAD_RUNNING
);
643 struct thread_info
*tp
;
645 for (tp
= thread_list
; tp
; tp
= tp
->next
)
646 if (tp
->state_
== THREAD_RUNNING
)
653 is_executing (ptid_t ptid
)
655 struct thread_info
*tp
;
657 tp
= find_thread_ptid (ptid
);
659 return tp
->executing_
;
663 set_executing (ptid_t ptid
, int executing
)
665 struct thread_info
*tp
;
666 int all
= ptid_equal (ptid
, minus_one_ptid
);
668 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
))
672 tp
->executing_
= executing
;
676 tp
= find_thread_ptid (ptid
);
678 tp
->executing_
= executing
;
683 set_stop_requested (ptid_t ptid
, int stop
)
685 struct thread_info
*tp
;
686 int all
= ptid_equal (ptid
, minus_one_ptid
);
688 if (all
|| ptid_is_pid (ptid
))
690 for (tp
= thread_list
; tp
; tp
= tp
->next
)
691 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
692 tp
->stop_requested
= stop
;
696 tp
= find_thread_ptid (ptid
);
698 tp
->stop_requested
= stop
;
701 /* Call the stop requested observer so other components of GDB can
702 react to this request. */
704 observer_notify_thread_stop_requested (ptid
);
708 finish_thread_state (ptid_t ptid
)
710 struct thread_info
*tp
;
714 all
= ptid_equal (ptid
, minus_one_ptid
);
716 if (all
|| ptid_is_pid (ptid
))
718 for (tp
= thread_list
; tp
; tp
= tp
->next
)
720 if (tp
->state_
== THREAD_EXITED
)
722 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
724 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
726 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
732 tp
= find_thread_ptid (ptid
);
734 if (tp
->state_
!= THREAD_EXITED
)
736 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
738 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
743 observer_notify_target_resumed (ptid
);
747 finish_thread_state_cleanup (void *arg
)
749 ptid_t
*ptid_p
= arg
;
753 finish_thread_state (*ptid_p
);
756 /* Prints the list of threads and their details on UIOUT.
757 This is a version of 'info_thread_command' suitable for
759 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
760 that should be printed. Otherwise, all threads are
762 If PID is not -1, only print threads from the process PID.
763 Otherwise, threads from all attached PIDs are printed.
764 If both REQUESTED_THREAD and PID are not -1, then the thread
765 is printed if it belongs to the specified process. Otherwise,
766 an error is raised. */
768 print_thread_info (struct ui_out
*uiout
, int requested_thread
, int pid
)
770 struct thread_info
*tp
;
772 struct cleanup
*old_chain
;
773 char *extra_info
, *name
, *target_id
;
774 int current_thread
= -1;
776 update_thread_list ();
777 current_ptid
= inferior_ptid
;
779 /* We'll be switching threads temporarily. */
780 old_chain
= make_cleanup_restore_current_thread ();
782 /* For backward compatibility, we make a list for MI. A table is
783 preferable for the CLI, though, because it shows table
785 if (ui_out_is_mi_like_p (uiout
))
786 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
791 for (tp
= thread_list
; tp
; tp
= tp
->next
)
793 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
796 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
799 if (tp
->state_
== THREAD_EXITED
)
807 if (requested_thread
== -1)
808 ui_out_message (uiout
, 0, _("No threads.\n"));
810 ui_out_message (uiout
, 0, _("No thread %d.\n"), requested_thread
);
811 do_cleanups (old_chain
);
815 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
817 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
818 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
819 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
820 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
821 ui_out_table_body (uiout
);
824 for (tp
= thread_list
; tp
; tp
= tp
->next
)
826 struct cleanup
*chain2
;
829 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
832 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
834 if (requested_thread
!= -1)
835 error (_("Requested thread not found in requested process"));
839 if (ptid_equal (tp
->ptid
, current_ptid
))
840 current_thread
= tp
->num
;
842 if (tp
->state_
== THREAD_EXITED
)
845 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
847 if (ui_out_is_mi_like_p (uiout
))
850 if (ptid_equal (tp
->ptid
, current_ptid
))
851 ui_out_text (uiout
, "* ");
853 ui_out_text (uiout
, " ");
857 if (ptid_equal (tp
->ptid
, current_ptid
))
858 ui_out_field_string (uiout
, "current", "*");
860 ui_out_field_skip (uiout
, "current");
863 ui_out_field_int (uiout
, "id", tp
->num
);
865 /* For the CLI, we stuff everything into the target-id field.
866 This is a gross hack to make the output come out looking
867 correct. The underlying problem here is that ui-out has no
868 way to specify that a field's space allocation should be
869 shared by several fields. For MI, we do the right thing
872 target_id
= target_pid_to_str (tp
->ptid
);
873 extra_info
= target_extra_thread_info (tp
);
874 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
876 if (ui_out_is_mi_like_p (uiout
))
878 ui_out_field_string (uiout
, "target-id", target_id
);
880 ui_out_field_string (uiout
, "details", extra_info
);
882 ui_out_field_string (uiout
, "name", name
);
886 struct cleanup
*str_cleanup
;
889 if (extra_info
&& name
)
890 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
893 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
895 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
897 contents
= xstrdup (target_id
);
898 str_cleanup
= make_cleanup (xfree
, contents
);
900 ui_out_field_string (uiout
, "target-id", contents
);
901 do_cleanups (str_cleanup
);
904 if (tp
->state_
== THREAD_RUNNING
)
905 ui_out_text (uiout
, "(running)\n");
908 /* The switch below puts us at the top of the stack (leaf
910 switch_to_thread (tp
->ptid
);
911 print_stack_frame (get_selected_frame (NULL
),
912 /* For MI output, print frame level. */
913 ui_out_is_mi_like_p (uiout
),
917 if (ui_out_is_mi_like_p (uiout
))
919 char *state
= "stopped";
921 if (tp
->state_
== THREAD_RUNNING
)
923 ui_out_field_string (uiout
, "state", state
);
926 core
= target_core_of_thread (tp
->ptid
);
927 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
928 ui_out_field_int (uiout
, "core", core
);
930 do_cleanups (chain2
);
933 /* Restores the current thread and the frame selected before
934 the "info threads" command. */
935 do_cleanups (old_chain
);
937 if (pid
== -1 && requested_thread
== -1)
939 gdb_assert (current_thread
!= -1
941 || ptid_equal (inferior_ptid
, null_ptid
));
942 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
943 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
945 if (current_thread
!= -1 && is_exited (current_ptid
))
946 ui_out_message (uiout
, 0, "\n\
947 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
950 && current_thread
== -1
951 && ptid_equal (current_ptid
, null_ptid
))
952 ui_out_message (uiout
, 0, "\n\
953 No selected thread. See `help thread'.\n");
958 /* Print information about currently known threads
960 * Note: this has the drawback that it _really_ switches
961 * threads, which frees the frame cache. A no-side
962 * effects info-threads command would be nicer.
966 info_threads_command (char *arg
, int from_tty
)
968 print_thread_info (uiout
, -1, -1);
971 /* Switch from one thread to another. */
974 switch_to_thread (ptid_t ptid
)
976 /* Switch the program space as well, if we can infer it from the now
977 current thread. Otherwise, it's up to the caller to select the
979 if (!ptid_equal (ptid
, null_ptid
))
981 struct inferior
*inf
;
983 inf
= find_inferior_pid (ptid_get_pid (ptid
));
984 gdb_assert (inf
!= NULL
);
985 set_current_program_space (inf
->pspace
);
986 set_current_inferior (inf
);
989 if (ptid_equal (ptid
, inferior_ptid
))
992 inferior_ptid
= ptid
;
993 reinit_frame_cache ();
994 registers_changed ();
996 /* We don't check for is_stopped, because we're called at times
997 while in the TARGET_RUNNING state, e.g., while handling an
999 if (!ptid_equal (inferior_ptid
, null_ptid
)
1000 && !is_exited (ptid
)
1001 && !is_executing (ptid
))
1002 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1004 stop_pc
= ~(CORE_ADDR
) 0;
1008 restore_current_thread (ptid_t ptid
)
1010 switch_to_thread (ptid
);
1014 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1016 struct frame_info
*frame
= NULL
;
1019 gdb_assert (frame_level
>= 0);
1021 /* Restore by level first, check if the frame id is the same as
1022 expected. If that fails, try restoring by frame id. If that
1023 fails, nothing to do, just warn the user. */
1025 count
= frame_level
;
1026 frame
= find_relative_frame (get_current_frame (), &count
);
1029 /* The frame ids must match - either both valid or both outer_frame_id.
1030 The latter case is not failsafe, but since it's highly unlikely
1031 the search by level finds the wrong frame, it's 99.9(9)% of
1032 the time (for all practical purposes) safe. */
1033 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1035 /* Cool, all is fine. */
1036 select_frame (frame
);
1040 frame
= frame_find_by_id (a_frame_id
);
1043 /* Cool, refound it. */
1044 select_frame (frame
);
1048 /* Nothing else to do, the frame layout really changed. Select the
1049 innermost stack frame. */
1050 select_frame (get_current_frame ());
1052 /* Warn the user. */
1053 if (frame_level
> 0 && !ui_out_is_mi_like_p (uiout
))
1055 warning (_("Couldn't restore frame #%d in "
1056 "current thread, at reparsed frame #0\n"),
1058 /* For MI, we should probably have a notification about
1059 current frame change. But this error is not very
1060 likely, so don't bother for now. */
1061 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1065 struct current_thread_cleanup
1067 ptid_t inferior_ptid
;
1068 struct frame_id selected_frame_id
;
1069 int selected_frame_level
;
1075 do_restore_current_thread_cleanup (void *arg
)
1077 struct thread_info
*tp
;
1078 struct current_thread_cleanup
*old
= arg
;
1080 tp
= find_thread_ptid (old
->inferior_ptid
);
1082 /* If the previously selected thread belonged to a process that has
1083 in the mean time been deleted (due to normal exit, detach, etc.),
1084 then don't revert back to it, but instead simply drop back to no
1087 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1088 restore_current_thread (old
->inferior_ptid
);
1091 restore_current_thread (null_ptid
);
1092 set_current_inferior (find_inferior_id (old
->inf_id
));
1095 /* The running state of the originally selected thread may have
1096 changed, so we have to recheck it here. */
1097 if (!ptid_equal (inferior_ptid
, null_ptid
)
1099 && is_stopped (inferior_ptid
)
1100 && target_has_registers
1102 && target_has_memory
)
1103 restore_selected_frame (old
->selected_frame_id
,
1104 old
->selected_frame_level
);
1108 restore_current_thread_cleanup_dtor (void *arg
)
1110 struct current_thread_cleanup
*old
= arg
;
1111 struct thread_info
*tp
;
1113 tp
= find_thread_ptid (old
->inferior_ptid
);
1120 make_cleanup_restore_current_thread (void)
1122 struct thread_info
*tp
;
1123 struct frame_info
*frame
;
1124 struct current_thread_cleanup
*old
;
1126 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1127 old
->inferior_ptid
= inferior_ptid
;
1128 old
->inf_id
= current_inferior ()->num
;
1130 if (!ptid_equal (inferior_ptid
, null_ptid
))
1132 old
->was_stopped
= is_stopped (inferior_ptid
);
1133 if (old
->was_stopped
1134 && target_has_registers
1136 && target_has_memory
)
1137 frame
= get_selected_frame (NULL
);
1141 old
->selected_frame_id
= get_frame_id (frame
);
1142 old
->selected_frame_level
= frame_relative_level (frame
);
1144 tp
= find_thread_ptid (inferior_ptid
);
1149 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1150 restore_current_thread_cleanup_dtor
);
1153 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1154 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1155 of two numbers seperated by a hyphen. Examples:
1157 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1158 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1159 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1162 thread_apply_all_command (char *cmd
, int from_tty
)
1164 struct thread_info
*tp
;
1165 struct cleanup
*old_chain
;
1168 if (cmd
== NULL
|| *cmd
== '\000')
1169 error (_("Please specify a command following the thread ID list"));
1171 update_thread_list ();
1173 old_chain
= make_cleanup_restore_current_thread ();
1175 /* Save a copy of the command in case it is clobbered by
1177 saved_cmd
= xstrdup (cmd
);
1178 make_cleanup (xfree
, saved_cmd
);
1179 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1180 if (thread_alive (tp
))
1182 switch_to_thread (tp
->ptid
);
1184 printf_filtered (_("\nThread %d (%s):\n"),
1185 tp
->num
, target_pid_to_str (inferior_ptid
));
1186 execute_command (cmd
, from_tty
);
1187 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1191 do_cleanups (old_chain
);
1195 thread_apply_command (char *tidlist
, int from_tty
)
1199 struct cleanup
*old_chain
;
1202 if (tidlist
== NULL
|| *tidlist
== '\000')
1203 error (_("Please specify a thread ID list"));
1205 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1208 error (_("Please specify a command following the thread ID list"));
1210 /* Save a copy of the command in case it is clobbered by
1212 saved_cmd
= xstrdup (cmd
);
1213 old_chain
= make_cleanup (xfree
, saved_cmd
);
1214 while (tidlist
< cmd
)
1216 struct thread_info
*tp
;
1219 start
= strtol (tidlist
, &p
, 10);
1221 error (_("Error parsing %s"), tidlist
);
1224 while (*tidlist
== ' ' || *tidlist
== '\t')
1227 if (*tidlist
== '-') /* Got a range of IDs? */
1229 tidlist
++; /* Skip the - */
1230 end
= strtol (tidlist
, &p
, 10);
1232 error (_("Error parsing %s"), tidlist
);
1235 while (*tidlist
== ' ' || *tidlist
== '\t')
1241 make_cleanup_restore_current_thread ();
1243 for (; start
<= end
; start
++)
1245 tp
= find_thread_id (start
);
1248 warning (_("Unknown thread %d."), start
);
1249 else if (!thread_alive (tp
))
1250 warning (_("Thread %d has terminated."), start
);
1253 switch_to_thread (tp
->ptid
);
1255 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1256 target_pid_to_str (inferior_ptid
));
1257 execute_command (cmd
, from_tty
);
1259 /* Restore exact command used previously. */
1260 strcpy (cmd
, saved_cmd
);
1265 do_cleanups (old_chain
);
1268 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1269 if prefix of arg is `apply'. */
1272 thread_command (char *tidstr
, int from_tty
)
1276 if (ptid_equal (inferior_ptid
, null_ptid
))
1277 error (_("No thread selected"));
1279 if (target_has_stack
)
1281 if (is_exited (inferior_ptid
))
1282 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1283 pid_to_thread_id (inferior_ptid
),
1284 target_pid_to_str (inferior_ptid
));
1286 printf_filtered (_("[Current thread is %d (%s)]\n"),
1287 pid_to_thread_id (inferior_ptid
),
1288 target_pid_to_str (inferior_ptid
));
1291 error (_("No stack."));
1295 gdb_thread_select (uiout
, tidstr
, NULL
);
1298 /* Implementation of `thread name'. */
1301 thread_name_command (char *arg
, int from_tty
)
1303 struct thread_info
*info
;
1305 if (ptid_equal (inferior_ptid
, null_ptid
))
1306 error (_("No thread selected"));
1308 while (arg
&& isspace (*arg
))
1311 info
= inferior_thread ();
1313 info
->name
= arg
? xstrdup (arg
) : NULL
;
1316 /* Print notices when new threads are attached and detached. */
1317 int print_thread_events
= 1;
1319 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1320 struct cmd_list_element
*c
, const char *value
)
1322 fprintf_filtered (file
,
1323 _("Printing of thread events is %s.\n"),
1328 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1331 struct thread_info
*tp
;
1333 num
= value_as_long (parse_and_eval (tidstr
));
1335 tp
= find_thread_id (num
);
1338 error (_("Thread ID %d not known."), num
);
1340 if (!thread_alive (tp
))
1341 error (_("Thread ID %d has terminated."), num
);
1343 switch_to_thread (tp
->ptid
);
1345 annotate_thread_changed ();
1347 ui_out_text (uiout
, "[Switching to thread ");
1348 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1349 ui_out_text (uiout
, " (");
1350 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1351 ui_out_text (uiout
, ")]");
1353 /* Note that we can't reach this with an exited thread, due to the
1354 thread_alive check above. */
1355 if (tp
->state_
== THREAD_RUNNING
)
1356 ui_out_text (uiout
, "(running)\n");
1359 ui_out_text (uiout
, "\n");
1360 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1363 /* Since the current thread may have changed, see if there is any
1364 exited thread we can now delete. */
1371 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1373 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1374 error_message
, RETURN_MASK_ALL
) < 0)
1380 update_thread_list (void)
1383 target_find_new_threads ();
1386 /* Return a new value for the selected thread's id. Return a value of 0 if
1387 no thread is selected, or no threads exist. */
1389 static struct value
*
1390 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1392 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1394 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1395 (tp
? tp
->num
: 0));
1398 /* Commands with a prefix of `thread'. */
1399 struct cmd_list_element
*thread_cmd_list
= NULL
;
1402 _initialize_thread (void)
1404 static struct cmd_list_element
*thread_apply_list
= NULL
;
1406 add_info ("threads", info_threads_command
,
1407 _("IDs of currently known threads."));
1409 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1410 Use this command to switch between threads.\n\
1411 The new thread ID must be currently known."),
1412 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1414 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1415 _("Apply a command to a list of threads."),
1416 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1418 add_cmd ("all", class_run
, thread_apply_all_command
,
1419 _("Apply a command to all threads."), &thread_apply_list
);
1421 add_cmd ("name", class_run
, thread_name_command
,
1422 _("Set the current thread's name.\n\
1423 Usage: thread name [NAME]\n\
1424 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1427 add_com_alias ("t", "thread", class_run
, 1);
1429 add_setshow_boolean_cmd ("thread-events", no_class
,
1430 &print_thread_events
, _("\
1431 Set printing of thread events (such as thread start and exit)."), _("\
1432 Show printing of thread events (such as thread start and exit)."), NULL
,
1434 show_print_thread_events
,
1435 &setprintlist
, &showprintlist
);
1437 create_internalvar_type_lazy ("_thread", thread_id_make_value
);