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