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;
58 static struct thread_info *thread_list = NULL;
59 static int highest_thread_num;
62 thread_command PARAMS ((char * tidstr, int from_tty));
65 prune_threads PARAMS ((void));
68 switch_to_thread PARAMS ((int pid));
70 static struct thread_info *
71 find_thread_id PARAMS ((int num));
74 info_threads_command PARAMS ((char *, int));
77 restore_current_thread PARAMS ((int));
80 thread_apply_all_command PARAMS ((char *, int));
83 thread_apply_command PARAMS ((char *, int));
86 thread_alive PARAMS ((struct thread_info *));
91 struct thread_info *tp, *tpnext;
96 for (tp = thread_list; tp; tp = tpnext)
103 highest_thread_num = 0;
110 struct thread_info *tp;
112 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
115 tp->num = ++highest_thread_num;
117 tp->prev_func_start = 0;
118 tp->prev_func_name = NULL;
119 tp->step_range_start = 0;
120 tp->step_range_end = 0;
121 tp->step_frame_address =0;
122 tp->step_resume_breakpoint = 0;
123 tp->through_sigtramp_breakpoint = 0;
124 tp->handling_longjmp = 0;
125 tp->trap_expected = 0;
126 tp->another_trap = 0;
127 tp->next = thread_list;
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;
156 static struct thread_info *
160 struct thread_info *tp;
162 for (tp = thread_list; tp; tp = tp->next)
170 valid_thread_id (num)
173 struct thread_info *tp;
175 for (tp = thread_list; tp; tp = tp->next)
183 pid_to_thread_id (pid)
186 struct thread_info *tp;
188 for (tp = thread_list; tp; tp = tp->next)
196 thread_id_to_pid (num)
199 struct thread_info *thread = find_thread_id (num);
210 struct thread_info *tp;
212 for (tp = thread_list; tp; tp = tp->next)
216 return 0; /* Never heard of 'im */
219 /* Load infrun state for the thread PID. */
221 void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
222 trap_expected, step_resume_breakpoint,
223 through_sigtramp_breakpoint, step_range_start,
224 step_range_end, step_frame_address,
225 handling_longjmp, another_trap)
228 CORE_ADDR *prev_func_start;
229 char **prev_func_name;
231 struct breakpoint **step_resume_breakpoint;
232 struct breakpoint **through_sigtramp_breakpoint;
233 CORE_ADDR *step_range_start;
234 CORE_ADDR *step_range_end;
235 CORE_ADDR *step_frame_address;
236 int *handling_longjmp;
239 struct thread_info *tp;
241 /* If we can't find the thread, then we're debugging a single threaded
242 process. No need to do anything in that case. */
243 tp = find_thread_id (pid_to_thread_id (pid));
247 *prev_pc = tp->prev_pc;
248 *prev_func_start = tp->prev_func_start;
249 *prev_func_name = tp->prev_func_name;
250 *step_resume_breakpoint = tp->step_resume_breakpoint;
251 *step_range_start = tp->step_range_start;
252 *step_range_end = tp->step_range_end;
253 *step_frame_address = tp->step_frame_address;
254 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
255 *handling_longjmp = tp->handling_longjmp;
256 *trap_expected = tp->trap_expected;
257 *another_trap = tp->another_trap;
260 /* Save infrun state for the thread PID. */
262 void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
263 trap_expected, step_resume_breakpoint,
264 through_sigtramp_breakpoint, step_range_start,
265 step_range_end, step_frame_address,
266 handling_longjmp, another_trap)
269 CORE_ADDR prev_func_start;
270 char *prev_func_name;
272 struct breakpoint *step_resume_breakpoint;
273 struct breakpoint *through_sigtramp_breakpoint;
274 CORE_ADDR step_range_start;
275 CORE_ADDR step_range_end;
276 CORE_ADDR step_frame_address;
277 int handling_longjmp;
280 struct thread_info *tp;
282 /* If we can't find the thread, then we're debugging a single-threaded
283 process. Nothing to do in that case. */
284 tp = find_thread_id (pid_to_thread_id (pid));
288 tp->prev_pc = prev_pc;
289 tp->prev_func_start = prev_func_start;
290 tp->prev_func_name = prev_func_name;
291 tp->step_resume_breakpoint = step_resume_breakpoint;
292 tp->step_range_start = step_range_start;
293 tp->step_range_end = step_range_end;
294 tp->step_frame_address = step_frame_address;
295 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
296 tp->handling_longjmp = handling_longjmp;
297 tp->trap_expected = trap_expected;
298 tp->another_trap = another_trap;
301 /* Return true if TP is an active thread. */
304 struct thread_info *tp;
308 if (! target_thread_alive (tp->pid))
310 tp->pid = -1; /* Mark it as dead */
319 struct thread_info *tp, *tpprev, *next;
322 for (tp = thread_list; tp; tp = next)
325 if (!thread_alive (tp))
338 /* Print information about currently known threads */
341 info_threads_command (arg, from_tty)
345 struct thread_info *tp;
346 int current_pid = inferior_pid;
348 /* Avoid coredumps which would happen if we tried to access a NULL
350 if (!target_has_stack) error ("No stack.");
353 #if defined(FIND_NEW_THREADS)
357 for (tp = thread_list; tp; tp = tp->next)
359 if (tp->pid == current_pid)
360 printf_filtered ("* ");
362 printf_filtered (" ");
364 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
366 switch_to_thread (tp->pid);
368 print_stack_frame (selected_frame, -1, 0);
370 printf_filtered ("[No stack.]\n");
373 switch_to_thread (current_pid);
376 /* Switch from one thread to another. */
379 switch_to_thread (pid)
382 if (pid == inferior_pid)
386 flush_cached_frames ();
387 registers_changed ();
389 select_frame (get_current_frame (), 0);
393 restore_current_thread (pid)
396 if (pid != inferior_pid)
397 switch_to_thread (pid);
400 /* Apply a GDB command to a list of threads. List syntax is a whitespace
401 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
402 of two numbers seperated by a hyphen. Examples:
404 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
405 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
406 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
410 thread_apply_all_command (cmd, from_tty)
414 struct thread_info *tp;
415 struct cleanup *old_chain;
417 if (cmd == NULL || *cmd == '\000')
418 error ("Please specify a command following the thread ID list");
420 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
421 (void *) inferior_pid);
423 for (tp = thread_list; tp; tp = tp->next)
424 if (thread_alive (tp))
426 switch_to_thread (tp->pid);
427 printf_filtered ("\nThread %d (%s):\n", tp->num,
428 target_pid_to_str (inferior_pid));
429 execute_command (cmd, from_tty);
434 thread_apply_command (tidlist, from_tty)
440 struct cleanup *old_chain;
442 if (tidlist == NULL || *tidlist == '\000')
443 error ("Please specify a thread ID list");
445 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
448 error ("Please specify a command following the thread ID list");
450 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
451 (void *) inferior_pid);
453 while (tidlist < cmd)
455 struct thread_info *tp;
458 start = strtol (tidlist, &p, 10);
460 error ("Error parsing %s", tidlist);
463 while (*tidlist == ' ' || *tidlist == '\t')
466 if (*tidlist == '-') /* Got a range of IDs? */
468 tidlist++; /* Skip the - */
469 end = strtol (tidlist, &p, 10);
471 error ("Error parsing %s", tidlist);
474 while (*tidlist == ' ' || *tidlist == '\t')
480 for (; start <= end; start++)
482 tp = find_thread_id (start);
485 warning ("Unknown thread %d.", start);
486 else if (!thread_alive (tp))
487 warning ("Thread %d has terminated.", start);
490 switch_to_thread (tp->pid);
491 printf_filtered ("\nThread %d (%s):\n", tp->num,
492 target_pid_to_str (inferior_pid));
493 execute_command (cmd, from_tty);
499 /* Switch to the specified thread. Will dispatch off to thread_apply_command
500 if prefix of arg is `apply'. */
503 thread_command (tidstr, from_tty)
508 struct thread_info *tp;
511 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
512 see the IDs of currently known threads.");
516 tp = find_thread_id (num);
519 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
520 see the IDs of currently known threads.", num);
522 if (!thread_alive (tp))
523 error ("Thread ID %d has terminated.\n", num);
525 switch_to_thread (tp->pid);
528 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
529 print_stack_frame (selected_frame, selected_frame_level, 1);
532 /* Commands with a prefix of `thread'. */
533 struct cmd_list_element *thread_cmd_list = NULL;
536 _initialize_thread ()
538 static struct cmd_list_element *thread_apply_list = NULL;
539 extern struct cmd_list_element *cmdlist;
541 add_info ("threads", info_threads_command,
542 "IDs of currently known threads.");
544 add_prefix_cmd ("thread", class_run, thread_command,
545 "Use this command to switch between threads.\n\
546 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
549 add_prefix_cmd ("apply", class_run, thread_apply_command,
550 "Apply a command to a list of threads.",
551 &thread_apply_list, "apply ", 1, &thread_cmd_list);
553 add_cmd ("all", class_run, thread_apply_all_command,
554 "Apply a command to all threads.",
557 add_com_alias ("t", "thread", class_run, 1);