1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993, 1998, 1999, 2000
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
31 #include "gdbthread.h"
36 #include <sys/types.h>
42 /*#include "lynxos-core.h" */
44 /* Definition of struct thread_info exported to gdbthread.h */
46 /* Prototypes for exported functions. */
48 void _initialize_thread (void);
50 /* Prototypes for local functions. */
52 static struct thread_info
*thread_list
= NULL
;
53 static int highest_thread_num
;
55 static struct thread_info
*find_thread_id (int num
);
57 static void thread_command (char *tidstr
, int from_tty
);
58 static void thread_apply_all_command (char *, int);
59 static int thread_alive (struct thread_info
*);
60 static void info_threads_command (char *, int);
61 static void thread_apply_command (char *, int);
62 static void restore_current_thread (int);
63 static void switch_to_thread (int pid
);
64 static void prune_threads (void);
67 free_thread (struct thread_info
*tp
)
69 /* NOTE: this will take care of any left-over step_resume breakpoints,
70 but not any user-specified thread-specific breakpoints. */
71 if (tp
->step_resume_breakpoint
)
72 delete_breakpoint (tp
->step_resume_breakpoint
);
74 /* FIXME: do I ever need to call the back-end to give it a
75 chance at this private data before deleting the thread? */
83 init_thread_list (void)
85 struct thread_info
*tp
, *tpnext
;
87 highest_thread_num
= 0;
91 for (tp
= thread_list
; tp
; tp
= tpnext
)
100 /* add_thread now returns a pointer to the new thread_info,
101 so that back_ends can initialize their private data. */
106 struct thread_info
*tp
;
108 tp
= (struct thread_info
*) xmalloc (sizeof (struct thread_info
));
111 tp
->num
= ++highest_thread_num
;
113 tp
->prev_func_start
= 0;
114 tp
->prev_func_name
= NULL
;
115 tp
->step_range_start
= 0;
116 tp
->step_range_end
= 0;
117 tp
->step_frame_address
= 0;
118 tp
->step_resume_breakpoint
= 0;
119 tp
->through_sigtramp_breakpoint
= 0;
120 tp
->handling_longjmp
= 0;
121 tp
->trap_expected
= 0;
122 tp
->another_trap
= 0;
123 tp
->stepping_through_solib_after_catch
= 0;
124 tp
->stepping_through_solib_catchpoints
= NULL
;
125 tp
->stepping_through_sigtramp
= 0;
126 tp
->next
= thread_list
;
133 delete_thread (int pid
)
135 struct thread_info
*tp
, *tpprev
;
139 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
147 tpprev
->next
= tp
->next
;
149 thread_list
= tp
->next
;
154 static struct thread_info
*
155 find_thread_id (int num
)
157 struct thread_info
*tp
;
159 for (tp
= thread_list
; tp
; tp
= tp
->next
)
166 /* Find a thread_info by matching 'pid'. */
168 find_thread_pid (int pid
)
170 struct thread_info
*tp
;
172 for (tp
= thread_list
; tp
; tp
= tp
->next
)
180 * Thread iterator function.
182 * Calls a callback function once for each thread, so long as
183 * the callback function returns false. If the callback function
184 * returns true, the iteration will end and the current thread
185 * will be returned. This can be useful for implementing a
186 * search for a thread with arbitrary attributes, or for applying
187 * some operation to every thread.
189 * FIXME: some of the existing functionality, such as
190 * "Thread apply all", might be rewritten using this functionality.
194 iterate_over_threads (callback
, data
)
198 struct thread_info
*tp
;
200 for (tp
= thread_list
; tp
; tp
= tp
->next
)
201 if ((*callback
) (tp
, data
))
208 valid_thread_id (int num
)
210 struct thread_info
*tp
;
212 for (tp
= thread_list
; tp
; tp
= tp
->next
)
220 pid_to_thread_id (int pid
)
222 struct thread_info
*tp
;
224 for (tp
= thread_list
; tp
; tp
= tp
->next
)
232 thread_id_to_pid (int num
)
234 struct thread_info
*thread
= find_thread_id (num
);
242 in_thread_list (int pid
)
244 struct thread_info
*tp
;
246 for (tp
= thread_list
; tp
; tp
= tp
->next
)
250 return 0; /* Never heard of 'im */
253 /* Print a list of thread ids currently known, and the total number of
254 threads. To be used from within catch_errors. */
256 do_captured_list_thread_ids (void *arg
)
258 struct thread_info
*tp
;
261 ui_out_list_begin (uiout
, "thread-ids");
263 for (tp
= thread_list
; tp
; tp
= tp
->next
)
266 ui_out_field_int (uiout
, "thread-id", tp
->num
);
269 ui_out_list_end (uiout
);
270 ui_out_field_int (uiout
, "number-of-threads", num
);
274 /* Official gdblib interface function to get a list of thread ids and
277 gdb_list_thread_ids (/* output object */)
279 return catch_errors (do_captured_list_thread_ids
, NULL
,
280 NULL
, RETURN_MASK_ALL
);
284 /* Load infrun state for the thread PID. */
287 load_infrun_state (int pid
, CORE_ADDR
*prev_pc
, CORE_ADDR
*prev_func_start
,
288 char **prev_func_name
, int *trap_expected
,
289 struct breakpoint
**step_resume_breakpoint
,
290 struct breakpoint
**through_sigtramp_breakpoint
,
291 CORE_ADDR
*step_range_start
, CORE_ADDR
*step_range_end
,
292 CORE_ADDR
*step_frame_address
, int *handling_longjmp
,
293 int *another_trap
, int *stepping_through_solib_after_catch
,
294 bpstat
*stepping_through_solib_catchpoints
,
295 int *stepping_through_sigtramp
)
297 struct thread_info
*tp
;
299 /* If we can't find the thread, then we're debugging a single threaded
300 process. No need to do anything in that case. */
301 tp
= find_thread_id (pid_to_thread_id (pid
));
305 *prev_pc
= tp
->prev_pc
;
306 *prev_func_start
= tp
->prev_func_start
;
307 *prev_func_name
= tp
->prev_func_name
;
308 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
309 *step_range_start
= tp
->step_range_start
;
310 *step_range_end
= tp
->step_range_end
;
311 *step_frame_address
= tp
->step_frame_address
;
312 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
313 *handling_longjmp
= tp
->handling_longjmp
;
314 *trap_expected
= tp
->trap_expected
;
315 *another_trap
= tp
->another_trap
;
316 *stepping_through_solib_after_catch
= tp
->stepping_through_solib_after_catch
;
317 *stepping_through_solib_catchpoints
= tp
->stepping_through_solib_catchpoints
;
318 *stepping_through_sigtramp
= tp
->stepping_through_sigtramp
;
321 /* Save infrun state for the thread PID. */
324 save_infrun_state (int pid
, CORE_ADDR prev_pc
, CORE_ADDR prev_func_start
,
325 char *prev_func_name
, int trap_expected
,
326 struct breakpoint
*step_resume_breakpoint
,
327 struct breakpoint
*through_sigtramp_breakpoint
,
328 CORE_ADDR step_range_start
, CORE_ADDR step_range_end
,
329 CORE_ADDR step_frame_address
, int handling_longjmp
,
330 int another_trap
, int stepping_through_solib_after_catch
,
331 bpstat stepping_through_solib_catchpoints
,
332 int stepping_through_sigtramp
)
334 struct thread_info
*tp
;
336 /* If we can't find the thread, then we're debugging a single-threaded
337 process. Nothing to do in that case. */
338 tp
= find_thread_id (pid_to_thread_id (pid
));
342 tp
->prev_pc
= prev_pc
;
343 tp
->prev_func_start
= prev_func_start
;
344 tp
->prev_func_name
= prev_func_name
;
345 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
346 tp
->step_range_start
= step_range_start
;
347 tp
->step_range_end
= step_range_end
;
348 tp
->step_frame_address
= step_frame_address
;
349 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
350 tp
->handling_longjmp
= handling_longjmp
;
351 tp
->trap_expected
= trap_expected
;
352 tp
->another_trap
= another_trap
;
353 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
354 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
355 tp
->stepping_through_sigtramp
= stepping_through_sigtramp
;
358 /* Return true if TP is an active thread. */
360 thread_alive (struct thread_info
*tp
)
364 if (!target_thread_alive (tp
->pid
))
366 tp
->pid
= -1; /* Mark it as dead */
375 struct thread_info
*tp
, *next
;
377 for (tp
= thread_list
; tp
; tp
= next
)
380 if (!thread_alive (tp
))
381 delete_thread (tp
->pid
);
385 /* Print information about currently known threads
387 * Note: this has the drawback that it _really_ switches
388 * threads, which frees the frame cache. A no-side
389 * effects info-threads command would be nicer.
393 info_threads_command (char *arg
, int from_tty
)
395 struct thread_info
*tp
;
397 struct frame_info
*cur_frame
;
398 int saved_frame_level
= selected_frame_level
;
402 /* Avoid coredumps which would happen if we tried to access a NULL
404 if (!target_has_stack
)
408 target_find_new_threads ();
409 current_pid
= inferior_pid
;
410 for (tp
= thread_list
; tp
; tp
= tp
->next
)
412 if (tp
->pid
== current_pid
)
413 printf_filtered ("* ");
415 printf_filtered (" ");
418 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->pid
));
420 printf_filtered ("%d %s", tp
->num
, target_pid_to_str (tp
->pid
));
423 extra_info
= target_extra_thread_info (tp
);
425 printf_filtered (" (%s)", extra_info
);
428 switch_to_thread (tp
->pid
);
430 print_only_stack_frame (selected_frame
, -1, 0);
432 printf_filtered ("[No stack.]\n");
435 switch_to_thread (current_pid
);
437 /* Code below copied from "up_silently_base" in "stack.c".
438 * It restores the frame set by the user before the "info threads"
439 * command. We have finished the info-threads display by switching
440 * back to the current thread. That switch has put us at the top
441 * of the stack (leaf frame).
443 counter
= saved_frame_level
;
444 cur_frame
= find_relative_frame (selected_frame
, &counter
);
447 /* Ooops, can't restore, tell user where we are. */
448 warning ("Couldn't restore frame in current thread, at frame 0");
449 print_stack_frame (selected_frame
, -1, 0);
453 select_frame (cur_frame
, saved_frame_level
);
456 /* re-show current frame. */
457 show_stack_frame (cur_frame
);
460 /* Switch from one thread to another. */
463 switch_to_thread (int pid
)
465 if (pid
== inferior_pid
)
469 flush_cached_frames ();
470 registers_changed ();
471 stop_pc
= read_pc ();
472 select_frame (get_current_frame (), 0);
476 restore_current_thread (int pid
)
478 if (pid
!= inferior_pid
)
480 switch_to_thread (pid
);
481 print_stack_frame (get_current_frame (), 0, -1);
485 struct current_thread_cleanup
491 do_restore_current_thread_cleanup (void *arg
)
493 struct current_thread_cleanup
*old
= arg
;
494 restore_current_thread (old
->inferior_pid
);
498 static struct cleanup
*
499 make_cleanup_restore_current_thread (int inferior_pid
)
501 struct current_thread_cleanup
*old
502 = xmalloc (sizeof (struct current_thread_cleanup
));
503 old
->inferior_pid
= inferior_pid
;
504 return make_cleanup (do_restore_current_thread_cleanup
, old
);
507 /* Apply a GDB command to a list of threads. List syntax is a whitespace
508 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
509 of two numbers seperated by a hyphen. Examples:
511 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
512 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
513 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
517 thread_apply_all_command (char *cmd
, int from_tty
)
519 struct thread_info
*tp
;
520 struct cleanup
*old_chain
;
522 if (cmd
== NULL
|| *cmd
== '\000')
523 error ("Please specify a command following the thread ID list");
525 old_chain
= make_cleanup_restore_current_thread (inferior_pid
);
527 /* It is safe to update the thread list now, before
528 traversing it for "thread apply all". MVS */
529 target_find_new_threads ();
531 for (tp
= thread_list
; tp
; tp
= tp
->next
)
532 if (thread_alive (tp
))
534 switch_to_thread (tp
->pid
);
536 printf_filtered ("\nThread %d (%s):\n",
538 target_tid_to_str (inferior_pid
));
540 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
541 target_pid_to_str (inferior_pid
));
543 execute_command (cmd
, from_tty
);
546 do_cleanups (old_chain
);
550 thread_apply_command (char *tidlist
, int from_tty
)
554 struct cleanup
*old_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_pid
);
566 while (tidlist
< cmd
)
568 struct thread_info
*tp
;
571 start
= strtol (tidlist
, &p
, 10);
573 error ("Error parsing %s", tidlist
);
576 while (*tidlist
== ' ' || *tidlist
== '\t')
579 if (*tidlist
== '-') /* Got a range of IDs? */
581 tidlist
++; /* Skip the - */
582 end
= strtol (tidlist
, &p
, 10);
584 error ("Error parsing %s", tidlist
);
587 while (*tidlist
== ' ' || *tidlist
== '\t')
593 for (; start
<= end
; start
++)
595 tp
= find_thread_id (start
);
598 warning ("Unknown thread %d.", start
);
599 else if (!thread_alive (tp
))
600 warning ("Thread %d has terminated.", start
);
603 switch_to_thread (tp
->pid
);
605 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
606 target_tid_to_str (inferior_pid
));
608 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
609 target_pid_to_str (inferior_pid
));
611 execute_command (cmd
, from_tty
);
616 do_cleanups (old_chain
);
619 /* Switch to the specified thread. Will dispatch off to thread_apply_command
620 if prefix of arg is `apply'. */
623 thread_command (char *tidstr
, int from_tty
)
627 /* Don't generate an error, just say which thread is current. */
628 if (target_has_stack
)
629 printf_filtered ("[Current thread is %d (%s)]\n",
630 pid_to_thread_id (inferior_pid
),
631 #if defined(HPUXHPPA)
632 target_tid_to_str (inferior_pid
)
634 target_pid_to_str (inferior_pid
)
642 gdb_thread_select (tidstr
);
646 do_captured_thread_select (void *tidstr
)
649 struct thread_info
*tp
;
651 num
= atoi ((char *)tidstr
);
653 tp
= find_thread_id (num
);
657 error ("Thread ID %d not known.", num
);
660 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
661 see the IDs of currently known threads.", num
);
664 if (!thread_alive (tp
))
665 error ("Thread ID %d has terminated.\n", num
);
667 switch_to_thread (tp
->pid
);
670 ui_out_text (uiout
, "[Switching to thread ");
671 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_pid
));
672 ui_out_text (uiout
, " (");
673 #if defined(HPUXHPPA)
674 ui_out_text (uiout
, target_tid_to_str (inferior_pid
));
676 ui_out_text (uiout
, target_pid_to_str (inferior_pid
));
678 ui_out_text (uiout
, ")]");
680 printf_filtered ("[Switching to thread %d (%s)]\n",
681 pid_to_thread_id (inferior_pid
),
682 #if defined(HPUXHPPA)
683 target_tid_to_str (inferior_pid
)
685 target_pid_to_str (inferior_pid
)
690 print_stack_frame (selected_frame
, selected_frame_level
, 1);
695 gdb_thread_select (char *tidstr
)
697 return catch_errors (do_captured_thread_select
, tidstr
,
698 NULL
, RETURN_MASK_ALL
);
701 /* Commands with a prefix of `thread'. */
702 struct cmd_list_element
*thread_cmd_list
= NULL
;
705 _initialize_thread (void)
707 static struct cmd_list_element
*thread_apply_list
= NULL
;
709 add_info ("threads", info_threads_command
,
710 "IDs of currently known threads.");
712 add_prefix_cmd ("thread", class_run
, thread_command
,
713 "Use this command to switch between threads.\n\
714 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
717 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
718 "Apply a command to a list of threads.",
719 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
721 add_cmd ("all", class_run
, thread_apply_all_command
,
722 "Apply a command to all threads.",
726 add_com_alias ("t", "thread", class_run
, 1);