GDBserver

gdbthread.h
Go to the documentation of this file.
1 /* Multi-thread control defs for remote server for GDB.
2 Copyright (C) 1993-2018 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 GDB_THREAD_H
20 #define GDB_THREAD_H
21
22 #include "common-gdbthread.h"
23 #include "inferiors.h"
24
25 #include <list>
26
27 struct btrace_target_info;
28 struct regcache;
29
30 struct thread_info
31 {
32 /* The id of this thread. */
33 ptid_t id;
34
35 void *target_data;
36 struct regcache *regcache_data;
37
38 /* The last resume GDB requested on this thread. */
39 enum resume_kind last_resume_kind;
40
41 /* The last wait status reported for this thread. */
42 struct target_waitstatus last_status;
43
44 /* True if LAST_STATUS hasn't been reported to GDB yet. */
45 int status_pending_p;
46
47 /* Given `while-stepping', a thread may be collecting data for more
48 than one tracepoint simultaneously. E.g.:
49
50 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
51 ff0002 INSN2
52 ff0003 INSN3 <-- TP2, collect $regs
53 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
54 ff0005 INSN5
55
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.
61
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.
65
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
69 action. */
70 struct wstep_state *while_stepping;
71
72 /* Branch trace target information for this thread. */
73 struct btrace_target_info *btrace;
74 };
75
76 extern std::list<thread_info *> all_threads;
77
78 void remove_thread (struct thread_info *thread);
79 struct thread_info *add_thread (ptid_t ptid, void *target_data);
80
81 /* Return a pointer to the first thread, or NULL if there isn't one. */
82
83 struct thread_info *get_first_thread (void);
84
85 struct thread_info *find_thread_ptid (ptid_t ptid);
86
87 /* Find any thread of the PID process. Returns NULL if none is
88 found. */
89 struct thread_info *find_any_thread_of_pid (int pid);
90
91 /* Find the first thread for which FUNC returns true. Return NULL if no thread
92 satisfying FUNC is found. */
93
94 template <typename Func>
95 static thread_info *
96 find_thread (Func func)
97 {
98 std::list<thread_info *>::iterator next, cur = all_threads.begin ();
99
100 while (cur != all_threads.end ())
101 {
102 next = cur;
103 next++;
104
105 if (func (*cur))
106 return *cur;
107
108 cur = next;
109 }
110
111 return NULL;
112 }
113
114 /* Like the above, but only consider threads with pid PID. */
115
116 template <typename Func>
117 static thread_info *
118 find_thread (int pid, Func func)
119 {
120 return find_thread ([&] (thread_info *thread)
121 {
122 return thread->id.pid () == pid && func (thread);
123 });
124 }
125
126 /* Find the first thread that matches FILTER for which FUNC returns true.
127 Return NULL if no thread satisfying these conditions is found. */
128
129 template <typename Func>
130 static thread_info *
131 find_thread (ptid_t filter, Func func)
132 {
133 return find_thread ([&] (thread_info *thread) {
134 return thread->id.matches (filter) && func (thread);
135 });
136 }
137
138 /* Invoke FUNC for each thread. */
139
140 template <typename Func>
141 static void
142 for_each_thread (Func func)
143 {
144 std::list<thread_info *>::iterator next, cur = all_threads.begin ();
145
146 while (cur != all_threads.end ())
147 {
148 next = cur;
149 next++;
150 func (*cur);
151 cur = next;
152 }
153 }
154
155 /* Like the above, but only consider threads with pid PID. */
156
157 template <typename Func>
158 static void
159 for_each_thread (int pid, Func func)
160 {
161 for_each_thread ([&] (thread_info *thread)
162 {
163 if (pid == thread->id.pid ())
164 func (thread);
165 });
166 }
167
168 /* Find the a random thread for which FUNC (THREAD) returns true. If
169 no entry is found then return NULL. */
170
171 template <typename Func>
172 static thread_info *
173 find_thread_in_random (Func func)
174 {
175 int count = 0;
176 int random_selector;
177
178 /* First count how many interesting entries we have. */
179 for_each_thread ([&] (thread_info *thread) {
180 if (func (thread))
181 count++;
182 });
183
184 if (count == 0)
185 return NULL;
186
187 /* Now randomly pick an entry out of those. */
188 random_selector = (int)
189 ((count * (double) rand ()) / (RAND_MAX + 1.0));
190
191 thread_info *thread = find_thread ([&] (thread_info *thread) {
192 return func (thread) && (random_selector-- == 0);
193 });
194
195 gdb_assert (thread != NULL);
196
197 return thread;
198 }
199
200 /* Get current thread ID (Linux task ID). */
201 #define current_ptid (current_thread->id)
202
203 /* Get the ptid of THREAD. */
204
205 static inline ptid_t
206 ptid_of (const thread_info *thread)
207 {
208 return thread->id;
209 }
210
211 /* Get the pid of THREAD. */
212
213 static inline int
214 pid_of (const thread_info *thread)
215 {
216 return thread->id.pid ();
217 }
218
219 /* Get the lwp of THREAD. */
220
221 static inline long
222 lwpid_of (const thread_info *thread)
223 {
224 return thread->id.lwp ();
225 }
226
227 /* Create a cleanup to restore current_thread. */
228 struct cleanup *make_cleanup_restore_current_thread (void);
229
230 #endif /* GDB_THREAD_H */
ptid_t::pid
constexpr int pid() const
Definition: ptid.h:53
ptid_t::lwp
constexpr long lwp() const
Definition: ptid.h:63
find_thread_ptid
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: inferiors.c:64
thread_info::btrace
struct btrace_target_info * btrace
Definition: gdbthread.h:73
btrace_target_info
Definition: linux-btrace.h:85
inferiors.h
common-gdbthread.h
lwpid_of
static long lwpid_of(const thread_info *thread)
Definition: gdbthread.h:222
ptid_of
static ptid_t ptid_of(const thread_info *thread)
Definition: gdbthread.h:206
find_thread
static thread_info * find_thread(Func func)
Definition: gdbthread.h:96
wstep_state
Definition: tracepoint.c:811
find_any_thread_of_pid
struct thread_info * find_any_thread_of_pid(int pid)
Definition: inferiors.c:83
ptid_t::matches
constexpr bool matches(const ptid_t &filter) const
Definition: ptid.h:112
thread_info::while_stepping
struct wstep_state * while_stepping
Definition: gdbthread.h:70
cleanup
Definition: cleanups.c:38
for_each_thread
static void for_each_thread(Func func)
Definition: gdbthread.h:142
pid_of
static int pid_of(const thread_info *thread)
Definition: gdbthread.h:214
regcache
Definition: regcache.h:31
thread_info::last_resume_kind
enum resume_kind last_resume_kind
Definition: gdbthread.h:39
thread_info
Definition: gdbthread.h:30
thread_info::target_data
void * target_data
Definition: gdbthread.h:35
ptid_t
Definition: ptid.h:35
thread_info::status_pending_p
int status_pending_p
Definition: gdbthread.h:45
thread_info::regcache_data
struct regcache * regcache_data
Definition: gdbthread.h:36
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
get_first_thread
struct thread_info * get_first_thread(void)
Definition: inferiors.c:55
thread_info::id
ptid_t id
Definition: gdbthread.h:33
all_threads
std::list< thread_info * > all_threads
Definition: inferiors.c:26
make_cleanup_restore_current_thread
struct cleanup * make_cleanup_restore_current_thread(void)
Definition: inferiors.c:223
thread_info::last_status
struct target_waitstatus last_status
Definition: gdbthread.h:42
remove_thread
void remove_thread(struct thread_info *thread)
Definition: inferiors.c:98
find_thread_in_random
static thread_info * find_thread_in_random(Func func)
Definition: gdbthread.h:173
add_thread
struct thread_info * add_thread(ptid_t ptid, void *target_data)
Definition: inferiors.c:34