import gdb-1999-05-25 snapshot
[binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993, 1998
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 /* This is set TRUE when a catchpoint of a shared library event
58 triggers. Since we don't wish to leave the inferior in the
59 solib hook when we report the event, we step the inferior
60 back to user code before stopping and reporting the event.
61 */
62 int stepping_through_solib_after_catch;
63
64 /* When stepping_through_solib_after_catch is TRUE, this is a
65 list of the catchpoints that should be reported as triggering
66 when we finally do stop stepping.
67 */
68 bpstat stepping_through_solib_catchpoints;
69
70 /* This is set to TRUE when this thread is in a signal handler
71 trampoline and we're single-stepping through it */
72 int stepping_through_sigtramp;
73
74 };
75
76 /* Prototypes for exported functions. */
77
78 void _initialize_thread PARAMS ((void));
79
80 /* Prototypes for local functions. */
81
82 #if !defined(FIND_NEW_THREADS)
83 #define FIND_NEW_THREADS local_find_new_threads
84 #endif
85
86 static struct thread_info *thread_list = NULL;
87 static int highest_thread_num;
88
89 static struct thread_info * find_thread_id PARAMS ((int num));
90
91 static void thread_command PARAMS ((char * tidstr, int from_tty));
92 static void thread_apply_all_command PARAMS ((char *, int));
93 static int thread_alive PARAMS ((struct thread_info *));
94 static void info_threads_command PARAMS ((char *, int));
95 static void thread_apply_command PARAMS ((char *, int));
96 static void restore_current_thread PARAMS ((int));
97 static void switch_to_thread PARAMS ((int pid));
98 static void prune_threads PARAMS ((void));
99 static int local_find_new_threads PARAMS ((void));
100
101 /* If the host has threads, the host machine definition may set this
102 macro. But, for remote thread debugging, it gets more complex and
103 setting macros does not bind to the various target dependent
104 methods well. So, we use the vector target_thread_functions */
105
106 static struct target_thread_vector *target_thread_functions;
107
108 static int
109 local_find_new_threads ()
110 {
111 int retval = 0;
112 if (target_thread_functions &&
113 target_thread_functions->find_new_threads)
114 retval = (*(target_thread_functions->find_new_threads)) ();
115 return retval; /* no support */
116 }
117
118
119 int
120 target_get_thread_info PARAMS ((gdb_threadref * ref,
121 int selection, /* FIXME: Selection */
122 struct gdb_ext_thread_info * info));
123
124 int
125 target_get_thread_info (ref, selection, info)
126
127 gdb_threadref *ref;
128 int selection;
129 /* FIXME: Selection */
130 struct gdb_ext_thread_info *info;
131
132 {
133 int retval = 0;
134 if (target_thread_functions
135 && target_thread_functions->get_thread_info)
136 retval = (*(target_thread_functions->get_thread_info)) (ref, selection, info);
137 return retval;
138 }
139
140
141 /* It is possible that these bind and unbinf functions implement a
142 stack the interface allows it, but its not implemented that way
143 */
144
145
146 void
147 bind_target_thread_vector (vec)
148 struct target_thread_vector *vec;
149 {
150 target_thread_functions = vec;
151 }
152
153 struct target_thread_vector *
154 unbind_target_thread_vector ()
155 {
156 struct target_thread_vector *retval;
157 retval = target_thread_functions;
158 target_thread_functions = 0;
159 return retval;
160 } /* unbind_target_thread-vector */
161
162 void
163 init_thread_list ()
164 {
165 struct thread_info *tp, *tpnext;
166
167 if (!thread_list)
168 return;
169
170 for (tp = thread_list; tp; tp = tpnext)
171 {
172 tpnext = tp->next;
173 free (tp);
174 }
175
176 thread_list = NULL;
177 highest_thread_num = 0;
178 }
179
180 void
181 add_thread (pid)
182 int pid;
183 {
184 struct thread_info *tp;
185
186 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
187
188 tp->pid = pid;
189 tp->num = ++highest_thread_num;
190 tp->prev_pc = 0;
191 tp->prev_func_start = 0;
192 tp->prev_func_name = NULL;
193 tp->step_range_start = 0;
194 tp->step_range_end = 0;
195 tp->step_frame_address =0;
196 tp->step_resume_breakpoint = 0;
197 tp->through_sigtramp_breakpoint = 0;
198 tp->handling_longjmp = 0;
199 tp->trap_expected = 0;
200 tp->another_trap = 0;
201 tp->stepping_through_solib_after_catch = 0;
202 tp->stepping_through_solib_catchpoints = NULL;
203 tp->stepping_through_sigtramp = 0;
204 tp->next = thread_list;
205 thread_list = tp;
206 }
207
208 void
209 delete_thread (pid)
210 int pid;
211 {
212 struct thread_info *tp, *tpprev;
213
214 tpprev = NULL;
215
216 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
217 if (tp->pid == pid)
218 break;
219
220 if (!tp)
221 return;
222
223 if (tpprev)
224 tpprev->next = tp->next;
225 else
226 thread_list = tp->next;
227
228 free (tp);
229
230 return;
231 }
232
233 static struct thread_info *
234 find_thread_id (num)
235 int num;
236 {
237 struct thread_info *tp;
238
239 for (tp = thread_list; tp; tp = tp->next)
240 if (tp->num == num)
241 return tp;
242
243 return NULL;
244 }
245
246 int
247 valid_thread_id (num)
248 int num;
249 {
250 struct thread_info *tp;
251
252 for (tp = thread_list; tp; tp = tp->next)
253 if (tp->num == num)
254 return 1;
255
256 return 0;
257 }
258
259 int
260 pid_to_thread_id (pid)
261 int pid;
262 {
263 struct thread_info *tp;
264
265 for (tp = thread_list; tp; tp = tp->next)
266 if (tp->pid == pid)
267 return tp->num;
268
269 return 0;
270 }
271
272 int
273 thread_id_to_pid (num)
274 int num;
275 {
276 struct thread_info *thread = find_thread_id (num);
277 if (thread)
278 return thread->pid;
279 else
280 return -1;
281 }
282
283 int
284 in_thread_list (pid)
285 int pid;
286 {
287 struct thread_info *tp;
288
289 for (tp = thread_list; tp; tp = tp->next)
290 if (tp->pid == pid)
291 return 1;
292
293 return 0; /* Never heard of 'im */
294 }
295
296 /* Load infrun state for the thread PID. */
297
298 void load_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
299 trap_expected, step_resume_breakpoint,
300 through_sigtramp_breakpoint, step_range_start,
301 step_range_end, step_frame_address,
302 handling_longjmp, another_trap,
303 stepping_through_solib_after_catch,
304 stepping_through_solib_catchpoints,
305 stepping_through_sigtramp)
306 int pid;
307 CORE_ADDR *prev_pc;
308 CORE_ADDR *prev_func_start;
309 char **prev_func_name;
310 int *trap_expected;
311 struct breakpoint **step_resume_breakpoint;
312 struct breakpoint **through_sigtramp_breakpoint;
313 CORE_ADDR *step_range_start;
314 CORE_ADDR *step_range_end;
315 CORE_ADDR *step_frame_address;
316 int *handling_longjmp;
317 int *another_trap;
318 int * stepping_through_solib_after_catch;
319 bpstat * stepping_through_solib_catchpoints;
320 int * stepping_through_sigtramp;
321 {
322 struct thread_info *tp;
323
324 /* If we can't find the thread, then we're debugging a single threaded
325 process. No need to do anything in that case. */
326 tp = find_thread_id (pid_to_thread_id (pid));
327 if (tp == NULL)
328 return;
329
330 *prev_pc = tp->prev_pc;
331 *prev_func_start = tp->prev_func_start;
332 *prev_func_name = tp->prev_func_name;
333 *step_resume_breakpoint = tp->step_resume_breakpoint;
334 *step_range_start = tp->step_range_start;
335 *step_range_end = tp->step_range_end;
336 *step_frame_address = tp->step_frame_address;
337 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
338 *handling_longjmp = tp->handling_longjmp;
339 *trap_expected = tp->trap_expected;
340 *another_trap = tp->another_trap;
341 *stepping_through_solib_after_catch = tp->stepping_through_solib_after_catch;
342 *stepping_through_solib_catchpoints = tp->stepping_through_solib_catchpoints;
343 *stepping_through_sigtramp = tp->stepping_through_sigtramp;
344 }
345
346 /* Save infrun state for the thread PID. */
347
348 void save_infrun_state (pid, prev_pc, prev_func_start, prev_func_name,
349 trap_expected, step_resume_breakpoint,
350 through_sigtramp_breakpoint, step_range_start,
351 step_range_end, step_frame_address,
352 handling_longjmp, another_trap,
353 stepping_through_solib_after_catch,
354 stepping_through_solib_catchpoints,
355 stepping_through_sigtramp)
356 int pid;
357 CORE_ADDR prev_pc;
358 CORE_ADDR prev_func_start;
359 char *prev_func_name;
360 int trap_expected;
361 struct breakpoint *step_resume_breakpoint;
362 struct breakpoint *through_sigtramp_breakpoint;
363 CORE_ADDR step_range_start;
364 CORE_ADDR step_range_end;
365 CORE_ADDR step_frame_address;
366 int handling_longjmp;
367 int another_trap;
368 int stepping_through_solib_after_catch;
369 bpstat stepping_through_solib_catchpoints;
370 int stepping_through_sigtramp;
371 {
372 struct thread_info *tp;
373
374 /* If we can't find the thread, then we're debugging a single-threaded
375 process. Nothing to do in that case. */
376 tp = find_thread_id (pid_to_thread_id (pid));
377 if (tp == NULL)
378 return;
379
380 tp->prev_pc = prev_pc;
381 tp->prev_func_start = prev_func_start;
382 tp->prev_func_name = prev_func_name;
383 tp->step_resume_breakpoint = step_resume_breakpoint;
384 tp->step_range_start = step_range_start;
385 tp->step_range_end = step_range_end;
386 tp->step_frame_address = step_frame_address;
387 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
388 tp->handling_longjmp = handling_longjmp;
389 tp->trap_expected = trap_expected;
390 tp->another_trap = another_trap;
391 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
392 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
393 tp->stepping_through_sigtramp = stepping_through_sigtramp;
394 }
395
396 /* Return true if TP is an active thread. */
397 static int
398 thread_alive (tp)
399 struct thread_info *tp;
400 {
401 if (tp->pid == -1)
402 return 0;
403 if (! target_thread_alive (tp->pid))
404 {
405 tp->pid = -1; /* Mark it as dead */
406 return 0;
407 }
408 return 1;
409 }
410
411 static void
412 prune_threads ()
413 {
414 struct thread_info *tp, *tpprev, *next;
415
416 tpprev = 0;
417 for (tp = thread_list; tp; tp = next)
418 {
419 next = tp->next;
420 if (!thread_alive (tp))
421 {
422 if (tpprev)
423 tpprev->next = next;
424 else
425 thread_list = next;
426 free (tp);
427 }
428 else
429 tpprev = tp;
430 }
431 }
432
433 /* Print information about currently known threads
434 *
435 * Note: this has the drawback that it _really_ switches
436 * threads, which frees the frame cache. A no-side
437 * effects info-threads command would be nicer.
438 */
439
440 static void
441 info_threads_command (arg, from_tty)
442 char *arg;
443 int from_tty;
444 {
445 struct thread_info *tp;
446 int current_pid;
447 struct frame_info *cur_frame;
448 int saved_frame_level = selected_frame_level;
449 int counter;
450
451 /* Avoid coredumps which would happen if we tried to access a NULL
452 selected_frame. */
453 if (!target_has_stack) error ("No stack.");
454
455 prune_threads ();
456 target_find_new_threads ();
457 current_pid = inferior_pid;
458 for (tp = thread_list; tp; tp = tp->next)
459 {
460 if (tp->pid == current_pid)
461 printf_filtered ("* ");
462 else
463 printf_filtered (" ");
464
465 #ifdef HPUXHPPA
466 printf_filtered ("%d %s ", tp->num, target_tid_to_str (tp->pid));
467 #else
468 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
469 #endif
470 switch_to_thread (tp->pid);
471 if (selected_frame)
472 print_only_stack_frame (selected_frame, -1, 0);
473 else
474 printf_filtered ("[No stack.]\n");
475 }
476
477 switch_to_thread (current_pid);
478
479 /* Code below copied from "up_silently_base" in "stack.c".
480 * It restores the frame set by the user before the "info threads"
481 * command. We have finished the info-threads display by switching
482 * back to the current thread. That switch has put us at the top
483 * of the stack (leaf frame).
484 */
485 counter = saved_frame_level;
486 cur_frame = find_relative_frame(selected_frame, &counter);
487 if (counter != 0)
488 {
489 /* Ooops, can't restore, tell user where we are. */
490 warning ("Couldn't restore frame in current thread, at frame 0");
491 print_stack_frame (selected_frame, -1, 0);
492 }
493 else
494 {
495 select_frame(cur_frame, saved_frame_level);
496 }
497
498 /* re-show current frame. */
499 show_stack_frame(cur_frame);
500 }
501
502 /* Switch from one thread to another. */
503
504 static void
505 switch_to_thread (pid)
506 int pid;
507 {
508 if (pid == inferior_pid)
509 return;
510
511 inferior_pid = pid;
512 flush_cached_frames ();
513 registers_changed ();
514 stop_pc = read_pc();
515 select_frame (get_current_frame (), 0);
516 }
517
518 static void
519 restore_current_thread (pid)
520 int pid;
521 {
522 if (pid != inferior_pid)
523 {
524 switch_to_thread (pid);
525 print_stack_frame( get_current_frame(), 0, -1);
526 }
527 }
528
529 /* Apply a GDB command to a list of threads. List syntax is a whitespace
530 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
531 of two numbers seperated by a hyphen. Examples:
532
533 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
534 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
535 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
536 */
537
538 static void
539 thread_apply_all_command (cmd, from_tty)
540 char *cmd;
541 int from_tty;
542 {
543 struct thread_info *tp;
544 struct cleanup *old_chain;
545
546 if (cmd == NULL || *cmd == '\000')
547 error ("Please specify a command following the thread ID list");
548
549 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
550 (void *) inferior_pid);
551
552 for (tp = thread_list; tp; tp = tp->next)
553 if (thread_alive (tp))
554 {
555 switch_to_thread (tp->pid);
556 #ifdef HPUXHPPA
557 printf_filtered ("\nThread %d (%s):\n",
558 tp->num,
559 target_tid_to_str (inferior_pid));
560 #else
561 printf_filtered ("\nThread %d (%s):\n", tp->num,
562 target_pid_to_str (inferior_pid));
563 #endif
564 execute_command (cmd, from_tty);
565 }
566 }
567
568 static void
569 thread_apply_command (tidlist, from_tty)
570 char *tidlist;
571 int from_tty;
572 {
573 char *cmd;
574 char *p;
575 struct cleanup *old_chain;
576
577 if (tidlist == NULL || *tidlist == '\000')
578 error ("Please specify a thread ID list");
579
580 for (cmd = tidlist; *cmd != '\000' && !isalpha(*cmd); cmd++);
581
582 if (*cmd == '\000')
583 error ("Please specify a command following the thread ID list");
584
585 old_chain = make_cleanup ((make_cleanup_func) restore_current_thread,
586 (void *) inferior_pid);
587
588 while (tidlist < cmd)
589 {
590 struct thread_info *tp;
591 int start, end;
592
593 start = strtol (tidlist, &p, 10);
594 if (p == tidlist)
595 error ("Error parsing %s", tidlist);
596 tidlist = p;
597
598 while (*tidlist == ' ' || *tidlist == '\t')
599 tidlist++;
600
601 if (*tidlist == '-') /* Got a range of IDs? */
602 {
603 tidlist++; /* Skip the - */
604 end = strtol (tidlist, &p, 10);
605 if (p == tidlist)
606 error ("Error parsing %s", tidlist);
607 tidlist = p;
608
609 while (*tidlist == ' ' || *tidlist == '\t')
610 tidlist++;
611 }
612 else
613 end = start;
614
615 for (; start <= end; start++)
616 {
617 tp = find_thread_id (start);
618
619 if (!tp)
620 warning ("Unknown thread %d.", start);
621 else if (!thread_alive (tp))
622 warning ("Thread %d has terminated.", start);
623 else
624 {
625 switch_to_thread (tp->pid);
626 #ifdef HPUXHPPA
627 printf_filtered ("\nThread %d (%s):\n", tp->num,
628 target_tid_to_str (inferior_pid));
629 #else
630 printf_filtered ("\nThread %d (%s):\n", tp->num,
631 target_pid_to_str (inferior_pid));
632 #endif
633 execute_command (cmd, from_tty);
634 }
635 }
636 }
637 }
638
639 /* Switch to the specified thread. Will dispatch off to thread_apply_command
640 if prefix of arg is `apply'. */
641
642 static void
643 thread_command (tidstr, from_tty)
644 char *tidstr;
645 int from_tty;
646 {
647 int num;
648 struct thread_info *tp;
649
650 if (!tidstr)
651 {
652 /* Don't generate an error, just say which thread is current. */
653 if (target_has_stack)
654 printf_filtered ("[Current thread is %d (%s)]\n",
655 pid_to_thread_id(inferior_pid),
656 #if defined(HPUXHPPA)
657 target_tid_to_str(inferior_pid)
658 #else
659 target_pid_to_str(inferior_pid)
660 #endif
661 );
662 else
663 error ("No stack.");
664 return;
665 }
666 num = atoi (tidstr);
667
668 tp = find_thread_id (num);
669
670 if (!tp)
671 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
672 see the IDs of currently known threads.", num);
673
674 if (!thread_alive (tp))
675 error ("Thread ID %d has terminated.\n", num);
676
677 switch_to_thread (tp->pid);
678
679 if (context_hook)
680 context_hook (num);
681
682 printf_filtered ("[Switching to thread %d (%s)]\n",
683 pid_to_thread_id (inferior_pid),
684 #if defined(HPUXHPPA)
685 target_tid_to_str (inferior_pid)
686 #else
687 target_pid_to_str (inferior_pid)
688 #endif
689 );
690 print_stack_frame (selected_frame, selected_frame_level, 1);
691 }
692
693 /* Commands with a prefix of `thread'. */
694 struct cmd_list_element *thread_cmd_list = NULL;
695
696 void
697 _initialize_thread ()
698 {
699 static struct cmd_list_element *thread_apply_list = NULL;
700
701 add_info ("threads", info_threads_command,
702 "IDs of currently known threads.");
703
704 add_prefix_cmd ("thread", class_run, thread_command,
705 "Use this command to switch between threads.\n\
706 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1,
707 &cmdlist);
708
709 add_prefix_cmd ("apply", class_run, thread_apply_command,
710 "Apply a command to a list of threads.",
711 &thread_apply_list, "apply ", 1, &thread_cmd_list);
712
713 add_cmd ("all", class_run, thread_apply_all_command,
714 "Apply a command to all threads.",
715 &thread_apply_list);
716
717 if (!xdb_commands)
718 add_com_alias ("t", "thread", class_run, 1);
719 }