1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 */
47 static struct thread_info *thread_list = NULL;
48 static int highest_thread_num;
50 static void thread_command PARAMS ((char * tidstr, int from_tty));
52 static void prune_threads PARAMS ((void));
54 static void thread_switch PARAMS ((int pid));
56 static struct thread_info * find_thread_id PARAMS ((int num));
61 struct thread_info *tp, *tpnext;
66 for (tp = thread_list; tp; tp = tpnext)
73 highest_thread_num = 0;
80 struct thread_info *tp;
82 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
85 tp->num = ++highest_thread_num;
86 tp->next = thread_list;
90 static struct thread_info *
94 struct thread_info *tp;
96 for (tp = thread_list; tp; tp = tp->next)
104 valid_thread_id (num)
107 struct thread_info *tp;
109 for (tp = thread_list; tp; tp = tp->next)
117 pid_to_thread_id (pid)
120 struct thread_info *tp;
122 for (tp = thread_list; tp; tp = tp->next)
133 struct thread_info *tp;
135 for (tp = thread_list; tp; tp = tp->next)
139 return 0; /* Never heard of 'im */
145 struct thread_info *tp, *tpprev;
149 for (tp = thread_list; tp; tp = tp->next)
153 tpprev->next = tp->next;
163 /* Print information about currently known threads */
166 info_threads_command (arg, from_tty)
170 struct thread_info *tp;
171 int current_pid = inferior_pid;
173 for (tp = thread_list; tp; tp = tp->next)
175 if (target_has_execution
176 && kill (tp->pid, 0) == -1)
178 tp->pid = -1; /* Mark it as dead */
182 if (tp->pid == current_pid)
183 printf_filtered ("* ");
185 printf_filtered (" ");
187 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
189 thread_switch (tp->pid);
190 print_stack_frame (selected_frame, -1, 0);
193 thread_switch (current_pid);
197 /* Switch from one thread to another. */
203 if (pid == inferior_pid)
207 flush_cached_frames ();
208 registers_changed ();
210 set_current_frame (create_new_frame (read_fp (), stop_pc));
211 stop_frame_address = FRAME_FP (get_current_frame ());
212 select_frame (get_current_frame (), 0);
216 restore_current_thread (pid)
219 if (pid != inferior_pid)
223 /* Apply a GDB command to a list of threads. List syntax is a whitespace
224 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
225 of two numbers seperated by a hyphen. Examples:
227 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
228 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
229 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
233 thread_apply_all_command (cmd, from_tty)
237 struct thread_info *tp;
238 struct cleanup *old_chain;
240 if (cmd == NULL || *cmd == '\000')
241 error ("Please specify a command following the thread ID list");
243 old_chain = make_cleanup (restore_current_thread, inferior_pid);
245 for (tp = thread_list; tp; tp = tp->next)
247 thread_switch (tp->pid);
248 printf_filtered ("\nThread %d (%s):\n", tp->num,
249 target_pid_to_str (inferior_pid));
250 execute_command (cmd, from_tty);
255 thread_apply_command (tidlist, from_tty)
261 struct cleanup *old_chain;
263 if (tidlist == NULL || *tidlist == '\000')
264 error ("Please specify a thread ID list");
266 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
269 error ("Please specify a command following the thread ID list");
271 old_chain = make_cleanup (restore_current_thread, inferior_pid);
273 while (tidlist < cmd)
275 struct thread_info *tp;
278 start = strtol (tidlist, &p, 10);
280 error ("Error parsing %s", tidlist);
283 while (*tidlist == ' ' || *tidlist == '\t')
286 if (*tidlist == '-') /* Got a range of IDs? */
288 tidlist++; /* Skip the - */
289 end = strtol (tidlist, &p, 10);
291 error ("Error parsing %s", tidlist);
294 while (*tidlist == ' ' || *tidlist == '\t')
300 for (; start <= end; start++)
302 tp = find_thread_id (start);
306 warning ("Unknown thread %d.", start);
310 thread_switch (tp->pid);
311 printf_filtered ("\nThread %d (%s):\n", tp->num,
312 target_pid_to_str (inferior_pid));
313 execute_command (cmd, from_tty);
318 /* Switch to the specified thread. Will dispatch off to thread_apply_command
319 if prefix of arg is `apply'. */
322 thread_command (tidstr, from_tty)
327 struct thread_info *tp;
330 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
331 see the IDs of currently known threads.");
335 tp = find_thread_id (num);
338 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
339 see the IDs of currently known threads.", num);
341 thread_switch (tp->pid);
343 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
344 print_stack_frame (selected_frame, selected_frame_level, 1);
348 _initialize_thread ()
350 static struct cmd_list_element *thread_cmd_list = NULL;
351 static struct cmd_list_element *thread_apply_list = NULL;
352 extern struct cmd_list_element *cmdlist;
354 add_info ("threads", info_threads_command,
355 "IDs of currently known threads.");
357 add_prefix_cmd ("thread", class_run, thread_command,
358 "Use this command to switch between threads.\n\
359 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
362 add_prefix_cmd ("apply", class_run, thread_apply_command,
363 "Apply a command to a list of threads.",
364 &thread_apply_list, "apply ", 1, &thread_cmd_list);
366 add_cmd ("all", class_run, thread_apply_all_command,
367 "Apply a command to all threads.",
370 add_com_alias ("t", "thread", class_run, 1);