Replace "struct continuation" mechanism by something more extensible
[binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "environ.h"
27 #include "value.h"
28 #include "target.h"
29 #include "gdbthread.h"
30 #include "command.h"
31 #include "gdbcmd.h"
32 #include "regcache.h"
33 #include "gdb.h"
34 #include "btrace.h"
35
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "ui-out.h"
40 #include "observer.h"
41 #include "annotate.h"
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "continuations.h"
46 #include "thread-fsm.h"
47
48 /* Definition of struct thread_info exported to gdbthread.h. */
49
50 /* Prototypes for exported functions. */
51
52 void _initialize_thread (void);
53
54 /* Prototypes for local functions. */
55
56 struct thread_info *thread_list = NULL;
57 static int highest_thread_num;
58
59 /* True if any thread is, or may be executing. We need to track this
60 separately because until we fully sync the thread list, we won't
61 know whether the target is fully stopped, even if we see stop
62 events for all known threads, because any of those threads may have
63 spawned new threads we haven't heard of yet. */
64 static int threads_executing;
65
66 static void thread_apply_all_command (char *, int);
67 static int thread_alive (struct thread_info *);
68 static void info_threads_command (char *, int);
69 static void thread_apply_command (char *, int);
70 static void restore_current_thread (ptid_t);
71
72 /* Data to cleanup thread array. */
73
74 struct thread_array_cleanup
75 {
76 /* Array of thread pointers used to set
77 reference count. */
78 struct thread_info **tp_array;
79
80 /* Thread count in the array. */
81 int count;
82 };
83
84
85 struct thread_info*
86 inferior_thread (void)
87 {
88 struct thread_info *tp = find_thread_ptid (inferior_ptid);
89 gdb_assert (tp);
90 return tp;
91 }
92
93 /* Delete the breakpoint pointed at by BP_P, if there's one. */
94
95 static void
96 delete_thread_breakpoint (struct breakpoint **bp_p)
97 {
98 if (*bp_p != NULL)
99 {
100 delete_breakpoint (*bp_p);
101 *bp_p = NULL;
102 }
103 }
104
105 void
106 delete_step_resume_breakpoint (struct thread_info *tp)
107 {
108 if (tp != NULL)
109 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
110 }
111
112 void
113 delete_exception_resume_breakpoint (struct thread_info *tp)
114 {
115 if (tp != NULL)
116 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
117 }
118
119 /* See gdbthread.h. */
120
121 void
122 delete_single_step_breakpoints (struct thread_info *tp)
123 {
124 if (tp != NULL)
125 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
126 }
127
128 /* Delete the breakpoint pointed at by BP_P at the next stop, if
129 there's one. */
130
131 static void
132 delete_at_next_stop (struct breakpoint **bp)
133 {
134 if (*bp != NULL)
135 {
136 (*bp)->disposition = disp_del_at_next_stop;
137 *bp = NULL;
138 }
139 }
140
141 /* See gdbthread.h. */
142
143 int
144 thread_has_single_step_breakpoints_set (struct thread_info *tp)
145 {
146 return tp->control.single_step_breakpoints != NULL;
147 }
148
149 /* See gdbthread.h. */
150
151 int
152 thread_has_single_step_breakpoint_here (struct thread_info *tp,
153 struct address_space *aspace,
154 CORE_ADDR addr)
155 {
156 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
157
158 return (ss_bps != NULL
159 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
160 }
161
162 /* See gdbthread.h. */
163
164 void
165 thread_cancel_execution_command (struct thread_info *thr)
166 {
167 if (thr->thread_fsm != NULL)
168 {
169 thread_fsm_clean_up (thr->thread_fsm);
170 thread_fsm_delete (thr->thread_fsm);
171 thr->thread_fsm = NULL;
172 }
173 }
174
175 static void
176 clear_thread_inferior_resources (struct thread_info *tp)
177 {
178 /* NOTE: this will take care of any left-over step_resume breakpoints,
179 but not any user-specified thread-specific breakpoints. We can not
180 delete the breakpoint straight-off, because the inferior might not
181 be stopped at the moment. */
182 delete_at_next_stop (&tp->control.step_resume_breakpoint);
183 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
184 delete_at_next_stop (&tp->control.single_step_breakpoints);
185
186 delete_longjmp_breakpoint_at_next_stop (tp->num);
187
188 bpstat_clear (&tp->control.stop_bpstat);
189
190 btrace_teardown (tp);
191
192 thread_cancel_execution_command (tp);
193
194 do_all_intermediate_continuations_thread (tp, 1);
195 do_all_continuations_thread (tp, 1);
196 }
197
198 static void
199 free_thread (struct thread_info *tp)
200 {
201 if (tp->priv)
202 {
203 if (tp->private_dtor)
204 tp->private_dtor (tp->priv);
205 else
206 xfree (tp->priv);
207 }
208
209 xfree (tp->name);
210 xfree (tp);
211 }
212
213 void
214 init_thread_list (void)
215 {
216 struct thread_info *tp, *tpnext;
217
218 highest_thread_num = 0;
219
220 if (!thread_list)
221 return;
222
223 for (tp = thread_list; tp; tp = tpnext)
224 {
225 tpnext = tp->next;
226 free_thread (tp);
227 }
228
229 thread_list = NULL;
230 threads_executing = 0;
231 }
232
233 /* Allocate a new thread with target id PTID and add it to the thread
234 list. */
235
236 static struct thread_info *
237 new_thread (ptid_t ptid)
238 {
239 struct thread_info *tp = XCNEW (struct thread_info);
240
241 tp->ptid = ptid;
242 tp->num = ++highest_thread_num;
243 tp->next = thread_list;
244 thread_list = tp;
245
246 /* Nothing to follow yet. */
247 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
248 tp->state = THREAD_STOPPED;
249 tp->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
250
251 return tp;
252 }
253
254 struct thread_info *
255 add_thread_silent (ptid_t ptid)
256 {
257 struct thread_info *tp;
258
259 tp = find_thread_ptid (ptid);
260 if (tp)
261 /* Found an old thread with the same id. It has to be dead,
262 otherwise we wouldn't be adding a new thread with the same id.
263 The OS is reusing this id --- delete it, and recreate a new
264 one. */
265 {
266 /* In addition to deleting the thread, if this is the current
267 thread, then we need to take care that delete_thread doesn't
268 really delete the thread if it is inferior_ptid. Create a
269 new template thread in the list with an invalid ptid, switch
270 to it, delete the original thread, reset the new thread's
271 ptid, and switch to it. */
272
273 if (ptid_equal (inferior_ptid, ptid))
274 {
275 tp = new_thread (null_ptid);
276
277 /* Make switch_to_thread not read from the thread. */
278 tp->state = THREAD_EXITED;
279 switch_to_thread (null_ptid);
280
281 /* Now we can delete it. */
282 delete_thread (ptid);
283
284 /* Now reset its ptid, and reswitch inferior_ptid to it. */
285 tp->ptid = ptid;
286 tp->state = THREAD_STOPPED;
287 switch_to_thread (ptid);
288
289 observer_notify_new_thread (tp);
290
291 /* All done. */
292 return tp;
293 }
294 else
295 /* Just go ahead and delete it. */
296 delete_thread (ptid);
297 }
298
299 tp = new_thread (ptid);
300 observer_notify_new_thread (tp);
301
302 return tp;
303 }
304
305 struct thread_info *
306 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
307 {
308 struct thread_info *result = add_thread_silent (ptid);
309
310 result->priv = priv;
311
312 if (print_thread_events)
313 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
314
315 annotate_new_thread ();
316 return result;
317 }
318
319 struct thread_info *
320 add_thread (ptid_t ptid)
321 {
322 return add_thread_with_info (ptid, NULL);
323 }
324
325 /* Add TP to the end of the step-over chain LIST_P. */
326
327 static void
328 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
329 {
330 gdb_assert (tp->step_over_next == NULL);
331 gdb_assert (tp->step_over_prev == NULL);
332
333 if (*list_p == NULL)
334 {
335 *list_p = tp;
336 tp->step_over_prev = tp->step_over_next = tp;
337 }
338 else
339 {
340 struct thread_info *head = *list_p;
341 struct thread_info *tail = head->step_over_prev;
342
343 tp->step_over_prev = tail;
344 tp->step_over_next = head;
345 head->step_over_prev = tp;
346 tail->step_over_next = tp;
347 }
348 }
349
350 /* Remove TP from step-over chain LIST_P. */
351
352 static void
353 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
354 {
355 gdb_assert (tp->step_over_next != NULL);
356 gdb_assert (tp->step_over_prev != NULL);
357
358 if (*list_p == tp)
359 {
360 if (tp == tp->step_over_next)
361 *list_p = NULL;
362 else
363 *list_p = tp->step_over_next;
364 }
365
366 tp->step_over_prev->step_over_next = tp->step_over_next;
367 tp->step_over_next->step_over_prev = tp->step_over_prev;
368 tp->step_over_prev = tp->step_over_next = NULL;
369 }
370
371 /* See gdbthread.h. */
372
373 struct thread_info *
374 thread_step_over_chain_next (struct thread_info *tp)
375 {
376 struct thread_info *next = tp->step_over_next;
377
378 return (next == step_over_queue_head ? NULL : next);
379 }
380
381 /* See gdbthread.h. */
382
383 int
384 thread_is_in_step_over_chain (struct thread_info *tp)
385 {
386 return (tp->step_over_next != NULL);
387 }
388
389 /* See gdbthread.h. */
390
391 void
392 thread_step_over_chain_enqueue (struct thread_info *tp)
393 {
394 step_over_chain_enqueue (&step_over_queue_head, tp);
395 }
396
397 /* See gdbthread.h. */
398
399 void
400 thread_step_over_chain_remove (struct thread_info *tp)
401 {
402 step_over_chain_remove (&step_over_queue_head, tp);
403 }
404
405 /* Delete thread PTID. If SILENT, don't notify the observer of this
406 exit. */
407 static void
408 delete_thread_1 (ptid_t ptid, int silent)
409 {
410 struct thread_info *tp, *tpprev;
411
412 tpprev = NULL;
413
414 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
415 if (ptid_equal (tp->ptid, ptid))
416 break;
417
418 if (!tp)
419 return;
420
421 /* Dead threads don't need to step-over. Remove from queue. */
422 if (tp->step_over_next != NULL)
423 thread_step_over_chain_remove (tp);
424
425 /* If this is the current thread, or there's code out there that
426 relies on it existing (refcount > 0) we can't delete yet. Mark
427 it as exited, and notify it. */
428 if (tp->refcount > 0
429 || ptid_equal (tp->ptid, inferior_ptid))
430 {
431 if (tp->state != THREAD_EXITED)
432 {
433 observer_notify_thread_exit (tp, silent);
434
435 /* Tag it as exited. */
436 tp->state = THREAD_EXITED;
437
438 /* Clear breakpoints, etc. associated with this thread. */
439 clear_thread_inferior_resources (tp);
440 }
441
442 /* Will be really deleted some other time. */
443 return;
444 }
445
446 /* Notify thread exit, but only if we haven't already. */
447 if (tp->state != THREAD_EXITED)
448 observer_notify_thread_exit (tp, silent);
449
450 /* Tag it as exited. */
451 tp->state = THREAD_EXITED;
452 clear_thread_inferior_resources (tp);
453
454 if (tpprev)
455 tpprev->next = tp->next;
456 else
457 thread_list = tp->next;
458
459 free_thread (tp);
460 }
461
462 /* Delete thread PTID and notify of thread exit. If this is
463 inferior_ptid, don't actually delete it, but tag it as exited and
464 do the notification. If PTID is the user selected thread, clear
465 it. */
466 void
467 delete_thread (ptid_t ptid)
468 {
469 delete_thread_1 (ptid, 0 /* not silent */);
470 }
471
472 void
473 delete_thread_silent (ptid_t ptid)
474 {
475 delete_thread_1 (ptid, 1 /* silent */);
476 }
477
478 struct thread_info *
479 find_thread_id (int num)
480 {
481 struct thread_info *tp;
482
483 for (tp = thread_list; tp; tp = tp->next)
484 if (tp->num == num)
485 return tp;
486
487 return NULL;
488 }
489
490 /* Find a thread_info by matching PTID. */
491 struct thread_info *
492 find_thread_ptid (ptid_t ptid)
493 {
494 struct thread_info *tp;
495
496 for (tp = thread_list; tp; tp = tp->next)
497 if (ptid_equal (tp->ptid, ptid))
498 return tp;
499
500 return NULL;
501 }
502
503 /*
504 * Thread iterator function.
505 *
506 * Calls a callback function once for each thread, so long as
507 * the callback function returns false. If the callback function
508 * returns true, the iteration will end and the current thread
509 * will be returned. This can be useful for implementing a
510 * search for a thread with arbitrary attributes, or for applying
511 * some operation to every thread.
512 *
513 * FIXME: some of the existing functionality, such as
514 * "Thread apply all", might be rewritten using this functionality.
515 */
516
517 struct thread_info *
518 iterate_over_threads (int (*callback) (struct thread_info *, void *),
519 void *data)
520 {
521 struct thread_info *tp, *next;
522
523 for (tp = thread_list; tp; tp = next)
524 {
525 next = tp->next;
526 if ((*callback) (tp, data))
527 return tp;
528 }
529
530 return NULL;
531 }
532
533 int
534 thread_count (void)
535 {
536 int result = 0;
537 struct thread_info *tp;
538
539 for (tp = thread_list; tp; tp = tp->next)
540 ++result;
541
542 return result;
543 }
544
545 int
546 valid_thread_id (int num)
547 {
548 struct thread_info *tp;
549
550 for (tp = thread_list; tp; tp = tp->next)
551 if (tp->num == num)
552 return 1;
553
554 return 0;
555 }
556
557 int
558 pid_to_thread_id (ptid_t ptid)
559 {
560 struct thread_info *tp;
561
562 for (tp = thread_list; tp; tp = tp->next)
563 if (ptid_equal (tp->ptid, ptid))
564 return tp->num;
565
566 return 0;
567 }
568
569 ptid_t
570 thread_id_to_pid (int num)
571 {
572 struct thread_info *thread = find_thread_id (num);
573
574 if (thread)
575 return thread->ptid;
576 else
577 return pid_to_ptid (-1);
578 }
579
580 int
581 in_thread_list (ptid_t ptid)
582 {
583 struct thread_info *tp;
584
585 for (tp = thread_list; tp; tp = tp->next)
586 if (ptid_equal (tp->ptid, ptid))
587 return 1;
588
589 return 0; /* Never heard of 'im. */
590 }
591
592 /* Finds the first thread of the inferior given by PID. If PID is -1,
593 return the first thread in the list. */
594
595 struct thread_info *
596 first_thread_of_process (int pid)
597 {
598 struct thread_info *tp, *ret = NULL;
599
600 for (tp = thread_list; tp; tp = tp->next)
601 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
602 if (ret == NULL || tp->num < ret->num)
603 ret = tp;
604
605 return ret;
606 }
607
608 struct thread_info *
609 any_thread_of_process (int pid)
610 {
611 struct thread_info *tp;
612
613 gdb_assert (pid != 0);
614
615 /* Prefer the current thread. */
616 if (ptid_get_pid (inferior_ptid) == pid)
617 return inferior_thread ();
618
619 ALL_NON_EXITED_THREADS (tp)
620 if (ptid_get_pid (tp->ptid) == pid)
621 return tp;
622
623 return NULL;
624 }
625
626 struct thread_info *
627 any_live_thread_of_process (int pid)
628 {
629 struct thread_info *curr_tp = NULL;
630 struct thread_info *tp;
631 struct thread_info *tp_executing = NULL;
632
633 gdb_assert (pid != 0);
634
635 /* Prefer the current thread if it's not executing. */
636 if (ptid_get_pid (inferior_ptid) == pid)
637 {
638 /* If the current thread is dead, forget it. If it's not
639 executing, use it. Otherwise, still choose it (below), but
640 only if no other non-executing thread is found. */
641 curr_tp = inferior_thread ();
642 if (curr_tp->state == THREAD_EXITED)
643 curr_tp = NULL;
644 else if (!curr_tp->executing)
645 return curr_tp;
646 }
647
648 ALL_NON_EXITED_THREADS (tp)
649 if (ptid_get_pid (tp->ptid) == pid)
650 {
651 if (!tp->executing)
652 return tp;
653
654 tp_executing = tp;
655 }
656
657 /* If both the current thread and all live threads are executing,
658 prefer the current thread. */
659 if (curr_tp != NULL)
660 return curr_tp;
661
662 /* Otherwise, just return an executing thread, if any. */
663 return tp_executing;
664 }
665
666 /* Print a list of thread ids currently known, and the total number of
667 threads. To be used from within catch_errors. */
668 static int
669 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
670 {
671 struct thread_info *tp;
672 int num = 0;
673 struct cleanup *cleanup_chain;
674 int current_thread = -1;
675
676 update_thread_list ();
677
678 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
679
680 for (tp = thread_list; tp; tp = tp->next)
681 {
682 if (tp->state == THREAD_EXITED)
683 continue;
684
685 if (ptid_equal (tp->ptid, inferior_ptid))
686 current_thread = tp->num;
687
688 num++;
689 ui_out_field_int (uiout, "thread-id", tp->num);
690 }
691
692 do_cleanups (cleanup_chain);
693
694 if (current_thread != -1)
695 ui_out_field_int (uiout, "current-thread-id", current_thread);
696 ui_out_field_int (uiout, "number-of-threads", num);
697 return GDB_RC_OK;
698 }
699
700 /* Official gdblib interface function to get a list of thread ids and
701 the total number. */
702 enum gdb_rc
703 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
704 {
705 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
706 error_message, RETURN_MASK_ALL) < 0)
707 return GDB_RC_FAIL;
708 return GDB_RC_OK;
709 }
710
711 /* Return true if TP is an active thread. */
712 static int
713 thread_alive (struct thread_info *tp)
714 {
715 if (tp->state == THREAD_EXITED)
716 return 0;
717 if (!target_thread_alive (tp->ptid))
718 return 0;
719 return 1;
720 }
721
722 /* See gdbthreads.h. */
723
724 void
725 prune_threads (void)
726 {
727 struct thread_info *tp, *tmp;
728
729 ALL_THREADS_SAFE (tp, tmp)
730 {
731 if (!thread_alive (tp))
732 delete_thread (tp->ptid);
733 }
734 }
735
736 /* See gdbthreads.h. */
737
738 void
739 delete_exited_threads (void)
740 {
741 struct thread_info *tp, *tmp;
742
743 ALL_THREADS_SAFE (tp, tmp)
744 {
745 if (tp->state == THREAD_EXITED)
746 delete_thread (tp->ptid);
747 }
748 }
749
750 /* Disable storing stack temporaries for the thread whose id is
751 stored in DATA. */
752
753 static void
754 disable_thread_stack_temporaries (void *data)
755 {
756 ptid_t *pd = data;
757 struct thread_info *tp = find_thread_ptid (*pd);
758
759 if (tp != NULL)
760 {
761 tp->stack_temporaries_enabled = 0;
762 VEC_free (value_ptr, tp->stack_temporaries);
763 }
764
765 xfree (pd);
766 }
767
768 /* Enable storing stack temporaries for thread with id PTID and return a
769 cleanup which can disable and clear the stack temporaries. */
770
771 struct cleanup *
772 enable_thread_stack_temporaries (ptid_t ptid)
773 {
774 struct thread_info *tp = find_thread_ptid (ptid);
775 ptid_t *data;
776 struct cleanup *c;
777
778 gdb_assert (tp != NULL);
779
780 tp->stack_temporaries_enabled = 1;
781 tp->stack_temporaries = NULL;
782 data = XNEW (ptid_t);
783 *data = ptid;
784 c = make_cleanup (disable_thread_stack_temporaries, data);
785
786 return c;
787 }
788
789 /* Return non-zero value if stack temporaies are enabled for the thread
790 with id PTID. */
791
792 int
793 thread_stack_temporaries_enabled_p (ptid_t ptid)
794 {
795 struct thread_info *tp = find_thread_ptid (ptid);
796
797 if (tp == NULL)
798 return 0;
799 else
800 return tp->stack_temporaries_enabled;
801 }
802
803 /* Push V on to the stack temporaries of the thread with id PTID. */
804
805 void
806 push_thread_stack_temporary (ptid_t ptid, struct value *v)
807 {
808 struct thread_info *tp = find_thread_ptid (ptid);
809
810 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
811 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
812 }
813
814 /* Return 1 if VAL is among the stack temporaries of the thread
815 with id PTID. Return 0 otherwise. */
816
817 int
818 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
819 {
820 struct thread_info *tp = find_thread_ptid (ptid);
821
822 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
823 if (!VEC_empty (value_ptr, tp->stack_temporaries))
824 {
825 struct value *v;
826 int i;
827
828 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
829 if (v == val)
830 return 1;
831 }
832
833 return 0;
834 }
835
836 /* Return the last of the stack temporaries for thread with id PTID.
837 Return NULL if there are no stack temporaries for the thread. */
838
839 struct value *
840 get_last_thread_stack_temporary (ptid_t ptid)
841 {
842 struct value *lastval = NULL;
843 struct thread_info *tp = find_thread_ptid (ptid);
844
845 gdb_assert (tp != NULL);
846 if (!VEC_empty (value_ptr, tp->stack_temporaries))
847 lastval = VEC_last (value_ptr, tp->stack_temporaries);
848
849 return lastval;
850 }
851
852 void
853 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
854 {
855 struct inferior *inf;
856 struct thread_info *tp;
857
858 /* It can happen that what we knew as the target inferior id
859 changes. E.g, target remote may only discover the remote process
860 pid after adding the inferior to GDB's list. */
861 inf = find_inferior_ptid (old_ptid);
862 inf->pid = ptid_get_pid (new_ptid);
863
864 tp = find_thread_ptid (old_ptid);
865 tp->ptid = new_ptid;
866
867 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
868 }
869
870 /* See gdbthread.h. */
871
872 void
873 set_resumed (ptid_t ptid, int resumed)
874 {
875 struct thread_info *tp;
876 int all = ptid_equal (ptid, minus_one_ptid);
877
878 if (all || ptid_is_pid (ptid))
879 {
880 for (tp = thread_list; tp; tp = tp->next)
881 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
882 tp->resumed = resumed;
883 }
884 else
885 {
886 tp = find_thread_ptid (ptid);
887 gdb_assert (tp != NULL);
888 tp->resumed = resumed;
889 }
890 }
891
892 /* Helper for set_running, that marks one thread either running or
893 stopped. */
894
895 static int
896 set_running_thread (struct thread_info *tp, int running)
897 {
898 int started = 0;
899
900 if (running && tp->state == THREAD_STOPPED)
901 started = 1;
902 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
903
904 if (!running)
905 {
906 /* If the thread is now marked stopped, remove it from
907 the step-over queue, so that we don't try to resume
908 it until the user wants it to. */
909 if (tp->step_over_next != NULL)
910 thread_step_over_chain_remove (tp);
911 }
912
913 return started;
914 }
915
916 void
917 set_running (ptid_t ptid, int running)
918 {
919 struct thread_info *tp;
920 int all = ptid_equal (ptid, minus_one_ptid);
921 int any_started = 0;
922
923 /* We try not to notify the observer if no thread has actually changed
924 the running state -- merely to reduce the number of messages to
925 frontend. Frontend is supposed to handle multiple *running just fine. */
926 if (all || ptid_is_pid (ptid))
927 {
928 for (tp = thread_list; tp; tp = tp->next)
929 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
930 {
931 if (tp->state == THREAD_EXITED)
932 continue;
933
934 if (set_running_thread (tp, running))
935 any_started = 1;
936 }
937 }
938 else
939 {
940 tp = find_thread_ptid (ptid);
941 gdb_assert (tp != NULL);
942 gdb_assert (tp->state != THREAD_EXITED);
943 if (set_running_thread (tp, running))
944 any_started = 1;
945 }
946 if (any_started)
947 observer_notify_target_resumed (ptid);
948 }
949
950 static int
951 is_thread_state (ptid_t ptid, enum thread_state state)
952 {
953 struct thread_info *tp;
954
955 tp = find_thread_ptid (ptid);
956 gdb_assert (tp);
957 return tp->state == state;
958 }
959
960 int
961 is_stopped (ptid_t ptid)
962 {
963 return is_thread_state (ptid, THREAD_STOPPED);
964 }
965
966 int
967 is_exited (ptid_t ptid)
968 {
969 return is_thread_state (ptid, THREAD_EXITED);
970 }
971
972 int
973 is_running (ptid_t ptid)
974 {
975 return is_thread_state (ptid, THREAD_RUNNING);
976 }
977
978 int
979 is_executing (ptid_t ptid)
980 {
981 struct thread_info *tp;
982
983 tp = find_thread_ptid (ptid);
984 gdb_assert (tp);
985 return tp->executing;
986 }
987
988 void
989 set_executing (ptid_t ptid, int executing)
990 {
991 struct thread_info *tp;
992 int all = ptid_equal (ptid, minus_one_ptid);
993
994 if (all || ptid_is_pid (ptid))
995 {
996 for (tp = thread_list; tp; tp = tp->next)
997 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
998 tp->executing = executing;
999 }
1000 else
1001 {
1002 tp = find_thread_ptid (ptid);
1003 gdb_assert (tp);
1004 tp->executing = executing;
1005 }
1006
1007 /* It only takes one running thread to spawn more threads.*/
1008 if (executing)
1009 threads_executing = 1;
1010 /* Only clear the flag if the caller is telling us everything is
1011 stopped. */
1012 else if (ptid_equal (minus_one_ptid, ptid))
1013 threads_executing = 0;
1014 }
1015
1016 /* See gdbthread.h. */
1017
1018 int
1019 threads_are_executing (void)
1020 {
1021 return threads_executing;
1022 }
1023
1024 void
1025 set_stop_requested (ptid_t ptid, int stop)
1026 {
1027 struct thread_info *tp;
1028 int all = ptid_equal (ptid, minus_one_ptid);
1029
1030 if (all || ptid_is_pid (ptid))
1031 {
1032 for (tp = thread_list; tp; tp = tp->next)
1033 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1034 tp->stop_requested = stop;
1035 }
1036 else
1037 {
1038 tp = find_thread_ptid (ptid);
1039 gdb_assert (tp);
1040 tp->stop_requested = stop;
1041 }
1042
1043 /* Call the stop requested observer so other components of GDB can
1044 react to this request. */
1045 if (stop)
1046 observer_notify_thread_stop_requested (ptid);
1047 }
1048
1049 void
1050 finish_thread_state (ptid_t ptid)
1051 {
1052 struct thread_info *tp;
1053 int all;
1054 int any_started = 0;
1055
1056 all = ptid_equal (ptid, minus_one_ptid);
1057
1058 if (all || ptid_is_pid (ptid))
1059 {
1060 for (tp = thread_list; tp; tp = tp->next)
1061 {
1062 if (tp->state == THREAD_EXITED)
1063 continue;
1064 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1065 {
1066 if (set_running_thread (tp, tp->executing))
1067 any_started = 1;
1068 }
1069 }
1070 }
1071 else
1072 {
1073 tp = find_thread_ptid (ptid);
1074 gdb_assert (tp);
1075 if (tp->state != THREAD_EXITED)
1076 {
1077 if (set_running_thread (tp, tp->executing))
1078 any_started = 1;
1079 }
1080 }
1081
1082 if (any_started)
1083 observer_notify_target_resumed (ptid);
1084 }
1085
1086 void
1087 finish_thread_state_cleanup (void *arg)
1088 {
1089 ptid_t *ptid_p = arg;
1090
1091 gdb_assert (arg);
1092
1093 finish_thread_state (*ptid_p);
1094 }
1095
1096 int
1097 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1098 {
1099 return (pc >= thread->control.step_range_start
1100 && pc < thread->control.step_range_end);
1101 }
1102
1103 /* Prints the list of threads and their details on UIOUT.
1104 This is a version of 'info_threads_command' suitable for
1105 use from MI.
1106 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1107 that should be printed. Otherwise, all threads are
1108 printed.
1109 If PID is not -1, only print threads from the process PID.
1110 Otherwise, threads from all attached PIDs are printed.
1111 If both REQUESTED_THREAD and PID are not -1, then the thread
1112 is printed if it belongs to the specified process. Otherwise,
1113 an error is raised. */
1114 void
1115 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1116 {
1117 struct thread_info *tp;
1118 ptid_t current_ptid;
1119 struct cleanup *old_chain;
1120 char *extra_info, *name, *target_id;
1121 int current_thread = -1;
1122
1123 update_thread_list ();
1124 current_ptid = inferior_ptid;
1125
1126 /* We'll be switching threads temporarily. */
1127 old_chain = make_cleanup_restore_current_thread ();
1128
1129 /* For backward compatibility, we make a list for MI. A table is
1130 preferable for the CLI, though, because it shows table
1131 headers. */
1132 if (ui_out_is_mi_like_p (uiout))
1133 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1134 else
1135 {
1136 int n_threads = 0;
1137
1138 for (tp = thread_list; tp; tp = tp->next)
1139 {
1140 if (!number_is_in_list (requested_threads, tp->num))
1141 continue;
1142
1143 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1144 continue;
1145
1146 if (tp->state == THREAD_EXITED)
1147 continue;
1148
1149 ++n_threads;
1150 }
1151
1152 if (n_threads == 0)
1153 {
1154 if (requested_threads == NULL || *requested_threads == '\0')
1155 ui_out_message (uiout, 0, _("No threads.\n"));
1156 else
1157 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1158 requested_threads);
1159 do_cleanups (old_chain);
1160 return;
1161 }
1162
1163 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1164
1165 ui_out_table_header (uiout, 1, ui_left, "current", "");
1166 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1167 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1168 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1169 ui_out_table_body (uiout);
1170 }
1171
1172 for (tp = thread_list; tp; tp = tp->next)
1173 {
1174 struct cleanup *chain2;
1175 int core;
1176
1177 if (!number_is_in_list (requested_threads, tp->num))
1178 continue;
1179
1180 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1181 {
1182 if (requested_threads != NULL && *requested_threads != '\0')
1183 error (_("Requested thread not found in requested process"));
1184 continue;
1185 }
1186
1187 if (ptid_equal (tp->ptid, current_ptid))
1188 current_thread = tp->num;
1189
1190 if (tp->state == THREAD_EXITED)
1191 continue;
1192
1193 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1194
1195 if (ui_out_is_mi_like_p (uiout))
1196 {
1197 /* Compatibility. */
1198 if (ptid_equal (tp->ptid, current_ptid))
1199 ui_out_text (uiout, "* ");
1200 else
1201 ui_out_text (uiout, " ");
1202 }
1203 else
1204 {
1205 if (ptid_equal (tp->ptid, current_ptid))
1206 ui_out_field_string (uiout, "current", "*");
1207 else
1208 ui_out_field_skip (uiout, "current");
1209 }
1210
1211 ui_out_field_int (uiout, "id", tp->num);
1212
1213 /* For the CLI, we stuff everything into the target-id field.
1214 This is a gross hack to make the output come out looking
1215 correct. The underlying problem here is that ui-out has no
1216 way to specify that a field's space allocation should be
1217 shared by several fields. For MI, we do the right thing
1218 instead. */
1219
1220 target_id = target_pid_to_str (tp->ptid);
1221 extra_info = target_extra_thread_info (tp);
1222 name = tp->name ? tp->name : target_thread_name (tp);
1223
1224 if (ui_out_is_mi_like_p (uiout))
1225 {
1226 ui_out_field_string (uiout, "target-id", target_id);
1227 if (extra_info)
1228 ui_out_field_string (uiout, "details", extra_info);
1229 if (name)
1230 ui_out_field_string (uiout, "name", name);
1231 }
1232 else
1233 {
1234 struct cleanup *str_cleanup;
1235 char *contents;
1236
1237 if (extra_info && name)
1238 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1239 name, extra_info);
1240 else if (extra_info)
1241 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1242 else if (name)
1243 contents = xstrprintf ("%s \"%s\"", target_id, name);
1244 else
1245 contents = xstrdup (target_id);
1246 str_cleanup = make_cleanup (xfree, contents);
1247
1248 ui_out_field_string (uiout, "target-id", contents);
1249 do_cleanups (str_cleanup);
1250 }
1251
1252 if (tp->state == THREAD_RUNNING)
1253 ui_out_text (uiout, "(running)\n");
1254 else
1255 {
1256 /* The switch below puts us at the top of the stack (leaf
1257 frame). */
1258 switch_to_thread (tp->ptid);
1259 print_stack_frame (get_selected_frame (NULL),
1260 /* For MI output, print frame level. */
1261 ui_out_is_mi_like_p (uiout),
1262 LOCATION, 0);
1263 }
1264
1265 if (ui_out_is_mi_like_p (uiout))
1266 {
1267 char *state = "stopped";
1268
1269 if (tp->state == THREAD_RUNNING)
1270 state = "running";
1271 ui_out_field_string (uiout, "state", state);
1272 }
1273
1274 core = target_core_of_thread (tp->ptid);
1275 if (ui_out_is_mi_like_p (uiout) && core != -1)
1276 ui_out_field_int (uiout, "core", core);
1277
1278 do_cleanups (chain2);
1279 }
1280
1281 /* Restores the current thread and the frame selected before
1282 the "info threads" command. */
1283 do_cleanups (old_chain);
1284
1285 if (pid == -1 && requested_threads == NULL)
1286 {
1287 gdb_assert (current_thread != -1
1288 || !thread_list
1289 || ptid_equal (inferior_ptid, null_ptid));
1290 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1291 ui_out_field_int (uiout, "current-thread-id", current_thread);
1292
1293 if (current_thread != -1 && is_exited (current_ptid))
1294 ui_out_message (uiout, 0, "\n\
1295 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1296 current_thread);
1297 else if (thread_list
1298 && current_thread == -1
1299 && ptid_equal (current_ptid, null_ptid))
1300 ui_out_message (uiout, 0, "\n\
1301 No selected thread. See `help thread'.\n");
1302 }
1303 }
1304
1305 /* Print information about currently known threads
1306
1307 Optional ARG is a thread id, or list of thread ids.
1308
1309 Note: this has the drawback that it _really_ switches
1310 threads, which frees the frame cache. A no-side
1311 effects info-threads command would be nicer. */
1312
1313 static void
1314 info_threads_command (char *arg, int from_tty)
1315 {
1316 print_thread_info (current_uiout, arg, -1);
1317 }
1318
1319 /* Switch from one thread to another. */
1320
1321 void
1322 switch_to_thread (ptid_t ptid)
1323 {
1324 /* Switch the program space as well, if we can infer it from the now
1325 current thread. Otherwise, it's up to the caller to select the
1326 space it wants. */
1327 if (!ptid_equal (ptid, null_ptid))
1328 {
1329 struct inferior *inf;
1330
1331 inf = find_inferior_ptid (ptid);
1332 gdb_assert (inf != NULL);
1333 set_current_program_space (inf->pspace);
1334 set_current_inferior (inf);
1335 }
1336
1337 if (ptid_equal (ptid, inferior_ptid))
1338 return;
1339
1340 inferior_ptid = ptid;
1341 reinit_frame_cache ();
1342
1343 /* We don't check for is_stopped, because we're called at times
1344 while in the TARGET_RUNNING state, e.g., while handling an
1345 internal event. */
1346 if (!ptid_equal (inferior_ptid, null_ptid)
1347 && !is_exited (ptid)
1348 && !is_executing (ptid))
1349 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1350 else
1351 stop_pc = ~(CORE_ADDR) 0;
1352 }
1353
1354 static void
1355 restore_current_thread (ptid_t ptid)
1356 {
1357 switch_to_thread (ptid);
1358 }
1359
1360 static void
1361 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1362 {
1363 struct frame_info *frame = NULL;
1364 int count;
1365
1366 /* This means there was no selected frame. */
1367 if (frame_level == -1)
1368 {
1369 select_frame (NULL);
1370 return;
1371 }
1372
1373 gdb_assert (frame_level >= 0);
1374
1375 /* Restore by level first, check if the frame id is the same as
1376 expected. If that fails, try restoring by frame id. If that
1377 fails, nothing to do, just warn the user. */
1378
1379 count = frame_level;
1380 frame = find_relative_frame (get_current_frame (), &count);
1381 if (count == 0
1382 && frame != NULL
1383 /* The frame ids must match - either both valid or both outer_frame_id.
1384 The latter case is not failsafe, but since it's highly unlikely
1385 the search by level finds the wrong frame, it's 99.9(9)% of
1386 the time (for all practical purposes) safe. */
1387 && frame_id_eq (get_frame_id (frame), a_frame_id))
1388 {
1389 /* Cool, all is fine. */
1390 select_frame (frame);
1391 return;
1392 }
1393
1394 frame = frame_find_by_id (a_frame_id);
1395 if (frame != NULL)
1396 {
1397 /* Cool, refound it. */
1398 select_frame (frame);
1399 return;
1400 }
1401
1402 /* Nothing else to do, the frame layout really changed. Select the
1403 innermost stack frame. */
1404 select_frame (get_current_frame ());
1405
1406 /* Warn the user. */
1407 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1408 {
1409 warning (_("Couldn't restore frame #%d in "
1410 "current thread. Bottom (innermost) frame selected:"),
1411 frame_level);
1412 /* For MI, we should probably have a notification about
1413 current frame change. But this error is not very
1414 likely, so don't bother for now. */
1415 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1416 }
1417 }
1418
1419 /* Data used by the cleanup installed by
1420 'make_cleanup_restore_current_thread'. */
1421
1422 struct current_thread_cleanup
1423 {
1424 /* Next in list of currently installed 'struct
1425 current_thread_cleanup' cleanups. See
1426 'current_thread_cleanup_chain' below. */
1427 struct current_thread_cleanup *next;
1428
1429 ptid_t inferior_ptid;
1430 struct frame_id selected_frame_id;
1431 int selected_frame_level;
1432 int was_stopped;
1433 int inf_id;
1434 int was_removable;
1435 };
1436
1437 /* A chain of currently installed 'struct current_thread_cleanup'
1438 cleanups. Restoring the previously selected thread looks up the
1439 old thread in the thread list by ptid. If the thread changes ptid,
1440 we need to update the cleanup's thread structure so the look up
1441 succeeds. */
1442 static struct current_thread_cleanup *current_thread_cleanup_chain;
1443
1444 /* A thread_ptid_changed observer. Update all currently installed
1445 current_thread_cleanup cleanups that want to switch back to
1446 OLD_PTID to switch back to NEW_PTID instead. */
1447
1448 static void
1449 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1450 {
1451 struct current_thread_cleanup *it;
1452
1453 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1454 {
1455 if (ptid_equal (it->inferior_ptid, old_ptid))
1456 it->inferior_ptid = new_ptid;
1457 }
1458 }
1459
1460 static void
1461 do_restore_current_thread_cleanup (void *arg)
1462 {
1463 struct thread_info *tp;
1464 struct current_thread_cleanup *old = arg;
1465
1466 tp = find_thread_ptid (old->inferior_ptid);
1467
1468 /* If the previously selected thread belonged to a process that has
1469 in the mean time been deleted (due to normal exit, detach, etc.),
1470 then don't revert back to it, but instead simply drop back to no
1471 thread selected. */
1472 if (tp
1473 && find_inferior_ptid (tp->ptid) != NULL)
1474 restore_current_thread (old->inferior_ptid);
1475 else
1476 {
1477 restore_current_thread (null_ptid);
1478 set_current_inferior (find_inferior_id (old->inf_id));
1479 }
1480
1481 /* The running state of the originally selected thread may have
1482 changed, so we have to recheck it here. */
1483 if (!ptid_equal (inferior_ptid, null_ptid)
1484 && old->was_stopped
1485 && is_stopped (inferior_ptid)
1486 && target_has_registers
1487 && target_has_stack
1488 && target_has_memory)
1489 restore_selected_frame (old->selected_frame_id,
1490 old->selected_frame_level);
1491 }
1492
1493 static void
1494 restore_current_thread_cleanup_dtor (void *arg)
1495 {
1496 struct current_thread_cleanup *old = arg;
1497 struct thread_info *tp;
1498 struct inferior *inf;
1499
1500 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1501
1502 tp = find_thread_ptid (old->inferior_ptid);
1503 if (tp)
1504 tp->refcount--;
1505 inf = find_inferior_id (old->inf_id);
1506 if (inf != NULL)
1507 inf->removable = old->was_removable;
1508 xfree (old);
1509 }
1510
1511 /* Set the thread reference count. */
1512
1513 static void
1514 set_thread_refcount (void *data)
1515 {
1516 int k;
1517 struct thread_array_cleanup *ta_cleanup = data;
1518
1519 for (k = 0; k != ta_cleanup->count; k++)
1520 ta_cleanup->tp_array[k]->refcount--;
1521 }
1522
1523 struct cleanup *
1524 make_cleanup_restore_current_thread (void)
1525 {
1526 struct thread_info *tp;
1527 struct frame_info *frame;
1528 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1529
1530 old->inferior_ptid = inferior_ptid;
1531 old->inf_id = current_inferior ()->num;
1532 old->was_removable = current_inferior ()->removable;
1533
1534 old->next = current_thread_cleanup_chain;
1535 current_thread_cleanup_chain = old;
1536
1537 if (!ptid_equal (inferior_ptid, null_ptid))
1538 {
1539 old->was_stopped = is_stopped (inferior_ptid);
1540 if (old->was_stopped
1541 && target_has_registers
1542 && target_has_stack
1543 && target_has_memory)
1544 {
1545 /* When processing internal events, there might not be a
1546 selected frame. If we naively call get_selected_frame
1547 here, then we can end up reading debuginfo for the
1548 current frame, but we don't generally need the debuginfo
1549 at this point. */
1550 frame = get_selected_frame_if_set ();
1551 }
1552 else
1553 frame = NULL;
1554
1555 old->selected_frame_id = get_frame_id (frame);
1556 old->selected_frame_level = frame_relative_level (frame);
1557
1558 tp = find_thread_ptid (inferior_ptid);
1559 if (tp)
1560 tp->refcount++;
1561 }
1562
1563 current_inferior ()->removable = 0;
1564
1565 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1566 restore_current_thread_cleanup_dtor);
1567 }
1568
1569 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1570 descending order. */
1571
1572 static int tp_array_compar_ascending;
1573
1574 /* Sort an array for struct thread_info pointers by their NUM, order is
1575 determined by TP_ARRAY_COMPAR_ASCENDING. */
1576
1577 static int
1578 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1579 {
1580 const struct thread_info *const *ap = ap_voidp;
1581 const struct thread_info *const *bp = bp_voidp;
1582
1583 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1584 * (tp_array_compar_ascending ? +1 : -1));
1585 }
1586
1587 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1588 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1589 of two numbers seperated by a hyphen. Examples:
1590
1591 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1592 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1593 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1594
1595 static void
1596 thread_apply_all_command (char *cmd, int from_tty)
1597 {
1598 struct cleanup *old_chain;
1599 char *saved_cmd;
1600 int tc;
1601 struct thread_array_cleanup ta_cleanup;
1602
1603 tp_array_compar_ascending = 0;
1604 if (cmd != NULL
1605 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1606 {
1607 cmd = skip_spaces (cmd);
1608 tp_array_compar_ascending = 1;
1609 }
1610
1611 if (cmd == NULL || *cmd == '\000')
1612 error (_("Please specify a command following the thread ID list"));
1613
1614 update_thread_list ();
1615
1616 old_chain = make_cleanup_restore_current_thread ();
1617
1618 /* Save a copy of the command in case it is clobbered by
1619 execute_command. */
1620 saved_cmd = xstrdup (cmd);
1621 make_cleanup (xfree, saved_cmd);
1622
1623 /* Note this includes exited threads. */
1624 tc = thread_count ();
1625 if (tc != 0)
1626 {
1627 struct thread_info **tp_array;
1628 struct thread_info *tp;
1629 int i = 0, k;
1630
1631 /* Save a copy of the thread_list in case we execute detach
1632 command. */
1633 tp_array = XNEWVEC (struct thread_info *, tc);
1634 make_cleanup (xfree, tp_array);
1635
1636 ALL_NON_EXITED_THREADS (tp)
1637 {
1638 tp_array[i] = tp;
1639 tp->refcount++;
1640 i++;
1641 }
1642 /* Because we skipped exited threads, we may end up with fewer
1643 threads in the array than the total count of threads. */
1644 gdb_assert (i <= tc);
1645
1646 if (i != 0)
1647 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1648
1649 ta_cleanup.tp_array = tp_array;
1650 ta_cleanup.count = i;
1651 make_cleanup (set_thread_refcount, &ta_cleanup);
1652
1653 for (k = 0; k != i; k++)
1654 if (thread_alive (tp_array[k]))
1655 {
1656 switch_to_thread (tp_array[k]->ptid);
1657 printf_filtered (_("\nThread %d (%s):\n"),
1658 tp_array[k]->num,
1659 target_pid_to_str (inferior_ptid));
1660 execute_command (cmd, from_tty);
1661
1662 /* Restore exact command used previously. */
1663 strcpy (cmd, saved_cmd);
1664 }
1665 }
1666
1667 do_cleanups (old_chain);
1668 }
1669
1670 static void
1671 thread_apply_command (char *tidlist, int from_tty)
1672 {
1673 char *cmd;
1674 struct cleanup *old_chain;
1675 char *saved_cmd;
1676 struct get_number_or_range_state state;
1677
1678 if (tidlist == NULL || *tidlist == '\000')
1679 error (_("Please specify a thread ID list"));
1680
1681 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1682
1683 if (*cmd == '\000')
1684 error (_("Please specify a command following the thread ID list"));
1685
1686 /* Save a copy of the command in case it is clobbered by
1687 execute_command. */
1688 saved_cmd = xstrdup (cmd);
1689 old_chain = make_cleanup (xfree, saved_cmd);
1690
1691 init_number_or_range (&state, tidlist);
1692 while (!state.finished && state.string < cmd)
1693 {
1694 struct thread_info *tp;
1695 int start;
1696
1697 start = get_number_or_range (&state);
1698
1699 make_cleanup_restore_current_thread ();
1700
1701 tp = find_thread_id (start);
1702
1703 if (!tp)
1704 warning (_("Unknown thread %d."), start);
1705 else if (!thread_alive (tp))
1706 warning (_("Thread %d has terminated."), start);
1707 else
1708 {
1709 switch_to_thread (tp->ptid);
1710
1711 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1712 target_pid_to_str (inferior_ptid));
1713 execute_command (cmd, from_tty);
1714
1715 /* Restore exact command used previously. */
1716 strcpy (cmd, saved_cmd);
1717 }
1718 }
1719
1720 do_cleanups (old_chain);
1721 }
1722
1723 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1724 if prefix of arg is `apply'. */
1725
1726 void
1727 thread_command (char *tidstr, int from_tty)
1728 {
1729 if (!tidstr)
1730 {
1731 if (ptid_equal (inferior_ptid, null_ptid))
1732 error (_("No thread selected"));
1733
1734 if (target_has_stack)
1735 {
1736 if (is_exited (inferior_ptid))
1737 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1738 pid_to_thread_id (inferior_ptid),
1739 target_pid_to_str (inferior_ptid));
1740 else
1741 printf_filtered (_("[Current thread is %d (%s)]\n"),
1742 pid_to_thread_id (inferior_ptid),
1743 target_pid_to_str (inferior_ptid));
1744 }
1745 else
1746 error (_("No stack."));
1747 return;
1748 }
1749
1750 gdb_thread_select (current_uiout, tidstr, NULL);
1751 }
1752
1753 /* Implementation of `thread name'. */
1754
1755 static void
1756 thread_name_command (char *arg, int from_tty)
1757 {
1758 struct thread_info *info;
1759
1760 if (ptid_equal (inferior_ptid, null_ptid))
1761 error (_("No thread selected"));
1762
1763 arg = skip_spaces (arg);
1764
1765 info = inferior_thread ();
1766 xfree (info->name);
1767 info->name = arg ? xstrdup (arg) : NULL;
1768 }
1769
1770 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1771
1772 static void
1773 thread_find_command (char *arg, int from_tty)
1774 {
1775 struct thread_info *tp;
1776 char *tmp;
1777 unsigned long match = 0;
1778
1779 if (arg == NULL || *arg == '\0')
1780 error (_("Command requires an argument."));
1781
1782 tmp = re_comp (arg);
1783 if (tmp != 0)
1784 error (_("Invalid regexp (%s): %s"), tmp, arg);
1785
1786 update_thread_list ();
1787 for (tp = thread_list; tp; tp = tp->next)
1788 {
1789 if (tp->name != NULL && re_exec (tp->name))
1790 {
1791 printf_filtered (_("Thread %d has name '%s'\n"),
1792 tp->num, tp->name);
1793 match++;
1794 }
1795
1796 tmp = target_thread_name (tp);
1797 if (tmp != NULL && re_exec (tmp))
1798 {
1799 printf_filtered (_("Thread %d has target name '%s'\n"),
1800 tp->num, tmp);
1801 match++;
1802 }
1803
1804 tmp = target_pid_to_str (tp->ptid);
1805 if (tmp != NULL && re_exec (tmp))
1806 {
1807 printf_filtered (_("Thread %d has target id '%s'\n"),
1808 tp->num, tmp);
1809 match++;
1810 }
1811
1812 tmp = target_extra_thread_info (tp);
1813 if (tmp != NULL && re_exec (tmp))
1814 {
1815 printf_filtered (_("Thread %d has extra info '%s'\n"),
1816 tp->num, tmp);
1817 match++;
1818 }
1819 }
1820 if (!match)
1821 printf_filtered (_("No threads match '%s'\n"), arg);
1822 }
1823
1824 /* Print notices when new threads are attached and detached. */
1825 int print_thread_events = 1;
1826 static void
1827 show_print_thread_events (struct ui_file *file, int from_tty,
1828 struct cmd_list_element *c, const char *value)
1829 {
1830 fprintf_filtered (file,
1831 _("Printing of thread events is %s.\n"),
1832 value);
1833 }
1834
1835 static int
1836 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1837 {
1838 int num;
1839 struct thread_info *tp;
1840
1841 num = value_as_long (parse_and_eval (tidstr));
1842
1843 tp = find_thread_id (num);
1844
1845 if (!tp)
1846 error (_("Thread ID %d not known."), num);
1847
1848 if (!thread_alive (tp))
1849 error (_("Thread ID %d has terminated."), num);
1850
1851 switch_to_thread (tp->ptid);
1852
1853 annotate_thread_changed ();
1854
1855 ui_out_text (uiout, "[Switching to thread ");
1856 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1857 ui_out_text (uiout, " (");
1858 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1859 ui_out_text (uiout, ")]");
1860
1861 /* Note that we can't reach this with an exited thread, due to the
1862 thread_alive check above. */
1863 if (tp->state == THREAD_RUNNING)
1864 ui_out_text (uiout, "(running)\n");
1865 else
1866 {
1867 ui_out_text (uiout, "\n");
1868 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1869 }
1870
1871 /* Since the current thread may have changed, see if there is any
1872 exited thread we can now delete. */
1873 prune_threads ();
1874
1875 return GDB_RC_OK;
1876 }
1877
1878 enum gdb_rc
1879 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1880 {
1881 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1882 error_message, RETURN_MASK_ALL) < 0)
1883 return GDB_RC_FAIL;
1884 return GDB_RC_OK;
1885 }
1886
1887 /* Update the 'threads_executing' global based on the threads we know
1888 about right now. */
1889
1890 static void
1891 update_threads_executing (void)
1892 {
1893 struct thread_info *tp;
1894
1895 threads_executing = 0;
1896 ALL_NON_EXITED_THREADS (tp)
1897 {
1898 if (tp->executing)
1899 {
1900 threads_executing = 1;
1901 break;
1902 }
1903 }
1904 }
1905
1906 void
1907 update_thread_list (void)
1908 {
1909 target_update_thread_list ();
1910 update_threads_executing ();
1911 }
1912
1913 /* Return a new value for the selected thread's id. Return a value of 0 if
1914 no thread is selected, or no threads exist. */
1915
1916 static struct value *
1917 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1918 void *ignore)
1919 {
1920 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1921
1922 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1923 (tp ? tp->num : 0));
1924 }
1925
1926 /* Commands with a prefix of `thread'. */
1927 struct cmd_list_element *thread_cmd_list = NULL;
1928
1929 /* Implementation of `thread' variable. */
1930
1931 static const struct internalvar_funcs thread_funcs =
1932 {
1933 thread_id_make_value,
1934 NULL,
1935 NULL
1936 };
1937
1938 void
1939 _initialize_thread (void)
1940 {
1941 static struct cmd_list_element *thread_apply_list = NULL;
1942
1943 add_info ("threads", info_threads_command,
1944 _("Display currently known threads.\n\
1945 Usage: info threads [ID]...\n\
1946 Optional arguments are thread IDs with spaces between.\n\
1947 If no arguments, all threads are displayed."));
1948
1949 add_prefix_cmd ("thread", class_run, thread_command, _("\
1950 Use this command to switch between threads.\n\
1951 The new thread ID must be currently known."),
1952 &thread_cmd_list, "thread ", 1, &cmdlist);
1953
1954 add_prefix_cmd ("apply", class_run, thread_apply_command,
1955 _("Apply a command to a list of threads."),
1956 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1957
1958 add_cmd ("all", class_run, thread_apply_all_command,
1959 _("\
1960 Apply a command to all threads.\n\
1961 \n\
1962 Usage: thread apply all [-ascending] <command>\n\
1963 -ascending: Call <command> for all threads in ascending order.\n\
1964 The default is descending order.\
1965 "),
1966 &thread_apply_list);
1967
1968 add_cmd ("name", class_run, thread_name_command,
1969 _("Set the current thread's name.\n\
1970 Usage: thread name [NAME]\n\
1971 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1972
1973 add_cmd ("find", class_run, thread_find_command, _("\
1974 Find threads that match a regular expression.\n\
1975 Usage: thread find REGEXP\n\
1976 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1977 &thread_cmd_list);
1978
1979 add_com_alias ("t", "thread", class_run, 1);
1980
1981 add_setshow_boolean_cmd ("thread-events", no_class,
1982 &print_thread_events, _("\
1983 Set printing of thread events (such as thread start and exit)."), _("\
1984 Show printing of thread events (such as thread start and exit)."), NULL,
1985 NULL,
1986 show_print_thread_events,
1987 &setprintlist, &showprintlist);
1988
1989 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1990
1991 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);
1992 }