2008-08-18 Pedro Alves <pedro@codesourcery.com>
[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, 2008 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 #include "observer.h"
43 #include "annotate.h"
44 #include "cli/cli-decode.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 void thread_command (char *tidstr, int from_tty);
58 static void thread_apply_all_command (char *, int);
59 static int thread_alive (struct thread_info *);
60 static void info_threads_command (char *, int);
61 static void thread_apply_command (char *, int);
62 static void restore_current_thread (ptid_t);
63 static void prune_threads (void);
64
65 /* Frontend view of the thread state. Possible extensions: stepping,
66 finishing, until(ling),... */
67 enum thread_state
68 {
69 THREAD_STOPPED,
70 THREAD_RUNNING,
71 THREAD_EXITED,
72 };
73
74 static enum thread_state main_thread_state = THREAD_STOPPED;
75 static int main_thread_executing = 0;
76
77 void
78 delete_step_resume_breakpoint (void *arg)
79 {
80 struct breakpoint **breakpointp = (struct breakpoint **) arg;
81 struct thread_info *tp;
82
83 if (*breakpointp != NULL)
84 {
85 delete_breakpoint (*breakpointp);
86 for (tp = thread_list; tp; tp = tp->next)
87 if (tp->step_resume_breakpoint == *breakpointp)
88 tp->step_resume_breakpoint = NULL;
89
90 *breakpointp = NULL;
91 }
92 }
93
94 static void
95 clear_thread_inferior_resources (struct thread_info *tp)
96 {
97 /* NOTE: this will take care of any left-over step_resume breakpoints,
98 but not any user-specified thread-specific breakpoints. We can not
99 delete the breakpoint straight-off, because the inferior might not
100 be stopped at the moment. */
101 if (tp->step_resume_breakpoint)
102 {
103 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
104 tp->step_resume_breakpoint = NULL;
105 }
106
107 bpstat_clear (&tp->stop_bpstat);
108 }
109
110 static void
111 free_thread (struct thread_info *tp)
112 {
113 clear_thread_inferior_resources (tp);
114
115 /* FIXME: do I ever need to call the back-end to give it a
116 chance at this private data before deleting the thread? */
117 if (tp->private)
118 xfree (tp->private);
119
120 xfree (tp);
121 }
122
123 void
124 init_thread_list (void)
125 {
126 struct thread_info *tp, *tpnext;
127
128 highest_thread_num = 0;
129 main_thread_state = THREAD_STOPPED;
130 main_thread_executing = 0;
131
132 if (!thread_list)
133 return;
134
135 for (tp = thread_list; tp; tp = tpnext)
136 {
137 tpnext = tp->next;
138 free_thread (tp);
139 }
140
141 thread_list = NULL;
142 }
143
144 struct thread_info *
145 add_thread_silent (ptid_t ptid)
146 {
147 struct thread_info *tp;
148
149 tp = find_thread_pid (ptid);
150 if (tp)
151 /* Found an old thread with the same id. It has to be dead,
152 otherwise we wouldn't be adding a new thread with the same id.
153 The OS is reusing this id --- delete it, and recreate a new
154 one. */
155 {
156 /* In addition to deleting the thread, if this is the current
157 thread, then we need to also get rid of the current infrun
158 context, and take care that delete_thread doesn't really
159 delete the thread if it is inferior_ptid. Create a new
160 template thread in the list with an invalid ptid, context
161 switch to it, delete the original thread, reset the new
162 thread's ptid, and switch to it. */
163
164 if (ptid_equal (inferior_ptid, ptid))
165 {
166 tp = xmalloc (sizeof (*tp));
167 memset (tp, 0, sizeof (*tp));
168 tp->ptid = minus_one_ptid;
169 tp->num = ++highest_thread_num;
170 tp->next = thread_list;
171 thread_list = tp;
172 context_switch_to (minus_one_ptid);
173
174 /* Now we can delete it. */
175 delete_thread (ptid);
176
177 /* Since the context is already set to this new thread,
178 reset its ptid, and reswitch inferior_ptid to it. */
179 tp->ptid = ptid;
180 switch_to_thread (ptid);
181
182 observer_notify_new_thread (tp);
183
184 /* All done. */
185 return tp;
186 }
187 else
188 /* Just go ahead and delete it. */
189 delete_thread (ptid);
190 }
191
192 tp = (struct thread_info *) xmalloc (sizeof (*tp));
193 memset (tp, 0, sizeof (*tp));
194 tp->ptid = ptid;
195 tp->num = ++highest_thread_num;
196 tp->next = thread_list;
197 thread_list = tp;
198
199 observer_notify_new_thread (tp);
200
201 return tp;
202 }
203
204 struct thread_info *
205 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
206 {
207 struct thread_info *result = add_thread_silent (ptid);
208
209 result->private = private;
210
211 if (print_thread_events)
212 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
213
214 annotate_new_thread ();
215 return result;
216 }
217
218 struct thread_info *
219 add_thread (ptid_t ptid)
220 {
221 return add_thread_with_info (ptid, NULL);
222 }
223
224 /* Delete thread PTID. If SILENT, don't notify the observer of this
225 exit. */
226 static void
227 delete_thread_1 (ptid_t ptid, int silent)
228 {
229 struct thread_info *tp, *tpprev;
230
231 tpprev = NULL;
232
233 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
234 if (ptid_equal (tp->ptid, ptid))
235 break;
236
237 if (!tp)
238 return;
239
240 /* If this is the current thread, or there's code out there that
241 relies on it existing (refcount > 0) we can't delete yet. Mark
242 it as exited, and notify it. */
243 if (tp->refcount > 0
244 || ptid_equal (tp->ptid, inferior_ptid))
245 {
246 if (tp->state_ != THREAD_EXITED)
247 {
248 if (!silent)
249 observer_notify_thread_exit (tp);
250
251 /* Tag it as exited. */
252 tp->state_ = THREAD_EXITED;
253
254 /* Clear breakpoints, etc. associated with this thread. */
255 clear_thread_inferior_resources (tp);
256 }
257
258 /* Will be really deleted some other time. */
259 return;
260 }
261
262 if (tpprev)
263 tpprev->next = tp->next;
264 else
265 thread_list = tp->next;
266
267 /* Notify thread exit, but only if we haven't already. */
268 if (!silent && tp->state_ != THREAD_EXITED)
269 observer_notify_thread_exit (tp);
270
271 free_thread (tp);
272 }
273
274 /* Delete thread PTID and notify of thread exit. If this is
275 inferior_ptid, don't actually delete it, but tag it as exited and
276 do the notification. If PTID is the user selected thread, clear
277 it. */
278 void
279 delete_thread (ptid_t ptid)
280 {
281 delete_thread_1 (ptid, 0 /* not silent */);
282 }
283
284 void
285 delete_thread_silent (ptid_t ptid)
286 {
287 delete_thread_1 (ptid, 1 /* silent */);
288 }
289
290 struct thread_info *
291 find_thread_id (int num)
292 {
293 struct thread_info *tp;
294
295 for (tp = thread_list; tp; tp = tp->next)
296 if (tp->num == num)
297 return tp;
298
299 return NULL;
300 }
301
302 /* Find a thread_info by matching PTID. */
303 struct thread_info *
304 find_thread_pid (ptid_t ptid)
305 {
306 struct thread_info *tp;
307
308 for (tp = thread_list; tp; tp = tp->next)
309 if (ptid_equal (tp->ptid, ptid))
310 return tp;
311
312 return NULL;
313 }
314
315 /*
316 * Thread iterator function.
317 *
318 * Calls a callback function once for each thread, so long as
319 * the callback function returns false. If the callback function
320 * returns true, the iteration will end and the current thread
321 * will be returned. This can be useful for implementing a
322 * search for a thread with arbitrary attributes, or for applying
323 * some operation to every thread.
324 *
325 * FIXME: some of the existing functionality, such as
326 * "Thread apply all", might be rewritten using this functionality.
327 */
328
329 struct thread_info *
330 iterate_over_threads (int (*callback) (struct thread_info *, void *),
331 void *data)
332 {
333 struct thread_info *tp, *next;
334
335 for (tp = thread_list; tp; tp = next)
336 {
337 next = tp->next;
338 if ((*callback) (tp, data))
339 return tp;
340 }
341
342 return NULL;
343 }
344
345 int
346 thread_count (void)
347 {
348 int result = 0;
349 struct thread_info *tp;
350
351 for (tp = thread_list; tp; tp = tp->next)
352 ++result;
353
354 return result;
355 }
356
357 int
358 valid_thread_id (int num)
359 {
360 struct thread_info *tp;
361
362 for (tp = thread_list; tp; tp = tp->next)
363 if (tp->num == num)
364 return 1;
365
366 return 0;
367 }
368
369 int
370 pid_to_thread_id (ptid_t ptid)
371 {
372 struct thread_info *tp;
373
374 for (tp = thread_list; tp; tp = tp->next)
375 if (ptid_equal (tp->ptid, ptid))
376 return tp->num;
377
378 return 0;
379 }
380
381 ptid_t
382 thread_id_to_pid (int num)
383 {
384 struct thread_info *thread = find_thread_id (num);
385 if (thread)
386 return thread->ptid;
387 else
388 return pid_to_ptid (-1);
389 }
390
391 int
392 in_thread_list (ptid_t ptid)
393 {
394 struct thread_info *tp;
395
396 for (tp = thread_list; tp; tp = tp->next)
397 if (ptid_equal (tp->ptid, ptid))
398 return 1;
399
400 return 0; /* Never heard of 'im */
401 }
402
403 /* Print a list of thread ids currently known, and the total number of
404 threads. To be used from within catch_errors. */
405 static int
406 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
407 {
408 struct thread_info *tp;
409 int num = 0;
410 struct cleanup *cleanup_chain;
411
412 prune_threads ();
413 target_find_new_threads ();
414
415 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
416
417 for (tp = thread_list; tp; tp = tp->next)
418 {
419 if (tp->state_ == THREAD_EXITED)
420 continue;
421 num++;
422 ui_out_field_int (uiout, "thread-id", tp->num);
423 }
424
425 do_cleanups (cleanup_chain);
426 ui_out_field_int (uiout, "number-of-threads", num);
427 return GDB_RC_OK;
428 }
429
430 /* Official gdblib interface function to get a list of thread ids and
431 the total number. */
432 enum gdb_rc
433 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
434 {
435 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
436 error_message, RETURN_MASK_ALL) < 0)
437 return GDB_RC_FAIL;
438 return GDB_RC_OK;
439 }
440
441 /* Load infrun state for the thread PID. */
442
443 void
444 load_infrun_state (ptid_t ptid,
445 CORE_ADDR *prev_pc,
446 int *trap_expected,
447 struct breakpoint **step_resume_breakpoint,
448 CORE_ADDR *step_range_start,
449 CORE_ADDR *step_range_end,
450 struct frame_id *step_frame_id,
451 int *stepping_over_breakpoint,
452 int *stepping_through_solib_after_catch,
453 bpstat *stepping_through_solib_catchpoints,
454 int *current_line,
455 struct symtab **current_symtab,
456 struct continuation **continuations,
457 struct continuation **intermediate_continuations,
458 int *proceed_to_finish,
459 enum step_over_calls_kind *step_over_calls,
460 int *stop_step,
461 int *step_multi,
462 enum target_signal *stop_signal,
463 bpstat *stop_bpstat)
464 {
465 struct thread_info *tp;
466
467 /* If we can't find the thread, then we're debugging a single threaded
468 process. No need to do anything in that case. */
469 tp = find_thread_id (pid_to_thread_id (ptid));
470 if (tp == NULL)
471 return;
472
473 *prev_pc = tp->prev_pc;
474 *trap_expected = tp->trap_expected;
475 *step_resume_breakpoint = tp->step_resume_breakpoint;
476 *step_range_start = tp->step_range_start;
477 *step_range_end = tp->step_range_end;
478 *step_frame_id = tp->step_frame_id;
479 *stepping_over_breakpoint = tp->stepping_over_breakpoint;
480 *stepping_through_solib_after_catch =
481 tp->stepping_through_solib_after_catch;
482 *stepping_through_solib_catchpoints =
483 tp->stepping_through_solib_catchpoints;
484 *current_line = tp->current_line;
485 *current_symtab = tp->current_symtab;
486
487 /* In all-stop mode, these are global state, while in non-stop mode,
488 they are per thread. */
489 if (non_stop)
490 {
491 *continuations = tp->continuations;
492 tp->continuations = NULL;
493 *intermediate_continuations = tp->intermediate_continuations;
494 tp->intermediate_continuations = NULL;
495 *proceed_to_finish = tp->proceed_to_finish;
496 *step_over_calls = tp->step_over_calls;
497 *stop_step = tp->stop_step;
498 *step_multi = tp->step_multi;
499 *stop_signal = tp->stop_signal;
500
501 /* Swap instead of copy, so we only have to update one of
502 them. */
503 *stop_bpstat = tp->stop_bpstat;
504 tp->stop_bpstat = 0;
505 }
506 }
507
508 /* Save infrun state for the thread PID. */
509
510 void
511 save_infrun_state (ptid_t ptid,
512 CORE_ADDR prev_pc,
513 int trap_expected,
514 struct breakpoint *step_resume_breakpoint,
515 CORE_ADDR step_range_start,
516 CORE_ADDR step_range_end,
517 const struct frame_id *step_frame_id,
518 int stepping_over_breakpoint,
519 int stepping_through_solib_after_catch,
520 bpstat stepping_through_solib_catchpoints,
521 int current_line,
522 struct symtab *current_symtab,
523 struct continuation *continuations,
524 struct continuation *intermediate_continuations,
525 int proceed_to_finish,
526 enum step_over_calls_kind step_over_calls,
527 int stop_step,
528 int step_multi,
529 enum target_signal stop_signal,
530 bpstat stop_bpstat)
531 {
532 struct thread_info *tp;
533
534 /* If we can't find the thread, then we're debugging a single-threaded
535 process. Nothing to do in that case. */
536 tp = find_thread_id (pid_to_thread_id (ptid));
537 if (tp == NULL)
538 return;
539
540 tp->prev_pc = prev_pc;
541 tp->trap_expected = trap_expected;
542 tp->step_resume_breakpoint = step_resume_breakpoint;
543 tp->step_range_start = step_range_start;
544 tp->step_range_end = step_range_end;
545 tp->step_frame_id = (*step_frame_id);
546 tp->stepping_over_breakpoint = stepping_over_breakpoint;
547 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
548 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
549 tp->current_line = current_line;
550 tp->current_symtab = current_symtab;
551
552 /* In all-stop mode, these are global state, while in non-stop mode,
553 they are per thread. */
554 if (non_stop)
555 {
556 tp->continuations = continuations;
557 tp->intermediate_continuations = intermediate_continuations;
558 tp->proceed_to_finish = proceed_to_finish;
559 tp->step_over_calls = step_over_calls;
560 tp->stop_step = stop_step;
561 tp->step_multi = step_multi;
562 tp->stop_signal = stop_signal;
563 tp->stop_bpstat = stop_bpstat;
564 }
565 }
566
567 /* Return true if TP is an active thread. */
568 static int
569 thread_alive (struct thread_info *tp)
570 {
571 if (tp->state_ == THREAD_EXITED)
572 return 0;
573 if (!target_thread_alive (tp->ptid))
574 return 0;
575 return 1;
576 }
577
578 static void
579 prune_threads (void)
580 {
581 struct thread_info *tp, *next;
582
583 for (tp = thread_list; tp; tp = next)
584 {
585 next = tp->next;
586 if (!thread_alive (tp))
587 delete_thread (tp->ptid);
588 }
589 }
590
591 void
592 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
593 {
594 struct thread_info * tp = find_thread_pid (old_ptid);
595 tp->ptid = new_ptid;
596
597 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
598 }
599
600 void
601 set_running (ptid_t ptid, int running)
602 {
603 struct thread_info *tp;
604
605 if (!thread_list)
606 {
607 /* This is one of the targets that does not add main
608 thread to the thread list. Just use a single
609 global flag to indicate that a thread is running.
610
611 This problem is unique to ST programs. For MT programs,
612 the main thread is always present in the thread list. If it's
613 not, the first call to context_switch will mess up GDB internal
614 state. */
615 if (running
616 && main_thread_state != THREAD_RUNNING
617 && !suppress_resume_observer)
618 observer_notify_target_resumed (ptid);
619 main_thread_state = running ? THREAD_RUNNING : THREAD_STOPPED;
620 return;
621 }
622
623 /* We try not to notify the observer if no thread has actually changed
624 the running state -- merely to reduce the number of messages to
625 frontend. Frontend is supposed to handle multiple *running just fine. */
626 if (PIDGET (ptid) == -1)
627 {
628 int any_started = 0;
629 for (tp = thread_list; tp; tp = tp->next)
630 {
631 if (tp->state_ == THREAD_EXITED)
632 continue;
633 if (running && tp->state_ == THREAD_STOPPED)
634 any_started = 1;
635 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
636 }
637 if (any_started && !suppress_resume_observer)
638 observer_notify_target_resumed (ptid);
639 }
640 else
641 {
642 int started = 0;
643 tp = find_thread_pid (ptid);
644 gdb_assert (tp);
645 gdb_assert (tp->state_ != THREAD_EXITED);
646 if (running && tp->state_ == THREAD_STOPPED)
647 started = 1;
648 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
649 if (started && !suppress_resume_observer)
650 observer_notify_target_resumed (ptid);
651 }
652 }
653
654 static int
655 is_thread_state (ptid_t ptid, enum thread_state state)
656 {
657 struct thread_info *tp;
658
659 if (!target_has_execution)
660 return 0;
661
662 if (!thread_list)
663 return main_thread_state == state;
664
665 tp = find_thread_pid (ptid);
666 gdb_assert (tp);
667 return tp->state_ == state;
668 }
669
670 int
671 is_stopped (ptid_t ptid)
672 {
673 /* Without execution, this property is always true. */
674 if (!target_has_execution)
675 return 1;
676
677 return is_thread_state (ptid, THREAD_STOPPED);
678 }
679
680 int
681 is_exited (ptid_t ptid)
682 {
683 /* Without execution, this property is always false. */
684 if (!target_has_execution)
685 return 0;
686
687 return is_thread_state (ptid, THREAD_EXITED);
688 }
689
690 int
691 is_running (ptid_t ptid)
692 {
693 /* Without execution, this property is always false. */
694 if (!target_has_execution)
695 return 0;
696
697 return is_thread_state (ptid, THREAD_RUNNING);
698 }
699
700 int
701 any_running (void)
702 {
703 struct thread_info *tp;
704
705 if (!target_has_execution)
706 return 0;
707
708 if (!thread_list)
709 return main_thread_state == THREAD_RUNNING;
710
711 for (tp = thread_list; tp; tp = tp->next)
712 if (tp->state_ == THREAD_RUNNING)
713 return 1;
714
715 return 0;
716 }
717
718 int
719 is_executing (ptid_t ptid)
720 {
721 struct thread_info *tp;
722
723 if (!target_has_execution)
724 return 0;
725
726 if (!thread_list)
727 return main_thread_executing;
728
729 tp = find_thread_pid (ptid);
730 gdb_assert (tp);
731 return tp->executing_;
732 }
733
734 void
735 set_executing (ptid_t ptid, int executing)
736 {
737 struct thread_info *tp;
738
739 if (!thread_list)
740 {
741 /* This target does not add the main thread to the thread list.
742 Use a global flag to indicate that the thread is
743 executing. */
744 main_thread_executing = executing;
745 return;
746 }
747
748 if (PIDGET (ptid) == -1)
749 {
750 for (tp = thread_list; tp; tp = tp->next)
751 tp->executing_ = executing;
752 }
753 else
754 {
755 tp = find_thread_pid (ptid);
756 gdb_assert (tp);
757 tp->executing_ = executing;
758 }
759 }
760
761 /* Prints the list of threads and their details on UIOUT.
762 This is a version of 'info_thread_command' suitable for
763 use from MI.
764 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
765 that should be printed. Otherwise, all threads are
766 printed. */
767 void
768 print_thread_info (struct ui_out *uiout, int requested_thread)
769 {
770 struct thread_info *tp;
771 ptid_t current_ptid;
772 struct cleanup *old_chain;
773 char *extra_info;
774 int current_thread = -1;
775
776 prune_threads ();
777 target_find_new_threads ();
778 current_ptid = inferior_ptid;
779
780 /* We'll be switching threads temporarily. */
781 old_chain = make_cleanup_restore_current_thread ();
782
783 make_cleanup_ui_out_list_begin_end (uiout, "threads");
784 for (tp = thread_list; tp; tp = tp->next)
785 {
786 struct cleanup *chain2;
787
788 if (requested_thread != -1 && tp->num != requested_thread)
789 continue;
790
791 if (ptid_equal (tp->ptid, current_ptid))
792 current_thread = tp->num;
793
794 if (tp->state_ == THREAD_EXITED)
795 continue;
796
797 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
798
799 if (ptid_equal (tp->ptid, current_ptid))
800 ui_out_text (uiout, "* ");
801 else
802 ui_out_text (uiout, " ");
803
804 ui_out_field_int (uiout, "id", tp->num);
805 ui_out_text (uiout, " ");
806 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
807
808 if (tp->state_ != THREAD_EXITED)
809 {
810 extra_info = target_extra_thread_info (tp);
811 if (extra_info)
812 {
813 ui_out_text (uiout, " (");
814 ui_out_field_string (uiout, "details", extra_info);
815 ui_out_text (uiout, ")");
816 }
817 ui_out_text (uiout, " ");
818 }
819
820 if (tp->state_ == THREAD_RUNNING)
821 ui_out_text (uiout, "(running)\n");
822 else
823 {
824 /* The switch below puts us at the top of the stack (leaf
825 frame). */
826 switch_to_thread (tp->ptid);
827 print_stack_frame (get_selected_frame (NULL),
828 /* For MI output, print frame level. */
829 ui_out_is_mi_like_p (uiout),
830 LOCATION);
831 }
832
833 if (ui_out_is_mi_like_p (uiout))
834 {
835 char *state = "stopped";
836 if (tp->state_ == THREAD_EXITED)
837 state = "exited";
838 else if (tp->state_ == THREAD_RUNNING)
839 state = "running";
840 ui_out_field_string (uiout, "state", state);
841 }
842
843 do_cleanups (chain2);
844 }
845
846 /* Restores the current thread and the frame selected before
847 the "info threads" command. */
848 do_cleanups (old_chain);
849
850 if (requested_thread == -1)
851 {
852 gdb_assert (current_thread != -1
853 || !thread_list);
854 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
855 ui_out_field_int (uiout, "current-thread-id", current_thread);
856
857 if (current_thread != -1 && is_exited (current_ptid))
858 ui_out_message (uiout, 0, "\n\
859 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
860 current_thread);
861 }
862 }
863
864
865 /* Print information about currently known threads
866
867 * Note: this has the drawback that it _really_ switches
868 * threads, which frees the frame cache. A no-side
869 * effects info-threads command would be nicer.
870 */
871
872 static void
873 info_threads_command (char *arg, int from_tty)
874 {
875 print_thread_info (uiout, -1);
876 }
877
878 /* Switch from one thread to another. */
879
880 void
881 switch_to_thread (ptid_t ptid)
882 {
883 if (ptid_equal (ptid, inferior_ptid))
884 return;
885
886 inferior_ptid = ptid;
887 reinit_frame_cache ();
888 registers_changed ();
889
890 /* We don't check for is_stopped, because we're called at times
891 while in the TARGET_RUNNING state, e.g., while handling an
892 internal event. */
893 if (!is_exited (ptid) && !is_executing (ptid))
894 stop_pc = read_pc ();
895 else
896 stop_pc = ~(CORE_ADDR) 0;
897 }
898
899 static void
900 restore_current_thread (ptid_t ptid)
901 {
902 if (!ptid_equal (ptid, inferior_ptid))
903 {
904 if (non_stop)
905 context_switch_to (ptid);
906 else
907 switch_to_thread (ptid);
908 }
909 }
910
911 static void
912 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
913 {
914 struct frame_info *frame = NULL;
915 int count;
916
917 gdb_assert (frame_level >= 0);
918
919 /* Restore by level first, check if the frame id is the same as
920 expected. If that fails, try restoring by frame id. If that
921 fails, nothing to do, just warn the user. */
922
923 count = frame_level;
924 frame = find_relative_frame (get_current_frame (), &count);
925 if (count == 0
926 && frame != NULL
927 /* Either the frame ids match, of they're both invalid. The
928 latter case is not failsafe, but since it's highly unlikely
929 the search by level finds the wrong frame, it's 99.9(9)% of
930 the time (for all practical purposes) safe. */
931 && (frame_id_eq (get_frame_id (frame), a_frame_id)
932 /* Note: could be better to check every frame_id
933 member for equality here. */
934 || (!frame_id_p (get_frame_id (frame))
935 && !frame_id_p (a_frame_id))))
936 {
937 /* Cool, all is fine. */
938 select_frame (frame);
939 return;
940 }
941
942 frame = frame_find_by_id (a_frame_id);
943 if (frame != NULL)
944 {
945 /* Cool, refound it. */
946 select_frame (frame);
947 return;
948 }
949
950 /* Nothing else to do, the frame layout really changed. Select the
951 innermost stack frame. */
952 select_frame (get_current_frame ());
953
954 /* Warn the user. */
955 if (!ui_out_is_mi_like_p (uiout))
956 {
957 warning (_("\
958 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
959 frame_level);
960 /* For MI, we should probably have a notification about
961 current frame change. But this error is not very
962 likely, so don't bother for now. */
963 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
964 }
965 }
966
967 struct current_thread_cleanup
968 {
969 ptid_t inferior_ptid;
970 struct frame_id selected_frame_id;
971 int selected_frame_level;
972 int was_stopped;
973 };
974
975 static void
976 do_restore_current_thread_cleanup (void *arg)
977 {
978 struct thread_info *tp;
979 struct current_thread_cleanup *old = arg;
980 restore_current_thread (old->inferior_ptid);
981
982 /* The running state of the originally selected thread may have
983 changed, so we have to recheck it here. */
984 if (old->was_stopped
985 && is_stopped (inferior_ptid)
986 && target_has_registers
987 && target_has_stack
988 && target_has_memory)
989 restore_selected_frame (old->selected_frame_id,
990 old->selected_frame_level);
991 }
992
993 static void
994 restore_current_thread_cleanup_dtor (void *arg)
995 {
996 struct current_thread_cleanup *old = arg;
997 struct thread_info *tp;
998 tp = find_thread_pid (old->inferior_ptid);
999 if (tp)
1000 tp->refcount--;
1001 xfree (old);
1002 }
1003
1004 struct cleanup *
1005 make_cleanup_restore_current_thread (void)
1006 {
1007 struct thread_info *tp;
1008 struct frame_info *frame;
1009 struct current_thread_cleanup *old;
1010
1011 old = xmalloc (sizeof (struct current_thread_cleanup));
1012 old->inferior_ptid = inferior_ptid;
1013 old->was_stopped = is_stopped (inferior_ptid);
1014 if (old->was_stopped
1015 && target_has_registers
1016 && target_has_stack
1017 && target_has_memory)
1018 frame = get_selected_frame (NULL);
1019 else
1020 frame = NULL;
1021
1022 old->selected_frame_id = get_frame_id (frame);
1023 old->selected_frame_level = frame_relative_level (frame);
1024
1025 tp = find_thread_pid (inferior_ptid);
1026 if (tp)
1027 tp->refcount++;
1028
1029 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1030 restore_current_thread_cleanup_dtor);
1031 }
1032
1033 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1034 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1035 of two numbers seperated by a hyphen. Examples:
1036
1037 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1038 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1039 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1040 */
1041
1042 static void
1043 thread_apply_all_command (char *cmd, int from_tty)
1044 {
1045 struct thread_info *tp;
1046 struct cleanup *old_chain;
1047 char *saved_cmd;
1048
1049 if (cmd == NULL || *cmd == '\000')
1050 error (_("Please specify a command following the thread ID list"));
1051
1052 prune_threads ();
1053 target_find_new_threads ();
1054
1055 old_chain = make_cleanup_restore_current_thread ();
1056
1057 /* Save a copy of the command in case it is clobbered by
1058 execute_command */
1059 saved_cmd = xstrdup (cmd);
1060 make_cleanup (xfree, saved_cmd);
1061 for (tp = thread_list; tp; tp = tp->next)
1062 if (thread_alive (tp))
1063 {
1064 if (non_stop)
1065 context_switch_to (tp->ptid);
1066 else
1067 switch_to_thread (tp->ptid);
1068
1069 printf_filtered (_("\nThread %d (%s):\n"),
1070 tp->num, target_tid_to_str (inferior_ptid));
1071 execute_command (cmd, from_tty);
1072 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
1073 }
1074
1075 do_cleanups (old_chain);
1076 }
1077
1078 static void
1079 thread_apply_command (char *tidlist, int from_tty)
1080 {
1081 char *cmd;
1082 char *p;
1083 struct cleanup *old_chain;
1084 char *saved_cmd;
1085
1086 if (tidlist == NULL || *tidlist == '\000')
1087 error (_("Please specify a thread ID list"));
1088
1089 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1090
1091 if (*cmd == '\000')
1092 error (_("Please specify a command following the thread ID list"));
1093
1094 /* Save a copy of the command in case it is clobbered by
1095 execute_command */
1096 saved_cmd = xstrdup (cmd);
1097 old_chain = make_cleanup (xfree, saved_cmd);
1098 while (tidlist < cmd)
1099 {
1100 struct thread_info *tp;
1101 int start, end;
1102
1103 start = strtol (tidlist, &p, 10);
1104 if (p == tidlist)
1105 error (_("Error parsing %s"), tidlist);
1106 tidlist = p;
1107
1108 while (*tidlist == ' ' || *tidlist == '\t')
1109 tidlist++;
1110
1111 if (*tidlist == '-') /* Got a range of IDs? */
1112 {
1113 tidlist++; /* Skip the - */
1114 end = strtol (tidlist, &p, 10);
1115 if (p == tidlist)
1116 error (_("Error parsing %s"), tidlist);
1117 tidlist = p;
1118
1119 while (*tidlist == ' ' || *tidlist == '\t')
1120 tidlist++;
1121 }
1122 else
1123 end = start;
1124
1125 make_cleanup_restore_current_thread ();
1126
1127 for (; start <= end; start++)
1128 {
1129 tp = find_thread_id (start);
1130
1131 if (!tp)
1132 warning (_("Unknown thread %d."), start);
1133 else if (!thread_alive (tp))
1134 warning (_("Thread %d has terminated."), start);
1135 else
1136 {
1137 if (non_stop)
1138 context_switch_to (tp->ptid);
1139 else
1140 switch_to_thread (tp->ptid);
1141
1142 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1143 target_tid_to_str (inferior_ptid));
1144 execute_command (cmd, from_tty);
1145
1146 /* Restore exact command used previously. */
1147 strcpy (cmd, saved_cmd);
1148 }
1149 }
1150 }
1151
1152 do_cleanups (old_chain);
1153 }
1154
1155 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1156 if prefix of arg is `apply'. */
1157
1158 static void
1159 thread_command (char *tidstr, int from_tty)
1160 {
1161 if (!tidstr)
1162 {
1163 if (target_has_stack)
1164 {
1165 if (is_exited (inferior_ptid))
1166 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1167 pid_to_thread_id (inferior_ptid),
1168 target_tid_to_str (inferior_ptid));
1169 else
1170 printf_filtered (_("[Current thread is %d (%s)]\n"),
1171 pid_to_thread_id (inferior_ptid),
1172 target_tid_to_str (inferior_ptid));
1173 }
1174 else
1175 error (_("No stack."));
1176 return;
1177 }
1178
1179 annotate_thread_changed ();
1180 gdb_thread_select (uiout, tidstr, NULL);
1181 }
1182
1183 /* Print notices when new threads are attached and detached. */
1184 int print_thread_events = 1;
1185 static void
1186 show_print_thread_events (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
1188 {
1189 fprintf_filtered (file, _("\
1190 Printing of thread events is %s.\n"),
1191 value);
1192 }
1193
1194 static int
1195 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1196 {
1197 int num;
1198 struct thread_info *tp;
1199
1200 num = value_as_long (parse_and_eval (tidstr));
1201
1202 tp = find_thread_id (num);
1203
1204 if (!tp)
1205 error (_("Thread ID %d not known."), num);
1206
1207 if (!thread_alive (tp))
1208 error (_("Thread ID %d has terminated."), num);
1209
1210 if (non_stop)
1211 context_switch_to (tp->ptid);
1212 else
1213 switch_to_thread (tp->ptid);
1214
1215 ui_out_text (uiout, "[Switching to thread ");
1216 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1217 ui_out_text (uiout, " (");
1218 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
1219 ui_out_text (uiout, ")]");
1220
1221 /* Note that we can't reach this with an exited thread, due to the
1222 thread_alive check above. */
1223 if (tp->state_ == THREAD_RUNNING)
1224 ui_out_text (uiout, "(running)\n");
1225 else
1226 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1227
1228 /* Since the current thread may have changed, see if there is any
1229 exited thread we can now delete. */
1230 prune_threads ();
1231
1232 return GDB_RC_OK;
1233 }
1234
1235 enum gdb_rc
1236 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1237 {
1238 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1239 error_message, RETURN_MASK_ALL) < 0)
1240 return GDB_RC_FAIL;
1241 return GDB_RC_OK;
1242 }
1243
1244 /* Commands with a prefix of `thread'. */
1245 struct cmd_list_element *thread_cmd_list = NULL;
1246
1247 void
1248 _initialize_thread (void)
1249 {
1250 static struct cmd_list_element *thread_apply_list = NULL;
1251 struct cmd_list_element *c;
1252
1253 c = add_info ("threads", info_threads_command,
1254 _("IDs of currently known threads."));
1255 set_cmd_no_selected_thread_ok (c);
1256
1257 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1258 Use this command to switch between threads.\n\
1259 The new thread ID must be currently known."),
1260 &thread_cmd_list, "thread ", 1, &cmdlist);
1261 set_cmd_no_selected_thread_ok (c);
1262
1263 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1264 _("Apply a command to a list of threads."),
1265 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1266 set_cmd_no_selected_thread_ok (c);
1267
1268 c = add_cmd ("all", class_run, thread_apply_all_command,
1269 _("Apply a command to all threads."), &thread_apply_list);
1270 set_cmd_no_selected_thread_ok (c);
1271
1272 if (!xdb_commands)
1273 add_com_alias ("t", "thread", class_run, 1);
1274
1275 add_setshow_boolean_cmd ("thread-events", no_class,
1276 &print_thread_events, _("\
1277 Set printing of thread events (such as thread start and exit)."), _("\
1278 Show printing of thread events (such as thread start and exit)."), NULL,
1279 NULL,
1280 show_print_thread_events,
1281 &setprintlist, &showprintlist);
1282 }
This page took 0.058095 seconds and 5 git commands to generate.