1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 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., 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);
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
, void *arg
)
262 struct thread_info
*tp
;
264 struct cleanup
*cleanup_chain
;
267 target_find_new_threads ();
269 cleanup_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "thread-ids");
271 for (tp
= thread_list
; tp
; tp
= tp
->next
)
274 ui_out_field_int (uiout
, "thread-id", tp
->num
);
277 do_cleanups (cleanup_chain
);
278 ui_out_field_int (uiout
, "number-of-threads", num
);
282 /* Official gdblib interface function to get a list of thread ids and
285 gdb_list_thread_ids (struct ui_out
*uiout
, char **error_message
)
287 return catch_exceptions_with_msg (uiout
, do_captured_list_thread_ids
, NULL
,
288 error_message
, RETURN_MASK_ALL
);
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 frame_id saved_frame_id
= get_frame_id (get_selected_frame (NULL
));
415 target_find_new_threads ();
416 current_ptid
= inferior_ptid
;
417 for (tp
= thread_list
; tp
; tp
= tp
->next
)
419 if (ptid_equal (tp
->ptid
, current_ptid
))
420 printf_filtered ("* ");
422 printf_filtered (" ");
424 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->ptid
));
426 extra_info
= target_extra_thread_info (tp
);
428 printf_filtered (" (%s)", extra_info
);
431 switch_to_thread (tp
->ptid
);
432 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
435 switch_to_thread (current_ptid
);
437 /* Restores the frame set by the user before the "info threads"
438 command. We have finished the info-threads display by switching
439 back to the current thread. That switch has put us at the top of
440 the stack (leaf frame). */
441 cur_frame
= frame_find_by_id (saved_frame_id
);
442 if (cur_frame
== NULL
)
444 /* Ooops, can't restore, tell user where we are. */
445 warning (_("Couldn't restore frame in current thread, at frame 0"));
446 print_stack_frame (get_selected_frame (NULL
), 0, LOCATION
);
450 select_frame (cur_frame
);
451 /* re-show current frame. */
452 show_stack_frame (cur_frame
);
456 /* Switch from one thread to another. */
459 switch_to_thread (ptid_t ptid
)
461 if (ptid_equal (ptid
, inferior_ptid
))
464 inferior_ptid
= ptid
;
465 flush_cached_frames ();
466 registers_changed ();
467 stop_pc
= read_pc ();
468 select_frame (get_current_frame ());
472 restore_current_thread (ptid_t ptid
)
474 if (!ptid_equal (ptid
, inferior_ptid
))
476 switch_to_thread (ptid
);
477 print_stack_frame (get_current_frame (), 1, SRC_LINE
);
481 struct current_thread_cleanup
483 ptid_t inferior_ptid
;
487 do_restore_current_thread_cleanup (void *arg
)
489 struct current_thread_cleanup
*old
= arg
;
490 restore_current_thread (old
->inferior_ptid
);
494 static struct cleanup
*
495 make_cleanup_restore_current_thread (ptid_t inferior_ptid
)
497 struct current_thread_cleanup
*old
498 = xmalloc (sizeof (struct current_thread_cleanup
));
499 old
->inferior_ptid
= inferior_ptid
;
500 return make_cleanup (do_restore_current_thread_cleanup
, old
);
503 /* Apply a GDB command to a list of threads. List syntax is a whitespace
504 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
505 of two numbers seperated by a hyphen. Examples:
507 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
508 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
509 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
513 thread_apply_all_command (char *cmd
, int from_tty
)
515 struct thread_info
*tp
;
516 struct cleanup
*old_chain
;
517 struct cleanup
*saved_cmd_cleanup_chain
;
520 if (cmd
== NULL
|| *cmd
== '\000')
521 error (_("Please specify a command following the thread ID list"));
523 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
);
525 /* It is safe to update the thread list now, before
526 traversing it for "thread apply all". MVS */
527 target_find_new_threads ();
529 /* Save a copy of the command in case it is clobbered by
531 saved_cmd
= xstrdup (cmd
);
532 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
533 for (tp
= thread_list
; tp
; tp
= tp
->next
)
534 if (thread_alive (tp
))
536 switch_to_thread (tp
->ptid
);
537 printf_filtered (_("\nThread %d (%s):\n"),
538 tp
->num
, target_tid_to_str (inferior_ptid
));
539 execute_command (cmd
, from_tty
);
540 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
543 do_cleanups (saved_cmd_cleanup_chain
);
544 do_cleanups (old_chain
);
548 thread_apply_command (char *tidlist
, int from_tty
)
552 struct cleanup
*old_chain
;
553 struct cleanup
*saved_cmd_cleanup_chain
;
556 if (tidlist
== NULL
|| *tidlist
== '\000')
557 error (_("Please specify a thread ID list"));
559 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
562 error (_("Please specify a command following the thread ID list"));
564 old_chain
= make_cleanup_restore_current_thread (inferior_ptid
);
566 /* Save a copy of the command in case it is clobbered by
568 saved_cmd
= xstrdup (cmd
);
569 saved_cmd_cleanup_chain
= make_cleanup (xfree
, (void *) saved_cmd
);
570 while (tidlist
< cmd
)
572 struct thread_info
*tp
;
575 start
= strtol (tidlist
, &p
, 10);
577 error (_("Error parsing %s"), tidlist
);
580 while (*tidlist
== ' ' || *tidlist
== '\t')
583 if (*tidlist
== '-') /* Got a range of IDs? */
585 tidlist
++; /* Skip the - */
586 end
= strtol (tidlist
, &p
, 10);
588 error (_("Error parsing %s"), tidlist
);
591 while (*tidlist
== ' ' || *tidlist
== '\t')
597 for (; start
<= end
; start
++)
599 tp
= find_thread_id (start
);
602 warning (_("Unknown thread %d."), start
);
603 else if (!thread_alive (tp
))
604 warning (_("Thread %d has terminated."), start
);
607 switch_to_thread (tp
->ptid
);
608 printf_filtered (_("\nThread %d (%s):\n"), tp
->num
,
609 target_tid_to_str (inferior_ptid
));
610 execute_command (cmd
, from_tty
);
611 strcpy (cmd
, saved_cmd
); /* Restore exact command used previously */
616 do_cleanups (saved_cmd_cleanup_chain
);
617 do_cleanups (old_chain
);
620 /* Switch to the specified thread. Will dispatch off to thread_apply_command
621 if prefix of arg is `apply'. */
624 thread_command (char *tidstr
, int from_tty
)
628 /* Don't generate an error, just say which thread is current. */
629 if (target_has_stack
)
630 printf_filtered (_("[Current thread is %d (%s)]\n"),
631 pid_to_thread_id (inferior_ptid
),
632 target_tid_to_str (inferior_ptid
));
634 error (_("No stack."));
638 gdb_thread_select (uiout
, tidstr
, NULL
);
642 do_captured_thread_select (struct ui_out
*uiout
, void *tidstr
)
645 struct thread_info
*tp
;
647 num
= value_as_long (parse_and_eval (tidstr
));
649 tp
= find_thread_id (num
);
652 error (_("Thread ID %d not known."), num
);
654 if (!thread_alive (tp
))
655 error (_("Thread ID %d has terminated."), num
);
657 switch_to_thread (tp
->ptid
);
659 ui_out_text (uiout
, "[Switching to thread ");
660 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_ptid
));
661 ui_out_text (uiout
, " (");
662 ui_out_text (uiout
, target_tid_to_str (inferior_ptid
));
663 ui_out_text (uiout
, ")]");
665 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
670 gdb_thread_select (struct ui_out
*uiout
, char *tidstr
, char **error_message
)
672 return catch_exceptions_with_msg (uiout
, do_captured_thread_select
, tidstr
,
673 error_message
, RETURN_MASK_ALL
);
676 /* Commands with a prefix of `thread'. */
677 struct cmd_list_element
*thread_cmd_list
= NULL
;
680 _initialize_thread (void)
682 static struct cmd_list_element
*thread_apply_list
= NULL
;
684 add_info ("threads", info_threads_command
,
685 _("IDs of currently known threads."));
687 add_prefix_cmd ("thread", class_run
, thread_command
, _("\
688 Use this command to switch between threads.\n\
689 The new thread ID must be currently known."),
690 &thread_cmd_list
, "thread ", 1, &cmdlist
);
692 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
693 _("Apply a command to a list of threads."),
694 &thread_apply_list
, "thread apply ", 1, &thread_cmd_list
);
696 add_cmd ("all", class_run
, thread_apply_all_command
,
697 _("Apply a command to all threads."), &thread_apply_list
);
700 add_com_alias ("t", "thread", class_run
, 1);