GDB (xrefs)
Loading...
Searching...
No Matches
thread-iter.h
Go to the documentation of this file.
1/* Thread iterators and ranges for GDB, the GNU debugger.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
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 "gdbsupport/filtered-iterator.h"
23#include "gdbsupport/iterator-range.h"
24#include "gdbsupport/next-iterator.h"
25#include "gdbsupport/reference-to-pointer-iterator.h"
26#include "gdbsupport/safe-iterator.h"
27
28/* A forward iterator that iterates over a given inferior's
29 threads. */
30
32 = reference_to_pointer_iterator<intrusive_list<thread_info>::iterator>;
33
34/* A forward iterator that iterates over all threads of all
35 inferiors. */
38{
39public:
41 typedef struct thread_info *value_type;
42 typedef struct thread_info *&reference;
43 typedef struct thread_info **pointer;
44 typedef std::forward_iterator_tag iterator_category;
45 typedef int difference_type;
46
47 /* Tag type. */
48 struct begin_t {};
49
50 /* Create an iterator that points to the first thread of the first
51 inferior. */
53
54 /* Create a one-past-end iterator. */
56 : m_thr (nullptr)
57 {}
59 thread_info *operator* () const { return m_thr; }
62 {
63 advance ();
64 return *this;
65 }
67 bool operator== (const all_threads_iterator &other) const
68 { return m_thr == other.m_thr; }
70 bool operator!= (const all_threads_iterator &other) const
71 { return m_thr != other.m_thr; }
72
73private:
74 /* Advance to the next thread. */
75 void advance ();
76
77private:
78 /* The current inferior and thread. M_THR is NULL if we reached the
79 end of the threads list of the last inferior. */
82};
83
84/* Iterate over all threads that match a given PTID. */
87{
88public:
90 typedef struct thread_info *value_type;
91 typedef struct thread_info *&reference;
92 typedef struct thread_info **pointer;
93 typedef std::forward_iterator_tag iterator_category;
94 typedef int difference_type;
95
96 /* Creates an iterator that iterates over all threads that match
97 FILTER_PTID. */
99 ptid_t filter_ptid);
100
101 /* Create a one-past-end iterator. */
104 thread_info *operator* () const { return m_thr; }
107 {
108 advance ();
109 return *this;
110 }
112 bool operator== (const all_matching_threads_iterator &other) const
113 { return m_thr == other.m_thr; }
115 bool operator!= (const all_matching_threads_iterator &other) const
116 { return m_thr != other.m_thr; }
117
118private:
119 /* Advance to next thread, skipping filtered threads. */
120 void advance ();
121
122 /* True if M_INF has the process target M_FILTER_TARGET. */
123 bool m_inf_matches ();
124
125private:
126 enum class mode
127 {
128 /* All threads, possibly filtered down to a single target. */
130
131 /* All threads of the given inferior. */
133
134 /* A specific thread. */
136 } m_mode;
137
138 /* The current inferior. */
139 inferior *m_inf = nullptr;
140
141 /* The current thread. */
142 thread_info *m_thr = nullptr;
143
144 /* The target we filter on (may be nullptr). */
146};
147
148/* Filter for filtered_iterator. Filters out exited threads. */
152 bool operator() (struct thread_info *thr) const
153 {
154 return thr->state != THREAD_EXITED;
155 }
156};
157
158/* Iterate over all non-exited threads that match a given PTID. */
161 = filtered_iterator<all_matching_threads_iterator, non_exited_thread_filter>;
162
163/* Iterate over all non-exited threads of an inferior. */
164
166 = filtered_iterator<inf_threads_iterator, non_exited_thread_filter>;
168/* Iterate over all threads of all inferiors, safely. */
169
171 = basic_safe_iterator<all_threads_iterator>;
172
173/* Iterate over all threads of an inferior, safely. */
174
176 = basic_safe_iterator<inf_threads_iterator>;
177
178/* A range adapter that makes it possible to iterate over all threads
179 of an inferior with range-for. */
180
181using inf_threads_range = iterator_range<inf_threads_iterator>;
182
183/* A range adapter that makes it possible to iterate over all
184 non-exited threads of an inferior with range-for. */
185
187 = iterator_range<inf_non_exited_threads_iterator>;
188
189/* A range adapter that makes it possible to iterate over all threads
190 of an inferior with range-for, safely. */
191
192using safe_inf_threads_range = iterator_range<safe_inf_threads_iterator>;
193
194/* A range adapter that makes it possible to iterate over all threads
195 with range-for "safely". I.e., it is safe to delete the
196 currently-iterated thread. */
198using all_threads_safe_range = iterator_range<all_threads_safe_iterator>;
199
200/* A range adapter that makes it possible to iterate over all threads
201 that match a PTID filter with range-for. */
202
205public:
207 ptid_t filter_ptid)
208 : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
209 {}
212 {}
213
217 { return all_matching_threads_iterator (); }
218
219private:
220 /* The filter. */
222 ptid_t m_filter_ptid;
223};
225/* A range adapter that makes it possible to iterate over all
226 non-exited threads of all inferiors, with range-for.
227 Threads/inferiors that do not match FILTER_PTID are filtered
228 out. */
229
231{
232public:
234 ptid_t filter_ptid)
235 : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
236 {}
237
240 {}
241
246
247private:
249 ptid_t m_filter_ptid;
250};
251
252#endif /* THREAD_ITER_H */
process_stratum_target * m_filter_target
all_matching_threads_iterator self_type
Definition thread-iter.h:88
enum all_matching_threads_iterator::mode m_mode
std::forward_iterator_tag iterator_category
Definition thread-iter.h:92
bool operator!=(const all_matching_threads_iterator &other) const
struct thread_info *& reference
Definition thread-iter.h:90
bool operator==(const all_matching_threads_iterator &other) const
struct thread_info ** pointer
Definition thread-iter.h:91
thread_info * operator*() const
struct thread_info * value_type
Definition thread-iter.h:89
all_matching_threads_iterator & operator++()
all_non_exited_threads_iterator end() const
process_stratum_target * m_filter_target
all_non_exited_threads_iterator begin() const
thread_info * m_thr
Definition thread-iter.h:80
all_threads_iterator self_type
Definition thread-iter.h:39
thread_info * operator*() const
Definition thread-iter.h:58
std::forward_iterator_tag iterator_category
Definition thread-iter.h:43
struct thread_info ** pointer
Definition thread-iter.h:42
struct thread_info * value_type
Definition thread-iter.h:40
struct thread_info *& reference
Definition thread-iter.h:41
bool operator!=(const all_threads_iterator &other) const
Definition thread-iter.h:69
all_threads_iterator & operator++()
Definition thread-iter.h:60
bool operator==(const all_threads_iterator &other) const
Definition thread-iter.h:66
enum thread_state state
Definition gdbthread.h:339
@ THREAD_EXITED
Definition gdbthread.h:79
all_matching_threads_iterator begin() const
all_matching_threads_iterator end() const
process_stratum_target * m_filter_target
bool operator()(struct thread_info *thr) const
basic_safe_iterator< all_threads_iterator > all_threads_safe_iterator
filtered_iterator< all_matching_threads_iterator, non_exited_thread_filter > all_non_exited_threads_iterator
iterator_range< safe_inf_threads_iterator > safe_inf_threads_range
reference_to_pointer_iterator< intrusive_list< thread_info >::iterator > inf_threads_iterator
Definition thread-iter.h:31
iterator_range< inf_non_exited_threads_iterator > inf_non_exited_threads_range
iterator_range< inf_threads_iterator > inf_threads_range
iterator_range< all_threads_safe_iterator > all_threads_safe_range
basic_safe_iterator< inf_threads_iterator > safe_inf_threads_iterator
filtered_iterator< inf_threads_iterator, non_exited_thread_filter > inf_non_exited_threads_iterator
#define nullptr
Definition x86-cpuid.h:28