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