S/390: Move start of 64 bit binaries from 2GB to 256MB.
[binutils-gdb.git] / gdb / thread.c
bloba66a2b5338eea5f09db76a439db3405f38a1e5fd
1 /* Multi-process/thread control for GDB, the GNU debugger.
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7 This file is part of GDB.
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.
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.
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/>. */
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"
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"
48 /* Definition of struct thread_info exported to gdbthread.h. */
50 /* Prototypes for exported functions. */
52 void _initialize_thread (void);
54 /* Prototypes for local functions. */
56 struct thread_info *thread_list = NULL;
57 static int highest_thread_num;
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;
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);
72 /* Data to cleanup thread array. */
74 struct thread_array_cleanup
76 /* Array of thread pointers used to set
77 reference count. */
78 struct thread_info **tp_array;
80 /* Thread count in the array. */
81 int count;
85 struct thread_info*
86 inferior_thread (void)
88 struct thread_info *tp = find_thread_ptid (inferior_ptid);
89 gdb_assert (tp);
90 return tp;
93 /* Delete the breakpoint pointed at by BP_P, if there's one. */
95 static void
96 delete_thread_breakpoint (struct breakpoint **bp_p)
98 if (*bp_p != NULL)
100 delete_breakpoint (*bp_p);
101 *bp_p = NULL;
105 void
106 delete_step_resume_breakpoint (struct thread_info *tp)
108 if (tp != NULL)
109 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
112 void
113 delete_exception_resume_breakpoint (struct thread_info *tp)
115 if (tp != NULL)
116 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
119 /* See gdbthread.h. */
121 void
122 delete_single_step_breakpoints (struct thread_info *tp)
124 if (tp != NULL)
125 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
128 /* Delete the breakpoint pointed at by BP_P at the next stop, if
129 there's one. */
131 static void
132 delete_at_next_stop (struct breakpoint **bp)
134 if (*bp != NULL)
136 (*bp)->disposition = disp_del_at_next_stop;
137 *bp = NULL;
141 /* See gdbthread.h. */
144 thread_has_single_step_breakpoints_set (struct thread_info *tp)
146 return tp->control.single_step_breakpoints != NULL;
149 /* See gdbthread.h. */
152 thread_has_single_step_breakpoint_here (struct thread_info *tp,
153 struct address_space *aspace,
154 CORE_ADDR addr)
156 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
158 return (ss_bps != NULL
159 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
162 /* See gdbthread.h. */
164 void
165 thread_cancel_execution_command (struct thread_info *thr)
167 if (thr->thread_fsm != NULL)
169 thread_fsm_clean_up (thr->thread_fsm, thr);
170 thread_fsm_delete (thr->thread_fsm);
171 thr->thread_fsm = NULL;
175 static void
176 clear_thread_inferior_resources (struct thread_info *tp)
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);
186 delete_longjmp_breakpoint_at_next_stop (tp->global_num);
188 bpstat_clear (&tp->control.stop_bpstat);
190 btrace_teardown (tp);
192 thread_cancel_execution_command (tp);
195 static void
196 free_thread (struct thread_info *tp)
198 if (tp->priv)
200 if (tp->private_dtor)
201 tp->private_dtor (tp->priv);
202 else
203 xfree (tp->priv);
206 xfree (tp->name);
207 xfree (tp);
210 void
211 init_thread_list (void)
213 struct thread_info *tp, *tpnext;
215 highest_thread_num = 0;
217 if (!thread_list)
218 return;
220 for (tp = thread_list; tp; tp = tpnext)
222 tpnext = tp->next;
223 free_thread (tp);
226 thread_list = NULL;
227 threads_executing = 0;
230 /* Allocate a new thread of inferior INF with target id PTID and add
231 it to the thread list. */
233 static struct thread_info *
234 new_thread (struct inferior *inf, ptid_t ptid)
236 struct thread_info *tp;
238 gdb_assert (inf != NULL);
240 tp = XCNEW (struct thread_info);
242 tp->ptid = ptid;
243 tp->global_num = ++highest_thread_num;
244 tp->inf = inf;
245 tp->per_inf_num = ++inf->highest_thread_num;
247 if (thread_list == NULL)
248 thread_list = tp;
249 else
251 struct thread_info *last;
253 for (last = thread_list; last->next != NULL; last = last->next)
255 last->next = tp;
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;
263 return tp;
266 struct thread_info *
267 add_thread_silent (ptid_t ptid)
269 struct thread_info *tp;
270 struct inferior *inf = find_inferior_ptid (ptid);
271 gdb_assert (inf != NULL);
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. */
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. */
287 if (ptid_equal (inferior_ptid, ptid))
289 tp = new_thread (inf, null_ptid);
291 /* Make switch_to_thread not read from the thread. */
292 tp->state = THREAD_EXITED;
293 switch_to_thread (null_ptid);
295 /* Now we can delete it. */
296 delete_thread (ptid);
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);
303 observer_notify_new_thread (tp);
305 /* All done. */
306 return tp;
308 else
309 /* Just go ahead and delete it. */
310 delete_thread (ptid);
313 tp = new_thread (inf, ptid);
314 observer_notify_new_thread (tp);
316 return tp;
319 struct thread_info *
320 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
322 struct thread_info *result = add_thread_silent (ptid);
324 result->priv = priv;
326 if (print_thread_events)
327 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
329 annotate_new_thread ();
330 return result;
333 struct thread_info *
334 add_thread (ptid_t ptid)
336 return add_thread_with_info (ptid, NULL);
339 /* Add TP to the end of the step-over chain LIST_P. */
341 static void
342 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
344 gdb_assert (tp->step_over_next == NULL);
345 gdb_assert (tp->step_over_prev == NULL);
347 if (*list_p == NULL)
349 *list_p = tp;
350 tp->step_over_prev = tp->step_over_next = tp;
352 else
354 struct thread_info *head = *list_p;
355 struct thread_info *tail = head->step_over_prev;
357 tp->step_over_prev = tail;
358 tp->step_over_next = head;
359 head->step_over_prev = tp;
360 tail->step_over_next = tp;
364 /* Remove TP from step-over chain LIST_P. */
366 static void
367 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
369 gdb_assert (tp->step_over_next != NULL);
370 gdb_assert (tp->step_over_prev != NULL);
372 if (*list_p == tp)
374 if (tp == tp->step_over_next)
375 *list_p = NULL;
376 else
377 *list_p = tp->step_over_next;
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;
385 /* See gdbthread.h. */
387 struct thread_info *
388 thread_step_over_chain_next (struct thread_info *tp)
390 struct thread_info *next = tp->step_over_next;
392 return (next == step_over_queue_head ? NULL : next);
395 /* See gdbthread.h. */
398 thread_is_in_step_over_chain (struct thread_info *tp)
400 return (tp->step_over_next != NULL);
403 /* See gdbthread.h. */
405 void
406 thread_step_over_chain_enqueue (struct thread_info *tp)
408 step_over_chain_enqueue (&step_over_queue_head, tp);
411 /* See gdbthread.h. */
413 void
414 thread_step_over_chain_remove (struct thread_info *tp)
416 step_over_chain_remove (&step_over_queue_head, tp);
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)
424 struct thread_info *tp, *tpprev;
426 tpprev = NULL;
428 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
429 if (ptid_equal (tp->ptid, ptid))
430 break;
432 if (!tp)
433 return;
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);
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))
445 if (tp->state != THREAD_EXITED)
447 observer_notify_thread_exit (tp, silent);
449 /* Tag it as exited. */
450 tp->state = THREAD_EXITED;
452 /* Clear breakpoints, etc. associated with this thread. */
453 clear_thread_inferior_resources (tp);
456 /* Will be really deleted some other time. */
457 return;
460 /* Notify thread exit, but only if we haven't already. */
461 if (tp->state != THREAD_EXITED)
462 observer_notify_thread_exit (tp, silent);
464 /* Tag it as exited. */
465 tp->state = THREAD_EXITED;
466 clear_thread_inferior_resources (tp);
468 if (tpprev)
469 tpprev->next = tp->next;
470 else
471 thread_list = tp->next;
473 free_thread (tp);
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)
483 delete_thread_1 (ptid, 0 /* not silent */);
486 void
487 delete_thread_silent (ptid_t ptid)
489 delete_thread_1 (ptid, 1 /* silent */);
492 struct thread_info *
493 find_thread_global_id (int global_id)
495 struct thread_info *tp;
497 for (tp = thread_list; tp; tp = tp->next)
498 if (tp->global_num == global_id)
499 return tp;
501 return NULL;
504 static struct thread_info *
505 find_thread_id (struct inferior *inf, int thr_num)
507 struct thread_info *tp;
509 for (tp = thread_list; tp; tp = tp->next)
510 if (tp->inf == inf && tp->per_inf_num == thr_num)
511 return tp;
513 return NULL;
516 /* Find a thread_info by matching PTID. */
517 struct thread_info *
518 find_thread_ptid (ptid_t ptid)
520 struct thread_info *tp;
522 for (tp = thread_list; tp; tp = tp->next)
523 if (ptid_equal (tp->ptid, ptid))
524 return tp;
526 return NULL;
530 * Thread iterator function.
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.
539 * FIXME: some of the existing functionality, such as
540 * "Thread apply all", might be rewritten using this functionality.
543 struct thread_info *
544 iterate_over_threads (int (*callback) (struct thread_info *, void *),
545 void *data)
547 struct thread_info *tp, *next;
549 for (tp = thread_list; tp; tp = next)
551 next = tp->next;
552 if ((*callback) (tp, data))
553 return tp;
556 return NULL;
560 thread_count (void)
562 int result = 0;
563 struct thread_info *tp;
565 for (tp = thread_list; tp; tp = tp->next)
566 ++result;
568 return result;
572 valid_global_thread_id (int global_id)
574 struct thread_info *tp;
576 for (tp = thread_list; tp; tp = tp->next)
577 if (tp->global_num == global_id)
578 return 1;
580 return 0;
584 ptid_to_global_thread_id (ptid_t ptid)
586 struct thread_info *tp;
588 for (tp = thread_list; tp; tp = tp->next)
589 if (ptid_equal (tp->ptid, ptid))
590 return tp->global_num;
592 return 0;
595 ptid_t
596 global_thread_id_to_ptid (int global_id)
598 struct thread_info *thread = find_thread_global_id (global_id);
600 if (thread)
601 return thread->ptid;
602 else
603 return minus_one_ptid;
607 in_thread_list (ptid_t ptid)
609 struct thread_info *tp;
611 for (tp = thread_list; tp; tp = tp->next)
612 if (ptid_equal (tp->ptid, ptid))
613 return 1;
615 return 0; /* Never heard of 'im. */
618 /* Finds the first thread of the inferior given by PID. If PID is -1,
619 return the first thread in the list. */
621 struct thread_info *
622 first_thread_of_process (int pid)
624 struct thread_info *tp, *ret = NULL;
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;
631 return ret;
634 struct thread_info *
635 any_thread_of_process (int pid)
637 struct thread_info *tp;
639 gdb_assert (pid != 0);
641 /* Prefer the current thread. */
642 if (ptid_get_pid (inferior_ptid) == pid)
643 return inferior_thread ();
645 ALL_NON_EXITED_THREADS (tp)
646 if (ptid_get_pid (tp->ptid) == pid)
647 return tp;
649 return NULL;
652 struct thread_info *
653 any_live_thread_of_process (int pid)
655 struct thread_info *curr_tp = NULL;
656 struct thread_info *tp;
657 struct thread_info *tp_executing = NULL;
659 gdb_assert (pid != 0);
661 /* Prefer the current thread if it's not executing. */
662 if (ptid_get_pid (inferior_ptid) == pid)
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;
674 ALL_NON_EXITED_THREADS (tp)
675 if (ptid_get_pid (tp->ptid) == pid)
677 if (!tp->executing)
678 return tp;
680 tp_executing = tp;
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;
688 /* Otherwise, just return an executing thread, if any. */
689 return tp_executing;
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)
697 struct thread_info *tp;
698 int num = 0;
699 struct cleanup *cleanup_chain;
700 int current_thread = -1;
702 update_thread_list ();
704 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
706 for (tp = thread_list; tp; tp = tp->next)
708 if (tp->state == THREAD_EXITED)
709 continue;
711 if (ptid_equal (tp->ptid, inferior_ptid))
712 current_thread = tp->global_num;
714 num++;
715 ui_out_field_int (uiout, "thread-id", tp->global_num);
718 do_cleanups (cleanup_chain);
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;
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)
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;
737 /* Return true if TP is an active thread. */
738 static int
739 thread_alive (struct thread_info *tp)
741 if (tp->state == THREAD_EXITED)
742 return 0;
743 if (!target_thread_alive (tp->ptid))
744 return 0;
745 return 1;
748 /* See gdbthreads.h. */
750 void
751 prune_threads (void)
753 struct thread_info *tp, *tmp;
755 ALL_THREADS_SAFE (tp, tmp)
757 if (!thread_alive (tp))
758 delete_thread (tp->ptid);
762 /* See gdbthreads.h. */
764 void
765 delete_exited_threads (void)
767 struct thread_info *tp, *tmp;
769 ALL_THREADS_SAFE (tp, tmp)
771 if (tp->state == THREAD_EXITED)
772 delete_thread (tp->ptid);
776 /* Disable storing stack temporaries for the thread whose id is
777 stored in DATA. */
779 static void
780 disable_thread_stack_temporaries (void *data)
782 ptid_t *pd = (ptid_t *) data;
783 struct thread_info *tp = find_thread_ptid (*pd);
785 if (tp != NULL)
787 tp->stack_temporaries_enabled = 0;
788 VEC_free (value_ptr, tp->stack_temporaries);
791 xfree (pd);
794 /* Enable storing stack temporaries for thread with id PTID and return a
795 cleanup which can disable and clear the stack temporaries. */
797 struct cleanup *
798 enable_thread_stack_temporaries (ptid_t ptid)
800 struct thread_info *tp = find_thread_ptid (ptid);
801 ptid_t *data;
802 struct cleanup *c;
804 gdb_assert (tp != NULL);
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);
812 return c;
815 /* Return non-zero value if stack temporaies are enabled for the thread
816 with id PTID. */
819 thread_stack_temporaries_enabled_p (ptid_t ptid)
821 struct thread_info *tp = find_thread_ptid (ptid);
823 if (tp == NULL)
824 return 0;
825 else
826 return tp->stack_temporaries_enabled;
829 /* Push V on to the stack temporaries of the thread with id PTID. */
831 void
832 push_thread_stack_temporary (ptid_t ptid, struct value *v)
834 struct thread_info *tp = find_thread_ptid (ptid);
836 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
837 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
840 /* Return 1 if VAL is among the stack temporaries of the thread
841 with id PTID. Return 0 otherwise. */
844 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
846 struct thread_info *tp = find_thread_ptid (ptid);
848 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
849 if (!VEC_empty (value_ptr, tp->stack_temporaries))
851 struct value *v;
852 int i;
854 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
855 if (v == val)
856 return 1;
859 return 0;
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. */
865 struct value *
866 get_last_thread_stack_temporary (ptid_t ptid)
868 struct value *lastval = NULL;
869 struct thread_info *tp = find_thread_ptid (ptid);
871 gdb_assert (tp != NULL);
872 if (!VEC_empty (value_ptr, tp->stack_temporaries))
873 lastval = VEC_last (value_ptr, tp->stack_temporaries);
875 return lastval;
878 void
879 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
881 struct inferior *inf;
882 struct thread_info *tp;
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);
890 tp = find_thread_ptid (old_ptid);
891 tp->ptid = new_ptid;
893 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
896 /* See gdbthread.h. */
898 void
899 set_resumed (ptid_t ptid, int resumed)
901 struct thread_info *tp;
902 int all = ptid_equal (ptid, minus_one_ptid);
904 if (all || ptid_is_pid (ptid))
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;
910 else
912 tp = find_thread_ptid (ptid);
913 gdb_assert (tp != NULL);
914 tp->resumed = resumed;
918 /* Helper for set_running, that marks one thread either running or
919 stopped. */
921 static int
922 set_running_thread (struct thread_info *tp, int running)
924 int started = 0;
926 if (running && tp->state == THREAD_STOPPED)
927 started = 1;
928 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
930 if (!running)
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);
939 return started;
942 void
943 set_running (ptid_t ptid, int running)
945 struct thread_info *tp;
946 int all = ptid_equal (ptid, minus_one_ptid);
947 int any_started = 0;
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))
954 for (tp = thread_list; tp; tp = tp->next)
955 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
957 if (tp->state == THREAD_EXITED)
958 continue;
960 if (set_running_thread (tp, running))
961 any_started = 1;
964 else
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;
972 if (any_started)
973 observer_notify_target_resumed (ptid);
976 static int
977 is_thread_state (ptid_t ptid, enum thread_state state)
979 struct thread_info *tp;
981 tp = find_thread_ptid (ptid);
982 gdb_assert (tp);
983 return tp->state == state;
987 is_stopped (ptid_t ptid)
989 return is_thread_state (ptid, THREAD_STOPPED);
993 is_exited (ptid_t ptid)
995 return is_thread_state (ptid, THREAD_EXITED);
999 is_running (ptid_t ptid)
1001 return is_thread_state (ptid, THREAD_RUNNING);
1005 is_executing (ptid_t ptid)
1007 struct thread_info *tp;
1009 tp = find_thread_ptid (ptid);
1010 gdb_assert (tp);
1011 return tp->executing;
1014 void
1015 set_executing (ptid_t ptid, int executing)
1017 struct thread_info *tp;
1018 int all = ptid_equal (ptid, minus_one_ptid);
1020 if (all || ptid_is_pid (ptid))
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;
1026 else
1028 tp = find_thread_ptid (ptid);
1029 gdb_assert (tp);
1030 tp->executing = executing;
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;
1042 /* See gdbthread.h. */
1045 threads_are_executing (void)
1047 return threads_executing;
1050 void
1051 set_stop_requested (ptid_t ptid, int stop)
1053 struct thread_info *tp;
1054 int all = ptid_equal (ptid, minus_one_ptid);
1056 if (all || ptid_is_pid (ptid))
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;
1062 else
1064 tp = find_thread_ptid (ptid);
1065 gdb_assert (tp);
1066 tp->stop_requested = stop;
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);
1075 void
1076 finish_thread_state (ptid_t ptid)
1078 struct thread_info *tp;
1079 int all;
1080 int any_started = 0;
1082 all = ptid_equal (ptid, minus_one_ptid);
1084 if (all || ptid_is_pid (ptid))
1086 for (tp = thread_list; tp; tp = tp->next)
1088 if (tp->state == THREAD_EXITED)
1089 continue;
1090 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1092 if (set_running_thread (tp, tp->executing))
1093 any_started = 1;
1097 else
1099 tp = find_thread_ptid (ptid);
1100 gdb_assert (tp);
1101 if (tp->state != THREAD_EXITED)
1103 if (set_running_thread (tp, tp->executing))
1104 any_started = 1;
1108 if (any_started)
1109 observer_notify_target_resumed (ptid);
1112 void
1113 finish_thread_state_cleanup (void *arg)
1115 ptid_t *ptid_p = (ptid_t *) arg;
1117 gdb_assert (arg);
1119 finish_thread_state (*ptid_p);
1122 /* See gdbthread.h. */
1124 void
1125 validate_registers_access (void)
1127 /* No selected thread, no registers. */
1128 if (ptid_equal (inferior_ptid, null_ptid))
1129 error (_("No thread selected."));
1131 /* Don't try to read from a dead thread. */
1132 if (is_exited (inferior_ptid))
1133 error (_("The current thread has terminated"));
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."));
1145 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1147 return (pc >= thread->control.step_range_start
1148 && pc < thread->control.step_range_end);
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. */
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)
1165 if (requested_threads != NULL && *requested_threads != '\0')
1167 int in_list;
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;
1178 if (pid != -1 && ptid_get_pid (thr->ptid) != pid)
1180 if (requested_threads != NULL && *requested_threads != '\0')
1181 error (_("Requested thread not found in requested process"));
1182 return 0;
1185 if (thr->state == THREAD_EXITED)
1186 return 0;
1188 return 1;
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. */
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)
1200 struct thread_info *tp;
1201 ptid_t current_ptid;
1202 struct cleanup *old_chain;
1203 const char *extra_info, *name, *target_id;
1204 struct inferior *inf;
1205 int default_inf_num = current_inferior ()->num;
1207 update_thread_list ();
1208 current_ptid = inferior_ptid;
1210 /* We'll be switching threads temporarily. */
1211 old_chain = make_cleanup_restore_current_thread ();
1213 /* For backward compatibility, we make a list for MI. A table is
1214 preferable for the CLI, though, because it shows table
1215 headers. */
1216 if (ui_out_is_mi_like_p (uiout))
1217 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1218 else
1220 int n_threads = 0;
1222 for (tp = thread_list; tp; tp = tp->next)
1224 if (!should_print_thread (requested_threads, default_inf_num,
1225 global_ids, pid, tp))
1226 continue;
1228 ++n_threads;
1231 if (n_threads == 0)
1233 if (requested_threads == NULL || *requested_threads == '\0')
1234 ui_out_message (uiout, 0, _("No threads.\n"));
1235 else
1236 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1237 requested_threads);
1238 do_cleanups (old_chain);
1239 return;
1242 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1243 make_cleanup_ui_out_table_begin_end (uiout, 5, n_threads, "threads");
1244 else
1245 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1247 ui_out_table_header (uiout, 1, ui_left, "current", "");
1249 if (!ui_out_is_mi_like_p (uiout))
1250 ui_out_table_header (uiout, 4, ui_left, "id-in-tg", "Id");
1251 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1252 ui_out_table_header (uiout, 4, ui_left, "id", "GId");
1253 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1254 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1255 ui_out_table_body (uiout);
1258 ALL_THREADS_BY_INFERIOR (inf, tp)
1260 struct cleanup *chain2;
1261 int core;
1263 if (!should_print_thread (requested_threads, default_inf_num,
1264 global_ids, pid, tp))
1265 continue;
1267 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1269 if (ui_out_is_mi_like_p (uiout))
1271 /* Compatibility. */
1272 if (ptid_equal (tp->ptid, current_ptid))
1273 ui_out_text (uiout, "* ");
1274 else
1275 ui_out_text (uiout, " ");
1277 else
1279 if (ptid_equal (tp->ptid, current_ptid))
1280 ui_out_field_string (uiout, "current", "*");
1281 else
1282 ui_out_field_skip (uiout, "current");
1285 if (!ui_out_is_mi_like_p (uiout))
1286 ui_out_field_string (uiout, "id-in-tg", print_thread_id (tp));
1288 if (show_global_ids || ui_out_is_mi_like_p (uiout))
1289 ui_out_field_int (uiout, "id", tp->global_num);
1291 /* For the CLI, we stuff everything into the target-id field.
1292 This is a gross hack to make the output come out looking
1293 correct. The underlying problem here is that ui-out has no
1294 way to specify that a field's space allocation should be
1295 shared by several fields. For MI, we do the right thing
1296 instead. */
1298 target_id = target_pid_to_str (tp->ptid);
1299 extra_info = target_extra_thread_info (tp);
1300 name = tp->name ? tp->name : target_thread_name (tp);
1302 if (ui_out_is_mi_like_p (uiout))
1304 ui_out_field_string (uiout, "target-id", target_id);
1305 if (extra_info)
1306 ui_out_field_string (uiout, "details", extra_info);
1307 if (name)
1308 ui_out_field_string (uiout, "name", name);
1310 else
1312 struct cleanup *str_cleanup;
1313 char *contents;
1315 if (extra_info && name)
1316 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1317 name, extra_info);
1318 else if (extra_info)
1319 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1320 else if (name)
1321 contents = xstrprintf ("%s \"%s\"", target_id, name);
1322 else
1323 contents = xstrdup (target_id);
1324 str_cleanup = make_cleanup (xfree, contents);
1326 ui_out_field_string (uiout, "target-id", contents);
1327 do_cleanups (str_cleanup);
1330 if (tp->state == THREAD_RUNNING)
1331 ui_out_text (uiout, "(running)\n");
1332 else
1334 /* The switch below puts us at the top of the stack (leaf
1335 frame). */
1336 switch_to_thread (tp->ptid);
1337 print_stack_frame (get_selected_frame (NULL),
1338 /* For MI output, print frame level. */
1339 ui_out_is_mi_like_p (uiout),
1340 LOCATION, 0);
1343 if (ui_out_is_mi_like_p (uiout))
1345 char *state = "stopped";
1347 if (tp->state == THREAD_RUNNING)
1348 state = "running";
1349 ui_out_field_string (uiout, "state", state);
1352 core = target_core_of_thread (tp->ptid);
1353 if (ui_out_is_mi_like_p (uiout) && core != -1)
1354 ui_out_field_int (uiout, "core", core);
1356 do_cleanups (chain2);
1359 /* Restores the current thread and the frame selected before
1360 the "info threads" command. */
1361 do_cleanups (old_chain);
1363 if (pid == -1 && requested_threads == NULL)
1365 if (ui_out_is_mi_like_p (uiout)
1366 && !ptid_equal (inferior_ptid, null_ptid))
1368 int num = ptid_to_global_thread_id (inferior_ptid);
1370 gdb_assert (num != 0);
1371 ui_out_field_int (uiout, "current-thread-id", num);
1374 if (!ptid_equal (inferior_ptid, null_ptid) && is_exited (inferior_ptid))
1375 ui_out_message (uiout, 0, "\n\
1376 The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1377 print_thread_id (inferior_thread ()));
1378 else if (thread_list != NULL
1379 && ptid_equal (inferior_ptid, null_ptid))
1380 ui_out_message (uiout, 0, "\n\
1381 No selected thread. See `help thread'.\n");
1385 /* See gdbthread.h. */
1387 void
1388 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1390 print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
1393 /* Implementation of the "info threads" command.
1395 Note: this has the drawback that it _really_ switches
1396 threads, which frees the frame cache. A no-side
1397 effects info-threads command would be nicer. */
1399 static void
1400 info_threads_command (char *arg, int from_tty)
1402 int show_global_ids = 0;
1404 if (arg != NULL
1405 && check_for_argument (&arg, "-gid", sizeof ("-gid") - 1))
1407 arg = skip_spaces (arg);
1408 show_global_ids = 1;
1411 print_thread_info_1 (current_uiout, arg, 0, -1, show_global_ids);
1414 /* See gdbthread.h. */
1416 void
1417 switch_to_thread_no_regs (struct thread_info *thread)
1419 struct inferior *inf;
1421 inf = find_inferior_ptid (thread->ptid);
1422 gdb_assert (inf != NULL);
1423 set_current_program_space (inf->pspace);
1424 set_current_inferior (inf);
1426 inferior_ptid = thread->ptid;
1427 stop_pc = ~(CORE_ADDR) 0;
1430 /* Switch from one thread to another. */
1432 void
1433 switch_to_thread (ptid_t ptid)
1435 /* Switch the program space as well, if we can infer it from the now
1436 current thread. Otherwise, it's up to the caller to select the
1437 space it wants. */
1438 if (!ptid_equal (ptid, null_ptid))
1440 struct inferior *inf;
1442 inf = find_inferior_ptid (ptid);
1443 gdb_assert (inf != NULL);
1444 set_current_program_space (inf->pspace);
1445 set_current_inferior (inf);
1448 if (ptid_equal (ptid, inferior_ptid))
1449 return;
1451 inferior_ptid = ptid;
1452 reinit_frame_cache ();
1454 /* We don't check for is_stopped, because we're called at times
1455 while in the TARGET_RUNNING state, e.g., while handling an
1456 internal event. */
1457 if (!ptid_equal (inferior_ptid, null_ptid)
1458 && !is_exited (ptid)
1459 && !is_executing (ptid))
1460 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1461 else
1462 stop_pc = ~(CORE_ADDR) 0;
1465 static void
1466 restore_current_thread (ptid_t ptid)
1468 switch_to_thread (ptid);
1471 static void
1472 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1474 struct frame_info *frame = NULL;
1475 int count;
1477 /* This means there was no selected frame. */
1478 if (frame_level == -1)
1480 select_frame (NULL);
1481 return;
1484 gdb_assert (frame_level >= 0);
1486 /* Restore by level first, check if the frame id is the same as
1487 expected. If that fails, try restoring by frame id. If that
1488 fails, nothing to do, just warn the user. */
1490 count = frame_level;
1491 frame = find_relative_frame (get_current_frame (), &count);
1492 if (count == 0
1493 && frame != NULL
1494 /* The frame ids must match - either both valid or both outer_frame_id.
1495 The latter case is not failsafe, but since it's highly unlikely
1496 the search by level finds the wrong frame, it's 99.9(9)% of
1497 the time (for all practical purposes) safe. */
1498 && frame_id_eq (get_frame_id (frame), a_frame_id))
1500 /* Cool, all is fine. */
1501 select_frame (frame);
1502 return;
1505 frame = frame_find_by_id (a_frame_id);
1506 if (frame != NULL)
1508 /* Cool, refound it. */
1509 select_frame (frame);
1510 return;
1513 /* Nothing else to do, the frame layout really changed. Select the
1514 innermost stack frame. */
1515 select_frame (get_current_frame ());
1517 /* Warn the user. */
1518 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1520 warning (_("Couldn't restore frame #%d in "
1521 "current thread. Bottom (innermost) frame selected:"),
1522 frame_level);
1523 /* For MI, we should probably have a notification about
1524 current frame change. But this error is not very
1525 likely, so don't bother for now. */
1526 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1530 /* Data used by the cleanup installed by
1531 'make_cleanup_restore_current_thread'. */
1533 struct current_thread_cleanup
1535 /* Next in list of currently installed 'struct
1536 current_thread_cleanup' cleanups. See
1537 'current_thread_cleanup_chain' below. */
1538 struct current_thread_cleanup *next;
1540 ptid_t inferior_ptid;
1541 struct frame_id selected_frame_id;
1542 int selected_frame_level;
1543 int was_stopped;
1544 int inf_id;
1545 int was_removable;
1548 /* A chain of currently installed 'struct current_thread_cleanup'
1549 cleanups. Restoring the previously selected thread looks up the
1550 old thread in the thread list by ptid. If the thread changes ptid,
1551 we need to update the cleanup's thread structure so the look up
1552 succeeds. */
1553 static struct current_thread_cleanup *current_thread_cleanup_chain;
1555 /* A thread_ptid_changed observer. Update all currently installed
1556 current_thread_cleanup cleanups that want to switch back to
1557 OLD_PTID to switch back to NEW_PTID instead. */
1559 static void
1560 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1562 struct current_thread_cleanup *it;
1564 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1566 if (ptid_equal (it->inferior_ptid, old_ptid))
1567 it->inferior_ptid = new_ptid;
1571 static void
1572 do_restore_current_thread_cleanup (void *arg)
1574 struct thread_info *tp;
1575 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1577 tp = find_thread_ptid (old->inferior_ptid);
1579 /* If the previously selected thread belonged to a process that has
1580 in the mean time been deleted (due to normal exit, detach, etc.),
1581 then don't revert back to it, but instead simply drop back to no
1582 thread selected. */
1583 if (tp
1584 && find_inferior_ptid (tp->ptid) != NULL)
1585 restore_current_thread (old->inferior_ptid);
1586 else
1588 restore_current_thread (null_ptid);
1589 set_current_inferior (find_inferior_id (old->inf_id));
1592 /* The running state of the originally selected thread may have
1593 changed, so we have to recheck it here. */
1594 if (!ptid_equal (inferior_ptid, null_ptid)
1595 && old->was_stopped
1596 && is_stopped (inferior_ptid)
1597 && target_has_registers
1598 && target_has_stack
1599 && target_has_memory)
1600 restore_selected_frame (old->selected_frame_id,
1601 old->selected_frame_level);
1604 static void
1605 restore_current_thread_cleanup_dtor (void *arg)
1607 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1608 struct thread_info *tp;
1609 struct inferior *inf;
1611 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1613 tp = find_thread_ptid (old->inferior_ptid);
1614 if (tp)
1615 tp->refcount--;
1616 inf = find_inferior_id (old->inf_id);
1617 if (inf != NULL)
1618 inf->removable = old->was_removable;
1619 xfree (old);
1622 /* Set the thread reference count. */
1624 static void
1625 set_thread_refcount (void *data)
1627 int k;
1628 struct thread_array_cleanup *ta_cleanup
1629 = (struct thread_array_cleanup *) data;
1631 for (k = 0; k != ta_cleanup->count; k++)
1632 ta_cleanup->tp_array[k]->refcount--;
1635 struct cleanup *
1636 make_cleanup_restore_current_thread (void)
1638 struct thread_info *tp;
1639 struct frame_info *frame;
1640 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1642 old->inferior_ptid = inferior_ptid;
1643 old->inf_id = current_inferior ()->num;
1644 old->was_removable = current_inferior ()->removable;
1646 old->next = current_thread_cleanup_chain;
1647 current_thread_cleanup_chain = old;
1649 if (!ptid_equal (inferior_ptid, null_ptid))
1651 old->was_stopped = is_stopped (inferior_ptid);
1652 if (old->was_stopped
1653 && target_has_registers
1654 && target_has_stack
1655 && target_has_memory)
1657 /* When processing internal events, there might not be a
1658 selected frame. If we naively call get_selected_frame
1659 here, then we can end up reading debuginfo for the
1660 current frame, but we don't generally need the debuginfo
1661 at this point. */
1662 frame = get_selected_frame_if_set ();
1664 else
1665 frame = NULL;
1667 old->selected_frame_id = get_frame_id (frame);
1668 old->selected_frame_level = frame_relative_level (frame);
1670 tp = find_thread_ptid (inferior_ptid);
1671 if (tp)
1672 tp->refcount++;
1675 current_inferior ()->removable = 0;
1677 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1678 restore_current_thread_cleanup_dtor);
1681 /* See gdbthread.h. */
1684 show_thread_that_caused_stop (void)
1686 return highest_thread_num > 1;
1689 /* See gdbthread.h. */
1692 show_inferior_qualified_tids (void)
1694 return (inferior_list->next != NULL || inferior_list->num != 1);
1697 /* See gdbthread.h. */
1699 const char *
1700 print_thread_id (struct thread_info *thr)
1702 char *s = get_print_cell ();
1704 if (show_inferior_qualified_tids ())
1705 xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num);
1706 else
1707 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num);
1708 return s;
1711 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1712 descending order. */
1714 static int tp_array_compar_ascending;
1716 /* Sort an array for struct thread_info pointers by thread ID (first
1717 by inferior number, and then by per-inferior thread number). The
1718 order is determined by TP_ARRAY_COMPAR_ASCENDING. */
1720 static int
1721 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1723 const struct thread_info *a = *(const struct thread_info * const *) ap_voidp;
1724 const struct thread_info *b = *(const struct thread_info * const *) bp_voidp;
1726 if (a->inf->num != b->inf->num)
1728 return (((a->inf->num > b->inf->num) - (a->inf->num < b->inf->num))
1729 * (tp_array_compar_ascending ? +1 : -1));
1732 return (((a->per_inf_num > b->per_inf_num)
1733 - (a->per_inf_num < b->per_inf_num))
1734 * (tp_array_compar_ascending ? +1 : -1));
1737 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1738 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1739 of two numbers seperated by a hyphen. Examples:
1741 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1742 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1743 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1745 static void
1746 thread_apply_all_command (char *cmd, int from_tty)
1748 struct cleanup *old_chain;
1749 char *saved_cmd;
1750 int tc;
1751 struct thread_array_cleanup ta_cleanup;
1753 tp_array_compar_ascending = 0;
1754 if (cmd != NULL
1755 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1757 cmd = skip_spaces (cmd);
1758 tp_array_compar_ascending = 1;
1761 if (cmd == NULL || *cmd == '\000')
1762 error (_("Please specify a command following the thread ID list"));
1764 update_thread_list ();
1766 old_chain = make_cleanup_restore_current_thread ();
1768 /* Save a copy of the command in case it is clobbered by
1769 execute_command. */
1770 saved_cmd = xstrdup (cmd);
1771 make_cleanup (xfree, saved_cmd);
1773 /* Note this includes exited threads. */
1774 tc = thread_count ();
1775 if (tc != 0)
1777 struct thread_info **tp_array;
1778 struct thread_info *tp;
1779 int i = 0, k;
1781 /* Save a copy of the thread_list in case we execute detach
1782 command. */
1783 tp_array = XNEWVEC (struct thread_info *, tc);
1784 make_cleanup (xfree, tp_array);
1786 ALL_NON_EXITED_THREADS (tp)
1788 tp_array[i] = tp;
1789 tp->refcount++;
1790 i++;
1792 /* Because we skipped exited threads, we may end up with fewer
1793 threads in the array than the total count of threads. */
1794 gdb_assert (i <= tc);
1796 if (i != 0)
1797 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1799 ta_cleanup.tp_array = tp_array;
1800 ta_cleanup.count = i;
1801 make_cleanup (set_thread_refcount, &ta_cleanup);
1803 for (k = 0; k != i; k++)
1804 if (thread_alive (tp_array[k]))
1806 switch_to_thread (tp_array[k]->ptid);
1807 printf_filtered (_("\nThread %s (%s):\n"),
1808 print_thread_id (tp_array[k]),
1809 target_pid_to_str (inferior_ptid));
1810 execute_command (cmd, from_tty);
1812 /* Restore exact command used previously. */
1813 strcpy (cmd, saved_cmd);
1817 do_cleanups (old_chain);
1820 /* Implementation of the "thread apply" command. */
1822 static void
1823 thread_apply_command (char *tidlist, int from_tty)
1825 char *cmd = NULL;
1826 struct cleanup *old_chain;
1827 char *saved_cmd;
1828 struct tid_range_parser parser;
1830 if (tidlist == NULL || *tidlist == '\000')
1831 error (_("Please specify a thread ID list"));
1833 tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
1834 while (!tid_range_parser_finished (&parser))
1836 int inf_num, thr_start, thr_end;
1838 if (!tid_range_parser_get_tid_range (&parser,
1839 &inf_num, &thr_start, &thr_end))
1841 cmd = (char *) tid_range_parser_string (&parser);
1842 break;
1846 if (cmd == NULL)
1847 error (_("Please specify a command following the thread ID list"));
1849 if (tidlist == cmd || !isalpha (cmd[0]))
1850 invalid_thread_id_error (cmd);
1852 /* Save a copy of the command in case it is clobbered by
1853 execute_command. */
1854 saved_cmd = xstrdup (cmd);
1855 old_chain = make_cleanup (xfree, saved_cmd);
1857 make_cleanup_restore_current_thread ();
1859 tid_range_parser_init (&parser, tidlist, current_inferior ()->num);
1860 while (!tid_range_parser_finished (&parser)
1861 && tid_range_parser_string (&parser) < cmd)
1863 struct thread_info *tp = NULL;
1864 struct inferior *inf;
1865 int inf_num, thr_num;
1867 tid_range_parser_get_tid (&parser, &inf_num, &thr_num);
1868 inf = find_inferior_id (inf_num);
1869 if (inf != NULL)
1870 tp = find_thread_id (inf, thr_num);
1872 if (tid_range_parser_star_range (&parser))
1874 if (inf == NULL)
1876 warning (_("Unknown inferior %d"), inf_num);
1877 tid_range_parser_skip (&parser);
1878 continue;
1881 /* No use looking for threads past the highest thread number
1882 the inferior ever had. */
1883 if (thr_num >= inf->highest_thread_num)
1884 tid_range_parser_skip (&parser);
1886 /* Be quiet about unknown threads numbers. */
1887 if (tp == NULL)
1888 continue;
1891 if (tp == NULL)
1893 if (show_inferior_qualified_tids ()
1894 || tid_range_parser_qualified (&parser))
1895 warning (_("Unknown thread %d.%d"), inf_num, thr_num);
1896 else
1897 warning (_("Unknown thread %d"), thr_num);
1898 continue;
1901 if (!thread_alive (tp))
1903 warning (_("Thread %s has terminated."), print_thread_id (tp));
1904 continue;
1907 switch_to_thread (tp->ptid);
1909 printf_filtered (_("\nThread %s (%s):\n"), print_thread_id (tp),
1910 target_pid_to_str (inferior_ptid));
1911 execute_command (cmd, from_tty);
1913 /* Restore exact command used previously. */
1914 strcpy (cmd, saved_cmd);
1917 do_cleanups (old_chain);
1920 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1921 if prefix of arg is `apply'. */
1923 void
1924 thread_command (char *tidstr, int from_tty)
1926 if (!tidstr)
1928 if (ptid_equal (inferior_ptid, null_ptid))
1929 error (_("No thread selected"));
1931 if (target_has_stack)
1933 struct thread_info *tp = inferior_thread ();
1935 if (is_exited (inferior_ptid))
1936 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1937 print_thread_id (tp),
1938 target_pid_to_str (inferior_ptid));
1939 else
1940 printf_filtered (_("[Current thread is %s (%s)]\n"),
1941 print_thread_id (tp),
1942 target_pid_to_str (inferior_ptid));
1944 else
1945 error (_("No stack."));
1946 return;
1949 gdb_thread_select (current_uiout, tidstr, NULL);
1952 /* Implementation of `thread name'. */
1954 static void
1955 thread_name_command (char *arg, int from_tty)
1957 struct thread_info *info;
1959 if (ptid_equal (inferior_ptid, null_ptid))
1960 error (_("No thread selected"));
1962 arg = skip_spaces (arg);
1964 info = inferior_thread ();
1965 xfree (info->name);
1966 info->name = arg ? xstrdup (arg) : NULL;
1969 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1971 static void
1972 thread_find_command (char *arg, int from_tty)
1974 struct thread_info *tp;
1975 const char *tmp;
1976 unsigned long match = 0;
1978 if (arg == NULL || *arg == '\0')
1979 error (_("Command requires an argument."));
1981 tmp = re_comp (arg);
1982 if (tmp != 0)
1983 error (_("Invalid regexp (%s): %s"), tmp, arg);
1985 update_thread_list ();
1986 for (tp = thread_list; tp; tp = tp->next)
1988 if (tp->name != NULL && re_exec (tp->name))
1990 printf_filtered (_("Thread %s has name '%s'\n"),
1991 print_thread_id (tp), tp->name);
1992 match++;
1995 tmp = target_thread_name (tp);
1996 if (tmp != NULL && re_exec (tmp))
1998 printf_filtered (_("Thread %s has target name '%s'\n"),
1999 print_thread_id (tp), tmp);
2000 match++;
2003 tmp = target_pid_to_str (tp->ptid);
2004 if (tmp != NULL && re_exec (tmp))
2006 printf_filtered (_("Thread %s has target id '%s'\n"),
2007 print_thread_id (tp), tmp);
2008 match++;
2011 tmp = target_extra_thread_info (tp);
2012 if (tmp != NULL && re_exec (tmp))
2014 printf_filtered (_("Thread %s has extra info '%s'\n"),
2015 print_thread_id (tp), tmp);
2016 match++;
2019 if (!match)
2020 printf_filtered (_("No threads match '%s'\n"), arg);
2023 /* Print notices when new threads are attached and detached. */
2024 int print_thread_events = 1;
2025 static void
2026 show_print_thread_events (struct ui_file *file, int from_tty,
2027 struct cmd_list_element *c, const char *value)
2029 fprintf_filtered (file,
2030 _("Printing of thread events is %s.\n"),
2031 value);
2034 static int
2035 do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
2037 const char *tidstr = (const char *) tidstr_v;
2038 struct thread_info *tp;
2040 if (ui_out_is_mi_like_p (uiout))
2042 int num = value_as_long (parse_and_eval (tidstr));
2044 tp = find_thread_global_id (num);
2045 if (tp == NULL)
2046 error (_("Thread ID %d not known."), num);
2048 else
2050 tp = parse_thread_id (tidstr, NULL);
2051 gdb_assert (tp != NULL);
2054 if (!thread_alive (tp))
2055 error (_("Thread ID %s has terminated."), tidstr);
2057 switch_to_thread (tp->ptid);
2059 annotate_thread_changed ();
2061 if (ui_out_is_mi_like_p (uiout))
2062 ui_out_field_int (uiout, "new-thread-id", inferior_thread ()->global_num);
2063 else
2065 ui_out_text (uiout, "[Switching to thread ");
2066 ui_out_field_string (uiout, "new-thread-id", print_thread_id (tp));
2067 ui_out_text (uiout, " (");
2068 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
2069 ui_out_text (uiout, ")]");
2072 /* Note that we can't reach this with an exited thread, due to the
2073 thread_alive check above. */
2074 if (tp->state == THREAD_RUNNING)
2075 ui_out_text (uiout, "(running)\n");
2076 else
2078 ui_out_text (uiout, "\n");
2079 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2082 /* Since the current thread may have changed, see if there is any
2083 exited thread we can now delete. */
2084 prune_threads ();
2086 return GDB_RC_OK;
2089 enum gdb_rc
2090 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
2092 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
2093 error_message, RETURN_MASK_ALL) < 0)
2094 return GDB_RC_FAIL;
2095 return GDB_RC_OK;
2098 /* Update the 'threads_executing' global based on the threads we know
2099 about right now. */
2101 static void
2102 update_threads_executing (void)
2104 struct thread_info *tp;
2106 threads_executing = 0;
2107 ALL_NON_EXITED_THREADS (tp)
2109 if (tp->executing)
2111 threads_executing = 1;
2112 break;
2117 void
2118 update_thread_list (void)
2120 target_update_thread_list ();
2121 update_threads_executing ();
2124 /* Return a new value for the selected thread's id. Return a value of
2125 0 if no thread is selected. If GLOBAL is true, return the thread's
2126 global number. Otherwise return the per-inferior number. */
2128 static struct value *
2129 thread_num_make_value_helper (struct gdbarch *gdbarch, int global)
2131 struct thread_info *tp = find_thread_ptid (inferior_ptid);
2132 int int_val;
2134 if (tp == NULL)
2135 int_val = 0;
2136 else if (global)
2137 int_val = tp->global_num;
2138 else
2139 int_val = tp->per_inf_num;
2141 return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val);
2144 /* Return a new value for the selected thread's per-inferior thread
2145 number. Return a value of 0 if no thread is selected, or no
2146 threads exist. */
2148 static struct value *
2149 thread_id_per_inf_num_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2150 void *ignore)
2152 return thread_num_make_value_helper (gdbarch, 0);
2155 /* Return a new value for the selected thread's global id. Return a
2156 value of 0 if no thread is selected, or no threads exist. */
2158 static struct value *
2159 global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2160 void *ignore)
2162 return thread_num_make_value_helper (gdbarch, 1);
2165 /* Commands with a prefix of `thread'. */
2166 struct cmd_list_element *thread_cmd_list = NULL;
2168 /* Implementation of `thread' variable. */
2170 static const struct internalvar_funcs thread_funcs =
2172 thread_id_per_inf_num_make_value,
2173 NULL,
2174 NULL
2177 /* Implementation of `gthread' variable. */
2179 static const struct internalvar_funcs gthread_funcs =
2181 global_thread_id_make_value,
2182 NULL,
2183 NULL
2186 void
2187 _initialize_thread (void)
2189 static struct cmd_list_element *thread_apply_list = NULL;
2191 add_info ("threads", info_threads_command,
2192 _("Display currently known threads.\n\
2193 Usage: info threads [-gid] [ID]...\n\
2194 -gid: Show global thread IDs.\n\
2195 If ID is given, it is a space-separated list of IDs of threads to display.\n\
2196 Otherwise, all threads are displayed."));
2198 add_prefix_cmd ("thread", class_run, thread_command, _("\
2199 Use this command to switch between threads.\n\
2200 The new thread ID must be currently known."),
2201 &thread_cmd_list, "thread ", 1, &cmdlist);
2203 add_prefix_cmd ("apply", class_run, thread_apply_command,
2204 _("Apply a command to a list of threads."),
2205 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
2207 add_cmd ("all", class_run, thread_apply_all_command,
2208 _("\
2209 Apply a command to all threads.\n\
2211 Usage: thread apply all [-ascending] <command>\n\
2212 -ascending: Call <command> for all threads in ascending order.\n\
2213 The default is descending order.\
2215 &thread_apply_list);
2217 add_cmd ("name", class_run, thread_name_command,
2218 _("Set the current thread's name.\n\
2219 Usage: thread name [NAME]\n\
2220 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2222 add_cmd ("find", class_run, thread_find_command, _("\
2223 Find threads that match a regular expression.\n\
2224 Usage: thread find REGEXP\n\
2225 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2226 &thread_cmd_list);
2228 add_com_alias ("t", "thread", class_run, 1);
2230 add_setshow_boolean_cmd ("thread-events", no_class,
2231 &print_thread_events, _("\
2232 Set printing of thread events (such as thread start and exit)."), _("\
2233 Show printing of thread events (such as thread start and exit)."), NULL,
2234 NULL,
2235 show_print_thread_events,
2236 &setprintlist, &showprintlist);
2238 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
2239 create_internalvar_type_lazy ("_gthread", &gthread_funcs, NULL);
2241 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);