]>
Commit | Line | Data |
---|---|---|
623b6bdf | 1 | /* Multi-thread control defs for remote server for GDB. |
1d506c26 | 2 | Copyright (C) 1993-2024 Free Software Foundation, Inc. |
623b6bdf YQ |
3 | |
4 | This file is part of GDB. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. */ | |
18 | ||
1a5c2598 TT |
19 | #ifndef GDBSERVER_GDBTHREAD_H |
20 | #define GDBSERVER_GDBTHREAD_H | |
623b6bdf | 21 | |
268a13a5 | 22 | #include "gdbsupport/common-gdbthread.h" |
6a6bbd9d | 23 | #include "inferiors.h" |
623b6bdf | 24 | |
9c80ecd6 SM |
25 | #include <list> |
26 | ||
9accd112 | 27 | struct btrace_target_info; |
a44892be | 28 | struct regcache; |
9accd112 | 29 | |
623b6bdf YQ |
30 | struct thread_info |
31 | { | |
d2f325df SM |
32 | thread_info (ptid_t id, void *target_data) |
33 | : id (id), target_data (target_data) | |
183be222 | 34 | {} |
d2f325df SM |
35 | |
36 | ~thread_info () | |
37 | { | |
38 | free_register_cache (this->regcache_data); | |
39 | } | |
40 | ||
9c80ecd6 SM |
41 | /* The id of this thread. */ |
42 | ptid_t id; | |
80894984 | 43 | |
623b6bdf | 44 | void *target_data; |
d2f325df | 45 | struct regcache *regcache_data = nullptr; |
623b6bdf YQ |
46 | |
47 | /* The last resume GDB requested on this thread. */ | |
d2f325df | 48 | enum resume_kind last_resume_kind = resume_continue; |
623b6bdf YQ |
49 | |
50 | /* The last wait status reported for this thread. */ | |
51 | struct target_waitstatus last_status; | |
52 | ||
b7ea362b | 53 | /* True if LAST_STATUS hasn't been reported to GDB yet. */ |
d2f325df | 54 | int status_pending_p = 0; |
b7ea362b | 55 | |
623b6bdf YQ |
56 | /* Given `while-stepping', a thread may be collecting data for more |
57 | than one tracepoint simultaneously. E.g.: | |
58 | ||
59 | ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs | |
60 | ff0002 INSN2 | |
61 | ff0003 INSN3 <-- TP2, collect $regs | |
62 | ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs | |
63 | ff0005 INSN5 | |
64 | ||
65 | Notice that when instruction INSN5 is reached, the while-stepping | |
66 | actions of both TP1 and TP3 are still being collected, and that TP2 | |
67 | had been collected meanwhile. The whole range of ff0001-ff0005 | |
68 | should be single-stepped, due to at least TP1's while-stepping | |
69 | action covering the whole range. | |
70 | ||
71 | On the other hand, the same tracepoint with a while-stepping action | |
72 | may be hit by more than one thread simultaneously, hence we can't | |
73 | keep the current step count in the tracepoint itself. | |
74 | ||
75 | This is the head of the list of the states of `while-stepping' | |
76 | tracepoint actions this thread is now collecting; NULL if empty. | |
77 | Each item in the list holds the current step of the while-stepping | |
78 | action. */ | |
d2f325df | 79 | struct wstep_state *while_stepping = nullptr; |
9accd112 MM |
80 | |
81 | /* Branch trace target information for this thread. */ | |
d2f325df | 82 | struct btrace_target_info *btrace = nullptr; |
65c459ab PA |
83 | |
84 | /* Thread options GDB requested with QThreadOptions. */ | |
85 | gdb_thread_options thread_options = 0; | |
623b6bdf YQ |
86 | }; |
87 | ||
9c80ecd6 | 88 | extern std::list<thread_info *> all_threads; |
623b6bdf YQ |
89 | |
90 | void remove_thread (struct thread_info *thread); | |
f7667f0d | 91 | struct thread_info *add_thread (ptid_t ptid, void *target_data); |
623b6bdf | 92 | |
9c80ecd6 SM |
93 | /* Return a pointer to the first thread, or NULL if there isn't one. */ |
94 | ||
649ebbca DE |
95 | struct thread_info *get_first_thread (void); |
96 | ||
623b6bdf | 97 | struct thread_info *find_thread_ptid (ptid_t ptid); |
623b6bdf | 98 | |
34c65914 PA |
99 | /* Find any thread of the PID process. Returns NULL if none is |
100 | found. */ | |
101 | struct thread_info *find_any_thread_of_pid (int pid); | |
102 | ||
9c80ecd6 SM |
103 | /* Find the first thread for which FUNC returns true. Return NULL if no thread |
104 | satisfying FUNC is found. */ | |
105 | ||
106 | template <typename Func> | |
107 | static thread_info * | |
108 | find_thread (Func func) | |
109 | { | |
110 | std::list<thread_info *>::iterator next, cur = all_threads.begin (); | |
111 | ||
112 | while (cur != all_threads.end ()) | |
113 | { | |
114 | next = cur; | |
115 | next++; | |
116 | ||
117 | if (func (*cur)) | |
118 | return *cur; | |
119 | ||
120 | cur = next; | |
121 | } | |
122 | ||
123 | return NULL; | |
124 | } | |
125 | ||
4d3bb80e SM |
126 | /* Like the above, but only consider threads with pid PID. */ |
127 | ||
128 | template <typename Func> | |
129 | static thread_info * | |
130 | find_thread (int pid, Func func) | |
131 | { | |
132 | return find_thread ([&] (thread_info *thread) | |
133 | { | |
134 | return thread->id.pid () == pid && func (thread); | |
135 | }); | |
136 | } | |
137 | ||
6d1e5673 SM |
138 | /* Find the first thread that matches FILTER for which FUNC returns true. |
139 | Return NULL if no thread satisfying these conditions is found. */ | |
140 | ||
141 | template <typename Func> | |
142 | static thread_info * | |
143 | find_thread (ptid_t filter, Func func) | |
144 | { | |
145 | return find_thread ([&] (thread_info *thread) { | |
146 | return thread->id.matches (filter) && func (thread); | |
147 | }); | |
148 | } | |
149 | ||
9c80ecd6 SM |
150 | /* Invoke FUNC for each thread. */ |
151 | ||
152 | template <typename Func> | |
153 | static void | |
154 | for_each_thread (Func func) | |
155 | { | |
156 | std::list<thread_info *>::iterator next, cur = all_threads.begin (); | |
157 | ||
158 | while (cur != all_threads.end ()) | |
159 | { | |
160 | next = cur; | |
161 | next++; | |
162 | func (*cur); | |
163 | cur = next; | |
164 | } | |
165 | } | |
166 | ||
4d3bb80e SM |
167 | /* Like the above, but only consider threads with pid PID. */ |
168 | ||
169 | template <typename Func> | |
170 | static void | |
171 | for_each_thread (int pid, Func func) | |
172 | { | |
173 | for_each_thread ([&] (thread_info *thread) | |
174 | { | |
175 | if (pid == thread->id.pid ()) | |
176 | func (thread); | |
177 | }); | |
178 | } | |
179 | ||
9c80ecd6 SM |
180 | /* Find the a random thread for which FUNC (THREAD) returns true. If |
181 | no entry is found then return NULL. */ | |
182 | ||
183 | template <typename Func> | |
184 | static thread_info * | |
185 | find_thread_in_random (Func func) | |
186 | { | |
187 | int count = 0; | |
188 | int random_selector; | |
189 | ||
190 | /* First count how many interesting entries we have. */ | |
191 | for_each_thread ([&] (thread_info *thread) { | |
192 | if (func (thread)) | |
193 | count++; | |
194 | }); | |
195 | ||
196 | if (count == 0) | |
197 | return NULL; | |
198 | ||
199 | /* Now randomly pick an entry out of those. */ | |
200 | random_selector = (int) | |
201 | ((count * (double) rand ()) / (RAND_MAX + 1.0)); | |
202 | ||
da4ae14a TT |
203 | thread_info *thread = find_thread ([&] (thread_info *thr_arg) { |
204 | return func (thr_arg) && (random_selector-- == 0); | |
9c80ecd6 SM |
205 | }); |
206 | ||
207 | gdb_assert (thread != NULL); | |
208 | ||
209 | return thread; | |
210 | } | |
211 | ||
fbd5db48 | 212 | /* Get current thread ID (Linux task ID). */ |
9c80ecd6 | 213 | #define current_ptid (current_thread->id) |
f5a02773 | 214 | |
9179355e SM |
215 | /* Get the ptid of THREAD. */ |
216 | ||
217 | static inline ptid_t | |
218 | ptid_of (const thread_info *thread) | |
219 | { | |
9c80ecd6 | 220 | return thread->id; |
9179355e SM |
221 | } |
222 | ||
223 | /* Get the pid of THREAD. */ | |
224 | ||
225 | static inline int | |
226 | pid_of (const thread_info *thread) | |
227 | { | |
9c80ecd6 | 228 | return thread->id.pid (); |
9179355e SM |
229 | } |
230 | ||
231 | /* Get the lwp of THREAD. */ | |
232 | ||
233 | static inline long | |
234 | lwpid_of (const thread_info *thread) | |
235 | { | |
9c80ecd6 | 236 | return thread->id.lwp (); |
9179355e SM |
237 | } |
238 | ||
f24791b7 TBA |
239 | /* Switch the current thread. */ |
240 | ||
241 | void switch_to_thread (thread_info *thread); | |
242 | ||
243 | /* Save/restore current thread. */ | |
244 | ||
245 | class scoped_restore_current_thread | |
246 | { | |
247 | public: | |
248 | scoped_restore_current_thread (); | |
249 | ~scoped_restore_current_thread (); | |
250 | ||
251 | DISABLE_COPY_AND_ASSIGN (scoped_restore_current_thread); | |
252 | ||
253 | /* Cancel restoring on scope exit. */ | |
254 | void dont_restore () { m_dont_restore = true; } | |
255 | ||
256 | private: | |
257 | bool m_dont_restore = false; | |
7f8acede | 258 | process_info *m_process; |
f24791b7 TBA |
259 | thread_info *m_thread; |
260 | }; | |
261 | ||
1a5c2598 | 262 | #endif /* GDBSERVER_GDBTHREAD_H */ |