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