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