]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/thread.c
7ffc65af811f2d3ff0daa9c95bb16557fbf9becc
[thirdparty/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 /* Return true if TP is an active thread. */
274 static int
275 thread_alive (tp)
276 struct thread_info *tp;
277 {
278 if (tp->pid == -1)
279 return 0;
280 if (! target_thread_alive (tp->pid))
281 {
282 tp->pid = -1; /* Mark it as dead */
283 return 0;
284 }
285 return 1;
286 }
287
288 static void
289 prune_threads ()
290 {
291 struct thread_info *tp, *tpprev, *next;
292
293 tpprev = 0;
294 for (tp = thread_list; tp; tp = next)
295 {
296 next = tp->next;
297 if (!thread_alive (tp))
298 {
299 if (tpprev)
300 tpprev->next = next;
301 else
302 thread_list = next;
303 free (tp);
304 }
305 else
306 tpprev = tp;
307 }
308 }
309
310 /* Print information about currently known threads */
311
312 static void
313 info_threads_command (arg, from_tty)
314 char *arg;
315 int from_tty;
316 {
317 struct thread_info *tp;
318 int current_pid = inferior_pid;
319
320 /* Avoid coredumps which would happen if we tried to access a NULL
321 selected_frame. */
322 if (!target_has_stack) error ("No stack.");
323
324 prune_threads ();
325 for (tp = thread_list; tp; tp = tp->next)
326 {
327 if (tp->pid == current_pid)
328 printf_filtered ("* ");
329 else
330 printf_filtered (" ");
331
332 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
333
334 switch_to_thread (tp->pid);
335 print_stack_frame (selected_frame, -1, 0);
336 }
337
338 switch_to_thread (current_pid);
339 }
340
341 /* Switch from one thread to another. */
342
343 static void
344 switch_to_thread (pid)
345 int pid;
346 {
347 if (pid == inferior_pid)
348 return;
349
350 inferior_pid = pid;
351 flush_cached_frames ();
352 registers_changed ();
353 stop_pc = read_pc();
354 select_frame (get_current_frame (), 0);
355 }
356
357 static void
358 restore_current_thread (pid)
359 int pid;
360 {
361 if (pid != inferior_pid)
362 switch_to_thread (pid);
363 }
364
365 /* Apply a GDB command to a list of threads. List syntax is a whitespace
366 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
367 of two numbers seperated by a hyphen. Examples:
368
369 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
370 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
371 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
372 */
373
374 static void
375 thread_apply_all_command (cmd, from_tty)
376 char *cmd;
377 int from_tty;
378 {
379 struct thread_info *tp;
380 struct cleanup *old_chain;
381
382 if (cmd == NULL || *cmd == '\000')
383 error ("Please specify a command following the thread ID list");
384
385 old_chain = make_cleanup (restore_current_thread, inferior_pid);
386
387 for (tp = thread_list; tp; tp = tp->next)
388 if (thread_alive (tp))
389 {
390 switch_to_thread (tp->pid);
391 printf_filtered ("\nThread %d (%s):\n", tp->num,
392 target_pid_to_str (inferior_pid));
393 execute_command (cmd, from_tty);
394 }
395 }
396
397 static void
398 thread_apply_command (tidlist, from_tty)
399 char *tidlist;
400 int from_tty;
401 {
402 char *cmd;
403 char *p;
404 struct cleanup *old_chain;
405
406 if (tidlist == NULL || *tidlist == '\000')
407 error ("Please specify a thread ID list");
408
409 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
410
411 if (*cmd == '\000')
412 error ("Please specify a command following the thread ID list");
413
414 old_chain = make_cleanup (restore_current_thread, inferior_pid);
415
416 while (tidlist < cmd)
417 {
418 struct thread_info *tp;
419 int start, end;
420
421 start = 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 if (*tidlist == '-') /* Got a range of IDs? */
430 {
431 tidlist++; /* Skip the - */
432 end = strtol (tidlist, &p, 10);
433 if (p == tidlist)
434 error ("Error parsing %s", tidlist);
435 tidlist = p;
436
437 while (*tidlist == ' ' || *tidlist == '\t')
438 tidlist++;
439 }
440 else
441 end = start;
442
443 for (; start <= end; start++)
444 {
445 tp = find_thread_id (start);
446
447 if (!tp)
448 warning ("Unknown thread %d.", start);
449 else if (!thread_alive (tp))
450 warning ("Thread %d has terminated.", start);
451 else
452 {
453 switch_to_thread (tp->pid);
454 printf_filtered ("\nThread %d (%s):\n", tp->num,
455 target_pid_to_str (inferior_pid));
456 execute_command (cmd, from_tty);
457 }
458 }
459 }
460 }
461
462 /* Switch to the specified thread. Will dispatch off to thread_apply_command
463 if prefix of arg is `apply'. */
464
465 static void
466 thread_command (tidstr, from_tty)
467 char *tidstr;
468 int from_tty;
469 {
470 int num;
471 struct thread_info *tp;
472
473 if (!tidstr)
474 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
475 see the IDs of currently known threads.");
476
477 num = atoi (tidstr);
478
479 tp = find_thread_id (num);
480
481 if (!tp)
482 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
483 see the IDs of currently known threads.", num);
484
485 if (!thread_alive (tp))
486 error ("Thread ID %d has terminated.\n", num);
487
488 switch_to_thread (tp->pid);
489
490 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
491 print_stack_frame (selected_frame, selected_frame_level, 1);
492 }
493
494 /* Commands with a prefix of `thread'. */
495 struct cmd_list_element *thread_cmd_list = NULL;
496
497 void
498 _initialize_thread ()
499 {
500 static struct cmd_list_element *thread_apply_list = NULL;
501 extern struct cmd_list_element *cmdlist;
502
503 add_info ("threads", info_threads_command,
504 "IDs of currently known threads.");
505
506 add_prefix_cmd ("thread", class_run, thread_command,
507 "Use this command to switch between threads.\n\
508 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
509 &cmdlist);
510
511 add_prefix_cmd ("apply", class_run, thread_apply_command,
512 "Apply a command to a list of threads.",
513 &thread_apply_list, "apply ", 1, &thread_cmd_list);
514
515 add_cmd ("all", class_run, thread_apply_all_command,
516 "Apply a command to all threads.",
517 &thread_apply_list);
518
519 add_com_alias ("t", "thread", class_run, 1);
520 }