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