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