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 flush_cached_frames ();
465 registers_changed ();
466 stop_pc
= read_pc ();
467 select_frame (get_current_frame ());
471 restore_current_thread (ptid_t ptid
)
473 if (!ptid_equal (ptid
, inferior_ptid
))
475 switch_to_thread (ptid
);
480 restore_selected_frame (struct frame_id a_frame_id
)
482 struct frame_info
*selected_frame_info
= NULL
;
484 if (frame_id_eq (a_frame_id
, null_frame_id
))
487 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
489 select_frame (selected_frame_info
);
493 struct current_thread_cleanup
495 ptid_t inferior_ptid
;
496 struct frame_id selected_frame_id
;
500 do_restore_current_thread_cleanup (void *arg
)
502 struct current_thread_cleanup
*old
= arg
;
503 restore_current_thread (old
->inferior_ptid
);
504 restore_selected_frame (old
->selected_frame_id
);
508 static struct cleanup
*
509 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
510 struct frame_id a_frame_id
)
512 struct current_thread_cleanup
*old
513 = xmalloc (sizeof (struct current_thread_cleanup
));
514 old
->inferior_ptid
= inferior_ptid
;
515 old
->selected_frame_id
= a_frame_id
;
516 return make_cleanup (do_restore_current_thread_cleanup
, old
);
519 /* Apply a GDB command to a list of threads. List syntax is a whitespace
520 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
521 of two numbers seperated by a hyphen. Examples:
523 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
524 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
525 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
529 thread_apply_all_command (char *cmd
, int from_tty
)
531 struct thread_info
*tp
;
532 struct cleanup
*old_chain
;
533 struct cleanup
*saved_cmd_cleanup_chain
;
535 struct frame_id saved_frame_id
;
537 int thread_has_changed
= 0;
539 if (cmd
== NULL
|| *cmd
== '\000')
540 error (_("Please specify a command following the thread ID list"));
542 current_ptid
= inferior_ptid
;
543 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
544 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
546 /* It is safe to update the thread list now, before
547 traversing it for "thread apply all". MVS */
548 target_find_new_threads ();
550 /* Save a copy of the command in case it is clobbered by
552 saved_cmd
= xstrdup (cmd
);
553 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
554 for (tp
= thread_list
; tp
; tp
= tp
->next
)
555 if (thread_alive (tp
))
557 switch_to_thread (tp
->ptid
);
558 printf_filtered (_("\nThread %d (%s):\n"),
559 tp
->num
, target_tid_to_str (inferior_ptid
));
560 execute_command (cmd
, from_tty
);
561 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
564 if (!ptid_equal (current_ptid
, inferior_ptid
))
565 thread_has_changed
= 1;
567 do_cleanups (saved_cmd_cleanup_chain
);
568 do_cleanups (old_chain
);
569 /* Print stack frame only if we changed thread. */
570 if (thread_has_changed
)
571 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
576 thread_apply_command (char *tidlist
, int from_tty
)
580 struct cleanup
*old_chain
;
581 struct cleanup
*saved_cmd_cleanup_chain
;
583 struct frame_id saved_frame_id
;
585 int thread_has_changed
= 0;
587 if (tidlist
== NULL
|| *tidlist
== '\000')
588 error (_("Please specify a thread ID list"));
590 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
593 error (_("Please specify a command following the thread ID list"));
595 current_ptid
= inferior_ptid
;
596 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
597 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
599 /* Save a copy of the command in case it is clobbered by
601 saved_cmd
= xstrdup (cmd
);
602 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
603 while (tidlist
< cmd
)
605 struct thread_info
*tp
;
608 start
= strtol (tidlist
, &p
, 10);
610 error (_("Error parsing %s"), tidlist
);
613 while (*tidlist
== ' ' || *tidlist
== '\t')
616 if (*tidlist
== '-') /* Got a range of IDs? */
618 tidlist
++; /* Skip the - */
619 end
= strtol (tidlist
, &p
, 10);
621 error (_("Error parsing %s"), tidlist
);
624 while (*tidlist
== ' ' || *tidlist
== '\t')
630 for (; start
<= end
; start
++)
632 tp
= find_thread_id (start
);
635 warning (_("Unknown thread %d."), start
);
636 else if (!thread_alive (tp
))
637 warning (_("Thread %d has terminated."), start
);
640 switch_to_thread (tp
->ptid
);
641 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
642 target_tid_to_str (inferior_ptid
));
643 execute_command (cmd
, from_tty
);
644 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
649 if (!ptid_equal (current_ptid
, inferior_ptid
))
650 thread_has_changed
= 1;
652 do_cleanups (saved_cmd_cleanup_chain
);
653 do_cleanups (old_chain
);
654 /* Print stack frame only if we changed thread. */
655 if (thread_has_changed
)
656 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
659 /* Switch to the specified thread. Will dispatch off to thread_apply_command
660 if prefix of arg is `apply'. */
663 thread_command (char *tidstr
, int from_tty
)
667 /* Don't generate an error, just say which thread is current. */
668 if (target_has_stack
)
669 printf_filtered (_("[Current thread is %d (%s)]\n"),
670 pid_to_thread_id (inferior_ptid
),
671 target_tid_to_str (inferior_ptid
));
673 error (_("No stack."));
677 gdb_thread_select (uiout
, tidstr
, NULL
);
681 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
684 struct thread_info
*tp
;
686 num
= value_as_long (parse_and_eval (tidstr
));
688 tp
= find_thread_id (num
);
691 error (_("Thread ID %d not known."), num
);
693 if (!thread_alive (tp
))
694 error (_("Thread ID %d has terminated."), num
);
696 switch_to_thread (tp
->ptid
);
698 ui_out_text (uiout
, "[Switching to thread ");
699 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
700 ui_out_text (uiout
, " (");
701 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
702 ui_out_text (uiout
, ")]");
704 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
709 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
711 return catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
712 error_message
, RETURN_MASK_ALL
);
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);