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);
64 static struct cleanup
*make_cleanup_restore_current_thread (ptid_t
,
68 delete_step_resume_breakpoint (void *arg
)
70 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
71 struct thread_info
*tp
;
73 if (*breakpointp
!= NULL
)
75 delete_breakpoint (*breakpointp
);
76 for (tp
= thread_list
; tp
; tp
= tp
->next
)
77 if (tp
->step_resume_breakpoint
== *breakpointp
)
78 tp
->step_resume_breakpoint
= NULL
;
85 free_thread (struct thread_info
*tp
)
87 /* NOTE: this will take care of any left-over step_resume breakpoints,
88 but not any user-specified thread-specific breakpoints. We can not
89 delete the breakpoint straight-off, because the inferior might not
90 be stopped at the moment. */
91 if (tp
->step_resume_breakpoint
)
92 tp
->step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
94 /* FIXME: do I ever need to call the back-end to give it a
95 chance at this private data before deleting the thread? */
103 init_thread_list (void)
105 struct thread_info
*tp
, *tpnext
;
107 highest_thread_num
= 0;
111 for (tp
= thread_list
; tp
; tp
= tpnext
)
121 add_thread_silent (ptid_t ptid
)
123 struct thread_info
*tp
;
125 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
126 memset (tp
, 0, sizeof (*tp
));
128 tp
->num
= ++highest_thread_num
;
129 tp
->next
= thread_list
;
135 add_thread (ptid_t ptid
)
137 struct thread_info
*result
= add_thread_silent (ptid
);
139 if (print_thread_events
)
140 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
142 observer_notify_new_thread (result
);
148 delete_thread (ptid_t ptid
)
150 struct thread_info
*tp
, *tpprev
;
154 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
155 if (ptid_equal (tp
->ptid
, ptid
))
162 tpprev
->next
= tp
->next
;
164 thread_list
= tp
->next
;
169 static struct thread_info
*
170 find_thread_id (int num
)
172 struct thread_info
*tp
;
174 for (tp
= thread_list
; tp
; tp
= tp
->next
)
181 /* Find a thread_info by matching PTID. */
183 find_thread_pid (ptid_t ptid
)
185 struct thread_info
*tp
;
187 for (tp
= thread_list
; tp
; tp
= tp
->next
)
188 if (ptid_equal (tp
->ptid
, ptid
))
195 * Thread iterator function.
197 * Calls a callback function once for each thread, so long as
198 * the callback function returns false. If the callback function
199 * returns true, the iteration will end and the current thread
200 * will be returned. This can be useful for implementing a
201 * search for a thread with arbitrary attributes, or for applying
202 * some operation to every thread.
204 * FIXME: some of the existing functionality, such as
205 * "Thread apply all", might be rewritten using this functionality.
209 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
212 struct thread_info
*tp
;
214 for (tp
= thread_list
; tp
; tp
= tp
->next
)
215 if ((*callback
) (tp
, data
))
222 valid_thread_id (int num
)
224 struct thread_info
*tp
;
226 for (tp
= thread_list
; tp
; tp
= tp
->next
)
234 pid_to_thread_id (ptid_t ptid
)
236 struct thread_info
*tp
;
238 for (tp
= thread_list
; tp
; tp
= tp
->next
)
239 if (ptid_equal (tp
->ptid
, ptid
))
246 thread_id_to_pid (int num
)
248 struct thread_info
*thread
= find_thread_id (num
);
252 return pid_to_ptid (-1);
256 in_thread_list (ptid_t ptid
)
258 struct thread_info
*tp
;
260 for (tp
= thread_list
; tp
; tp
= tp
->next
)
261 if (ptid_equal (tp
->ptid
, ptid
))
264 return 0; /* Never heard of 'im */
267 /* Print a list of thread ids currently known, and the total number of
268 threads. To be used from within catch_errors. */
270 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
272 struct thread_info
*tp
;
274 struct cleanup
*cleanup_chain
;
277 target_find_new_threads ();
279 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
281 for (tp
= thread_list
; tp
; tp
= tp
->next
)
284 ui_out_field_int (uiout
, "thread-id", tp
->num
);
287 do_cleanups (cleanup_chain
);
288 ui_out_field_int (uiout
, "number-of-threads", num
);
292 /* Official gdblib interface function to get a list of thread ids and
295 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
297 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
298 error_message
, RETURN_MASK_ALL
) < 0)
303 /* Load infrun state for the thread PID. */
306 load_infrun_state (ptid_t ptid
,
309 struct breakpoint
**step_resume_breakpoint
,
310 CORE_ADDR
*step_range_start
,
311 CORE_ADDR
*step_range_end
,
312 struct frame_id
*step_frame_id
,
313 int *handling_longjmp
,
314 int *stepping_over_breakpoint
,
315 int *stepping_through_solib_after_catch
,
316 bpstat
*stepping_through_solib_catchpoints
,
318 struct symtab
**current_symtab
)
320 struct thread_info
*tp
;
322 /* If we can't find the thread, then we're debugging a single threaded
323 process. No need to do anything in that case. */
324 tp
= find_thread_id (pid_to_thread_id (ptid
));
328 *prev_pc
= tp
->prev_pc
;
329 *trap_expected
= tp
->trap_expected
;
330 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
331 *step_range_start
= tp
->step_range_start
;
332 *step_range_end
= tp
->step_range_end
;
333 *step_frame_id
= tp
->step_frame_id
;
334 *handling_longjmp
= tp
->handling_longjmp
;
335 *stepping_over_breakpoint
= tp
->stepping_over_breakpoint
;
336 *stepping_through_solib_after_catch
=
337 tp
->stepping_through_solib_after_catch
;
338 *stepping_through_solib_catchpoints
=
339 tp
->stepping_through_solib_catchpoints
;
340 *current_line
= tp
->current_line
;
341 *current_symtab
= tp
->current_symtab
;
344 /* Save infrun state for the thread PID. */
347 save_infrun_state (ptid_t ptid
,
350 struct breakpoint
*step_resume_breakpoint
,
351 CORE_ADDR step_range_start
,
352 CORE_ADDR step_range_end
,
353 const struct frame_id
*step_frame_id
,
354 int handling_longjmp
,
355 int stepping_over_breakpoint
,
356 int stepping_through_solib_after_catch
,
357 bpstat stepping_through_solib_catchpoints
,
359 struct symtab
*current_symtab
)
361 struct thread_info
*tp
;
363 /* If we can't find the thread, then we're debugging a single-threaded
364 process. Nothing to do in that case. */
365 tp
= find_thread_id (pid_to_thread_id (ptid
));
369 tp
->prev_pc
= prev_pc
;
370 tp
->trap_expected
= trap_expected
;
371 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
372 tp
->step_range_start
= step_range_start
;
373 tp
->step_range_end
= step_range_end
;
374 tp
->step_frame_id
= (*step_frame_id
);
375 tp
->handling_longjmp
= handling_longjmp
;
376 tp
->stepping_over_breakpoint
= stepping_over_breakpoint
;
377 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
378 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
379 tp
->current_line
= current_line
;
380 tp
->current_symtab
= current_symtab
;
383 /* Return true if TP is an active thread. */
385 thread_alive (struct thread_info
*tp
)
387 if (PIDGET (tp
->ptid
) == -1)
389 if (!target_thread_alive (tp
->ptid
))
391 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
400 struct thread_info
*tp
, *next
;
402 for (tp
= thread_list
; tp
; tp
= next
)
405 if (!thread_alive (tp
))
406 delete_thread (tp
->ptid
);
410 /* Print information about currently known threads
412 * Note: this has the drawback that it _really_ switches
413 * threads, which frees the frame cache. A no-side
414 * effects info-threads command would be nicer.
418 info_threads_command (char *arg
, int from_tty
)
420 struct thread_info
*tp
;
422 struct frame_info
*cur_frame
;
423 struct cleanup
*old_chain
;
424 struct frame_id saved_frame_id
;
427 /* Backup current thread and selected frame. */
428 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
429 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
432 target_find_new_threads ();
433 current_ptid
= inferior_ptid
;
434 for (tp
= thread_list
; tp
; tp
= tp
->next
)
436 if (ptid_equal (tp
->ptid
, current_ptid
))
437 printf_filtered ("* ");
439 printf_filtered (" ");
441 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
443 extra_info
= target_extra_thread_info (tp
);
445 printf_filtered (" (%s)", extra_info
);
447 /* That switch put us at the top of the stack (leaf frame). */
448 switch_to_thread (tp
->ptid
);
449 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
452 /* Restores the current thread and the frame selected before
453 the "info threads" command. */
454 do_cleanups (old_chain
);
456 /* If case we were not able to find the original frame, print the
457 new selected frame. */
458 if (frame_find_by_id (saved_frame_id
) == NULL
)
460 warning (_("Couldn't restore frame in current thread, at frame 0"));
461 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
465 /* Switch from one thread to another. */
468 switch_to_thread (ptid_t ptid
)
470 if (ptid_equal (ptid
, inferior_ptid
))
473 inferior_ptid
= ptid
;
474 reinit_frame_cache ();
475 registers_changed ();
476 stop_pc
= read_pc ();
480 restore_current_thread (ptid_t ptid
)
482 if (!ptid_equal (ptid
, inferior_ptid
))
484 switch_to_thread (ptid
);
489 restore_selected_frame (struct frame_id a_frame_id
)
491 struct frame_info
*selected_frame_info
= NULL
;
493 if (frame_id_eq (a_frame_id
, null_frame_id
))
496 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
498 select_frame (selected_frame_info
);
502 struct current_thread_cleanup
504 ptid_t inferior_ptid
;
505 struct frame_id selected_frame_id
;
509 do_restore_current_thread_cleanup (void *arg
)
511 struct current_thread_cleanup
*old
= arg
;
512 restore_current_thread (old
->inferior_ptid
);
513 restore_selected_frame (old
->selected_frame_id
);
517 static struct cleanup
*
518 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
519 struct frame_id a_frame_id
)
521 struct current_thread_cleanup
*old
522 = xmalloc (sizeof (struct current_thread_cleanup
));
523 old
->inferior_ptid
= inferior_ptid
;
524 old
->selected_frame_id
= a_frame_id
;
525 return make_cleanup (do_restore_current_thread_cleanup
, old
);
528 /* Apply a GDB command to a list of threads. List syntax is a whitespace
529 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
530 of two numbers seperated by a hyphen. Examples:
532 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
533 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
534 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
538 thread_apply_all_command (char *cmd
, int from_tty
)
540 struct thread_info
*tp
;
541 struct cleanup
*old_chain
;
542 struct cleanup
*saved_cmd_cleanup_chain
;
544 struct frame_id saved_frame_id
;
546 int thread_has_changed
= 0;
548 if (cmd
== NULL
|| *cmd
== '\000')
549 error (_("Please specify a command following the thread ID list"));
551 current_ptid
= inferior_ptid
;
552 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
553 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
555 /* It is safe to update the thread list now, before
556 traversing it for "thread apply all". MVS */
557 target_find_new_threads ();
559 /* Save a copy of the command in case it is clobbered by
561 saved_cmd
= xstrdup (cmd
);
562 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
563 for (tp
= thread_list
; tp
; tp
= tp
->next
)
564 if (thread_alive (tp
))
566 switch_to_thread (tp
->ptid
);
567 printf_filtered (_("\nThread %d (%s):\n"),
568 tp
->num
, target_tid_to_str (inferior_ptid
));
569 execute_command (cmd
, from_tty
);
570 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
573 if (!ptid_equal (current_ptid
, inferior_ptid
))
574 thread_has_changed
= 1;
576 do_cleanups (saved_cmd_cleanup_chain
);
577 do_cleanups (old_chain
);
578 /* Print stack frame only if we changed thread. */
579 if (thread_has_changed
)
580 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
585 thread_apply_command (char *tidlist
, int from_tty
)
589 struct cleanup
*old_chain
;
590 struct cleanup
*saved_cmd_cleanup_chain
;
592 struct frame_id saved_frame_id
;
594 int thread_has_changed
= 0;
596 if (tidlist
== NULL
|| *tidlist
== '\000')
597 error (_("Please specify a thread ID list"));
599 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
602 error (_("Please specify a command following the thread ID list"));
604 current_ptid
= inferior_ptid
;
605 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
606 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
608 /* Save a copy of the command in case it is clobbered by
610 saved_cmd
= xstrdup (cmd
);
611 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
612 while (tidlist
< cmd
)
614 struct thread_info
*tp
;
617 start
= strtol (tidlist
, &p
, 10);
619 error (_("Error parsing %s"), tidlist
);
622 while (*tidlist
== ' ' || *tidlist
== '\t')
625 if (*tidlist
== '-') /* Got a range of IDs? */
627 tidlist
++; /* Skip the - */
628 end
= strtol (tidlist
, &p
, 10);
630 error (_("Error parsing %s"), tidlist
);
633 while (*tidlist
== ' ' || *tidlist
== '\t')
639 for (; start
<= end
; start
++)
641 tp
= find_thread_id (start
);
644 warning (_("Unknown thread %d."), start
);
645 else if (!thread_alive (tp
))
646 warning (_("Thread %d has terminated."), start
);
649 switch_to_thread (tp
->ptid
);
650 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
651 target_tid_to_str (inferior_ptid
));
652 execute_command (cmd
, from_tty
);
653 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
658 if (!ptid_equal (current_ptid
, inferior_ptid
))
659 thread_has_changed
= 1;
661 do_cleanups (saved_cmd_cleanup_chain
);
662 do_cleanups (old_chain
);
663 /* Print stack frame only if we changed thread. */
664 if (thread_has_changed
)
665 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
668 /* Switch to the specified thread. Will dispatch off to thread_apply_command
669 if prefix of arg is `apply'. */
672 thread_command (char *tidstr
, int from_tty
)
676 /* Don't generate an error, just say which thread is current. */
677 if (target_has_stack
)
678 printf_filtered (_("[Current thread is %d (%s)]\n"),
679 pid_to_thread_id (inferior_ptid
),
680 target_tid_to_str (inferior_ptid
));
682 error (_("No stack."));
686 gdb_thread_select (uiout
, tidstr
, NULL
);
689 /* Print notices when new threads are attached and detached. */
690 int print_thread_events
= 1;
692 show_print_thread_events (struct ui_file
*file
, int from_tty
,
693 struct cmd_list_element
*c
, const char *value
)
695 fprintf_filtered (file
, _("\
696 Printing of thread events is %s.\n"),
701 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
704 struct thread_info
*tp
;
706 num
= value_as_long (parse_and_eval (tidstr
));
708 tp
= find_thread_id (num
);
711 error (_("Thread ID %d not known."), num
);
713 if (!thread_alive (tp
))
714 error (_("Thread ID %d has terminated."), num
);
716 switch_to_thread (tp
->ptid
);
718 ui_out_text (uiout
, "[Switching to thread ");
719 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
720 ui_out_text (uiout
, " (");
721 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
722 ui_out_text (uiout
, ")]");
724 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
729 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
731 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
732 error_message
, RETURN_MASK_ALL
) < 0)
737 /* Commands with a prefix of `thread'. */
738 struct cmd_list_element
*thread_cmd_list
= NULL
;
741 _initialize_thread (void)
743 static struct cmd_list_element
*thread_apply_list
= NULL
;
745 add_info ("threads", info_threads_command
,
746 _("IDs of currently known threads."));
748 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
749 Use this command to switch between threads.\n\
750 The new thread ID must be currently known."),
751 &thread_cmd_list
, "thread ", 1, &cmdlist
);
753 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
754 _("Apply a command to a list of threads."),
755 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
757 add_cmd ("all", class_run
, thread_apply_all_command
,
758 _("Apply a command to all threads."), &thread_apply_list
);
761 add_com_alias ("t", "thread", class_run
, 1);
763 add_setshow_boolean_cmd ("thread-events", no_class
,
764 &print_thread_events
, _("\
765 Set printing of thread events (e.g., thread start and exit)."), _("\
766 Show printing of thread events (e.g., thread start and exit)."), NULL
,
768 show_print_thread_events
,
769 &setprintlist
, &showprintlist
);