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