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