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)
119 /* add_thread now returns a pointer to the new thread_info,
120 so that back_ends can initialize their private data. */
123 add_thread (ptid_t ptid)
125 struct thread_info *tp;
127 tp = (struct thread_info *) xmalloc (sizeof (*tp));
128 memset (tp, 0, sizeof (*tp));
130 tp->num = ++highest_thread_num;
131 tp->next = thread_list;
137 delete_thread (ptid_t ptid)
139 struct thread_info *tp, *tpprev;
143 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
144 if (ptid_equal (tp->ptid, ptid))
151 tpprev->next = tp->next;
153 thread_list = tp->next;
158 static struct thread_info *
159 find_thread_id (int num)
161 struct thread_info *tp;
163 for (tp = thread_list; tp; tp = tp->next)
170 /* Find a thread_info by matching PTID. */
172 find_thread_pid (ptid_t ptid)
174 struct thread_info *tp;
176 for (tp = thread_list; tp; tp = tp->next)
177 if (ptid_equal (tp->ptid, ptid))
184 * Thread iterator function.
186 * Calls a callback function once for each thread, so long as
187 * the callback function returns false. If the callback function
188 * returns true, the iteration will end and the current thread
189 * will be returned. This can be useful for implementing a
190 * search for a thread with arbitrary attributes, or for applying
191 * some operation to every thread.
193 * FIXME: some of the existing functionality, such as
194 * "Thread apply all", might be rewritten using this functionality.
198 iterate_over_threads (int (*callback) (struct thread_info *, void *),
201 struct thread_info *tp;
203 for (tp = thread_list; tp; tp = tp->next)
204 if ((*callback) (tp, data))
211 valid_thread_id (int num)
213 struct thread_info *tp;
215 for (tp = thread_list; tp; tp = tp->next)
223 pid_to_thread_id (ptid_t ptid)
225 struct thread_info *tp;
227 for (tp = thread_list; tp; tp = tp->next)
228 if (ptid_equal (tp->ptid, ptid))
235 thread_id_to_pid (int num)
237 struct thread_info *thread = find_thread_id (num);
241 return pid_to_ptid (-1);
245 in_thread_list (ptid_t ptid)
247 struct thread_info *tp;
249 for (tp = thread_list; tp; tp = tp->next)
250 if (ptid_equal (tp->ptid, ptid))
253 return 0; /* Never heard of 'im */
256 /* Print a list of thread ids currently known, and the total number of
257 threads. To be used from within catch_errors. */
259 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
261 struct thread_info *tp;
263 struct cleanup *cleanup_chain;
266 target_find_new_threads ();
268 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
270 for (tp = thread_list; tp; tp = tp->next)
273 ui_out_field_int (uiout, "thread-id", tp->num);
276 do_cleanups (cleanup_chain);
277 ui_out_field_int (uiout, "number-of-threads", num);
281 /* Official gdblib interface function to get a list of thread ids and
284 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
286 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
287 error_message, RETURN_MASK_ALL) < 0)
292 /* Load infrun state for the thread PID. */
295 load_infrun_state (ptid_t ptid,
298 struct breakpoint **step_resume_breakpoint,
299 CORE_ADDR *step_range_start,
300 CORE_ADDR *step_range_end,
301 struct frame_id *step_frame_id,
302 int *handling_longjmp,
303 int *stepping_over_breakpoint,
304 int *stepping_through_solib_after_catch,
305 bpstat *stepping_through_solib_catchpoints,
307 struct symtab **current_symtab)
309 struct thread_info *tp;
311 /* If we can't find the thread, then we're debugging a single threaded
312 process. No need to do anything in that case. */
313 tp = find_thread_id (pid_to_thread_id (ptid));
317 *prev_pc = tp->prev_pc;
318 *trap_expected = tp->trap_expected;
319 *step_resume_breakpoint = tp->step_resume_breakpoint;
320 *step_range_start = tp->step_range_start;
321 *step_range_end = tp->step_range_end;
322 *step_frame_id = tp->step_frame_id;
323 *handling_longjmp = tp->handling_longjmp;
324 *stepping_over_breakpoint = tp->stepping_over_breakpoint;
325 *stepping_through_solib_after_catch =
326 tp->stepping_through_solib_after_catch;
327 *stepping_through_solib_catchpoints =
328 tp->stepping_through_solib_catchpoints;
329 *current_line = tp->current_line;
330 *current_symtab = tp->current_symtab;
333 /* Save infrun state for the thread PID. */
336 save_infrun_state (ptid_t ptid,
339 struct breakpoint *step_resume_breakpoint,
340 CORE_ADDR step_range_start,
341 CORE_ADDR step_range_end,
342 const struct frame_id *step_frame_id,
343 int handling_longjmp,
344 int stepping_over_breakpoint,
345 int stepping_through_solib_after_catch,
346 bpstat stepping_through_solib_catchpoints,
348 struct symtab *current_symtab)
350 struct thread_info *tp;
352 /* If we can't find the thread, then we're debugging a single-threaded
353 process. Nothing to do in that case. */
354 tp = find_thread_id (pid_to_thread_id (ptid));
358 tp->prev_pc = prev_pc;
359 tp->trap_expected = trap_expected;
360 tp->step_resume_breakpoint = step_resume_breakpoint;
361 tp->step_range_start = step_range_start;
362 tp->step_range_end = step_range_end;
363 tp->step_frame_id = (*step_frame_id);
364 tp->handling_longjmp = handling_longjmp;
365 tp->stepping_over_breakpoint = stepping_over_breakpoint;
366 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
367 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
368 tp->current_line = current_line;
369 tp->current_symtab = current_symtab;
372 /* Return true if TP is an active thread. */
374 thread_alive (struct thread_info *tp)
376 if (PIDGET (tp->ptid) == -1)
378 if (!target_thread_alive (tp->ptid))
380 tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
389 struct thread_info *tp, *next;
391 for (tp = thread_list; tp; tp = next)
394 if (!thread_alive (tp))
395 delete_thread (tp->ptid);
399 /* Print information about currently known threads
401 * Note: this has the drawback that it _really_ switches
402 * threads, which frees the frame cache. A no-side
403 * effects info-threads command would be nicer.
407 info_threads_command (char *arg, int from_tty)
409 struct thread_info *tp;
411 struct frame_info *cur_frame;
412 struct cleanup *old_chain;
413 struct frame_id saved_frame_id;
416 /* Backup current thread and selected frame. */
417 saved_frame_id = get_frame_id (get_selected_frame (NULL));
418 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
421 target_find_new_threads ();
422 current_ptid = inferior_ptid;
423 for (tp = thread_list; tp; tp = tp->next)
425 if (ptid_equal (tp->ptid, current_ptid))
426 printf_filtered ("* ");
428 printf_filtered (" ");
430 printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
432 extra_info = target_extra_thread_info (tp);
434 printf_filtered (" (%s)", extra_info);
436 /* That switch put us at the top of the stack (leaf frame). */
437 switch_to_thread (tp->ptid);
438 print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
441 /* Restores the current thread and the frame selected before
442 the "info threads" command. */
443 do_cleanups (old_chain);
445 /* If case we were not able to find the original frame, print the
446 new selected frame. */
447 if (frame_find_by_id (saved_frame_id) == NULL)
449 warning (_("Couldn't restore frame in current thread, at frame 0"));
450 print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
454 /* Switch from one thread to another. */
457 switch_to_thread (ptid_t ptid)
459 if (ptid_equal (ptid, inferior_ptid))
462 inferior_ptid = ptid;
463 reinit_frame_cache ();
464 registers_changed ();
465 stop_pc = read_pc ();
469 restore_current_thread (ptid_t ptid)
471 if (!ptid_equal (ptid, inferior_ptid))
473 switch_to_thread (ptid);
478 restore_selected_frame (struct frame_id a_frame_id)
480 struct frame_info *selected_frame_info = NULL;
482 if (frame_id_eq (a_frame_id, null_frame_id))
485 if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL)
487 select_frame (selected_frame_info);
491 struct current_thread_cleanup
493 ptid_t inferior_ptid;
494 struct frame_id selected_frame_id;
498 do_restore_current_thread_cleanup (void *arg)
500 struct current_thread_cleanup *old = arg;
501 restore_current_thread (old->inferior_ptid);
502 restore_selected_frame (old->selected_frame_id);
506 static struct cleanup *
507 make_cleanup_restore_current_thread (ptid_t inferior_ptid,
508 struct frame_id a_frame_id)
510 struct current_thread_cleanup *old
511 = xmalloc (sizeof (struct current_thread_cleanup));
512 old->inferior_ptid = inferior_ptid;
513 old->selected_frame_id = a_frame_id;
514 return make_cleanup (do_restore_current_thread_cleanup, old);
517 /* Apply a GDB command to a list of threads. List syntax is a whitespace
518 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
519 of two numbers seperated by a hyphen. Examples:
521 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
522 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
523 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
527 thread_apply_all_command (char *cmd, int from_tty)
529 struct thread_info *tp;
530 struct cleanup *old_chain;
531 struct cleanup *saved_cmd_cleanup_chain;
533 struct frame_id saved_frame_id;
535 int thread_has_changed = 0;
537 if (cmd == NULL || *cmd == '\000')
538 error (_("Please specify a command following the thread ID list"));
540 current_ptid = inferior_ptid;
541 saved_frame_id = get_frame_id (get_selected_frame (NULL));
542 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
544 /* It is safe to update the thread list now, before
545 traversing it for "thread apply all". MVS */
546 target_find_new_threads ();
548 /* Save a copy of the command in case it is clobbered by
550 saved_cmd = xstrdup (cmd);
551 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
552 for (tp = thread_list; tp; tp = tp->next)
553 if (thread_alive (tp))
555 switch_to_thread (tp->ptid);
556 printf_filtered (_("\nThread %d (%s):\n"),
557 tp->num, target_tid_to_str (inferior_ptid));
558 execute_command (cmd, from_tty);
559 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
562 if (!ptid_equal (current_ptid, inferior_ptid))
563 thread_has_changed = 1;
565 do_cleanups (saved_cmd_cleanup_chain);
566 do_cleanups (old_chain);
567 /* Print stack frame only if we changed thread. */
568 if (thread_has_changed)
569 print_stack_frame (get_current_frame (), 1, SRC_LINE);
574 thread_apply_command (char *tidlist, int from_tty)
578 struct cleanup *old_chain;
579 struct cleanup *saved_cmd_cleanup_chain;
581 struct frame_id saved_frame_id;
583 int thread_has_changed = 0;
585 if (tidlist == NULL || *tidlist == '\000')
586 error (_("Please specify a thread ID list"));
588 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
591 error (_("Please specify a command following the thread ID list"));
593 current_ptid = inferior_ptid;
594 saved_frame_id = get_frame_id (get_selected_frame (NULL));
595 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
597 /* Save a copy of the command in case it is clobbered by
599 saved_cmd = xstrdup (cmd);
600 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
601 while (tidlist < cmd)
603 struct thread_info *tp;
606 start = strtol (tidlist, &p, 10);
608 error (_("Error parsing %s"), tidlist);
611 while (*tidlist == ' ' || *tidlist == '\t')
614 if (*tidlist == '-') /* Got a range of IDs? */
616 tidlist++; /* Skip the - */
617 end = strtol (tidlist, &p, 10);
619 error (_("Error parsing %s"), tidlist);
622 while (*tidlist == ' ' || *tidlist == '\t')
628 for (; start <= end; start++)
630 tp = find_thread_id (start);
633 warning (_("Unknown thread %d."), start);
634 else if (!thread_alive (tp))
635 warning (_("Thread %d has terminated."), start);
638 switch_to_thread (tp->ptid);
639 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
640 target_tid_to_str (inferior_ptid));
641 execute_command (cmd, from_tty);
642 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
647 if (!ptid_equal (current_ptid, inferior_ptid))
648 thread_has_changed = 1;
650 do_cleanups (saved_cmd_cleanup_chain);
651 do_cleanups (old_chain);
652 /* Print stack frame only if we changed thread. */
653 if (thread_has_changed)
654 print_stack_frame (get_current_frame (), 1, SRC_LINE);
657 /* Switch to the specified thread. Will dispatch off to thread_apply_command
658 if prefix of arg is `apply'. */
661 thread_command (char *tidstr, int from_tty)
665 /* Don't generate an error, just say which thread is current. */
666 if (target_has_stack)
667 printf_filtered (_("[Current thread is %d (%s)]\n"),
668 pid_to_thread_id (inferior_ptid),
669 target_tid_to_str (inferior_ptid));
671 error (_("No stack."));
675 gdb_thread_select (uiout, tidstr, NULL);
679 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
682 struct thread_info *tp;
684 num = value_as_long (parse_and_eval (tidstr));
686 tp = find_thread_id (num);
689 error (_("Thread ID %d not known."), num);
691 if (!thread_alive (tp))
692 error (_("Thread ID %d has terminated."), num);
694 switch_to_thread (tp->ptid);
696 ui_out_text (uiout, "[Switching to thread ");
697 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
698 ui_out_text (uiout, " (");
699 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
700 ui_out_text (uiout, ")]");
702 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
707 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
709 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
710 error_message, RETURN_MASK_ALL) < 0)
715 /* Commands with a prefix of `thread'. */
716 struct cmd_list_element *thread_cmd_list = NULL;
719 _initialize_thread (void)
721 static struct cmd_list_element *thread_apply_list = NULL;
723 add_info ("threads", info_threads_command,
724 _("IDs of currently known threads."));
726 add_prefix_cmd ("thread", class_run, thread_command, _("\
727 Use this command to switch between threads.\n\
728 The new thread ID must be currently known."),
729 &thread_cmd_list, "thread ", 1, &cmdlist);
731 add_prefix_cmd ("apply", class_run, thread_apply_command,
732 _("Apply a command to a list of threads."),
733 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
735 add_cmd ("all", class_run, thread_apply_all_command,
736 _("Apply a command to all threads."), &thread_apply_list);
739 add_com_alias ("t", "thread", class_run, 1);