1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004 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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
32 #include "gdbthread.h"
37 #include "gdb_string.h"
40 #include <sys/types.h>
44 /*#include "lynxos-core.h" */
46 /* Definition of struct thread_info exported to gdbthread.h */
48 /* Prototypes for exported functions. */
50 void _initialize_thread (void);
52 /* Prototypes for local functions. */
54 static struct thread_info *thread_list = NULL;
55 static int highest_thread_num;
57 static struct thread_info *find_thread_id (int num);
59 static void thread_command (char *tidstr, int from_tty);
60 static void thread_apply_all_command (char *, int);
61 static int thread_alive (struct thread_info *);
62 static void info_threads_command (char *, int);
63 static void thread_apply_command (char *, int);
64 static void restore_current_thread (ptid_t);
65 static void switch_to_thread (ptid_t ptid);
66 static void prune_threads (void);
69 delete_step_resume_breakpoint (void *arg)
71 struct breakpoint **breakpointp = (struct breakpoint **) arg;
72 struct thread_info *tp;
74 if (*breakpointp != NULL)
76 delete_breakpoint (*breakpointp);
77 for (tp = thread_list; tp; tp = tp->next)
78 if (tp->step_resume_breakpoint == *breakpointp)
79 tp->step_resume_breakpoint = NULL;
86 free_thread (struct thread_info *tp)
88 /* NOTE: this will take care of any left-over step_resume breakpoints,
89 but not any user-specified thread-specific breakpoints. */
90 if (tp->step_resume_breakpoint)
91 delete_breakpoint (tp->step_resume_breakpoint);
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)
286 return catch_exceptions (uiout, do_captured_list_thread_ids, NULL,
287 NULL, RETURN_MASK_ALL);
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,
304 int *stepping_through_sigtramp,
306 struct symtab **current_symtab)
308 struct thread_info *tp;
310 /* If we can't find the thread, then we're debugging a single threaded
311 process. No need to do anything in that case. */
312 tp = find_thread_id (pid_to_thread_id (ptid));
316 *prev_pc = tp->prev_pc;
317 *trap_expected = tp->trap_expected;
318 *step_resume_breakpoint = tp->step_resume_breakpoint;
319 *step_range_start = tp->step_range_start;
320 *step_range_end = tp->step_range_end;
321 *step_frame_id = tp->step_frame_id;
322 *handling_longjmp = tp->handling_longjmp;
323 *another_trap = tp->another_trap;
324 *stepping_through_solib_after_catch =
325 tp->stepping_through_solib_after_catch;
326 *stepping_through_solib_catchpoints =
327 tp->stepping_through_solib_catchpoints;
328 *stepping_through_sigtramp = tp->stepping_through_sigtramp;
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,
345 int stepping_through_solib_after_catch,
346 bpstat stepping_through_solib_catchpoints,
347 int stepping_through_sigtramp,
349 struct symtab *current_symtab)
351 struct thread_info *tp;
353 /* If we can't find the thread, then we're debugging a single-threaded
354 process. Nothing to do in that case. */
355 tp = find_thread_id (pid_to_thread_id (ptid));
359 tp->prev_pc = prev_pc;
360 tp->trap_expected = trap_expected;
361 tp->step_resume_breakpoint = step_resume_breakpoint;
362 tp->step_range_start = step_range_start;
363 tp->step_range_end = step_range_end;
364 tp->step_frame_id = (*step_frame_id);
365 tp->handling_longjmp = handling_longjmp;
366 tp->another_trap = another_trap;
367 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
368 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
369 tp->stepping_through_sigtramp = stepping_through_sigtramp;
370 tp->current_line = current_line;
371 tp->current_symtab = current_symtab;
374 /* Return true if TP is an active thread. */
376 thread_alive (struct thread_info *tp)
378 if (PIDGET (tp->ptid) == -1)
380 if (!target_thread_alive (tp->ptid))
382 tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
391 struct thread_info *tp, *next;
393 for (tp = thread_list; tp; tp = next)
396 if (!thread_alive (tp))
397 delete_thread (tp->ptid);
401 /* Print information about currently known threads
403 * Note: this has the drawback that it _really_ switches
404 * threads, which frees the frame cache. A no-side
405 * effects info-threads command would be nicer.
409 info_threads_command (char *arg, int from_tty)
411 struct thread_info *tp;
413 struct frame_info *cur_frame;
414 struct frame_id saved_frame_id = get_frame_id (get_selected_frame ());
418 target_find_new_threads ();
419 current_ptid = inferior_ptid;
420 for (tp = thread_list; tp; tp = tp->next)
422 if (ptid_equal (tp->ptid, current_ptid))
423 printf_filtered ("* ");
425 printf_filtered (" ");
427 printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
429 extra_info = target_extra_thread_info (tp);
431 printf_filtered (" (%s)", extra_info);
434 switch_to_thread (tp->ptid);
435 print_stack_frame (get_selected_frame (), 0, LOCATION);
438 switch_to_thread (current_ptid);
440 /* Restores the frame set by the user before the "info threads"
441 command. We have finished the info-threads display by switching
442 back to the current thread. That switch has put us at the top of
443 the stack (leaf frame). */
444 cur_frame = frame_find_by_id (saved_frame_id);
445 if (cur_frame == NULL)
447 /* Ooops, can't restore, tell user where we are. */
448 warning ("Couldn't restore frame in current thread, at frame 0");
449 print_stack_frame (get_selected_frame (), 0, LOCATION);
453 select_frame (cur_frame);
454 /* re-show current frame. */
455 show_stack_frame (cur_frame);
459 /* Switch from one thread to another. */
462 switch_to_thread (ptid_t ptid)
464 if (ptid_equal (ptid, inferior_ptid))
467 inferior_ptid = ptid;
468 flush_cached_frames ();
469 registers_changed ();
470 stop_pc = read_pc ();
471 select_frame (get_current_frame ());
475 restore_current_thread (ptid_t ptid)
477 if (!ptid_equal (ptid, inferior_ptid))
479 switch_to_thread (ptid);
480 print_stack_frame (get_current_frame (), 1, SRC_LINE);
484 struct current_thread_cleanup
486 ptid_t inferior_ptid;
490 do_restore_current_thread_cleanup (void *arg)
492 struct current_thread_cleanup *old = arg;
493 restore_current_thread (old->inferior_ptid);
497 static struct cleanup *
498 make_cleanup_restore_current_thread (ptid_t inferior_ptid)
500 struct current_thread_cleanup *old
501 = xmalloc (sizeof (struct current_thread_cleanup));
502 old->inferior_ptid = inferior_ptid;
503 return make_cleanup (do_restore_current_thread_cleanup, old);
506 /* Apply a GDB command to a list of threads. List syntax is a whitespace
507 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
508 of two numbers seperated by a hyphen. Examples:
510 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
511 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
512 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
516 thread_apply_all_command (char *cmd, int from_tty)
518 struct thread_info *tp;
519 struct cleanup *old_chain;
520 struct cleanup *saved_cmd_cleanup_chain;
523 if (cmd == NULL || *cmd == '\000')
524 error ("Please specify a command following the thread ID list");
526 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
528 /* It is safe to update the thread list now, before
529 traversing it for "thread apply all". MVS */
530 target_find_new_threads ();
532 /* Save a copy of the command in case it is clobbered by
534 saved_cmd = xstrdup (cmd);
535 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
536 for (tp = thread_list; tp; tp = tp->next)
537 if (thread_alive (tp))
539 switch_to_thread (tp->ptid);
540 printf_filtered ("\nThread %d (%s):\n",
541 tp->num, target_tid_to_str (inferior_ptid));
542 execute_command (cmd, from_tty);
543 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
546 do_cleanups (saved_cmd_cleanup_chain);
547 do_cleanups (old_chain);
551 thread_apply_command (char *tidlist, int from_tty)
555 struct cleanup *old_chain;
556 struct cleanup *saved_cmd_cleanup_chain;
559 if (tidlist == NULL || *tidlist == '\000')
560 error ("Please specify a thread ID list");
562 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
565 error ("Please specify a command following the thread ID list");
567 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
569 /* Save a copy of the command in case it is clobbered by
571 saved_cmd = xstrdup (cmd);
572 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
573 while (tidlist < cmd)
575 struct thread_info *tp;
578 start = strtol (tidlist, &p, 10);
580 error ("Error parsing %s", tidlist);
583 while (*tidlist == ' ' || *tidlist == '\t')
586 if (*tidlist == '-') /* Got a range of IDs? */
588 tidlist++; /* Skip the - */
589 end = strtol (tidlist, &p, 10);
591 error ("Error parsing %s", tidlist);
594 while (*tidlist == ' ' || *tidlist == '\t')
600 for (; start <= end; start++)
602 tp = find_thread_id (start);
605 warning ("Unknown thread %d.", start);
606 else if (!thread_alive (tp))
607 warning ("Thread %d has terminated.", start);
610 switch_to_thread (tp->ptid);
611 printf_filtered ("\nThread %d (%s):\n", tp->num,
612 target_tid_to_str (inferior_ptid));
613 execute_command (cmd, from_tty);
614 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
619 do_cleanups (saved_cmd_cleanup_chain);
620 do_cleanups (old_chain);
623 /* Switch to the specified thread. Will dispatch off to thread_apply_command
624 if prefix of arg is `apply'. */
627 thread_command (char *tidstr, int from_tty)
631 /* Don't generate an error, just say which thread is current. */
632 if (target_has_stack)
633 printf_filtered ("[Current thread is %d (%s)]\n",
634 pid_to_thread_id (inferior_ptid),
635 target_tid_to_str (inferior_ptid));
641 gdb_thread_select (uiout, tidstr);
645 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
648 struct thread_info *tp;
650 num = value_as_long (parse_and_eval (tidstr));
652 tp = find_thread_id (num);
655 error ("Thread ID %d not known.", num);
657 if (!thread_alive (tp))
658 error ("Thread ID %d has terminated.\n", num);
660 switch_to_thread (tp->ptid);
662 ui_out_text (uiout, "[Switching to thread ");
663 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
664 ui_out_text (uiout, " (");
665 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
666 ui_out_text (uiout, ")]");
668 print_stack_frame (get_selected_frame (), 1, SRC_AND_LOC);
673 gdb_thread_select (struct ui_out *uiout, char *tidstr)
675 return catch_exceptions (uiout, do_captured_thread_select, tidstr,
676 NULL, RETURN_MASK_ALL);
679 /* Commands with a prefix of `thread'. */
680 struct cmd_list_element *thread_cmd_list = NULL;
683 _initialize_thread (void)
685 static struct cmd_list_element *thread_apply_list = NULL;
687 add_info ("threads", info_threads_command,
688 "IDs of currently known threads.");
690 add_prefix_cmd ("thread", class_run, thread_command,
691 "Use this command to switch between threads.\n\
692 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1, &cmdlist);
694 add_prefix_cmd ("apply", class_run, thread_apply_command,
695 "Apply a command to a list of threads.",
696 &thread_apply_list, "apply ", 1, &thread_cmd_list);
698 add_cmd ("all", class_run, thread_apply_all_command,
699 "Apply a command to all threads.", &thread_apply_list);
702 add_com_alias ("t", "thread", class_run, 1);