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"
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 static 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);
67 /* Frontend view of the thread state. Possible extensions: stepping,
68 finishing, until(ling),... */
77 inferior_thread (void)
79 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
85 delete_step_resume_breakpoint (struct thread_info
*tp
)
87 if (tp
&& tp
->control
.step_resume_breakpoint
)
89 delete_breakpoint (tp
->control
.step_resume_breakpoint
);
90 tp
->control
.step_resume_breakpoint
= NULL
;
95 delete_exception_resume_breakpoint (struct thread_info
*tp
)
97 if (tp
&& tp
->control
.exception_resume_breakpoint
)
99 delete_breakpoint (tp
->control
.exception_resume_breakpoint
);
100 tp
->control
.exception_resume_breakpoint
= NULL
;
105 clear_thread_inferior_resources (struct thread_info
*tp
)
107 /* NOTE: this will take care of any left-over step_resume breakpoints,
108 but not any user-specified thread-specific breakpoints. We can not
109 delete the breakpoint straight-off, because the inferior might not
110 be stopped at the moment. */
111 if (tp
->control
.step_resume_breakpoint
)
113 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
114 tp
->control
.step_resume_breakpoint
= NULL
;
117 if (tp
->control
.exception_resume_breakpoint
)
119 tp
->control
.exception_resume_breakpoint
->disposition
120 = disp_del_at_next_stop
;
121 tp
->control
.exception_resume_breakpoint
= NULL
;
124 bpstat_clear (&tp
->control
.stop_bpstat
);
126 discard_all_intermediate_continuations_thread (tp
);
127 discard_all_continuations_thread (tp
);
129 delete_longjmp_breakpoint (tp
->num
);
133 free_thread (struct thread_info
*tp
)
135 clear_thread_inferior_resources (tp
);
139 if (tp
->private_dtor
)
140 tp
->private_dtor (tp
->private);
150 init_thread_list (void)
152 struct thread_info
*tp
, *tpnext
;
154 highest_thread_num
= 0;
159 for (tp
= thread_list
; tp
; tp
= tpnext
)
168 /* Allocate a new thread with target id PTID and add it to the thread
171 static struct thread_info
*
172 new_thread (ptid_t ptid
)
174 struct thread_info
*tp
;
176 tp
= xcalloc (1, sizeof (*tp
));
179 tp
->num
= ++highest_thread_num
;
180 tp
->next
= thread_list
;
183 /* Nothing to follow yet. */
184 tp
->pending_follow
.kind
= TARGET_WAITKIND_SPURIOUS
;
185 tp
->state_
= THREAD_STOPPED
;
191 add_thread_silent (ptid_t ptid
)
193 struct thread_info
*tp
;
195 tp
= find_thread_ptid (ptid
);
197 /* Found an old thread with the same id. It has to be dead,
198 otherwise we wouldn't be adding a new thread with the same id.
199 The OS is reusing this id --- delete it, and recreate a new
202 /* In addition to deleting the thread, if this is the current
203 thread, then we need to take care that delete_thread doesn't
204 really delete the thread if it is inferior_ptid. Create a
205 new template thread in the list with an invalid ptid, switch
206 to it, delete the original thread, reset the new thread's
207 ptid, and switch to it. */
209 if (ptid_equal (inferior_ptid
, ptid
))
211 tp
= new_thread (null_ptid
);
213 /* Make switch_to_thread not read from the thread. */
214 tp
->state_
= THREAD_EXITED
;
215 switch_to_thread (null_ptid
);
217 /* Now we can delete it. */
218 delete_thread (ptid
);
220 /* Now reset its ptid, and reswitch inferior_ptid to it. */
222 tp
->state_
= THREAD_STOPPED
;
223 switch_to_thread (ptid
);
225 observer_notify_new_thread (tp
);
231 /* Just go ahead and delete it. */
232 delete_thread (ptid
);
235 tp
= new_thread (ptid
);
236 observer_notify_new_thread (tp
);
242 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
244 struct thread_info
*result
= add_thread_silent (ptid
);
246 result
->private = private;
248 if (print_thread_events
)
249 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
251 annotate_new_thread ();
256 add_thread (ptid_t ptid
)
258 return add_thread_with_info (ptid
, NULL
);
261 /* Delete thread PTID. If SILENT, don't notify the observer of this
264 delete_thread_1 (ptid_t ptid
, int silent
)
266 struct thread_info
*tp
, *tpprev
;
270 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
271 if (ptid_equal (tp
->ptid
, ptid
))
277 /* If this is the current thread, or there's code out there that
278 relies on it existing (refcount > 0) we can't delete yet. Mark
279 it as exited, and notify it. */
281 || ptid_equal (tp
->ptid
, inferior_ptid
))
283 if (tp
->state_
!= THREAD_EXITED
)
285 observer_notify_thread_exit (tp
, silent
);
287 /* Tag it as exited. */
288 tp
->state_
= THREAD_EXITED
;
290 /* Clear breakpoints, etc. associated with this thread. */
291 clear_thread_inferior_resources (tp
);
294 /* Will be really deleted some other time. */
299 tpprev
->next
= tp
->next
;
301 thread_list
= tp
->next
;
303 /* Notify thread exit, but only if we haven't already. */
304 if (tp
->state_
!= THREAD_EXITED
)
305 observer_notify_thread_exit (tp
, silent
);
310 /* Delete thread PTID and notify of thread exit. If this is
311 inferior_ptid, don't actually delete it, but tag it as exited and
312 do the notification. If PTID is the user selected thread, clear
315 delete_thread (ptid_t ptid
)
317 delete_thread_1 (ptid
, 0 /* not silent */);
321 delete_thread_silent (ptid_t ptid
)
323 delete_thread_1 (ptid
, 1 /* silent */);
327 find_thread_id (int num
)
329 struct thread_info
*tp
;
331 for (tp
= thread_list
; tp
; tp
= tp
->next
)
338 /* Find a thread_info by matching PTID. */
340 find_thread_ptid (ptid_t ptid
)
342 struct thread_info
*tp
;
344 for (tp
= thread_list
; tp
; tp
= tp
->next
)
345 if (ptid_equal (tp
->ptid
, ptid
))
352 * Thread iterator function.
354 * Calls a callback function once for each thread, so long as
355 * the callback function returns false. If the callback function
356 * returns true, the iteration will end and the current thread
357 * will be returned. This can be useful for implementing a
358 * search for a thread with arbitrary attributes, or for applying
359 * some operation to every thread.
361 * FIXME: some of the existing functionality, such as
362 * "Thread apply all", might be rewritten using this functionality.
366 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
369 struct thread_info
*tp
, *next
;
371 for (tp
= thread_list
; tp
; tp
= next
)
374 if ((*callback
) (tp
, data
))
385 struct thread_info
*tp
;
387 for (tp
= thread_list
; tp
; tp
= tp
->next
)
394 valid_thread_id (int num
)
396 struct thread_info
*tp
;
398 for (tp
= thread_list
; tp
; tp
= tp
->next
)
406 pid_to_thread_id (ptid_t ptid
)
408 struct thread_info
*tp
;
410 for (tp
= thread_list
; tp
; tp
= tp
->next
)
411 if (ptid_equal (tp
->ptid
, ptid
))
418 thread_id_to_pid (int num
)
420 struct thread_info
*thread
= find_thread_id (num
);
425 return pid_to_ptid (-1);
429 in_thread_list (ptid_t ptid
)
431 struct thread_info
*tp
;
433 for (tp
= thread_list
; tp
; tp
= tp
->next
)
434 if (ptid_equal (tp
->ptid
, ptid
))
437 return 0; /* Never heard of 'im. */
440 /* Finds the first thread of the inferior given by PID. If PID is -1,
441 return the first thread in the list. */
444 first_thread_of_process (int pid
)
446 struct thread_info
*tp
, *ret
= NULL
;
448 for (tp
= thread_list
; tp
; tp
= tp
->next
)
449 if (pid
== -1 || ptid_get_pid (tp
->ptid
) == pid
)
450 if (ret
== NULL
|| tp
->num
< ret
->num
)
457 any_thread_of_process (int pid
)
459 struct thread_info
*tp
;
461 for (tp
= thread_list
; tp
; tp
= tp
->next
)
462 if (ptid_get_pid (tp
->ptid
) == pid
)
469 any_live_thread_of_process (int pid
)
471 struct thread_info
*tp
;
472 struct thread_info
*tp_running
= NULL
;
474 for (tp
= thread_list
; tp
; tp
= tp
->next
)
475 if (ptid_get_pid (tp
->ptid
) == pid
)
477 if (tp
->state_
== THREAD_STOPPED
)
479 else if (tp
->state_
== THREAD_RUNNING
)
486 /* Print a list of thread ids currently known, and the total number of
487 threads. To be used from within catch_errors. */
489 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
491 struct thread_info
*tp
;
493 struct cleanup
*cleanup_chain
;
494 int current_thread
= -1;
496 update_thread_list ();
498 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
500 for (tp
= thread_list
; tp
; tp
= tp
->next
)
502 if (tp
->state_
== THREAD_EXITED
)
505 if (ptid_equal (tp
->ptid
, inferior_ptid
))
506 current_thread
= tp
->num
;
509 ui_out_field_int (uiout
, "thread-id", tp
->num
);
512 do_cleanups (cleanup_chain
);
514 if (current_thread
!= -1)
515 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
516 ui_out_field_int (uiout
, "number-of-threads", num
);
520 /* Official gdblib interface function to get a list of thread ids and
523 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
525 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
526 error_message
, RETURN_MASK_ALL
) < 0)
531 /* Return true if TP is an active thread. */
533 thread_alive (struct thread_info
*tp
)
535 if (tp
->state_
== THREAD_EXITED
)
537 if (!target_thread_alive (tp
->ptid
))
545 struct thread_info
*tp
, *next
;
547 for (tp
= thread_list
; tp
; tp
= next
)
550 if (!thread_alive (tp
))
551 delete_thread (tp
->ptid
);
556 thread_change_ptid (ptid_t old_ptid
, ptid_t new_ptid
)
558 struct inferior
*inf
;
559 struct thread_info
*tp
;
561 /* It can happen that what we knew as the target inferior id
562 changes. E.g, target remote may only discover the remote process
563 pid after adding the inferior to GDB's list. */
564 inf
= find_inferior_pid (ptid_get_pid (old_ptid
));
565 inf
->pid
= ptid_get_pid (new_ptid
);
567 tp
= find_thread_ptid (old_ptid
);
570 observer_notify_thread_ptid_changed (old_ptid
, new_ptid
);
574 set_running (ptid_t ptid
, int running
)
576 struct thread_info
*tp
;
577 int all
= ptid_equal (ptid
, minus_one_ptid
);
579 /* We try not to notify the observer if no thread has actually changed
580 the running state -- merely to reduce the number of messages to
581 frontend. Frontend is supposed to handle multiple *running just fine. */
582 if (all
|| ptid_is_pid (ptid
))
586 for (tp
= thread_list
; tp
; tp
= tp
->next
)
587 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
589 if (tp
->state_
== THREAD_EXITED
)
591 if (running
&& tp
->state_
== THREAD_STOPPED
)
593 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
596 observer_notify_target_resumed (ptid
);
602 tp
= find_thread_ptid (ptid
);
604 gdb_assert (tp
->state_
!= THREAD_EXITED
);
605 if (running
&& tp
->state_
== THREAD_STOPPED
)
607 tp
->state_
= running
? THREAD_RUNNING
: THREAD_STOPPED
;
609 observer_notify_target_resumed (ptid
);
614 is_thread_state (ptid_t ptid
, enum thread_state state
)
616 struct thread_info
*tp
;
618 tp
= find_thread_ptid (ptid
);
620 return tp
->state_
== state
;
624 is_stopped (ptid_t ptid
)
626 return is_thread_state (ptid
, THREAD_STOPPED
);
630 is_exited (ptid_t ptid
)
632 return is_thread_state (ptid
, THREAD_EXITED
);
636 is_running (ptid_t ptid
)
638 return is_thread_state (ptid
, THREAD_RUNNING
);
644 struct thread_info
*tp
;
646 for (tp
= thread_list
; tp
; tp
= tp
->next
)
647 if (tp
->state_
== THREAD_RUNNING
)
654 is_executing (ptid_t ptid
)
656 struct thread_info
*tp
;
658 tp
= find_thread_ptid (ptid
);
660 return tp
->executing_
;
664 set_executing (ptid_t ptid
, int executing
)
666 struct thread_info
*tp
;
667 int all
= ptid_equal (ptid
, minus_one_ptid
);
669 if (all
|| ptid_is_pid (ptid
))
671 for (tp
= thread_list
; tp
; tp
= tp
->next
)
672 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
673 tp
->executing_
= executing
;
677 tp
= find_thread_ptid (ptid
);
679 tp
->executing_
= executing
;
684 set_stop_requested (ptid_t ptid
, int stop
)
686 struct thread_info
*tp
;
687 int all
= ptid_equal (ptid
, minus_one_ptid
);
689 if (all
|| ptid_is_pid (ptid
))
691 for (tp
= thread_list
; tp
; tp
= tp
->next
)
692 if (all
|| ptid_get_pid (tp
->ptid
) == ptid_get_pid (ptid
))
693 tp
->stop_requested
= stop
;
697 tp
= find_thread_ptid (ptid
);
699 tp
->stop_requested
= stop
;
702 /* Call the stop requested observer so other components of GDB can
703 react to this request. */
705 observer_notify_thread_stop_requested (ptid
);
709 finish_thread_state (ptid_t ptid
)
711 struct thread_info
*tp
;
715 all
= ptid_equal (ptid
, minus_one_ptid
);
717 if (all
|| ptid_is_pid (ptid
))
719 for (tp
= thread_list
; tp
; tp
= tp
->next
)
721 if (tp
->state_
== THREAD_EXITED
)
723 if (all
|| ptid_get_pid (ptid
) == ptid_get_pid (tp
->ptid
))
725 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
727 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
733 tp
= find_thread_ptid (ptid
);
735 if (tp
->state_
!= THREAD_EXITED
)
737 if (tp
->executing_
&& tp
->state_
== THREAD_STOPPED
)
739 tp
->state_
= tp
->executing_
? THREAD_RUNNING
: THREAD_STOPPED
;
744 observer_notify_target_resumed (ptid
);
748 finish_thread_state_cleanup (void *arg
)
750 ptid_t
*ptid_p
= arg
;
754 finish_thread_state (*ptid_p
);
757 /* Prints the list of threads and their details on UIOUT.
758 This is a version of 'info_thread_command' suitable for
760 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
761 that should be printed. Otherwise, all threads are
763 If PID is not -1, only print threads from the process PID.
764 Otherwise, threads from all attached PIDs are printed.
765 If both REQUESTED_THREAD and PID are not -1, then the thread
766 is printed if it belongs to the specified process. Otherwise,
767 an error is raised. */
769 print_thread_info (struct ui_out
*uiout
, int requested_thread
, int pid
)
771 struct thread_info
*tp
;
773 struct cleanup
*old_chain
;
774 char *extra_info
, *name
, *target_id
;
775 int current_thread
= -1;
777 update_thread_list ();
778 current_ptid
= inferior_ptid
;
780 /* We'll be switching threads temporarily. */
781 old_chain
= make_cleanup_restore_current_thread ();
783 /* For backward compatibility, we make a list for MI. A table is
784 preferable for the CLI, though, because it shows table
786 if (ui_out_is_mi_like_p (uiout
))
787 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
792 for (tp
= thread_list
; tp
; tp
= tp
->next
)
794 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
797 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
800 if (tp
->state_
== THREAD_EXITED
)
808 if (requested_thread
== -1)
809 ui_out_message (uiout
, 0, _("No threads.\n"));
811 ui_out_message (uiout
, 0, _("No thread %d.\n"), requested_thread
);
812 do_cleanups (old_chain
);
816 make_cleanup_ui_out_table_begin_end (uiout
, 4, n_threads
, "threads");
818 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
819 ui_out_table_header (uiout
, 4, ui_left
, "id", "Id");
820 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Target Id");
821 ui_out_table_header (uiout
, 1, ui_left
, "frame", "Frame");
822 ui_out_table_body (uiout
);
825 for (tp
= thread_list
; tp
; tp
= tp
->next
)
827 struct cleanup
*chain2
;
830 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
833 if (pid
!= -1 && PIDGET (tp
->ptid
) != pid
)
835 if (requested_thread
!= -1)
836 error (_("Requested thread not found in requested process"));
840 if (ptid_equal (tp
->ptid
, current_ptid
))
841 current_thread
= tp
->num
;
843 if (tp
->state_
== THREAD_EXITED
)
846 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
848 if (ui_out_is_mi_like_p (uiout
))
851 if (ptid_equal (tp
->ptid
, current_ptid
))
852 ui_out_text (uiout
, "* ");
854 ui_out_text (uiout
, " ");
858 if (ptid_equal (tp
->ptid
, current_ptid
))
859 ui_out_field_string (uiout
, "current", "*");
861 ui_out_field_skip (uiout
, "current");
864 ui_out_field_int (uiout
, "id", tp
->num
);
866 /* For the CLI, we stuff everything into the target-id field.
867 This is a gross hack to make the output come out looking
868 correct. The underlying problem here is that ui-out has no
869 way to specify that a field's space allocation should be
870 shared by several fields. For MI, we do the right thing
873 target_id
= target_pid_to_str (tp
->ptid
);
874 extra_info
= target_extra_thread_info (tp
);
875 name
= tp
->name
? tp
->name
: target_thread_name (tp
);
877 if (ui_out_is_mi_like_p (uiout
))
879 ui_out_field_string (uiout
, "target-id", target_id
);
881 ui_out_field_string (uiout
, "details", extra_info
);
883 ui_out_field_string (uiout
, "name", name
);
887 struct cleanup
*str_cleanup
;
890 if (extra_info
&& name
)
891 contents
= xstrprintf ("%s \"%s\" (%s)", target_id
,
894 contents
= xstrprintf ("%s (%s)", target_id
, extra_info
);
896 contents
= xstrprintf ("%s \"%s\"", target_id
, name
);
898 contents
= xstrdup (target_id
);
899 str_cleanup
= make_cleanup (xfree
, contents
);
901 ui_out_field_string (uiout
, "target-id", contents
);
902 do_cleanups (str_cleanup
);
905 if (tp
->state_
== THREAD_RUNNING
)
906 ui_out_text (uiout
, "(running)\n");
909 /* The switch below puts us at the top of the stack (leaf
911 switch_to_thread (tp
->ptid
);
912 print_stack_frame (get_selected_frame (NULL
),
913 /* For MI output, print frame level. */
914 ui_out_is_mi_like_p (uiout
),
918 if (ui_out_is_mi_like_p (uiout
))
920 char *state
= "stopped";
922 if (tp
->state_
== THREAD_RUNNING
)
924 ui_out_field_string (uiout
, "state", state
);
927 core
= target_core_of_thread (tp
->ptid
);
928 if (ui_out_is_mi_like_p (uiout
) && core
!= -1)
929 ui_out_field_int (uiout
, "core", core
);
931 do_cleanups (chain2
);
934 /* Restores the current thread and the frame selected before
935 the "info threads" command. */
936 do_cleanups (old_chain
);
938 if (pid
== -1 && requested_thread
== -1)
940 gdb_assert (current_thread
!= -1
942 || ptid_equal (inferior_ptid
, null_ptid
));
943 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
944 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
946 if (current_thread
!= -1 && is_exited (current_ptid
))
947 ui_out_message (uiout
, 0, "\n\
948 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
951 && current_thread
== -1
952 && ptid_equal (current_ptid
, null_ptid
))
953 ui_out_message (uiout
, 0, "\n\
954 No selected thread. See `help thread'.\n");
958 /* Print information about currently known threads
960 Optional ARG is a thread id, or list of thread ids.
962 Note: this has the drawback that it _really_ switches
963 threads, which frees the frame cache. A no-side
964 effects info-threads command would be nicer. */
967 info_threads_command (char *arg
, int from_tty
)
971 if (arg
== NULL
|| *arg
== '\0')
973 print_thread_info (uiout
, -1, -1);
977 while (arg
!= NULL
&& *arg
!= '\0')
979 tid
= get_number_or_range (&arg
);
982 error (_("invalid thread id %d"), tid
);
984 print_thread_info (uiout
, tid
, -1);
988 /* Switch from one thread to another. */
991 switch_to_thread (ptid_t ptid
)
993 /* Switch the program space as well, if we can infer it from the now
994 current thread. Otherwise, it's up to the caller to select the
996 if (!ptid_equal (ptid
, null_ptid
))
998 struct inferior
*inf
;
1000 inf
= find_inferior_pid (ptid_get_pid (ptid
));
1001 gdb_assert (inf
!= NULL
);
1002 set_current_program_space (inf
->pspace
);
1003 set_current_inferior (inf
);
1006 if (ptid_equal (ptid
, inferior_ptid
))
1009 inferior_ptid
= ptid
;
1010 reinit_frame_cache ();
1011 registers_changed ();
1013 /* We don't check for is_stopped, because we're called at times
1014 while in the TARGET_RUNNING state, e.g., while handling an
1016 if (!ptid_equal (inferior_ptid
, null_ptid
)
1017 && !is_exited (ptid
)
1018 && !is_executing (ptid
))
1019 stop_pc
= regcache_read_pc (get_thread_regcache (ptid
));
1021 stop_pc
= ~(CORE_ADDR
) 0;
1025 restore_current_thread (ptid_t ptid
)
1027 switch_to_thread (ptid
);
1031 restore_selected_frame (struct frame_id a_frame_id
, int frame_level
)
1033 struct frame_info
*frame
= NULL
;
1036 gdb_assert (frame_level
>= 0);
1038 /* Restore by level first, check if the frame id is the same as
1039 expected. If that fails, try restoring by frame id. If that
1040 fails, nothing to do, just warn the user. */
1042 count
= frame_level
;
1043 frame
= find_relative_frame (get_current_frame (), &count
);
1046 /* The frame ids must match - either both valid or both outer_frame_id.
1047 The latter case is not failsafe, but since it's highly unlikely
1048 the search by level finds the wrong frame, it's 99.9(9)% of
1049 the time (for all practical purposes) safe. */
1050 && frame_id_eq (get_frame_id (frame
), a_frame_id
))
1052 /* Cool, all is fine. */
1053 select_frame (frame
);
1057 frame
= frame_find_by_id (a_frame_id
);
1060 /* Cool, refound it. */
1061 select_frame (frame
);
1065 /* Nothing else to do, the frame layout really changed. Select the
1066 innermost stack frame. */
1067 select_frame (get_current_frame ());
1069 /* Warn the user. */
1070 if (frame_level
> 0 && !ui_out_is_mi_like_p (uiout
))
1072 warning (_("Couldn't restore frame #%d in "
1073 "current thread, at reparsed frame #0\n"),
1075 /* For MI, we should probably have a notification about
1076 current frame change. But this error is not very
1077 likely, so don't bother for now. */
1078 print_stack_frame (get_selected_frame (NULL
), 1, SRC_LINE
);
1082 struct current_thread_cleanup
1084 ptid_t inferior_ptid
;
1085 struct frame_id selected_frame_id
;
1086 int selected_frame_level
;
1092 do_restore_current_thread_cleanup (void *arg
)
1094 struct thread_info
*tp
;
1095 struct current_thread_cleanup
*old
= arg
;
1097 tp
= find_thread_ptid (old
->inferior_ptid
);
1099 /* If the previously selected thread belonged to a process that has
1100 in the mean time been deleted (due to normal exit, detach, etc.),
1101 then don't revert back to it, but instead simply drop back to no
1104 && find_inferior_pid (ptid_get_pid (tp
->ptid
)) != NULL
)
1105 restore_current_thread (old
->inferior_ptid
);
1108 restore_current_thread (null_ptid
);
1109 set_current_inferior (find_inferior_id (old
->inf_id
));
1112 /* The running state of the originally selected thread may have
1113 changed, so we have to recheck it here. */
1114 if (!ptid_equal (inferior_ptid
, null_ptid
)
1116 && is_stopped (inferior_ptid
)
1117 && target_has_registers
1119 && target_has_memory
)
1120 restore_selected_frame (old
->selected_frame_id
,
1121 old
->selected_frame_level
);
1125 restore_current_thread_cleanup_dtor (void *arg
)
1127 struct current_thread_cleanup
*old
= arg
;
1128 struct thread_info
*tp
;
1130 tp
= find_thread_ptid (old
->inferior_ptid
);
1137 make_cleanup_restore_current_thread (void)
1139 struct thread_info
*tp
;
1140 struct frame_info
*frame
;
1141 struct current_thread_cleanup
*old
;
1143 old
= xmalloc (sizeof (struct current_thread_cleanup
));
1144 old
->inferior_ptid
= inferior_ptid
;
1145 old
->inf_id
= current_inferior ()->num
;
1147 if (!ptid_equal (inferior_ptid
, null_ptid
))
1149 old
->was_stopped
= is_stopped (inferior_ptid
);
1150 if (old
->was_stopped
1151 && target_has_registers
1153 && target_has_memory
)
1154 frame
= get_selected_frame (NULL
);
1158 old
->selected_frame_id
= get_frame_id (frame
);
1159 old
->selected_frame_level
= frame_relative_level (frame
);
1161 tp
= find_thread_ptid (inferior_ptid
);
1166 return make_cleanup_dtor (do_restore_current_thread_cleanup
, old
,
1167 restore_current_thread_cleanup_dtor
);
1170 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1171 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1172 of two numbers seperated by a hyphen. Examples:
1174 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1175 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1176 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1179 thread_apply_all_command (char *cmd
, int from_tty
)
1181 struct thread_info
*tp
;
1182 struct cleanup
*old_chain
;
1185 if (cmd
== NULL
|| *cmd
== '\000')
1186 error (_("Please specify a command following the thread ID list"));
1188 update_thread_list ();
1190 old_chain
= make_cleanup_restore_current_thread ();
1192 /* Save a copy of the command in case it is clobbered by
1194 saved_cmd
= xstrdup (cmd
);
1195 make_cleanup (xfree
, saved_cmd
);
1196 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1197 if (thread_alive (tp
))
1199 switch_to_thread (tp
->ptid
);
1201 printf_filtered (_("\nThread %d (%s):\n"),
1202 tp
->num
, target_pid_to_str (inferior_ptid
));
1203 execute_command (cmd
, from_tty
);
1204 strcpy (cmd
, saved_cmd
); /* Restore exact command used
1208 do_cleanups (old_chain
);
1212 thread_apply_command (char *tidlist
, int from_tty
)
1216 struct cleanup
*old_chain
;
1219 if (tidlist
== NULL
|| *tidlist
== '\000')
1220 error (_("Please specify a thread ID list"));
1222 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
1225 error (_("Please specify a command following the thread ID list"));
1227 /* Save a copy of the command in case it is clobbered by
1229 saved_cmd
= xstrdup (cmd
);
1230 old_chain
= make_cleanup (xfree
, saved_cmd
);
1231 while (tidlist
< cmd
)
1233 struct thread_info
*tp
;
1236 start
= strtol (tidlist
, &p
, 10);
1238 error (_("Error parsing %s"), tidlist
);
1241 while (*tidlist
== ' ' || *tidlist
== '\t')
1244 if (*tidlist
== '-') /* Got a range of IDs? */
1246 tidlist
++; /* Skip the - */
1247 end
= strtol (tidlist
, &p
, 10);
1249 error (_("Error parsing %s"), tidlist
);
1252 while (*tidlist
== ' ' || *tidlist
== '\t')
1258 make_cleanup_restore_current_thread ();
1260 for (; start
<= end
; start
++)
1262 tp
= find_thread_id (start
);
1265 warning (_("Unknown thread %d."), start
);
1266 else if (!thread_alive (tp
))
1267 warning (_("Thread %d has terminated."), start
);
1270 switch_to_thread (tp
->ptid
);
1272 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
1273 target_pid_to_str (inferior_ptid
));
1274 execute_command (cmd
, from_tty
);
1276 /* Restore exact command used previously. */
1277 strcpy (cmd
, saved_cmd
);
1282 do_cleanups (old_chain
);
1285 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1286 if prefix of arg is `apply'. */
1289 thread_command (char *tidstr
, int from_tty
)
1293 if (ptid_equal (inferior_ptid
, null_ptid
))
1294 error (_("No thread selected"));
1296 if (target_has_stack
)
1298 if (is_exited (inferior_ptid
))
1299 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1300 pid_to_thread_id (inferior_ptid
),
1301 target_pid_to_str (inferior_ptid
));
1303 printf_filtered (_("[Current thread is %d (%s)]\n"),
1304 pid_to_thread_id (inferior_ptid
),
1305 target_pid_to_str (inferior_ptid
));
1308 error (_("No stack."));
1312 gdb_thread_select (uiout
, tidstr
, NULL
);
1315 /* Implementation of `thread name'. */
1318 thread_name_command (char *arg
, int from_tty
)
1320 struct thread_info
*info
;
1322 if (ptid_equal (inferior_ptid
, null_ptid
))
1323 error (_("No thread selected"));
1325 while (arg
&& isspace (*arg
))
1328 info
= inferior_thread ();
1330 info
->name
= arg
? xstrdup (arg
) : NULL
;
1333 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1336 thread_find_command (char *arg
, int from_tty
)
1338 struct thread_info
*tp
;
1340 unsigned long match
= 0;
1342 if (arg
== NULL
|| *arg
== '\0')
1343 error (_("Command requires an argument."));
1345 tmp
= re_comp (arg
);
1347 error (_("Invalid regexp (%s): %s"), tmp
, arg
);
1349 update_thread_list ();
1350 for (tp
= thread_list
; tp
; tp
= tp
->next
)
1352 if (tp
->name
!= NULL
&& re_exec (tp
->name
))
1354 printf_filtered (_("Thread %d has name '%s'\n"),
1359 tmp
= target_thread_name (tp
);
1360 if (tmp
!= NULL
&& re_exec (tmp
))
1362 printf_filtered (_("Thread %d has target name '%s'\n"),
1367 tmp
= target_pid_to_str (tp
->ptid
);
1368 if (tmp
!= NULL
&& re_exec (tmp
))
1370 printf_filtered (_("Thread %d has target id '%s'\n"),
1375 tmp
= target_extra_thread_info (tp
);
1376 if (tmp
!= NULL
&& re_exec (tmp
))
1378 printf_filtered (_("Thread %d has extra info '%s'\n"),
1384 printf_filtered (_("No threads match '%s'\n"), arg
);
1387 /* Print notices when new threads are attached and detached. */
1388 int print_thread_events
= 1;
1390 show_print_thread_events (struct ui_file
*file
, int from_tty
,
1391 struct cmd_list_element
*c
, const char *value
)
1393 fprintf_filtered (file
,
1394 _("Printing of thread events is %s.\n"),
1399 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1402 struct thread_info
*tp
;
1404 num
= value_as_long (parse_and_eval (tidstr
));
1406 tp
= find_thread_id (num
);
1409 error (_("Thread ID %d not known."), num
);
1411 if (!thread_alive (tp
))
1412 error (_("Thread ID %d has terminated."), num
);
1414 switch_to_thread (tp
->ptid
);
1416 annotate_thread_changed ();
1418 ui_out_text (uiout
, "[Switching to thread ");
1419 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1420 ui_out_text (uiout
, " (");
1421 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
1422 ui_out_text (uiout
, ")]");
1424 /* Note that we can't reach this with an exited thread, due to the
1425 thread_alive check above. */
1426 if (tp
->state_
== THREAD_RUNNING
)
1427 ui_out_text (uiout
, "(running)\n");
1430 ui_out_text (uiout
, "\n");
1431 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1434 /* Since the current thread may have changed, see if there is any
1435 exited thread we can now delete. */
1442 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1444 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1445 error_message
, RETURN_MASK_ALL
) < 0)
1451 update_thread_list (void)
1454 target_find_new_threads ();
1457 /* Return a new value for the selected thread's id. Return a value of 0 if
1458 no thread is selected, or no threads exist. */
1460 static struct value
*
1461 thread_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
)
1463 struct thread_info
*tp
= find_thread_ptid (inferior_ptid
);
1465 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
1466 (tp
? tp
->num
: 0));
1469 /* Commands with a prefix of `thread'. */
1470 struct cmd_list_element
*thread_cmd_list
= NULL
;
1473 _initialize_thread (void)
1475 static struct cmd_list_element
*thread_apply_list
= NULL
;
1477 add_info ("threads", info_threads_command
,
1478 _("Display currently known threads.\n\
1479 Usage: info threads [ID]...\n\
1480 Optional arguments are thread IDs with spaces between.\n\
1481 If no arguments, all threads are displayed."));
1483 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1484 Use this command to switch between threads.\n\
1485 The new thread ID must be currently known."),
1486 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1488 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1489 _("Apply a command to a list of threads."),
1490 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1492 add_cmd ("all", class_run
, thread_apply_all_command
,
1493 _("Apply a command to all threads."), &thread_apply_list
);
1495 add_cmd ("name", class_run
, thread_name_command
,
1496 _("Set the current thread's name.\n\
1497 Usage: thread name [NAME]\n\
1498 If NAME is not given, then any existing name is removed."), &thread_cmd_list
);
1500 add_cmd ("find", class_run
, thread_find_command
, _("\
1501 Find threads that match a regular expression.\n\
1502 Usage: thread find REGEXP\n\
1503 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1507 add_com_alias ("t", "thread", class_run
, 1);
1509 add_setshow_boolean_cmd ("thread-events", no_class
,
1510 &print_thread_events
, _("\
1511 Set printing of thread events (such as thread start and exit)."), _("\
1512 Show printing of thread events (such as thread start and exit)."), NULL
,
1514 show_print_thread_events
,
1515 &setprintlist
, &showprintlist
);
1517 create_internalvar_type_lazy ("_thread", thread_id_make_value
);