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 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. */
88 if (tp
->step_resume_breakpoint
)
89 delete_breakpoint (tp
->step_resume_breakpoint
);
91 /* FIXME: do I ever need to call the back-end to give it a
92 chance at this private data before deleting the thread? */
100 init_thread_list (void)
102 struct thread_info
*tp
, *tpnext
;
104 highest_thread_num
= 0;
108 for (tp
= thread_list
; tp
; tp
= tpnext
)
117 /* add_thread now returns a pointer to the new thread_info,
118 so that back_ends can initialize their private data. */
121 add_thread (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 delete_thread (ptid_t ptid
)
137 struct thread_info
*tp
, *tpprev
;
141 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
142 if (ptid_equal (tp
->ptid
, ptid
))
149 tpprev
->next
= tp
->next
;
151 thread_list
= tp
->next
;
156 static struct thread_info
*
157 find_thread_id (int num
)
159 struct thread_info
*tp
;
161 for (tp
= thread_list
; tp
; tp
= tp
->next
)
168 /* Find a thread_info by matching PTID. */
170 find_thread_pid (ptid_t ptid
)
172 struct thread_info
*tp
;
174 for (tp
= thread_list
; tp
; tp
= tp
->next
)
175 if (ptid_equal (tp
->ptid
, ptid
))
182 * Thread iterator function.
184 * Calls a callback function once for each thread, so long as
185 * the callback function returns false. If the callback function
186 * returns true, the iteration will end and the current thread
187 * will be returned. This can be useful for implementing a
188 * search for a thread with arbitrary attributes, or for applying
189 * some operation to every thread.
191 * FIXME: some of the existing functionality, such as
192 * "Thread apply all", might be rewritten using this functionality.
196 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
199 struct thread_info
*tp
;
201 for (tp
= thread_list
; tp
; tp
= tp
->next
)
202 if ((*callback
) (tp
, data
))
209 valid_thread_id (int num
)
211 struct thread_info
*tp
;
213 for (tp
= thread_list
; tp
; tp
= tp
->next
)
221 pid_to_thread_id (ptid_t ptid
)
223 struct thread_info
*tp
;
225 for (tp
= thread_list
; tp
; tp
= tp
->next
)
226 if (ptid_equal (tp
->ptid
, ptid
))
233 thread_id_to_pid (int num
)
235 struct thread_info
*thread
= find_thread_id (num
);
239 return pid_to_ptid (-1);
243 in_thread_list (ptid_t ptid
)
245 struct thread_info
*tp
;
247 for (tp
= thread_list
; tp
; tp
= tp
->next
)
248 if (ptid_equal (tp
->ptid
, ptid
))
251 return 0; /* Never heard of 'im */
254 /* Print a list of thread ids currently known, and the total number of
255 threads. To be used from within catch_errors. */
257 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
259 struct thread_info
*tp
;
261 struct cleanup
*cleanup_chain
;
264 target_find_new_threads ();
266 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
268 for (tp
= thread_list
; tp
; tp
= tp
->next
)
271 ui_out_field_int (uiout
, "thread-id", tp
->num
);
274 do_cleanups (cleanup_chain
);
275 ui_out_field_int (uiout
, "number-of-threads", num
);
279 /* Official gdblib interface function to get a list of thread ids and
282 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
284 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
285 error_message
, RETURN_MASK_ALL
) < 0)
290 /* Load infrun state for the thread PID. */
293 load_infrun_state (ptid_t ptid
,
296 struct breakpoint
**step_resume_breakpoint
,
297 CORE_ADDR
*step_range_start
,
298 CORE_ADDR
*step_range_end
,
299 struct frame_id
*step_frame_id
,
300 int *handling_longjmp
,
302 int *stepping_through_solib_after_catch
,
303 bpstat
*stepping_through_solib_catchpoints
,
305 struct symtab
**current_symtab
)
307 struct thread_info
*tp
;
309 /* If we can't find the thread, then we're debugging a single threaded
310 process. No need to do anything in that case. */
311 tp
= find_thread_id (pid_to_thread_id (ptid
));
315 *prev_pc
= tp
->prev_pc
;
316 *trap_expected
= tp
->trap_expected
;
317 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
318 *step_range_start
= tp
->step_range_start
;
319 *step_range_end
= tp
->step_range_end
;
320 *step_frame_id
= tp
->step_frame_id
;
321 *handling_longjmp
= tp
->handling_longjmp
;
322 *another_trap
= tp
->another_trap
;
323 *stepping_through_solib_after_catch
=
324 tp
->stepping_through_solib_after_catch
;
325 *stepping_through_solib_catchpoints
=
326 tp
->stepping_through_solib_catchpoints
;
327 *current_line
= tp
->current_line
;
328 *current_symtab
= tp
->current_symtab
;
331 /* Save infrun state for the thread PID. */
334 save_infrun_state (ptid_t ptid
,
337 struct breakpoint
*step_resume_breakpoint
,
338 CORE_ADDR step_range_start
,
339 CORE_ADDR step_range_end
,
340 const struct frame_id
*step_frame_id
,
341 int handling_longjmp
,
343 int stepping_through_solib_after_catch
,
344 bpstat stepping_through_solib_catchpoints
,
346 struct symtab
*current_symtab
)
348 struct thread_info
*tp
;
350 /* If we can't find the thread, then we're debugging a single-threaded
351 process. Nothing to do in that case. */
352 tp
= find_thread_id (pid_to_thread_id (ptid
));
356 tp
->prev_pc
= prev_pc
;
357 tp
->trap_expected
= trap_expected
;
358 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
359 tp
->step_range_start
= step_range_start
;
360 tp
->step_range_end
= step_range_end
;
361 tp
->step_frame_id
= (*step_frame_id
);
362 tp
->handling_longjmp
= handling_longjmp
;
363 tp
->another_trap
= another_trap
;
364 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
365 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
366 tp
->current_line
= current_line
;
367 tp
->current_symtab
= current_symtab
;
370 /* Return true if TP is an active thread. */
372 thread_alive (struct thread_info
*tp
)
374 if (PIDGET (tp
->ptid
) == -1)
376 if (!target_thread_alive (tp
->ptid
))
378 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
387 struct thread_info
*tp
, *next
;
389 for (tp
= thread_list
; tp
; tp
= next
)
392 if (!thread_alive (tp
))
393 delete_thread (tp
->ptid
);
397 /* Print information about currently known threads
399 * Note: this has the drawback that it _really_ switches
400 * threads, which frees the frame cache. A no-side
401 * effects info-threads command would be nicer.
405 info_threads_command (char *arg
, int from_tty
)
407 struct thread_info
*tp
;
409 struct frame_info
*cur_frame
;
410 struct cleanup
*old_chain
;
411 struct frame_id saved_frame_id
;
414 /* Backup current thread and selected frame. */
415 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
416 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
419 target_find_new_threads ();
420 current_ptid
= inferior_ptid
;
421 for (tp
= thread_list
; tp
; tp
= tp
->next
)
423 if (ptid_equal (tp
->ptid
, current_ptid
))
424 printf_filtered ("* ");
426 printf_filtered (" ");
428 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
430 extra_info
= target_extra_thread_info (tp
);
432 printf_filtered (" (%s)", extra_info
);
434 /* That switch put us at the top of the stack (leaf frame). */
435 switch_to_thread (tp
->ptid
);
436 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
439 /* Restores the current thread and the frame selected before
440 the "info threads" command. */
441 do_cleanups (old_chain
);
443 /* If case we were not able to find the original frame, print the
444 new selected frame. */
445 if (frame_find_by_id (saved_frame_id
) == NULL
)
447 warning (_("Couldn't restore frame in current thread, at frame 0"));
448 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
452 /* Switch from one thread to another. */
455 switch_to_thread (ptid_t ptid
)
457 if (ptid_equal (ptid
, inferior_ptid
))
460 inferior_ptid
= ptid
;
461 reinit_frame_cache ();
462 registers_changed ();
463 stop_pc
= read_pc ();
467 restore_current_thread (ptid_t ptid
)
469 if (!ptid_equal (ptid
, inferior_ptid
))
471 switch_to_thread (ptid
);
476 restore_selected_frame (struct frame_id a_frame_id
)
478 struct frame_info
*selected_frame_info
= NULL
;
480 if (frame_id_eq (a_frame_id
, null_frame_id
))
483 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
485 select_frame (selected_frame_info
);
489 struct current_thread_cleanup
491 ptid_t inferior_ptid
;
492 struct frame_id selected_frame_id
;
496 do_restore_current_thread_cleanup (void *arg
)
498 struct current_thread_cleanup
*old
= arg
;
499 restore_current_thread (old
->inferior_ptid
);
500 restore_selected_frame (old
->selected_frame_id
);
504 static struct cleanup
*
505 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
506 struct frame_id a_frame_id
)
508 struct current_thread_cleanup
*old
509 = xmalloc (sizeof (struct current_thread_cleanup
));
510 old
->inferior_ptid
= inferior_ptid
;
511 old
->selected_frame_id
= a_frame_id
;
512 return make_cleanup (do_restore_current_thread_cleanup
, old
);
515 /* Apply a GDB command to a list of threads. List syntax is a whitespace
516 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
517 of two numbers seperated by a hyphen. Examples:
519 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
520 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
521 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
525 thread_apply_all_command (char *cmd
, int from_tty
)
527 struct thread_info
*tp
;
528 struct cleanup
*old_chain
;
529 struct cleanup
*saved_cmd_cleanup_chain
;
531 struct frame_id saved_frame_id
;
533 int thread_has_changed
= 0;
535 if (cmd
== NULL
|| *cmd
== '\000')
536 error (_("Please specify a command following the thread ID list"));
538 current_ptid
= inferior_ptid
;
539 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
540 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
542 /* It is safe to update the thread list now, before
543 traversing it for "thread apply all". MVS */
544 target_find_new_threads ();
546 /* Save a copy of the command in case it is clobbered by
548 saved_cmd
= xstrdup (cmd
);
549 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
550 for (tp
= thread_list
; tp
; tp
= tp
->next
)
551 if (thread_alive (tp
))
553 switch_to_thread (tp
->ptid
);
554 printf_filtered (_("\nThread %d (%s):\n"),
555 tp
->num
, target_tid_to_str (inferior_ptid
));
556 execute_command (cmd
, from_tty
);
557 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
560 if (!ptid_equal (current_ptid
, inferior_ptid
))
561 thread_has_changed
= 1;
563 do_cleanups (saved_cmd_cleanup_chain
);
564 do_cleanups (old_chain
);
565 /* Print stack frame only if we changed thread. */
566 if (thread_has_changed
)
567 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
572 thread_apply_command (char *tidlist
, int from_tty
)
576 struct cleanup
*old_chain
;
577 struct cleanup
*saved_cmd_cleanup_chain
;
579 struct frame_id saved_frame_id
;
581 int thread_has_changed
= 0;
583 if (tidlist
== NULL
|| *tidlist
== '\000')
584 error (_("Please specify a thread ID list"));
586 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
589 error (_("Please specify a command following the thread ID list"));
591 current_ptid
= inferior_ptid
;
592 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
593 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
595 /* Save a copy of the command in case it is clobbered by
597 saved_cmd
= xstrdup (cmd
);
598 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
599 while (tidlist
< cmd
)
601 struct thread_info
*tp
;
604 start
= strtol (tidlist
, &p
, 10);
606 error (_("Error parsing %s"), tidlist
);
609 while (*tidlist
== ' ' || *tidlist
== '\t')
612 if (*tidlist
== '-') /* Got a range of IDs? */
614 tidlist
++; /* Skip the - */
615 end
= strtol (tidlist
, &p
, 10);
617 error (_("Error parsing %s"), tidlist
);
620 while (*tidlist
== ' ' || *tidlist
== '\t')
626 for (; start
<= end
; start
++)
628 tp
= find_thread_id (start
);
631 warning (_("Unknown thread %d."), start
);
632 else if (!thread_alive (tp
))
633 warning (_("Thread %d has terminated."), start
);
636 switch_to_thread (tp
->ptid
);
637 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
638 target_tid_to_str (inferior_ptid
));
639 execute_command (cmd
, from_tty
);
640 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
645 if (!ptid_equal (current_ptid
, inferior_ptid
))
646 thread_has_changed
= 1;
648 do_cleanups (saved_cmd_cleanup_chain
);
649 do_cleanups (old_chain
);
650 /* Print stack frame only if we changed thread. */
651 if (thread_has_changed
)
652 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
655 /* Switch to the specified thread. Will dispatch off to thread_apply_command
656 if prefix of arg is `apply'. */
659 thread_command (char *tidstr
, int from_tty
)
663 /* Don't generate an error, just say which thread is current. */
664 if (target_has_stack
)
665 printf_filtered (_("[Current thread is %d (%s)]\n"),
666 pid_to_thread_id (inferior_ptid
),
667 target_tid_to_str (inferior_ptid
));
669 error (_("No stack."));
673 gdb_thread_select (uiout
, tidstr
, NULL
);
677 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
680 struct thread_info
*tp
;
682 num
= value_as_long (parse_and_eval (tidstr
));
684 tp
= find_thread_id (num
);
687 error (_("Thread ID %d not known."), num
);
689 if (!thread_alive (tp
))
690 error (_("Thread ID %d has terminated."), num
);
692 switch_to_thread (tp
->ptid
);
694 ui_out_text (uiout
, "[Switching to thread ");
695 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
696 ui_out_text (uiout
, " (");
697 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
698 ui_out_text (uiout
, ")]");
700 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
705 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
707 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
708 error_message
, RETURN_MASK_ALL
) < 0)
713 /* Commands with a prefix of `thread'. */
714 struct cmd_list_element
*thread_cmd_list
= NULL
;
717 _initialize_thread (void)
719 static struct cmd_list_element
*thread_apply_list
= NULL
;
721 add_info ("threads", info_threads_command
,
722 _("IDs of currently known threads."));
724 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
725 Use this command to switch between threads.\n\
726 The new thread ID must be currently known."),
727 &thread_cmd_list
, "thread ", 1, &cmdlist
);
729 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
730 _("Apply a command to a list of threads."),
731 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
733 add_cmd ("all", class_run
, thread_apply_all_command
,
734 _("Apply a command to all threads."), &thread_apply_list
);
737 add_com_alias ("t", "thread", class_run
, 1);