Minor typos.
[deliverable/binutils-gdb.git] / gdb / thread.c
CommitLineData
da0baf42 1/* Multi-process/thread control for GDB, the GNU debugger.
87feff9d 2 Copyright 1986, 1987, 1988, 1993, 1998
da0baf42
SG
3
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
6
7This file is part of GDB.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
6c9638b4 21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25286543
SG
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"
fdfa3315 30#include "gdbthread.h"
46c28185 31#include "command.h"
89ce0c8f 32#include "gdbcmd.h"
25286543 33
89ce0c8f 34#include <ctype.h>
25286543
SG
35#include <sys/types.h>
36#include <signal.h>
37
38/*#include "lynxos-core.h"*/
39
40struct thread_info
41{
42 struct thread_info *next;
43 int pid; /* Actual process id */
44 int num; /* Convenient handle */
88a294b1
JL
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;
25286543
SG
56};
57
58static struct thread_info *thread_list = NULL;
59static int highest_thread_num;
60
b607efe7
FF
61static void
62thread_command PARAMS ((char * tidstr, int from_tty));
25286543 63
b607efe7
FF
64static void
65prune_threads PARAMS ((void));
25286543 66
b607efe7 67static void
c889a1eb 68switch_to_thread PARAMS ((int pid));
25286543 69
b607efe7
FF
70static struct thread_info *
71find_thread_id PARAMS ((int num));
72
73static void
74info_threads_command PARAMS ((char *, int));
75
76static void
77restore_current_thread PARAMS ((int));
78
79static void
80thread_apply_all_command PARAMS ((char *, int));
81
82static void
83thread_apply_command PARAMS ((char *, int));
de43d7d0 84
87feff9d
JM
85static int
86thread_alive PARAMS ((struct thread_info *));
87
25286543
SG
88void
89init_thread_list ()
90{
91 struct thread_info *tp, *tpnext;
92
93 if (!thread_list)
94 return;
95
96 for (tp = thread_list; tp; tp = tpnext)
97 {
98 tpnext = tp->next;
99 free (tp);
100 }
101
102 thread_list = NULL;
103 highest_thread_num = 0;
104}
105
106void
107add_thread (pid)
108 int pid;
109{
110 struct thread_info *tp;
111
3082244d 112 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
25286543
SG
113
114 tp->pid = pid;
115 tp->num = ++highest_thread_num;
88a294b1
JL
116 tp->prev_pc = 0;
117 tp->prev_func_start = 0;
118 tp->prev_func_name = NULL;
119 tp->step_range_start = 0;
120 tp->step_range_end = 0;
121 tp->step_frame_address =0;
122 tp->step_resume_breakpoint = 0;
123 tp->through_sigtramp_breakpoint = 0;
124 tp->handling_longjmp = 0;
125 tp->trap_expected = 0;
126 tp->another_trap = 0;
25286543
SG
127 tp->next = thread_list;
128 thread_list = tp;
129}
130
3780c337
MS
131void
132delete_thread (pid)
133 int pid;
134{
135 struct thread_info *tp, *tpprev;
136
137 tpprev = NULL;
138
139 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
140 if (tp->pid == pid)
141 break;
142
143 if (!tp)
144 return;
145
146 if (tpprev)
147 tpprev->next = tp->next;
148 else
149 thread_list = tp->next;
150
151 free (tp);
152
153 return;
154}
155
25286543
SG
156static struct thread_info *
157find_thread_id (num)
158 int num;
159{
160 struct thread_info *tp;
161
162 for (tp = thread_list; tp; tp = tp->next)
163 if (tp->num == num)
164 return tp;
165
166 return NULL;
167}
168
de43d7d0
SG
169int
170valid_thread_id (num)
171 int num;
172{
173 struct thread_info *tp;
174
175 for (tp = thread_list; tp; tp = tp->next)
176 if (tp->num == num)
177 return 1;
178
179 return 0;
180}
181
182int
183pid_to_thread_id (pid)
184 int pid;
185{
186 struct thread_info *tp;
187
188 for (tp = thread_list; tp; tp = tp->next)
189 if (tp->pid == pid)
190 return tp->num;
191
192 return 0;
193}
194
c889a1eb
MS
195int
196thread_id_to_pid (num)
197 int num;
198{
199 struct thread_info *thread = find_thread_id (num);
200 if (thread)
201 return thread->pid;
202 else
203 return -1;
204}
205
25286543
SG
206int
207in_thread_list (pid)
208 int pid;
209{
210 struct thread_info *tp;
211
212 for (tp = thread_list; tp; tp = tp->next)
213 if (tp->pid == pid)
214 return 1;
215
216 return 0; /* Never heard of 'im */
217}
218
88a294b1
JL
219/* Load infrun state for the thread PID. */
220
221void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
222 trap_expected, step_resume_breakpoint,
223 through_sigtramp_breakpoint, step_range_start,
224 step_range_end, step_frame_address,
225 handling_longjmp, another_trap)
226 int pid;
227 CORE_ADDR *prev_pc;
228 CORE_ADDR *prev_func_start;
229 char **prev_func_name;
230 int *trap_expected;
231 struct breakpoint **step_resume_breakpoint;
232 struct breakpoint **through_sigtramp_breakpoint;
233 CORE_ADDR *step_range_start;
234 CORE_ADDR *step_range_end;
235 CORE_ADDR *step_frame_address;
236 int *handling_longjmp;
237 int *another_trap;
238{
239 struct thread_info *tp;
240
241 /* If we can't find the thread, then we're debugging a single threaded
242 process. No need to do anything in that case. */
243 tp = find_thread_id (pid_to_thread_id (pid));
244 if (tp == NULL)
245 return;
246
247 *prev_pc = tp->prev_pc;
248 *prev_func_start = tp->prev_func_start;
249 *prev_func_name = tp->prev_func_name;
250 *step_resume_breakpoint = tp->step_resume_breakpoint;
251 *step_range_start = tp->step_range_start;
252 *step_range_end = tp->step_range_end;
253 *step_frame_address = tp->step_frame_address;
254 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
255 *handling_longjmp = tp->handling_longjmp;
256 *trap_expected = tp->trap_expected;
257 *another_trap = tp->another_trap;
258}
259
260/* Save infrun state for the thread PID. */
261
262void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
263 trap_expected, step_resume_breakpoint,
264 through_sigtramp_breakpoint, step_range_start,
265 step_range_end, step_frame_address,
266 handling_longjmp, another_trap)
267 int pid;
268 CORE_ADDR prev_pc;
269 CORE_ADDR prev_func_start;
270 char *prev_func_name;
271 int trap_expected;
272 struct breakpoint *step_resume_breakpoint;
273 struct breakpoint *through_sigtramp_breakpoint;
274 CORE_ADDR step_range_start;
275 CORE_ADDR step_range_end;
276 CORE_ADDR step_frame_address;
277 int handling_longjmp;
278 int another_trap;
279{
280 struct thread_info *tp;
281
282 /* If we can't find the thread, then we're debugging a single-threaded
283 process. Nothing to do in that case. */
284 tp = find_thread_id (pid_to_thread_id (pid));
285 if (tp == NULL)
286 return;
287
288 tp->prev_pc = prev_pc;
289 tp->prev_func_start = prev_func_start;
290 tp->prev_func_name = prev_func_name;
291 tp->step_resume_breakpoint = step_resume_breakpoint;
292 tp->step_range_start = step_range_start;
293 tp->step_range_end = step_range_end;
294 tp->step_frame_address = step_frame_address;
295 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
296 tp->handling_longjmp = handling_longjmp;
297 tp->trap_expected = trap_expected;
298 tp->another_trap = another_trap;
299}
300
2847920a
MS
301/* Return true if TP is an active thread. */
302static int
303thread_alive (tp)
304 struct thread_info *tp;
305{
306 if (tp->pid == -1)
307 return 0;
308 if (! target_thread_alive (tp->pid))
309 {
310 tp->pid = -1; /* Mark it as dead */
311 return 0;
312 }
313 return 1;
314}
315
25286543
SG
316static void
317prune_threads ()
318{
2847920a 319 struct thread_info *tp, *tpprev, *next;
25286543
SG
320
321 tpprev = 0;
2847920a
MS
322 for (tp = thread_list; tp; tp = next)
323 {
324 next = tp->next;
325 if (!thread_alive (tp))
326 {
327 if (tpprev)
328 tpprev->next = next;
329 else
330 thread_list = next;
331 free (tp);
332 }
333 else
334 tpprev = tp;
335 }
25286543
SG
336}
337
338/* Print information about currently known threads */
339
340static void
341info_threads_command (arg, from_tty)
342 char *arg;
343 int from_tty;
344{
345 struct thread_info *tp;
346 int current_pid = inferior_pid;
347
4cc1b3f7
JK
348 /* Avoid coredumps which would happen if we tried to access a NULL
349 selected_frame. */
350 if (!target_has_stack) error ("No stack.");
351
2847920a 352 prune_threads ();
02227894
DT
353#if defined(FIND_NEW_THREADS)
354 FIND_NEW_THREADS ();
355#endif
356
25286543
SG
357 for (tp = thread_list; tp; tp = tp->next)
358 {
25286543
SG
359 if (tp->pid == current_pid)
360 printf_filtered ("* ");
361 else
362 printf_filtered (" ");
363
364 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
365
c889a1eb 366 switch_to_thread (tp->pid);
3780c337
MS
367 if (selected_frame)
368 print_stack_frame (selected_frame, -1, 0);
369 else
370 printf_filtered ("[No stack.]\n");
25286543
SG
371 }
372
c889a1eb 373 switch_to_thread (current_pid);
25286543
SG
374}
375
376/* Switch from one thread to another. */
377
3082244d 378static void
c889a1eb 379switch_to_thread (pid)
25286543
SG
380 int pid;
381{
382 if (pid == inferior_pid)
383 return;
384
385 inferior_pid = pid;
25286543
SG
386 flush_cached_frames ();
387 registers_changed ();
388 stop_pc = read_pc();
25286543
SG
389 select_frame (get_current_frame (), 0);
390}
391
5090e82c
SG
392static void
393restore_current_thread (pid)
394 int pid;
395{
396 if (pid != inferior_pid)
c889a1eb 397 switch_to_thread (pid);
5090e82c
SG
398}
399
400/* Apply a GDB command to a list of threads. List syntax is a whitespace
401 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
402 of two numbers seperated by a hyphen. Examples:
403
404 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
405 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
406 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
407*/
408
409static void
410thread_apply_all_command (cmd, from_tty)
411 char *cmd;
412 int from_tty;
413{
414 struct thread_info *tp;
415 struct cleanup *old_chain;
416
417 if (cmd == NULL || *cmd == '\000')
418 error ("Please specify a command following the thread ID list");
419
420 old_chain = make_cleanup (restore_current_thread, inferior_pid);
421
422 for (tp = thread_list; tp; tp = tp->next)
2847920a
MS
423 if (thread_alive (tp))
424 {
425 switch_to_thread (tp->pid);
426 printf_filtered ("\nThread %d (%s):\n", tp->num,
427 target_pid_to_str (inferior_pid));
428 execute_command (cmd, from_tty);
429 }
5090e82c
SG
430}
431
432static void
433thread_apply_command (tidlist, from_tty)
434 char *tidlist;
435 int from_tty;
436{
437 char *cmd;
438 char *p;
439 struct cleanup *old_chain;
440
441 if (tidlist == NULL || *tidlist == '\000')
442 error ("Please specify a thread ID list");
443
444 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
445
446 if (*cmd == '\000')
447 error ("Please specify a command following the thread ID list");
448
449 old_chain = make_cleanup (restore_current_thread, inferior_pid);
450
451 while (tidlist < cmd)
452 {
453 struct thread_info *tp;
454 int start, end;
455
456 start = strtol (tidlist, &p, 10);
457 if (p == tidlist)
458 error ("Error parsing %s", tidlist);
459 tidlist = p;
460
461 while (*tidlist == ' ' || *tidlist == '\t')
462 tidlist++;
463
464 if (*tidlist == '-') /* Got a range of IDs? */
465 {
466 tidlist++; /* Skip the - */
467 end = strtol (tidlist, &p, 10);
468 if (p == tidlist)
469 error ("Error parsing %s", tidlist);
470 tidlist = p;
471
472 while (*tidlist == ' ' || *tidlist == '\t')
473 tidlist++;
474 }
475 else
476 end = start;
477
478 for (; start <= end; start++)
479 {
480 tp = find_thread_id (start);
481
482 if (!tp)
2847920a
MS
483 warning ("Unknown thread %d.", start);
484 else if (!thread_alive (tp))
485 warning ("Thread %d has terminated.", start);
486 else
5090e82c 487 {
2847920a
MS
488 switch_to_thread (tp->pid);
489 printf_filtered ("\nThread %d (%s):\n", tp->num,
490 target_pid_to_str (inferior_pid));
491 execute_command (cmd, from_tty);
5090e82c 492 }
5090e82c
SG
493 }
494 }
495}
496
497/* Switch to the specified thread. Will dispatch off to thread_apply_command
498 if prefix of arg is `apply'. */
499
25286543
SG
500static void
501thread_command (tidstr, from_tty)
502 char *tidstr;
503 int from_tty;
504{
505 int num;
506 struct thread_info *tp;
507
508 if (!tidstr)
509 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
510see the IDs of currently known threads.");
511
25286543
SG
512 num = atoi (tidstr);
513
514 tp = find_thread_id (num);
515
516 if (!tp)
517 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
518see the IDs of currently known threads.", num);
2847920a
MS
519
520 if (!thread_alive (tp))
521 error ("Thread ID %d has terminated.\n", num);
25286543 522
c889a1eb 523 switch_to_thread (tp->pid);
4ff5d55a
MH
524 if (context_hook)
525 context_hook (num);
25286543
SG
526 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
527 print_stack_frame (selected_frame, selected_frame_level, 1);
528}
529
c889a1eb
MS
530/* Commands with a prefix of `thread'. */
531struct cmd_list_element *thread_cmd_list = NULL;
532
25286543
SG
533void
534_initialize_thread ()
535{
5090e82c
SG
536 static struct cmd_list_element *thread_apply_list = NULL;
537 extern struct cmd_list_element *cmdlist;
538
25286543
SG
539 add_info ("threads", info_threads_command,
540 "IDs of currently known threads.");
5090e82c
SG
541
542 add_prefix_cmd ("thread", class_run, thread_command,
543 "Use this command to switch between threads.\n\
544The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
545 &cmdlist);
546
547 add_prefix_cmd ("apply", class_run, thread_apply_command,
548 "Apply a command to a list of threads.",
549 &thread_apply_list, "apply ", 1, &thread_cmd_list);
550
551 add_cmd ("all", class_run, thread_apply_all_command,
552 "Apply a command to all threads.",
553 &thread_apply_list);
554
555 add_com_alias ("t", "thread", class_run, 1);
25286543 556}
This page took 0.292724 seconds and 4 git commands to generate.