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