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