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