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>
43 /* Definition of struct thread_info exported to gdbthread.h */
45 /* Prototypes for exported functions. */
47 void _initialize_thread (void);
49 /* Prototypes for local functions. */
51 static struct thread_info
*thread_list
= NULL
;
52 static int highest_thread_num
;
54 static struct thread_info
*find_thread_id (int num
);
56 static void thread_command (char *tidstr
, int from_tty
);
57 static void thread_apply_all_command (char *, int);
58 static int thread_alive (struct thread_info
*);
59 static void info_threads_command (char *, int);
60 static void thread_apply_command (char *, int);
61 static void restore_current_thread (ptid_t
);
62 static void prune_threads (void);
63 static struct cleanup
*make_cleanup_restore_current_thread (ptid_t
,
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
;
134 add_thread (ptid_t ptid
)
136 struct thread_info
*result
= add_thread_silent (ptid
);
138 if (print_thread_events
)
139 printf_filtered (_("[New %s]\n"), target_pid_to_str (ptid
));
145 delete_thread (ptid_t ptid
)
147 struct thread_info
*tp
, *tpprev
;
151 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
152 if (ptid_equal (tp
->ptid
, ptid
))
159 tpprev
->next
= tp
->next
;
161 thread_list
= tp
->next
;
166 static struct thread_info
*
167 find_thread_id (int num
)
169 struct thread_info
*tp
;
171 for (tp
= thread_list
; tp
; tp
= tp
->next
)
178 /* Find a thread_info by matching PTID. */
180 find_thread_pid (ptid_t ptid
)
182 struct thread_info
*tp
;
184 for (tp
= thread_list
; tp
; tp
= tp
->next
)
185 if (ptid_equal (tp
->ptid
, ptid
))
192 * Thread iterator function.
194 * Calls a callback function once for each thread, so long as
195 * the callback function returns false. If the callback function
196 * returns true, the iteration will end and the current thread
197 * will be returned. This can be useful for implementing a
198 * search for a thread with arbitrary attributes, or for applying
199 * some operation to every thread.
201 * FIXME: some of the existing functionality, such as
202 * "Thread apply all", might be rewritten using this functionality.
206 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
209 struct thread_info
*tp
;
211 for (tp
= thread_list
; tp
; tp
= tp
->next
)
212 if ((*callback
) (tp
, data
))
219 valid_thread_id (int num
)
221 struct thread_info
*tp
;
223 for (tp
= thread_list
; tp
; tp
= tp
->next
)
231 pid_to_thread_id (ptid_t ptid
)
233 struct thread_info
*tp
;
235 for (tp
= thread_list
; tp
; tp
= tp
->next
)
236 if (ptid_equal (tp
->ptid
, ptid
))
243 thread_id_to_pid (int num
)
245 struct thread_info
*thread
= find_thread_id (num
);
249 return pid_to_ptid (-1);
253 in_thread_list (ptid_t ptid
)
255 struct thread_info
*tp
;
257 for (tp
= thread_list
; tp
; tp
= tp
->next
)
258 if (ptid_equal (tp
->ptid
, ptid
))
261 return 0; /* Never heard of 'im */
264 /* Print a list of thread ids currently known, and the total number of
265 threads. To be used from within catch_errors. */
267 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
269 struct thread_info
*tp
;
271 struct cleanup
*cleanup_chain
;
274 target_find_new_threads ();
276 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
278 for (tp
= thread_list
; tp
; tp
= tp
->next
)
281 ui_out_field_int (uiout
, "thread-id", tp
->num
);
284 do_cleanups (cleanup_chain
);
285 ui_out_field_int (uiout
, "number-of-threads", num
);
289 /* Official gdblib interface function to get a list of thread ids and
292 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
294 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
295 error_message
, RETURN_MASK_ALL
) < 0)
300 /* Load infrun state for the thread PID. */
303 load_infrun_state (ptid_t ptid
,
306 struct breakpoint
**step_resume_breakpoint
,
307 CORE_ADDR
*step_range_start
,
308 CORE_ADDR
*step_range_end
,
309 struct frame_id
*step_frame_id
,
310 int *handling_longjmp
,
311 int *stepping_over_breakpoint
,
312 int *stepping_through_solib_after_catch
,
313 bpstat
*stepping_through_solib_catchpoints
,
315 struct symtab
**current_symtab
)
317 struct thread_info
*tp
;
319 /* If we can't find the thread, then we're debugging a single threaded
320 process. No need to do anything in that case. */
321 tp
= find_thread_id (pid_to_thread_id (ptid
));
325 *prev_pc
= tp
->prev_pc
;
326 *trap_expected
= tp
->trap_expected
;
327 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
328 *step_range_start
= tp
->step_range_start
;
329 *step_range_end
= tp
->step_range_end
;
330 *step_frame_id
= tp
->step_frame_id
;
331 *handling_longjmp
= tp
->handling_longjmp
;
332 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
333 *stepping_through_solib_after_catch
=
334 tp
->stepping_through_solib_after_catch
;
335 *stepping_through_solib_catchpoints
=
336 tp
->stepping_through_solib_catchpoints
;
337 *current_line
= tp
->current_line
;
338 *current_symtab
= tp
->current_symtab
;
341 /* Save infrun state for the thread PID. */
344 save_infrun_state (ptid_t ptid
,
347 struct breakpoint
*step_resume_breakpoint
,
348 CORE_ADDR step_range_start
,
349 CORE_ADDR step_range_end
,
350 const struct frame_id
*step_frame_id
,
351 int handling_longjmp
,
352 int stepping_over_breakpoint
,
353 int stepping_through_solib_after_catch
,
354 bpstat stepping_through_solib_catchpoints
,
356 struct symtab
*current_symtab
)
358 struct thread_info
*tp
;
360 /* If we can't find the thread, then we're debugging a single-threaded
361 process. Nothing to do in that case. */
362 tp
= find_thread_id (pid_to_thread_id (ptid
));
366 tp
->prev_pc
= prev_pc
;
367 tp
->trap_expected
= trap_expected
;
368 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
369 tp
->step_range_start
= step_range_start
;
370 tp
->step_range_end
= step_range_end
;
371 tp
->step_frame_id
= (*step_frame_id
);
372 tp
->handling_longjmp
= handling_longjmp
;
373 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
374 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
375 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
376 tp
->current_line
= current_line
;
377 tp
->current_symtab
= current_symtab
;
380 /* Return true if TP is an active thread. */
382 thread_alive (struct thread_info
*tp
)
384 if (PIDGET (tp
->ptid
) == -1)
386 if (!target_thread_alive (tp
->ptid
))
388 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
397 struct thread_info
*tp
, *next
;
399 for (tp
= thread_list
; tp
; tp
= next
)
402 if (!thread_alive (tp
))
403 delete_thread (tp
->ptid
);
407 /* Print information about currently known threads
409 * Note: this has the drawback that it _really_ switches
410 * threads, which frees the frame cache. A no-side
411 * effects info-threads command would be nicer.
415 info_threads_command (char *arg
, int from_tty
)
417 struct thread_info
*tp
;
419 struct frame_info
*cur_frame
;
420 struct cleanup
*old_chain
;
421 struct frame_id saved_frame_id
;
424 /* Backup current thread and selected frame. */
425 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
426 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
429 target_find_new_threads ();
430 current_ptid
= inferior_ptid
;
431 for (tp
= thread_list
; tp
; tp
= tp
->next
)
433 if (ptid_equal (tp
->ptid
, current_ptid
))
434 printf_filtered ("* ");
436 printf_filtered (" ");
438 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
440 extra_info
= target_extra_thread_info (tp
);
442 printf_filtered (" (%s)", extra_info
);
444 /* That switch put us at the top of the stack (leaf frame). */
445 switch_to_thread (tp
->ptid
);
446 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
449 /* Restores the current thread and the frame selected before
450 the "info threads" command. */
451 do_cleanups (old_chain
);
453 /* If case we were not able to find the original frame, print the
454 new selected frame. */
455 if (frame_find_by_id (saved_frame_id
) == NULL
)
457 warning (_("Couldn't restore frame in current thread, at frame 0"));
458 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
462 /* Switch from one thread to another. */
465 switch_to_thread (ptid_t ptid
)
467 if (ptid_equal (ptid
, inferior_ptid
))
470 inferior_ptid
= ptid
;
471 reinit_frame_cache ();
472 registers_changed ();
473 stop_pc
= read_pc ();
477 restore_current_thread (ptid_t ptid
)
479 if (!ptid_equal (ptid
, inferior_ptid
))
481 switch_to_thread (ptid
);
486 restore_selected_frame (struct frame_id a_frame_id
)
488 struct frame_info
*selected_frame_info
= NULL
;
490 if (frame_id_eq (a_frame_id
, null_frame_id
))
493 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
495 select_frame (selected_frame_info
);
499 struct current_thread_cleanup
501 ptid_t inferior_ptid
;
502 struct frame_id selected_frame_id
;
506 do_restore_current_thread_cleanup (void *arg
)
508 struct current_thread_cleanup
*old
= arg
;
509 restore_current_thread (old
->inferior_ptid
);
510 restore_selected_frame (old
->selected_frame_id
);
514 static struct cleanup
*
515 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
516 struct frame_id a_frame_id
)
518 struct current_thread_cleanup
*old
519 = xmalloc (sizeof (struct current_thread_cleanup
));
520 old
->inferior_ptid
= inferior_ptid
;
521 old
->selected_frame_id
= a_frame_id
;
522 return make_cleanup (do_restore_current_thread_cleanup
, old
);
525 /* Apply a GDB command to a list of threads. List syntax is a whitespace
526 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
527 of two numbers seperated by a hyphen. Examples:
529 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
530 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
531 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
535 thread_apply_all_command (char *cmd
, int from_tty
)
537 struct thread_info
*tp
;
538 struct cleanup
*old_chain
;
539 struct cleanup
*saved_cmd_cleanup_chain
;
541 struct frame_id saved_frame_id
;
543 int thread_has_changed
= 0;
545 if (cmd
== NULL
|| *cmd
== '\000')
546 error (_("Please specify a command following the thread ID list"));
548 current_ptid
= inferior_ptid
;
549 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
550 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
552 /* It is safe to update the thread list now, before
553 traversing it for "thread apply all". MVS */
554 target_find_new_threads ();
556 /* Save a copy of the command in case it is clobbered by
558 saved_cmd
= xstrdup (cmd
);
559 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
560 for (tp
= thread_list
; tp
; tp
= tp
->next
)
561 if (thread_alive (tp
))
563 switch_to_thread (tp
->ptid
);
564 printf_filtered (_("\nThread %d (%s):\n"),
565 tp
->num
, target_tid_to_str (inferior_ptid
));
566 execute_command (cmd
, from_tty
);
567 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
570 if (!ptid_equal (current_ptid
, inferior_ptid
))
571 thread_has_changed
= 1;
573 do_cleanups (saved_cmd_cleanup_chain
);
574 do_cleanups (old_chain
);
575 /* Print stack frame only if we changed thread. */
576 if (thread_has_changed
)
577 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
582 thread_apply_command (char *tidlist
, int from_tty
)
586 struct cleanup
*old_chain
;
587 struct cleanup
*saved_cmd_cleanup_chain
;
589 struct frame_id saved_frame_id
;
591 int thread_has_changed
= 0;
593 if (tidlist
== NULL
|| *tidlist
== '\000')
594 error (_("Please specify a thread ID list"));
596 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
599 error (_("Please specify a command following the thread ID list"));
601 current_ptid
= inferior_ptid
;
602 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
603 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
605 /* Save a copy of the command in case it is clobbered by
607 saved_cmd
= xstrdup (cmd
);
608 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
609 while (tidlist
< cmd
)
611 struct thread_info
*tp
;
614 start
= strtol (tidlist
, &p
, 10);
616 error (_("Error parsing %s"), tidlist
);
619 while (*tidlist
== ' ' || *tidlist
== '\t')
622 if (*tidlist
== '-') /* Got a range of IDs? */
624 tidlist
++; /* Skip the - */
625 end
= strtol (tidlist
, &p
, 10);
627 error (_("Error parsing %s"), tidlist
);
630 while (*tidlist
== ' ' || *tidlist
== '\t')
636 for (; start
<= end
; start
++)
638 tp
= find_thread_id (start
);
641 warning (_("Unknown thread %d."), start
);
642 else if (!thread_alive (tp
))
643 warning (_("Thread %d has terminated."), start
);
646 switch_to_thread (tp
->ptid
);
647 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
648 target_tid_to_str (inferior_ptid
));
649 execute_command (cmd
, from_tty
);
650 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
655 if (!ptid_equal (current_ptid
, inferior_ptid
))
656 thread_has_changed
= 1;
658 do_cleanups (saved_cmd_cleanup_chain
);
659 do_cleanups (old_chain
);
660 /* Print stack frame only if we changed thread. */
661 if (thread_has_changed
)
662 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
665 /* Switch to the specified thread. Will dispatch off to thread_apply_command
666 if prefix of arg is `apply'. */
669 thread_command (char *tidstr
, int from_tty
)
673 /* Don't generate an error, just say which thread is current. */
674 if (target_has_stack
)
675 printf_filtered (_("[Current thread is %d (%s)]\n"),
676 pid_to_thread_id (inferior_ptid
),
677 target_tid_to_str (inferior_ptid
));
679 error (_("No stack."));
683 gdb_thread_select (uiout
, tidstr
, NULL
);
686 /* Print notices when new threads are attached and detached. */
687 int print_thread_events
= 1;
689 show_print_thread_events (struct ui_file
*file
, int from_tty
,
690 struct cmd_list_element
*c
, const char *value
)
692 fprintf_filtered (file
, _("\
693 Printing of thread events is %s.\n"),
698 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
701 struct thread_info
*tp
;
703 num
= value_as_long (parse_and_eval (tidstr
));
705 tp
= find_thread_id (num
);
708 error (_("Thread ID %d not known."), num
);
710 if (!thread_alive (tp
))
711 error (_("Thread ID %d has terminated."), num
);
713 switch_to_thread (tp
->ptid
);
715 ui_out_text (uiout
, "[Switching to thread ");
716 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
717 ui_out_text (uiout
, " (");
718 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
719 ui_out_text (uiout
, ")]");
721 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
726 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
728 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
729 error_message
, RETURN_MASK_ALL
) < 0)
734 /* Commands with a prefix of `thread'. */
735 struct cmd_list_element
*thread_cmd_list
= NULL
;
738 _initialize_thread (void)
740 static struct cmd_list_element
*thread_apply_list
= NULL
;
742 add_info ("threads", info_threads_command
,
743 _("IDs of currently known threads."));
745 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
746 Use this command to switch between threads.\n\
747 The new thread ID must be currently known."),
748 &thread_cmd_list
, "thread ", 1, &cmdlist
);
750 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
751 _("Apply a command to a list of threads."),
752 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
754 add_cmd ("all", class_run
, thread_apply_all_command
,
755 _("Apply a command to all threads."), &thread_apply_list
);
758 add_com_alias ("t", "thread", class_run
, 1);
760 add_setshow_boolean_cmd ("thread-events", no_class
,
761 &print_thread_events
, _("\
762 Set printing of thread events (e.g., thread start and exit)."), _("\
763 Show printing of thread events (e.g., thread start and exit)."), NULL
,
765 show_print_thread_events
,
766 &setprintlist
, &showprintlist
);