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"
46 #include "gdb_regex.h"
47 #include "cli/cli-utils.h"
48 #include "continuations.h"
50 /* Definition of struct thread_info exported to gdbthread.h. */
52 /* Prototypes for exported functions. */
54 void _initialize_thread (void);
56 /* Prototypes for local functions. */
58 static struct thread_info
*thread_list
= NULL
;
59 static int highest_thread_num
;
61 static void thread_command (char *tidstr
, int from_tty
);
62 static void thread_apply_all_command (char *, int);
63 static int thread_alive (struct thread_info
*);
64 static void info_threads_command (char *, int);
65 static void thread_apply_command (char *, int);
66 static void restore_current_thread (ptid_t
);
67 static void prune_threads (void);
70 inferior_thread (void)
72 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
78 delete_step_resume_breakpoint (struct thread_info
*tp
)
80 if (tp
&& tp
->control
.step_resume_breakpoint
)
82 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
83 tp
->control
.step_resume_breakpoint
= NULL
;
88 delete_exception_resume_breakpoint (struct thread_info
*tp
)
90 if (tp
&& tp
->control
.exception_resume_breakpoint
)
92 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
93 tp
->control
.exception_resume_breakpoint
= NULL
;
98 clear_thread_inferior_resources (struct thread_info
*tp
)
100 /* NOTE: this will take care of any left-over step_resume breakpoints,
101 but not any user-specified thread-specific breakpoints. We can not
102 delete the breakpoint straight-off, because the inferior might not
103 be stopped at the moment. */
104 if (tp
->control
.step_resume_breakpoint
)
106 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
107 tp
->control
.step_resume_breakpoint
= NULL
;
110 if (tp
->control
.exception_resume_breakpoint
)
112 tp
->control
.exception_resume_breakpoint
->disposition
113 = disp_del_at_next_stop
;
114 tp
->control
.exception_resume_breakpoint
= NULL
;
117 bpstat_clear (&tp
->control
.stop_bpstat
);
119 do_all_intermediate_continuations_thread (tp
, 1);
120 do_all_continuations_thread (tp
, 1);
122 delete_longjmp_breakpoint (tp
->num
);
126 free_thread (struct thread_info
*tp
)
130 if (tp
->private_dtor
)
131 tp
->private_dtor (tp
->private);
141 init_thread_list (void)
143 struct thread_info
*tp
, *tpnext
;
145 highest_thread_num
= 0;
150 for (tp
= thread_list
; tp
; tp
= tpnext
)
159 /* Allocate a new thread with target id PTID and add it to the thread
162 static struct thread_info
*
163 new_thread (ptid_t ptid
)
165 struct thread_info
*tp
;
167 tp
= xcalloc (1, sizeof (*tp
));
170 tp
->num
= ++highest_thread_num
;
171 tp
->next
= thread_list
;
174 /* Nothing to follow yet. */
175 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
176 tp
->state
= THREAD_STOPPED
;
182 add_thread_silent (ptid_t ptid
)
184 struct thread_info
*tp
;
186 tp
= find_thread_ptid (ptid
);
188 /* Found an old thread with the same id. It has to be dead,
189 otherwise we wouldn't be adding a new thread with the same id.
190 The OS is reusing this id --- delete it, and recreate a new
193 /* In addition to deleting the thread, if this is the current
194 thread, then we need to take care that delete_thread doesn't
195 really delete the thread if it is inferior_ptid. Create a
196 new template thread in the list with an invalid ptid, switch
197 to it, delete the original thread, reset the new thread's
198 ptid, and switch to it. */
200 if (ptid_equal (inferior_ptid
, ptid
))
202 tp
= new_thread (null_ptid
);
204 /* Make switch_to_thread not read from the thread. */
205 tp
->state
= THREAD_EXITED
;
206 switch_to_thread (null_ptid
);
208 /* Now we can delete it. */
209 delete_thread (ptid
);
211 /* Now reset its ptid, and reswitch inferior_ptid to it. */
213 tp
->state
= THREAD_STOPPED
;
214 switch_to_thread (ptid
);
216 observer_notify_new_thread (tp
);
222 /* Just go ahead and delete it. */
223 delete_thread (ptid
);
226 tp
= new_thread (ptid
);
227 observer_notify_new_thread (tp
);
233 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
235 struct thread_info
*result
= add_thread_silent (ptid
);
237 result
->private = private;
239 if (print_thread_events
)
240 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
242 annotate_new_thread ();
247 add_thread (ptid_t ptid
)
249 return add_thread_with_info (ptid
, NULL
);
252 /* Delete thread PTID. If SILENT, don't notify the observer of this
255 delete_thread_1 (ptid_t ptid
, int silent
)
257 struct thread_info
*tp
, *tpprev
;
261 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
262 if (ptid_equal (tp
->ptid
, ptid
))
268 /* If this is the current thread, or there's code out there that
269 relies on it existing (refcount > 0) we can't delete yet. Mark
270 it as exited, and notify it. */
272 || ptid_equal (tp
->ptid
, inferior_ptid
))
274 if (tp
->state
!= THREAD_EXITED
)
276 observer_notify_thread_exit (tp
, silent
);
278 /* Tag it as exited. */
279 tp
->state
= THREAD_EXITED
;
281 /* Clear breakpoints, etc. associated with this thread. */
282 clear_thread_inferior_resources (tp
);
285 /* Will be really deleted some other time. */
289 /* Notify thread exit, but only if we haven't already. */
290 if (tp
->state
!= THREAD_EXITED
)
291 observer_notify_thread_exit (tp
, silent
);
293 /* Tag it as exited. */
294 tp
->state
= THREAD_EXITED
;
295 clear_thread_inferior_resources (tp
);
298 tpprev
->next
= tp
->next
;
300 thread_list
= tp
->next
;
305 /* Delete thread PTID and notify of thread exit. If this is
306 inferior_ptid, don't actually delete it, but tag it as exited and
307 do the notification. If PTID is the user selected thread, clear
310 delete_thread (ptid_t ptid
)
312 delete_thread_1 (ptid
, 0 /* not silent */);
316 delete_thread_silent (ptid_t ptid
)
318 delete_thread_1 (ptid
, 1 /* silent */);
322 find_thread_id (int num
)
324 struct thread_info
*tp
;
326 for (tp
= thread_list
; tp
; tp
= tp
->next
)
333 /* Find a thread_info by matching PTID. */
335 find_thread_ptid (ptid_t ptid
)
337 struct thread_info
*tp
;
339 for (tp
= thread_list
; tp
; tp
= tp
->next
)
340 if (ptid_equal (tp
->ptid
, ptid
))
347 * Thread iterator function.
349 * Calls a callback function once for each thread, so long as
350 * the callback function returns false. If the callback function
351 * returns true, the iteration will end and the current thread
352 * will be returned. This can be useful for implementing a
353 * search for a thread with arbitrary attributes, or for applying
354 * some operation to every thread.
356 * FIXME: some of the existing functionality, such as
357 * "Thread apply all", might be rewritten using this functionality.
361 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
364 struct thread_info
*tp
, *next
;
366 for (tp
= thread_list
; tp
; tp
= next
)
369 if ((*callback
) (tp
, data
))
380 struct thread_info
*tp
;
382 for (tp
= thread_list
; tp
; tp
= tp
->next
)
389 valid_thread_id (int num
)
391 struct thread_info
*tp
;
393 for (tp
= thread_list
; tp
; tp
= tp
->next
)
401 pid_to_thread_id (ptid_t ptid
)
403 struct thread_info
*tp
;
405 for (tp
= thread_list
; tp
; tp
= tp
->next
)
406 if (ptid_equal (tp
->ptid
, ptid
))
413 thread_id_to_pid (int num
)
415 struct thread_info
*thread
= find_thread_id (num
);
420 return pid_to_ptid (-1);
424 in_thread_list (ptid_t ptid
)
426 struct thread_info
*tp
;
428 for (tp
= thread_list
; tp
; tp
= tp
->next
)
429 if (ptid_equal (tp
->ptid
, ptid
))
432 return 0; /* Never heard of 'im. */
435 /* Finds the first thread of the inferior given by PID. If PID is -1,
436 return the first thread in the list. */
439 first_thread_of_process (int pid
)
441 struct thread_info
*tp
, *ret
= NULL
;
443 for (tp
= thread_list
; tp
; tp
= tp
->next
)
444 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
445 if (ret
== NULL
|| tp
->num
< ret
->num
)
452 any_thread_of_process (int pid
)
454 struct thread_info
*tp
;
456 for (tp
= thread_list
; tp
; tp
= tp
->next
)
457 if (ptid_get_pid (tp
->ptid
) == pid
)
464 any_live_thread_of_process (int pid
)
466 struct thread_info
*tp
;
467 struct thread_info
*tp_executing
= NULL
;
469 for (tp
= thread_list
; tp
; tp
= tp
->next
)
470 if (tp
->state
!= THREAD_EXITED
&& ptid_get_pid (tp
->ptid
) == pid
)
481 /* Print a list of thread ids currently known, and the total number of
482 threads. To be used from within catch_errors. */
484 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
486 struct thread_info
*tp
;
488 struct cleanup
*cleanup_chain
;
489 int current_thread
= -1;
491 update_thread_list ();
493 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
495 for (tp
= thread_list
; tp
; tp
= tp
->next
)
497 if (tp
->state
== THREAD_EXITED
)
500 if (ptid_equal (tp
->ptid
, inferior_ptid
))
501 current_thread
= tp
->num
;
504 ui_out_field_int (uiout
, "thread-id", tp
->num
);
507 do_cleanups (cleanup_chain
);
509 if (current_thread
!= -1)
510 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
511 ui_out_field_int (uiout
, "number-of-threads", num
);
515 /* Official gdblib interface function to get a list of thread ids and
518 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
520 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
521 error_message
, RETURN_MASK_ALL
) < 0)
526 /* Return true if TP is an active thread. */
528 thread_alive (struct thread_info
*tp
)
530 if (tp
->state
== THREAD_EXITED
)
532 if (!target_thread_alive (tp
->ptid
))
540 struct thread_info
*tp
, *next
;
542 for (tp
= thread_list
; tp
; tp
= next
)
545 if (!thread_alive (tp
))
546 delete_thread (tp
->ptid
);
551 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
553 struct inferior
*inf
;
554 struct thread_info
*tp
;
556 /* It can happen that what we knew as the target inferior id
557 changes. E.g, target remote may only discover the remote process
558 pid after adding the inferior to GDB's list. */
559 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
560 inf
->pid
= ptid_get_pid (new_ptid
);
562 tp
= find_thread_ptid (old_ptid
);
565 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
569 set_running (ptid_t ptid
, int running
)
571 struct thread_info
*tp
;
572 int all
= ptid_equal (ptid
, minus_one_ptid
);
574 /* We try not to notify the observer if no thread has actually changed
575 the running state -- merely to reduce the number of messages to
576 frontend. Frontend is supposed to handle multiple *running just fine. */
577 if (all
|| ptid_is_pid (ptid
))
581 for (tp
= thread_list
; tp
; tp
= tp
->next
)
582 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
584 if (tp
->state
== THREAD_EXITED
)
586 if (running
&& tp
->state
== THREAD_STOPPED
)
588 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
591 observer_notify_target_resumed (ptid
);
597 tp
= find_thread_ptid (ptid
);
599 gdb_assert (tp
->state
!= THREAD_EXITED
);
600 if (running
&& tp
->state
== THREAD_STOPPED
)
602 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
604 observer_notify_target_resumed (ptid
);
609 is_thread_state (ptid_t ptid
, enum thread_state state
)
611 struct thread_info
*tp
;
613 tp
= find_thread_ptid (ptid
);
615 return tp
->state
== state
;
619 is_stopped (ptid_t ptid
)
621 return is_thread_state (ptid
, THREAD_STOPPED
);
625 is_exited (ptid_t ptid
)
627 return is_thread_state (ptid
, THREAD_EXITED
);
631 is_running (ptid_t ptid
)
633 return is_thread_state (ptid
, THREAD_RUNNING
);
639 struct thread_info
*tp
;
641 for (tp
= thread_list
; tp
; tp
= tp
->next
)
642 if (tp
->state
== THREAD_RUNNING
)
649 is_executing (ptid_t ptid
)
651 struct thread_info
*tp
;
653 tp
= find_thread_ptid (ptid
);
655 return tp
->executing
;
659 set_executing (ptid_t ptid
, int executing
)
661 struct thread_info
*tp
;
662 int all
= ptid_equal (ptid
, minus_one_ptid
);
664 if (all
|| ptid_is_pid (ptid
))
666 for (tp
= thread_list
; tp
; tp
= tp
->next
)
667 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
668 tp
->executing
= executing
;
672 tp
= find_thread_ptid (ptid
);
674 tp
->executing
= executing
;
679 set_stop_requested (ptid_t ptid
, int stop
)
681 struct thread_info
*tp
;
682 int all
= ptid_equal (ptid
, minus_one_ptid
);
684 if (all
|| ptid_is_pid (ptid
))
686 for (tp
= thread_list
; tp
; tp
= tp
->next
)
687 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
688 tp
->stop_requested
= stop
;
692 tp
= find_thread_ptid (ptid
);
694 tp
->stop_requested
= stop
;
697 /* Call the stop requested observer so other components of GDB can
698 react to this request. */
700 observer_notify_thread_stop_requested (ptid
);
704 finish_thread_state (ptid_t ptid
)
706 struct thread_info
*tp
;
710 all
= ptid_equal (ptid
, minus_one_ptid
);
712 if (all
|| ptid_is_pid (ptid
))
714 for (tp
= thread_list
; tp
; tp
= tp
->next
)
716 if (tp
->state
== THREAD_EXITED
)
718 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
720 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
722 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
728 tp
= find_thread_ptid (ptid
);
730 if (tp
->state
!= THREAD_EXITED
)
732 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
734 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
739 observer_notify_target_resumed (ptid
);
743 finish_thread_state_cleanup (void *arg
)
745 ptid_t
*ptid_p
= arg
;
749 finish_thread_state (*ptid_p
);
752 /* Prints the list of threads and their details on UIOUT.
753 This is a version of 'info_threads_command' suitable for
755 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
756 that should be printed. Otherwise, all threads are
758 If PID is not -1, only print threads from the process PID.
759 Otherwise, threads from all attached PIDs are printed.
760 If both REQUESTED_THREAD and PID are not -1, then the thread
761 is printed if it belongs to the specified process. Otherwise,
762 an error is raised. */
764 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
766 struct thread_info
*tp
;
768 struct cleanup
*old_chain
;
769 char *extra_info
, *name
, *target_id
;
770 int current_thread
= -1;
772 update_thread_list ();
773 current_ptid
= inferior_ptid
;
775 /* We'll be switching threads temporarily. */
776 old_chain
= make_cleanup_restore_current_thread ();
778 /* For backward compatibility, we make a list for MI. A table is
779 preferable for the CLI, though, because it shows table
781 if (ui_out_is_mi_like_p (uiout
))
782 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
787 for (tp
= thread_list
; tp
; tp
= tp
->next
)
789 if (!number_is_in_list (requested_threads
, tp
->num
))
792 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
795 if (tp
->state
== THREAD_EXITED
)
803 if (requested_threads
== NULL
|| *requested_threads
== '\0')
804 ui_out_message (uiout
, 0, _("No threads.\n"));
806 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
808 do_cleanups (old_chain
);
812 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
814 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
815 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
816 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
817 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
818 ui_out_table_body (uiout
);
821 for (tp
= thread_list
; tp
; tp
= tp
->next
)
823 struct cleanup
*chain2
;
826 if (!number_is_in_list (requested_threads
, tp
->num
))
829 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
831 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
832 error (_("Requested thread not found in requested process"));
836 if (ptid_equal (tp
->ptid
, current_ptid
))
837 current_thread
= tp
->num
;
839 if (tp
->state
== THREAD_EXITED
)
842 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
844 if (ui_out_is_mi_like_p (uiout
))
847 if (ptid_equal (tp
->ptid
, current_ptid
))
848 ui_out_text (uiout
, "* ");
850 ui_out_text (uiout
, " ");
854 if (ptid_equal (tp
->ptid
, current_ptid
))
855 ui_out_field_string (uiout
, "current", "*");
857 ui_out_field_skip (uiout
, "current");
860 ui_out_field_int (uiout
, "id", tp
->num
);
862 /* For the CLI, we stuff everything into the target-id field.
863 This is a gross hack to make the output come out looking
864 correct. The underlying problem here is that ui-out has no
865 way to specify that a field's space allocation should be
866 shared by several fields. For MI, we do the right thing
869 target_id
= target_pid_to_str (tp
->ptid
);
870 extra_info
= target_extra_thread_info (tp
);
871 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
873 if (ui_out_is_mi_like_p (uiout
))
875 ui_out_field_string (uiout
, "target-id", target_id
);
877 ui_out_field_string (uiout
, "details", extra_info
);
879 ui_out_field_string (uiout
, "name", name
);
883 struct cleanup
*str_cleanup
;
886 if (extra_info
&& name
)
887 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
890 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
892 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
894 contents
= xstrdup (target_id
);
895 str_cleanup
= make_cleanup (xfree
, contents
);
897 ui_out_field_string (uiout
, "target-id", contents
);
898 do_cleanups (str_cleanup
);
901 if (tp
->state
== THREAD_RUNNING
)
902 ui_out_text (uiout
, "(running)\n");
905 /* The switch below puts us at the top of the stack (leaf
907 switch_to_thread (tp
->ptid
);
908 print_stack_frame (get_selected_frame (NULL
),
909 /* For MI output, print frame level. */
910 ui_out_is_mi_like_p (uiout
),
914 if (ui_out_is_mi_like_p (uiout
))
916 char *state
= "stopped";
918 if (tp
->state
== THREAD_RUNNING
)
920 ui_out_field_string (uiout
, "state", state
);
923 core
= target_core_of_thread (tp
->ptid
);
924 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
925 ui_out_field_int (uiout
, "core", core
);
927 do_cleanups (chain2
);
930 /* Restores the current thread and the frame selected before
931 the "info threads" command. */
932 do_cleanups (old_chain
);
934 if (pid
== -1 && requested_threads
== NULL
)
936 gdb_assert (current_thread
!= -1
938 || ptid_equal (inferior_ptid
, null_ptid
));
939 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
940 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
942 if (current_thread
!= -1 && is_exited (current_ptid
))
943 ui_out_message (uiout
, 0, "\n\
944 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
947 && current_thread
== -1
948 && ptid_equal (current_ptid
, null_ptid
))
949 ui_out_message (uiout
, 0, "\n\
950 No selected thread. See `help thread'.\n");
954 /* Print information about currently known threads
956 Optional ARG is a thread id, or list of thread ids.
958 Note: this has the drawback that it _really_ switches
959 threads, which frees the frame cache. A no-side
960 effects info-threads command would be nicer. */
963 info_threads_command (char *arg
, int from_tty
)
965 print_thread_info (current_uiout
, arg
, -1);
968 /* Switch from one thread to another. */
971 switch_to_thread (ptid_t ptid
)
973 /* Switch the program space as well, if we can infer it from the now
974 current thread. Otherwise, it's up to the caller to select the
976 if (!ptid_equal (ptid
, null_ptid
))
978 struct inferior
*inf
;
980 inf
= find_inferior_pid (ptid_get_pid (ptid
));
981 gdb_assert (inf
!= NULL
);
982 set_current_program_space (inf
->pspace
);
983 set_current_inferior (inf
);
986 if (ptid_equal (ptid
, inferior_ptid
))
989 inferior_ptid
= ptid
;
990 reinit_frame_cache ();
991 registers_changed ();
993 /* We don't check for is_stopped, because we're called at times
994 while in the TARGET_RUNNING state, e.g., while handling an
996 if (!ptid_equal (inferior_ptid
, null_ptid
)
998 && !is_executing (ptid
))
999 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1001 stop_pc
= ~(CORE_ADDR
) 0;
1005 restore_current_thread (ptid_t ptid
)
1007 switch_to_thread (ptid
);
1011 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1013 struct frame_info
*frame
= NULL
;
1016 /* This means there was no selected frame. */
1017 if (frame_level
== -1)
1019 select_frame (NULL
);
1023 gdb_assert (frame_level
>= 0);
1025 /* Restore by level first, check if the frame id is the same as
1026 expected. If that fails, try restoring by frame id. If that
1027 fails, nothing to do, just warn the user. */
1029 count
= frame_level
;
1030 frame
= find_relative_frame (get_current_frame (), &count
);
1033 /* The frame ids must match - either both valid or both outer_frame_id.
1034 The latter case is not failsafe, but since it's highly unlikely
1035 the search by level finds the wrong frame, it's 99.9(9)% of
1036 the time (for all practical purposes) safe. */
1037 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1039 /* Cool, all is fine. */
1040 select_frame (frame
);
1044 frame
= frame_find_by_id (a_frame_id
);
1047 /* Cool, refound it. */
1048 select_frame (frame
);
1052 /* Nothing else to do, the frame layout really changed. Select the
1053 innermost stack frame. */
1054 select_frame (get_current_frame ());
1056 /* Warn the user. */
1057 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1059 warning (_("Couldn't restore frame #%d in "
1060 "current thread, at reparsed frame #0\n"),
1062 /* For MI, we should probably have a notification about
1063 current frame change. But this error is not very
1064 likely, so don't bother for now. */
1065 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1069 struct current_thread_cleanup
1071 ptid_t inferior_ptid
;
1072 struct frame_id selected_frame_id
;
1073 int selected_frame_level
;
1079 do_restore_current_thread_cleanup (void *arg
)
1081 struct thread_info
*tp
;
1082 struct current_thread_cleanup
*old
= arg
;
1084 tp
= find_thread_ptid (old
->inferior_ptid
);
1086 /* If the previously selected thread belonged to a process that has
1087 in the mean time been deleted (due to normal exit, detach, etc.),
1088 then don't revert back to it, but instead simply drop back to no
1091 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1092 restore_current_thread (old
->inferior_ptid
);
1095 restore_current_thread (null_ptid
);
1096 set_current_inferior (find_inferior_id (old
->inf_id
));
1099 /* The running state of the originally selected thread may have
1100 changed, so we have to recheck it here. */
1101 if (!ptid_equal (inferior_ptid
, null_ptid
)
1103 && is_stopped (inferior_ptid
)
1104 && target_has_registers
1106 && target_has_memory
)
1107 restore_selected_frame (old
->selected_frame_id
,
1108 old
->selected_frame_level
);
1112 restore_current_thread_cleanup_dtor (void *arg
)
1114 struct current_thread_cleanup
*old
= arg
;
1115 struct thread_info
*tp
;
1117 tp
= find_thread_ptid (old
->inferior_ptid
);
1124 make_cleanup_restore_current_thread (void)
1126 struct thread_info
*tp
;
1127 struct frame_info
*frame
;
1128 struct current_thread_cleanup
*old
;
1130 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1131 old
->inferior_ptid
= inferior_ptid
;
1132 old
->inf_id
= current_inferior ()->num
;
1134 if (!ptid_equal (inferior_ptid
, null_ptid
))
1136 old
->was_stopped
= is_stopped (inferior_ptid
);
1137 if (old
->was_stopped
1138 && target_has_registers
1140 && target_has_memory
)
1142 /* When processing internal events, there might not be a
1143 selected frame. If we naively call get_selected_frame
1144 here, then we can end up reading debuginfo for the
1145 current frame, but we don't generally need the debuginfo
1147 frame
= get_selected_frame_if_set ();
1152 old
->selected_frame_id
= get_frame_id (frame
);
1153 old
->selected_frame_level
= frame_relative_level (frame
);
1155 tp
= find_thread_ptid (inferior_ptid
);
1160 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1161 restore_current_thread_cleanup_dtor
);
1164 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1165 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1166 of two numbers seperated by a hyphen. Examples:
1168 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1169 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1170 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1173 thread_apply_all_command (char *cmd
, int from_tty
)
1175 struct thread_info
*tp
;
1176 struct cleanup
*old_chain
;
1179 if (cmd
== NULL
|| *cmd
== '\000')
1180 error (_("Please specify a command following the thread ID list"));
1182 update_thread_list ();
1184 old_chain
= make_cleanup_restore_current_thread ();
1186 /* Save a copy of the command in case it is clobbered by
1188 saved_cmd
= xstrdup (cmd
);
1189 make_cleanup (xfree
, saved_cmd
);
1190 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1191 if (thread_alive (tp
))
1193 switch_to_thread (tp
->ptid
);
1195 printf_filtered (_("\nThread %d (%s):\n"),
1196 tp
->num
, target_pid_to_str (inferior_ptid
));
1197 execute_command (cmd
, from_tty
);
1198 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1202 do_cleanups (old_chain
);
1206 thread_apply_command (char *tidlist
, int from_tty
)
1209 struct cleanup
*old_chain
;
1211 struct get_number_or_range_state state
;
1213 if (tidlist
== NULL
|| *tidlist
== '\000')
1214 error (_("Please specify a thread ID list"));
1216 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1219 error (_("Please specify a command following the thread ID list"));
1221 /* Save a copy of the command in case it is clobbered by
1223 saved_cmd
= xstrdup (cmd
);
1224 old_chain
= make_cleanup (xfree
, saved_cmd
);
1226 init_number_or_range (&state
, tidlist
);
1227 while (!state
.finished
&& state
.string
< cmd
)
1229 struct thread_info
*tp
;
1233 start
= get_number_or_range (&state
);
1235 make_cleanup_restore_current_thread ();
1237 tp
= find_thread_id (start
);
1240 warning (_("Unknown thread %d."), start
);
1241 else if (!thread_alive (tp
))
1242 warning (_("Thread %d has terminated."), start
);
1245 switch_to_thread (tp
->ptid
);
1247 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1248 target_pid_to_str (inferior_ptid
));
1249 execute_command (cmd
, from_tty
);
1251 /* Restore exact command used previously. */
1252 strcpy (cmd
, saved_cmd
);
1256 do_cleanups (old_chain
);
1259 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1260 if prefix of arg is `apply'. */
1263 thread_command (char *tidstr
, int from_tty
)
1267 if (ptid_equal (inferior_ptid
, null_ptid
))
1268 error (_("No thread selected"));
1270 if (target_has_stack
)
1272 if (is_exited (inferior_ptid
))
1273 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1274 pid_to_thread_id (inferior_ptid
),
1275 target_pid_to_str (inferior_ptid
));
1277 printf_filtered (_("[Current thread is %d (%s)]\n"),
1278 pid_to_thread_id (inferior_ptid
),
1279 target_pid_to_str (inferior_ptid
));
1282 error (_("No stack."));
1286 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1289 /* Implementation of `thread name'. */
1292 thread_name_command (char *arg
, int from_tty
)
1294 struct thread_info
*info
;
1296 if (ptid_equal (inferior_ptid
, null_ptid
))
1297 error (_("No thread selected"));
1299 while (arg
&& isspace (*arg
))
1302 info
= inferior_thread ();
1304 info
->name
= arg
? xstrdup (arg
) : NULL
;
1307 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1310 thread_find_command (char *arg
, int from_tty
)
1312 struct thread_info
*tp
;
1314 unsigned long match
= 0;
1316 if (arg
== NULL
|| *arg
== '\0')
1317 error (_("Command requires an argument."));
1319 tmp
= re_comp (arg
);
1321 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1323 update_thread_list ();
1324 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1326 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1328 printf_filtered (_("Thread %d has name '%s'\n"),
1333 tmp
= target_thread_name (tp
);
1334 if (tmp
!= NULL
&& re_exec (tmp
))
1336 printf_filtered (_("Thread %d has target name '%s'\n"),
1341 tmp
= target_pid_to_str (tp
->ptid
);
1342 if (tmp
!= NULL
&& re_exec (tmp
))
1344 printf_filtered (_("Thread %d has target id '%s'\n"),
1349 tmp
= target_extra_thread_info (tp
);
1350 if (tmp
!= NULL
&& re_exec (tmp
))
1352 printf_filtered (_("Thread %d has extra info '%s'\n"),
1358 printf_filtered (_("No threads match '%s'\n"), arg
);
1361 /* Print notices when new threads are attached and detached. */
1362 int print_thread_events
= 1;
1364 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1365 struct cmd_list_element
*c
, const char *value
)
1367 fprintf_filtered (file
,
1368 _("Printing of thread events is %s.\n"),
1373 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1376 struct thread_info
*tp
;
1378 num
= value_as_long (parse_and_eval (tidstr
));
1380 tp
= find_thread_id (num
);
1383 error (_("Thread ID %d not known."), num
);
1385 if (!thread_alive (tp
))
1386 error (_("Thread ID %d has terminated."), num
);
1388 switch_to_thread (tp
->ptid
);
1390 annotate_thread_changed ();
1392 ui_out_text (uiout
, "[Switching to thread ");
1393 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1394 ui_out_text (uiout
, " (");
1395 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1396 ui_out_text (uiout
, ")]");
1398 /* Note that we can't reach this with an exited thread, due to the
1399 thread_alive check above. */
1400 if (tp
->state
== THREAD_RUNNING
)
1401 ui_out_text (uiout
, "(running)\n");
1404 ui_out_text (uiout
, "\n");
1405 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1408 /* Since the current thread may have changed, see if there is any
1409 exited thread we can now delete. */
1416 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1418 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1419 error_message
, RETURN_MASK_ALL
) < 0)
1425 update_thread_list (void)
1428 target_find_new_threads ();
1431 /* Return a new value for the selected thread's id. Return a value of 0 if
1432 no thread is selected, or no threads exist. */
1434 static struct value
*
1435 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1437 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1439 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1440 (tp
? tp
->num
: 0));
1443 /* Commands with a prefix of `thread'. */
1444 struct cmd_list_element
*thread_cmd_list
= NULL
;
1447 _initialize_thread (void)
1449 static struct cmd_list_element
*thread_apply_list
= NULL
;
1451 add_info ("threads", info_threads_command
,
1452 _("Display currently known threads.\n\
1453 Usage: info threads [ID]...\n\
1454 Optional arguments are thread IDs with spaces between.\n\
1455 If no arguments, all threads are displayed."));
1457 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1458 Use this command to switch between threads.\n\
1459 The new thread ID must be currently known."),
1460 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1462 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1463 _("Apply a command to a list of threads."),
1464 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1466 add_cmd ("all", class_run
, thread_apply_all_command
,
1467 _("Apply a command to all threads."), &thread_apply_list
);
1469 add_cmd ("name", class_run
, thread_name_command
,
1470 _("Set the current thread's name.\n\
1471 Usage: thread name [NAME]\n\
1472 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1474 add_cmd ("find", class_run
, thread_find_command
, _("\
1475 Find threads that match a regular expression.\n\
1476 Usage: thread find REGEXP\n\
1477 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1481 add_com_alias ("t", "thread", class_run
, 1);
1483 add_setshow_boolean_cmd ("thread-events", no_class
,
1484 &print_thread_events
, _("\
1485 Set printing of thread events (such as thread start and exit)."), _("\
1486 Show printing of thread events (such as thread start and exit)."), NULL
,
1488 show_print_thread_events
,
1489 &setprintlist
, &showprintlist
);
1491 create_internalvar_type_lazy ("_thread", thread_id_make_value
);