]> Git Repo - binutils.git/blob - gdb/thread.c
gcc -Wall lint.
[binutils.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   /* Avoid coredumps which would happen if we tried to access a NULL
174      selected_frame.  */
175   if (!target_has_stack) error ("No stack.");
176
177   for (tp = thread_list; tp; tp = tp->next)
178     {
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
181          thread ID.  */
182       if (!STREQ (current_target.to_shortname, "remote")
183           && target_has_execution
184           && kill (tp->pid, 0) == -1)
185         {
186           tp->pid = -1; /* Mark it as dead */
187           continue;
188         }
189
190       if (tp->pid == current_pid)
191         printf_filtered ("* ");
192       else
193         printf_filtered ("  ");
194
195       printf_filtered ("%d %s  ", tp->num, target_pid_to_str (tp->pid));
196
197       thread_switch (tp->pid);
198       print_stack_frame (selected_frame, -1, 0);
199     }
200
201   thread_switch (current_pid);
202   prune_threads ();
203 }
204
205 /* Switch from one thread to another. */
206
207 static void
208 thread_switch (pid)
209      int pid;
210 {
211   if (pid == inferior_pid)
212     return;
213
214   inferior_pid = pid;
215   flush_cached_frames ();
216   registers_changed ();
217   stop_pc = read_pc();
218   select_frame (get_current_frame (), 0);
219 }
220
221 static void
222 restore_current_thread (pid)
223      int pid;
224 {
225   if (pid != inferior_pid)
226     thread_switch (pid);
227 }
228
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:
232
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
236 */
237
238 static void
239 thread_apply_all_command (cmd, from_tty)
240      char *cmd;
241      int from_tty;
242 {
243   struct thread_info *tp;
244   struct cleanup *old_chain;
245
246   if (cmd == NULL || *cmd == '\000')
247     error ("Please specify a command following the thread ID list");
248
249   old_chain = make_cleanup (restore_current_thread, inferior_pid);
250
251   for (tp = thread_list; tp; tp = tp->next)
252     {
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);
257     }
258 }
259
260 static void
261 thread_apply_command (tidlist, from_tty)
262      char *tidlist;
263      int from_tty;
264 {
265   char *cmd;
266   char *p;
267   struct cleanup *old_chain;
268
269   if (tidlist == NULL || *tidlist == '\000')
270     error ("Please specify a thread ID list");
271
272   for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
273
274   if (*cmd == '\000')
275     error ("Please specify a command following the thread ID list");
276
277   old_chain = make_cleanup (restore_current_thread, inferior_pid);
278
279   while (tidlist < cmd)
280     {
281       struct thread_info *tp;
282       int start, end;
283
284       start = strtol (tidlist, &p, 10);
285       if (p == tidlist)
286         error ("Error parsing %s", tidlist);
287       tidlist = p;
288
289       while (*tidlist == ' ' || *tidlist == '\t')
290         tidlist++;
291
292       if (*tidlist == '-')      /* Got a range of IDs? */
293         {
294           tidlist++;    /* Skip the - */
295           end = strtol (tidlist, &p, 10);
296           if (p == tidlist)
297             error ("Error parsing %s", tidlist);
298           tidlist = p;
299
300           while (*tidlist == ' ' || *tidlist == '\t')
301             tidlist++;
302         }
303       else
304         end = start;
305
306       for (; start <= end; start++)
307         {
308           tp = find_thread_id (start);
309
310           if (!tp)
311             {
312               warning ("Unknown thread %d.", start);
313               continue;
314             }
315
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);
320         }
321     }
322 }
323
324 /* Switch to the specified thread.  Will dispatch off to thread_apply_command
325    if prefix of arg is `apply'.  */
326
327 static void
328 thread_command (tidstr, from_tty)
329      char *tidstr;
330      int from_tty;
331 {
332   int num;
333   struct thread_info *tp;
334
335   if (!tidstr)
336     error ("Please specify a thread ID.  Use the \"info threads\" command to\n\
337 see the IDs of currently known threads.");
338
339   num = atoi (tidstr);
340
341   tp = find_thread_id (num);
342
343   if (!tp)
344     error ("Thread ID %d not known.  Use the \"info threads\" command to\n\
345 see the IDs of currently known threads.", num);
346
347   thread_switch (tp->pid);
348
349   printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
350   print_stack_frame (selected_frame, selected_frame_level, 1);
351 }
352
353 void
354 _initialize_thread ()
355 {
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;
359
360   add_info ("threads", info_threads_command,
361             "IDs of currently known threads.");
362
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,
366                   &cmdlist);
367
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);
371
372   add_cmd ("all", class_run, thread_apply_all_command,
373            "Apply a command to all threads.",
374            &thread_apply_list);
375
376   add_com_alias ("t", "thread", class_run, 1);
377 }
This page took 0.043242 seconds and 4 git commands to generate.