db0bbbf2a72b13b40029add3ed6479d4ec5e1a96
[deliverable/binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993
3
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "environ.h"
28 #include "value.h"
29 #include "target.h"
30 #include "thread.h"
31 #include "command.h"
32 #include "gdbcmd.h"
33
34 #include <ctype.h>
35 #include <sys/types.h>
36 #include <signal.h>
37
38 /*#include "lynxos-core.h"*/
39
40 struct thread_info
41 {
42 struct thread_info *next;
43 int pid; /* Actual process id */
44 int num; /* Convenient handle */
45 };
46
47 static struct thread_info *thread_list = NULL;
48 static int highest_thread_num;
49
50 static void thread_command PARAMS ((char * tidstr, int from_tty));
51
52 static void prune_threads PARAMS ((void));
53
54 static void thread_switch PARAMS ((int pid));
55
56 static struct thread_info * find_thread_id PARAMS ((int num));
57
58 void
59 init_thread_list ()
60 {
61 struct thread_info *tp, *tpnext;
62
63 if (!thread_list)
64 return;
65
66 for (tp = thread_list; tp; tp = tpnext)
67 {
68 tpnext = tp->next;
69 free (tp);
70 }
71
72 thread_list = NULL;
73 highest_thread_num = 0;
74 }
75
76 void
77 add_thread (pid)
78 int pid;
79 {
80 struct thread_info *tp;
81
82 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
83
84 tp->pid = pid;
85 tp->num = ++highest_thread_num;
86 tp->next = thread_list;
87 thread_list = tp;
88 }
89
90 static struct thread_info *
91 find_thread_id (num)
92 int num;
93 {
94 struct thread_info *tp;
95
96 for (tp = thread_list; tp; tp = tp->next)
97 if (tp->num == num)
98 return tp;
99
100 return NULL;
101 }
102
103 int
104 valid_thread_id (num)
105 int num;
106 {
107 struct thread_info *tp;
108
109 for (tp = thread_list; tp; tp = tp->next)
110 if (tp->num == num)
111 return 1;
112
113 return 0;
114 }
115
116 int
117 pid_to_thread_id (pid)
118 int pid;
119 {
120 struct thread_info *tp;
121
122 for (tp = thread_list; tp; tp = tp->next)
123 if (tp->pid == pid)
124 return tp->num;
125
126 return 0;
127 }
128
129 int
130 in_thread_list (pid)
131 int pid;
132 {
133 struct thread_info *tp;
134
135 for (tp = thread_list; tp; tp = tp->next)
136 if (tp->pid == pid)
137 return 1;
138
139 return 0; /* Never heard of 'im */
140 }
141
142 static void
143 prune_threads ()
144 {
145 struct thread_info *tp, *tpprev;
146
147 tpprev = 0;
148
149 for (tp = thread_list; tp; tp = tp->next)
150 if (tp->pid == -1)
151 {
152 if (tpprev)
153 tpprev->next = tp->next;
154 else
155 thread_list = NULL;
156
157 free (tp);
158 }
159 else
160 tpprev = tp;
161 }
162
163 /* Print information about currently known threads */
164
165 static void
166 info_threads_command (arg, from_tty)
167 char *arg;
168 int from_tty;
169 {
170 struct thread_info *tp;
171 int current_pid = inferior_pid;
172
173 for (tp = thread_list; tp; tp = tp->next)
174 {
175 if (target_has_execution
176 && kill (tp->pid, 0) == -1)
177 {
178 tp->pid = -1; /* Mark it as dead */
179 continue;
180 }
181
182 if (tp->pid == current_pid)
183 printf_filtered ("* ");
184 else
185 printf_filtered (" ");
186
187 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
188
189 thread_switch (tp->pid);
190 print_stack_frame (selected_frame, -1, 0);
191 }
192
193 thread_switch (current_pid);
194 prune_threads ();
195 }
196
197 /* Switch from one thread to another. */
198
199 static void
200 thread_switch (pid)
201 int pid;
202 {
203 if (pid == inferior_pid)
204 return;
205
206 inferior_pid = pid;
207 flush_cached_frames ();
208 registers_changed ();
209 stop_pc = read_pc();
210 select_frame (get_current_frame (), 0);
211 }
212
213 static void
214 restore_current_thread (pid)
215 int pid;
216 {
217 if (pid != inferior_pid)
218 thread_switch (pid);
219 }
220
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:
224
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
228 */
229
230 static void
231 thread_apply_all_command (cmd, from_tty)
232 char *cmd;
233 int from_tty;
234 {
235 struct thread_info *tp;
236 struct cleanup *old_chain;
237
238 if (cmd == NULL || *cmd == '\000')
239 error ("Please specify a command following the thread ID list");
240
241 old_chain = make_cleanup (restore_current_thread, inferior_pid);
242
243 for (tp = thread_list; tp; tp = tp->next)
244 {
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);
249 }
250 }
251
252 static void
253 thread_apply_command (tidlist, from_tty)
254 char *tidlist;
255 int from_tty;
256 {
257 char *cmd;
258 char *p;
259 struct cleanup *old_chain;
260
261 if (tidlist == NULL || *tidlist == '\000')
262 error ("Please specify a thread ID list");
263
264 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
265
266 if (*cmd == '\000')
267 error ("Please specify a command following the thread ID list");
268
269 old_chain = make_cleanup (restore_current_thread, inferior_pid);
270
271 while (tidlist < cmd)
272 {
273 struct thread_info *tp;
274 int start, end;
275
276 start = strtol (tidlist, &p, 10);
277 if (p == tidlist)
278 error ("Error parsing %s", tidlist);
279 tidlist = p;
280
281 while (*tidlist == ' ' || *tidlist == '\t')
282 tidlist++;
283
284 if (*tidlist == '-') /* Got a range of IDs? */
285 {
286 tidlist++; /* Skip the - */
287 end = strtol (tidlist, &p, 10);
288 if (p == tidlist)
289 error ("Error parsing %s", tidlist);
290 tidlist = p;
291
292 while (*tidlist == ' ' || *tidlist == '\t')
293 tidlist++;
294 }
295 else
296 end = start;
297
298 for (; start <= end; start++)
299 {
300 tp = find_thread_id (start);
301
302 if (!tp)
303 {
304 warning ("Unknown thread %d.", start);
305 continue;
306 }
307
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);
312 }
313 }
314 }
315
316 /* Switch to the specified thread. Will dispatch off to thread_apply_command
317 if prefix of arg is `apply'. */
318
319 static void
320 thread_command (tidstr, from_tty)
321 char *tidstr;
322 int from_tty;
323 {
324 int num;
325 struct thread_info *tp;
326
327 if (!tidstr)
328 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
329 see the IDs of currently known threads.");
330
331 num = atoi (tidstr);
332
333 tp = find_thread_id (num);
334
335 if (!tp)
336 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
337 see the IDs of currently known threads.", num);
338
339 thread_switch (tp->pid);
340
341 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
342 print_stack_frame (selected_frame, selected_frame_level, 1);
343 }
344
345 void
346 _initialize_thread ()
347 {
348 static struct cmd_list_element *thread_cmd_list = NULL;
349 static struct cmd_list_element *thread_apply_list = NULL;
350 extern struct cmd_list_element *cmdlist;
351
352 add_info ("threads", info_threads_command,
353 "IDs of currently known threads.");
354
355 add_prefix_cmd ("thread", class_run, thread_command,
356 "Use this command to switch between threads.\n\
357 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
358 &cmdlist);
359
360 add_prefix_cmd ("apply", class_run, thread_apply_command,
361 "Apply a command to a list of threads.",
362 &thread_apply_list, "apply ", 1, &thread_cmd_list);
363
364 add_cmd ("all", class_run, thread_apply_all_command,
365 "Apply a command to all threads.",
366 &thread_apply_list);
367
368 add_com_alias ("t", "thread", class_run, 1);
369 }
This page took 0.037231 seconds and 4 git commands to generate.