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
);
157 delete_thread (ptid_t ptid
)
159 struct thread_info
*tp
, *tpprev
;
163 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
164 if (ptid_equal (tp
->ptid
, ptid
))
171 tpprev
->next
= tp
->next
;
173 thread_list
= tp
->next
;
175 observer_notify_thread_exit (tp
);
180 static struct thread_info
*
181 find_thread_id (int num
)
183 struct thread_info
*tp
;
185 for (tp
= thread_list
; tp
; tp
= tp
->next
)
192 /* Find a thread_info by matching PTID. */
194 find_thread_pid (ptid_t ptid
)
196 struct thread_info
*tp
;
198 for (tp
= thread_list
; tp
; tp
= tp
->next
)
199 if (ptid_equal (tp
->ptid
, ptid
))
206 * Thread iterator function.
208 * Calls a callback function once for each thread, so long as
209 * the callback function returns false. If the callback function
210 * returns true, the iteration will end and the current thread
211 * will be returned. This can be useful for implementing a
212 * search for a thread with arbitrary attributes, or for applying
213 * some operation to every thread.
215 * FIXME: some of the existing functionality, such as
216 * "Thread apply all", might be rewritten using this functionality.
220 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
223 struct thread_info
*tp
;
225 for (tp
= thread_list
; tp
; tp
= tp
->next
)
226 if ((*callback
) (tp
, data
))
233 valid_thread_id (int num
)
235 struct thread_info
*tp
;
237 for (tp
= thread_list
; tp
; tp
= tp
->next
)
245 pid_to_thread_id (ptid_t ptid
)
247 struct thread_info
*tp
;
249 for (tp
= thread_list
; tp
; tp
= tp
->next
)
250 if (ptid_equal (tp
->ptid
, ptid
))
257 thread_id_to_pid (int num
)
259 struct thread_info
*thread
= find_thread_id (num
);
263 return pid_to_ptid (-1);
267 in_thread_list (ptid_t ptid
)
269 struct thread_info
*tp
;
271 for (tp
= thread_list
; tp
; tp
= tp
->next
)
272 if (ptid_equal (tp
->ptid
, ptid
))
275 return 0; /* Never heard of 'im */
278 /* Print a list of thread ids currently known, and the total number of
279 threads. To be used from within catch_errors. */
281 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
283 struct thread_info
*tp
;
285 struct cleanup
*cleanup_chain
;
288 target_find_new_threads ();
290 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
292 for (tp
= thread_list
; tp
; tp
= tp
->next
)
295 ui_out_field_int (uiout
, "thread-id", tp
->num
);
298 do_cleanups (cleanup_chain
);
299 ui_out_field_int (uiout
, "number-of-threads", num
);
303 /* Official gdblib interface function to get a list of thread ids and
306 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
308 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
309 error_message
, RETURN_MASK_ALL
) < 0)
314 /* Load infrun state for the thread PID. */
317 load_infrun_state (ptid_t ptid
,
320 struct breakpoint
**step_resume_breakpoint
,
321 CORE_ADDR
*step_range_start
,
322 CORE_ADDR
*step_range_end
,
323 struct frame_id
*step_frame_id
,
324 int *stepping_over_breakpoint
,
325 int *stepping_through_solib_after_catch
,
326 bpstat
*stepping_through_solib_catchpoints
,
328 struct symtab
**current_symtab
)
330 struct thread_info
*tp
;
332 /* If we can't find the thread, then we're debugging a single threaded
333 process. No need to do anything in that case. */
334 tp
= find_thread_id (pid_to_thread_id (ptid
));
338 *prev_pc
= tp
->prev_pc
;
339 *trap_expected
= tp
->trap_expected
;
340 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
341 *step_range_start
= tp
->step_range_start
;
342 *step_range_end
= tp
->step_range_end
;
343 *step_frame_id
= tp
->step_frame_id
;
344 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
345 *stepping_through_solib_after_catch
=
346 tp
->stepping_through_solib_after_catch
;
347 *stepping_through_solib_catchpoints
=
348 tp
->stepping_through_solib_catchpoints
;
349 *current_line
= tp
->current_line
;
350 *current_symtab
= tp
->current_symtab
;
353 /* Save infrun state for the thread PID. */
356 save_infrun_state (ptid_t ptid
,
359 struct breakpoint
*step_resume_breakpoint
,
360 CORE_ADDR step_range_start
,
361 CORE_ADDR step_range_end
,
362 const struct frame_id
*step_frame_id
,
363 int stepping_over_breakpoint
,
364 int stepping_through_solib_after_catch
,
365 bpstat stepping_through_solib_catchpoints
,
367 struct symtab
*current_symtab
)
369 struct thread_info
*tp
;
371 /* If we can't find the thread, then we're debugging a single-threaded
372 process. Nothing to do in that case. */
373 tp
= find_thread_id (pid_to_thread_id (ptid
));
377 tp
->prev_pc
= prev_pc
;
378 tp
->trap_expected
= trap_expected
;
379 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
380 tp
->step_range_start
= step_range_start
;
381 tp
->step_range_end
= step_range_end
;
382 tp
->step_frame_id
= (*step_frame_id
);
383 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
384 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
385 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
386 tp
->current_line
= current_line
;
387 tp
->current_symtab
= current_symtab
;
390 /* Return true if TP is an active thread. */
392 thread_alive (struct thread_info
*tp
)
394 if (PIDGET (tp
->ptid
) == -1)
396 if (!target_thread_alive (tp
->ptid
))
398 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
407 struct thread_info
*tp
, *next
;
409 for (tp
= thread_list
; tp
; tp
= next
)
412 if (!thread_alive (tp
))
413 delete_thread (tp
->ptid
);
417 static int main_thread_running
= 0;
420 set_running (ptid_t ptid
, int running
)
422 struct thread_info
*tp
;
426 /* This is one of the targets that does not add main
427 thread to the thread list. Just use a single
428 global flag to indicate that a thread is running.
430 This problem is unique to ST programs. For MT programs,
431 the main thread is always present in the thread list. If it's
432 not, the first call to context_switch will mess up GDB internal
434 if (running
&& !main_thread_running
&& !suppress_resume_observer
)
435 observer_notify_target_resumed (ptid
);
436 main_thread_running
= running
;
440 /* We try not to notify the observer if no thread has actually changed
441 the running state -- merely to reduce the number of messages to
442 frontend. Frontend is supposed to handle multiple *running just fine. */
443 if (PIDGET (ptid
) == -1)
446 for (tp
= thread_list
; tp
; tp
= tp
->next
)
448 if (running
&& !tp
->running_
)
450 tp
->running_
= running
;
452 if (any_started
&& !suppress_resume_observer
)
453 observer_notify_target_resumed (ptid
);
457 tp
= find_thread_pid (ptid
);
459 if (running
&& !tp
->running_
&& !suppress_resume_observer
)
460 observer_notify_target_resumed (ptid
);
461 tp
->running_
= running
;
466 is_running (ptid_t ptid
)
468 struct thread_info
*tp
;
471 return main_thread_running
;
473 tp
= find_thread_pid (ptid
);
478 /* Prints the list of threads and their details on UIOUT.
479 This is a version of 'info_thread_command' suitable for
481 If REQESTED_THREAD is not -1, it's the GDB id of the thread
482 that should be printed. Otherwise, all threads are
485 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
487 struct thread_info
*tp
;
489 struct frame_info
*cur_frame
;
490 struct cleanup
*old_chain
;
491 struct frame_id saved_frame_id
;
493 int current_thread
= -1;
495 /* Backup current thread and selected frame. */
496 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
497 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
499 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
502 target_find_new_threads ();
503 current_ptid
= inferior_ptid
;
504 for (tp
= thread_list
; tp
; tp
= tp
->next
)
506 struct cleanup
*chain2
;
508 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
511 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
513 if (ptid_equal (tp
->ptid
, current_ptid
))
515 current_thread
= tp
->num
;
516 ui_out_text (uiout
, "* ");
519 ui_out_text (uiout
, " ");
521 ui_out_field_int (uiout
, "id", tp
->num
);
522 ui_out_text (uiout
, " ");
523 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
525 extra_info
= target_extra_thread_info (tp
);
528 ui_out_text (uiout
, " (");
529 ui_out_field_string (uiout
, "details", extra_info
);
530 ui_out_text (uiout
, ")");
532 ui_out_text (uiout
, " ");
533 /* That switch put us at the top of the stack (leaf frame). */
534 switch_to_thread (tp
->ptid
);
535 print_stack_frame (get_selected_frame (NULL
),
536 /* For MI output, print frame level. */
537 ui_out_is_mi_like_p (uiout
),
540 do_cleanups (chain2
);
543 /* Restores the current thread and the frame selected before
544 the "info threads" command. */
545 do_cleanups (old_chain
);
547 if (requested_thread
== -1)
549 gdb_assert (current_thread
!= -1 || !thread_list
);
550 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
551 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
554 /* If case we were not able to find the original frame, print the
555 new selected frame. */
556 if (frame_find_by_id (saved_frame_id
) == NULL
)
558 warning (_("Couldn't restore frame in current thread, at frame 0"));
559 /* For MI, we should probably have a notification about
560 current frame change. But this error is not very likely, so
561 don't bother for now. */
562 if (!ui_out_is_mi_like_p (uiout
))
563 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
568 /* Print information about currently known threads
570 * Note: this has the drawback that it _really_ switches
571 * threads, which frees the frame cache. A no-side
572 * effects info-threads command would be nicer.
576 info_threads_command (char *arg
, int from_tty
)
578 print_thread_info (uiout
, -1);
581 /* Switch from one thread to another. */
584 switch_to_thread (ptid_t ptid
)
586 if (ptid_equal (ptid
, inferior_ptid
))
589 inferior_ptid
= ptid
;
590 reinit_frame_cache ();
591 registers_changed ();
592 stop_pc
= read_pc ();
596 restore_current_thread (ptid_t ptid
)
598 if (!ptid_equal (ptid
, inferior_ptid
))
600 switch_to_thread (ptid
);
605 restore_selected_frame (struct frame_id a_frame_id
)
607 struct frame_info
*selected_frame_info
= NULL
;
609 if (frame_id_eq (a_frame_id
, null_frame_id
))
612 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
614 select_frame (selected_frame_info
);
618 struct current_thread_cleanup
620 ptid_t inferior_ptid
;
621 struct frame_id selected_frame_id
;
625 do_restore_current_thread_cleanup (void *arg
)
627 struct current_thread_cleanup
*old
= arg
;
628 restore_current_thread (old
->inferior_ptid
);
629 restore_selected_frame (old
->selected_frame_id
);
634 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
635 struct frame_id a_frame_id
)
637 struct current_thread_cleanup
*old
638 = xmalloc (sizeof (struct current_thread_cleanup
));
639 old
->inferior_ptid
= inferior_ptid
;
640 old
->selected_frame_id
= a_frame_id
;
641 return make_cleanup (do_restore_current_thread_cleanup
, old
);
644 /* Apply a GDB command to a list of threads. List syntax is a whitespace
645 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
646 of two numbers seperated by a hyphen. Examples:
648 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
649 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
650 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
654 thread_apply_all_command (char *cmd
, int from_tty
)
656 struct thread_info
*tp
;
657 struct cleanup
*old_chain
;
658 struct cleanup
*saved_cmd_cleanup_chain
;
660 struct frame_id saved_frame_id
;
662 int thread_has_changed
= 0;
664 if (cmd
== NULL
|| *cmd
== '\000')
665 error (_("Please specify a command following the thread ID list"));
667 current_ptid
= inferior_ptid
;
668 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
669 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
671 /* It is safe to update the thread list now, before
672 traversing it for "thread apply all". MVS */
673 target_find_new_threads ();
675 /* Save a copy of the command in case it is clobbered by
677 saved_cmd
= xstrdup (cmd
);
678 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
679 for (tp
= thread_list
; tp
; tp
= tp
->next
)
680 if (thread_alive (tp
))
682 switch_to_thread (tp
->ptid
);
683 printf_filtered (_("\nThread %d (%s):\n"),
684 tp
->num
, target_tid_to_str (inferior_ptid
));
685 execute_command (cmd
, from_tty
);
686 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
689 if (!ptid_equal (current_ptid
, inferior_ptid
))
690 thread_has_changed
= 1;
692 do_cleanups (saved_cmd_cleanup_chain
);
693 do_cleanups (old_chain
);
694 /* Print stack frame only if we changed thread. */
695 if (thread_has_changed
)
696 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
701 thread_apply_command (char *tidlist
, int from_tty
)
705 struct cleanup
*old_chain
;
706 struct cleanup
*saved_cmd_cleanup_chain
;
708 struct frame_id saved_frame_id
;
710 int thread_has_changed
= 0;
712 if (tidlist
== NULL
|| *tidlist
== '\000')
713 error (_("Please specify a thread ID list"));
715 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
718 error (_("Please specify a command following the thread ID list"));
720 current_ptid
= inferior_ptid
;
721 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
722 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
724 /* Save a copy of the command in case it is clobbered by
726 saved_cmd
= xstrdup (cmd
);
727 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
728 while (tidlist
< cmd
)
730 struct thread_info
*tp
;
733 start
= strtol (tidlist
, &p
, 10);
735 error (_("Error parsing %s"), tidlist
);
738 while (*tidlist
== ' ' || *tidlist
== '\t')
741 if (*tidlist
== '-') /* Got a range of IDs? */
743 tidlist
++; /* Skip the - */
744 end
= strtol (tidlist
, &p
, 10);
746 error (_("Error parsing %s"), tidlist
);
749 while (*tidlist
== ' ' || *tidlist
== '\t')
755 for (; start
<= end
; start
++)
757 tp
= find_thread_id (start
);
760 warning (_("Unknown thread %d."), start
);
761 else if (!thread_alive (tp
))
762 warning (_("Thread %d has terminated."), start
);
765 switch_to_thread (tp
->ptid
);
766 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
767 target_tid_to_str (inferior_ptid
));
768 execute_command (cmd
, from_tty
);
769 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
774 if (!ptid_equal (current_ptid
, inferior_ptid
))
775 thread_has_changed
= 1;
777 do_cleanups (saved_cmd_cleanup_chain
);
778 do_cleanups (old_chain
);
779 /* Print stack frame only if we changed thread. */
780 if (thread_has_changed
)
781 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
784 /* Switch to the specified thread. Will dispatch off to thread_apply_command
785 if prefix of arg is `apply'. */
788 thread_command (char *tidstr
, int from_tty
)
792 /* Don't generate an error, just say which thread is current. */
793 if (target_has_stack
)
794 printf_filtered (_("[Current thread is %d (%s)]\n"),
795 pid_to_thread_id (inferior_ptid
),
796 target_tid_to_str (inferior_ptid
));
798 error (_("No stack."));
802 annotate_thread_changed ();
803 gdb_thread_select (uiout
, tidstr
, NULL
);
806 /* Print notices when new threads are attached and detached. */
807 int print_thread_events
= 1;
809 show_print_thread_events (struct ui_file
*file
, int from_tty
,
810 struct cmd_list_element
*c
, const char *value
)
812 fprintf_filtered (file
, _("\
813 Printing of thread events is %s.\n"),
818 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
821 struct thread_info
*tp
;
823 num
= value_as_long (parse_and_eval (tidstr
));
825 tp
= find_thread_id (num
);
828 error (_("Thread ID %d not known."), num
);
830 if (!thread_alive (tp
))
831 error (_("Thread ID %d has terminated."), num
);
833 switch_to_thread (tp
->ptid
);
835 ui_out_text (uiout
, "[Switching to thread ");
836 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
837 ui_out_text (uiout
, " (");
838 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
839 ui_out_text (uiout
, ")]");
841 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
846 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
848 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
849 error_message
, RETURN_MASK_ALL
) < 0)
854 /* Commands with a prefix of `thread'. */
855 struct cmd_list_element
*thread_cmd_list
= NULL
;
858 _initialize_thread (void)
860 static struct cmd_list_element
*thread_apply_list
= NULL
;
862 add_info ("threads", info_threads_command
,
863 _("IDs of currently known threads."));
865 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
866 Use this command to switch between threads.\n\
867 The new thread ID must be currently known."),
868 &thread_cmd_list
, "thread ", 1, &cmdlist
);
870 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
871 _("Apply a command to a list of threads."),
872 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
874 add_cmd ("all", class_run
, thread_apply_all_command
,
875 _("Apply a command to all threads."), &thread_apply_list
);
878 add_com_alias ("t", "thread", class_run
, 1);
880 add_setshow_boolean_cmd ("thread-events", no_class
,
881 &print_thread_events
, _("\
882 Set printing of thread events (such as thread start and exit)."), _("\
883 Show printing of thread events (such as thread start and exit)."), NULL
,
885 show_print_thread_events
,
886 &setprintlist
, &showprintlist
);