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>
44 /* Definition of struct thread_info exported to gdbthread.h */
46 /* Prototypes for exported functions. */
48 void _initialize_thread (void);
50 /* Prototypes for local functions. */
52 static struct thread_info
*thread_list
= NULL
;
53 static int highest_thread_num
;
55 static struct thread_info
*find_thread_id (int num
);
57 static void thread_command (char *tidstr
, int from_tty
);
58 static void thread_apply_all_command (char *, int);
59 static int thread_alive (struct thread_info
*);
60 static void info_threads_command (char *, int);
61 static void thread_apply_command (char *, int);
62 static void restore_current_thread (ptid_t
);
63 static void prune_threads (void);
66 delete_step_resume_breakpoint (void *arg
)
68 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
69 struct thread_info
*tp
;
71 if (*breakpointp
!= NULL
)
73 delete_breakpoint (*breakpointp
);
74 for (tp
= thread_list
; tp
; tp
= tp
->next
)
75 if (tp
->step_resume_breakpoint
== *breakpointp
)
76 tp
->step_resume_breakpoint
= NULL
;
83 free_thread (struct thread_info
*tp
)
85 /* NOTE: this will take care of any left-over step_resume breakpoints,
86 but not any user-specified thread-specific breakpoints. We can not
87 delete the breakpoint straight-off, because the inferior might not
88 be stopped at the moment. */
89 if (tp
->step_resume_breakpoint
)
90 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
92 /* FIXME: do I ever need to call the back-end to give it a
93 chance at this private data before deleting the thread? */
101 init_thread_list (void)
103 struct thread_info
*tp
, *tpnext
;
105 highest_thread_num
= 0;
109 for (tp
= thread_list
; tp
; tp
= tpnext
)
119 add_thread_silent (ptid_t ptid
)
121 struct thread_info
*tp
;
123 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
124 memset (tp
, 0, sizeof (*tp
));
126 tp
->num
= ++highest_thread_num
;
127 tp
->next
= thread_list
;
130 observer_notify_new_thread (tp
);
136 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
138 struct thread_info
*result
= add_thread_silent (ptid
);
140 result
->private = private;
142 if (print_thread_events
)
143 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
149 add_thread (ptid_t ptid
)
151 return add_thread_with_info (ptid
, NULL
);
155 delete_thread (ptid_t ptid
)
157 struct thread_info
*tp
, *tpprev
;
161 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
162 if (ptid_equal (tp
->ptid
, ptid
))
169 tpprev
->next
= tp
->next
;
171 thread_list
= tp
->next
;
173 observer_notify_thread_exit (tp
);
178 static struct thread_info
*
179 find_thread_id (int num
)
181 struct thread_info
*tp
;
183 for (tp
= thread_list
; tp
; tp
= tp
->next
)
190 /* Find a thread_info by matching PTID. */
192 find_thread_pid (ptid_t ptid
)
194 struct thread_info
*tp
;
196 for (tp
= thread_list
; tp
; tp
= tp
->next
)
197 if (ptid_equal (tp
->ptid
, ptid
))
204 * Thread iterator function.
206 * Calls a callback function once for each thread, so long as
207 * the callback function returns false. If the callback function
208 * returns true, the iteration will end and the current thread
209 * will be returned. This can be useful for implementing a
210 * search for a thread with arbitrary attributes, or for applying
211 * some operation to every thread.
213 * FIXME: some of the existing functionality, such as
214 * "Thread apply all", might be rewritten using this functionality.
218 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
221 struct thread_info
*tp
;
223 for (tp
= thread_list
; tp
; tp
= tp
->next
)
224 if ((*callback
) (tp
, data
))
231 valid_thread_id (int num
)
233 struct thread_info
*tp
;
235 for (tp
= thread_list
; tp
; tp
= tp
->next
)
243 pid_to_thread_id (ptid_t ptid
)
245 struct thread_info
*tp
;
247 for (tp
= thread_list
; tp
; tp
= tp
->next
)
248 if (ptid_equal (tp
->ptid
, ptid
))
255 thread_id_to_pid (int num
)
257 struct thread_info
*thread
= find_thread_id (num
);
261 return pid_to_ptid (-1);
265 in_thread_list (ptid_t ptid
)
267 struct thread_info
*tp
;
269 for (tp
= thread_list
; tp
; tp
= tp
->next
)
270 if (ptid_equal (tp
->ptid
, ptid
))
273 return 0; /* Never heard of 'im */
276 /* Print a list of thread ids currently known, and the total number of
277 threads. To be used from within catch_errors. */
279 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
281 struct thread_info
*tp
;
283 struct cleanup
*cleanup_chain
;
286 target_find_new_threads ();
288 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
290 for (tp
= thread_list
; tp
; tp
= tp
->next
)
293 ui_out_field_int (uiout
, "thread-id", tp
->num
);
296 do_cleanups (cleanup_chain
);
297 ui_out_field_int (uiout
, "number-of-threads", num
);
301 /* Official gdblib interface function to get a list of thread ids and
304 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
306 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
307 error_message
, RETURN_MASK_ALL
) < 0)
312 /* Load infrun state for the thread PID. */
315 load_infrun_state (ptid_t ptid
,
318 struct breakpoint
**step_resume_breakpoint
,
319 CORE_ADDR
*step_range_start
,
320 CORE_ADDR
*step_range_end
,
321 struct frame_id
*step_frame_id
,
322 int *handling_longjmp
,
323 int *stepping_over_breakpoint
,
324 int *stepping_through_solib_after_catch
,
325 bpstat
*stepping_through_solib_catchpoints
,
327 struct symtab
**current_symtab
)
329 struct thread_info
*tp
;
331 /* If we can't find the thread, then we're debugging a single threaded
332 process. No need to do anything in that case. */
333 tp
= find_thread_id (pid_to_thread_id (ptid
));
337 *prev_pc
= tp
->prev_pc
;
338 *trap_expected
= tp
->trap_expected
;
339 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
340 *step_range_start
= tp
->step_range_start
;
341 *step_range_end
= tp
->step_range_end
;
342 *step_frame_id
= tp
->step_frame_id
;
343 *handling_longjmp
= tp
->handling_longjmp
;
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 handling_longjmp
,
364 int stepping_over_breakpoint
,
365 int stepping_through_solib_after_catch
,
366 bpstat stepping_through_solib_catchpoints
,
368 struct symtab
*current_symtab
)
370 struct thread_info
*tp
;
372 /* If we can't find the thread, then we're debugging a single-threaded
373 process. Nothing to do in that case. */
374 tp
= find_thread_id (pid_to_thread_id (ptid
));
378 tp
->prev_pc
= prev_pc
;
379 tp
->trap_expected
= trap_expected
;
380 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
381 tp
->step_range_start
= step_range_start
;
382 tp
->step_range_end
= step_range_end
;
383 tp
->step_frame_id
= (*step_frame_id
);
384 tp
->handling_longjmp
= handling_longjmp
;
385 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
386 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
387 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
388 tp
->current_line
= current_line
;
389 tp
->current_symtab
= current_symtab
;
392 /* Return true if TP is an active thread. */
394 thread_alive (struct thread_info
*tp
)
396 if (PIDGET (tp
->ptid
) == -1)
398 if (!target_thread_alive (tp
->ptid
))
400 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
409 struct thread_info
*tp
, *next
;
411 for (tp
= thread_list
; tp
; tp
= next
)
414 if (!thread_alive (tp
))
415 delete_thread (tp
->ptid
);
419 /* Prints the list of threads and their details on UIOUT.
420 This is a version of 'info_thread_command' suitable for
422 If REQESTED_THREAD is not -1, it's the GDB id of the thread
423 that should be printed. Otherwise, all threads are
426 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
428 struct thread_info
*tp
;
430 struct frame_info
*cur_frame
;
431 struct cleanup
*old_chain
;
432 struct frame_id saved_frame_id
;
434 int current_thread
= -1;
436 /* Backup current thread and selected frame. */
437 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
438 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
440 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
443 target_find_new_threads ();
444 current_ptid
= inferior_ptid
;
445 for (tp
= thread_list
; tp
; tp
= tp
->next
)
447 struct cleanup
*chain2
;
449 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
452 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
454 if (ptid_equal (tp
->ptid
, current_ptid
))
456 current_thread
= tp
->num
;
457 ui_out_text (uiout
, "* ");
460 ui_out_text (uiout
, " ");
462 ui_out_field_int (uiout
, "id", tp
->num
);
463 ui_out_text (uiout
, " ");
464 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
466 extra_info
= target_extra_thread_info (tp
);
469 ui_out_text (uiout
, " (");
470 ui_out_field_string (uiout
, "details", extra_info
);
471 ui_out_text (uiout
, ")");
473 ui_out_text (uiout
, " ");
474 /* That switch put us at the top of the stack (leaf frame). */
475 switch_to_thread (tp
->ptid
);
476 print_stack_frame (get_selected_frame (NULL
),
477 /* For MI output, print frame level. */
478 ui_out_is_mi_like_p (uiout
),
481 do_cleanups (chain2
);
484 /* Restores the current thread and the frame selected before
485 the "info threads" command. */
486 do_cleanups (old_chain
);
488 if (requested_thread
== -1)
490 gdb_assert (current_thread
!= -1 || !thread_list
);
491 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
492 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
495 /* If case we were not able to find the original frame, print the
496 new selected frame. */
497 if (frame_find_by_id (saved_frame_id
) == NULL
)
499 warning (_("Couldn't restore frame in current thread, at frame 0"));
500 /* For MI, we should probably have a notification about
501 current frame change. But this error is not very likely, so
502 don't bother for now. */
503 if (!ui_out_is_mi_like_p (uiout
))
504 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
509 /* Print information about currently known threads
511 * Note: this has the drawback that it _really_ switches
512 * threads, which frees the frame cache. A no-side
513 * effects info-threads command would be nicer.
517 info_threads_command (char *arg
, int from_tty
)
519 print_thread_info (uiout
, -1);
522 /* Switch from one thread to another. */
525 switch_to_thread (ptid_t ptid
)
527 if (ptid_equal (ptid
, inferior_ptid
))
530 inferior_ptid
= ptid
;
531 reinit_frame_cache ();
532 registers_changed ();
533 stop_pc
= read_pc ();
537 restore_current_thread (ptid_t ptid
)
539 if (!ptid_equal (ptid
, inferior_ptid
))
541 switch_to_thread (ptid
);
546 restore_selected_frame (struct frame_id a_frame_id
)
548 struct frame_info
*selected_frame_info
= NULL
;
550 if (frame_id_eq (a_frame_id
, null_frame_id
))
553 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
555 select_frame (selected_frame_info
);
559 struct current_thread_cleanup
561 ptid_t inferior_ptid
;
562 struct frame_id selected_frame_id
;
566 do_restore_current_thread_cleanup (void *arg
)
568 struct current_thread_cleanup
*old
= arg
;
569 restore_current_thread (old
->inferior_ptid
);
570 restore_selected_frame (old
->selected_frame_id
);
575 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
576 struct frame_id a_frame_id
)
578 struct current_thread_cleanup
*old
579 = xmalloc (sizeof (struct current_thread_cleanup
));
580 old
->inferior_ptid
= inferior_ptid
;
581 old
->selected_frame_id
= a_frame_id
;
582 return make_cleanup (do_restore_current_thread_cleanup
, old
);
585 /* Apply a GDB command to a list of threads. List syntax is a whitespace
586 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
587 of two numbers seperated by a hyphen. Examples:
589 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
590 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
591 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
595 thread_apply_all_command (char *cmd
, int from_tty
)
597 struct thread_info
*tp
;
598 struct cleanup
*old_chain
;
599 struct cleanup
*saved_cmd_cleanup_chain
;
601 struct frame_id saved_frame_id
;
603 int thread_has_changed
= 0;
605 if (cmd
== NULL
|| *cmd
== '\000')
606 error (_("Please specify a command following the thread ID list"));
608 current_ptid
= inferior_ptid
;
609 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
610 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
612 /* It is safe to update the thread list now, before
613 traversing it for "thread apply all". MVS */
614 target_find_new_threads ();
616 /* Save a copy of the command in case it is clobbered by
618 saved_cmd
= xstrdup (cmd
);
619 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
620 for (tp
= thread_list
; tp
; tp
= tp
->next
)
621 if (thread_alive (tp
))
623 switch_to_thread (tp
->ptid
);
624 printf_filtered (_("\nThread %d (%s):\n"),
625 tp
->num
, target_tid_to_str (inferior_ptid
));
626 execute_command (cmd
, from_tty
);
627 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
630 if (!ptid_equal (current_ptid
, inferior_ptid
))
631 thread_has_changed
= 1;
633 do_cleanups (saved_cmd_cleanup_chain
);
634 do_cleanups (old_chain
);
635 /* Print stack frame only if we changed thread. */
636 if (thread_has_changed
)
637 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
642 thread_apply_command (char *tidlist
, int from_tty
)
646 struct cleanup
*old_chain
;
647 struct cleanup
*saved_cmd_cleanup_chain
;
649 struct frame_id saved_frame_id
;
651 int thread_has_changed
= 0;
653 if (tidlist
== NULL
|| *tidlist
== '\000')
654 error (_("Please specify a thread ID list"));
656 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
659 error (_("Please specify a command following the thread ID list"));
661 current_ptid
= inferior_ptid
;
662 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
663 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
665 /* Save a copy of the command in case it is clobbered by
667 saved_cmd
= xstrdup (cmd
);
668 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
669 while (tidlist
< cmd
)
671 struct thread_info
*tp
;
674 start
= strtol (tidlist
, &p
, 10);
676 error (_("Error parsing %s"), tidlist
);
679 while (*tidlist
== ' ' || *tidlist
== '\t')
682 if (*tidlist
== '-') /* Got a range of IDs? */
684 tidlist
++; /* Skip the - */
685 end
= strtol (tidlist
, &p
, 10);
687 error (_("Error parsing %s"), tidlist
);
690 while (*tidlist
== ' ' || *tidlist
== '\t')
696 for (; start
<= end
; start
++)
698 tp
= find_thread_id (start
);
701 warning (_("Unknown thread %d."), start
);
702 else if (!thread_alive (tp
))
703 warning (_("Thread %d has terminated."), start
);
706 switch_to_thread (tp
->ptid
);
707 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
708 target_tid_to_str (inferior_ptid
));
709 execute_command (cmd
, from_tty
);
710 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
715 if (!ptid_equal (current_ptid
, inferior_ptid
))
716 thread_has_changed
= 1;
718 do_cleanups (saved_cmd_cleanup_chain
);
719 do_cleanups (old_chain
);
720 /* Print stack frame only if we changed thread. */
721 if (thread_has_changed
)
722 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
725 /* Switch to the specified thread. Will dispatch off to thread_apply_command
726 if prefix of arg is `apply'. */
729 thread_command (char *tidstr
, int from_tty
)
733 /* Don't generate an error, just say which thread is current. */
734 if (target_has_stack
)
735 printf_filtered (_("[Current thread is %d (%s)]\n"),
736 pid_to_thread_id (inferior_ptid
),
737 target_tid_to_str (inferior_ptid
));
739 error (_("No stack."));
743 gdb_thread_select (uiout
, tidstr
, NULL
);
746 /* Print notices when new threads are attached and detached. */
747 int print_thread_events
= 1;
749 show_print_thread_events (struct ui_file
*file
, int from_tty
,
750 struct cmd_list_element
*c
, const char *value
)
752 fprintf_filtered (file
, _("\
753 Printing of thread events is %s.\n"),
758 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
761 struct thread_info
*tp
;
763 num
= value_as_long (parse_and_eval (tidstr
));
765 tp
= find_thread_id (num
);
768 error (_("Thread ID %d not known."), num
);
770 if (!thread_alive (tp
))
771 error (_("Thread ID %d has terminated."), num
);
773 switch_to_thread (tp
->ptid
);
775 ui_out_text (uiout
, "[Switching to thread ");
776 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
777 ui_out_text (uiout
, " (");
778 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
779 ui_out_text (uiout
, ")]");
781 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
786 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
788 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
789 error_message
, RETURN_MASK_ALL
) < 0)
794 /* Commands with a prefix of `thread'. */
795 struct cmd_list_element
*thread_cmd_list
= NULL
;
798 _initialize_thread (void)
800 static struct cmd_list_element
*thread_apply_list
= NULL
;
802 add_info ("threads", info_threads_command
,
803 _("IDs of currently known threads."));
805 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
806 Use this command to switch between threads.\n\
807 The new thread ID must be currently known."),
808 &thread_cmd_list
, "thread ", 1, &cmdlist
);
810 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
811 _("Apply a command to a list of threads."),
812 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
814 add_cmd ("all", class_run
, thread_apply_all_command
,
815 _("Apply a command to all threads."), &thread_apply_list
);
818 add_com_alias ("t", "thread", class_run
, 1);
820 add_setshow_boolean_cmd ("thread-events", no_class
,
821 &print_thread_events
, _("\
822 Set printing of thread events (e.g., thread start and exit)."), _("\
823 Show printing of thread events (e.g., thread start and exit)."), NULL
,
825 show_print_thread_events
,
826 &setprintlist
, &showprintlist
);