Commit | Line | Data |
---|---|---|
08036331 | 1 | /* Thread iterators and ranges for GDB, the GNU debugger. |
42a4f53d | 2 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
08036331 PA |
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 | ||
19 | #ifndef THREAD_ITER_H | |
20 | #define THREAD_ITER_H | |
21 | ||
22 | #include "common/filtered-iterator.h" | |
23 | #include "common/safe-iterator.h" | |
24 | ||
25 | /* A forward iterator that iterates over a given inferior's | |
26 | threads. */ | |
27 | ||
28 | class inf_threads_iterator | |
29 | { | |
30 | public: | |
31 | typedef inf_threads_iterator self_type; | |
32 | typedef struct thread_info *value_type; | |
33 | typedef struct thread_info *&reference; | |
34 | typedef struct thread_info **pointer; | |
35 | typedef std::forward_iterator_tag iterator_category; | |
36 | typedef int difference_type; | |
37 | ||
38 | /* Create an iterator pointing at HEAD. This takes a thread pointer | |
39 | instead of an inferior pointer to avoid circular dependencies | |
40 | between the thread and inferior header files. */ | |
41 | explicit inf_threads_iterator (struct thread_info *head) | |
42 | : m_thr (head) | |
43 | {} | |
44 | ||
45 | /* Create a one-past-end iterator. */ | |
46 | inf_threads_iterator () | |
47 | : m_thr (nullptr) | |
48 | {} | |
49 | ||
50 | inf_threads_iterator& operator++ () | |
51 | { | |
52 | m_thr = m_thr->next; | |
53 | return *this; | |
54 | } | |
55 | ||
56 | thread_info *operator* () const { return m_thr; } | |
57 | ||
58 | bool operator!= (const inf_threads_iterator &other) const | |
59 | { return m_thr != other.m_thr; } | |
60 | ||
61 | private: | |
62 | /* The currently-iterated thread. NULL if we reached the end of the | |
63 | list. */ | |
64 | thread_info *m_thr; | |
65 | }; | |
66 | ||
67 | /* A range adapter that makes it possible to iterate over an | |
68 | inferior's thread list with range-for. */ | |
69 | template<typename Iterator> | |
70 | struct basic_inf_threads_range | |
71 | { | |
72 | friend struct inferior; | |
73 | private: | |
74 | explicit basic_inf_threads_range (struct thread_info *head) | |
75 | : m_head (head) | |
76 | {} | |
77 | ||
78 | public: | |
79 | Iterator begin () const { return Iterator (m_head); } | |
80 | Iterator end () const { return Iterator (); } | |
81 | ||
82 | private: | |
83 | thread_info *m_head; | |
84 | }; | |
85 | ||
86 | /* A forward iterator that iterates over all threads of all | |
87 | inferiors. */ | |
88 | ||
89 | class all_threads_iterator | |
90 | { | |
91 | public: | |
92 | typedef all_threads_iterator self_type; | |
93 | typedef struct thread_info *value_type; | |
94 | typedef struct thread_info *&reference; | |
95 | typedef struct thread_info **pointer; | |
96 | typedef std::forward_iterator_tag iterator_category; | |
97 | typedef int difference_type; | |
98 | ||
99 | /* Tag type. */ | |
100 | struct begin_t {}; | |
101 | ||
102 | /* Create an iterator that points to the first thread of the first | |
103 | inferior. */ | |
104 | explicit all_threads_iterator (begin_t); | |
105 | ||
106 | /* Create a one-past-end iterator. */ | |
107 | all_threads_iterator () | |
108 | : m_thr (nullptr) | |
109 | {} | |
110 | ||
111 | thread_info *operator* () const { return m_thr; } | |
112 | ||
113 | all_threads_iterator &operator++ () | |
114 | { | |
115 | advance (); | |
116 | return *this; | |
117 | } | |
118 | ||
119 | bool operator== (const all_threads_iterator &other) const | |
120 | { return m_thr == other.m_thr; } | |
121 | ||
122 | bool operator!= (const all_threads_iterator &other) const | |
123 | { return m_thr != other.m_thr; } | |
124 | ||
125 | private: | |
126 | /* Advance to the next thread. */ | |
127 | void advance (); | |
128 | ||
129 | private: | |
130 | /* The current inferior and thread. M_THR is NULL if we reached the | |
131 | end of the threads list of the last inferior. */ | |
132 | inferior *m_inf; | |
133 | thread_info *m_thr; | |
134 | }; | |
135 | ||
136 | /* Iterate over all threads that match a given PTID. */ | |
137 | ||
138 | class all_matching_threads_iterator | |
139 | { | |
140 | public: | |
141 | typedef all_matching_threads_iterator self_type; | |
142 | typedef struct thread_info *value_type; | |
143 | typedef struct thread_info *&reference; | |
144 | typedef struct thread_info **pointer; | |
145 | typedef std::forward_iterator_tag iterator_category; | |
146 | typedef int difference_type; | |
147 | ||
148 | /* Creates an iterator that iterates over all threads that match | |
149 | FILTER_PTID. */ | |
150 | explicit all_matching_threads_iterator (ptid_t filter_ptid); | |
151 | ||
152 | /* Create a one-past-end iterator. */ | |
153 | all_matching_threads_iterator () | |
154 | : m_inf (nullptr), | |
155 | m_thr (nullptr), | |
156 | m_filter_ptid (minus_one_ptid) | |
157 | {} | |
158 | ||
159 | thread_info *operator* () const { return m_thr; } | |
160 | ||
161 | all_matching_threads_iterator &operator++ () | |
162 | { | |
163 | advance (); | |
164 | return *this; | |
165 | } | |
166 | ||
167 | bool operator== (const all_matching_threads_iterator &other) const | |
168 | { return m_thr == other.m_thr; } | |
169 | ||
170 | bool operator!= (const all_matching_threads_iterator &other) const | |
171 | { return m_thr != other.m_thr; } | |
172 | ||
173 | private: | |
174 | /* Advance to next thread, skipping filtered threads. */ | |
175 | void advance (); | |
176 | ||
177 | /* True if M_INF matches the process identified by | |
178 | M_FILTER_PTID. */ | |
179 | bool m_inf_matches (); | |
180 | ||
181 | private: | |
182 | /* The current inferior. */ | |
183 | inferior *m_inf; | |
184 | ||
185 | /* The current thread. */ | |
186 | thread_info *m_thr; | |
187 | ||
188 | /* The filter. */ | |
189 | ptid_t m_filter_ptid; | |
190 | }; | |
191 | ||
192 | /* Filter for filtered_iterator. Filters out exited threads. */ | |
193 | ||
194 | struct non_exited_thread_filter | |
195 | { | |
196 | bool operator() (struct thread_info *thr) const | |
197 | { | |
198 | return thr->state != THREAD_EXITED; | |
199 | } | |
200 | }; | |
201 | ||
202 | /* Iterate over all non-exited threads that match a given PTID. */ | |
203 | ||
204 | using all_non_exited_threads_iterator | |
205 | = filtered_iterator<all_matching_threads_iterator, non_exited_thread_filter>; | |
206 | ||
207 | /* Iterate over all non-exited threads of an inferior. */ | |
208 | ||
209 | using inf_non_exited_threads_iterator | |
210 | = filtered_iterator<inf_threads_iterator, non_exited_thread_filter>; | |
211 | ||
212 | /* Iterate over all threads of all inferiors, safely. */ | |
213 | ||
214 | using all_threads_safe_iterator | |
215 | = basic_safe_iterator<all_threads_iterator>; | |
216 | ||
217 | /* Iterate over all threads of an inferior, safely. */ | |
218 | ||
219 | using safe_inf_threads_iterator | |
220 | = basic_safe_iterator<inf_threads_iterator>; | |
221 | ||
222 | /* A range adapter that makes it possible to iterate over all threads | |
223 | of an inferior with range-for. */ | |
224 | ||
225 | using inf_threads_range | |
226 | = basic_inf_threads_range<inf_threads_iterator>; | |
227 | ||
228 | /* A range adapter that makes it possible to iterate over all | |
229 | non-exited threads of an inferior with range-for. */ | |
230 | ||
231 | using inf_non_exited_threads_range | |
232 | = basic_inf_threads_range<inf_non_exited_threads_iterator>; | |
233 | ||
234 | /* A range adapter that makes it possible to iterate over all threads | |
235 | of an inferior with range-for, safely. */ | |
236 | ||
237 | using safe_inf_threads_range | |
238 | = basic_inf_threads_range<safe_inf_threads_iterator>; | |
239 | ||
240 | /* A range adapter that makes it possible to iterate over all threads | |
241 | of all inferiors with range-for. */ | |
242 | ||
243 | struct all_threads_range | |
244 | { | |
245 | all_threads_iterator begin () const | |
246 | { return all_threads_iterator (all_threads_iterator::begin_t {}); } | |
247 | all_threads_iterator end () const | |
248 | { return all_threads_iterator (); } | |
249 | }; | |
250 | ||
251 | /* A range adapter that makes it possible to iterate over all threads | |
252 | with range-for "safely". I.e., it is safe to delete the | |
253 | currently-iterated thread. */ | |
254 | ||
255 | struct all_threads_safe_range | |
256 | { | |
257 | all_threads_safe_iterator begin () const | |
258 | { return all_threads_safe_iterator (all_threads_iterator::begin_t {}); } | |
259 | all_threads_safe_iterator end () const | |
260 | { return all_threads_safe_iterator (); } | |
261 | }; | |
262 | ||
263 | /* A range adapter that makes it possible to iterate over all threads | |
264 | that match a PTID filter with range-for. */ | |
265 | ||
266 | struct all_matching_threads_range | |
267 | { | |
268 | public: | |
269 | explicit all_matching_threads_range (ptid_t filter_ptid) | |
270 | : m_filter_ptid (filter_ptid) | |
271 | {} | |
272 | all_matching_threads_range () | |
273 | : m_filter_ptid (minus_one_ptid) | |
274 | {} | |
275 | ||
276 | all_matching_threads_iterator begin () const | |
277 | { return all_matching_threads_iterator (m_filter_ptid); } | |
278 | all_matching_threads_iterator end () const | |
279 | { return all_matching_threads_iterator (); } | |
280 | ||
281 | private: | |
282 | /* The filter. */ | |
283 | ptid_t m_filter_ptid; | |
284 | }; | |
285 | ||
286 | /* A range adapter that makes it possible to iterate over all | |
287 | non-exited threads of all inferiors, with range-for. | |
288 | Threads/inferiors that do not match FILTER_PTID are filtered | |
289 | out. */ | |
290 | ||
291 | class all_non_exited_threads_range | |
292 | { | |
293 | public: | |
294 | explicit all_non_exited_threads_range (ptid_t filter_ptid) | |
295 | : m_filter_ptid (filter_ptid) | |
296 | {} | |
297 | ||
298 | all_non_exited_threads_range () | |
299 | : m_filter_ptid (minus_one_ptid) | |
300 | {} | |
301 | ||
302 | all_non_exited_threads_iterator begin () const | |
303 | { return all_non_exited_threads_iterator (m_filter_ptid); } | |
304 | all_non_exited_threads_iterator end () const | |
305 | { return all_non_exited_threads_iterator (); } | |
306 | ||
307 | private: | |
308 | ptid_t m_filter_ptid; | |
309 | }; | |
310 | ||
311 | #endif /* THREAD_ITER_H */ |