1 /* Multi-thread control defs for remote server for GDB.
2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "common-gdbthread.h"
23 #include "inferiors.h"
27 struct btrace_target_info
;
32 /* The id of this thread. */
36 struct regcache
*regcache_data
;
38 /* The last resume GDB requested on this thread. */
39 enum resume_kind last_resume_kind
;
41 /* The last wait status reported for this thread. */
42 struct target_waitstatus last_status
;
44 /* True if LAST_STATUS hasn't been reported to GDB yet. */
47 /* Given `while-stepping', a thread may be collecting data for more
48 than one tracepoint simultaneously. E.g.:
50 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
52 ff0003 INSN3 <-- TP2, collect $regs
53 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
56 Notice that when instruction INSN5 is reached, the while-stepping
57 actions of both TP1 and TP3 are still being collected, and that TP2
58 had been collected meanwhile. The whole range of ff0001-ff0005
59 should be single-stepped, due to at least TP1's while-stepping
60 action covering the whole range.
62 On the other hand, the same tracepoint with a while-stepping action
63 may be hit by more than one thread simultaneously, hence we can't
64 keep the current step count in the tracepoint itself.
66 This is the head of the list of the states of `while-stepping'
67 tracepoint actions this thread is now collecting; NULL if empty.
68 Each item in the list holds the current step of the while-stepping
70 struct wstep_state
*while_stepping
;
72 /* Branch trace target information for this thread. */
73 struct btrace_target_info
*btrace
;
76 extern std::list
<thread_info
*> all_threads
;
78 void remove_thread (struct thread_info
*thread
);
79 struct thread_info
*add_thread (ptid_t ptid
, void *target_data
);
81 /* Return a pointer to the first thread, or NULL if there isn't one. */
83 struct thread_info
*get_first_thread (void);
85 struct thread_info
*find_thread_ptid (ptid_t ptid
);
87 /* Find any thread of the PID process. Returns NULL if none is
89 struct thread_info
*find_any_thread_of_pid (int pid
);
91 /* Find the first thread for which FUNC returns true. Return NULL if no thread
92 satisfying FUNC is found. */
94 template <typename Func
>
96 find_thread (Func func
)
98 std::list
<thread_info
*>::iterator next
, cur
= all_threads
.begin ();
100 while (cur
!= all_threads
.end ())
114 /* Like the above, but only consider threads with pid PID. */
116 template <typename Func
>
118 find_thread (int pid
, Func func
)
120 return find_thread ([&] (thread_info
*thread
)
122 return thread
->id
.pid () == pid
&& func (thread
);
126 /* Invoke FUNC for each thread. */
128 template <typename Func
>
130 for_each_thread (Func func
)
132 std::list
<thread_info
*>::iterator next
, cur
= all_threads
.begin ();
134 while (cur
!= all_threads
.end ())
143 /* Like the above, but only consider threads with pid PID. */
145 template <typename Func
>
147 for_each_thread (int pid
, Func func
)
149 for_each_thread ([&] (thread_info
*thread
)
151 if (pid
== thread
->id
.pid ())
156 /* Find the a random thread for which FUNC (THREAD) returns true. If
157 no entry is found then return NULL. */
159 template <typename Func
>
161 find_thread_in_random (Func func
)
166 /* First count how many interesting entries we have. */
167 for_each_thread ([&] (thread_info
*thread
) {
175 /* Now randomly pick an entry out of those. */
176 random_selector
= (int)
177 ((count
* (double) rand ()) / (RAND_MAX
+ 1.0));
179 thread_info
*thread
= find_thread ([&] (thread_info
*thread
) {
180 return func (thread
) && (random_selector
-- == 0);
183 gdb_assert (thread
!= NULL
);
188 /* Get current thread ID (Linux task ID). */
189 #define current_ptid (current_thread->id)
191 /* Get the ptid of THREAD. */
194 ptid_of (const thread_info
*thread
)
199 /* Get the pid of THREAD. */
202 pid_of (const thread_info
*thread
)
204 return thread
->id
.pid ();
207 /* Get the lwp of THREAD. */
210 lwpid_of (const thread_info
*thread
)
212 return thread
->id
.lwp ();
215 /* Create a cleanup to restore current_thread. */
216 struct cleanup
*make_cleanup_restore_current_thread (void);
218 #endif /* GDB_THREAD_H */