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