1 /* Thread iterators and ranges for GDB, the GNU debugger.
2 Copyright (C) 2018-2019 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/filtered-iterator.h"
23 #include "common/safe-iterator.h"
25 /* A forward iterator that iterates over a given inferior's
28 class inf_threads_iterator
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
;
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
)
45 /* Create a one-past-end iterator. */
46 inf_threads_iterator ()
50 inf_threads_iterator
& operator++ ()
56 thread_info
*operator* () const { return m_thr
; }
58 bool operator!= (const inf_threads_iterator
&other
) const
59 { return m_thr
!= other
.m_thr
; }
62 /* The currently-iterated thread. NULL if we reached the end of the
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
72 friend struct inferior
;
74 explicit basic_inf_threads_range (struct thread_info
*head
)
79 Iterator
begin () const { return Iterator (m_head
); }
80 Iterator
end () const { return Iterator (); }
86 /* A forward iterator that iterates over all threads of all
89 class all_threads_iterator
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
;
102 /* Create an iterator that points to the first thread of the first
104 explicit all_threads_iterator (begin_t
);
106 /* Create a one-past-end iterator. */
107 all_threads_iterator ()
111 thread_info
*operator* () const { return m_thr
; }
113 all_threads_iterator
&operator++ ()
119 bool operator== (const all_threads_iterator
&other
) const
120 { return m_thr
== other
.m_thr
; }
122 bool operator!= (const all_threads_iterator
&other
) const
123 { return m_thr
!= other
.m_thr
; }
126 /* Advance to the next thread. */
130 /* The current inferior and thread. M_THR is NULL if we reached the
131 end of the threads list of the last inferior. */
136 /* Iterate over all threads that match a given PTID. */
138 class all_matching_threads_iterator
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
;
148 /* Creates an iterator that iterates over all threads that match
150 explicit all_matching_threads_iterator (ptid_t filter_ptid
);
152 /* Create a one-past-end iterator. */
153 all_matching_threads_iterator ()
156 m_filter_ptid (minus_one_ptid
)
159 thread_info
*operator* () const { return m_thr
; }
161 all_matching_threads_iterator
&operator++ ()
167 bool operator== (const all_matching_threads_iterator
&other
) const
168 { return m_thr
== other
.m_thr
; }
170 bool operator!= (const all_matching_threads_iterator
&other
) const
171 { return m_thr
!= other
.m_thr
; }
174 /* Advance to next thread, skipping filtered threads. */
177 /* True if M_INF matches the process identified by
179 bool m_inf_matches ();
182 /* The current inferior. */
185 /* The current thread. */
189 ptid_t m_filter_ptid
;
192 /* Filter for filtered_iterator. Filters out exited threads. */
194 struct non_exited_thread_filter
196 bool operator() (struct thread_info
*thr
) const
198 return thr
->state
!= THREAD_EXITED
;
202 /* Iterate over all non-exited threads that match a given PTID. */
204 using all_non_exited_threads_iterator
205 = filtered_iterator
<all_matching_threads_iterator
, non_exited_thread_filter
>;
207 /* Iterate over all non-exited threads of an inferior. */
209 using inf_non_exited_threads_iterator
210 = filtered_iterator
<inf_threads_iterator
, non_exited_thread_filter
>;
212 /* Iterate over all threads of all inferiors, safely. */
214 using all_threads_safe_iterator
215 = basic_safe_iterator
<all_threads_iterator
>;
217 /* Iterate over all threads of an inferior, safely. */
219 using safe_inf_threads_iterator
220 = basic_safe_iterator
<inf_threads_iterator
>;
222 /* A range adapter that makes it possible to iterate over all threads
223 of an inferior with range-for. */
225 using inf_threads_range
226 = basic_inf_threads_range
<inf_threads_iterator
>;
228 /* A range adapter that makes it possible to iterate over all
229 non-exited threads of an inferior with range-for. */
231 using inf_non_exited_threads_range
232 = basic_inf_threads_range
<inf_non_exited_threads_iterator
>;
234 /* A range adapter that makes it possible to iterate over all threads
235 of an inferior with range-for, safely. */
237 using safe_inf_threads_range
238 = basic_inf_threads_range
<safe_inf_threads_iterator
>;
240 /* A range adapter that makes it possible to iterate over all threads
241 of all inferiors with range-for. */
243 struct all_threads_range
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 (); }
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. */
255 struct all_threads_safe_range
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 (); }
263 /* A range adapter that makes it possible to iterate over all threads
264 that match a PTID filter with range-for. */
266 struct all_matching_threads_range
269 explicit all_matching_threads_range (ptid_t filter_ptid
)
270 : m_filter_ptid (filter_ptid
)
272 all_matching_threads_range ()
273 : m_filter_ptid (minus_one_ptid
)
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 (); }
283 ptid_t m_filter_ptid
;
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
291 class all_non_exited_threads_range
294 explicit all_non_exited_threads_range (ptid_t filter_ptid
)
295 : m_filter_ptid (filter_ptid
)
298 all_non_exited_threads_range ()
299 : m_filter_ptid (minus_one_ptid
)
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 (); }
308 ptid_t m_filter_ptid
;
311 #endif /* THREAD_ITER_H */