Remove path name from test case
[binutils-gdb.git] / gdbserver / inferiors.cc
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2023 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 /* The current process. */
30 static process_info *current_process_;
31
32 /* The current thread. This is either a thread of CURRENT_PROCESS, or
33 NULL. */
34 struct thread_info *current_thread;
35
36 /* The current working directory used to start the inferior.
37
38 Empty if not specified. */
39 static std::string current_inferior_cwd;
40
41 struct thread_info *
42 add_thread (ptid_t thread_id, void *target_data)
43 {
44 thread_info *new_thread = new thread_info (thread_id, target_data);
45
46 all_threads.push_back (new_thread);
47
48 if (current_thread == NULL)
49 switch_to_thread (new_thread);
50
51 return new_thread;
52 }
53
54 /* See gdbthread.h. */
55
56 struct thread_info *
57 get_first_thread (void)
58 {
59 if (!all_threads.empty ())
60 return all_threads.front ();
61 else
62 return NULL;
63 }
64
65 struct thread_info *
66 find_thread_ptid (ptid_t ptid)
67 {
68 return find_thread ([&] (thread_info *thread) {
69 return thread->id == ptid;
70 });
71 }
72
73 /* Find a thread associated with the given PROCESS, or NULL if no
74 such thread exists. */
75
76 static struct thread_info *
77 find_thread_process (const struct process_info *const process)
78 {
79 return find_any_thread_of_pid (process->pid);
80 }
81
82 /* See gdbthread.h. */
83
84 struct thread_info *
85 find_any_thread_of_pid (int pid)
86 {
87 return find_thread (pid, [] (thread_info *thread) {
88 return true;
89 });
90 }
91
92 static void
93 free_one_thread (thread_info *thread)
94 {
95 delete thread;
96 }
97
98 void
99 remove_thread (struct thread_info *thread)
100 {
101 if (thread->btrace != NULL)
102 target_disable_btrace (thread->btrace);
103
104 discard_queued_stop_replies (ptid_of (thread));
105 all_threads.remove (thread);
106 if (current_thread == thread)
107 switch_to_thread (nullptr);
108 free_one_thread (thread);
109 }
110
111 void *
112 thread_target_data (struct thread_info *thread)
113 {
114 return thread->target_data;
115 }
116
117 struct regcache *
118 thread_regcache_data (struct thread_info *thread)
119 {
120 return thread->regcache_data;
121 }
122
123 void
124 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
125 {
126 thread->regcache_data = data;
127 }
128
129 void
130 clear_inferiors (void)
131 {
132 for_each_thread (free_one_thread);
133 all_threads.clear ();
134
135 clear_dlls ();
136
137 switch_to_thread (nullptr);
138 current_process_ = nullptr;
139 }
140
141 struct process_info *
142 add_process (int pid, int attached)
143 {
144 process_info *process = new process_info (pid, attached);
145
146 all_processes.push_back (process);
147
148 return process;
149 }
150
151 /* Remove a process from the common process list and free the memory
152 allocated for it.
153 The caller is responsible for freeing private data first. */
154
155 void
156 remove_process (struct process_info *process)
157 {
158 clear_symbol_cache (&process->symbol_cache);
159 free_all_breakpoints (process);
160 gdb_assert (find_thread_process (process) == NULL);
161 all_processes.remove (process);
162 if (current_process () == process)
163 switch_to_process (nullptr);
164 delete process;
165 }
166
167 process_info *
168 find_process_pid (int pid)
169 {
170 return find_process ([&] (process_info *process) {
171 return process->pid == pid;
172 });
173 }
174
175 /* Get the first process in the process list, or NULL if the list is empty. */
176
177 process_info *
178 get_first_process (void)
179 {
180 if (!all_processes.empty ())
181 return all_processes.front ();
182 else
183 return NULL;
184 }
185
186 /* Return non-zero if there are any inferiors that we have created
187 (as opposed to attached-to). */
188
189 int
190 have_started_inferiors_p (void)
191 {
192 return find_process ([] (process_info *process) {
193 return !process->attached;
194 }) != NULL;
195 }
196
197 /* Return non-zero if there are any inferiors that we have attached to. */
198
199 int
200 have_attached_inferiors_p (void)
201 {
202 return find_process ([] (process_info *process) {
203 return process->attached;
204 }) != NULL;
205 }
206
207 struct process_info *
208 get_thread_process (const struct thread_info *thread)
209 {
210 return find_process_pid (thread->id.pid ());
211 }
212
213 struct process_info *
214 current_process (void)
215 {
216 return current_process_;
217 }
218
219 /* See gdbsupport/common-gdbthread.h. */
220
221 void
222 switch_to_thread (process_stratum_target *ops, ptid_t ptid)
223 {
224 gdb_assert (ptid != minus_one_ptid);
225 switch_to_thread (find_thread_ptid (ptid));
226 }
227
228 /* See gdbthread.h. */
229
230 void
231 switch_to_thread (thread_info *thread)
232 {
233 if (thread != nullptr)
234 current_process_ = get_thread_process (thread);
235 else
236 current_process_ = nullptr;
237 current_thread = thread;
238 }
239
240 /* See inferiors.h. */
241
242 void
243 switch_to_process (process_info *proc)
244 {
245 current_process_ = proc;
246 current_thread = nullptr;
247 }
248
249 /* See gdbsupport/common-inferior.h. */
250
251 const std::string &
252 get_inferior_cwd ()
253 {
254 return current_inferior_cwd;
255 }
256
257 /* See inferiors.h. */
258
259 void
260 set_inferior_cwd (std::string cwd)
261 {
262 current_inferior_cwd = std::move (cwd);
263 }
264
265 scoped_restore_current_thread::scoped_restore_current_thread ()
266 {
267 m_process = current_process_;
268 m_thread = current_thread;
269 }
270
271 scoped_restore_current_thread::~scoped_restore_current_thread ()
272 {
273 if (m_dont_restore)
274 return;
275
276 if (m_thread != nullptr)
277 switch_to_thread (m_thread);
278 else
279 switch_to_process (m_process);
280 }