1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 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"
30 #include "exceptions.h"
35 #include "gdb_string.h"
38 #include <sys/types.h>
43 #include "cli/cli-decode.h"
44 #include "gdb_regex.h"
45 #include "cli/cli-utils.h"
46 #include "continuations.h"
48 /* Definition of struct thread_info exported to gdbthread.h. */
50 /* Prototypes for exported functions. */
52 void _initialize_thread (void);
54 /* Prototypes for local functions. */
56 struct thread_info
*thread_list
= NULL
;
57 static int highest_thread_num
;
59 static void thread_command (char *tidstr
, int from_tty
);
60 static void thread_apply_all_command (char *, int);
61 static int thread_alive (struct thread_info
*);
62 static void info_threads_command (char *, int);
63 static void thread_apply_command (char *, int);
64 static void restore_current_thread (ptid_t
);
65 static void prune_threads (void);
68 inferior_thread (void)
70 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
76 delete_step_resume_breakpoint (struct thread_info
*tp
)
78 if (tp
&& tp
->control
.step_resume_breakpoint
)
80 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
81 tp
->control
.step_resume_breakpoint
= NULL
;
86 delete_exception_resume_breakpoint (struct thread_info
*tp
)
88 if (tp
&& tp
->control
.exception_resume_breakpoint
)
90 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
91 tp
->control
.exception_resume_breakpoint
= NULL
;
96 clear_thread_inferior_resources (struct thread_info
*tp
)
98 /* NOTE: this will take care of any left-over step_resume breakpoints,
99 but not any user-specified thread-specific breakpoints. We can not
100 delete the breakpoint straight-off, because the inferior might not
101 be stopped at the moment. */
102 if (tp
->control
.step_resume_breakpoint
)
104 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
105 tp
->control
.step_resume_breakpoint
= NULL
;
108 if (tp
->control
.exception_resume_breakpoint
)
110 tp
->control
.exception_resume_breakpoint
->disposition
111 = disp_del_at_next_stop
;
112 tp
->control
.exception_resume_breakpoint
= NULL
;
115 delete_longjmp_breakpoint_at_next_stop (tp
->num
);
117 bpstat_clear (&tp
->control
.stop_bpstat
);
119 do_all_intermediate_continuations_thread (tp
, 1);
120 do_all_continuations_thread (tp
, 1);
124 free_thread (struct thread_info
*tp
)
128 if (tp
->private_dtor
)
129 tp
->private_dtor (tp
->private);
139 init_thread_list (void)
141 struct thread_info
*tp
, *tpnext
;
143 highest_thread_num
= 0;
148 for (tp
= thread_list
; tp
; tp
= tpnext
)
157 /* Allocate a new thread with target id PTID and add it to the thread
160 static struct thread_info
*
161 new_thread (ptid_t ptid
)
163 struct thread_info
*tp
;
165 tp
= xcalloc (1, sizeof (*tp
));
168 tp
->num
= ++highest_thread_num
;
169 tp
->next
= thread_list
;
172 /* Nothing to follow yet. */
173 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
174 tp
->state
= THREAD_STOPPED
;
180 add_thread_silent (ptid_t ptid
)
182 struct thread_info
*tp
;
184 tp
= find_thread_ptid (ptid
);
186 /* Found an old thread with the same id. It has to be dead,
187 otherwise we wouldn't be adding a new thread with the same id.
188 The OS is reusing this id --- delete it, and recreate a new
191 /* In addition to deleting the thread, if this is the current
192 thread, then we need to take care that delete_thread doesn't
193 really delete the thread if it is inferior_ptid. Create a
194 new template thread in the list with an invalid ptid, switch
195 to it, delete the original thread, reset the new thread's
196 ptid, and switch to it. */
198 if (ptid_equal (inferior_ptid
, ptid
))
200 tp
= new_thread (null_ptid
);
202 /* Make switch_to_thread not read from the thread. */
203 tp
->state
= THREAD_EXITED
;
204 switch_to_thread (null_ptid
);
206 /* Now we can delete it. */
207 delete_thread (ptid
);
209 /* Now reset its ptid, and reswitch inferior_ptid to it. */
211 tp
->state
= THREAD_STOPPED
;
212 switch_to_thread (ptid
);
214 observer_notify_new_thread (tp
);
220 /* Just go ahead and delete it. */
221 delete_thread (ptid
);
224 tp
= new_thread (ptid
);
225 observer_notify_new_thread (tp
);
231 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
233 struct thread_info
*result
= add_thread_silent (ptid
);
235 result
->private = private;
237 if (print_thread_events
)
238 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
240 annotate_new_thread ();
245 add_thread (ptid_t ptid
)
247 return add_thread_with_info (ptid
, NULL
);
250 /* Delete thread PTID. If SILENT, don't notify the observer of this
253 delete_thread_1 (ptid_t ptid
, int silent
)
255 struct thread_info
*tp
, *tpprev
;
259 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
260 if (ptid_equal (tp
->ptid
, ptid
))
266 /* If this is the current thread, or there's code out there that
267 relies on it existing (refcount > 0) we can't delete yet. Mark
268 it as exited, and notify it. */
270 || ptid_equal (tp
->ptid
, inferior_ptid
))
272 if (tp
->state
!= THREAD_EXITED
)
274 observer_notify_thread_exit (tp
, silent
);
276 /* Tag it as exited. */
277 tp
->state
= THREAD_EXITED
;
279 /* Clear breakpoints, etc. associated with this thread. */
280 clear_thread_inferior_resources (tp
);
283 /* Will be really deleted some other time. */
287 /* Notify thread exit, but only if we haven't already. */
288 if (tp
->state
!= THREAD_EXITED
)
289 observer_notify_thread_exit (tp
, silent
);
291 /* Tag it as exited. */
292 tp
->state
= THREAD_EXITED
;
293 clear_thread_inferior_resources (tp
);
296 tpprev
->next
= tp
->next
;
298 thread_list
= tp
->next
;
303 /* Delete thread PTID and notify of thread exit. If this is
304 inferior_ptid, don't actually delete it, but tag it as exited and
305 do the notification. If PTID is the user selected thread, clear
308 delete_thread (ptid_t ptid
)
310 delete_thread_1 (ptid
, 0 /* not silent */);
314 delete_thread_silent (ptid_t ptid
)
316 delete_thread_1 (ptid
, 1 /* silent */);
320 find_thread_id (int num
)
322 struct thread_info
*tp
;
324 for (tp
= thread_list
; tp
; tp
= tp
->next
)
331 /* Find a thread_info by matching PTID. */
333 find_thread_ptid (ptid_t ptid
)
335 struct thread_info
*tp
;
337 for (tp
= thread_list
; tp
; tp
= tp
->next
)
338 if (ptid_equal (tp
->ptid
, ptid
))
345 * Thread iterator function.
347 * Calls a callback function once for each thread, so long as
348 * the callback function returns false. If the callback function
349 * returns true, the iteration will end and the current thread
350 * will be returned. This can be useful for implementing a
351 * search for a thread with arbitrary attributes, or for applying
352 * some operation to every thread.
354 * FIXME: some of the existing functionality, such as
355 * "Thread apply all", might be rewritten using this functionality.
359 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
362 struct thread_info
*tp
, *next
;
364 for (tp
= thread_list
; tp
; tp
= next
)
367 if ((*callback
) (tp
, data
))
378 struct thread_info
*tp
;
380 for (tp
= thread_list
; tp
; tp
= tp
->next
)
387 valid_thread_id (int num
)
389 struct thread_info
*tp
;
391 for (tp
= thread_list
; tp
; tp
= tp
->next
)
399 pid_to_thread_id (ptid_t ptid
)
401 struct thread_info
*tp
;
403 for (tp
= thread_list
; tp
; tp
= tp
->next
)
404 if (ptid_equal (tp
->ptid
, ptid
))
411 thread_id_to_pid (int num
)
413 struct thread_info
*thread
= find_thread_id (num
);
418 return pid_to_ptid (-1);
422 in_thread_list (ptid_t ptid
)
424 struct thread_info
*tp
;
426 for (tp
= thread_list
; tp
; tp
= tp
->next
)
427 if (ptid_equal (tp
->ptid
, ptid
))
430 return 0; /* Never heard of 'im. */
433 /* Finds the first thread of the inferior given by PID. If PID is -1,
434 return the first thread in the list. */
437 first_thread_of_process (int pid
)
439 struct thread_info
*tp
, *ret
= NULL
;
441 for (tp
= thread_list
; tp
; tp
= tp
->next
)
442 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
443 if (ret
== NULL
|| tp
->num
< ret
->num
)
450 any_thread_of_process (int pid
)
452 struct thread_info
*tp
;
454 for (tp
= thread_list
; tp
; tp
= tp
->next
)
455 if (ptid_get_pid (tp
->ptid
) == pid
)
462 any_live_thread_of_process (int pid
)
464 struct thread_info
*tp
;
465 struct thread_info
*tp_executing
= NULL
;
467 for (tp
= thread_list
; tp
; tp
= tp
->next
)
468 if (tp
->state
!= THREAD_EXITED
&& ptid_get_pid (tp
->ptid
) == pid
)
479 /* Print a list of thread ids currently known, and the total number of
480 threads. To be used from within catch_errors. */
482 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
484 struct thread_info
*tp
;
486 struct cleanup
*cleanup_chain
;
487 int current_thread
= -1;
489 update_thread_list ();
491 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
493 for (tp
= thread_list
; tp
; tp
= tp
->next
)
495 if (tp
->state
== THREAD_EXITED
)
498 if (ptid_equal (tp
->ptid
, inferior_ptid
))
499 current_thread
= tp
->num
;
502 ui_out_field_int (uiout
, "thread-id", tp
->num
);
505 do_cleanups (cleanup_chain
);
507 if (current_thread
!= -1)
508 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
509 ui_out_field_int (uiout
, "number-of-threads", num
);
513 /* Official gdblib interface function to get a list of thread ids and
516 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
518 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
519 error_message
, RETURN_MASK_ALL
) < 0)
524 /* Return true if TP is an active thread. */
526 thread_alive (struct thread_info
*tp
)
528 if (tp
->state
== THREAD_EXITED
)
530 if (!target_thread_alive (tp
->ptid
))
538 struct thread_info
*tp
, *next
;
540 for (tp
= thread_list
; tp
; tp
= next
)
543 if (!thread_alive (tp
))
544 delete_thread (tp
->ptid
);
549 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
551 struct inferior
*inf
;
552 struct thread_info
*tp
;
554 /* It can happen that what we knew as the target inferior id
555 changes. E.g, target remote may only discover the remote process
556 pid after adding the inferior to GDB's list. */
557 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
558 inf
->pid
= ptid_get_pid (new_ptid
);
560 tp
= find_thread_ptid (old_ptid
);
563 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
567 set_running (ptid_t ptid
, int running
)
569 struct thread_info
*tp
;
570 int all
= ptid_equal (ptid
, minus_one_ptid
);
572 /* We try not to notify the observer if no thread has actually changed
573 the running state -- merely to reduce the number of messages to
574 frontend. Frontend is supposed to handle multiple *running just fine. */
575 if (all
|| ptid_is_pid (ptid
))
579 for (tp
= thread_list
; tp
; tp
= tp
->next
)
580 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
582 if (tp
->state
== THREAD_EXITED
)
584 if (running
&& tp
->state
== THREAD_STOPPED
)
586 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
589 observer_notify_target_resumed (ptid
);
595 tp
= find_thread_ptid (ptid
);
597 gdb_assert (tp
->state
!= THREAD_EXITED
);
598 if (running
&& tp
->state
== THREAD_STOPPED
)
600 tp
->state
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
602 observer_notify_target_resumed (ptid
);
607 is_thread_state (ptid_t ptid
, enum thread_state state
)
609 struct thread_info
*tp
;
611 tp
= find_thread_ptid (ptid
);
613 return tp
->state
== state
;
617 is_stopped (ptid_t ptid
)
619 return is_thread_state (ptid
, THREAD_STOPPED
);
623 is_exited (ptid_t ptid
)
625 return is_thread_state (ptid
, THREAD_EXITED
);
629 is_running (ptid_t ptid
)
631 return is_thread_state (ptid
, THREAD_RUNNING
);
637 struct thread_info
*tp
;
639 for (tp
= thread_list
; tp
; tp
= tp
->next
)
640 if (tp
->state
== THREAD_RUNNING
)
647 is_executing (ptid_t ptid
)
649 struct thread_info
*tp
;
651 tp
= find_thread_ptid (ptid
);
653 return tp
->executing
;
657 set_executing (ptid_t ptid
, int executing
)
659 struct thread_info
*tp
;
660 int all
= ptid_equal (ptid
, minus_one_ptid
);
662 if (all
|| ptid_is_pid (ptid
))
664 for (tp
= thread_list
; tp
; tp
= tp
->next
)
665 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
666 tp
->executing
= executing
;
670 tp
= find_thread_ptid (ptid
);
672 tp
->executing
= executing
;
677 set_stop_requested (ptid_t ptid
, int stop
)
679 struct thread_info
*tp
;
680 int all
= ptid_equal (ptid
, minus_one_ptid
);
682 if (all
|| ptid_is_pid (ptid
))
684 for (tp
= thread_list
; tp
; tp
= tp
->next
)
685 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
686 tp
->stop_requested
= stop
;
690 tp
= find_thread_ptid (ptid
);
692 tp
->stop_requested
= stop
;
695 /* Call the stop requested observer so other components of GDB can
696 react to this request. */
698 observer_notify_thread_stop_requested (ptid
);
702 finish_thread_state (ptid_t ptid
)
704 struct thread_info
*tp
;
708 all
= ptid_equal (ptid
, minus_one_ptid
);
710 if (all
|| ptid_is_pid (ptid
))
712 for (tp
= thread_list
; tp
; tp
= tp
->next
)
714 if (tp
->state
== THREAD_EXITED
)
716 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
718 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
720 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
726 tp
= find_thread_ptid (ptid
);
728 if (tp
->state
!= THREAD_EXITED
)
730 if (tp
->executing
&& tp
->state
== THREAD_STOPPED
)
732 tp
->state
= tp
->executing
? THREAD_RUNNING
: THREAD_STOPPED
;
737 observer_notify_target_resumed (ptid
);
741 finish_thread_state_cleanup (void *arg
)
743 ptid_t
*ptid_p
= arg
;
747 finish_thread_state (*ptid_p
);
750 /* Prints the list of threads and their details on UIOUT.
751 This is a version of 'info_threads_command' suitable for
753 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
754 that should be printed. Otherwise, all threads are
756 If PID is not -1, only print threads from the process PID.
757 Otherwise, threads from all attached PIDs are printed.
758 If both REQUESTED_THREAD and PID are not -1, then the thread
759 is printed if it belongs to the specified process. Otherwise,
760 an error is raised. */
762 print_thread_info (struct ui_out
*uiout
, char *requested_threads
, int pid
)
764 struct thread_info
*tp
;
766 struct cleanup
*old_chain
;
767 char *extra_info
, *name
, *target_id
;
768 int current_thread
= -1;
770 update_thread_list ();
771 current_ptid
= inferior_ptid
;
773 /* We'll be switching threads temporarily. */
774 old_chain
= make_cleanup_restore_current_thread ();
776 /* For backward compatibility, we make a list for MI. A table is
777 preferable for the CLI, though, because it shows table
779 if (ui_out_is_mi_like_p (uiout
))
780 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
785 for (tp
= thread_list
; tp
; tp
= tp
->next
)
787 if (!number_is_in_list (requested_threads
, tp
->num
))
790 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
793 if (tp
->state
== THREAD_EXITED
)
801 if (requested_threads
== NULL
|| *requested_threads
== '\0')
802 ui_out_message (uiout
, 0, _("No threads.\n"));
804 ui_out_message (uiout
, 0, _("No threads match '%s'.\n"),
806 do_cleanups (old_chain
);
810 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
812 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
813 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
814 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
815 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
816 ui_out_table_body (uiout
);
819 for (tp
= thread_list
; tp
; tp
= tp
->next
)
821 struct cleanup
*chain2
;
824 if (!number_is_in_list (requested_threads
, tp
->num
))
827 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
829 if (requested_threads
!= NULL
&& *requested_threads
!= '\0')
830 error (_("Requested thread not found in requested process"));
834 if (ptid_equal (tp
->ptid
, current_ptid
))
835 current_thread
= tp
->num
;
837 if (tp
->state
== THREAD_EXITED
)
840 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
842 if (ui_out_is_mi_like_p (uiout
))
845 if (ptid_equal (tp
->ptid
, current_ptid
))
846 ui_out_text (uiout
, "* ");
848 ui_out_text (uiout
, " ");
852 if (ptid_equal (tp
->ptid
, current_ptid
))
853 ui_out_field_string (uiout
, "current", "*");
855 ui_out_field_skip (uiout
, "current");
858 ui_out_field_int (uiout
, "id", tp
->num
);
860 /* For the CLI, we stuff everything into the target-id field.
861 This is a gross hack to make the output come out looking
862 correct. The underlying problem here is that ui-out has no
863 way to specify that a field's space allocation should be
864 shared by several fields. For MI, we do the right thing
867 target_id
= target_pid_to_str (tp
->ptid
);
868 extra_info
= target_extra_thread_info (tp
);
869 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
871 if (ui_out_is_mi_like_p (uiout
))
873 ui_out_field_string (uiout
, "target-id", target_id
);
875 ui_out_field_string (uiout
, "details", extra_info
);
877 ui_out_field_string (uiout
, "name", name
);
881 struct cleanup
*str_cleanup
;
884 if (extra_info
&& name
)
885 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
888 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
890 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
892 contents
= xstrdup (target_id
);
893 str_cleanup
= make_cleanup (xfree
, contents
);
895 ui_out_field_string (uiout
, "target-id", contents
);
896 do_cleanups (str_cleanup
);
899 if (tp
->state
== THREAD_RUNNING
)
900 ui_out_text (uiout
, "(running)\n");
903 /* The switch below puts us at the top of the stack (leaf
905 switch_to_thread (tp
->ptid
);
906 print_stack_frame (get_selected_frame (NULL
),
907 /* For MI output, print frame level. */
908 ui_out_is_mi_like_p (uiout
),
912 if (ui_out_is_mi_like_p (uiout
))
914 char *state
= "stopped";
916 if (tp
->state
== THREAD_RUNNING
)
918 ui_out_field_string (uiout
, "state", state
);
921 core
= target_core_of_thread (tp
->ptid
);
922 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
923 ui_out_field_int (uiout
, "core", core
);
925 do_cleanups (chain2
);
928 /* Restores the current thread and the frame selected before
929 the "info threads" command. */
930 do_cleanups (old_chain
);
932 if (pid
== -1 && requested_threads
== NULL
)
934 gdb_assert (current_thread
!= -1
936 || ptid_equal (inferior_ptid
, null_ptid
));
937 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
938 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
940 if (current_thread
!= -1 && is_exited (current_ptid
))
941 ui_out_message (uiout
, 0, "\n\
942 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
945 && current_thread
== -1
946 && ptid_equal (current_ptid
, null_ptid
))
947 ui_out_message (uiout
, 0, "\n\
948 No selected thread. See `help thread'.\n");
952 /* Print information about currently known threads
954 Optional ARG is a thread id, or list of thread ids.
956 Note: this has the drawback that it _really_ switches
957 threads, which frees the frame cache. A no-side
958 effects info-threads command would be nicer. */
961 info_threads_command (char *arg
, int from_tty
)
963 print_thread_info (current_uiout
, arg
, -1);
966 /* Switch from one thread to another. */
969 switch_to_thread (ptid_t ptid
)
971 /* Switch the program space as well, if we can infer it from the now
972 current thread. Otherwise, it's up to the caller to select the
974 if (!ptid_equal (ptid
, null_ptid
))
976 struct inferior
*inf
;
978 inf
= find_inferior_pid (ptid_get_pid (ptid
));
979 gdb_assert (inf
!= NULL
);
980 set_current_program_space (inf
->pspace
);
981 set_current_inferior (inf
);
984 if (ptid_equal (ptid
, inferior_ptid
))
987 inferior_ptid
= ptid
;
988 reinit_frame_cache ();
990 /* We don't check for is_stopped, because we're called at times
991 while in the TARGET_RUNNING state, e.g., while handling an
993 if (!ptid_equal (inferior_ptid
, null_ptid
)
995 && !is_executing (ptid
))
996 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
998 stop_pc
= ~(CORE_ADDR
) 0;
1002 restore_current_thread (ptid_t ptid
)
1004 switch_to_thread (ptid
);
1008 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1010 struct frame_info
*frame
= NULL
;
1013 /* This means there was no selected frame. */
1014 if (frame_level
== -1)
1016 select_frame (NULL
);
1020 gdb_assert (frame_level
>= 0);
1022 /* Restore by level first, check if the frame id is the same as
1023 expected. If that fails, try restoring by frame id. If that
1024 fails, nothing to do, just warn the user. */
1026 count
= frame_level
;
1027 frame
= find_relative_frame (get_current_frame (), &count
);
1030 /* The frame ids must match - either both valid or both outer_frame_id.
1031 The latter case is not failsafe, but since it's highly unlikely
1032 the search by level finds the wrong frame, it's 99.9(9)% of
1033 the time (for all practical purposes) safe. */
1034 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1036 /* Cool, all is fine. */
1037 select_frame (frame
);
1041 frame
= frame_find_by_id (a_frame_id
);
1044 /* Cool, refound it. */
1045 select_frame (frame
);
1049 /* Nothing else to do, the frame layout really changed. Select the
1050 innermost stack frame. */
1051 select_frame (get_current_frame ());
1053 /* Warn the user. */
1054 if (frame_level
> 0 && !ui_out_is_mi_like_p (current_uiout
))
1056 warning (_("Couldn't restore frame #%d in "
1057 "current thread, at reparsed frame #0\n"),
1059 /* For MI, we should probably have a notification about
1060 current frame change. But this error is not very
1061 likely, so don't bother for now. */
1062 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1066 struct current_thread_cleanup
1068 ptid_t inferior_ptid
;
1069 struct frame_id selected_frame_id
;
1070 int selected_frame_level
;
1077 do_restore_current_thread_cleanup (void *arg
)
1079 struct thread_info
*tp
;
1080 struct current_thread_cleanup
*old
= arg
;
1082 tp
= find_thread_ptid (old
->inferior_ptid
);
1084 /* If the previously selected thread belonged to a process that has
1085 in the mean time been deleted (due to normal exit, detach, etc.),
1086 then don't revert back to it, but instead simply drop back to no
1089 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1090 restore_current_thread (old
->inferior_ptid
);
1093 restore_current_thread (null_ptid
);
1094 set_current_inferior (find_inferior_id (old
->inf_id
));
1097 /* The running state of the originally selected thread may have
1098 changed, so we have to recheck it here. */
1099 if (!ptid_equal (inferior_ptid
, null_ptid
)
1101 && is_stopped (inferior_ptid
)
1102 && target_has_registers
1104 && target_has_memory
)
1105 restore_selected_frame (old
->selected_frame_id
,
1106 old
->selected_frame_level
);
1110 restore_current_thread_cleanup_dtor (void *arg
)
1112 struct current_thread_cleanup
*old
= arg
;
1113 struct thread_info
*tp
;
1114 struct inferior
*inf
;
1116 tp
= find_thread_ptid (old
->inferior_ptid
);
1119 inf
= find_inferior_id (old
->inf_id
);
1121 inf
->removable
= old
->was_removable
;
1126 make_cleanup_restore_current_thread (void)
1128 struct thread_info
*tp
;
1129 struct frame_info
*frame
;
1130 struct current_thread_cleanup
*old
;
1132 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1133 old
->inferior_ptid
= inferior_ptid
;
1134 old
->inf_id
= current_inferior ()->num
;
1135 old
->was_removable
= current_inferior ()->removable
;
1137 if (!ptid_equal (inferior_ptid
, null_ptid
))
1139 old
->was_stopped
= is_stopped (inferior_ptid
);
1140 if (old
->was_stopped
1141 && target_has_registers
1143 && target_has_memory
)
1145 /* When processing internal events, there might not be a
1146 selected frame. If we naively call get_selected_frame
1147 here, then we can end up reading debuginfo for the
1148 current frame, but we don't generally need the debuginfo
1150 frame
= get_selected_frame_if_set ();
1155 old
->selected_frame_id
= get_frame_id (frame
);
1156 old
->selected_frame_level
= frame_relative_level (frame
);
1158 tp
= find_thread_ptid (inferior_ptid
);
1163 current_inferior ()->removable
= 0;
1165 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1166 restore_current_thread_cleanup_dtor
);
1169 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1170 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1171 of two numbers seperated by a hyphen. Examples:
1173 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1174 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1175 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1178 thread_apply_all_command (char *cmd
, int from_tty
)
1180 struct thread_info
*tp
;
1181 struct cleanup
*old_chain
;
1184 if (cmd
== NULL
|| *cmd
== '\000')
1185 error (_("Please specify a command following the thread ID list"));
1187 update_thread_list ();
1189 old_chain
= make_cleanup_restore_current_thread ();
1191 /* Save a copy of the command in case it is clobbered by
1193 saved_cmd
= xstrdup (cmd
);
1194 make_cleanup (xfree
, saved_cmd
);
1195 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1196 if (thread_alive (tp
))
1198 switch_to_thread (tp
->ptid
);
1200 printf_filtered (_("\nThread %d (%s):\n"),
1201 tp
->num
, target_pid_to_str (inferior_ptid
));
1202 execute_command (cmd
, from_tty
);
1203 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1207 do_cleanups (old_chain
);
1211 thread_apply_command (char *tidlist
, int from_tty
)
1214 struct cleanup
*old_chain
;
1216 struct get_number_or_range_state state
;
1218 if (tidlist
== NULL
|| *tidlist
== '\000')
1219 error (_("Please specify a thread ID list"));
1221 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1224 error (_("Please specify a command following the thread ID list"));
1226 /* Save a copy of the command in case it is clobbered by
1228 saved_cmd
= xstrdup (cmd
);
1229 old_chain
= make_cleanup (xfree
, saved_cmd
);
1231 init_number_or_range (&state
, tidlist
);
1232 while (!state
.finished
&& state
.string
< cmd
)
1234 struct thread_info
*tp
;
1238 start
= get_number_or_range (&state
);
1240 make_cleanup_restore_current_thread ();
1242 tp
= find_thread_id (start
);
1245 warning (_("Unknown thread %d."), start
);
1246 else if (!thread_alive (tp
))
1247 warning (_("Thread %d has terminated."), start
);
1250 switch_to_thread (tp
->ptid
);
1252 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1253 target_pid_to_str (inferior_ptid
));
1254 execute_command (cmd
, from_tty
);
1256 /* Restore exact command used previously. */
1257 strcpy (cmd
, saved_cmd
);
1261 do_cleanups (old_chain
);
1264 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1265 if prefix of arg is `apply'. */
1268 thread_command (char *tidstr
, int from_tty
)
1272 if (ptid_equal (inferior_ptid
, null_ptid
))
1273 error (_("No thread selected"));
1275 if (target_has_stack
)
1277 if (is_exited (inferior_ptid
))
1278 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1279 pid_to_thread_id (inferior_ptid
),
1280 target_pid_to_str (inferior_ptid
));
1282 printf_filtered (_("[Current thread is %d (%s)]\n"),
1283 pid_to_thread_id (inferior_ptid
),
1284 target_pid_to_str (inferior_ptid
));
1287 error (_("No stack."));
1291 gdb_thread_select (current_uiout
, tidstr
, NULL
);
1294 /* Implementation of `thread name'. */
1297 thread_name_command (char *arg
, int from_tty
)
1299 struct thread_info
*info
;
1301 if (ptid_equal (inferior_ptid
, null_ptid
))
1302 error (_("No thread selected"));
1304 while (arg
&& isspace (*arg
))
1307 info
= inferior_thread ();
1309 info
->name
= arg
? xstrdup (arg
) : NULL
;
1312 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1315 thread_find_command (char *arg
, int from_tty
)
1317 struct thread_info
*tp
;
1319 unsigned long match
= 0;
1321 if (arg
== NULL
|| *arg
== '\0')
1322 error (_("Command requires an argument."));
1324 tmp
= re_comp (arg
);
1326 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1328 update_thread_list ();
1329 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1331 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1333 printf_filtered (_("Thread %d has name '%s'\n"),
1338 tmp
= target_thread_name (tp
);
1339 if (tmp
!= NULL
&& re_exec (tmp
))
1341 printf_filtered (_("Thread %d has target name '%s'\n"),
1346 tmp
= target_pid_to_str (tp
->ptid
);
1347 if (tmp
!= NULL
&& re_exec (tmp
))
1349 printf_filtered (_("Thread %d has target id '%s'\n"),
1354 tmp
= target_extra_thread_info (tp
);
1355 if (tmp
!= NULL
&& re_exec (tmp
))
1357 printf_filtered (_("Thread %d has extra info '%s'\n"),
1363 printf_filtered (_("No threads match '%s'\n"), arg
);
1366 /* Print notices when new threads are attached and detached. */
1367 int print_thread_events
= 1;
1369 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1370 struct cmd_list_element
*c
, const char *value
)
1372 fprintf_filtered (file
,
1373 _("Printing of thread events is %s.\n"),
1378 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1381 struct thread_info
*tp
;
1383 num
= value_as_long (parse_and_eval (tidstr
));
1385 tp
= find_thread_id (num
);
1388 error (_("Thread ID %d not known."), num
);
1390 if (!thread_alive (tp
))
1391 error (_("Thread ID %d has terminated."), num
);
1393 switch_to_thread (tp
->ptid
);
1395 annotate_thread_changed ();
1397 ui_out_text (uiout
, "[Switching to thread ");
1398 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1399 ui_out_text (uiout
, " (");
1400 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1401 ui_out_text (uiout
, ")]");
1403 /* Note that we can't reach this with an exited thread, due to the
1404 thread_alive check above. */
1405 if (tp
->state
== THREAD_RUNNING
)
1406 ui_out_text (uiout
, "(running)\n");
1409 ui_out_text (uiout
, "\n");
1410 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1413 /* Since the current thread may have changed, see if there is any
1414 exited thread we can now delete. */
1421 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1423 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1424 error_message
, RETURN_MASK_ALL
) < 0)
1430 update_thread_list (void)
1433 target_find_new_threads ();
1436 /* Return a new value for the selected thread's id. Return a value of 0 if
1437 no thread is selected, or no threads exist. */
1439 static struct value
*
1440 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1443 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1445 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1446 (tp
? tp
->num
: 0));
1449 /* Commands with a prefix of `thread'. */
1450 struct cmd_list_element
*thread_cmd_list
= NULL
;
1452 /* Implementation of `thread' variable. */
1454 static const struct internalvar_funcs thread_funcs
=
1456 thread_id_make_value
,
1462 _initialize_thread (void)
1464 static struct cmd_list_element
*thread_apply_list
= NULL
;
1466 add_info ("threads", info_threads_command
,
1467 _("Display currently known threads.\n\
1468 Usage: info threads [ID]...\n\
1469 Optional arguments are thread IDs with spaces between.\n\
1470 If no arguments, all threads are displayed."));
1472 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1473 Use this command to switch between threads.\n\
1474 The new thread ID must be currently known."),
1475 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1477 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1478 _("Apply a command to a list of threads."),
1479 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1481 add_cmd ("all", class_run
, thread_apply_all_command
,
1482 _("Apply a command to all threads."), &thread_apply_list
);
1484 add_cmd ("name", class_run
, thread_name_command
,
1485 _("Set the current thread's name.\n\
1486 Usage: thread name [NAME]\n\
1487 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1489 add_cmd ("find", class_run
, thread_find_command
, _("\
1490 Find threads that match a regular expression.\n\
1491 Usage: thread find REGEXP\n\
1492 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1496 add_com_alias ("t", "thread", class_run
, 1);
1498 add_setshow_boolean_cmd ("thread-events", no_class
,
1499 &print_thread_events
, _("\
1500 Set printing of thread events (such as thread start and exit)."), _("\
1501 Show printing of thread events (such as thread start and exit)."), NULL
,
1503 show_print_thread_events
,
1504 &setprintlist
, &showprintlist
);
1506 create_internalvar_type_lazy ("_thread", &thread_funcs
, NULL
);