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