* mips-tdep.c (mips_eabi_push_dummy_call): Revert the last
[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 /* Definition of struct thread_info exported to gdbthread.h */
46
47 /* Prototypes for exported functions. */
48
49 void _initialize_thread (void);
50
51 /* Prototypes for local functions. */
52
53 static struct thread_info *thread_list = NULL;
54 static int highest_thread_num;
55
56 static struct thread_info *find_thread_id (int num);
57
58 static void thread_command (char *tidstr, int from_tty);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info *);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t);
64 static void switch_to_thread (ptid_t ptid);
65 static void prune_threads (void);
66 static struct cleanup *make_cleanup_restore_current_thread (ptid_t,
67 struct frame_id);
68
69 void
70 delete_step_resume_breakpoint (void *arg)
71 {
72 struct breakpoint **breakpointp = (struct breakpoint **) arg;
73 struct thread_info *tp;
74
75 if (*breakpointp != NULL)
76 {
77 delete_breakpoint (*breakpointp);
78 for (tp = thread_list; tp; tp = tp->next)
79 if (tp->step_resume_breakpoint == *breakpointp)
80 tp->step_resume_breakpoint = NULL;
81
82 *breakpointp = NULL;
83 }
84 }
85
86 static void
87 free_thread (struct thread_info *tp)
88 {
89 /* NOTE: this will take care of any left-over step_resume breakpoints,
90 but not any user-specified thread-specific breakpoints. */
91 if (tp->step_resume_breakpoint)
92 delete_breakpoint (tp->step_resume_breakpoint);
93
94 /* FIXME: do I ever need to call the back-end to give it a
95 chance at this private data before deleting the thread? */
96 if (tp->private)
97 xfree (tp->private);
98
99 xfree (tp);
100 }
101
102 void
103 init_thread_list (void)
104 {
105 struct thread_info *tp, *tpnext;
106
107 highest_thread_num = 0;
108 if (!thread_list)
109 return;
110
111 for (tp = thread_list; tp; tp = tpnext)
112 {
113 tpnext = tp->next;
114 free_thread (tp);
115 }
116
117 thread_list = NULL;
118 }
119
120 /* add_thread now returns a pointer to the new thread_info,
121 so that back_ends can initialize their private data. */
122
123 struct thread_info *
124 add_thread (ptid_t ptid)
125 {
126 struct thread_info *tp;
127
128 tp = (struct thread_info *) xmalloc (sizeof (*tp));
129 memset (tp, 0, sizeof (*tp));
130 tp->ptid = ptid;
131 tp->num = ++highest_thread_num;
132 tp->next = thread_list;
133 thread_list = tp;
134 return tp;
135 }
136
137 void
138 delete_thread (ptid_t ptid)
139 {
140 struct thread_info *tp, *tpprev;
141
142 tpprev = NULL;
143
144 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
145 if (ptid_equal (tp->ptid, ptid))
146 break;
147
148 if (!tp)
149 return;
150
151 if (tpprev)
152 tpprev->next = tp->next;
153 else
154 thread_list = tp->next;
155
156 free_thread (tp);
157 }
158
159 static struct thread_info *
160 find_thread_id (int num)
161 {
162 struct thread_info *tp;
163
164 for (tp = thread_list; tp; tp = tp->next)
165 if (tp->num == num)
166 return tp;
167
168 return NULL;
169 }
170
171 /* Find a thread_info by matching PTID. */
172 struct thread_info *
173 find_thread_pid (ptid_t ptid)
174 {
175 struct thread_info *tp;
176
177 for (tp = thread_list; tp; tp = tp->next)
178 if (ptid_equal (tp->ptid, ptid))
179 return tp;
180
181 return NULL;
182 }
183
184 /*
185 * Thread iterator function.
186 *
187 * Calls a callback function once for each thread, so long as
188 * the callback function returns false. If the callback function
189 * returns true, the iteration will end and the current thread
190 * will be returned. This can be useful for implementing a
191 * search for a thread with arbitrary attributes, or for applying
192 * some operation to every thread.
193 *
194 * FIXME: some of the existing functionality, such as
195 * "Thread apply all", might be rewritten using this functionality.
196 */
197
198 struct thread_info *
199 iterate_over_threads (int (*callback) (struct thread_info *, void *),
200 void *data)
201 {
202 struct thread_info *tp;
203
204 for (tp = thread_list; tp; tp = tp->next)
205 if ((*callback) (tp, data))
206 return tp;
207
208 return NULL;
209 }
210
211 int
212 valid_thread_id (int num)
213 {
214 struct thread_info *tp;
215
216 for (tp = thread_list; tp; tp = tp->next)
217 if (tp->num == num)
218 return 1;
219
220 return 0;
221 }
222
223 int
224 pid_to_thread_id (ptid_t ptid)
225 {
226 struct thread_info *tp;
227
228 for (tp = thread_list; tp; tp = tp->next)
229 if (ptid_equal (tp->ptid, ptid))
230 return tp->num;
231
232 return 0;
233 }
234
235 ptid_t
236 thread_id_to_pid (int num)
237 {
238 struct thread_info *thread = find_thread_id (num);
239 if (thread)
240 return thread->ptid;
241 else
242 return pid_to_ptid (-1);
243 }
244
245 int
246 in_thread_list (ptid_t ptid)
247 {
248 struct thread_info *tp;
249
250 for (tp = thread_list; tp; tp = tp->next)
251 if (ptid_equal (tp->ptid, ptid))
252 return 1;
253
254 return 0; /* Never heard of 'im */
255 }
256
257 /* Print a list of thread ids currently known, and the total number of
258 threads. To be used from within catch_errors. */
259 static int
260 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
261 {
262 struct thread_info *tp;
263 int num = 0;
264 struct cleanup *cleanup_chain;
265
266 prune_threads ();
267 target_find_new_threads ();
268
269 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
270
271 for (tp = thread_list; tp; tp = tp->next)
272 {
273 num++;
274 ui_out_field_int (uiout, "thread-id", tp->num);
275 }
276
277 do_cleanups (cleanup_chain);
278 ui_out_field_int (uiout, "number-of-threads", num);
279 return GDB_RC_OK;
280 }
281
282 /* Official gdblib interface function to get a list of thread ids and
283 the total number. */
284 enum gdb_rc
285 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
286 {
287 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
288 error_message, RETURN_MASK_ALL) < 0)
289 return GDB_RC_FAIL;
290 return GDB_RC_OK;
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 reinit_frame_cache ();
465 registers_changed ();
466 stop_pc = read_pc ();
467 }
468
469 static void
470 restore_current_thread (ptid_t ptid)
471 {
472 if (!ptid_equal (ptid, inferior_ptid))
473 {
474 switch_to_thread (ptid);
475 }
476 }
477
478 static void
479 restore_selected_frame (struct frame_id a_frame_id)
480 {
481 struct frame_info *selected_frame_info = NULL;
482
483 if (frame_id_eq (a_frame_id, null_frame_id))
484 return;
485
486 if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL)
487 {
488 select_frame (selected_frame_info);
489 }
490 }
491
492 struct current_thread_cleanup
493 {
494 ptid_t inferior_ptid;
495 struct frame_id selected_frame_id;
496 };
497
498 static void
499 do_restore_current_thread_cleanup (void *arg)
500 {
501 struct current_thread_cleanup *old = arg;
502 restore_current_thread (old->inferior_ptid);
503 restore_selected_frame (old->selected_frame_id);
504 xfree (old);
505 }
506
507 static struct cleanup *
508 make_cleanup_restore_current_thread (ptid_t inferior_ptid,
509 struct frame_id a_frame_id)
510 {
511 struct current_thread_cleanup *old
512 = xmalloc (sizeof (struct current_thread_cleanup));
513 old->inferior_ptid = inferior_ptid;
514 old->selected_frame_id = a_frame_id;
515 return make_cleanup (do_restore_current_thread_cleanup, old);
516 }
517
518 /* Apply a GDB command to a list of threads. List syntax is a whitespace
519 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
520 of two numbers seperated by a hyphen. Examples:
521
522 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
523 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
524 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
525 */
526
527 static void
528 thread_apply_all_command (char *cmd, int from_tty)
529 {
530 struct thread_info *tp;
531 struct cleanup *old_chain;
532 struct cleanup *saved_cmd_cleanup_chain;
533 char *saved_cmd;
534 struct frame_id saved_frame_id;
535 ptid_t current_ptid;
536 int thread_has_changed = 0;
537
538 if (cmd == NULL || *cmd == '\000')
539 error (_("Please specify a command following the thread ID list"));
540
541 current_ptid = inferior_ptid;
542 saved_frame_id = get_frame_id (get_selected_frame (NULL));
543 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
544
545 /* It is safe to update the thread list now, before
546 traversing it for "thread apply all". MVS */
547 target_find_new_threads ();
548
549 /* Save a copy of the command in case it is clobbered by
550 execute_command */
551 saved_cmd = xstrdup (cmd);
552 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
553 for (tp = thread_list; tp; tp = tp->next)
554 if (thread_alive (tp))
555 {
556 switch_to_thread (tp->ptid);
557 printf_filtered (_("\nThread %d (%s):\n"),
558 tp->num, target_tid_to_str (inferior_ptid));
559 execute_command (cmd, from_tty);
560 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
561 }
562
563 if (!ptid_equal (current_ptid, inferior_ptid))
564 thread_has_changed = 1;
565
566 do_cleanups (saved_cmd_cleanup_chain);
567 do_cleanups (old_chain);
568 /* Print stack frame only if we changed thread. */
569 if (thread_has_changed)
570 print_stack_frame (get_current_frame (), 1, SRC_LINE);
571
572 }
573
574 static void
575 thread_apply_command (char *tidlist, int from_tty)
576 {
577 char *cmd;
578 char *p;
579 struct cleanup *old_chain;
580 struct cleanup *saved_cmd_cleanup_chain;
581 char *saved_cmd;
582 struct frame_id saved_frame_id;
583 ptid_t current_ptid;
584 int thread_has_changed = 0;
585
586 if (tidlist == NULL || *tidlist == '\000')
587 error (_("Please specify a thread ID list"));
588
589 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
590
591 if (*cmd == '\000')
592 error (_("Please specify a command following the thread ID list"));
593
594 current_ptid = inferior_ptid;
595 saved_frame_id = get_frame_id (get_selected_frame (NULL));
596 old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
597
598 /* Save a copy of the command in case it is clobbered by
599 execute_command */
600 saved_cmd = xstrdup (cmd);
601 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
602 while (tidlist < cmd)
603 {
604 struct thread_info *tp;
605 int start, end;
606
607 start = 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 if (*tidlist == '-') /* Got a range of IDs? */
616 {
617 tidlist++; /* Skip the - */
618 end = strtol (tidlist, &p, 10);
619 if (p == tidlist)
620 error (_("Error parsing %s"), tidlist);
621 tidlist = p;
622
623 while (*tidlist == ' ' || *tidlist == '\t')
624 tidlist++;
625 }
626 else
627 end = start;
628
629 for (; start <= end; start++)
630 {
631 tp = find_thread_id (start);
632
633 if (!tp)
634 warning (_("Unknown thread %d."), start);
635 else if (!thread_alive (tp))
636 warning (_("Thread %d has terminated."), start);
637 else
638 {
639 switch_to_thread (tp->ptid);
640 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
641 target_tid_to_str (inferior_ptid));
642 execute_command (cmd, from_tty);
643 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
644 }
645 }
646 }
647
648 if (!ptid_equal (current_ptid, inferior_ptid))
649 thread_has_changed = 1;
650
651 do_cleanups (saved_cmd_cleanup_chain);
652 do_cleanups (old_chain);
653 /* Print stack frame only if we changed thread. */
654 if (thread_has_changed)
655 print_stack_frame (get_current_frame (), 1, SRC_LINE);
656 }
657
658 /* Switch to the specified thread. Will dispatch off to thread_apply_command
659 if prefix of arg is `apply'. */
660
661 static void
662 thread_command (char *tidstr, int from_tty)
663 {
664 if (!tidstr)
665 {
666 /* Don't generate an error, just say which thread is current. */
667 if (target_has_stack)
668 printf_filtered (_("[Current thread is %d (%s)]\n"),
669 pid_to_thread_id (inferior_ptid),
670 target_tid_to_str (inferior_ptid));
671 else
672 error (_("No stack."));
673 return;
674 }
675
676 gdb_thread_select (uiout, tidstr, NULL);
677 }
678
679 static int
680 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
681 {
682 int num;
683 struct thread_info *tp;
684
685 num = value_as_long (parse_and_eval (tidstr));
686
687 tp = find_thread_id (num);
688
689 if (!tp)
690 error (_("Thread ID %d not known."), num);
691
692 if (!thread_alive (tp))
693 error (_("Thread ID %d has terminated."), num);
694
695 switch_to_thread (tp->ptid);
696
697 ui_out_text (uiout, "[Switching to thread ");
698 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
699 ui_out_text (uiout, " (");
700 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
701 ui_out_text (uiout, ")]");
702
703 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
704 return GDB_RC_OK;
705 }
706
707 enum gdb_rc
708 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
709 {
710 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
711 error_message, RETURN_MASK_ALL) < 0)
712 return GDB_RC_FAIL;
713 return GDB_RC_OK;
714 }
715
716 /* Commands with a prefix of `thread'. */
717 struct cmd_list_element *thread_cmd_list = NULL;
718
719 void
720 _initialize_thread (void)
721 {
722 static struct cmd_list_element *thread_apply_list = NULL;
723
724 add_info ("threads", info_threads_command,
725 _("IDs of currently known threads."));
726
727 add_prefix_cmd ("thread", class_run, thread_command, _("\
728 Use this command to switch between threads.\n\
729 The new thread ID must be currently known."),
730 &thread_cmd_list, "thread ", 1, &cmdlist);
731
732 add_prefix_cmd ("apply", class_run, thread_apply_command,
733 _("Apply a command to a list of threads."),
734 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
735
736 add_cmd ("all", class_run, thread_apply_all_command,
737 _("Apply a command to all threads."), &thread_apply_list);
738
739 if (!xdb_commands)
740 add_com_alias ("t", "thread", class_run, 1);
741 }
This page took 0.07383 seconds and 4 git commands to generate.