1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993, 1998
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, Boston, MA 02111-1307, USA. */
30 #include "gdbthread.h"
35 #include <sys/types.h>
38 /*#include "lynxos-core.h"*/
42 struct thread_info *next;
43 int pid; /* Actual process id */
44 int num; /* Convenient handle */
45 CORE_ADDR prev_pc; /* State from wait_for_inferior */
46 CORE_ADDR prev_func_start;
48 struct breakpoint *step_resume_breakpoint;
49 struct breakpoint *through_sigtramp_breakpoint;
50 CORE_ADDR step_range_start;
51 CORE_ADDR step_range_end;
52 CORE_ADDR step_frame_address;
57 /* This is set TRUE when a catchpoint of a shared library event
58 triggers. Since we don't wish to leave the inferior in the
59 solib hook when we report the event, we step the inferior
60 back to user code before stopping and reporting the event.
62 int stepping_through_solib_after_catch;
64 /* When stepping_through_solib_after_catch is TRUE, this is a
65 list of the catchpoints that should be reported as triggering
66 when we finally do stop stepping.
68 bpstat stepping_through_solib_catchpoints;
70 /* This is set to TRUE when this thread is in a signal handler
71 trampoline and we're single-stepping through it */
72 int stepping_through_sigtramp;
76 /* Prototypes for exported functions. */
78 void _initialize_thread PARAMS ((void));
80 /* Prototypes for local functions. */
82 #if !defined(FIND_NEW_THREADS)
83 #define FIND_NEW_THREADS target_find_new_threads
86 static struct thread_info *thread_list = NULL;
87 static int highest_thread_num;
89 static struct thread_info * find_thread_id PARAMS ((int num));
91 static void thread_command PARAMS ((char * tidstr, int from_tty));
92 static void thread_apply_all_command PARAMS ((char *, int));
93 static int thread_alive PARAMS ((struct thread_info *));
94 static void info_threads_command PARAMS ((char *, int));
95 static void thread_apply_command PARAMS ((char *, int));
96 static void restore_current_thread PARAMS ((int));
97 static void switch_to_thread PARAMS ((int pid));
98 static void prune_threads PARAMS ((void));
100 /* If the host has threads, the host machine definition may set this
101 macro. But, for remote thread debugging, it gets more complex and
102 setting macros does not bind to the various target dependent
103 methods well. So, we use the vector target_thread_functions */
105 static struct target_thread_vector *target_thread_functions;
107 static int target_find_new_threads PARAMS ((void));
110 target_find_new_threads ()
113 if (target_thread_functions &&
114 target_thread_functions->find_new_threads)
115 retval = (*(target_thread_functions->find_new_threads)) ();
116 return retval; /* no support */
121 target_get_thread_info PARAMS ((gdb_threadref * ref,
122 int selection, /* FIXME: Selection */
123 struct gdb_ext_thread_info * info));
126 target_get_thread_info (ref, selection, info)
130 /* FIXME: Selection */
131 struct gdb_ext_thread_info *info;
135 if (target_thread_functions
136 && target_thread_functions->get_thread_info)
137 retval = (*(target_thread_functions->get_thread_info)) (ref, selection, info);
142 /* It is possible that these bind and unbinf functions implement a
143 stack the interface allows it, but its not implemented that way
148 bind_target_thread_vector (vec)
149 struct target_thread_vector *vec;
151 target_thread_functions = vec;
154 struct target_thread_vector *
155 unbind_target_thread_vector ()
157 struct target_thread_vector *retval;
158 retval = target_thread_functions;
159 target_thread_functions = 0;
161 } /* unbind_target_thread-vector */
166 struct thread_info *tp, *tpnext;
171 for (tp = thread_list; tp; tp = tpnext)
178 highest_thread_num = 0;
185 struct thread_info *tp;
187 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
190 tp->num = ++highest_thread_num;
192 tp->prev_func_start = 0;
193 tp->prev_func_name = NULL;
194 tp->step_range_start = 0;
195 tp->step_range_end = 0;
196 tp->step_frame_address =0;
197 tp->step_resume_breakpoint = 0;
198 tp->through_sigtramp_breakpoint = 0;
199 tp->handling_longjmp = 0;
200 tp->trap_expected = 0;
201 tp->another_trap = 0;
202 tp->stepping_through_solib_after_catch = 0;
203 tp->stepping_through_solib_catchpoints = NULL;
204 tp->stepping_through_sigtramp = 0;
205 tp->next = thread_list;
213 struct thread_info *tp, *tpprev;
217 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
225 tpprev->next = tp->next;
227 thread_list = tp->next;
234 static struct thread_info *
238 struct thread_info *tp;
240 for (tp = thread_list; tp; tp = tp->next)
248 valid_thread_id (num)
251 struct thread_info *tp;
253 for (tp = thread_list; tp; tp = tp->next)
261 pid_to_thread_id (pid)
264 struct thread_info *tp;
266 for (tp = thread_list; tp; tp = tp->next)
274 thread_id_to_pid (num)
277 struct thread_info *thread = find_thread_id (num);
288 struct thread_info *tp;
290 for (tp = thread_list; tp; tp = tp->next)
294 return 0; /* Never heard of 'im */
297 /* Load infrun state for the thread PID. */
299 void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
300 trap_expected, step_resume_breakpoint,
301 through_sigtramp_breakpoint, step_range_start,
302 step_range_end, step_frame_address,
303 handling_longjmp, another_trap,
304 stepping_through_solib_after_catch,
305 stepping_through_solib_catchpoints,
306 stepping_through_sigtramp)
309 CORE_ADDR *prev_func_start;
310 char **prev_func_name;
312 struct breakpoint **step_resume_breakpoint;
313 struct breakpoint **through_sigtramp_breakpoint;
314 CORE_ADDR *step_range_start;
315 CORE_ADDR *step_range_end;
316 CORE_ADDR *step_frame_address;
317 int *handling_longjmp;
319 int * stepping_through_solib_after_catch;
320 bpstat * stepping_through_solib_catchpoints;
321 int * stepping_through_sigtramp;
323 struct thread_info *tp;
325 /* If we can't find the thread, then we're debugging a single threaded
326 process. No need to do anything in that case. */
327 tp = find_thread_id (pid_to_thread_id (pid));
331 *prev_pc = tp->prev_pc;
332 *prev_func_start = tp->prev_func_start;
333 *prev_func_name = tp->prev_func_name;
334 *step_resume_breakpoint = tp->step_resume_breakpoint;
335 *step_range_start = tp->step_range_start;
336 *step_range_end = tp->step_range_end;
337 *step_frame_address = tp->step_frame_address;
338 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
339 *handling_longjmp = tp->handling_longjmp;
340 *trap_expected = tp->trap_expected;
341 *another_trap = tp->another_trap;
342 *stepping_through_solib_after_catch = tp->stepping_through_solib_after_catch;
343 *stepping_through_solib_catchpoints = tp->stepping_through_solib_catchpoints;
344 *stepping_through_sigtramp = tp->stepping_through_sigtramp;
347 /* Save infrun state for the thread PID. */
349 void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
350 trap_expected, step_resume_breakpoint,
351 through_sigtramp_breakpoint, step_range_start,
352 step_range_end, step_frame_address,
353 handling_longjmp, another_trap,
354 stepping_through_solib_after_catch,
355 stepping_through_solib_catchpoints,
356 stepping_through_sigtramp)
359 CORE_ADDR prev_func_start;
360 char *prev_func_name;
362 struct breakpoint *step_resume_breakpoint;
363 struct breakpoint *through_sigtramp_breakpoint;
364 CORE_ADDR step_range_start;
365 CORE_ADDR step_range_end;
366 CORE_ADDR step_frame_address;
367 int handling_longjmp;
369 int stepping_through_solib_after_catch;
370 bpstat stepping_through_solib_catchpoints;
371 int stepping_through_sigtramp;
373 struct thread_info *tp;
375 /* If we can't find the thread, then we're debugging a single-threaded
376 process. Nothing to do in that case. */
377 tp = find_thread_id (pid_to_thread_id (pid));
381 tp->prev_pc = prev_pc;
382 tp->prev_func_start = prev_func_start;
383 tp->prev_func_name = prev_func_name;
384 tp->step_resume_breakpoint = step_resume_breakpoint;
385 tp->step_range_start = step_range_start;
386 tp->step_range_end = step_range_end;
387 tp->step_frame_address = step_frame_address;
388 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
389 tp->handling_longjmp = handling_longjmp;
390 tp->trap_expected = trap_expected;
391 tp->another_trap = another_trap;
392 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
393 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
394 tp->stepping_through_sigtramp = stepping_through_sigtramp;
397 /* Return true if TP is an active thread. */
400 struct thread_info *tp;
404 if (! target_thread_alive (tp->pid))
406 tp->pid = -1; /* Mark it as dead */
415 struct thread_info *tp, *tpprev, *next;
418 for (tp = thread_list; tp; tp = next)
421 if (!thread_alive (tp))
434 /* Print information about currently known threads
436 * Note: this has the drawback that it _really_ switches
437 * threads, which frees the frame cache. A no-side
438 * effects info-threads command would be nicer.
442 info_threads_command (arg, from_tty)
446 struct thread_info *tp;
448 struct frame_info *cur_frame;
449 int saved_frame_level = selected_frame_level;
452 /* Avoid coredumps which would happen if we tried to access a NULL
454 if (!target_has_stack) error ("No stack.");
457 #if defined(FIND_NEW_THREADS)
460 current_pid = inferior_pid;
461 for (tp = thread_list; tp; tp = tp->next)
463 if (tp->pid == current_pid)
464 printf_filtered ("* ");
466 printf_filtered (" ");
469 printf_filtered ("%d %s ", tp->num, target_tid_to_str (tp->pid));
471 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
473 switch_to_thread (tp->pid);
475 print_only_stack_frame (selected_frame, -1, 0);
477 printf_filtered ("[No stack.]\n");
480 switch_to_thread (current_pid);
482 /* Code below copied from "up_silently_base" in "stack.c".
483 * It restores the frame set by the user before the "info threads"
484 * command. We have finished the info-threads display by switching
485 * back to the current thread. That switch has put us at the top
486 * of the stack (leaf frame).
488 counter = saved_frame_level;
489 cur_frame = find_relative_frame(selected_frame, &counter);
492 /* Ooops, can't restore, tell user where we are. */
493 warning ("Couldn't restore frame in current thread, at frame 0");
494 print_stack_frame (selected_frame, -1, 0);
498 select_frame(cur_frame, saved_frame_level);
501 /* re-show current frame. */
502 show_stack_frame(cur_frame);
505 /* Switch from one thread to another. */
508 switch_to_thread (pid)
511 if (pid == inferior_pid)
515 flush_cached_frames ();
516 registers_changed ();
518 select_frame (get_current_frame (), 0);
522 restore_current_thread (pid)
525 if (pid != inferior_pid)
527 switch_to_thread (pid);
528 print_stack_frame( get_current_frame(), 0, -1);
532 /* Apply a GDB command to a list of threads. List syntax is a whitespace
533 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
534 of two numbers seperated by a hyphen. Examples:
536 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
537 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
538 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
542 thread_apply_all_command (cmd, from_tty)
546 struct thread_info *tp;
547 struct cleanup *old_chain;
549 if (cmd == NULL || *cmd == '\000')
550 error ("Please specify a command following the thread ID list");
552 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
553 (void *) inferior_pid);
555 for (tp = thread_list; tp; tp = tp->next)
556 if (thread_alive (tp))
558 switch_to_thread (tp->pid);
560 printf_filtered ("\nThread %d (%s):\n",
562 target_tid_to_str (inferior_pid));
564 printf_filtered ("\nThread %d (%s):\n", tp->num,
565 target_pid_to_str (inferior_pid));
567 execute_command (cmd, from_tty);
572 thread_apply_command (tidlist, from_tty)
578 struct cleanup *old_chain;
580 if (tidlist == NULL || *tidlist == '\000')
581 error ("Please specify a thread ID list");
583 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
586 error ("Please specify a command following the thread ID list");
588 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
589 (void *) inferior_pid);
591 while (tidlist < cmd)
593 struct thread_info *tp;
596 start = strtol (tidlist, &p, 10);
598 error ("Error parsing %s", tidlist);
601 while (*tidlist == ' ' || *tidlist == '\t')
604 if (*tidlist == '-') /* Got a range of IDs? */
606 tidlist++; /* Skip the - */
607 end = strtol (tidlist, &p, 10);
609 error ("Error parsing %s", tidlist);
612 while (*tidlist == ' ' || *tidlist == '\t')
618 for (; start <= end; start++)
620 tp = find_thread_id (start);
623 warning ("Unknown thread %d.", start);
624 else if (!thread_alive (tp))
625 warning ("Thread %d has terminated.", start);
628 switch_to_thread (tp->pid);
630 printf_filtered ("\nThread %d (%s):\n", tp->num,
631 target_tid_to_str (inferior_pid));
633 printf_filtered ("\nThread %d (%s):\n", tp->num,
634 target_pid_to_str (inferior_pid));
636 execute_command (cmd, from_tty);
642 /* Switch to the specified thread. Will dispatch off to thread_apply_command
643 if prefix of arg is `apply'. */
646 thread_command (tidstr, from_tty)
651 struct thread_info *tp;
655 /* Don't generate an error, just say which thread is current. */
656 if (target_has_stack)
657 printf_filtered ("[Current thread is %d (%s)]\n",
658 pid_to_thread_id(inferior_pid),
659 #if defined(HPUXHPPA)
660 target_tid_to_str(inferior_pid)
662 target_pid_to_str(inferior_pid)
671 tp = find_thread_id (num);
674 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
675 see the IDs of currently known threads.", num);
677 if (!thread_alive (tp))
678 error ("Thread ID %d has terminated.\n", num);
680 switch_to_thread (tp->pid);
685 printf_filtered ("[Switching to thread %d (%s)]\n",
686 pid_to_thread_id (inferior_pid),
687 #if defined(HPUXHPPA)
688 target_tid_to_str (inferior_pid)
690 target_pid_to_str (inferior_pid)
693 print_stack_frame (selected_frame, selected_frame_level, 1);
696 /* Commands with a prefix of `thread'. */
697 struct cmd_list_element *thread_cmd_list = NULL;
700 _initialize_thread ()
702 static struct cmd_list_element *thread_apply_list = NULL;
704 add_info ("threads", info_threads_command,
705 "IDs of currently known threads.");
707 add_prefix_cmd ("thread", class_run, thread_command,
708 "Use this command to switch between threads.\n\
709 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
712 add_prefix_cmd ("apply", class_run, thread_apply_command,
713 "Apply a command to a list of threads.",
714 &thread_apply_list, "apply ", 1, &thread_cmd_list);
716 add_cmd ("all", class_run, thread_apply_all_command,
717 "Apply a command to all threads.",
721 add_com_alias ("t", "thread", class_run, 1);