]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/thread.c
* infrun.c (wait_for_inferior): When switching from one thread to
[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., 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 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 /* FIXME: need to figure out a way to do this for remote too,
284 or else the print_stack_frame below will fail with a bogus
285 thread ID. */
286 if (!STREQ (current_target.to_shortname, "remote")
287 && target_has_execution
288 && kill (tp->pid, 0) == -1)
289 {
290 tp->pid = -1; /* Mark it as dead */
291 continue;
292 }
293
294 if (tp->pid == current_pid)
295 printf_filtered ("* ");
296 else
297 printf_filtered (" ");
298
299 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
300
301 thread_switch (tp->pid);
302 print_stack_frame (selected_frame, -1, 0);
303 }
304
305 thread_switch (current_pid);
306 prune_threads ();
307 }
308
309 /* Switch from one thread to another. */
310
311 static void
312 thread_switch (pid)
313 int pid;
314 {
315 if (pid == inferior_pid)
316 return;
317
318 inferior_pid = pid;
319 flush_cached_frames ();
320 registers_changed ();
321 stop_pc = read_pc();
322 select_frame (get_current_frame (), 0);
323 }
324
325 static void
326 restore_current_thread (pid)
327 int pid;
328 {
329 if (pid != inferior_pid)
330 thread_switch (pid);
331 }
332
333 /* Apply a GDB command to a list of threads. List syntax is a whitespace
334 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
335 of two numbers seperated by a hyphen. Examples:
336
337 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
338 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
339 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
340 */
341
342 static void
343 thread_apply_all_command (cmd, from_tty)
344 char *cmd;
345 int from_tty;
346 {
347 struct thread_info *tp;
348 struct cleanup *old_chain;
349
350 if (cmd == NULL || *cmd == '\000')
351 error ("Please specify a command following the thread ID list");
352
353 old_chain = make_cleanup (restore_current_thread, inferior_pid);
354
355 for (tp = thread_list; tp; tp = tp->next)
356 {
357 thread_switch (tp->pid);
358 printf_filtered ("\nThread %d (%s):\n", tp->num,
359 target_pid_to_str (inferior_pid));
360 execute_command (cmd, from_tty);
361 }
362 }
363
364 static void
365 thread_apply_command (tidlist, from_tty)
366 char *tidlist;
367 int from_tty;
368 {
369 char *cmd;
370 char *p;
371 struct cleanup *old_chain;
372
373 if (tidlist == NULL || *tidlist == '\000')
374 error ("Please specify a thread ID list");
375
376 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
377
378 if (*cmd == '\000')
379 error ("Please specify a command following the thread ID list");
380
381 old_chain = make_cleanup (restore_current_thread, inferior_pid);
382
383 while (tidlist < cmd)
384 {
385 struct thread_info *tp;
386 int start, end;
387
388 start = strtol (tidlist, &p, 10);
389 if (p == tidlist)
390 error ("Error parsing %s", tidlist);
391 tidlist = p;
392
393 while (*tidlist == ' ' || *tidlist == '\t')
394 tidlist++;
395
396 if (*tidlist == '-') /* Got a range of IDs? */
397 {
398 tidlist++; /* Skip the - */
399 end = strtol (tidlist, &p, 10);
400 if (p == tidlist)
401 error ("Error parsing %s", tidlist);
402 tidlist = p;
403
404 while (*tidlist == ' ' || *tidlist == '\t')
405 tidlist++;
406 }
407 else
408 end = start;
409
410 for (; start <= end; start++)
411 {
412 tp = find_thread_id (start);
413
414 if (!tp)
415 {
416 warning ("Unknown thread %d.", start);
417 continue;
418 }
419
420 thread_switch (tp->pid);
421 printf_filtered ("\nThread %d (%s):\n", tp->num,
422 target_pid_to_str (inferior_pid));
423 execute_command (cmd, from_tty);
424 }
425 }
426 }
427
428 /* Switch to the specified thread. Will dispatch off to thread_apply_command
429 if prefix of arg is `apply'. */
430
431 static void
432 thread_command (tidstr, from_tty)
433 char *tidstr;
434 int from_tty;
435 {
436 int num;
437 struct thread_info *tp;
438
439 if (!tidstr)
440 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
441 see the IDs of currently known threads.");
442
443 num = atoi (tidstr);
444
445 tp = find_thread_id (num);
446
447 if (!tp)
448 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
449 see the IDs of currently known threads.", num);
450
451 thread_switch (tp->pid);
452
453 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
454 print_stack_frame (selected_frame, selected_frame_level, 1);
455 }
456
457 void
458 _initialize_thread ()
459 {
460 static struct cmd_list_element *thread_cmd_list = NULL;
461 static struct cmd_list_element *thread_apply_list = NULL;
462 extern struct cmd_list_element *cmdlist;
463
464 add_info ("threads", info_threads_command,
465 "IDs of currently known threads.");
466
467 add_prefix_cmd ("thread", class_run, thread_command,
468 "Use this command to switch between threads.\n\
469 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
470 &cmdlist);
471
472 add_prefix_cmd ("apply", class_run, thread_apply_command,
473 "Apply a command to a list of threads.",
474 &thread_apply_list, "apply ", 1, &thread_cmd_list);
475
476 add_cmd ("all", class_run, thread_apply_all_command,
477 "Apply a command to all threads.",
478 &thread_apply_list);
479
480 add_com_alias ("t", "thread", class_run, 1);
481 }