1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 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"
39 #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);
70 delete_step_resume_breakpoint (void *arg
)
72 struct breakpoint
**breakpointp
= (struct breakpoint
**) arg
;
73 struct thread_info
*tp
;
75 if (*breakpointp
!= NULL
)
77 delete_breakpoint (*breakpointp
);
78 for (tp
= thread_list
; tp
; tp
= tp
->next
)
79 if (tp
->step_resume_breakpoint
== *breakpointp
)
80 tp
->step_resume_breakpoint
= NULL
;
87 free_thread (struct thread_info
*tp
)
89 /* NOTE: this will take care of any left-over step_resume breakpoints,
90 but not any user-specified thread-specific breakpoints. */
91 if (tp
->step_resume_breakpoint
)
92 delete_breakpoint (tp
->step_resume_breakpoint
);
94 /* FIXME: do I ever need to call the back-end to give it a
95 chance at this private data before deleting the thread? */
103 init_thread_list (void)
105 struct thread_info
*tp
, *tpnext
;
107 highest_thread_num
= 0;
111 for (tp
= thread_list
; tp
; tp
= tpnext
)
120 /* add_thread now returns a pointer to the new thread_info,
121 so that back_ends can initialize their private data. */
124 add_thread (ptid_t ptid
)
126 struct thread_info
*tp
;
128 tp
= (struct thread_info
*) xmalloc (sizeof (*tp
));
129 memset (tp
, 0, sizeof (*tp
));
131 tp
->num
= ++highest_thread_num
;
132 tp
->next
= thread_list
;
138 delete_thread (ptid_t ptid
)
140 struct thread_info
*tp
, *tpprev
;
144 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
145 if (ptid_equal (tp
->ptid
, ptid
))
152 tpprev
->next
= tp
->next
;
154 thread_list
= tp
->next
;
159 static struct thread_info
*
160 find_thread_id (int num
)
162 struct thread_info
*tp
;
164 for (tp
= thread_list
; tp
; tp
= tp
->next
)
171 /* Find a thread_info by matching PTID. */
173 find_thread_pid (ptid_t ptid
)
175 struct thread_info
*tp
;
177 for (tp
= thread_list
; tp
; tp
= tp
->next
)
178 if (ptid_equal (tp
->ptid
, ptid
))
185 * Thread iterator function.
187 * Calls a callback function once for each thread, so long as
188 * the callback function returns false. If the callback function
189 * returns true, the iteration will end and the current thread
190 * will be returned. This can be useful for implementing a
191 * search for a thread with arbitrary attributes, or for applying
192 * some operation to every thread.
194 * FIXME: some of the existing functionality, such as
195 * "Thread apply all", might be rewritten using this functionality.
199 iterate_over_threads (int (*callback
) (struct thread_info
*, void *),
202 struct thread_info
*tp
;
204 for (tp
= thread_list
; tp
; tp
= tp
->next
)
205 if ((*callback
) (tp
, data
))
212 valid_thread_id (int num
)
214 struct thread_info
*tp
;
216 for (tp
= thread_list
; tp
; tp
= tp
->next
)
224 pid_to_thread_id (ptid_t ptid
)
226 struct thread_info
*tp
;
228 for (tp
= thread_list
; tp
; tp
= tp
->next
)
229 if (ptid_equal (tp
->ptid
, ptid
))
236 thread_id_to_pid (int num
)
238 struct thread_info
*thread
= find_thread_id (num
);
242 return pid_to_ptid (-1);
246 in_thread_list (ptid_t ptid
)
248 struct thread_info
*tp
;
250 for (tp
= thread_list
; tp
; tp
= tp
->next
)
251 if (ptid_equal (tp
->ptid
, ptid
))
254 return 0; /* Never heard of 'im */
257 /* Print a list of thread ids currently known, and the total number of
258 threads. To be used from within catch_errors. */
260 do_captured_list_thread_ids (struct ui_out
*uiout
,
263 struct thread_info
*tp
;
266 ui_out_tuple_begin (uiout
, "thread-ids");
268 for (tp
= thread_list
; tp
; tp
= tp
->next
)
271 ui_out_field_int (uiout
, "thread-id", tp
->num
);
274 ui_out_tuple_end (uiout
);
275 ui_out_field_int (uiout
, "number-of-threads", num
);
279 /* Official gdblib interface function to get a list of thread ids and
282 gdb_list_thread_ids (struct ui_out
*uiout
)
284 return catch_exceptions (uiout
, do_captured_list_thread_ids
, NULL
,
285 NULL
, RETURN_MASK_ALL
);
289 /* Load infrun state for the thread PID. */
292 load_infrun_state (ptid_t ptid
,
294 CORE_ADDR
*prev_func_start
,
295 char **prev_func_name
,
297 struct breakpoint
**step_resume_breakpoint
,
298 struct breakpoint
**through_sigtramp_breakpoint
,
299 CORE_ADDR
*step_range_start
,
300 CORE_ADDR
*step_range_end
,
301 CORE_ADDR
*step_frame_address
,
302 int *handling_longjmp
,
304 int *stepping_through_solib_after_catch
,
305 bpstat
*stepping_through_solib_catchpoints
,
306 int *stepping_through_sigtramp
,
308 struct symtab
**current_symtab
,
311 struct thread_info
*tp
;
313 /* If we can't find the thread, then we're debugging a single threaded
314 process. No need to do anything in that case. */
315 tp
= find_thread_id (pid_to_thread_id (ptid
));
319 *prev_pc
= tp
->prev_pc
;
320 *prev_func_start
= tp
->prev_func_start
;
321 *prev_func_name
= tp
->prev_func_name
;
322 *trap_expected
= tp
->trap_expected
;
323 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
324 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
325 *step_range_start
= tp
->step_range_start
;
326 *step_range_end
= tp
->step_range_end
;
327 *step_frame_address
= tp
->step_frame_address
;
328 *handling_longjmp
= tp
->handling_longjmp
;
329 *another_trap
= tp
->another_trap
;
330 *stepping_through_solib_after_catch
= tp
->stepping_through_solib_after_catch
;
331 *stepping_through_solib_catchpoints
= tp
->stepping_through_solib_catchpoints
;
332 *stepping_through_sigtramp
= tp
->stepping_through_sigtramp
;
333 *current_line
= tp
->current_line
;
334 *current_symtab
= tp
->current_symtab
;
335 *step_sp
= tp
->step_sp
;
338 /* Save infrun state for the thread PID. */
341 save_infrun_state (ptid_t ptid
,
343 CORE_ADDR prev_func_start
,
344 char *prev_func_name
,
346 struct breakpoint
*step_resume_breakpoint
,
347 struct breakpoint
*through_sigtramp_breakpoint
,
348 CORE_ADDR step_range_start
,
349 CORE_ADDR step_range_end
,
350 CORE_ADDR step_frame_address
,
351 int handling_longjmp
,
353 int stepping_through_solib_after_catch
,
354 bpstat stepping_through_solib_catchpoints
,
355 int stepping_through_sigtramp
,
357 struct symtab
*current_symtab
,
360 struct thread_info
*tp
;
362 /* If we can't find the thread, then we're debugging a single-threaded
363 process. Nothing to do in that case. */
364 tp
= find_thread_id (pid_to_thread_id (ptid
));
368 tp
->prev_pc
= prev_pc
;
369 tp
->prev_func_start
= prev_func_start
;
370 tp
->prev_func_name
= prev_func_name
;
371 tp
->trap_expected
= trap_expected
;
372 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
373 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
374 tp
->step_range_start
= step_range_start
;
375 tp
->step_range_end
= step_range_end
;
376 tp
->step_frame_address
= step_frame_address
;
377 tp
->handling_longjmp
= handling_longjmp
;
378 tp
->another_trap
= another_trap
;
379 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
380 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
381 tp
->stepping_through_sigtramp
= stepping_through_sigtramp
;
382 tp
->current_line
= current_line
;
383 tp
->current_symtab
= current_symtab
;
384 tp
->step_sp
= step_sp
;
387 /* Return true if TP is an active thread. */
389 thread_alive (struct thread_info
*tp
)
391 if (PIDGET (tp
->ptid
) == -1)
393 if (!target_thread_alive (tp
->ptid
))
395 tp
->ptid
= pid_to_ptid (-1); /* Mark it as dead */
404 struct thread_info
*tp
, *next
;
406 for (tp
= thread_list
; tp
; tp
= next
)
409 if (!thread_alive (tp
))
410 delete_thread (tp
->ptid
);
414 /* Print information about currently known threads
416 * Note: this has the drawback that it _really_ switches
417 * threads, which frees the frame cache. A no-side
418 * effects info-threads command would be nicer.
422 info_threads_command (char *arg
, int from_tty
)
424 struct thread_info
*tp
;
426 struct frame_info
*cur_frame
;
427 int saved_frame_level
= selected_frame_level
;
431 /* Avoid coredumps which would happen if we tried to access a NULL
433 if (!target_has_stack
)
437 target_find_new_threads ();
438 current_ptid
= inferior_ptid
;
439 for (tp
= thread_list
; tp
; tp
= tp
->next
)
441 if (ptid_equal (tp
->ptid
, current_ptid
))
442 printf_filtered ("* ");
444 printf_filtered (" ");
447 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
449 printf_filtered ("%d %s", tp
->num
, target_pid_to_str (tp
->ptid
));
452 extra_info
= target_extra_thread_info (tp
);
454 printf_filtered (" (%s)", extra_info
);
457 switch_to_thread (tp
->ptid
);
459 print_only_stack_frame (selected_frame
, -1, 0);
461 printf_filtered ("[No stack.]\n");
464 switch_to_thread (current_ptid
);
466 /* Code below copied from "up_silently_base" in "stack.c".
467 * It restores the frame set by the user before the "info threads"
468 * command. We have finished the info-threads display by switching
469 * back to the current thread. That switch has put us at the top
470 * of the stack (leaf frame).
472 counter
= saved_frame_level
;
473 cur_frame
= find_relative_frame (selected_frame
, &counter
);
476 /* Ooops, can't restore, tell user where we are. */
477 warning ("Couldn't restore frame in current thread, at frame 0");
478 print_stack_frame (selected_frame
, -1, 0);
482 select_frame (cur_frame
, saved_frame_level
);
485 /* re-show current frame. */
486 show_stack_frame (cur_frame
);
489 /* Switch from one thread to another. */
492 switch_to_thread (ptid_t ptid
)
494 if (ptid_equal (ptid
, inferior_ptid
))
497 inferior_ptid
= ptid
;
498 flush_cached_frames ();
499 registers_changed ();
500 stop_pc
= read_pc ();
501 select_frame (get_current_frame (), 0);
505 restore_current_thread (ptid_t ptid
)
507 if (! ptid_equal (ptid
, inferior_ptid
))
509 switch_to_thread (ptid
);
510 print_stack_frame (get_current_frame (), 0, -1);
514 struct current_thread_cleanup
516 ptid_t inferior_ptid
;
520 do_restore_current_thread_cleanup (void *arg
)
522 struct current_thread_cleanup
*old
= arg
;
523 restore_current_thread (old
->inferior_ptid
);
527 static struct cleanup
*
528 make_cleanup_restore_current_thread (ptid_t inferior_ptid
)
530 struct current_thread_cleanup
*old
531 = xmalloc (sizeof (struct current_thread_cleanup
));
532 old
->inferior_ptid
= inferior_ptid
;
533 return make_cleanup (do_restore_current_thread_cleanup
, old
);
536 /* Apply a GDB command to a list of threads. List syntax is a whitespace
537 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
538 of two numbers seperated by a hyphen. Examples:
540 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
541 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
542 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
546 thread_apply_all_command (char *cmd
, int from_tty
)
548 struct thread_info
*tp
;
549 struct cleanup
*old_chain
;
550 struct cleanup
*saved_cmd_cleanup_chain
;
553 if (cmd
== NULL
|| *cmd
== '\000')
554 error ("Please specify a command following the thread ID list");
556 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
);
558 /* It is safe to update the thread list now, before
559 traversing it for "thread apply all". MVS */
560 target_find_new_threads ();
562 /* Save a copy of the command in case it is clobbered by
564 saved_cmd
= xstrdup (cmd
);
565 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
566 for (tp
= thread_list
; tp
; tp
= tp
->next
)
567 if (thread_alive (tp
))
569 switch_to_thread (tp
->ptid
);
571 printf_filtered ("\nThread %d (%s):\n",
573 target_tid_to_str (inferior_ptid
));
575 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
576 target_pid_to_str (inferior_ptid
));
578 execute_command (cmd
, from_tty
);
579 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
582 do_cleanups (saved_cmd_cleanup_chain
);
583 do_cleanups (old_chain
);
587 thread_apply_command (char *tidlist
, int from_tty
)
591 struct cleanup
*old_chain
;
592 struct cleanup
*saved_cmd_cleanup_chain
;
595 if (tidlist
== NULL
|| *tidlist
== '\000')
596 error ("Please specify a thread ID list");
598 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
601 error ("Please specify a command following the thread ID list");
603 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
);
605 /* Save a copy of the command in case it is clobbered by
607 saved_cmd
= xstrdup (cmd
);
608 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
609 while (tidlist
< cmd
)
611 struct thread_info
*tp
;
614 start
= strtol (tidlist
, &p
, 10);
616 error ("Error parsing %s", tidlist
);
619 while (*tidlist
== ' ' || *tidlist
== '\t')
622 if (*tidlist
== '-') /* Got a range of IDs? */
624 tidlist
++; /* Skip the - */
625 end
= strtol (tidlist
, &p
, 10);
627 error ("Error parsing %s", tidlist
);
630 while (*tidlist
== ' ' || *tidlist
== '\t')
636 for (; start
<= end
; start
++)
638 tp
= find_thread_id (start
);
641 warning ("Unknown thread %d.", start
);
642 else if (!thread_alive (tp
))
643 warning ("Thread %d has terminated.", start
);
646 switch_to_thread (tp
->ptid
);
648 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
649 target_tid_to_str (inferior_ptid
));
651 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
652 target_pid_to_str (inferior_ptid
));
654 execute_command (cmd
, from_tty
);
655 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
660 do_cleanups (saved_cmd_cleanup_chain
);
661 do_cleanups (old_chain
);
664 /* Switch to the specified thread. Will dispatch off to thread_apply_command
665 if prefix of arg is `apply'. */
668 thread_command (char *tidstr
, int from_tty
)
672 /* Don't generate an error, just say which thread is current. */
673 if (target_has_stack
)
674 printf_filtered ("[Current thread is %d (%s)]\n",
675 pid_to_thread_id (inferior_ptid
),
676 #if defined(HPUXHPPA)
677 target_tid_to_str (inferior_ptid
)
679 target_pid_to_str (inferior_ptid
)
687 gdb_thread_select (uiout
, tidstr
);
691 do_captured_thread_select (struct ui_out
*uiout
,
695 struct thread_info
*tp
;
697 num
= value_as_long (parse_and_eval (tidstr
));
699 tp
= find_thread_id (num
);
703 error ("Thread ID %d not known.", num
);
706 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
707 see the IDs of currently known threads.", num
);
710 if (!thread_alive (tp
))
711 error ("Thread ID %d has terminated.\n", num
);
713 switch_to_thread (tp
->ptid
);
716 ui_out_text (uiout
, "[Switching to thread ");
717 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
718 ui_out_text (uiout
, " (");
719 #if defined(HPUXHPPA)
720 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
722 ui_out_text (uiout
, target_pid_to_str (inferior_ptid
));
724 ui_out_text (uiout
, ")]");
726 printf_filtered ("[Switching to thread %d (%s)]\n",
727 pid_to_thread_id (inferior_ptid
),
728 #if defined(HPUXHPPA)
729 target_tid_to_str (inferior_ptid
)
731 target_pid_to_str (inferior_ptid
)
736 print_stack_frame (selected_frame
, selected_frame_level
, 1);
741 gdb_thread_select (struct ui_out
*uiout
,
744 return catch_exceptions (uiout
, do_captured_thread_select
, tidstr
,
745 NULL
, RETURN_MASK_ALL
);
748 /* Commands with a prefix of `thread'. */
749 struct cmd_list_element
*thread_cmd_list
= NULL
;
752 _initialize_thread (void)
754 static struct cmd_list_element
*thread_apply_list
= NULL
;
756 add_info ("threads", info_threads_command
,
757 "IDs of currently known threads.");
759 add_prefix_cmd ("thread", class_run
, thread_command
,
760 "Use this command to switch between threads.\n\
761 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
764 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
765 "Apply a command to a list of threads.",
766 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
768 add_cmd ("all", class_run
, thread_apply_all_command
,
769 "Apply a command to all threads.",
773 add_com_alias ("t", "thread", class_run
, 1);