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. */
33 #include <sys/types.h>
36 /*#include "lynxos-core.h"*/
40 struct thread_info *next;
41 int pid; /* Actual process id */
42 int num; /* Convenient handle */
45 static struct thread_info *thread_list = NULL;
46 static int highest_thread_num;
48 static void thread_command PARAMS ((char * tidstr, int from_tty));
50 static void prune_threads PARAMS ((void));
52 static void thread_switch PARAMS ((int pid));
54 static struct thread_info * find_thread_id PARAMS ((int num));
59 struct thread_info *tp, *tpnext;
64 for (tp = thread_list; tp; tp = tpnext)
71 highest_thread_num = 0;
78 struct thread_info *tp;
80 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
83 tp->num = ++highest_thread_num;
84 tp->next = thread_list;
88 static struct thread_info *
92 struct thread_info *tp;
94 for (tp = thread_list; tp; tp = tp->next)
102 valid_thread_id (num)
105 struct thread_info *tp;
107 for (tp = thread_list; tp; tp = tp->next)
115 pid_to_thread_id (pid)
118 struct thread_info *tp;
120 for (tp = thread_list; tp; tp = tp->next)
131 struct thread_info *tp;
133 for (tp = thread_list; tp; tp = tp->next)
137 return 0; /* Never heard of 'im */
143 struct thread_info *tp, *tpprev;
147 for (tp = thread_list; tp; tp = tp->next)
151 tpprev->next = tp->next;
161 /* Print information about currently known threads */
164 info_threads_command (arg, from_tty)
168 struct thread_info *tp;
169 int current_pid = inferior_pid;
171 for (tp = thread_list; tp; tp = tp->next)
173 if (target_has_execution
174 && kill (tp->pid, 0) == -1)
176 tp->pid = -1; /* Mark it as dead */
180 if (tp->pid == current_pid)
181 printf_filtered ("* ");
183 printf_filtered (" ");
185 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
187 thread_switch (tp->pid);
188 print_stack_frame (selected_frame, -1, 0);
191 thread_switch (current_pid);
195 /* Switch from one thread to another. */
201 if (pid == inferior_pid)
205 flush_cached_frames ();
206 registers_changed ();
208 set_current_frame (create_new_frame (read_fp (), stop_pc));
209 stop_frame_address = FRAME_FP (get_current_frame ());
210 select_frame (get_current_frame (), 0);
214 restore_current_thread (pid)
217 if (pid != inferior_pid)
221 /* Apply a GDB command to a list of threads. List syntax is a whitespace
222 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
223 of two numbers seperated by a hyphen. Examples:
225 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
226 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
227 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
231 thread_apply_all_command (cmd, from_tty)
235 struct thread_info *tp;
236 struct cleanup *old_chain;
238 if (cmd == NULL || *cmd == '\000')
239 error ("Please specify a command following the thread ID list");
241 old_chain = make_cleanup (restore_current_thread, inferior_pid);
243 for (tp = thread_list; tp; tp = tp->next)
245 thread_switch (tp->pid);
246 printf_filtered ("\nThread %d (%s):\n", tp->num,
247 target_pid_to_str (inferior_pid));
248 execute_command (cmd, from_tty);
253 thread_apply_command (tidlist, from_tty)
259 struct cleanup *old_chain;
261 if (tidlist == NULL || *tidlist == '\000')
262 error ("Please specify a thread ID list");
264 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
267 error ("Please specify a command following the thread ID list");
269 old_chain = make_cleanup (restore_current_thread, inferior_pid);
271 while (tidlist < cmd)
273 struct thread_info *tp;
276 start = strtol (tidlist, &p, 10);
278 error ("Error parsing %s", tidlist);
281 while (*tidlist == ' ' || *tidlist == '\t')
284 if (*tidlist == '-') /* Got a range of IDs? */
286 tidlist++; /* Skip the - */
287 end = strtol (tidlist, &p, 10);
289 error ("Error parsing %s", tidlist);
292 while (*tidlist == ' ' || *tidlist == '\t')
298 for (; start <= end; start++)
300 tp = find_thread_id (start);
304 warning ("Unknown thread %d.", start);
308 thread_switch (tp->pid);
309 printf_filtered ("\nThread %d (%s):\n", tp->num,
310 target_pid_to_str (inferior_pid));
311 execute_command (cmd, from_tty);
316 /* Switch to the specified thread. Will dispatch off to thread_apply_command
317 if prefix of arg is `apply'. */
320 thread_command (tidstr, from_tty)
325 struct thread_info *tp;
329 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
330 see the IDs of currently known threads.");
334 tp = find_thread_id (num);
337 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
338 see the IDs of currently known threads.", num);
340 thread_switch (tp->pid);
342 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
343 print_stack_frame (selected_frame, selected_frame_level, 1);
347 _initialize_thread ()
349 static struct cmd_list_element *thread_cmd_list = NULL;
350 static struct cmd_list_element *thread_apply_list = NULL;
351 extern struct cmd_list_element *cmdlist;
353 add_info ("threads", info_threads_command,
354 "IDs of currently known threads.");
356 add_prefix_cmd ("thread", class_run, thread_command,
357 "Use this command to switch between threads.\n\
358 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
361 add_prefix_cmd ("apply", class_run, thread_apply_command,
362 "Apply a command to a list of threads.",
363 &thread_apply_list, "apply ", 1, &thread_cmd_list);
365 add_cmd ("all", class_run, thread_apply_all_command,
366 "Apply a command to all threads.",
369 add_com_alias ("t", "thread", class_run, 1);