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);
69 /* Frontend view of the thread state. Possible extensions: stepping,
70 finishing, until(ling),... */
79 inferior_thread (void)
81 struct thread_info *tp = find_thread_ptid (inferior_ptid);
87 delete_step_resume_breakpoint (struct thread_info *tp)
89 if (tp && tp->control.step_resume_breakpoint)
91 delete_breakpoint (tp->control.step_resume_breakpoint);
92 tp->control.step_resume_breakpoint = NULL;
97 delete_exception_resume_breakpoint (struct thread_info *tp)
99 if (tp && tp->control.exception_resume_breakpoint)
101 delete_breakpoint (tp->control.exception_resume_breakpoint);
102 tp->control.exception_resume_breakpoint = NULL;
107 clear_thread_inferior_resources (struct thread_info *tp)
109 /* NOTE: this will take care of any left-over step_resume breakpoints,
110 but not any user-specified thread-specific breakpoints. We can not
111 delete the breakpoint straight-off, because the inferior might not
112 be stopped at the moment. */
113 if (tp->control.step_resume_breakpoint)
115 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
116 tp->control.step_resume_breakpoint = NULL;
119 if (tp->control.exception_resume_breakpoint)
121 tp->control.exception_resume_breakpoint->disposition
122 = disp_del_at_next_stop;
123 tp->control.exception_resume_breakpoint = NULL;
126 bpstat_clear (&tp->control.stop_bpstat);
128 do_all_intermediate_continuations_thread (tp, 1);
129 do_all_continuations_thread (tp, 1);
131 delete_longjmp_breakpoint (tp->num);
135 free_thread (struct thread_info *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. */
298 /* Notify thread exit, but only if we haven't already. */
299 if (tp->state_ != THREAD_EXITED)
300 observer_notify_thread_exit (tp, silent);
302 /* Tag it as exited. */
303 tp->state_ = THREAD_EXITED;
304 clear_thread_inferior_resources (tp);
307 tpprev->next = tp->next;
309 thread_list = tp->next;
314 /* Delete thread PTID and notify of thread exit. If this is
315 inferior_ptid, don't actually delete it, but tag it as exited and
316 do the notification. If PTID is the user selected thread, clear
319 delete_thread (ptid_t ptid)
321 delete_thread_1 (ptid, 0 /* not silent */);
325 delete_thread_silent (ptid_t ptid)
327 delete_thread_1 (ptid, 1 /* silent */);
331 find_thread_id (int num)
333 struct thread_info *tp;
335 for (tp = thread_list; tp; tp = tp->next)
342 /* Find a thread_info by matching PTID. */
344 find_thread_ptid (ptid_t ptid)
346 struct thread_info *tp;
348 for (tp = thread_list; tp; tp = tp->next)
349 if (ptid_equal (tp->ptid, ptid))
356 * Thread iterator function.
358 * Calls a callback function once for each thread, so long as
359 * the callback function returns false. If the callback function
360 * returns true, the iteration will end and the current thread
361 * will be returned. This can be useful for implementing a
362 * search for a thread with arbitrary attributes, or for applying
363 * some operation to every thread.
365 * FIXME: some of the existing functionality, such as
366 * "Thread apply all", might be rewritten using this functionality.
370 iterate_over_threads (int (*callback) (struct thread_info *, void *),
373 struct thread_info *tp, *next;
375 for (tp = thread_list; tp; tp = next)
378 if ((*callback) (tp, data))
389 struct thread_info *tp;
391 for (tp = thread_list; tp; tp = tp->next)
398 valid_thread_id (int num)
400 struct thread_info *tp;
402 for (tp = thread_list; tp; tp = tp->next)
410 pid_to_thread_id (ptid_t ptid)
412 struct thread_info *tp;
414 for (tp = thread_list; tp; tp = tp->next)
415 if (ptid_equal (tp->ptid, ptid))
422 thread_id_to_pid (int num)
424 struct thread_info *thread = find_thread_id (num);
429 return pid_to_ptid (-1);
433 in_thread_list (ptid_t ptid)
435 struct thread_info *tp;
437 for (tp = thread_list; tp; tp = tp->next)
438 if (ptid_equal (tp->ptid, ptid))
441 return 0; /* Never heard of 'im. */
444 /* Finds the first thread of the inferior given by PID. If PID is -1,
445 return the first thread in the list. */
448 first_thread_of_process (int pid)
450 struct thread_info *tp, *ret = NULL;
452 for (tp = thread_list; tp; tp = tp->next)
453 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
454 if (ret == NULL || tp->num < ret->num)
461 any_thread_of_process (int pid)
463 struct thread_info *tp;
465 for (tp = thread_list; tp; tp = tp->next)
466 if (ptid_get_pid (tp->ptid) == pid)
473 any_live_thread_of_process (int pid)
475 struct thread_info *tp;
476 struct thread_info *tp_executing = NULL;
478 for (tp = thread_list; tp; tp = tp->next)
479 if (tp->state_ != THREAD_EXITED && ptid_get_pid (tp->ptid) == pid)
490 /* Print a list of thread ids currently known, and the total number of
491 threads. To be used from within catch_errors. */
493 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
495 struct thread_info *tp;
497 struct cleanup *cleanup_chain;
498 int current_thread = -1;
500 update_thread_list ();
502 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
504 for (tp = thread_list; tp; tp = tp->next)
506 if (tp->state_ == THREAD_EXITED)
509 if (ptid_equal (tp->ptid, inferior_ptid))
510 current_thread = tp->num;
513 ui_out_field_int (uiout, "thread-id", tp->num);
516 do_cleanups (cleanup_chain);
518 if (current_thread != -1)
519 ui_out_field_int (uiout, "current-thread-id", current_thread);
520 ui_out_field_int (uiout, "number-of-threads", num);
524 /* Official gdblib interface function to get a list of thread ids and
527 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
529 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
530 error_message, RETURN_MASK_ALL) < 0)
535 /* Return true if TP is an active thread. */
537 thread_alive (struct thread_info *tp)
539 if (tp->state_ == THREAD_EXITED)
541 if (!target_thread_alive (tp->ptid))
549 struct thread_info *tp, *next;
551 for (tp = thread_list; tp; tp = next)
554 if (!thread_alive (tp))
555 delete_thread (tp->ptid);
560 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
562 struct inferior *inf;
563 struct thread_info *tp;
565 /* It can happen that what we knew as the target inferior id
566 changes. E.g, target remote may only discover the remote process
567 pid after adding the inferior to GDB's list. */
568 inf = find_inferior_pid (ptid_get_pid (old_ptid));
569 inf->pid = ptid_get_pid (new_ptid);
571 tp = find_thread_ptid (old_ptid);
574 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
578 set_running (ptid_t ptid, int running)
580 struct thread_info *tp;
581 int all = ptid_equal (ptid, minus_one_ptid);
583 /* We try not to notify the observer if no thread has actually changed
584 the running state -- merely to reduce the number of messages to
585 frontend. Frontend is supposed to handle multiple *running just fine. */
586 if (all || ptid_is_pid (ptid))
590 for (tp = thread_list; tp; tp = tp->next)
591 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
593 if (tp->state_ == THREAD_EXITED)
595 if (running && tp->state_ == THREAD_STOPPED)
597 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
600 observer_notify_target_resumed (ptid);
606 tp = find_thread_ptid (ptid);
608 gdb_assert (tp->state_ != THREAD_EXITED);
609 if (running && tp->state_ == THREAD_STOPPED)
611 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
613 observer_notify_target_resumed (ptid);
618 is_thread_state (ptid_t ptid, enum thread_state state)
620 struct thread_info *tp;
622 tp = find_thread_ptid (ptid);
624 return tp->state_ == state;
628 is_stopped (ptid_t ptid)
630 return is_thread_state (ptid, THREAD_STOPPED);
634 is_exited (ptid_t ptid)
636 return is_thread_state (ptid, THREAD_EXITED);
640 is_running (ptid_t ptid)
642 return is_thread_state (ptid, THREAD_RUNNING);
648 struct thread_info *tp;
650 for (tp = thread_list; tp; tp = tp->next)
651 if (tp->state_ == THREAD_RUNNING)
658 is_executing (ptid_t ptid)
660 struct thread_info *tp;
662 tp = find_thread_ptid (ptid);
664 return tp->executing_;
668 set_executing (ptid_t ptid, int executing)
670 struct thread_info *tp;
671 int all = ptid_equal (ptid, minus_one_ptid);
673 if (all || ptid_is_pid (ptid))
675 for (tp = thread_list; tp; tp = tp->next)
676 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
677 tp->executing_ = executing;
681 tp = find_thread_ptid (ptid);
683 tp->executing_ = executing;
688 set_stop_requested (ptid_t ptid, int stop)
690 struct thread_info *tp;
691 int all = ptid_equal (ptid, minus_one_ptid);
693 if (all || ptid_is_pid (ptid))
695 for (tp = thread_list; tp; tp = tp->next)
696 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
697 tp->stop_requested = stop;
701 tp = find_thread_ptid (ptid);
703 tp->stop_requested = stop;
706 /* Call the stop requested observer so other components of GDB can
707 react to this request. */
709 observer_notify_thread_stop_requested (ptid);
713 finish_thread_state (ptid_t ptid)
715 struct thread_info *tp;
719 all = ptid_equal (ptid, minus_one_ptid);
721 if (all || ptid_is_pid (ptid))
723 for (tp = thread_list; tp; tp = tp->next)
725 if (tp->state_ == THREAD_EXITED)
727 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
729 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
731 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
737 tp = find_thread_ptid (ptid);
739 if (tp->state_ != THREAD_EXITED)
741 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
743 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
748 observer_notify_target_resumed (ptid);
752 finish_thread_state_cleanup (void *arg)
754 ptid_t *ptid_p = arg;
758 finish_thread_state (*ptid_p);
761 /* Prints the list of threads and their details on UIOUT.
762 This is a version of 'info_threads_command' suitable for
764 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
765 that should be printed. Otherwise, all threads are
767 If PID is not -1, only print threads from the process PID.
768 Otherwise, threads from all attached PIDs are printed.
769 If both REQUESTED_THREAD and PID are not -1, then the thread
770 is printed if it belongs to the specified process. Otherwise,
771 an error is raised. */
773 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
775 struct thread_info *tp;
777 struct cleanup *old_chain;
778 char *extra_info, *name, *target_id;
779 int current_thread = -1;
781 update_thread_list ();
782 current_ptid = inferior_ptid;
784 /* We'll be switching threads temporarily. */
785 old_chain = make_cleanup_restore_current_thread ();
787 /* For backward compatibility, we make a list for MI. A table is
788 preferable for the CLI, though, because it shows table
790 if (ui_out_is_mi_like_p (uiout))
791 make_cleanup_ui_out_list_begin_end (uiout, "threads");
796 for (tp = thread_list; tp; tp = tp->next)
798 if (!number_is_in_list (requested_threads, tp->num))
801 if (pid != -1 && PIDGET (tp->ptid) != pid)
804 if (tp->state_ == THREAD_EXITED)
812 if (requested_threads == NULL || *requested_threads == '\0')
813 ui_out_message (uiout, 0, _("No threads.\n"));
815 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
817 do_cleanups (old_chain);
821 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
823 ui_out_table_header (uiout, 1, ui_left, "current", "");
824 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
825 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
826 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
827 ui_out_table_body (uiout);
830 for (tp = thread_list; tp; tp = tp->next)
832 struct cleanup *chain2;
835 if (!number_is_in_list (requested_threads, tp->num))
838 if (pid != -1 && PIDGET (tp->ptid) != pid)
840 if (requested_threads != NULL && *requested_threads != '\0')
841 error (_("Requested thread not found in requested process"));
845 if (ptid_equal (tp->ptid, current_ptid))
846 current_thread = tp->num;
848 if (tp->state_ == THREAD_EXITED)
851 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
853 if (ui_out_is_mi_like_p (uiout))
856 if (ptid_equal (tp->ptid, current_ptid))
857 ui_out_text (uiout, "* ");
859 ui_out_text (uiout, " ");
863 if (ptid_equal (tp->ptid, current_ptid))
864 ui_out_field_string (uiout, "current", "*");
866 ui_out_field_skip (uiout, "current");
869 ui_out_field_int (uiout, "id", tp->num);
871 /* For the CLI, we stuff everything into the target-id field.
872 This is a gross hack to make the output come out looking
873 correct. The underlying problem here is that ui-out has no
874 way to specify that a field's space allocation should be
875 shared by several fields. For MI, we do the right thing
878 target_id = target_pid_to_str (tp->ptid);
879 extra_info = target_extra_thread_info (tp);
880 name = tp->name ? tp->name : target_thread_name (tp);
882 if (ui_out_is_mi_like_p (uiout))
884 ui_out_field_string (uiout, "target-id", target_id);
886 ui_out_field_string (uiout, "details", extra_info);
888 ui_out_field_string (uiout, "name", name);
892 struct cleanup *str_cleanup;
895 if (extra_info && name)
896 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
899 contents = xstrprintf ("%s (%s)", target_id, extra_info);
901 contents = xstrprintf ("%s \"%s\"", target_id, name);
903 contents = xstrdup (target_id);
904 str_cleanup = make_cleanup (xfree, contents);
906 ui_out_field_string (uiout, "target-id", contents);
907 do_cleanups (str_cleanup);
910 if (tp->state_ == THREAD_RUNNING)
911 ui_out_text (uiout, "(running)\n");
914 /* The switch below puts us at the top of the stack (leaf
916 switch_to_thread (tp->ptid);
917 print_stack_frame (get_selected_frame (NULL),
918 /* For MI output, print frame level. */
919 ui_out_is_mi_like_p (uiout),
923 if (ui_out_is_mi_like_p (uiout))
925 char *state = "stopped";
927 if (tp->state_ == THREAD_RUNNING)
929 ui_out_field_string (uiout, "state", state);
932 core = target_core_of_thread (tp->ptid);
933 if (ui_out_is_mi_like_p (uiout) && core != -1)
934 ui_out_field_int (uiout, "core", core);
936 do_cleanups (chain2);
939 /* Restores the current thread and the frame selected before
940 the "info threads" command. */
941 do_cleanups (old_chain);
943 if (pid == -1 && requested_threads == NULL)
945 gdb_assert (current_thread != -1
947 || ptid_equal (inferior_ptid, null_ptid));
948 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
949 ui_out_field_int (uiout, "current-thread-id", current_thread);
951 if (current_thread != -1 && is_exited (current_ptid))
952 ui_out_message (uiout, 0, "\n\
953 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
956 && current_thread == -1
957 && ptid_equal (current_ptid, null_ptid))
958 ui_out_message (uiout, 0, "\n\
959 No selected thread. See `help thread'.\n");
963 /* Print information about currently known threads
965 Optional ARG is a thread id, or list of thread ids.
967 Note: this has the drawback that it _really_ switches
968 threads, which frees the frame cache. A no-side
969 effects info-threads command would be nicer. */
972 info_threads_command (char *arg, int from_tty)
974 print_thread_info (uiout, arg, -1);
977 /* Switch from one thread to another. */
980 switch_to_thread (ptid_t ptid)
982 /* Switch the program space as well, if we can infer it from the now
983 current thread. Otherwise, it's up to the caller to select the
985 if (!ptid_equal (ptid, null_ptid))
987 struct inferior *inf;
989 inf = find_inferior_pid (ptid_get_pid (ptid));
990 gdb_assert (inf != NULL);
991 set_current_program_space (inf->pspace);
992 set_current_inferior (inf);
995 if (ptid_equal (ptid, inferior_ptid))
998 inferior_ptid = ptid;
999 reinit_frame_cache ();
1000 registers_changed ();
1002 /* We don't check for is_stopped, because we're called at times
1003 while in the TARGET_RUNNING state, e.g., while handling an
1005 if (!ptid_equal (inferior_ptid, null_ptid)
1006 && !is_exited (ptid)
1007 && !is_executing (ptid))
1008 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1010 stop_pc = ~(CORE_ADDR) 0;
1014 restore_current_thread (ptid_t ptid)
1016 switch_to_thread (ptid);
1020 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1022 struct frame_info *frame = NULL;
1025 /* This means there was no selected frame. */
1026 if (frame_level == -1)
1028 select_frame (NULL);
1032 gdb_assert (frame_level >= 0);
1034 /* Restore by level first, check if the frame id is the same as
1035 expected. If that fails, try restoring by frame id. If that
1036 fails, nothing to do, just warn the user. */
1038 count = frame_level;
1039 frame = find_relative_frame (get_current_frame (), &count);
1042 /* The frame ids must match - either both valid or both outer_frame_id.
1043 The latter case is not failsafe, but since it's highly unlikely
1044 the search by level finds the wrong frame, it's 99.9(9)% of
1045 the time (for all practical purposes) safe. */
1046 && frame_id_eq (get_frame_id (frame), a_frame_id))
1048 /* Cool, all is fine. */
1049 select_frame (frame);
1053 frame = frame_find_by_id (a_frame_id);
1056 /* Cool, refound it. */
1057 select_frame (frame);
1061 /* Nothing else to do, the frame layout really changed. Select the
1062 innermost stack frame. */
1063 select_frame (get_current_frame ());
1065 /* Warn the user. */
1066 if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
1068 warning (_("Couldn't restore frame #%d in "
1069 "current thread, at reparsed frame #0\n"),
1071 /* For MI, we should probably have a notification about
1072 current frame change. But this error is not very
1073 likely, so don't bother for now. */
1074 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
1078 struct current_thread_cleanup
1080 ptid_t inferior_ptid;
1081 struct frame_id selected_frame_id;
1082 int selected_frame_level;
1088 do_restore_current_thread_cleanup (void *arg)
1090 struct thread_info *tp;
1091 struct current_thread_cleanup *old = arg;
1093 tp = find_thread_ptid (old->inferior_ptid);
1095 /* If the previously selected thread belonged to a process that has
1096 in the mean time been deleted (due to normal exit, detach, etc.),
1097 then don't revert back to it, but instead simply drop back to no
1100 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
1101 restore_current_thread (old->inferior_ptid);
1104 restore_current_thread (null_ptid);
1105 set_current_inferior (find_inferior_id (old->inf_id));
1108 /* The running state of the originally selected thread may have
1109 changed, so we have to recheck it here. */
1110 if (!ptid_equal (inferior_ptid, null_ptid)
1112 && is_stopped (inferior_ptid)
1113 && target_has_registers
1115 && target_has_memory)
1116 restore_selected_frame (old->selected_frame_id,
1117 old->selected_frame_level);
1121 restore_current_thread_cleanup_dtor (void *arg)
1123 struct current_thread_cleanup *old = arg;
1124 struct thread_info *tp;
1126 tp = find_thread_ptid (old->inferior_ptid);
1133 make_cleanup_restore_current_thread (void)
1135 struct thread_info *tp;
1136 struct frame_info *frame;
1137 struct current_thread_cleanup *old;
1139 old = xmalloc (sizeof (struct current_thread_cleanup));
1140 old->inferior_ptid = inferior_ptid;
1141 old->inf_id = current_inferior ()->num;
1143 if (!ptid_equal (inferior_ptid, null_ptid))
1145 old->was_stopped = is_stopped (inferior_ptid);
1146 if (old->was_stopped
1147 && target_has_registers
1149 && target_has_memory)
1151 /* When processing internal events, there might not be a
1152 selected frame. If we naively call get_selected_frame
1153 here, then we can end up reading debuginfo for the
1154 current frame, but we don't generally need the debuginfo
1156 frame = get_selected_frame_if_set ();
1161 old->selected_frame_id = get_frame_id (frame);
1162 old->selected_frame_level = frame_relative_level (frame);
1164 tp = find_thread_ptid (inferior_ptid);
1169 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1170 restore_current_thread_cleanup_dtor);
1173 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1174 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1175 of two numbers seperated by a hyphen. Examples:
1177 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1178 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1179 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1182 thread_apply_all_command (char *cmd, int from_tty)
1184 struct thread_info *tp;
1185 struct cleanup *old_chain;
1188 if (cmd == NULL || *cmd == '\000')
1189 error (_("Please specify a command following the thread ID list"));
1191 update_thread_list ();
1193 old_chain = make_cleanup_restore_current_thread ();
1195 /* Save a copy of the command in case it is clobbered by
1197 saved_cmd = xstrdup (cmd);
1198 make_cleanup (xfree, saved_cmd);
1199 for (tp = thread_list; tp; tp = tp->next)
1200 if (thread_alive (tp))
1202 switch_to_thread (tp->ptid);
1204 printf_filtered (_("\nThread %d (%s):\n"),
1205 tp->num, target_pid_to_str (inferior_ptid));
1206 execute_command (cmd, from_tty);
1207 strcpy (cmd, saved_cmd); /* Restore exact command used
1211 do_cleanups (old_chain);
1215 thread_apply_command (char *tidlist, int from_tty)
1218 struct cleanup *old_chain;
1220 struct get_number_or_range_state state;
1222 if (tidlist == NULL || *tidlist == '\000')
1223 error (_("Please specify a thread ID list"));
1225 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1228 error (_("Please specify a command following the thread ID list"));
1230 /* Save a copy of the command in case it is clobbered by
1232 saved_cmd = xstrdup (cmd);
1233 old_chain = make_cleanup (xfree, saved_cmd);
1235 init_number_or_range (&state, tidlist);
1236 while (!state.finished && state.string < cmd)
1238 struct thread_info *tp;
1242 start = get_number_or_range (&state);
1244 make_cleanup_restore_current_thread ();
1246 tp = find_thread_id (start);
1249 warning (_("Unknown thread %d."), start);
1250 else if (!thread_alive (tp))
1251 warning (_("Thread %d has terminated."), start);
1254 switch_to_thread (tp->ptid);
1256 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1257 target_pid_to_str (inferior_ptid));
1258 execute_command (cmd, from_tty);
1260 /* Restore exact command used previously. */
1261 strcpy (cmd, saved_cmd);
1265 do_cleanups (old_chain);
1268 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1269 if prefix of arg is `apply'. */
1272 thread_command (char *tidstr, int from_tty)
1276 if (ptid_equal (inferior_ptid, null_ptid))
1277 error (_("No thread selected"));
1279 if (target_has_stack)
1281 if (is_exited (inferior_ptid))
1282 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1283 pid_to_thread_id (inferior_ptid),
1284 target_pid_to_str (inferior_ptid));
1286 printf_filtered (_("[Current thread is %d (%s)]\n"),
1287 pid_to_thread_id (inferior_ptid),
1288 target_pid_to_str (inferior_ptid));
1291 error (_("No stack."));
1295 gdb_thread_select (uiout, tidstr, NULL);
1298 /* Implementation of `thread name'. */
1301 thread_name_command (char *arg, int from_tty)
1303 struct thread_info *info;
1305 if (ptid_equal (inferior_ptid, null_ptid))
1306 error (_("No thread selected"));
1308 while (arg && isspace (*arg))
1311 info = inferior_thread ();
1313 info->name = arg ? xstrdup (arg) : NULL;
1316 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1319 thread_find_command (char *arg, int from_tty)
1321 struct thread_info *tp;
1323 unsigned long match = 0;
1325 if (arg == NULL || *arg == '\0')
1326 error (_("Command requires an argument."));
1328 tmp = re_comp (arg);
1330 error (_("Invalid regexp (%s): %s"), tmp, arg);
1332 update_thread_list ();
1333 for (tp = thread_list; tp; tp = tp->next)
1335 if (tp->name != NULL && re_exec (tp->name))
1337 printf_filtered (_("Thread %d has name '%s'\n"),
1342 tmp = target_thread_name (tp);
1343 if (tmp != NULL && re_exec (tmp))
1345 printf_filtered (_("Thread %d has target name '%s'\n"),
1350 tmp = target_pid_to_str (tp->ptid);
1351 if (tmp != NULL && re_exec (tmp))
1353 printf_filtered (_("Thread %d has target id '%s'\n"),
1358 tmp = target_extra_thread_info (tp);
1359 if (tmp != NULL && re_exec (tmp))
1361 printf_filtered (_("Thread %d has extra info '%s'\n"),
1367 printf_filtered (_("No threads match '%s'\n"), arg);
1370 /* Print notices when new threads are attached and detached. */
1371 int print_thread_events = 1;
1373 show_print_thread_events (struct ui_file *file, int from_tty,
1374 struct cmd_list_element *c, const char *value)
1376 fprintf_filtered (file,
1377 _("Printing of thread events is %s.\n"),
1382 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1385 struct thread_info *tp;
1387 num = value_as_long (parse_and_eval (tidstr));
1389 tp = find_thread_id (num);
1392 error (_("Thread ID %d not known."), num);
1394 if (!thread_alive (tp))
1395 error (_("Thread ID %d has terminated."), num);
1397 switch_to_thread (tp->ptid);
1399 annotate_thread_changed ();
1401 ui_out_text (uiout, "[Switching to thread ");
1402 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1403 ui_out_text (uiout, " (");
1404 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1405 ui_out_text (uiout, ")]");
1407 /* Note that we can't reach this with an exited thread, due to the
1408 thread_alive check above. */
1409 if (tp->state_ == THREAD_RUNNING)
1410 ui_out_text (uiout, "(running)\n");
1413 ui_out_text (uiout, "\n");
1414 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1417 /* Since the current thread may have changed, see if there is any
1418 exited thread we can now delete. */
1425 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1427 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1428 error_message, RETURN_MASK_ALL) < 0)
1434 update_thread_list (void)
1437 target_find_new_threads ();
1440 /* Return a new value for the selected thread's id. Return a value of 0 if
1441 no thread is selected, or no threads exist. */
1443 static struct value *
1444 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var)
1446 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1448 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1449 (tp ? tp->num : 0));
1452 /* Commands with a prefix of `thread'. */
1453 struct cmd_list_element *thread_cmd_list = NULL;
1456 _initialize_thread (void)
1458 static struct cmd_list_element *thread_apply_list = NULL;
1460 add_info ("threads", info_threads_command,
1461 _("Display currently known threads.\n\
1462 Usage: info threads [ID]...\n\
1463 Optional arguments are thread IDs with spaces between.\n\
1464 If no arguments, all threads are displayed."));
1466 add_prefix_cmd ("thread", class_run, thread_command, _("\
1467 Use this command to switch between threads.\n\
1468 The new thread ID must be currently known."),
1469 &thread_cmd_list, "thread ", 1, &cmdlist);
1471 add_prefix_cmd ("apply", class_run, thread_apply_command,
1472 _("Apply a command to a list of threads."),
1473 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1475 add_cmd ("all", class_run, thread_apply_all_command,
1476 _("Apply a command to all threads."), &thread_apply_list);
1478 add_cmd ("name", class_run, thread_name_command,
1479 _("Set the current thread's name.\n\
1480 Usage: thread name [NAME]\n\
1481 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1483 add_cmd ("find", class_run, thread_find_command, _("\
1484 Find threads that match a regular expression.\n\
1485 Usage: thread find REGEXP\n\
1486 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1490 add_com_alias ("t", "thread", class_run, 1);
1492 add_setshow_boolean_cmd ("thread-events", no_class,
1493 &print_thread_events, _("\
1494 Set printing of thread events (such as thread start and exit)."), _("\
1495 Show printing of thread events (such as thread start and exit)."), NULL,
1497 show_print_thread_events,
1498 &setprintlist, &showprintlist);
1500 create_internalvar_type_lazy ("_thread", thread_id_make_value);