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