2003-04-08 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3 Copyright 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "environ.h"
30 #include "value.h"
31 #include "target.h"
32 #include "gdbthread.h"
33 #include "command.h"
34 #include "gdbcmd.h"
35 #include "regcache.h"
36 #include "gdb.h"
37 #include "gdb_string.h"
38
39 #include <ctype.h>
40 #include <sys/types.h>
41 #include <signal.h>
42 #include "ui-out.h"
43
44 /*#include "lynxos-core.h" */
45
46 /* Definition of struct thread_info exported to gdbthread.h */
47
48 /* Prototypes for exported functions. */
49
50 void _initialize_thread (void);
51
52 /* Prototypes for local functions. */
53
54 static struct thread_info *thread_list = NULL;
55 static int highest_thread_num;
56
57 static struct thread_info *find_thread_id (int num);
58
59 static void thread_command (char *tidstr, int from_tty);
60 static void thread_apply_all_command (char *, int);
61 static int thread_alive (struct thread_info *);
62 static void info_threads_command (char *, int);
63 static void thread_apply_command (char *, int);
64 static void restore_current_thread (ptid_t);
65 static void switch_to_thread (ptid_t ptid);
66 static void prune_threads (void);
67
68 void
69 delete_step_resume_breakpoint (void *arg)
70 {
71 struct breakpoint **breakpointp = (struct breakpoint **) arg;
72 struct thread_info *tp;
73
74 if (*breakpointp != NULL)
75 {
76 delete_breakpoint (*breakpointp);
77 for (tp = thread_list; tp; tp = tp->next)
78 if (tp->step_resume_breakpoint == *breakpointp)
79 tp->step_resume_breakpoint = NULL;
80
81 *breakpointp = NULL;
82 }
83 }
84
85 static void
86 free_thread (struct thread_info *tp)
87 {
88 /* NOTE: this will take care of any left-over step_resume breakpoints,
89 but not any user-specified thread-specific breakpoints. */
90 if (tp->step_resume_breakpoint)
91 delete_breakpoint (tp->step_resume_breakpoint);
92
93 /* FIXME: do I ever need to call the back-end to give it a
94 chance at this private data before deleting the thread? */
95 if (tp->private)
96 xfree (tp->private);
97
98 xfree (tp);
99 }
100
101 void
102 init_thread_list (void)
103 {
104 struct thread_info *tp, *tpnext;
105
106 highest_thread_num = 0;
107 if (!thread_list)
108 return;
109
110 for (tp = thread_list; tp; tp = tpnext)
111 {
112 tpnext = tp->next;
113 free_thread (tp);
114 }
115
116 thread_list = NULL;
117 }
118
119 /* add_thread now returns a pointer to the new thread_info,
120 so that back_ends can initialize their private data. */
121
122 struct thread_info *
123 add_thread (ptid_t ptid)
124 {
125 struct thread_info *tp;
126
127 tp = (struct thread_info *) xmalloc (sizeof (*tp));
128 memset (tp, 0, sizeof (*tp));
129 tp->ptid = ptid;
130 tp->num = ++highest_thread_num;
131 tp->next = thread_list;
132 thread_list = tp;
133 return tp;
134 }
135
136 void
137 delete_thread (ptid_t ptid)
138 {
139 struct thread_info *tp, *tpprev;
140
141 tpprev = NULL;
142
143 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
144 if (ptid_equal (tp->ptid, ptid))
145 break;
146
147 if (!tp)
148 return;
149
150 if (tpprev)
151 tpprev->next = tp->next;
152 else
153 thread_list = tp->next;
154
155 free_thread (tp);
156 }
157
158 static struct thread_info *
159 find_thread_id (int num)
160 {
161 struct thread_info *tp;
162
163 for (tp = thread_list; tp; tp = tp->next)
164 if (tp->num == num)
165 return tp;
166
167 return NULL;
168 }
169
170 /* Find a thread_info by matching PTID. */
171 struct thread_info *
172 find_thread_pid (ptid_t ptid)
173 {
174 struct thread_info *tp;
175
176 for (tp = thread_list; tp; tp = tp->next)
177 if (ptid_equal (tp->ptid, ptid))
178 return tp;
179
180 return NULL;
181 }
182
183 /*
184 * Thread iterator function.
185 *
186 * Calls a callback function once for each thread, so long as
187 * the callback function returns false. If the callback function
188 * returns true, the iteration will end and the current thread
189 * will be returned. This can be useful for implementing a
190 * search for a thread with arbitrary attributes, or for applying
191 * some operation to every thread.
192 *
193 * FIXME: some of the existing functionality, such as
194 * "Thread apply all", might be rewritten using this functionality.
195 */
196
197 struct thread_info *
198 iterate_over_threads (int (*callback) (struct thread_info *, void *),
199 void *data)
200 {
201 struct thread_info *tp;
202
203 for (tp = thread_list; tp; tp = tp->next)
204 if ((*callback) (tp, data))
205 return tp;
206
207 return NULL;
208 }
209
210 int
211 valid_thread_id (int num)
212 {
213 struct thread_info *tp;
214
215 for (tp = thread_list; tp; tp = tp->next)
216 if (tp->num == num)
217 return 1;
218
219 return 0;
220 }
221
222 int
223 pid_to_thread_id (ptid_t ptid)
224 {
225 struct thread_info *tp;
226
227 for (tp = thread_list; tp; tp = tp->next)
228 if (ptid_equal (tp->ptid, ptid))
229 return tp->num;
230
231 return 0;
232 }
233
234 ptid_t
235 thread_id_to_pid (int num)
236 {
237 struct thread_info *thread = find_thread_id (num);
238 if (thread)
239 return thread->ptid;
240 else
241 return pid_to_ptid (-1);
242 }
243
244 int
245 in_thread_list (ptid_t ptid)
246 {
247 struct thread_info *tp;
248
249 for (tp = thread_list; tp; tp = tp->next)
250 if (ptid_equal (tp->ptid, ptid))
251 return 1;
252
253 return 0; /* Never heard of 'im */
254 }
255
256 /* Print a list of thread ids currently known, and the total number of
257 threads. To be used from within catch_errors. */
258 static int
259 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
260 {
261 struct thread_info *tp;
262 int num = 0;
263 struct cleanup *cleanup_chain;
264
265 prune_threads ();
266 target_find_new_threads ();
267
268 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
269
270 for (tp = thread_list; tp; tp = tp->next)
271 {
272 num++;
273 ui_out_field_int (uiout, "thread-id", tp->num);
274 }
275
276 do_cleanups (cleanup_chain);
277 ui_out_field_int (uiout, "number-of-threads", num);
278 return GDB_RC_OK;
279 }
280
281 /* Official gdblib interface function to get a list of thread ids and
282 the total number. */
283 enum gdb_rc
284 gdb_list_thread_ids (struct ui_out *uiout)
285 {
286 return catch_exceptions (uiout, do_captured_list_thread_ids, NULL,
287 NULL, RETURN_MASK_ALL);
288 }
289
290 /* Load infrun state for the thread PID. */
291
292 void
293 load_infrun_state (ptid_t ptid,
294 CORE_ADDR *prev_pc,
295 CORE_ADDR *prev_func_start,
296 char **prev_func_name,
297 int *trap_expected,
298 struct breakpoint **step_resume_breakpoint,
299 struct breakpoint **through_sigtramp_breakpoint,
300 CORE_ADDR *step_range_start,
301 CORE_ADDR *step_range_end,
302 struct frame_id *step_frame_id,
303 int *handling_longjmp,
304 int *another_trap,
305 int *stepping_through_solib_after_catch,
306 bpstat *stepping_through_solib_catchpoints,
307 int *stepping_through_sigtramp,
308 int *current_line,
309 struct symtab **current_symtab, CORE_ADDR *step_sp)
310 {
311 struct thread_info *tp;
312
313 /* If we can't find the thread, then we're debugging a single threaded
314 process. No need to do anything in that case. */
315 tp = find_thread_id (pid_to_thread_id (ptid));
316 if (tp == NULL)
317 return;
318
319 *prev_pc = tp->prev_pc;
320 *prev_func_start = tp->prev_func_start;
321 *prev_func_name = tp->prev_func_name;
322 *trap_expected = tp->trap_expected;
323 *step_resume_breakpoint = tp->step_resume_breakpoint;
324 *through_sigtramp_breakpoint = tp->through_sigtramp_breakpoint;
325 *step_range_start = tp->step_range_start;
326 *step_range_end = tp->step_range_end;
327 *step_frame_id = tp->step_frame_id;
328 *handling_longjmp = tp->handling_longjmp;
329 *another_trap = tp->another_trap;
330 *stepping_through_solib_after_catch =
331 tp->stepping_through_solib_after_catch;
332 *stepping_through_solib_catchpoints =
333 tp->stepping_through_solib_catchpoints;
334 *stepping_through_sigtramp = tp->stepping_through_sigtramp;
335 *current_line = tp->current_line;
336 *current_symtab = tp->current_symtab;
337 *step_sp = tp->step_sp;
338 }
339
340 /* Save infrun state for the thread PID. */
341
342 void
343 save_infrun_state (ptid_t ptid,
344 CORE_ADDR prev_pc,
345 CORE_ADDR prev_func_start,
346 char *prev_func_name,
347 int trap_expected,
348 struct breakpoint *step_resume_breakpoint,
349 struct breakpoint *through_sigtramp_breakpoint,
350 CORE_ADDR step_range_start,
351 CORE_ADDR step_range_end,
352 const struct frame_id *step_frame_id,
353 int handling_longjmp,
354 int another_trap,
355 int stepping_through_solib_after_catch,
356 bpstat stepping_through_solib_catchpoints,
357 int stepping_through_sigtramp,
358 int current_line,
359 struct symtab *current_symtab, CORE_ADDR step_sp)
360 {
361 struct thread_info *tp;
362
363 /* If we can't find the thread, then we're debugging a single-threaded
364 process. Nothing to do in that case. */
365 tp = find_thread_id (pid_to_thread_id (ptid));
366 if (tp == NULL)
367 return;
368
369 tp->prev_pc = prev_pc;
370 tp->prev_func_start = prev_func_start;
371 tp->prev_func_name = prev_func_name;
372 tp->trap_expected = trap_expected;
373 tp->step_resume_breakpoint = step_resume_breakpoint;
374 tp->through_sigtramp_breakpoint = through_sigtramp_breakpoint;
375 tp->step_range_start = step_range_start;
376 tp->step_range_end = step_range_end;
377 tp->step_frame_id = (*step_frame_id);
378 tp->handling_longjmp = handling_longjmp;
379 tp->another_trap = another_trap;
380 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
381 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
382 tp->stepping_through_sigtramp = stepping_through_sigtramp;
383 tp->current_line = current_line;
384 tp->current_symtab = current_symtab;
385 tp->step_sp = step_sp;
386 }
387
388 /* Return true if TP is an active thread. */
389 static int
390 thread_alive (struct thread_info *tp)
391 {
392 if (PIDGET (tp->ptid) == -1)
393 return 0;
394 if (!target_thread_alive (tp->ptid))
395 {
396 tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
397 return 0;
398 }
399 return 1;
400 }
401
402 static void
403 prune_threads (void)
404 {
405 struct thread_info *tp, *next;
406
407 for (tp = thread_list; tp; tp = next)
408 {
409 next = tp->next;
410 if (!thread_alive (tp))
411 delete_thread (tp->ptid);
412 }
413 }
414
415 /* Print information about currently known threads
416
417 * Note: this has the drawback that it _really_ switches
418 * threads, which frees the frame cache. A no-side
419 * effects info-threads command would be nicer.
420 */
421
422 static void
423 info_threads_command (char *arg, int from_tty)
424 {
425 struct thread_info *tp;
426 ptid_t current_ptid;
427 struct frame_info *cur_frame;
428 int saved_frame_level = frame_relative_level (deprecated_selected_frame);
429 int counter;
430 char *extra_info;
431
432 /* Avoid coredumps which would happen if we tried to access a NULL
433 deprecated_selected_frame. */
434 if (!target_has_stack)
435 error ("No stack.");
436
437 prune_threads ();
438 target_find_new_threads ();
439 current_ptid = inferior_ptid;
440 for (tp = thread_list; tp; tp = tp->next)
441 {
442 if (ptid_equal (tp->ptid, current_ptid))
443 printf_filtered ("* ");
444 else
445 printf_filtered (" ");
446
447 #ifdef HPUXHPPA
448 printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
449 #else
450 printf_filtered ("%d %s", tp->num, target_pid_to_str (tp->ptid));
451 #endif
452
453 extra_info = target_extra_thread_info (tp);
454 if (extra_info)
455 printf_filtered (" (%s)", extra_info);
456 puts_filtered (" ");
457
458 switch_to_thread (tp->ptid);
459 if (deprecated_selected_frame)
460 print_stack_frame (deprecated_selected_frame, -1, 0);
461 else
462 printf_filtered ("[No stack.]\n");
463 }
464
465 switch_to_thread (current_ptid);
466
467 /* Code below copied from "up_silently_base" in "stack.c".
468 * It restores the frame set by the user before the "info threads"
469 * command. We have finished the info-threads display by switching
470 * back to the current thread. That switch has put us at the top
471 * of the stack (leaf frame).
472 */
473 counter = saved_frame_level;
474 cur_frame = find_relative_frame (deprecated_selected_frame, &counter);
475 if (counter != 0)
476 {
477 /* Ooops, can't restore, tell user where we are. */
478 warning ("Couldn't restore frame in current thread, at frame 0");
479 print_stack_frame (deprecated_selected_frame, -1, 0);
480 }
481 else
482 {
483 select_frame (cur_frame);
484 }
485
486 /* re-show current frame. */
487 show_stack_frame (cur_frame);
488 }
489
490 /* Switch from one thread to another. */
491
492 static void
493 switch_to_thread (ptid_t ptid)
494 {
495 if (ptid_equal (ptid, inferior_ptid))
496 return;
497
498 inferior_ptid = ptid;
499 flush_cached_frames ();
500 registers_changed ();
501 stop_pc = read_pc ();
502 select_frame (get_current_frame ());
503 }
504
505 static void
506 restore_current_thread (ptid_t ptid)
507 {
508 if (!ptid_equal (ptid, inferior_ptid))
509 {
510 switch_to_thread (ptid);
511 print_stack_frame (get_current_frame (), 0, -1);
512 }
513 }
514
515 struct current_thread_cleanup
516 {
517 ptid_t inferior_ptid;
518 };
519
520 static void
521 do_restore_current_thread_cleanup (void *arg)
522 {
523 struct current_thread_cleanup *old = arg;
524 restore_current_thread (old->inferior_ptid);
525 xfree (old);
526 }
527
528 static struct cleanup *
529 make_cleanup_restore_current_thread (ptid_t inferior_ptid)
530 {
531 struct current_thread_cleanup *old
532 = xmalloc (sizeof (struct current_thread_cleanup));
533 old->inferior_ptid = inferior_ptid;
534 return make_cleanup (do_restore_current_thread_cleanup, old);
535 }
536
537 /* Apply a GDB command to a list of threads. List syntax is a whitespace
538 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
539 of two numbers seperated by a hyphen. Examples:
540
541 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
542 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
543 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
544 */
545
546 static void
547 thread_apply_all_command (char *cmd, int from_tty)
548 {
549 struct thread_info *tp;
550 struct cleanup *old_chain;
551 struct cleanup *saved_cmd_cleanup_chain;
552 char *saved_cmd;
553
554 if (cmd == NULL || *cmd == '\000')
555 error ("Please specify a command following the thread ID list");
556
557 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
558
559 /* It is safe to update the thread list now, before
560 traversing it for "thread apply all". MVS */
561 target_find_new_threads ();
562
563 /* Save a copy of the command in case it is clobbered by
564 execute_command */
565 saved_cmd = xstrdup (cmd);
566 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
567 for (tp = thread_list; tp; tp = tp->next)
568 if (thread_alive (tp))
569 {
570 switch_to_thread (tp->ptid);
571 #ifdef HPUXHPPA
572 printf_filtered ("\nThread %d (%s):\n",
573 tp->num, target_tid_to_str (inferior_ptid));
574 #else
575 printf_filtered ("\nThread %d (%s):\n", tp->num,
576 target_pid_to_str (inferior_ptid));
577 #endif
578 execute_command (cmd, from_tty);
579 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
580 }
581
582 do_cleanups (saved_cmd_cleanup_chain);
583 do_cleanups (old_chain);
584 }
585
586 static void
587 thread_apply_command (char *tidlist, int from_tty)
588 {
589 char *cmd;
590 char *p;
591 struct cleanup *old_chain;
592 struct cleanup *saved_cmd_cleanup_chain;
593 char *saved_cmd;
594
595 if (tidlist == NULL || *tidlist == '\000')
596 error ("Please specify a thread ID list");
597
598 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
599
600 if (*cmd == '\000')
601 error ("Please specify a command following the thread ID list");
602
603 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
604
605 /* Save a copy of the command in case it is clobbered by
606 execute_command */
607 saved_cmd = xstrdup (cmd);
608 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
609 while (tidlist < cmd)
610 {
611 struct thread_info *tp;
612 int start, end;
613
614 start = strtol (tidlist, &p, 10);
615 if (p == tidlist)
616 error ("Error parsing %s", tidlist);
617 tidlist = p;
618
619 while (*tidlist == ' ' || *tidlist == '\t')
620 tidlist++;
621
622 if (*tidlist == '-') /* Got a range of IDs? */
623 {
624 tidlist++; /* Skip the - */
625 end = strtol (tidlist, &p, 10);
626 if (p == tidlist)
627 error ("Error parsing %s", tidlist);
628 tidlist = p;
629
630 while (*tidlist == ' ' || *tidlist == '\t')
631 tidlist++;
632 }
633 else
634 end = start;
635
636 for (; start <= end; start++)
637 {
638 tp = find_thread_id (start);
639
640 if (!tp)
641 warning ("Unknown thread %d.", start);
642 else if (!thread_alive (tp))
643 warning ("Thread %d has terminated.", start);
644 else
645 {
646 switch_to_thread (tp->ptid);
647 #ifdef HPUXHPPA
648 printf_filtered ("\nThread %d (%s):\n", tp->num,
649 target_tid_to_str (inferior_ptid));
650 #else
651 printf_filtered ("\nThread %d (%s):\n", tp->num,
652 target_pid_to_str (inferior_ptid));
653 #endif
654 execute_command (cmd, from_tty);
655 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
656 }
657 }
658 }
659
660 do_cleanups (saved_cmd_cleanup_chain);
661 do_cleanups (old_chain);
662 }
663
664 /* Switch to the specified thread. Will dispatch off to thread_apply_command
665 if prefix of arg is `apply'. */
666
667 static void
668 thread_command (char *tidstr, int from_tty)
669 {
670 if (!tidstr)
671 {
672 /* Don't generate an error, just say which thread is current. */
673 if (target_has_stack)
674 printf_filtered ("[Current thread is %d (%s)]\n",
675 pid_to_thread_id (inferior_ptid),
676 #if defined(HPUXHPPA)
677 target_tid_to_str (inferior_ptid)
678 #else
679 target_pid_to_str (inferior_ptid)
680 #endif
681 );
682 else
683 error ("No stack.");
684 return;
685 }
686
687 gdb_thread_select (uiout, tidstr);
688 }
689
690 static int
691 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
692 {
693 int num;
694 struct thread_info *tp;
695
696 num = value_as_long (parse_and_eval (tidstr));
697
698 tp = find_thread_id (num);
699
700 if (!tp)
701 error ("Thread ID %d not known.", num);
702
703 if (!thread_alive (tp))
704 error ("Thread ID %d has terminated.\n", num);
705
706 switch_to_thread (tp->ptid);
707
708 ui_out_text (uiout, "[Switching to thread ");
709 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
710 ui_out_text (uiout, " (");
711 #if defined(HPUXHPPA)
712 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
713 #else
714 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
715 #endif
716 ui_out_text (uiout, ")]");
717
718 print_stack_frame (deprecated_selected_frame,
719 frame_relative_level (deprecated_selected_frame), 1);
720 return GDB_RC_OK;
721 }
722
723 enum gdb_rc
724 gdb_thread_select (struct ui_out *uiout, char *tidstr)
725 {
726 return catch_exceptions (uiout, do_captured_thread_select, tidstr,
727 NULL, RETURN_MASK_ALL);
728 }
729
730 /* Commands with a prefix of `thread'. */
731 struct cmd_list_element *thread_cmd_list = NULL;
732
733 void
734 _initialize_thread (void)
735 {
736 static struct cmd_list_element *thread_apply_list = NULL;
737
738 add_info ("threads", info_threads_command,
739 "IDs of currently known threads.");
740
741 add_prefix_cmd ("thread", class_run, thread_command,
742 "Use this command to switch between threads.\n\
743 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1, &cmdlist);
744
745 add_prefix_cmd ("apply", class_run, thread_apply_command,
746 "Apply a command to a list of threads.",
747 &thread_apply_list, "apply ", 1, &thread_cmd_list);
748
749 add_cmd ("all", class_run, thread_apply_all_command,
750 "Apply a command to all threads.", &thread_apply_list);
751
752 if (!xdb_commands)
753 add_com_alias ("t", "thread", class_run, 1);
754 }
This page took 0.043622 seconds and 4 git commands to generate.