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 #include "cli/cli-decode.h"
47 /* Definition of struct thread_info exported to gdbthread.h */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 static struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 static struct thread_info
*find_thread_id (int num
);
60 static void thread_command (char *tidstr
, int from_tty
);
61 static void thread_apply_all_command (char *, int);
62 static int thread_alive (struct thread_info
*);
63 static void info_threads_command (char *, int);
64 static void thread_apply_command (char *, int);
65 static void restore_current_thread (ptid_t
);
66 static void prune_threads (void);
68 static int main_thread_running
= 0;
69 static int main_thread_executing
= 0;
72 delete_step_resume_breakpoint (void *arg
)
74 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
75 struct thread_info
*tp
;
77 if (*breakpointp
!= NULL
)
79 delete_breakpoint (*breakpointp
);
80 for (tp
= thread_list
; tp
; tp
= tp
->next
)
81 if (tp
->step_resume_breakpoint
== *breakpointp
)
82 tp
->step_resume_breakpoint
= NULL
;
89 free_thread (struct thread_info
*tp
)
91 /* NOTE: this will take care of any left-over step_resume breakpoints,
92 but not any user-specified thread-specific breakpoints. We can not
93 delete the breakpoint straight-off, because the inferior might not
94 be stopped at the moment. */
95 if (tp
->step_resume_breakpoint
)
96 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
98 bpstat_clear (&tp
->stop_bpstat
);
100 /* FIXME: do I ever need to call the back-end to give it a
101 chance at this private data before deleting the thread? */
109 init_thread_list (void)
111 struct thread_info
*tp
, *tpnext
;
113 highest_thread_num
= 0;
114 main_thread_running
= 0;
115 main_thread_executing
= 0;
120 for (tp
= thread_list
; tp
; tp
= tpnext
)
130 add_thread_silent (ptid_t ptid
)
132 struct thread_info
*tp
;
134 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
135 memset (tp
, 0, sizeof (*tp
));
137 tp
->num
= ++highest_thread_num
;
138 tp
->next
= thread_list
;
141 observer_notify_new_thread (tp
);
147 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
149 struct thread_info
*result
= add_thread_silent (ptid
);
151 result
->private = private;
153 if (print_thread_events
)
154 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
156 annotate_new_thread ();
161 add_thread (ptid_t ptid
)
163 return add_thread_with_info (ptid
, NULL
);
166 /* Delete thread PTID. If SILENT, don't notify the observer of this
169 delete_thread_1 (ptid_t ptid
, int silent
)
171 struct thread_info
*tp
, *tpprev
;
175 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
176 if (ptid_equal (tp
->ptid
, ptid
))
183 tpprev
->next
= tp
->next
;
185 thread_list
= tp
->next
;
188 observer_notify_thread_exit (tp
);
194 delete_thread (ptid_t ptid
)
196 delete_thread_1 (ptid
, 0 /* not silent */);
200 delete_thread_silent (ptid_t ptid
)
202 delete_thread_1 (ptid
, 1 /* silent */);
205 static struct thread_info
*
206 find_thread_id (int num
)
208 struct thread_info
*tp
;
210 for (tp
= thread_list
; tp
; tp
= tp
->next
)
217 /* Find a thread_info by matching PTID. */
219 find_thread_pid (ptid_t ptid
)
221 struct thread_info
*tp
;
223 for (tp
= thread_list
; tp
; tp
= tp
->next
)
224 if (ptid_equal (tp
->ptid
, ptid
))
231 * Thread iterator function.
233 * Calls a callback function once for each thread, so long as
234 * the callback function returns false. If the callback function
235 * returns true, the iteration will end and the current thread
236 * will be returned. This can be useful for implementing a
237 * search for a thread with arbitrary attributes, or for applying
238 * some operation to every thread.
240 * FIXME: some of the existing functionality, such as
241 * "Thread apply all", might be rewritten using this functionality.
245 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
248 struct thread_info
*tp
;
250 for (tp
= thread_list
; tp
; tp
= tp
->next
)
251 if ((*callback
) (tp
, data
))
261 struct thread_info
*tp
;
263 for (tp
= thread_list
; tp
; tp
= tp
->next
)
270 valid_thread_id (int num
)
272 struct thread_info
*tp
;
274 for (tp
= thread_list
; tp
; tp
= tp
->next
)
282 pid_to_thread_id (ptid_t ptid
)
284 struct thread_info
*tp
;
286 for (tp
= thread_list
; tp
; tp
= tp
->next
)
287 if (ptid_equal (tp
->ptid
, ptid
))
294 thread_id_to_pid (int num
)
296 struct thread_info
*thread
= find_thread_id (num
);
300 return pid_to_ptid (-1);
304 in_thread_list (ptid_t ptid
)
306 struct thread_info
*tp
;
308 for (tp
= thread_list
; tp
; tp
= tp
->next
)
309 if (ptid_equal (tp
->ptid
, ptid
))
312 return 0; /* Never heard of 'im */
315 /* Print a list of thread ids currently known, and the total number of
316 threads. To be used from within catch_errors. */
318 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
320 struct thread_info
*tp
;
322 struct cleanup
*cleanup_chain
;
325 target_find_new_threads ();
327 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
329 for (tp
= thread_list
; tp
; tp
= tp
->next
)
332 ui_out_field_int (uiout
, "thread-id", tp
->num
);
335 do_cleanups (cleanup_chain
);
336 ui_out_field_int (uiout
, "number-of-threads", num
);
340 /* Official gdblib interface function to get a list of thread ids and
343 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
345 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
346 error_message
, RETURN_MASK_ALL
) < 0)
351 /* Load infrun state for the thread PID. */
354 load_infrun_state (ptid_t ptid
,
357 struct breakpoint
**step_resume_breakpoint
,
358 CORE_ADDR
*step_range_start
,
359 CORE_ADDR
*step_range_end
,
360 struct frame_id
*step_frame_id
,
361 int *stepping_over_breakpoint
,
362 int *stepping_through_solib_after_catch
,
363 bpstat
*stepping_through_solib_catchpoints
,
365 struct symtab
**current_symtab
,
366 struct continuation
**continuations
,
367 struct continuation
**intermediate_continuations
,
368 int *proceed_to_finish
,
369 enum step_over_calls_kind
*step_over_calls
,
372 enum target_signal
*stop_signal
,
375 struct thread_info
*tp
;
377 /* If we can't find the thread, then we're debugging a single threaded
378 process. No need to do anything in that case. */
379 tp
= find_thread_id (pid_to_thread_id (ptid
));
383 *prev_pc
= tp
->prev_pc
;
384 *trap_expected
= tp
->trap_expected
;
385 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
386 *step_range_start
= tp
->step_range_start
;
387 *step_range_end
= tp
->step_range_end
;
388 *step_frame_id
= tp
->step_frame_id
;
389 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
390 *stepping_through_solib_after_catch
=
391 tp
->stepping_through_solib_after_catch
;
392 *stepping_through_solib_catchpoints
=
393 tp
->stepping_through_solib_catchpoints
;
394 *current_line
= tp
->current_line
;
395 *current_symtab
= tp
->current_symtab
;
397 /* In all-stop mode, these are global state, while in non-stop mode,
398 they are per thread. */
401 *continuations
= tp
->continuations
;
402 tp
->continuations
= NULL
;
403 *intermediate_continuations
= tp
->intermediate_continuations
;
404 tp
->intermediate_continuations
= NULL
;
405 *proceed_to_finish
= tp
->proceed_to_finish
;
406 *step_over_calls
= tp
->step_over_calls
;
407 *stop_step
= tp
->stop_step
;
408 *step_multi
= tp
->step_multi
;
409 *stop_signal
= tp
->stop_signal
;
411 /* Swap instead of copy, so we only have to update one of
413 *stop_bpstat
= tp
->stop_bpstat
;
418 /* Save infrun state for the thread PID. */
421 save_infrun_state (ptid_t ptid
,
424 struct breakpoint
*step_resume_breakpoint
,
425 CORE_ADDR step_range_start
,
426 CORE_ADDR step_range_end
,
427 const struct frame_id
*step_frame_id
,
428 int stepping_over_breakpoint
,
429 int stepping_through_solib_after_catch
,
430 bpstat stepping_through_solib_catchpoints
,
432 struct symtab
*current_symtab
,
433 struct continuation
*continuations
,
434 struct continuation
*intermediate_continuations
,
435 int proceed_to_finish
,
436 enum step_over_calls_kind step_over_calls
,
439 enum target_signal stop_signal
,
442 struct thread_info
*tp
;
444 /* If we can't find the thread, then we're debugging a single-threaded
445 process. Nothing to do in that case. */
446 tp
= find_thread_id (pid_to_thread_id (ptid
));
450 tp
->prev_pc
= prev_pc
;
451 tp
->trap_expected
= trap_expected
;
452 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
453 tp
->step_range_start
= step_range_start
;
454 tp
->step_range_end
= step_range_end
;
455 tp
->step_frame_id
= (*step_frame_id
);
456 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
457 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
458 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
459 tp
->current_line
= current_line
;
460 tp
->current_symtab
= current_symtab
;
462 /* In all-stop mode, these are global state, while in non-stop mode,
463 they are per thread. */
466 tp
->continuations
= continuations
;
467 tp
->intermediate_continuations
= intermediate_continuations
;
468 tp
->proceed_to_finish
= proceed_to_finish
;
469 tp
->step_over_calls
= step_over_calls
;
470 tp
->stop_step
= stop_step
;
471 tp
->step_multi
= step_multi
;
472 tp
->stop_signal
= stop_signal
;
473 tp
->stop_bpstat
= stop_bpstat
;
477 /* Return true if TP is an active thread. */
479 thread_alive (struct thread_info
*tp
)
481 if (PIDGET (tp
->ptid
) == -1)
483 if (!target_thread_alive (tp
->ptid
))
485 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
494 struct thread_info
*tp
, *next
;
496 for (tp
= thread_list
; tp
; tp
= next
)
499 if (!thread_alive (tp
))
500 delete_thread (tp
->ptid
);
505 set_running (ptid_t ptid
, int running
)
507 struct thread_info
*tp
;
511 /* This is one of the targets that does not add main
512 thread to the thread list. Just use a single
513 global flag to indicate that a thread is running.
515 This problem is unique to ST programs. For MT programs,
516 the main thread is always present in the thread list. If it's
517 not, the first call to context_switch will mess up GDB internal
519 if (running
&& !main_thread_running
&& !suppress_resume_observer
)
520 observer_notify_target_resumed (ptid
);
521 main_thread_running
= running
;
525 /* We try not to notify the observer if no thread has actually changed
526 the running state -- merely to reduce the number of messages to
527 frontend. Frontend is supposed to handle multiple *running just fine. */
528 if (PIDGET (ptid
) == -1)
531 for (tp
= thread_list
; tp
; tp
= tp
->next
)
533 if (running
&& !tp
->running_
)
535 tp
->running_
= running
;
537 if (any_started
&& !suppress_resume_observer
)
538 observer_notify_target_resumed (ptid
);
542 tp
= find_thread_pid (ptid
);
544 if (running
&& !tp
->running_
&& !suppress_resume_observer
)
545 observer_notify_target_resumed (ptid
);
546 tp
->running_
= running
;
551 is_running (ptid_t ptid
)
553 struct thread_info
*tp
;
555 if (!target_has_execution
)
559 return main_thread_running
;
561 tp
= find_thread_pid (ptid
);
569 struct thread_info
*tp
;
571 if (!target_has_execution
)
575 return main_thread_running
;
577 for (tp
= thread_list
; tp
; tp
= tp
->next
)
585 is_executing (ptid_t ptid
)
587 struct thread_info
*tp
;
589 if (!target_has_execution
)
593 return main_thread_executing
;
595 tp
= find_thread_pid (ptid
);
597 return tp
->executing_
;
601 set_executing (ptid_t ptid
, int executing
)
603 struct thread_info
*tp
;
607 /* This target does not add the main thread to the thread list.
608 Use a global flag to indicate that the thread is
610 main_thread_executing
= executing
;
614 if (PIDGET (ptid
) == -1)
616 for (tp
= thread_list
; tp
; tp
= tp
->next
)
617 tp
->executing_
= executing
;
621 tp
= find_thread_pid (ptid
);
623 tp
->executing_
= executing
;
627 /* Prints the list of threads and their details on UIOUT.
628 This is a version of 'info_thread_command' suitable for
630 If REQESTED_THREAD is not -1, it's the GDB id of the thread
631 that should be printed. Otherwise, all threads are
634 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
636 struct thread_info
*tp
;
638 struct frame_info
*cur_frame
;
639 struct cleanup
*old_chain
;
640 struct frame_id saved_frame_id
;
642 int current_thread
= -1;
644 /* Backup current thread and selected frame. */
645 if (!is_running (inferior_ptid
))
646 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
648 saved_frame_id
= null_frame_id
;
650 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
651 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
654 target_find_new_threads ();
655 current_ptid
= inferior_ptid
;
656 for (tp
= thread_list
; tp
; tp
= tp
->next
)
658 struct cleanup
*chain2
;
660 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
663 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
665 if (ptid_equal (tp
->ptid
, current_ptid
))
667 current_thread
= tp
->num
;
668 ui_out_text (uiout
, "* ");
671 ui_out_text (uiout
, " ");
673 ui_out_field_int (uiout
, "id", tp
->num
);
674 ui_out_text (uiout
, " ");
675 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
677 extra_info
= target_extra_thread_info (tp
);
680 ui_out_text (uiout
, " (");
681 ui_out_field_string (uiout
, "details", extra_info
);
682 ui_out_text (uiout
, ")");
684 ui_out_text (uiout
, " ");
686 ui_out_text (uiout
, "(running)\n");
689 /* The switch below puts us at the top of the stack (leaf
691 switch_to_thread (tp
->ptid
);
692 print_stack_frame (get_selected_frame (NULL
),
693 /* For MI output, print frame level. */
694 ui_out_is_mi_like_p (uiout
),
698 do_cleanups (chain2
);
701 /* Restores the current thread and the frame selected before
702 the "info threads" command. */
703 do_cleanups (old_chain
);
705 if (requested_thread
== -1)
707 gdb_assert (current_thread
!= -1 || !thread_list
);
708 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
709 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
712 if (is_running (inferior_ptid
))
715 /* If case we were not able to find the original frame, print the
716 new selected frame. */
717 if (frame_find_by_id (saved_frame_id
) == NULL
)
719 warning (_("Couldn't restore frame in current thread, at frame 0"));
720 /* For MI, we should probably have a notification about
721 current frame change. But this error is not very likely, so
722 don't bother for now. */
723 if (!ui_out_is_mi_like_p (uiout
))
724 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
729 /* Print information about currently known threads
731 * Note: this has the drawback that it _really_ switches
732 * threads, which frees the frame cache. A no-side
733 * effects info-threads command would be nicer.
737 info_threads_command (char *arg
, int from_tty
)
739 print_thread_info (uiout
, -1);
742 /* Switch from one thread to another. */
745 switch_to_thread (ptid_t ptid
)
747 if (ptid_equal (ptid
, inferior_ptid
))
750 inferior_ptid
= ptid
;
751 reinit_frame_cache ();
752 registers_changed ();
754 if (!is_executing (ptid
))
755 stop_pc
= read_pc ();
757 stop_pc
= ~(CORE_ADDR
) 0;
761 restore_current_thread (ptid_t ptid
)
763 if (!ptid_equal (ptid
, inferior_ptid
))
765 switch_to_thread (ptid
);
770 restore_selected_frame (struct frame_id a_frame_id
)
772 struct frame_info
*selected_frame_info
= NULL
;
774 if (frame_id_eq (a_frame_id
, null_frame_id
))
777 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
779 select_frame (selected_frame_info
);
783 struct current_thread_cleanup
785 ptid_t inferior_ptid
;
786 struct frame_id selected_frame_id
;
790 do_restore_current_thread_cleanup (void *arg
)
792 struct current_thread_cleanup
*old
= arg
;
793 restore_current_thread (old
->inferior_ptid
);
795 /* A command like 'thread apply all $exec_command&' may change the
796 running state of the originally selected thread, so we have to
798 if (!is_running (old
->inferior_ptid
))
799 restore_selected_frame (old
->selected_frame_id
);
804 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
805 struct frame_id a_frame_id
)
807 struct current_thread_cleanup
*old
808 = xmalloc (sizeof (struct current_thread_cleanup
));
809 old
->inferior_ptid
= inferior_ptid
;
810 old
->selected_frame_id
= a_frame_id
;
811 return make_cleanup (do_restore_current_thread_cleanup
, old
);
814 /* Apply a GDB command to a list of threads. List syntax is a whitespace
815 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
816 of two numbers seperated by a hyphen. Examples:
818 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
819 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
820 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
824 thread_apply_all_command (char *cmd
, int from_tty
)
826 struct thread_info
*tp
;
827 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
829 struct frame_id saved_frame_id
;
831 int thread_has_changed
= 0;
833 if (cmd
== NULL
|| *cmd
== '\000')
834 error (_("Please specify a command following the thread ID list"));
836 current_ptid
= inferior_ptid
;
838 if (!is_running (inferior_ptid
))
839 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
841 saved_frame_id
= null_frame_id
;
842 make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
844 /* It is safe to update the thread list now, before
845 traversing it for "thread apply all". MVS */
846 target_find_new_threads ();
848 /* Save a copy of the command in case it is clobbered by
850 saved_cmd
= xstrdup (cmd
);
851 make_cleanup (xfree
, saved_cmd
);
852 for (tp
= thread_list
; tp
; tp
= tp
->next
)
853 if (thread_alive (tp
))
856 context_switch_to (tp
->ptid
);
858 switch_to_thread (tp
->ptid
);
860 printf_filtered (_("\nThread %d (%s):\n"),
861 tp
->num
, target_tid_to_str (inferior_ptid
));
862 execute_command (cmd
, from_tty
);
863 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
866 if (!ptid_equal (current_ptid
, inferior_ptid
))
867 thread_has_changed
= 1;
869 do_cleanups (old_chain
);
870 /* Print stack frame only if we changed thread. */
871 if (thread_has_changed
&& !is_running (inferior_ptid
))
872 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
876 thread_apply_command (char *tidlist
, int from_tty
)
880 struct cleanup
*old_chain
;
881 struct cleanup
*saved_cmd_cleanup_chain
;
883 struct frame_id saved_frame_id
;
885 int thread_has_changed
= 0;
887 if (tidlist
== NULL
|| *tidlist
== '\000')
888 error (_("Please specify a thread ID list"));
890 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
893 error (_("Please specify a command following the thread ID list"));
895 current_ptid
= inferior_ptid
;
897 if (!is_running (inferior_ptid
))
898 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
900 saved_frame_id
= null_frame_id
;
901 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
903 /* Save a copy of the command in case it is clobbered by
905 saved_cmd
= xstrdup (cmd
);
906 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
907 while (tidlist
< cmd
)
909 struct thread_info
*tp
;
912 start
= strtol (tidlist
, &p
, 10);
914 error (_("Error parsing %s"), tidlist
);
917 while (*tidlist
== ' ' || *tidlist
== '\t')
920 if (*tidlist
== '-') /* Got a range of IDs? */
922 tidlist
++; /* Skip the - */
923 end
= strtol (tidlist
, &p
, 10);
925 error (_("Error parsing %s"), tidlist
);
928 while (*tidlist
== ' ' || *tidlist
== '\t')
934 for (; start
<= end
; start
++)
936 tp
= find_thread_id (start
);
939 warning (_("Unknown thread %d."), start
);
940 else if (!thread_alive (tp
))
941 warning (_("Thread %d has terminated."), start
);
945 context_switch_to (tp
->ptid
);
947 switch_to_thread (tp
->ptid
);
948 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
949 target_tid_to_str (inferior_ptid
));
950 execute_command (cmd
, from_tty
);
951 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
956 if (!ptid_equal (current_ptid
, inferior_ptid
))
957 thread_has_changed
= 1;
959 do_cleanups (saved_cmd_cleanup_chain
);
960 do_cleanups (old_chain
);
961 /* Print stack frame only if we changed thread. */
962 if (thread_has_changed
)
963 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
966 /* Switch to the specified thread. Will dispatch off to thread_apply_command
967 if prefix of arg is `apply'. */
970 thread_command (char *tidstr
, int from_tty
)
974 /* Don't generate an error, just say which thread is current. */
975 if (target_has_stack
)
976 printf_filtered (_("[Current thread is %d (%s)]\n"),
977 pid_to_thread_id (inferior_ptid
),
978 target_tid_to_str (inferior_ptid
));
980 error (_("No stack."));
984 annotate_thread_changed ();
985 gdb_thread_select (uiout
, tidstr
, NULL
);
988 /* Print notices when new threads are attached and detached. */
989 int print_thread_events
= 1;
991 show_print_thread_events (struct ui_file
*file
, int from_tty
,
992 struct cmd_list_element
*c
, const char *value
)
994 fprintf_filtered (file
, _("\
995 Printing of thread events is %s.\n"),
1000 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
1003 struct thread_info
*tp
;
1005 num
= value_as_long (parse_and_eval (tidstr
));
1007 tp
= find_thread_id (num
);
1010 error (_("Thread ID %d not known."), num
);
1012 if (!thread_alive (tp
))
1013 error (_("Thread ID %d has terminated."), num
);
1016 context_switch_to (tp
->ptid
);
1018 switch_to_thread (tp
->ptid
);
1020 ui_out_text (uiout
, "[Switching to thread ");
1021 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
1022 ui_out_text (uiout
, " (");
1023 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
1024 ui_out_text (uiout
, ")]");
1027 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
1029 ui_out_text (uiout
, "(running)\n");
1035 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
1037 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
1038 error_message
, RETURN_MASK_ALL
) < 0)
1043 /* Commands with a prefix of `thread'. */
1044 struct cmd_list_element
*thread_cmd_list
= NULL
;
1047 _initialize_thread (void)
1049 static struct cmd_list_element
*thread_apply_list
= NULL
;
1050 struct cmd_list_element
*c
;
1052 c
= add_info ("threads", info_threads_command
,
1053 _("IDs of currently known threads."));
1054 set_cmd_async_ok (c
);
1056 c
= add_prefix_cmd ("thread", class_run
, thread_command
, _("\
1057 Use this command to switch between threads.\n\
1058 The new thread ID must be currently known."),
1059 &thread_cmd_list
, "thread ", 1, &cmdlist
);
1060 set_cmd_async_ok (c
);
1062 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
1063 _("Apply a command to a list of threads."),
1064 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
1066 add_cmd ("all", class_run
, thread_apply_all_command
,
1067 _("Apply a command to all threads."), &thread_apply_list
);
1070 add_com_alias ("t", "thread", class_run
, 1);
1072 add_setshow_boolean_cmd ("thread-events", no_class
,
1073 &print_thread_events
, _("\
1074 Set printing of thread events (such as thread start and exit)."), _("\
1075 Show printing of thread events (such as thread start and exit)."), NULL
,
1077 show_print_thread_events
,
1078 &setprintlist
, &showprintlist
);