Update copyright year in most headers.
[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->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 struct thread_info *
444 any_live_thread_of_process (int pid)
445 {
446 struct thread_info *tp;
447 struct thread_info *tp_running = NULL;
448
449 for (tp = thread_list; tp; tp = tp->next)
450 if (ptid_get_pid (tp->ptid) == pid)
451 {
452 if (tp->state_ == THREAD_STOPPED)
453 return tp;
454 else if (tp->state_ == THREAD_RUNNING)
455 tp_running = tp;
456 }
457
458 return tp_running;
459 }
460
461 /* Print a list of thread ids currently known, and the total number of
462 threads. To be used from within catch_errors. */
463 static int
464 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
465 {
466 struct thread_info *tp;
467 int num = 0;
468 struct cleanup *cleanup_chain;
469 int current_thread = -1;
470
471 prune_threads ();
472 target_find_new_threads ();
473
474 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
475
476 for (tp = thread_list; tp; tp = tp->next)
477 {
478 if (tp->state_ == THREAD_EXITED)
479 continue;
480
481 if (ptid_equal (tp->ptid, inferior_ptid))
482 current_thread = tp->num;
483
484 num++;
485 ui_out_field_int (uiout, "thread-id", tp->num);
486 }
487
488 do_cleanups (cleanup_chain);
489
490 if (current_thread != -1)
491 ui_out_field_int (uiout, "current-thread-id", current_thread);
492 ui_out_field_int (uiout, "number-of-threads", num);
493 return GDB_RC_OK;
494 }
495
496 /* Official gdblib interface function to get a list of thread ids and
497 the total number. */
498 enum gdb_rc
499 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
500 {
501 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
502 error_message, RETURN_MASK_ALL) < 0)
503 return GDB_RC_FAIL;
504 return GDB_RC_OK;
505 }
506
507 /* Return true if TP is an active thread. */
508 static int
509 thread_alive (struct thread_info *tp)
510 {
511 if (tp->state_ == THREAD_EXITED)
512 return 0;
513 if (!target_thread_alive (tp->ptid))
514 return 0;
515 return 1;
516 }
517
518 static void
519 prune_threads (void)
520 {
521 struct thread_info *tp, *next;
522
523 for (tp = thread_list; tp; tp = next)
524 {
525 next = tp->next;
526 if (!thread_alive (tp))
527 delete_thread (tp->ptid);
528 }
529 }
530
531 void
532 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
533 {
534 struct inferior *inf;
535 struct thread_info *tp;
536
537 /* It can happen that what we knew as the target inferior id
538 changes. E.g, target remote may only discover the remote process
539 pid after adding the inferior to GDB's list. */
540 inf = find_inferior_pid (ptid_get_pid (old_ptid));
541 inf->pid = ptid_get_pid (new_ptid);
542
543 tp = find_thread_ptid (old_ptid);
544 tp->ptid = new_ptid;
545
546 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
547 }
548
549 void
550 set_running (ptid_t ptid, int running)
551 {
552 struct thread_info *tp;
553 int all = ptid_equal (ptid, minus_one_ptid);
554
555 /* We try not to notify the observer if no thread has actually changed
556 the running state -- merely to reduce the number of messages to
557 frontend. Frontend is supposed to handle multiple *running just fine. */
558 if (all || ptid_is_pid (ptid))
559 {
560 int any_started = 0;
561 for (tp = thread_list; tp; tp = tp->next)
562 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
563 {
564 if (tp->state_ == THREAD_EXITED)
565 continue;
566 if (running && tp->state_ == THREAD_STOPPED)
567 any_started = 1;
568 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
569 }
570 if (any_started)
571 observer_notify_target_resumed (ptid);
572 }
573 else
574 {
575 int started = 0;
576 tp = find_thread_ptid (ptid);
577 gdb_assert (tp);
578 gdb_assert (tp->state_ != THREAD_EXITED);
579 if (running && tp->state_ == THREAD_STOPPED)
580 started = 1;
581 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
582 if (started)
583 observer_notify_target_resumed (ptid);
584 }
585 }
586
587 static int
588 is_thread_state (ptid_t ptid, enum thread_state state)
589 {
590 struct thread_info *tp;
591
592 tp = find_thread_ptid (ptid);
593 gdb_assert (tp);
594 return tp->state_ == state;
595 }
596
597 int
598 is_stopped (ptid_t ptid)
599 {
600 return is_thread_state (ptid, THREAD_STOPPED);
601 }
602
603 int
604 is_exited (ptid_t ptid)
605 {
606 return is_thread_state (ptid, THREAD_EXITED);
607 }
608
609 int
610 is_running (ptid_t ptid)
611 {
612 return is_thread_state (ptid, THREAD_RUNNING);
613 }
614
615 int
616 any_running (void)
617 {
618 struct thread_info *tp;
619
620 for (tp = thread_list; tp; tp = tp->next)
621 if (tp->state_ == THREAD_RUNNING)
622 return 1;
623
624 return 0;
625 }
626
627 int
628 is_executing (ptid_t ptid)
629 {
630 struct thread_info *tp;
631
632 tp = find_thread_ptid (ptid);
633 gdb_assert (tp);
634 return tp->executing_;
635 }
636
637 void
638 set_executing (ptid_t ptid, int executing)
639 {
640 struct thread_info *tp;
641 int all = ptid_equal (ptid, minus_one_ptid);
642
643 if (all || ptid_is_pid (ptid))
644 {
645 for (tp = thread_list; tp; tp = tp->next)
646 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
647 tp->executing_ = executing;
648 }
649 else
650 {
651 tp = find_thread_ptid (ptid);
652 gdb_assert (tp);
653 tp->executing_ = executing;
654 }
655 }
656
657 void
658 set_stop_requested (ptid_t ptid, int stop)
659 {
660 struct thread_info *tp;
661 int all = ptid_equal (ptid, minus_one_ptid);
662
663 if (all || ptid_is_pid (ptid))
664 {
665 for (tp = thread_list; tp; tp = tp->next)
666 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
667 tp->stop_requested = stop;
668 }
669 else
670 {
671 tp = find_thread_ptid (ptid);
672 gdb_assert (tp);
673 tp->stop_requested = stop;
674 }
675
676 /* Call the stop requested observer so other components of GDB can
677 react to this request. */
678 if (stop)
679 observer_notify_thread_stop_requested (ptid);
680 }
681
682 void
683 finish_thread_state (ptid_t ptid)
684 {
685 struct thread_info *tp;
686 int all;
687 int any_started = 0;
688
689 all = ptid_equal (ptid, minus_one_ptid);
690
691 if (all || ptid_is_pid (ptid))
692 {
693 for (tp = thread_list; tp; tp = tp->next)
694 {
695 if (tp->state_ == THREAD_EXITED)
696 continue;
697 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
698 {
699 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
700 any_started = 1;
701 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
702 }
703 }
704 }
705 else
706 {
707 tp = find_thread_ptid (ptid);
708 gdb_assert (tp);
709 if (tp->state_ != THREAD_EXITED)
710 {
711 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
712 any_started = 1;
713 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
714 }
715 }
716
717 if (any_started)
718 observer_notify_target_resumed (ptid);
719 }
720
721 void
722 finish_thread_state_cleanup (void *arg)
723 {
724 ptid_t *ptid_p = arg;
725
726 gdb_assert (arg);
727
728 finish_thread_state (*ptid_p);
729 }
730
731 /* Prints the list of threads and their details on UIOUT.
732 This is a version of 'info_thread_command' suitable for
733 use from MI.
734 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
735 that should be printed. Otherwise, all threads are
736 printed.
737 If PID is not -1, only print threads from the process PID.
738 Otherwise, threads from all attached PIDs are printed.
739 If both REQUESTED_THREAD and PID are not -1, then the thread
740 is printed if it belongs to the specified process. Otherwise,
741 an error is raised. */
742 void
743 print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
744 {
745 struct thread_info *tp;
746 ptid_t current_ptid;
747 struct cleanup *old_chain;
748 char *extra_info;
749 int current_thread = -1;
750
751 prune_threads ();
752 target_find_new_threads ();
753 current_ptid = inferior_ptid;
754
755 /* We'll be switching threads temporarily. */
756 old_chain = make_cleanup_restore_current_thread ();
757
758 make_cleanup_ui_out_list_begin_end (uiout, "threads");
759 for (tp = thread_list; tp; tp = tp->next)
760 {
761 struct cleanup *chain2;
762
763 if (requested_thread != -1 && tp->num != requested_thread)
764 continue;
765
766 if (pid != -1 && PIDGET (tp->ptid) != pid)
767 {
768 if (requested_thread != -1)
769 error (_("Requested thread not found in requested process"));
770 continue;
771 }
772
773 if (ptid_equal (tp->ptid, current_ptid))
774 current_thread = tp->num;
775
776 if (tp->state_ == THREAD_EXITED)
777 continue;
778
779 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
780
781 if (ptid_equal (tp->ptid, current_ptid))
782 ui_out_text (uiout, "* ");
783 else
784 ui_out_text (uiout, " ");
785
786 ui_out_field_int (uiout, "id", tp->num);
787 ui_out_text (uiout, " ");
788 ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
789
790 extra_info = target_extra_thread_info (tp);
791 if (extra_info)
792 {
793 ui_out_text (uiout, " (");
794 ui_out_field_string (uiout, "details", extra_info);
795 ui_out_text (uiout, ")");
796 }
797 ui_out_text (uiout, " ");
798
799 if (tp->state_ == THREAD_RUNNING)
800 ui_out_text (uiout, "(running)\n");
801 else
802 {
803 /* The switch below puts us at the top of the stack (leaf
804 frame). */
805 switch_to_thread (tp->ptid);
806 print_stack_frame (get_selected_frame (NULL),
807 /* For MI output, print frame level. */
808 ui_out_is_mi_like_p (uiout),
809 LOCATION);
810 }
811
812 if (ui_out_is_mi_like_p (uiout))
813 {
814 char *state = "stopped";
815 if (tp->state_ == THREAD_RUNNING)
816 state = "running";
817 ui_out_field_string (uiout, "state", state);
818 }
819
820 do_cleanups (chain2);
821 }
822
823 /* Restores the current thread and the frame selected before
824 the "info threads" command. */
825 do_cleanups (old_chain);
826
827 if (pid == -1 && requested_thread == -1)
828 {
829 gdb_assert (current_thread != -1
830 || !thread_list
831 || ptid_equal (inferior_ptid, null_ptid));
832 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
833 ui_out_field_int (uiout, "current-thread-id", current_thread);
834
835 if (current_thread != -1 && is_exited (current_ptid))
836 ui_out_message (uiout, 0, "\n\
837 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
838 current_thread);
839 else if (thread_list
840 && current_thread == -1
841 && ptid_equal (current_ptid, null_ptid))
842 ui_out_message (uiout, 0, "\n\
843 No selected thread. See `help thread'.\n");
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, -1);
859 }
860
861 /* Switch from one thread to another. */
862
863 void
864 switch_to_thread (ptid_t ptid)
865 {
866 /* Switch the program space as well, if we can infer it from the now
867 current thread. Otherwise, it's up to the caller to select the
868 space it wants. */
869 if (!ptid_equal (ptid, null_ptid))
870 {
871 struct inferior *inf;
872
873 inf = find_inferior_pid (ptid_get_pid (ptid));
874 gdb_assert (inf != NULL);
875 set_current_program_space (inf->pspace);
876 set_current_inferior (inf);
877 }
878
879 if (ptid_equal (ptid, inferior_ptid))
880 return;
881
882 inferior_ptid = ptid;
883 reinit_frame_cache ();
884 registers_changed ();
885
886 /* We don't check for is_stopped, because we're called at times
887 while in the TARGET_RUNNING state, e.g., while handling an
888 internal event. */
889 if (!ptid_equal (inferior_ptid, null_ptid)
890 && !is_exited (ptid)
891 && !is_executing (ptid))
892 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
893 else
894 stop_pc = ~(CORE_ADDR) 0;
895 }
896
897 static void
898 restore_current_thread (ptid_t ptid)
899 {
900 switch_to_thread (ptid);
901 }
902
903 static void
904 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
905 {
906 struct frame_info *frame = NULL;
907 int count;
908
909 gdb_assert (frame_level >= 0);
910
911 /* Restore by level first, check if the frame id is the same as
912 expected. If that fails, try restoring by frame id. If that
913 fails, nothing to do, just warn the user. */
914
915 count = frame_level;
916 frame = find_relative_frame (get_current_frame (), &count);
917 if (count == 0
918 && frame != NULL
919 /* The frame ids must match - either both valid or both outer_frame_id.
920 The latter case is not failsafe, but since it's highly unlikely
921 the search by level finds the wrong frame, it's 99.9(9)% of
922 the time (for all practical purposes) safe. */
923 && frame_id_eq (get_frame_id (frame), a_frame_id))
924 {
925 /* Cool, all is fine. */
926 select_frame (frame);
927 return;
928 }
929
930 frame = frame_find_by_id (a_frame_id);
931 if (frame != NULL)
932 {
933 /* Cool, refound it. */
934 select_frame (frame);
935 return;
936 }
937
938 /* Nothing else to do, the frame layout really changed. Select the
939 innermost stack frame. */
940 select_frame (get_current_frame ());
941
942 /* Warn the user. */
943 if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
944 {
945 warning (_("\
946 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
947 frame_level);
948 /* For MI, we should probably have a notification about
949 current frame change. But this error is not very
950 likely, so don't bother for now. */
951 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
952 }
953 }
954
955 struct current_thread_cleanup
956 {
957 ptid_t inferior_ptid;
958 struct frame_id selected_frame_id;
959 int selected_frame_level;
960 int was_stopped;
961 int inf_id;
962 };
963
964 static void
965 do_restore_current_thread_cleanup (void *arg)
966 {
967 struct thread_info *tp;
968 struct current_thread_cleanup *old = arg;
969
970 tp = find_thread_ptid (old->inferior_ptid);
971
972 /* If the previously selected thread belonged to a process that has
973 in the mean time been deleted (due to normal exit, detach, etc.),
974 then don't revert back to it, but instead simply drop back to no
975 thread selected. */
976 if (tp
977 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
978 restore_current_thread (old->inferior_ptid);
979 else
980 {
981 restore_current_thread (null_ptid);
982 set_current_inferior (find_inferior_id (old->inf_id));
983 }
984
985 /* The running state of the originally selected thread may have
986 changed, so we have to recheck it here. */
987 if (!ptid_equal (inferior_ptid, null_ptid)
988 && old->was_stopped
989 && is_stopped (inferior_ptid)
990 && target_has_registers
991 && target_has_stack
992 && target_has_memory)
993 restore_selected_frame (old->selected_frame_id,
994 old->selected_frame_level);
995 }
996
997 static void
998 restore_current_thread_cleanup_dtor (void *arg)
999 {
1000 struct current_thread_cleanup *old = arg;
1001 struct thread_info *tp;
1002 tp = find_thread_ptid (old->inferior_ptid);
1003 if (tp)
1004 tp->refcount--;
1005 xfree (old);
1006 }
1007
1008 struct cleanup *
1009 make_cleanup_restore_current_thread (void)
1010 {
1011 struct thread_info *tp;
1012 struct frame_info *frame;
1013 struct current_thread_cleanup *old;
1014
1015 old = xmalloc (sizeof (struct current_thread_cleanup));
1016 old->inferior_ptid = inferior_ptid;
1017 old->inf_id = current_inferior ()->num;
1018
1019 if (!ptid_equal (inferior_ptid, null_ptid))
1020 {
1021 old->was_stopped = is_stopped (inferior_ptid);
1022 if (old->was_stopped
1023 && target_has_registers
1024 && target_has_stack
1025 && target_has_memory)
1026 frame = get_selected_frame (NULL);
1027 else
1028 frame = NULL;
1029
1030 old->selected_frame_id = get_frame_id (frame);
1031 old->selected_frame_level = frame_relative_level (frame);
1032
1033 tp = find_thread_ptid (inferior_ptid);
1034 if (tp)
1035 tp->refcount++;
1036 }
1037
1038 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1039 restore_current_thread_cleanup_dtor);
1040 }
1041
1042 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1043 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1044 of two numbers seperated by a hyphen. Examples:
1045
1046 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1047 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1048 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1049 */
1050
1051 static void
1052 thread_apply_all_command (char *cmd, int from_tty)
1053 {
1054 struct thread_info *tp;
1055 struct cleanup *old_chain;
1056 char *saved_cmd;
1057
1058 if (cmd == NULL || *cmd == '\000')
1059 error (_("Please specify a command following the thread ID list"));
1060
1061 prune_threads ();
1062 target_find_new_threads ();
1063
1064 old_chain = make_cleanup_restore_current_thread ();
1065
1066 /* Save a copy of the command in case it is clobbered by
1067 execute_command */
1068 saved_cmd = xstrdup (cmd);
1069 make_cleanup (xfree, saved_cmd);
1070 for (tp = thread_list; tp; tp = tp->next)
1071 if (thread_alive (tp))
1072 {
1073 switch_to_thread (tp->ptid);
1074
1075 printf_filtered (_("\nThread %d (%s):\n"),
1076 tp->num, target_pid_to_str (inferior_ptid));
1077 execute_command (cmd, from_tty);
1078 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
1079 }
1080
1081 do_cleanups (old_chain);
1082 }
1083
1084 static void
1085 thread_apply_command (char *tidlist, int from_tty)
1086 {
1087 char *cmd;
1088 char *p;
1089 struct cleanup *old_chain;
1090 char *saved_cmd;
1091
1092 if (tidlist == NULL || *tidlist == '\000')
1093 error (_("Please specify a thread ID list"));
1094
1095 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1096
1097 if (*cmd == '\000')
1098 error (_("Please specify a command following the thread ID list"));
1099
1100 /* Save a copy of the command in case it is clobbered by
1101 execute_command */
1102 saved_cmd = xstrdup (cmd);
1103 old_chain = make_cleanup (xfree, saved_cmd);
1104 while (tidlist < cmd)
1105 {
1106 struct thread_info *tp;
1107 int start, end;
1108
1109 start = strtol (tidlist, &p, 10);
1110 if (p == tidlist)
1111 error (_("Error parsing %s"), tidlist);
1112 tidlist = p;
1113
1114 while (*tidlist == ' ' || *tidlist == '\t')
1115 tidlist++;
1116
1117 if (*tidlist == '-') /* Got a range of IDs? */
1118 {
1119 tidlist++; /* Skip the - */
1120 end = strtol (tidlist, &p, 10);
1121 if (p == tidlist)
1122 error (_("Error parsing %s"), tidlist);
1123 tidlist = p;
1124
1125 while (*tidlist == ' ' || *tidlist == '\t')
1126 tidlist++;
1127 }
1128 else
1129 end = start;
1130
1131 make_cleanup_restore_current_thread ();
1132
1133 for (; start <= end; start++)
1134 {
1135 tp = find_thread_id (start);
1136
1137 if (!tp)
1138 warning (_("Unknown thread %d."), start);
1139 else if (!thread_alive (tp))
1140 warning (_("Thread %d has terminated."), start);
1141 else
1142 {
1143 switch_to_thread (tp->ptid);
1144
1145 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1146 target_pid_to_str (inferior_ptid));
1147 execute_command (cmd, from_tty);
1148
1149 /* Restore exact command used previously. */
1150 strcpy (cmd, saved_cmd);
1151 }
1152 }
1153 }
1154
1155 do_cleanups (old_chain);
1156 }
1157
1158 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1159 if prefix of arg is `apply'. */
1160
1161 static void
1162 thread_command (char *tidstr, int from_tty)
1163 {
1164 if (!tidstr)
1165 {
1166 if (ptid_equal (inferior_ptid, null_ptid))
1167 error (_("No thread selected"));
1168
1169 if (target_has_stack)
1170 {
1171 if (is_exited (inferior_ptid))
1172 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1173 pid_to_thread_id (inferior_ptid),
1174 target_pid_to_str (inferior_ptid));
1175 else
1176 printf_filtered (_("[Current thread is %d (%s)]\n"),
1177 pid_to_thread_id (inferior_ptid),
1178 target_pid_to_str (inferior_ptid));
1179 }
1180 else
1181 error (_("No stack."));
1182 return;
1183 }
1184
1185 gdb_thread_select (uiout, tidstr, NULL);
1186 }
1187
1188 /* Print notices when new threads are attached and detached. */
1189 int print_thread_events = 1;
1190 static void
1191 show_print_thread_events (struct ui_file *file, int from_tty,
1192 struct cmd_list_element *c, const char *value)
1193 {
1194 fprintf_filtered (file, _("\
1195 Printing of thread events is %s.\n"),
1196 value);
1197 }
1198
1199 static int
1200 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1201 {
1202 int num;
1203 struct thread_info *tp;
1204
1205 num = value_as_long (parse_and_eval (tidstr));
1206
1207 tp = find_thread_id (num);
1208
1209 if (!tp)
1210 error (_("Thread ID %d not known."), num);
1211
1212 if (!thread_alive (tp))
1213 error (_("Thread ID %d has terminated."), num);
1214
1215 switch_to_thread (tp->ptid);
1216
1217 annotate_thread_changed ();
1218
1219 ui_out_text (uiout, "[Switching to thread ");
1220 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1221 ui_out_text (uiout, " (");
1222 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1223 ui_out_text (uiout, ")]");
1224
1225 /* Note that we can't reach this with an exited thread, due to the
1226 thread_alive check above. */
1227 if (tp->state_ == THREAD_RUNNING)
1228 ui_out_text (uiout, "(running)\n");
1229 else
1230 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1231
1232 /* Since the current thread may have changed, see if there is any
1233 exited thread we can now delete. */
1234 prune_threads ();
1235
1236 return GDB_RC_OK;
1237 }
1238
1239 enum gdb_rc
1240 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1241 {
1242 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1243 error_message, RETURN_MASK_ALL) < 0)
1244 return GDB_RC_FAIL;
1245 return GDB_RC_OK;
1246 }
1247
1248 /* Commands with a prefix of `thread'. */
1249 struct cmd_list_element *thread_cmd_list = NULL;
1250
1251 void
1252 _initialize_thread (void)
1253 {
1254 static struct cmd_list_element *thread_apply_list = NULL;
1255
1256 add_info ("threads", info_threads_command,
1257 _("IDs of currently known threads."));
1258
1259 add_prefix_cmd ("thread", class_run, thread_command, _("\
1260 Use this command to switch between threads.\n\
1261 The new thread ID must be currently known."),
1262 &thread_cmd_list, "thread ", 1, &cmdlist);
1263
1264 add_prefix_cmd ("apply", class_run, thread_apply_command,
1265 _("Apply a command to a list of threads."),
1266 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1267
1268 add_cmd ("all", class_run, thread_apply_all_command,
1269 _("Apply a command to all threads."), &thread_apply_list);
1270
1271 if (!xdb_commands)
1272 add_com_alias ("t", "thread", class_run, 1);
1273
1274 add_setshow_boolean_cmd ("thread-events", no_class,
1275 &print_thread_events, _("\
1276 Set printing of thread events (such as thread start and exit)."), _("\
1277 Show printing of thread events (such as thread start and exit)."), NULL,
1278 NULL,
1279 show_print_thread_events,
1280 &setprintlist, &showprintlist);
1281 }
This page took 0.058515 seconds and 5 git commands to generate.