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 switch_to_thread (ptid_t ptid
);
63 static void prune_threads (void);
64 static struct cleanup
*make_cleanup_restore_current_thread (ptid_t
,
68 delete_step_resume_breakpoint (void *arg
)
70 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
71 struct thread_info
*tp
;
73 if (*breakpointp
!= NULL
)
75 delete_breakpoint (*breakpointp
);
76 for (tp
= thread_list
; tp
; tp
= tp
->next
)
77 if (tp
->step_resume_breakpoint
== *breakpointp
)
78 tp
->step_resume_breakpoint
= NULL
;
85 free_thread (struct thread_info
*tp
)
87 /* NOTE: this will take care of any left-over step_resume breakpoints,
88 but not any user-specified thread-specific breakpoints. */
89 if (tp
->step_resume_breakpoint
)
90 delete_breakpoint (tp
->step_resume_breakpoint
);
92 /* FIXME: do I ever need to call the back-end to give it a
93 chance at this private data before deleting the thread? */
101 init_thread_list (void)
103 struct thread_info
*tp
, *tpnext
;
105 highest_thread_num
= 0;
109 for (tp
= thread_list
; tp
; tp
= tpnext
)
118 /* add_thread now returns a pointer to the new thread_info,
119 so that back_ends can initialize their private data. */
122 add_thread (ptid_t ptid
)
124 struct thread_info
*tp
;
126 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
127 memset (tp
, 0, sizeof (*tp
));
129 tp
->num
= ++highest_thread_num
;
130 tp
->next
= thread_list
;
136 delete_thread (ptid_t ptid
)
138 struct thread_info
*tp
, *tpprev
;
142 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
143 if (ptid_equal (tp
->ptid
, ptid
))
150 tpprev
->next
= tp
->next
;
152 thread_list
= tp
->next
;
157 static struct thread_info
*
158 find_thread_id (int num
)
160 struct thread_info
*tp
;
162 for (tp
= thread_list
; tp
; tp
= tp
->next
)
169 /* Find a thread_info by matching PTID. */
171 find_thread_pid (ptid_t ptid
)
173 struct thread_info
*tp
;
175 for (tp
= thread_list
; tp
; tp
= tp
->next
)
176 if (ptid_equal (tp
->ptid
, ptid
))
183 * Thread iterator function.
185 * Calls a callback function once for each thread, so long as
186 * the callback function returns false. If the callback function
187 * returns true, the iteration will end and the current thread
188 * will be returned. This can be useful for implementing a
189 * search for a thread with arbitrary attributes, or for applying
190 * some operation to every thread.
192 * FIXME: some of the existing functionality, such as
193 * "Thread apply all", might be rewritten using this functionality.
197 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
200 struct thread_info
*tp
;
202 for (tp
= thread_list
; tp
; tp
= tp
->next
)
203 if ((*callback
) (tp
, data
))
210 valid_thread_id (int num
)
212 struct thread_info
*tp
;
214 for (tp
= thread_list
; tp
; tp
= tp
->next
)
222 pid_to_thread_id (ptid_t ptid
)
224 struct thread_info
*tp
;
226 for (tp
= thread_list
; tp
; tp
= tp
->next
)
227 if (ptid_equal (tp
->ptid
, ptid
))
234 thread_id_to_pid (int num
)
236 struct thread_info
*thread
= find_thread_id (num
);
240 return pid_to_ptid (-1);
244 in_thread_list (ptid_t ptid
)
246 struct thread_info
*tp
;
248 for (tp
= thread_list
; tp
; tp
= tp
->next
)
249 if (ptid_equal (tp
->ptid
, ptid
))
252 return 0; /* Never heard of 'im */
255 /* Print a list of thread ids currently known, and the total number of
256 threads. To be used from within catch_errors. */
258 do_captured_list_thread_ids (struct ui_out
*uiout
, void *arg
)
260 struct thread_info
*tp
;
262 struct cleanup
*cleanup_chain
;
265 target_find_new_threads ();
267 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
269 for (tp
= thread_list
; tp
; tp
= tp
->next
)
272 ui_out_field_int (uiout
, "thread-id", tp
->num
);
275 do_cleanups (cleanup_chain
);
276 ui_out_field_int (uiout
, "number-of-threads", num
);
280 /* Official gdblib interface function to get a list of thread ids and
283 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
285 if (catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
286 error_message
, RETURN_MASK_ALL
) < 0)
291 /* Load infrun state for the thread PID. */
294 load_infrun_state (ptid_t ptid
,
297 struct breakpoint
**step_resume_breakpoint
,
298 CORE_ADDR
*step_range_start
,
299 CORE_ADDR
*step_range_end
,
300 struct frame_id
*step_frame_id
,
301 int *handling_longjmp
,
303 int *stepping_through_solib_after_catch
,
304 bpstat
*stepping_through_solib_catchpoints
,
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 *current_line
= tp
->current_line
;
329 *current_symtab
= tp
->current_symtab
;
332 /* Save infrun state for the thread PID. */
335 save_infrun_state (ptid_t ptid
,
338 struct breakpoint
*step_resume_breakpoint
,
339 CORE_ADDR step_range_start
,
340 CORE_ADDR step_range_end
,
341 const struct frame_id
*step_frame_id
,
342 int handling_longjmp
,
344 int stepping_through_solib_after_catch
,
345 bpstat stepping_through_solib_catchpoints
,
347 struct symtab
*current_symtab
)
349 struct thread_info
*tp
;
351 /* If we can't find the thread, then we're debugging a single-threaded
352 process. Nothing to do in that case. */
353 tp
= find_thread_id (pid_to_thread_id (ptid
));
357 tp
->prev_pc
= prev_pc
;
358 tp
->trap_expected
= trap_expected
;
359 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
360 tp
->step_range_start
= step_range_start
;
361 tp
->step_range_end
= step_range_end
;
362 tp
->step_frame_id
= (*step_frame_id
);
363 tp
->handling_longjmp
= handling_longjmp
;
364 tp
->another_trap
= another_trap
;
365 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
366 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
367 tp
->current_line
= current_line
;
368 tp
->current_symtab
= current_symtab
;
371 /* Return true if TP is an active thread. */
373 thread_alive (struct thread_info
*tp
)
375 if (PIDGET (tp
->ptid
) == -1)
377 if (!target_thread_alive (tp
->ptid
))
379 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
388 struct thread_info
*tp
, *next
;
390 for (tp
= thread_list
; tp
; tp
= next
)
393 if (!thread_alive (tp
))
394 delete_thread (tp
->ptid
);
398 /* Print information about currently known threads
400 * Note: this has the drawback that it _really_ switches
401 * threads, which frees the frame cache. A no-side
402 * effects info-threads command would be nicer.
406 info_threads_command (char *arg
, int from_tty
)
408 struct thread_info
*tp
;
410 struct frame_info
*cur_frame
;
411 struct cleanup
*old_chain
;
412 struct frame_id saved_frame_id
;
415 /* Backup current thread and selected frame. */
416 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
417 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
420 target_find_new_threads ();
421 current_ptid
= inferior_ptid
;
422 for (tp
= thread_list
; tp
; tp
= tp
->next
)
424 if (ptid_equal (tp
->ptid
, current_ptid
))
425 printf_filtered ("* ");
427 printf_filtered (" ");
429 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
431 extra_info
= target_extra_thread_info (tp
);
433 printf_filtered (" (%s)", extra_info
);
435 /* That switch put us at the top of the stack (leaf frame). */
436 switch_to_thread (tp
->ptid
);
437 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
440 /* Restores the current thread and the frame selected before
441 the "info threads" command. */
442 do_cleanups (old_chain
);
444 /* If case we were not able to find the original frame, print the
445 new selected frame. */
446 if (frame_find_by_id (saved_frame_id
) == NULL
)
448 warning (_("Couldn't restore frame in current thread, at frame 0"));
449 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
453 /* Switch from one thread to another. */
456 switch_to_thread (ptid_t ptid
)
458 if (ptid_equal (ptid
, inferior_ptid
))
461 inferior_ptid
= ptid
;
462 reinit_frame_cache ();
463 registers_changed ();
464 stop_pc
= read_pc ();
468 restore_current_thread (ptid_t ptid
)
470 if (!ptid_equal (ptid
, inferior_ptid
))
472 switch_to_thread (ptid
);
477 restore_selected_frame (struct frame_id a_frame_id
)
479 struct frame_info
*selected_frame_info
= NULL
;
481 if (frame_id_eq (a_frame_id
, null_frame_id
))
484 if ((selected_frame_info
= frame_find_by_id (a_frame_id
)) != NULL
)
486 select_frame (selected_frame_info
);
490 struct current_thread_cleanup
492 ptid_t inferior_ptid
;
493 struct frame_id selected_frame_id
;
497 do_restore_current_thread_cleanup (void *arg
)
499 struct current_thread_cleanup
*old
= arg
;
500 restore_current_thread (old
->inferior_ptid
);
501 restore_selected_frame (old
->selected_frame_id
);
505 static struct cleanup
*
506 make_cleanup_restore_current_thread (ptid_t inferior_ptid
,
507 struct frame_id a_frame_id
)
509 struct current_thread_cleanup
*old
510 = xmalloc (sizeof (struct current_thread_cleanup
));
511 old
->inferior_ptid
= inferior_ptid
;
512 old
->selected_frame_id
= a_frame_id
;
513 return make_cleanup (do_restore_current_thread_cleanup
, old
);
516 /* Apply a GDB command to a list of threads. List syntax is a whitespace
517 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
518 of two numbers seperated by a hyphen. Examples:
520 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
521 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
522 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
526 thread_apply_all_command (char *cmd
, int from_tty
)
528 struct thread_info
*tp
;
529 struct cleanup
*old_chain
;
530 struct cleanup
*saved_cmd_cleanup_chain
;
532 struct frame_id saved_frame_id
;
534 int thread_has_changed
= 0;
536 if (cmd
== NULL
|| *cmd
== '\000')
537 error (_("Please specify a command following the thread ID list"));
539 current_ptid
= inferior_ptid
;
540 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
541 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
543 /* It is safe to update the thread list now, before
544 traversing it for "thread apply all". MVS */
545 target_find_new_threads ();
547 /* Save a copy of the command in case it is clobbered by
549 saved_cmd
= xstrdup (cmd
);
550 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
551 for (tp
= thread_list
; tp
; tp
= tp
->next
)
552 if (thread_alive (tp
))
554 switch_to_thread (tp
->ptid
);
555 printf_filtered (_("\nThread %d (%s):\n"),
556 tp
->num
, target_tid_to_str (inferior_ptid
));
557 execute_command (cmd
, from_tty
);
558 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
561 if (!ptid_equal (current_ptid
, inferior_ptid
))
562 thread_has_changed
= 1;
564 do_cleanups (saved_cmd_cleanup_chain
);
565 do_cleanups (old_chain
);
566 /* Print stack frame only if we changed thread. */
567 if (thread_has_changed
)
568 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
573 thread_apply_command (char *tidlist
, int from_tty
)
577 struct cleanup
*old_chain
;
578 struct cleanup
*saved_cmd_cleanup_chain
;
580 struct frame_id saved_frame_id
;
582 int thread_has_changed
= 0;
584 if (tidlist
== NULL
|| *tidlist
== '\000')
585 error (_("Please specify a thread ID list"));
587 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
590 error (_("Please specify a command following the thread ID list"));
592 current_ptid
= inferior_ptid
;
593 saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
594 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
, saved_frame_id
);
596 /* Save a copy of the command in case it is clobbered by
598 saved_cmd
= xstrdup (cmd
);
599 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
600 while (tidlist
< cmd
)
602 struct thread_info
*tp
;
605 start
= strtol (tidlist
, &p
, 10);
607 error (_("Error parsing %s"), tidlist
);
610 while (*tidlist
== ' ' || *tidlist
== '\t')
613 if (*tidlist
== '-') /* Got a range of IDs? */
615 tidlist
++; /* Skip the - */
616 end
= strtol (tidlist
, &p
, 10);
618 error (_("Error parsing %s"), tidlist
);
621 while (*tidlist
== ' ' || *tidlist
== '\t')
627 for (; start
<= end
; start
++)
629 tp
= find_thread_id (start
);
632 warning (_("Unknown thread %d."), start
);
633 else if (!thread_alive (tp
))
634 warning (_("Thread %d has terminated."), start
);
637 switch_to_thread (tp
->ptid
);
638 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
639 target_tid_to_str (inferior_ptid
));
640 execute_command (cmd
, from_tty
);
641 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
646 if (!ptid_equal (current_ptid
, inferior_ptid
))
647 thread_has_changed
= 1;
649 do_cleanups (saved_cmd_cleanup_chain
);
650 do_cleanups (old_chain
);
651 /* Print stack frame only if we changed thread. */
652 if (thread_has_changed
)
653 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
656 /* Switch to the specified thread. Will dispatch off to thread_apply_command
657 if prefix of arg is `apply'. */
660 thread_command (char *tidstr
, int from_tty
)
664 /* Don't generate an error, just say which thread is current. */
665 if (target_has_stack
)
666 printf_filtered (_("[Current thread is %d (%s)]\n"),
667 pid_to_thread_id (inferior_ptid
),
668 target_tid_to_str (inferior_ptid
));
670 error (_("No stack."));
674 gdb_thread_select (uiout
, tidstr
, NULL
);
678 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
681 struct thread_info
*tp
;
683 num
= value_as_long (parse_and_eval (tidstr
));
685 tp
= find_thread_id (num
);
688 error (_("Thread ID %d not known."), num
);
690 if (!thread_alive (tp
))
691 error (_("Thread ID %d has terminated."), num
);
693 switch_to_thread (tp
->ptid
);
695 ui_out_text (uiout
, "[Switching to thread ");
696 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
697 ui_out_text (uiout
, " (");
698 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
699 ui_out_text (uiout
, ")]");
701 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
706 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
708 if (catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
709 error_message
, RETURN_MASK_ALL
) < 0)
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);