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 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., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
32 #include "gdbthread.h"
33 #include "exceptions.h"
38 #include "gdb_string.h"
41 #include <sys/types.h>
45 /*#include "lynxos-core.h" */
47 /* Definition of struct thread_info exported to gdbthread.h */
49 /* Prototypes for exported functions. */
51 void _initialize_thread (void);
53 /* Prototypes for local functions. */
55 static struct thread_info
*thread_list
= NULL
;
56 static int highest_thread_num
;
58 static struct thread_info
*find_thread_id (int num
);
60 static void thread_command (char *tidstr
, int from_tty
);
61 static void thread_apply_all_command (char *, int);
62 static int thread_alive (struct thread_info
*);
63 static void info_threads_command (char *, int);
64 static void thread_apply_command (char *, int);
65 static void restore_current_thread (ptid_t
);
66 static void switch_to_thread (ptid_t ptid
);
67 static void prune_threads (void);
68 static struct cleanup
*make_cleanup_restore_current_thread (ptid_t
,
72 delete_step_resume_breakpoint (void *arg
)
74 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
75 struct thread_info
*tp
;
77 if (*breakpointp
!= NULL
)
79 delete_breakpoint (*breakpointp
);
80 for (tp
= thread_list
; tp
; tp
= tp
->next
)
81 if (tp
->step_resume_breakpoint
== *breakpointp
)
82 tp
->step_resume_breakpoint
= NULL
;
89 free_thread (struct thread_info
*tp
)
91 /* NOTE: this will take care of any left-over step_resume breakpoints,
92 but not any user-specified thread-specific breakpoints. */
93 if (tp
->step_resume_breakpoint
)
94 delete_breakpoint (tp
->step_resume_breakpoint
);
96 /* FIXME: do I ever need to call the back-end to give it a
97 chance at this private data before deleting the thread? */
105 init_thread_list (void)
107 struct thread_info
*tp
, *tpnext
;
109 highest_thread_num
= 0;
113 for (tp
= thread_list
; tp
; tp
= tpnext
)
122 /* add_thread now returns a pointer to the new thread_info,
123 so that back_ends can initialize their private data. */
126 add_thread (ptid_t ptid
)
128 struct thread_info
*tp
;
130 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
131 memset (tp
, 0, sizeof (*tp
));
133 tp
->num
= ++highest_thread_num
;
134 tp
->next
= thread_list
;
140 delete_thread (ptid_t ptid
)
142 struct thread_info
*tp
, *tpprev
;
146 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
147 if (ptid_equal (tp
->ptid
, ptid
))
154 tpprev
->next
= tp
->next
;
156 thread_list
= tp
->next
;
161 static struct thread_info
*
162 find_thread_id (int num
)
164 struct thread_info
*tp
;
166 for (tp
= thread_list
; tp
; tp
= tp
->next
)
173 /* Find a thread_info by matching PTID. */
175 find_thread_pid (ptid_t ptid
)
177 struct thread_info
*tp
;
179 for (tp
= thread_list
; tp
; tp
= tp
->next
)
180 if (ptid_equal (tp
->ptid
, ptid
))
187 * Thread iterator function.
189 * Calls a callback function once for each thread, so long as
190 * the callback function returns false. If the callback function
191 * returns true, the iteration will end and the current thread
192 * will be returned. This can be useful for implementing a
193 * search for a thread with arbitrary attributes, or for applying
194 * some operation to every thread.
196 * FIXME: some of the existing functionality, such as
197 * "Thread apply all", might be rewritten using this functionality.
201 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
204 struct thread_info
*tp
;
206 for (tp
= thread_list
; tp
; tp
= tp
->next
)
207 if ((*callback
) (tp
, data
))
214 valid_thread_id (int num
)
216 struct thread_info
*tp
;
218 for (tp
= thread_list
; tp
; tp
= tp
->next
)
226 pid_to_thread_id (ptid_t ptid
)
228 struct thread_info
*tp
;
230 for (tp
= thread_list
; tp
; tp
= tp
->next
)
231 if (ptid_equal (tp
->ptid
, ptid
))
238 thread_id_to_pid (int num
)
240 struct thread_info
*thread
= find_thread_id (num
);
244 return pid_to_ptid (-1);
248 in_thread_list (ptid_t ptid
)
250 struct thread_info
*tp
;
252 for (tp
= thread_list
; tp
; tp
= tp
->next
)
253 if (ptid_equal (tp
->ptid
, ptid
))
256 return 0; /* Never heard of 'im */
259 /* Print a list of thread ids currently known, and the total number of
260 threads. To be used from within catch_errors. */
262 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
264 struct thread_info
*tp
;
266 struct cleanup
*cleanup_chain
;
269 target_find_new_threads ();
271 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
273 for (tp
= thread_list
; tp
; tp
= tp
->next
)
276 ui_out_field_int (uiout
, "thread-id", tp
->num
);
279 do_cleanups (cleanup_chain
);
280 ui_out_field_int (uiout
, "number-of-threads", num
);
284 /* Official gdblib interface function to get a list of thread ids and
287 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
289 return catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
290 error_message
, RETURN_MASK_ALL
);
293 /* Load infrun state for the thread PID. */
296 load_infrun_state (ptid_t ptid
,
299 struct breakpoint
**step_resume_breakpoint
,
300 CORE_ADDR
*step_range_start
,
301 CORE_ADDR
*step_range_end
,
302 struct frame_id
*step_frame_id
,
303 int *handling_longjmp
,
305 int *stepping_through_solib_after_catch
,
306 bpstat
*stepping_through_solib_catchpoints
,
308 struct symtab
**current_symtab
)
310 struct thread_info
*tp
;
312 /* If we can't find the thread, then we're debugging a single threaded
313 process. No need to do anything in that case. */
314 tp
= find_thread_id (pid_to_thread_id (ptid
));
318 *prev_pc
= tp
->prev_pc
;
319 *trap_expected
= tp
->trap_expected
;
320 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
321 *step_range_start
= tp
->step_range_start
;
322 *step_range_end
= tp
->step_range_end
;
323 *step_frame_id
= tp
->step_frame_id
;
324 *handling_longjmp
= tp
->handling_longjmp
;
325 *another_trap
= tp
->another_trap
;
326 *stepping_through_solib_after_catch
=
327 tp
->stepping_through_solib_after_catch
;
328 *stepping_through_solib_catchpoints
=
329 tp
->stepping_through_solib_catchpoints
;
330 *current_line
= tp
->current_line
;
331 *current_symtab
= tp
->current_symtab
;
334 /* Save infrun state for the thread PID. */
337 save_infrun_state (ptid_t ptid
,
340 struct breakpoint
*step_resume_breakpoint
,
341 CORE_ADDR step_range_start
,
342 CORE_ADDR step_range_end
,
343 const struct frame_id
*step_frame_id
,
344 int handling_longjmp
,
346 int stepping_through_solib_after_catch
,
347 bpstat stepping_through_solib_catchpoints
,
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
->current_line
= current_line
;
370 tp
->current_symtab
= current_symtab
;
373 /* Return true if TP is an active thread. */
375 thread_alive (struct thread_info
*tp
)
377 if (PIDGET (tp
->ptid
) == -1)
379 if (!target_thread_alive (tp
->ptid
))
381 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
390 struct thread_info
*tp
, *next
;
392 for (tp
= thread_list
; tp
; tp
= next
)
395 if (!thread_alive (tp
))
396 delete_thread (tp
->ptid
);
400 /* Print information about currently known threads
402 * Note: this has the drawback that it _really_ switches
403 * threads, which frees the frame cache. A no-side
404 * effects info-threads command would be nicer.
408 info_threads_command (char *arg
, int from_tty
)
410 struct thread_info
*tp
;
412 struct frame_info
*cur_frame
;
413 struct cleanup
*old_chain
;
414 struct frame_id saved_frame_id
;
417 /* Backup current thread and selected frame. */
418 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
419 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
422 target_find_new_threads ();
423 current_ptid
= inferior_ptid
;
424 for (tp
= thread_list
; tp
; tp
= tp
->next
)
426 if (ptid_equal (tp
->ptid
, current_ptid
))
427 printf_filtered ("* ");
429 printf_filtered (" ");
431 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
433 extra_info
= target_extra_thread_info (tp
);
435 printf_filtered (" (%s)", extra_info
);
437 /* That switch put us at the top of the stack (leaf frame). */
438 switch_to_thread (tp
->ptid
);
439 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
442 /* Restores the current thread and the frame selected before
443 the "info threads" command. */
444 do_cleanups (old_chain
);
446 /* If case we were not able to find the original frame, print the
447 new selected frame. */
448 if (frame_find_by_id (saved_frame_id
) == NULL
)
450 warning (_("Couldn't restore frame in current thread, at frame 0"));
451 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
455 /* Switch from one thread to another. */
458 switch_to_thread (ptid_t ptid
)
460 if (ptid_equal (ptid
, inferior_ptid
))
463 inferior_ptid
= ptid
;
464 reinit_frame_cache ();
465 registers_changed ();
466 stop_pc
= read_pc ();
470 restore_current_thread (ptid_t ptid
)
472 if (!ptid_equal (ptid
, inferior_ptid
))
474 switch_to_thread (ptid
);
479 restore_selected_frame (struct frame_id a_frame_id
)
481 struct frame_info
*selected_frame_info
= NULL
;
483 if (frame_id_eq (a_frame_id
, null_frame_id
))
486 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
488 select_frame (selected_frame_info
);
492 struct current_thread_cleanup
494 ptid_t inferior_ptid
;
495 struct frame_id selected_frame_id
;
499 do_restore_current_thread_cleanup (void *arg
)
501 struct current_thread_cleanup
*old
= arg
;
502 restore_current_thread (old
->inferior_ptid
);
503 restore_selected_frame (old
->selected_frame_id
);
507 static struct cleanup
*
508 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
509 struct frame_id a_frame_id
)
511 struct current_thread_cleanup
*old
512 = xmalloc (sizeof (struct current_thread_cleanup
));
513 old
->inferior_ptid
= inferior_ptid
;
514 old
->selected_frame_id
= a_frame_id
;
515 return make_cleanup (do_restore_current_thread_cleanup
, old
);
518 /* Apply a GDB command to a list of threads. List syntax is a whitespace
519 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
520 of two numbers seperated by a hyphen. Examples:
522 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
523 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
524 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
528 thread_apply_all_command (char *cmd
, int from_tty
)
530 struct thread_info
*tp
;
531 struct cleanup
*old_chain
;
532 struct cleanup
*saved_cmd_cleanup_chain
;
534 struct frame_id saved_frame_id
;
536 int thread_has_changed
= 0;
538 if (cmd
== NULL
|| *cmd
== '\000')
539 error (_("Please specify a command following the thread ID list"));
541 current_ptid
= inferior_ptid
;
542 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
543 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
545 /* It is safe to update the thread list now, before
546 traversing it for "thread apply all". MVS */
547 target_find_new_threads ();
549 /* Save a copy of the command in case it is clobbered by
551 saved_cmd
= xstrdup (cmd
);
552 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
553 for (tp
= thread_list
; tp
; tp
= tp
->next
)
554 if (thread_alive (tp
))
556 switch_to_thread (tp
->ptid
);
557 printf_filtered (_("\nThread %d (%s):\n"),
558 tp
->num
, target_tid_to_str (inferior_ptid
));
559 execute_command (cmd
, from_tty
);
560 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
563 if (!ptid_equal (current_ptid
, inferior_ptid
))
564 thread_has_changed
= 1;
566 do_cleanups (saved_cmd_cleanup_chain
);
567 do_cleanups (old_chain
);
568 /* Print stack frame only if we changed thread. */
569 if (thread_has_changed
)
570 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
575 thread_apply_command (char *tidlist
, int from_tty
)
579 struct cleanup
*old_chain
;
580 struct cleanup
*saved_cmd_cleanup_chain
;
582 struct frame_id saved_frame_id
;
584 int thread_has_changed
= 0;
586 if (tidlist
== NULL
|| *tidlist
== '\000')
587 error (_("Please specify a thread ID list"));
589 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
592 error (_("Please specify a command following the thread ID list"));
594 current_ptid
= inferior_ptid
;
595 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
596 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
598 /* Save a copy of the command in case it is clobbered by
600 saved_cmd
= xstrdup (cmd
);
601 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
602 while (tidlist
< cmd
)
604 struct thread_info
*tp
;
607 start
= strtol (tidlist
, &p
, 10);
609 error (_("Error parsing %s"), tidlist
);
612 while (*tidlist
== ' ' || *tidlist
== '\t')
615 if (*tidlist
== '-') /* Got a range of IDs? */
617 tidlist
++; /* Skip the - */
618 end
= strtol (tidlist
, &p
, 10);
620 error (_("Error parsing %s"), tidlist
);
623 while (*tidlist
== ' ' || *tidlist
== '\t')
629 for (; start
<= end
; start
++)
631 tp
= find_thread_id (start
);
634 warning (_("Unknown thread %d."), start
);
635 else if (!thread_alive (tp
))
636 warning (_("Thread %d has terminated."), start
);
639 switch_to_thread (tp
->ptid
);
640 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
641 target_tid_to_str (inferior_ptid
));
642 execute_command (cmd
, from_tty
);
643 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
648 if (!ptid_equal (current_ptid
, inferior_ptid
))
649 thread_has_changed
= 1;
651 do_cleanups (saved_cmd_cleanup_chain
);
652 do_cleanups (old_chain
);
653 /* Print stack frame only if we changed thread. */
654 if (thread_has_changed
)
655 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
658 /* Switch to the specified thread. Will dispatch off to thread_apply_command
659 if prefix of arg is `apply'. */
662 thread_command (char *tidstr
, int from_tty
)
666 /* Don't generate an error, just say which thread is current. */
667 if (target_has_stack
)
668 printf_filtered (_("[Current thread is %d (%s)]\n"),
669 pid_to_thread_id (inferior_ptid
),
670 target_tid_to_str (inferior_ptid
));
672 error (_("No stack."));
676 gdb_thread_select (uiout
, tidstr
, NULL
);
680 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
683 struct thread_info
*tp
;
685 num
= value_as_long (parse_and_eval (tidstr
));
687 tp
= find_thread_id (num
);
690 error (_("Thread ID %d not known."), num
);
692 if (!thread_alive (tp
))
693 error (_("Thread ID %d has terminated."), num
);
695 switch_to_thread (tp
->ptid
);
697 ui_out_text (uiout
, "[Switching to thread ");
698 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
699 ui_out_text (uiout
, " (");
700 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
701 ui_out_text (uiout
, ")]");
703 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
708 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
710 return catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
711 error_message
, RETURN_MASK_ALL
);
714 /* Commands with a prefix of `thread'. */
715 struct cmd_list_element
*thread_cmd_list
= NULL
;
718 _initialize_thread (void)
720 static struct cmd_list_element
*thread_apply_list
= NULL
;
722 add_info ("threads", info_threads_command
,
723 _("IDs of currently known threads."));
725 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
726 Use this command to switch between threads.\n\
727 The new thread ID must be currently known."),
728 &thread_cmd_list
, "thread ", 1, &cmdlist
);
730 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
731 _("Apply a command to a list of threads."),
732 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
734 add_cmd ("all", class_run
, thread_apply_all_command
,
735 _("Apply a command to all threads."), &thread_apply_list
);
738 add_com_alias ("t", "thread", class_run
, 1);