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);
67 delete_step_resume_breakpoint (void *arg
)
69 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
70 struct thread_info
*tp
;
72 if (*breakpointp
!= NULL
)
74 delete_breakpoint (*breakpointp
);
75 for (tp
= thread_list
; tp
; tp
= tp
->next
)
76 if (tp
->step_resume_breakpoint
== *breakpointp
)
77 tp
->step_resume_breakpoint
= NULL
;
84 free_thread (struct thread_info
*tp
)
86 /* NOTE: this will take care of any left-over step_resume breakpoints,
87 but not any user-specified thread-specific breakpoints. We can not
88 delete the breakpoint straight-off, because the inferior might not
89 be stopped at the moment. */
90 if (tp
->step_resume_breakpoint
)
91 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
93 /* FIXME: do I ever need to call the back-end to give it a
94 chance at this private data before deleting the thread? */
102 init_thread_list (void)
104 struct thread_info
*tp
, *tpnext
;
106 highest_thread_num
= 0;
110 for (tp
= thread_list
; tp
; tp
= tpnext
)
120 add_thread_silent (ptid_t ptid
)
122 struct thread_info
*tp
;
124 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
125 memset (tp
, 0, sizeof (*tp
));
127 tp
->num
= ++highest_thread_num
;
128 tp
->next
= thread_list
;
131 observer_notify_new_thread (tp
);
137 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
139 struct thread_info
*result
= add_thread_silent (ptid
);
141 result
->private = private;
143 if (print_thread_events
)
144 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
146 annotate_new_thread ();
151 add_thread (ptid_t ptid
)
153 return add_thread_with_info (ptid
, NULL
);
156 /* Delete thread PTID. If SILENT, don't notify the observer of this
159 delete_thread_1 (ptid_t ptid
, int silent
)
161 struct thread_info
*tp
, *tpprev
;
165 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
166 if (ptid_equal (tp
->ptid
, ptid
))
173 tpprev
->next
= tp
->next
;
175 thread_list
= tp
->next
;
178 observer_notify_thread_exit (tp
);
184 delete_thread (ptid_t ptid
)
186 delete_thread_1 (ptid
, 0 /* not silent */);
190 delete_thread_silent (ptid_t ptid
)
192 delete_thread_1 (ptid
, 1 /* silent */);
195 static struct thread_info
*
196 find_thread_id (int num
)
198 struct thread_info
*tp
;
200 for (tp
= thread_list
; tp
; tp
= tp
->next
)
207 /* Find a thread_info by matching PTID. */
209 find_thread_pid (ptid_t ptid
)
211 struct thread_info
*tp
;
213 for (tp
= thread_list
; tp
; tp
= tp
->next
)
214 if (ptid_equal (tp
->ptid
, ptid
))
221 * Thread iterator function.
223 * Calls a callback function once for each thread, so long as
224 * the callback function returns false. If the callback function
225 * returns true, the iteration will end and the current thread
226 * will be returned. This can be useful for implementing a
227 * search for a thread with arbitrary attributes, or for applying
228 * some operation to every thread.
230 * FIXME: some of the existing functionality, such as
231 * "Thread apply all", might be rewritten using this functionality.
235 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
238 struct thread_info
*tp
;
240 for (tp
= thread_list
; tp
; tp
= tp
->next
)
241 if ((*callback
) (tp
, data
))
251 struct thread_info
*tp
;
253 for (tp
= thread_list
; tp
; tp
= tp
->next
)
260 valid_thread_id (int num
)
262 struct thread_info
*tp
;
264 for (tp
= thread_list
; tp
; tp
= tp
->next
)
272 pid_to_thread_id (ptid_t ptid
)
274 struct thread_info
*tp
;
276 for (tp
= thread_list
; tp
; tp
= tp
->next
)
277 if (ptid_equal (tp
->ptid
, ptid
))
284 thread_id_to_pid (int num
)
286 struct thread_info
*thread
= find_thread_id (num
);
290 return pid_to_ptid (-1);
294 in_thread_list (ptid_t ptid
)
296 struct thread_info
*tp
;
298 for (tp
= thread_list
; tp
; tp
= tp
->next
)
299 if (ptid_equal (tp
->ptid
, ptid
))
302 return 0; /* Never heard of 'im */
305 /* Print a list of thread ids currently known, and the total number of
306 threads. To be used from within catch_errors. */
308 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
310 struct thread_info
*tp
;
312 struct cleanup
*cleanup_chain
;
315 target_find_new_threads ();
317 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
319 for (tp
= thread_list
; tp
; tp
= tp
->next
)
322 ui_out_field_int (uiout
, "thread-id", tp
->num
);
325 do_cleanups (cleanup_chain
);
326 ui_out_field_int (uiout
, "number-of-threads", num
);
330 /* Official gdblib interface function to get a list of thread ids and
333 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
335 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
336 error_message
, RETURN_MASK_ALL
) < 0)
341 /* Load infrun state for the thread PID. */
344 load_infrun_state (ptid_t ptid
,
347 struct breakpoint
**step_resume_breakpoint
,
348 CORE_ADDR
*step_range_start
,
349 CORE_ADDR
*step_range_end
,
350 struct frame_id
*step_frame_id
,
351 int *stepping_over_breakpoint
,
352 int *stepping_through_solib_after_catch
,
353 bpstat
*stepping_through_solib_catchpoints
,
355 struct symtab
**current_symtab
)
357 struct thread_info
*tp
;
359 /* If we can't find the thread, then we're debugging a single threaded
360 process. No need to do anything in that case. */
361 tp
= find_thread_id (pid_to_thread_id (ptid
));
365 *prev_pc
= tp
->prev_pc
;
366 *trap_expected
= tp
->trap_expected
;
367 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
368 *step_range_start
= tp
->step_range_start
;
369 *step_range_end
= tp
->step_range_end
;
370 *step_frame_id
= tp
->step_frame_id
;
371 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
372 *stepping_through_solib_after_catch
=
373 tp
->stepping_through_solib_after_catch
;
374 *stepping_through_solib_catchpoints
=
375 tp
->stepping_through_solib_catchpoints
;
376 *current_line
= tp
->current_line
;
377 *current_symtab
= tp
->current_symtab
;
380 /* Save infrun state for the thread PID. */
383 save_infrun_state (ptid_t ptid
,
386 struct breakpoint
*step_resume_breakpoint
,
387 CORE_ADDR step_range_start
,
388 CORE_ADDR step_range_end
,
389 const struct frame_id
*step_frame_id
,
390 int stepping_over_breakpoint
,
391 int stepping_through_solib_after_catch
,
392 bpstat stepping_through_solib_catchpoints
,
394 struct symtab
*current_symtab
)
396 struct thread_info
*tp
;
398 /* If we can't find the thread, then we're debugging a single-threaded
399 process. Nothing to do in that case. */
400 tp
= find_thread_id (pid_to_thread_id (ptid
));
404 tp
->prev_pc
= prev_pc
;
405 tp
->trap_expected
= trap_expected
;
406 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
407 tp
->step_range_start
= step_range_start
;
408 tp
->step_range_end
= step_range_end
;
409 tp
->step_frame_id
= (*step_frame_id
);
410 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
411 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
412 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
413 tp
->current_line
= current_line
;
414 tp
->current_symtab
= current_symtab
;
417 /* Return true if TP is an active thread. */
419 thread_alive (struct thread_info
*tp
)
421 if (PIDGET (tp
->ptid
) == -1)
423 if (!target_thread_alive (tp
->ptid
))
425 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
434 struct thread_info
*tp
, *next
;
436 for (tp
= thread_list
; tp
; tp
= next
)
439 if (!thread_alive (tp
))
440 delete_thread (tp
->ptid
);
444 static int main_thread_running
= 0;
447 set_running (ptid_t ptid
, int running
)
449 struct thread_info
*tp
;
453 /* This is one of the targets that does not add main
454 thread to the thread list. Just use a single
455 global flag to indicate that a thread is running.
457 This problem is unique to ST programs. For MT programs,
458 the main thread is always present in the thread list. If it's
459 not, the first call to context_switch will mess up GDB internal
461 if (running
&& !main_thread_running
&& !suppress_resume_observer
)
462 observer_notify_target_resumed (ptid
);
463 main_thread_running
= running
;
467 /* We try not to notify the observer if no thread has actually changed
468 the running state -- merely to reduce the number of messages to
469 frontend. Frontend is supposed to handle multiple *running just fine. */
470 if (PIDGET (ptid
) == -1)
473 for (tp
= thread_list
; tp
; tp
= tp
->next
)
475 if (running
&& !tp
->running_
)
477 tp
->running_
= running
;
479 if (any_started
&& !suppress_resume_observer
)
480 observer_notify_target_resumed (ptid
);
484 tp
= find_thread_pid (ptid
);
486 if (running
&& !tp
->running_
&& !suppress_resume_observer
)
487 observer_notify_target_resumed (ptid
);
488 tp
->running_
= running
;
493 is_running (ptid_t ptid
)
495 struct thread_info
*tp
;
498 return main_thread_running
;
500 tp
= find_thread_pid (ptid
);
505 /* Prints the list of threads and their details on UIOUT.
506 This is a version of 'info_thread_command' suitable for
508 If REQESTED_THREAD is not -1, it's the GDB id of the thread
509 that should be printed. Otherwise, all threads are
512 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
514 struct thread_info
*tp
;
516 struct frame_info
*cur_frame
;
517 struct cleanup
*old_chain
;
518 struct frame_id saved_frame_id
;
520 int current_thread
= -1;
522 /* Backup current thread and selected frame. */
523 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
524 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
526 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
529 target_find_new_threads ();
530 current_ptid
= inferior_ptid
;
531 for (tp
= thread_list
; tp
; tp
= tp
->next
)
533 struct cleanup
*chain2
;
535 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
538 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
540 if (ptid_equal (tp
->ptid
, current_ptid
))
542 current_thread
= tp
->num
;
543 ui_out_text (uiout
, "* ");
546 ui_out_text (uiout
, " ");
548 ui_out_field_int (uiout
, "id", tp
->num
);
549 ui_out_text (uiout
, " ");
550 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
552 extra_info
= target_extra_thread_info (tp
);
555 ui_out_text (uiout
, " (");
556 ui_out_field_string (uiout
, "details", extra_info
);
557 ui_out_text (uiout
, ")");
559 ui_out_text (uiout
, " ");
560 /* That switch put us at the top of the stack (leaf frame). */
561 switch_to_thread (tp
->ptid
);
562 print_stack_frame (get_selected_frame (NULL
),
563 /* For MI output, print frame level. */
564 ui_out_is_mi_like_p (uiout
),
567 do_cleanups (chain2
);
570 /* Restores the current thread and the frame selected before
571 the "info threads" command. */
572 do_cleanups (old_chain
);
574 if (requested_thread
== -1)
576 gdb_assert (current_thread
!= -1 || !thread_list
);
577 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
578 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
581 /* If case we were not able to find the original frame, print the
582 new selected frame. */
583 if (frame_find_by_id (saved_frame_id
) == NULL
)
585 warning (_("Couldn't restore frame in current thread, at frame 0"));
586 /* For MI, we should probably have a notification about
587 current frame change. But this error is not very likely, so
588 don't bother for now. */
589 if (!ui_out_is_mi_like_p (uiout
))
590 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
595 /* Print information about currently known threads
597 * Note: this has the drawback that it _really_ switches
598 * threads, which frees the frame cache. A no-side
599 * effects info-threads command would be nicer.
603 info_threads_command (char *arg
, int from_tty
)
605 print_thread_info (uiout
, -1);
608 /* Switch from one thread to another. */
611 switch_to_thread (ptid_t ptid
)
613 if (ptid_equal (ptid
, inferior_ptid
))
616 inferior_ptid
= ptid
;
617 reinit_frame_cache ();
618 registers_changed ();
619 stop_pc
= read_pc ();
623 restore_current_thread (ptid_t ptid
)
625 if (!ptid_equal (ptid
, inferior_ptid
))
627 switch_to_thread (ptid
);
632 restore_selected_frame (struct frame_id a_frame_id
)
634 struct frame_info
*selected_frame_info
= NULL
;
636 if (frame_id_eq (a_frame_id
, null_frame_id
))
639 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
641 select_frame (selected_frame_info
);
645 struct current_thread_cleanup
647 ptid_t inferior_ptid
;
648 struct frame_id selected_frame_id
;
652 do_restore_current_thread_cleanup (void *arg
)
654 struct current_thread_cleanup
*old
= arg
;
655 restore_current_thread (old
->inferior_ptid
);
656 restore_selected_frame (old
->selected_frame_id
);
661 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
662 struct frame_id a_frame_id
)
664 struct current_thread_cleanup
*old
665 = xmalloc (sizeof (struct current_thread_cleanup
));
666 old
->inferior_ptid
= inferior_ptid
;
667 old
->selected_frame_id
= a_frame_id
;
668 return make_cleanup (do_restore_current_thread_cleanup
, old
);
671 /* Apply a GDB command to a list of threads. List syntax is a whitespace
672 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
673 of two numbers seperated by a hyphen. Examples:
675 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
676 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
677 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
681 thread_apply_all_command (char *cmd
, int from_tty
)
683 struct thread_info
*tp
;
684 struct cleanup
*old_chain
;
685 struct cleanup
*saved_cmd_cleanup_chain
;
687 struct frame_id saved_frame_id
;
689 int thread_has_changed
= 0;
691 if (cmd
== NULL
|| *cmd
== '\000')
692 error (_("Please specify a command following the thread ID list"));
694 current_ptid
= inferior_ptid
;
695 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
696 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
698 /* It is safe to update the thread list now, before
699 traversing it for "thread apply all". MVS */
700 target_find_new_threads ();
702 /* Save a copy of the command in case it is clobbered by
704 saved_cmd
= xstrdup (cmd
);
705 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
706 for (tp
= thread_list
; tp
; tp
= tp
->next
)
707 if (thread_alive (tp
))
709 switch_to_thread (tp
->ptid
);
710 printf_filtered (_("\nThread %d (%s):\n"),
711 tp
->num
, target_tid_to_str (inferior_ptid
));
712 execute_command (cmd
, from_tty
);
713 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
716 if (!ptid_equal (current_ptid
, inferior_ptid
))
717 thread_has_changed
= 1;
719 do_cleanups (saved_cmd_cleanup_chain
);
720 do_cleanups (old_chain
);
721 /* Print stack frame only if we changed thread. */
722 if (thread_has_changed
)
723 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
728 thread_apply_command (char *tidlist
, int from_tty
)
732 struct cleanup
*old_chain
;
733 struct cleanup
*saved_cmd_cleanup_chain
;
735 struct frame_id saved_frame_id
;
737 int thread_has_changed
= 0;
739 if (tidlist
== NULL
|| *tidlist
== '\000')
740 error (_("Please specify a thread ID list"));
742 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
745 error (_("Please specify a command following the thread ID list"));
747 current_ptid
= inferior_ptid
;
748 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
749 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
751 /* Save a copy of the command in case it is clobbered by
753 saved_cmd
= xstrdup (cmd
);
754 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
755 while (tidlist
< cmd
)
757 struct thread_info
*tp
;
760 start
= strtol (tidlist
, &p
, 10);
762 error (_("Error parsing %s"), tidlist
);
765 while (*tidlist
== ' ' || *tidlist
== '\t')
768 if (*tidlist
== '-') /* Got a range of IDs? */
770 tidlist
++; /* Skip the - */
771 end
= strtol (tidlist
, &p
, 10);
773 error (_("Error parsing %s"), tidlist
);
776 while (*tidlist
== ' ' || *tidlist
== '\t')
782 for (; start
<= end
; start
++)
784 tp
= find_thread_id (start
);
787 warning (_("Unknown thread %d."), start
);
788 else if (!thread_alive (tp
))
789 warning (_("Thread %d has terminated."), start
);
792 switch_to_thread (tp
->ptid
);
793 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
794 target_tid_to_str (inferior_ptid
));
795 execute_command (cmd
, from_tty
);
796 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
801 if (!ptid_equal (current_ptid
, inferior_ptid
))
802 thread_has_changed
= 1;
804 do_cleanups (saved_cmd_cleanup_chain
);
805 do_cleanups (old_chain
);
806 /* Print stack frame only if we changed thread. */
807 if (thread_has_changed
)
808 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
811 /* Switch to the specified thread. Will dispatch off to thread_apply_command
812 if prefix of arg is `apply'. */
815 thread_command (char *tidstr
, int from_tty
)
819 /* Don't generate an error, just say which thread is current. */
820 if (target_has_stack
)
821 printf_filtered (_("[Current thread is %d (%s)]\n"),
822 pid_to_thread_id (inferior_ptid
),
823 target_tid_to_str (inferior_ptid
));
825 error (_("No stack."));
829 annotate_thread_changed ();
830 gdb_thread_select (uiout
, tidstr
, NULL
);
833 /* Print notices when new threads are attached and detached. */
834 int print_thread_events
= 1;
836 show_print_thread_events (struct ui_file
*file
, int from_tty
,
837 struct cmd_list_element
*c
, const char *value
)
839 fprintf_filtered (file
, _("\
840 Printing of thread events is %s.\n"),
845 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
848 struct thread_info
*tp
;
850 num
= value_as_long (parse_and_eval (tidstr
));
852 tp
= find_thread_id (num
);
855 error (_("Thread ID %d not known."), num
);
857 if (!thread_alive (tp
))
858 error (_("Thread ID %d has terminated."), num
);
860 switch_to_thread (tp
->ptid
);
862 ui_out_text (uiout
, "[Switching to thread ");
863 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
864 ui_out_text (uiout
, " (");
865 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
866 ui_out_text (uiout
, ")]");
868 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
873 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
875 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
876 error_message
, RETURN_MASK_ALL
) < 0)
881 /* Commands with a prefix of `thread'. */
882 struct cmd_list_element
*thread_cmd_list
= NULL
;
885 _initialize_thread (void)
887 static struct cmd_list_element
*thread_apply_list
= NULL
;
889 add_info ("threads", info_threads_command
,
890 _("IDs of currently known threads."));
892 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
893 Use this command to switch between threads.\n\
894 The new thread ID must be currently known."),
895 &thread_cmd_list
, "thread ", 1, &cmdlist
);
897 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
898 _("Apply a command to a list of threads."),
899 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
901 add_cmd ("all", class_run
, thread_apply_all_command
,
902 _("Apply a command to all threads."), &thread_apply_list
);
905 add_com_alias ("t", "thread", class_run
, 1);
907 add_setshow_boolean_cmd ("thread-events", no_class
,
908 &print_thread_events
, _("\
909 Set printing of thread events (such as thread start and exit)."), _("\
910 Show printing of thread events (such as thread start and exit)."), NULL
,
912 show_print_thread_events
,
913 &setprintlist
, &showprintlist
);