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
;
133 add_thread_with_info (ptid_t ptid
, struct private_thread_info
*private)
135 struct thread_info
*result
= add_thread_silent (ptid
);
137 result
->private = private;
139 if (print_thread_events
)
140 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
142 observer_notify_new_thread (result
);
148 add_thread (ptid_t ptid
)
150 return add_thread_with_info (ptid
, NULL
);
154 delete_thread (ptid_t ptid
)
156 struct thread_info
*tp
, *tpprev
;
160 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
161 if (ptid_equal (tp
->ptid
, ptid
))
168 tpprev
->next
= tp
->next
;
170 thread_list
= tp
->next
;
175 static struct thread_info
*
176 find_thread_id (int num
)
178 struct thread_info
*tp
;
180 for (tp
= thread_list
; tp
; tp
= tp
->next
)
187 /* Find a thread_info by matching PTID. */
189 find_thread_pid (ptid_t ptid
)
191 struct thread_info
*tp
;
193 for (tp
= thread_list
; tp
; tp
= tp
->next
)
194 if (ptid_equal (tp
->ptid
, ptid
))
201 * Thread iterator function.
203 * Calls a callback function once for each thread, so long as
204 * the callback function returns false. If the callback function
205 * returns true, the iteration will end and the current thread
206 * will be returned. This can be useful for implementing a
207 * search for a thread with arbitrary attributes, or for applying
208 * some operation to every thread.
210 * FIXME: some of the existing functionality, such as
211 * "Thread apply all", might be rewritten using this functionality.
215 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
218 struct thread_info
*tp
;
220 for (tp
= thread_list
; tp
; tp
= tp
->next
)
221 if ((*callback
) (tp
, data
))
228 valid_thread_id (int num
)
230 struct thread_info
*tp
;
232 for (tp
= thread_list
; tp
; tp
= tp
->next
)
240 pid_to_thread_id (ptid_t ptid
)
242 struct thread_info
*tp
;
244 for (tp
= thread_list
; tp
; tp
= tp
->next
)
245 if (ptid_equal (tp
->ptid
, ptid
))
252 thread_id_to_pid (int num
)
254 struct thread_info
*thread
= find_thread_id (num
);
258 return pid_to_ptid (-1);
262 in_thread_list (ptid_t ptid
)
264 struct thread_info
*tp
;
266 for (tp
= thread_list
; tp
; tp
= tp
->next
)
267 if (ptid_equal (tp
->ptid
, ptid
))
270 return 0; /* Never heard of 'im */
273 /* Print a list of thread ids currently known, and the total number of
274 threads. To be used from within catch_errors. */
276 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
278 struct thread_info
*tp
;
280 struct cleanup
*cleanup_chain
;
283 target_find_new_threads ();
285 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
287 for (tp
= thread_list
; tp
; tp
= tp
->next
)
290 ui_out_field_int (uiout
, "thread-id", tp
->num
);
293 do_cleanups (cleanup_chain
);
294 ui_out_field_int (uiout
, "number-of-threads", num
);
298 /* Official gdblib interface function to get a list of thread ids and
301 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
303 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
304 error_message
, RETURN_MASK_ALL
) < 0)
309 /* Load infrun state for the thread PID. */
312 load_infrun_state (ptid_t ptid
,
315 struct breakpoint
**step_resume_breakpoint
,
316 CORE_ADDR
*step_range_start
,
317 CORE_ADDR
*step_range_end
,
318 struct frame_id
*step_frame_id
,
319 int *handling_longjmp
,
320 int *stepping_over_breakpoint
,
321 int *stepping_through_solib_after_catch
,
322 bpstat
*stepping_through_solib_catchpoints
,
324 struct symtab
**current_symtab
)
326 struct thread_info
*tp
;
328 /* If we can't find the thread, then we're debugging a single threaded
329 process. No need to do anything in that case. */
330 tp
= find_thread_id (pid_to_thread_id (ptid
));
334 *prev_pc
= tp
->prev_pc
;
335 *trap_expected
= tp
->trap_expected
;
336 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
337 *step_range_start
= tp
->step_range_start
;
338 *step_range_end
= tp
->step_range_end
;
339 *step_frame_id
= tp
->step_frame_id
;
340 *handling_longjmp
= tp
->handling_longjmp
;
341 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
342 *stepping_through_solib_after_catch
=
343 tp
->stepping_through_solib_after_catch
;
344 *stepping_through_solib_catchpoints
=
345 tp
->stepping_through_solib_catchpoints
;
346 *current_line
= tp
->current_line
;
347 *current_symtab
= tp
->current_symtab
;
350 /* Save infrun state for the thread PID. */
353 save_infrun_state (ptid_t ptid
,
356 struct breakpoint
*step_resume_breakpoint
,
357 CORE_ADDR step_range_start
,
358 CORE_ADDR step_range_end
,
359 const struct frame_id
*step_frame_id
,
360 int handling_longjmp
,
361 int stepping_over_breakpoint
,
362 int stepping_through_solib_after_catch
,
363 bpstat stepping_through_solib_catchpoints
,
365 struct symtab
*current_symtab
)
367 struct thread_info
*tp
;
369 /* If we can't find the thread, then we're debugging a single-threaded
370 process. Nothing to do in that case. */
371 tp
= find_thread_id (pid_to_thread_id (ptid
));
375 tp
->prev_pc
= prev_pc
;
376 tp
->trap_expected
= trap_expected
;
377 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
378 tp
->step_range_start
= step_range_start
;
379 tp
->step_range_end
= step_range_end
;
380 tp
->step_frame_id
= (*step_frame_id
);
381 tp
->handling_longjmp
= handling_longjmp
;
382 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
383 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
384 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
385 tp
->current_line
= current_line
;
386 tp
->current_symtab
= current_symtab
;
389 /* Return true if TP is an active thread. */
391 thread_alive (struct thread_info
*tp
)
393 if (PIDGET (tp
->ptid
) == -1)
395 if (!target_thread_alive (tp
->ptid
))
397 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
406 struct thread_info
*tp
, *next
;
408 for (tp
= thread_list
; tp
; tp
= next
)
411 if (!thread_alive (tp
))
412 delete_thread (tp
->ptid
);
416 /* Prints the list of threads and their details on UIOUT.
417 This is a version of 'info_thread_command' suitable for
419 If REQESTED_THREAD is not -1, it's the GDB id of the thread
420 that should be printed. Otherwise, all threads are
423 print_thread_info (struct ui_out
*uiout
, int requested_thread
)
425 struct thread_info
*tp
;
427 struct frame_info
*cur_frame
;
428 struct cleanup
*old_chain
;
429 struct frame_id saved_frame_id
;
431 int current_thread
= -1;
433 /* Backup current thread and selected frame. */
434 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
435 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
437 make_cleanup_ui_out_list_begin_end (uiout
, "threads");
440 target_find_new_threads ();
441 current_ptid
= inferior_ptid
;
442 for (tp
= thread_list
; tp
; tp
= tp
->next
)
444 struct cleanup
*chain2
;
446 if (requested_thread
!= -1 && tp
->num
!= requested_thread
)
449 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
451 if (ptid_equal (tp
->ptid
, current_ptid
))
453 current_thread
= tp
->num
;
454 ui_out_text (uiout
, "* ");
457 ui_out_text (uiout
, " ");
459 ui_out_field_int (uiout
, "id", tp
->num
);
460 ui_out_text (uiout
, " ");
461 ui_out_field_string (uiout
, "target-id", target_tid_to_str (tp
->ptid
));
463 extra_info
= target_extra_thread_info (tp
);
466 ui_out_text (uiout
, " (");
467 ui_out_field_string (uiout
, "details", extra_info
);
468 ui_out_text (uiout
, ")");
470 ui_out_text (uiout
, " ");
471 /* That switch put us at the top of the stack (leaf frame). */
472 switch_to_thread (tp
->ptid
);
473 print_stack_frame (get_selected_frame (NULL
),
474 /* For MI output, print frame level. */
475 ui_out_is_mi_like_p (uiout
),
478 do_cleanups (chain2
);
481 /* Restores the current thread and the frame selected before
482 the "info threads" command. */
483 do_cleanups (old_chain
);
485 if (requested_thread
== -1)
487 gdb_assert (current_thread
!= -1 || !thread_list
);
488 if (current_thread
!= -1 && ui_out_is_mi_like_p (uiout
))
489 ui_out_field_int (uiout
, "current-thread-id", current_thread
);
492 /* If case we were not able to find the original frame, print the
493 new selected frame. */
494 if (frame_find_by_id (saved_frame_id
) == NULL
)
496 warning (_("Couldn't restore frame in current thread, at frame 0"));
497 /* For MI, we should probably have a notification about
498 current frame change. But this error is not very likely, so
499 don't bother for now. */
500 if (!ui_out_is_mi_like_p (uiout
))
501 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
506 /* Print information about currently known threads
508 * Note: this has the drawback that it _really_ switches
509 * threads, which frees the frame cache. A no-side
510 * effects info-threads command would be nicer.
514 info_threads_command (char *arg
, int from_tty
)
516 print_thread_info (uiout
, -1);
519 /* Switch from one thread to another. */
522 switch_to_thread (ptid_t ptid
)
524 if (ptid_equal (ptid
, inferior_ptid
))
527 inferior_ptid
= ptid
;
528 reinit_frame_cache ();
529 registers_changed ();
530 stop_pc
= read_pc ();
534 restore_current_thread (ptid_t ptid
)
536 if (!ptid_equal (ptid
, inferior_ptid
))
538 switch_to_thread (ptid
);
543 restore_selected_frame (struct frame_id a_frame_id
)
545 struct frame_info
*selected_frame_info
= NULL
;
547 if (frame_id_eq (a_frame_id
, null_frame_id
))
550 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
552 select_frame (selected_frame_info
);
556 struct current_thread_cleanup
558 ptid_t inferior_ptid
;
559 struct frame_id selected_frame_id
;
563 do_restore_current_thread_cleanup (void *arg
)
565 struct current_thread_cleanup
*old
= arg
;
566 restore_current_thread (old
->inferior_ptid
);
567 restore_selected_frame (old
->selected_frame_id
);
572 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
573 struct frame_id a_frame_id
)
575 struct current_thread_cleanup
*old
576 = xmalloc (sizeof (struct current_thread_cleanup
));
577 old
->inferior_ptid
= inferior_ptid
;
578 old
->selected_frame_id
= a_frame_id
;
579 return make_cleanup (do_restore_current_thread_cleanup
, old
);
582 /* Apply a GDB command to a list of threads. List syntax is a whitespace
583 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
584 of two numbers seperated by a hyphen. Examples:
586 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
587 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
588 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
592 thread_apply_all_command (char *cmd
, int from_tty
)
594 struct thread_info
*tp
;
595 struct cleanup
*old_chain
;
596 struct cleanup
*saved_cmd_cleanup_chain
;
598 struct frame_id saved_frame_id
;
600 int thread_has_changed
= 0;
602 if (cmd
== NULL
|| *cmd
== '\000')
603 error (_("Please specify a command following the thread ID list"));
605 current_ptid
= inferior_ptid
;
606 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
607 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
609 /* It is safe to update the thread list now, before
610 traversing it for "thread apply all". MVS */
611 target_find_new_threads ();
613 /* Save a copy of the command in case it is clobbered by
615 saved_cmd
= xstrdup (cmd
);
616 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
617 for (tp
= thread_list
; tp
; tp
= tp
->next
)
618 if (thread_alive (tp
))
620 switch_to_thread (tp
->ptid
);
621 printf_filtered (_("\nThread %d (%s):\n"),
622 tp
->num
, target_tid_to_str (inferior_ptid
));
623 execute_command (cmd
, from_tty
);
624 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
627 if (!ptid_equal (current_ptid
, inferior_ptid
))
628 thread_has_changed
= 1;
630 do_cleanups (saved_cmd_cleanup_chain
);
631 do_cleanups (old_chain
);
632 /* Print stack frame only if we changed thread. */
633 if (thread_has_changed
)
634 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
639 thread_apply_command (char *tidlist
, int from_tty
)
643 struct cleanup
*old_chain
;
644 struct cleanup
*saved_cmd_cleanup_chain
;
646 struct frame_id saved_frame_id
;
648 int thread_has_changed
= 0;
650 if (tidlist
== NULL
|| *tidlist
== '\000')
651 error (_("Please specify a thread ID list"));
653 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
656 error (_("Please specify a command following the thread ID list"));
658 current_ptid
= inferior_ptid
;
659 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
660 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
662 /* Save a copy of the command in case it is clobbered by
664 saved_cmd
= xstrdup (cmd
);
665 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
666 while (tidlist
< cmd
)
668 struct thread_info
*tp
;
671 start
= strtol (tidlist
, &p
, 10);
673 error (_("Error parsing %s"), tidlist
);
676 while (*tidlist
== ' ' || *tidlist
== '\t')
679 if (*tidlist
== '-') /* Got a range of IDs? */
681 tidlist
++; /* Skip the - */
682 end
= strtol (tidlist
, &p
, 10);
684 error (_("Error parsing %s"), tidlist
);
687 while (*tidlist
== ' ' || *tidlist
== '\t')
693 for (; start
<= end
; start
++)
695 tp
= find_thread_id (start
);
698 warning (_("Unknown thread %d."), start
);
699 else if (!thread_alive (tp
))
700 warning (_("Thread %d has terminated."), start
);
703 switch_to_thread (tp
->ptid
);
704 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
705 target_tid_to_str (inferior_ptid
));
706 execute_command (cmd
, from_tty
);
707 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
712 if (!ptid_equal (current_ptid
, inferior_ptid
))
713 thread_has_changed
= 1;
715 do_cleanups (saved_cmd_cleanup_chain
);
716 do_cleanups (old_chain
);
717 /* Print stack frame only if we changed thread. */
718 if (thread_has_changed
)
719 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
722 /* Switch to the specified thread. Will dispatch off to thread_apply_command
723 if prefix of arg is `apply'. */
726 thread_command (char *tidstr
, int from_tty
)
730 /* Don't generate an error, just say which thread is current. */
731 if (target_has_stack
)
732 printf_filtered (_("[Current thread is %d (%s)]\n"),
733 pid_to_thread_id (inferior_ptid
),
734 target_tid_to_str (inferior_ptid
));
736 error (_("No stack."));
740 gdb_thread_select (uiout
, tidstr
, NULL
);
743 /* Print notices when new threads are attached and detached. */
744 int print_thread_events
= 1;
746 show_print_thread_events (struct ui_file
*file
, int from_tty
,
747 struct cmd_list_element
*c
, const char *value
)
749 fprintf_filtered (file
, _("\
750 Printing of thread events is %s.\n"),
755 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
758 struct thread_info
*tp
;
760 num
= value_as_long (parse_and_eval (tidstr
));
762 tp
= find_thread_id (num
);
765 error (_("Thread ID %d not known."), num
);
767 if (!thread_alive (tp
))
768 error (_("Thread ID %d has terminated."), num
);
770 switch_to_thread (tp
->ptid
);
772 ui_out_text (uiout
, "[Switching to thread ");
773 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
774 ui_out_text (uiout
, " (");
775 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
776 ui_out_text (uiout
, ")]");
778 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
783 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
785 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
786 error_message
, RETURN_MASK_ALL
) < 0)
791 /* Commands with a prefix of `thread'. */
792 struct cmd_list_element
*thread_cmd_list
= NULL
;
795 _initialize_thread (void)
797 static struct cmd_list_element
*thread_apply_list
= NULL
;
799 add_info ("threads", info_threads_command
,
800 _("IDs of currently known threads."));
802 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
803 Use this command to switch between threads.\n\
804 The new thread ID must be currently known."),
805 &thread_cmd_list
, "thread ", 1, &cmdlist
);
807 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
808 _("Apply a command to a list of threads."),
809 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
811 add_cmd ("all", class_run
, thread_apply_all_command
,
812 _("Apply a command to all threads."), &thread_apply_list
);
815 add_com_alias ("t", "thread", class_run
, 1);
817 add_setshow_boolean_cmd ("thread-events", no_class
,
818 &print_thread_events
, _("\
819 Set printing of thread events (e.g., thread start and exit)."), _("\
820 Show printing of thread events (e.g., thread start and exit)."), NULL
,
822 show_print_thread_events
,
823 &setprintlist
, &showprintlist
);