d3ed393b2cec9e15dc99667bb1089290e00f465f
[external/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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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   CORE_ADDR prev_pc;            /* State from wait_for_inferior */
46   CORE_ADDR prev_func_start;
47   char *prev_func_name;
48   struct breakpoint *step_resume_breakpoint;
49   struct breakpoint *through_sigtramp_breakpoint;
50   CORE_ADDR step_range_start;
51   CORE_ADDR step_range_end;
52   CORE_ADDR step_frame_address;
53   int trap_expected;
54   int handling_longjmp;
55   int another_trap;
56 };
57
58 static struct thread_info *thread_list = NULL;
59 static int highest_thread_num;
60
61 static void thread_command PARAMS ((char * tidstr, int from_tty));
62
63 static void prune_threads PARAMS ((void));
64
65 static void thread_switch PARAMS ((int pid));
66
67 static struct thread_info * find_thread_id PARAMS ((int num));
68
69 void
70 init_thread_list ()
71 {
72   struct thread_info *tp, *tpnext;
73
74   if (!thread_list)
75     return;
76
77   for (tp = thread_list; tp; tp = tpnext)
78     {
79       tpnext = tp->next;
80       free (tp);
81     }
82
83   thread_list = NULL;
84   highest_thread_num = 0;
85 }
86
87 void
88 add_thread (pid)
89      int pid;
90 {
91   struct thread_info *tp;
92
93   tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
94
95   tp->pid = pid;
96   tp->num = ++highest_thread_num;
97   tp->prev_pc = 0;
98   tp->prev_func_start = 0;
99   tp->prev_func_name = NULL;
100   tp->step_range_start = 0;
101   tp->step_range_end = 0;
102   tp->step_frame_address =0;
103   tp->step_resume_breakpoint = 0;
104   tp->through_sigtramp_breakpoint = 0;
105   tp->handling_longjmp = 0;
106   tp->trap_expected = 0;
107   tp->another_trap = 0;
108   tp->next = thread_list;
109   thread_list = tp;
110 }
111
112 static struct thread_info *
113 find_thread_id (num)
114     int num;
115 {
116   struct thread_info *tp;
117
118   for (tp = thread_list; tp; tp = tp->next)
119     if (tp->num == num)
120       return tp;
121
122   return NULL;
123 }
124
125 int
126 valid_thread_id (num)
127     int num;
128 {
129   struct thread_info *tp;
130
131   for (tp = thread_list; tp; tp = tp->next)
132     if (tp->num == num)
133       return 1;
134
135   return 0;
136 }
137
138 int
139 pid_to_thread_id (pid)
140     int pid;
141 {
142   struct thread_info *tp;
143
144   for (tp = thread_list; tp; tp = tp->next)
145     if (tp->pid == pid)
146       return tp->num;
147
148   return 0;
149 }
150
151 int
152 in_thread_list (pid)
153     int pid;
154 {
155   struct thread_info *tp;
156
157   for (tp = thread_list; tp; tp = tp->next)
158     if (tp->pid == pid)
159       return 1;
160
161   return 0;                     /* Never heard of 'im */
162 }
163
164 /* Load infrun state for the thread PID.  */
165
166 void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
167                         trap_expected, step_resume_breakpoint,
168                         through_sigtramp_breakpoint, step_range_start,
169                         step_range_end, step_frame_address,
170                         handling_longjmp, another_trap)
171      int pid;
172      CORE_ADDR *prev_pc;
173      CORE_ADDR *prev_func_start;
174      char **prev_func_name;
175      int *trap_expected;
176      struct breakpoint **step_resume_breakpoint;
177      struct breakpoint **through_sigtramp_breakpoint;
178      CORE_ADDR *step_range_start;
179      CORE_ADDR *step_range_end;
180      CORE_ADDR *step_frame_address;
181      int *handling_longjmp;
182      int *another_trap;
183 {
184   struct thread_info *tp;
185
186   /* If we can't find the thread, then we're debugging a single threaded
187      process.  No need to do anything in that case.  */
188   tp = find_thread_id (pid_to_thread_id (pid));
189   if (tp == NULL)
190     return;
191
192   *prev_pc = tp->prev_pc;
193   *prev_func_start = tp->prev_func_start;
194   *prev_func_name = tp->prev_func_name;
195   *step_resume_breakpoint = tp->step_resume_breakpoint;
196   *step_range_start = tp->step_range_start;
197   *step_range_end = tp->step_range_end;
198   *step_frame_address = tp->step_frame_address;
199   *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
200   *handling_longjmp = tp->handling_longjmp;
201   *trap_expected = tp->trap_expected;
202   *another_trap = tp->another_trap;
203 }
204
205 /* Save infrun state for the thread PID.  */
206
207 void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
208                         trap_expected, step_resume_breakpoint,
209                         through_sigtramp_breakpoint, step_range_start,
210                         step_range_end, step_frame_address,
211                         handling_longjmp, another_trap)
212      int pid;
213      CORE_ADDR prev_pc;
214      CORE_ADDR prev_func_start;
215      char *prev_func_name;
216      int trap_expected;
217      struct breakpoint *step_resume_breakpoint;
218      struct breakpoint *through_sigtramp_breakpoint;
219      CORE_ADDR step_range_start;
220      CORE_ADDR step_range_end;
221      CORE_ADDR step_frame_address;
222      int handling_longjmp;
223      int another_trap;
224 {
225   struct thread_info *tp;
226
227   /* If we can't find the thread, then we're debugging a single-threaded
228      process.  Nothing to do in that case.  */
229   tp = find_thread_id (pid_to_thread_id (pid));
230   if (tp == NULL)
231     return;
232
233   tp->prev_pc = prev_pc;
234   tp->prev_func_start = prev_func_start;
235   tp->prev_func_name = prev_func_name;
236   tp->step_resume_breakpoint = step_resume_breakpoint;
237   tp->step_range_start = step_range_start;
238   tp->step_range_end = step_range_end;
239   tp->step_frame_address = step_frame_address;
240   tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
241   tp->handling_longjmp = handling_longjmp;
242   tp->trap_expected = trap_expected;
243   tp->another_trap = another_trap;
244 }
245
246 static void
247 prune_threads ()
248 {
249   struct thread_info *tp, *tpprev;
250
251   tpprev = 0;
252
253   for (tp = thread_list; tp; tp = tp->next)
254     if (tp->pid == -1)
255       {
256         if (tpprev)
257           tpprev->next = tp->next;
258         else
259           thread_list = NULL;
260
261         free (tp);
262       }
263     else
264       tpprev = tp;
265 }
266
267 /* Print information about currently known threads */
268
269 static void
270 info_threads_command (arg, from_tty)
271      char *arg;
272      int from_tty;
273 {
274   struct thread_info *tp;
275   int current_pid = inferior_pid;
276
277   /* Avoid coredumps which would happen if we tried to access a NULL
278      selected_frame.  */
279   if (!target_has_stack) error ("No stack.");
280
281   for (tp = thread_list; tp; tp = tp->next)
282     {
283       if (! target_thread_alive (tp->pid))
284         {
285           tp->pid = -1; /* Mark it as dead */
286           continue;
287         }
288
289       if (tp->pid == current_pid)
290         printf_filtered ("* ");
291       else
292         printf_filtered ("  ");
293
294       printf_filtered ("%d %s  ", tp->num, target_pid_to_str (tp->pid));
295
296       thread_switch (tp->pid);
297       print_stack_frame (selected_frame, -1, 0);
298     }
299
300   thread_switch (current_pid);
301   prune_threads ();
302 }
303
304 /* Switch from one thread to another. */
305
306 static void
307 thread_switch (pid)
308      int pid;
309 {
310   if (pid == inferior_pid)
311     return;
312
313   inferior_pid = pid;
314   flush_cached_frames ();
315   registers_changed ();
316   stop_pc = read_pc();
317   select_frame (get_current_frame (), 0);
318 }
319
320 static void
321 restore_current_thread (pid)
322      int pid;
323 {
324   if (pid != inferior_pid)
325     thread_switch (pid);
326 }
327
328 /* Apply a GDB command to a list of threads.  List syntax is a whitespace
329    seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
330    of two numbers seperated by a hyphen.  Examples:
331
332         thread apply 1 2 7 4 backtrace  Apply backtrace cmd to threads 1,2,7,4
333         thread apply 2-7 9 p foo(1)     Apply p foo(1) cmd to threads 2->7 & 9
334         thread apply all p x/i $pc      Apply x/i $pc cmd to all threads
335 */
336
337 static void
338 thread_apply_all_command (cmd, from_tty)
339      char *cmd;
340      int from_tty;
341 {
342   struct thread_info *tp;
343   struct cleanup *old_chain;
344
345   if (cmd == NULL || *cmd == '\000')
346     error ("Please specify a command following the thread ID list");
347
348   old_chain = make_cleanup (restore_current_thread, inferior_pid);
349
350   for (tp = thread_list; tp; tp = tp->next)
351     {
352       thread_switch (tp->pid);
353       printf_filtered ("\nThread %d (%s):\n", tp->num,
354                        target_pid_to_str (inferior_pid));
355       execute_command (cmd, from_tty);
356     }
357 }
358
359 static void
360 thread_apply_command (tidlist, from_tty)
361      char *tidlist;
362      int from_tty;
363 {
364   char *cmd;
365   char *p;
366   struct cleanup *old_chain;
367
368   if (tidlist == NULL || *tidlist == '\000')
369     error ("Please specify a thread ID list");
370
371   for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
372
373   if (*cmd == '\000')
374     error ("Please specify a command following the thread ID list");
375
376   old_chain = make_cleanup (restore_current_thread, inferior_pid);
377
378   while (tidlist < cmd)
379     {
380       struct thread_info *tp;
381       int start, end;
382
383       start = strtol (tidlist, &p, 10);
384       if (p == tidlist)
385         error ("Error parsing %s", tidlist);
386       tidlist = p;
387
388       while (*tidlist == ' ' || *tidlist == '\t')
389         tidlist++;
390
391       if (*tidlist == '-')      /* Got a range of IDs? */
392         {
393           tidlist++;    /* Skip the - */
394           end = strtol (tidlist, &p, 10);
395           if (p == tidlist)
396             error ("Error parsing %s", tidlist);
397           tidlist = p;
398
399           while (*tidlist == ' ' || *tidlist == '\t')
400             tidlist++;
401         }
402       else
403         end = start;
404
405       for (; start <= end; start++)
406         {
407           tp = find_thread_id (start);
408
409           if (!tp)
410             {
411               warning ("Unknown thread %d.", start);
412               continue;
413             }
414
415           thread_switch (tp->pid);
416           printf_filtered ("\nThread %d (%s):\n", tp->num,
417                            target_pid_to_str (inferior_pid));
418           execute_command (cmd, from_tty);
419         }
420     }
421 }
422
423 /* Switch to the specified thread.  Will dispatch off to thread_apply_command
424    if prefix of arg is `apply'.  */
425
426 static void
427 thread_command (tidstr, from_tty)
428      char *tidstr;
429      int from_tty;
430 {
431   int num;
432   struct thread_info *tp;
433
434   if (!tidstr)
435     error ("Please specify a thread ID.  Use the \"info threads\" command to\n\
436 see the IDs of currently known threads.");
437
438   num = atoi (tidstr);
439
440   tp = find_thread_id (num);
441
442   if (!tp)
443     error ("Thread ID %d not known.  Use the \"info threads\" command to\n\
444 see the IDs of currently known threads.", num);
445
446   thread_switch (tp->pid);
447
448   printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
449   print_stack_frame (selected_frame, selected_frame_level, 1);
450 }
451
452 void
453 _initialize_thread ()
454 {
455   static struct cmd_list_element *thread_cmd_list = NULL;
456   static struct cmd_list_element *thread_apply_list = NULL;
457   extern struct cmd_list_element *cmdlist;
458
459   add_info ("threads", info_threads_command,
460             "IDs of currently known threads.");
461
462   add_prefix_cmd ("thread", class_run, thread_command,
463                   "Use this command to switch between threads.\n\
464 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
465                   &cmdlist);
466
467   add_prefix_cmd ("apply", class_run, thread_apply_command,
468                   "Apply a command to a list of threads.",
469                   &thread_apply_list, "apply ", 1, &thread_cmd_list);
470
471   add_cmd ("all", class_run, thread_apply_all_command,
472            "Apply a command to all threads.",
473            &thread_apply_list);
474
475   add_com_alias ("t", "thread", class_run, 1);
476 }