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 Free Software Foundation, Inc.
6 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdbthread.h"
31 #include "exceptions.h"
36 #include "gdb_string.h"
39 #include <sys/types.h>
45 /* Definition of struct thread_info exported to gdbthread.h */
47 /* Prototypes for exported functions. */
49 void _initialize_thread (void);
51 /* Prototypes for local functions. */
53 static struct thread_info *thread_list = NULL;
54 static int highest_thread_num;
56 static struct thread_info *find_thread_id (int num);
58 static void thread_command (char *tidstr, int from_tty);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info *);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t);
64 static void prune_threads (void);
66 static int main_thread_running = 0;
67 static int main_thread_executing = 0;
70 delete_step_resume_breakpoint (void *arg)
72 struct breakpoint **breakpointp = (struct breakpoint **) arg;
73 struct thread_info *tp;
75 if (*breakpointp != NULL)
77 delete_breakpoint (*breakpointp);
78 for (tp = thread_list; tp; tp = tp->next)
79 if (tp->step_resume_breakpoint == *breakpointp)
80 tp->step_resume_breakpoint = NULL;
87 free_thread (struct thread_info *tp)
89 /* NOTE: this will take care of any left-over step_resume breakpoints,
90 but not any user-specified thread-specific breakpoints. We can not
91 delete the breakpoint straight-off, because the inferior might not
92 be stopped at the moment. */
93 if (tp->step_resume_breakpoint)
94 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
96 /* FIXME: do I ever need to call the back-end to give it a
97 chance at this private data before deleting the thread? */
105 init_thread_list (void)
107 struct thread_info *tp, *tpnext;
109 highest_thread_num = 0;
110 main_thread_running = 0;
111 main_thread_executing = 0;
116 for (tp = thread_list; tp; tp = tpnext)
126 add_thread_silent (ptid_t ptid)
128 struct thread_info *tp;
130 tp = (struct thread_info *) xmalloc (sizeof (*tp));
131 memset (tp, 0, sizeof (*tp));
133 tp->num = ++highest_thread_num;
134 tp->next = thread_list;
137 observer_notify_new_thread (tp);
143 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
145 struct thread_info *result = add_thread_silent (ptid);
147 result->private = private;
149 if (print_thread_events)
150 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
152 annotate_new_thread ();
157 add_thread (ptid_t ptid)
159 return add_thread_with_info (ptid, NULL);
162 /* Delete thread PTID. If SILENT, don't notify the observer of this
165 delete_thread_1 (ptid_t ptid, int silent)
167 struct thread_info *tp, *tpprev;
171 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
172 if (ptid_equal (tp->ptid, ptid))
179 tpprev->next = tp->next;
181 thread_list = tp->next;
184 observer_notify_thread_exit (tp);
190 delete_thread (ptid_t ptid)
192 delete_thread_1 (ptid, 0 /* not silent */);
196 delete_thread_silent (ptid_t ptid)
198 delete_thread_1 (ptid, 1 /* silent */);
201 static struct thread_info *
202 find_thread_id (int num)
204 struct thread_info *tp;
206 for (tp = thread_list; tp; tp = tp->next)
213 /* Find a thread_info by matching PTID. */
215 find_thread_pid (ptid_t ptid)
217 struct thread_info *tp;
219 for (tp = thread_list; tp; tp = tp->next)
220 if (ptid_equal (tp->ptid, ptid))
227 * Thread iterator function.
229 * Calls a callback function once for each thread, so long as
230 * the callback function returns false. If the callback function
231 * returns true, the iteration will end and the current thread
232 * will be returned. This can be useful for implementing a
233 * search for a thread with arbitrary attributes, or for applying
234 * some operation to every thread.
236 * FIXME: some of the existing functionality, such as
237 * "Thread apply all", might be rewritten using this functionality.
241 iterate_over_threads (int (*callback) (struct thread_info *, void *),
244 struct thread_info *tp;
246 for (tp = thread_list; tp; tp = tp->next)
247 if ((*callback) (tp, data))
257 struct thread_info *tp;
259 for (tp = thread_list; tp; tp = tp->next)
266 valid_thread_id (int num)
268 struct thread_info *tp;
270 for (tp = thread_list; tp; tp = tp->next)
278 pid_to_thread_id (ptid_t ptid)
280 struct thread_info *tp;
282 for (tp = thread_list; tp; tp = tp->next)
283 if (ptid_equal (tp->ptid, ptid))
290 thread_id_to_pid (int num)
292 struct thread_info *thread = find_thread_id (num);
296 return pid_to_ptid (-1);
300 in_thread_list (ptid_t ptid)
302 struct thread_info *tp;
304 for (tp = thread_list; tp; tp = tp->next)
305 if (ptid_equal (tp->ptid, ptid))
308 return 0; /* Never heard of 'im */
311 /* Print a list of thread ids currently known, and the total number of
312 threads. To be used from within catch_errors. */
314 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
316 struct thread_info *tp;
318 struct cleanup *cleanup_chain;
321 target_find_new_threads ();
323 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
325 for (tp = thread_list; tp; tp = tp->next)
328 ui_out_field_int (uiout, "thread-id", tp->num);
331 do_cleanups (cleanup_chain);
332 ui_out_field_int (uiout, "number-of-threads", num);
336 /* Official gdblib interface function to get a list of thread ids and
339 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
341 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
342 error_message, RETURN_MASK_ALL) < 0)
347 /* Load infrun state for the thread PID. */
350 load_infrun_state (ptid_t ptid,
353 struct breakpoint **step_resume_breakpoint,
354 CORE_ADDR *step_range_start,
355 CORE_ADDR *step_range_end,
356 struct frame_id *step_frame_id,
357 int *stepping_over_breakpoint,
358 int *stepping_through_solib_after_catch,
359 bpstat *stepping_through_solib_catchpoints,
361 struct symtab **current_symtab)
363 struct thread_info *tp;
365 /* If we can't find the thread, then we're debugging a single threaded
366 process. No need to do anything in that case. */
367 tp = find_thread_id (pid_to_thread_id (ptid));
371 *prev_pc = tp->prev_pc;
372 *trap_expected = tp->trap_expected;
373 *step_resume_breakpoint = tp->step_resume_breakpoint;
374 *step_range_start = tp->step_range_start;
375 *step_range_end = tp->step_range_end;
376 *step_frame_id = tp->step_frame_id;
377 *stepping_over_breakpoint = tp->stepping_over_breakpoint;
378 *stepping_through_solib_after_catch =
379 tp->stepping_through_solib_after_catch;
380 *stepping_through_solib_catchpoints =
381 tp->stepping_through_solib_catchpoints;
382 *current_line = tp->current_line;
383 *current_symtab = tp->current_symtab;
386 /* Save infrun state for the thread PID. */
389 save_infrun_state (ptid_t ptid,
392 struct breakpoint *step_resume_breakpoint,
393 CORE_ADDR step_range_start,
394 CORE_ADDR step_range_end,
395 const struct frame_id *step_frame_id,
396 int stepping_over_breakpoint,
397 int stepping_through_solib_after_catch,
398 bpstat stepping_through_solib_catchpoints,
400 struct symtab *current_symtab)
402 struct thread_info *tp;
404 /* If we can't find the thread, then we're debugging a single-threaded
405 process. Nothing to do in that case. */
406 tp = find_thread_id (pid_to_thread_id (ptid));
410 tp->prev_pc = prev_pc;
411 tp->trap_expected = trap_expected;
412 tp->step_resume_breakpoint = step_resume_breakpoint;
413 tp->step_range_start = step_range_start;
414 tp->step_range_end = step_range_end;
415 tp->step_frame_id = (*step_frame_id);
416 tp->stepping_over_breakpoint = stepping_over_breakpoint;
417 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
418 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
419 tp->current_line = current_line;
420 tp->current_symtab = current_symtab;
423 /* Return true if TP is an active thread. */
425 thread_alive (struct thread_info *tp)
427 if (PIDGET (tp->ptid) == -1)
429 if (!target_thread_alive (tp->ptid))
431 tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
440 struct thread_info *tp, *next;
442 for (tp = thread_list; tp; tp = next)
445 if (!thread_alive (tp))
446 delete_thread (tp->ptid);
451 set_running (ptid_t ptid, int running)
453 struct thread_info *tp;
457 /* This is one of the targets that does not add main
458 thread to the thread list. Just use a single
459 global flag to indicate that a thread is running.
461 This problem is unique to ST programs. For MT programs,
462 the main thread is always present in the thread list. If it's
463 not, the first call to context_switch will mess up GDB internal
465 if (running && !main_thread_running && !suppress_resume_observer)
466 observer_notify_target_resumed (ptid);
467 main_thread_running = running;
471 /* We try not to notify the observer if no thread has actually changed
472 the running state -- merely to reduce the number of messages to
473 frontend. Frontend is supposed to handle multiple *running just fine. */
474 if (PIDGET (ptid) == -1)
477 for (tp = thread_list; tp; tp = tp->next)
479 if (running && !tp->running_)
481 tp->running_ = running;
483 if (any_started && !suppress_resume_observer)
484 observer_notify_target_resumed (ptid);
488 tp = find_thread_pid (ptid);
490 if (running && !tp->running_ && !suppress_resume_observer)
491 observer_notify_target_resumed (ptid);
492 tp->running_ = running;
497 is_running (ptid_t ptid)
499 struct thread_info *tp;
501 if (!target_has_execution)
505 return main_thread_running;
507 tp = find_thread_pid (ptid);
515 struct thread_info *tp;
517 if (!target_has_execution)
521 return main_thread_running;
523 for (tp = thread_list; tp; tp = tp->next)
531 is_executing (ptid_t ptid)
533 struct thread_info *tp;
535 if (!target_has_execution)
539 return main_thread_executing;
541 tp = find_thread_pid (ptid);
543 return tp->executing_;
547 set_executing (ptid_t ptid, int executing)
549 struct thread_info *tp;
553 /* This target does not add the main thread to the thread list.
554 Use a global flag to indicate that the thread is
556 main_thread_executing = executing;
560 if (PIDGET (ptid) == -1)
562 for (tp = thread_list; tp; tp = tp->next)
563 tp->executing_ = executing;
567 tp = find_thread_pid (ptid);
569 tp->executing_ = executing;
573 /* Prints the list of threads and their details on UIOUT.
574 This is a version of 'info_thread_command' suitable for
576 If REQESTED_THREAD is not -1, it's the GDB id of the thread
577 that should be printed. Otherwise, all threads are
580 print_thread_info (struct ui_out *uiout, int requested_thread)
582 struct thread_info *tp;
584 struct frame_info *cur_frame;
585 struct cleanup *old_chain;
586 struct frame_id saved_frame_id;
588 int current_thread = -1;
590 /* Backup current thread and selected frame. */
591 saved_frame_id = get_frame_id (get_selected_frame (NULL));
592 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
594 make_cleanup_ui_out_list_begin_end (uiout, "threads");
597 target_find_new_threads ();
598 current_ptid = inferior_ptid;
599 for (tp = thread_list; tp; tp = tp->next)
601 struct cleanup *chain2;
603 if (requested_thread != -1 && tp->num != requested_thread)
606 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
608 if (ptid_equal (tp->ptid, current_ptid))
610 current_thread = tp->num;
611 ui_out_text (uiout, "* ");
614 ui_out_text (uiout, " ");
616 ui_out_field_int (uiout, "id", tp->num);
617 ui_out_text (uiout, " ");
618 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
620 extra_info = target_extra_thread_info (tp);
623 ui_out_text (uiout, " (");
624 ui_out_field_string (uiout, "details", extra_info);
625 ui_out_text (uiout, ")");
627 ui_out_text (uiout, " ");
628 /* That switch put us at the top of the stack (leaf frame). */
629 switch_to_thread (tp->ptid);
630 print_stack_frame (get_selected_frame (NULL),
631 /* For MI output, print frame level. */
632 ui_out_is_mi_like_p (uiout),
635 do_cleanups (chain2);
638 /* Restores the current thread and the frame selected before
639 the "info threads" command. */
640 do_cleanups (old_chain);
642 if (requested_thread == -1)
644 gdb_assert (current_thread != -1 || !thread_list);
645 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
646 ui_out_field_int (uiout, "current-thread-id", current_thread);
649 /* If case we were not able to find the original frame, print the
650 new selected frame. */
651 if (frame_find_by_id (saved_frame_id) == NULL)
653 warning (_("Couldn't restore frame in current thread, at frame 0"));
654 /* For MI, we should probably have a notification about
655 current frame change. But this error is not very likely, so
656 don't bother for now. */
657 if (!ui_out_is_mi_like_p (uiout))
658 print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
663 /* Print information about currently known threads
665 * Note: this has the drawback that it _really_ switches
666 * threads, which frees the frame cache. A no-side
667 * effects info-threads command would be nicer.
671 info_threads_command (char *arg, int from_tty)
673 print_thread_info (uiout, -1);
676 /* Switch from one thread to another. */
679 switch_to_thread (ptid_t ptid)
681 if (ptid_equal (ptid, inferior_ptid))
684 inferior_ptid = ptid;
685 reinit_frame_cache ();
686 registers_changed ();
687 stop_pc = read_pc ();
691 restore_current_thread (ptid_t ptid)
693 if (!ptid_equal (ptid, inferior_ptid))
695 switch_to_thread (ptid);
700 restore_selected_frame (struct frame_id a_frame_id)
702 struct frame_info *selected_frame_info = NULL;
704 if (frame_id_eq (a_frame_id, null_frame_id))
707 if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL)
709 select_frame (selected_frame_info);
713 struct current_thread_cleanup
715 ptid_t inferior_ptid;
716 struct frame_id selected_frame_id;
720 do_restore_current_thread_cleanup (void *arg)
722 struct current_thread_cleanup *old = arg;
723 restore_current_thread (old->inferior_ptid);
724 restore_selected_frame (old->selected_frame_id);
729 make_cleanup_restore_current_thread (ptid_t inferior_ptid,
730 struct frame_id a_frame_id)
732 struct current_thread_cleanup *old
733 = xmalloc (sizeof (struct current_thread_cleanup));
734 old->inferior_ptid = inferior_ptid;
735 old->selected_frame_id = a_frame_id;
736 return make_cleanup (do_restore_current_thread_cleanup, old);
739 /* Apply a GDB command to a list of threads. List syntax is a whitespace
740 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
741 of two numbers seperated by a hyphen. Examples:
743 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
744 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
745 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
749 thread_apply_all_command (char *cmd, int from_tty)
751 struct thread_info *tp;
752 struct cleanup *old_chain;
753 struct cleanup *saved_cmd_cleanup_chain;
755 struct frame_id saved_frame_id;
757 int thread_has_changed = 0;
759 if (cmd == NULL || *cmd == '\000')
760 error (_("Please specify a command following the thread ID list"));
762 current_ptid = inferior_ptid;
763 saved_frame_id = get_frame_id (get_selected_frame (NULL));
764 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
766 /* It is safe to update the thread list now, before
767 traversing it for "thread apply all". MVS */
768 target_find_new_threads ();
770 /* Save a copy of the command in case it is clobbered by
772 saved_cmd = xstrdup (cmd);
773 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
774 for (tp = thread_list; tp; tp = tp->next)
775 if (thread_alive (tp))
777 switch_to_thread (tp->ptid);
778 printf_filtered (_("\nThread %d (%s):\n"),
779 tp->num, target_tid_to_str (inferior_ptid));
780 execute_command (cmd, from_tty);
781 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
784 if (!ptid_equal (current_ptid, inferior_ptid))
785 thread_has_changed = 1;
787 do_cleanups (saved_cmd_cleanup_chain);
788 do_cleanups (old_chain);
789 /* Print stack frame only if we changed thread. */
790 if (thread_has_changed)
791 print_stack_frame (get_current_frame (), 1, SRC_LINE);
796 thread_apply_command (char *tidlist, int from_tty)
800 struct cleanup *old_chain;
801 struct cleanup *saved_cmd_cleanup_chain;
803 struct frame_id saved_frame_id;
805 int thread_has_changed = 0;
807 if (tidlist == NULL || *tidlist == '\000')
808 error (_("Please specify a thread ID list"));
810 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
813 error (_("Please specify a command following the thread ID list"));
815 current_ptid = inferior_ptid;
816 saved_frame_id = get_frame_id (get_selected_frame (NULL));
817 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
819 /* Save a copy of the command in case it is clobbered by
821 saved_cmd = xstrdup (cmd);
822 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
823 while (tidlist < cmd)
825 struct thread_info *tp;
828 start = strtol (tidlist, &p, 10);
830 error (_("Error parsing %s"), tidlist);
833 while (*tidlist == ' ' || *tidlist == '\t')
836 if (*tidlist == '-') /* Got a range of IDs? */
838 tidlist++; /* Skip the - */
839 end = strtol (tidlist, &p, 10);
841 error (_("Error parsing %s"), tidlist);
844 while (*tidlist == ' ' || *tidlist == '\t')
850 for (; start <= end; start++)
852 tp = find_thread_id (start);
855 warning (_("Unknown thread %d."), start);
856 else if (!thread_alive (tp))
857 warning (_("Thread %d has terminated."), start);
860 switch_to_thread (tp->ptid);
861 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
862 target_tid_to_str (inferior_ptid));
863 execute_command (cmd, from_tty);
864 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
869 if (!ptid_equal (current_ptid, inferior_ptid))
870 thread_has_changed = 1;
872 do_cleanups (saved_cmd_cleanup_chain);
873 do_cleanups (old_chain);
874 /* Print stack frame only if we changed thread. */
875 if (thread_has_changed)
876 print_stack_frame (get_current_frame (), 1, SRC_LINE);
879 /* Switch to the specified thread. Will dispatch off to thread_apply_command
880 if prefix of arg is `apply'. */
883 thread_command (char *tidstr, int from_tty)
887 /* Don't generate an error, just say which thread is current. */
888 if (target_has_stack)
889 printf_filtered (_("[Current thread is %d (%s)]\n"),
890 pid_to_thread_id (inferior_ptid),
891 target_tid_to_str (inferior_ptid));
893 error (_("No stack."));
897 annotate_thread_changed ();
898 gdb_thread_select (uiout, tidstr, NULL);
901 /* Print notices when new threads are attached and detached. */
902 int print_thread_events = 1;
904 show_print_thread_events (struct ui_file *file, int from_tty,
905 struct cmd_list_element *c, const char *value)
907 fprintf_filtered (file, _("\
908 Printing of thread events is %s.\n"),
913 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
916 struct thread_info *tp;
918 num = value_as_long (parse_and_eval (tidstr));
920 tp = find_thread_id (num);
923 error (_("Thread ID %d not known."), num);
925 if (!thread_alive (tp))
926 error (_("Thread ID %d has terminated."), num);
928 switch_to_thread (tp->ptid);
930 ui_out_text (uiout, "[Switching to thread ");
931 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
932 ui_out_text (uiout, " (");
933 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
934 ui_out_text (uiout, ")]");
936 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
941 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
943 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
944 error_message, RETURN_MASK_ALL) < 0)
949 /* Commands with a prefix of `thread'. */
950 struct cmd_list_element *thread_cmd_list = NULL;
953 _initialize_thread (void)
955 static struct cmd_list_element *thread_apply_list = NULL;
957 add_info ("threads", info_threads_command,
958 _("IDs of currently known threads."));
960 add_prefix_cmd ("thread", class_run, thread_command, _("\
961 Use this command to switch between threads.\n\
962 The new thread ID must be currently known."),
963 &thread_cmd_list, "thread ", 1, &cmdlist);
965 add_prefix_cmd ("apply", class_run, thread_apply_command,
966 _("Apply a command to a list of threads."),
967 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
969 add_cmd ("all", class_run, thread_apply_all_command,
970 _("Apply a command to all threads."), &thread_apply_list);
973 add_com_alias ("t", "thread", class_run, 1);
975 add_setshow_boolean_cmd ("thread-events", no_class,
976 &print_thread_events, _("\
977 Set printing of thread events (such as thread start and exit)."), _("\
978 Show printing of thread events (such as thread start and exit)."), NULL,
980 show_print_thread_events,
981 &setprintlist, &showprintlist);