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