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