gdbserver: replace direct assignments to current_thread
[binutils-gdb.git] / gdbserver / inferiors.cc
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "gdbsupport/common-inferior.h"
23 #include "gdbthread.h"
24 #include "dll.h"
25
26 std::list<process_info *> all_processes;
27 std::list<thread_info *> all_threads;
28
29 struct thread_info *current_thread;
30
31 /* The current working directory used to start the inferior.
32
33 Empty if not specified. */
34 static std::string current_inferior_cwd;
35
36 struct thread_info *
37 add_thread (ptid_t thread_id, void *target_data)
38 {
39 thread_info *new_thread = new thread_info (thread_id, target_data);
40
41 all_threads.push_back (new_thread);
42
43 if (current_thread == NULL)
44 switch_to_thread (new_thread);
45
46 return new_thread;
47 }
48
49 /* See gdbthread.h. */
50
51 struct thread_info *
52 get_first_thread (void)
53 {
54 if (!all_threads.empty ())
55 return all_threads.front ();
56 else
57 return NULL;
58 }
59
60 struct thread_info *
61 find_thread_ptid (ptid_t ptid)
62 {
63 return find_thread ([&] (thread_info *thread) {
64 return thread->id == ptid;
65 });
66 }
67
68 /* Find a thread associated with the given PROCESS, or NULL if no
69 such thread exists. */
70
71 static struct thread_info *
72 find_thread_process (const struct process_info *const process)
73 {
74 return find_any_thread_of_pid (process->pid);
75 }
76
77 /* See gdbthread.h. */
78
79 struct thread_info *
80 find_any_thread_of_pid (int pid)
81 {
82 return find_thread (pid, [] (thread_info *thread) {
83 return true;
84 });
85 }
86
87 static void
88 free_one_thread (thread_info *thread)
89 {
90 delete thread;
91 }
92
93 void
94 remove_thread (struct thread_info *thread)
95 {
96 if (thread->btrace != NULL)
97 target_disable_btrace (thread->btrace);
98
99 discard_queued_stop_replies (ptid_of (thread));
100 all_threads.remove (thread);
101 if (current_thread == thread)
102 switch_to_thread (nullptr);
103 free_one_thread (thread);
104 }
105
106 void *
107 thread_target_data (struct thread_info *thread)
108 {
109 return thread->target_data;
110 }
111
112 struct regcache *
113 thread_regcache_data (struct thread_info *thread)
114 {
115 return thread->regcache_data;
116 }
117
118 void
119 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
120 {
121 thread->regcache_data = data;
122 }
123
124 void
125 clear_inferiors (void)
126 {
127 for_each_thread (free_one_thread);
128 all_threads.clear ();
129
130 clear_dlls ();
131
132 switch_to_thread (nullptr);
133 }
134
135 struct process_info *
136 add_process (int pid, int attached)
137 {
138 process_info *process = new process_info (pid, attached);
139
140 all_processes.push_back (process);
141
142 return process;
143 }
144
145 /* Remove a process from the common process list and free the memory
146 allocated for it.
147 The caller is responsible for freeing private data first. */
148
149 void
150 remove_process (struct process_info *process)
151 {
152 clear_symbol_cache (&process->symbol_cache);
153 free_all_breakpoints (process);
154 gdb_assert (find_thread_process (process) == NULL);
155 all_processes.remove (process);
156 delete process;
157 }
158
159 process_info *
160 find_process_pid (int pid)
161 {
162 return find_process ([&] (process_info *process) {
163 return process->pid == pid;
164 });
165 }
166
167 /* Get the first process in the process list, or NULL if the list is empty. */
168
169 process_info *
170 get_first_process (void)
171 {
172 if (!all_processes.empty ())
173 return all_processes.front ();
174 else
175 return NULL;
176 }
177
178 /* Return non-zero if there are any inferiors that we have created
179 (as opposed to attached-to). */
180
181 int
182 have_started_inferiors_p (void)
183 {
184 return find_process ([] (process_info *process) {
185 return !process->attached;
186 }) != NULL;
187 }
188
189 /* Return non-zero if there are any inferiors that we have attached to. */
190
191 int
192 have_attached_inferiors_p (void)
193 {
194 return find_process ([] (process_info *process) {
195 return process->attached;
196 }) != NULL;
197 }
198
199 struct process_info *
200 get_thread_process (const struct thread_info *thread)
201 {
202 return find_process_pid (thread->id.pid ());
203 }
204
205 struct process_info *
206 current_process (void)
207 {
208 gdb_assert (current_thread != NULL);
209 return get_thread_process (current_thread);
210 }
211
212 /* See gdbsupport/common-gdbthread.h. */
213
214 void
215 switch_to_thread (process_stratum_target *ops, ptid_t ptid)
216 {
217 gdb_assert (ptid != minus_one_ptid);
218 switch_to_thread (find_thread_ptid (ptid));
219 }
220
221 /* See gdbthread.h. */
222
223 void
224 switch_to_thread (thread_info *thread)
225 {
226 current_thread = thread;
227 }
228
229 /* See inferiors.h. */
230
231 void
232 switch_to_process (process_info *proc)
233 {
234 int pid = pid_of (proc);
235
236 switch_to_thread (find_any_thread_of_pid (pid));
237 }
238
239 /* See gdbsupport/common-inferior.h. */
240
241 const std::string &
242 get_inferior_cwd ()
243 {
244 return current_inferior_cwd;
245 }
246
247 /* See inferiors.h. */
248
249 void
250 set_inferior_cwd (std::string cwd)
251 {
252 current_inferior_cwd = std::move (cwd);
253 }
254
255 scoped_restore_current_thread::scoped_restore_current_thread ()
256 {
257 m_thread = current_thread;
258 }
259
260 scoped_restore_current_thread::~scoped_restore_current_thread ()
261 {
262 if (m_dont_restore)
263 return;
264
265 switch_to_thread (m_thread);
266 }