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