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 /* Avoid coredumps which would happen if we tried to access a NULL
175 if (!target_has_stack) error ("No stack.");
177 for (tp = thread_list; tp; tp = tp->next)
179 /* FIXME: need to figure out a way to do this for remote too,
180 or else the print_stack_frame below will fail with a bogus
182 if (!STREQ (current_target.to_shortname, "remote")
183 && target_has_execution
184 && kill (tp->pid, 0) == -1)
186 tp->pid = -1; /* Mark it as dead */
190 if (tp->pid == current_pid)
191 printf_filtered ("* ");
193 printf_filtered (" ");
195 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
197 thread_switch (tp->pid);
198 print_stack_frame (selected_frame, -1, 0);
201 thread_switch (current_pid);
205 /* Switch from one thread to another. */
211 if (pid == inferior_pid)
215 flush_cached_frames ();
216 registers_changed ();
218 select_frame (get_current_frame (), 0);
222 restore_current_thread (pid)
225 if (pid != inferior_pid)
229 /* Apply a GDB command to a list of threads. List syntax is a whitespace
230 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
231 of two numbers seperated by a hyphen. Examples:
233 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
234 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
235 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
239 thread_apply_all_command (cmd, from_tty)
243 struct thread_info *tp;
244 struct cleanup *old_chain;
246 if (cmd == NULL || *cmd == '\000')
247 error ("Please specify a command following the thread ID list");
249 old_chain = make_cleanup (restore_current_thread, inferior_pid);
251 for (tp = thread_list; tp; tp = tp->next)
253 thread_switch (tp->pid);
254 printf_filtered ("\nThread %d (%s):\n", tp->num,
255 target_pid_to_str (inferior_pid));
256 execute_command (cmd, from_tty);
261 thread_apply_command (tidlist, from_tty)
267 struct cleanup *old_chain;
269 if (tidlist == NULL || *tidlist == '\000')
270 error ("Please specify a thread ID list");
272 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
275 error ("Please specify a command following the thread ID list");
277 old_chain = make_cleanup (restore_current_thread, inferior_pid);
279 while (tidlist < cmd)
281 struct thread_info *tp;
284 start = strtol (tidlist, &p, 10);
286 error ("Error parsing %s", tidlist);
289 while (*tidlist == ' ' || *tidlist == '\t')
292 if (*tidlist == '-') /* Got a range of IDs? */
294 tidlist++; /* Skip the - */
295 end = strtol (tidlist, &p, 10);
297 error ("Error parsing %s", tidlist);
300 while (*tidlist == ' ' || *tidlist == '\t')
306 for (; start <= end; start++)
308 tp = find_thread_id (start);
312 warning ("Unknown thread %d.", start);
316 thread_switch (tp->pid);
317 printf_filtered ("\nThread %d (%s):\n", tp->num,
318 target_pid_to_str (inferior_pid));
319 execute_command (cmd, from_tty);
324 /* Switch to the specified thread. Will dispatch off to thread_apply_command
325 if prefix of arg is `apply'. */
328 thread_command (tidstr, from_tty)
333 struct thread_info *tp;
336 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
337 see the IDs of currently known threads.");
341 tp = find_thread_id (num);
344 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
345 see the IDs of currently known threads.", num);
347 thread_switch (tp->pid);
349 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
350 print_stack_frame (selected_frame, selected_frame_level, 1);
354 _initialize_thread ()
356 static struct cmd_list_element *thread_cmd_list = NULL;
357 static struct cmd_list_element *thread_apply_list = NULL;
358 extern struct cmd_list_element *cmdlist;
360 add_info ("threads", info_threads_command,
361 "IDs of currently known threads.");
363 add_prefix_cmd ("thread", class_run, thread_command,
364 "Use this command to switch between threads.\n\
365 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
368 add_prefix_cmd ("apply", class_run, thread_apply_command,
369 "Apply a command to a list of threads.",
370 &thread_apply_list, "apply ", 1, &thread_cmd_list);
372 add_cmd ("all", class_run, thread_apply_all_command,
373 "Apply a command to all threads.",
376 add_com_alias ("t", "thread", class_run, 1);