RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdbserver / inferiors.cc
blobe72b1c9bfc25220a7e159a3bcb6031fcbf14ed3e
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdbsupport/common-gdbthread.h"
22 #include "gdbsupport/common-inferior.h"
23 #include "gdbsupport/owning_intrusive_list.h"
24 #include "gdbthread.h"
25 #include "dll.h"
27 owning_intrusive_list<process_info> all_processes;
29 /* The current process. */
30 static process_info *current_process_;
32 /* The current thread. This is either a thread of CURRENT_PROCESS, or
33 NULL. */
34 struct thread_info *current_thread;
36 /* The current working directory used to start the inferior.
38 Empty if not specified. */
39 static std::string current_inferior_cwd;
41 thread_info *
42 process_info::add_thread (ptid_t id, void *target_data)
44 auto &new_thread = m_thread_list.emplace_back (id, this, target_data);
45 bool inserted = m_ptid_thread_map.insert ({ id, &new_thread }).second;
47 /* A thread with this ptid should not exist in the map yet. */
48 gdb_assert (inserted);
50 if (current_thread == NULL)
51 switch_to_thread (&new_thread);
53 return &new_thread;
56 /* See gdbthread.h. */
58 struct thread_info *
59 get_first_thread (void)
61 return find_thread ([] (thread_info *thread)
63 return true;
64 });
67 struct thread_info *
68 find_thread_ptid (ptid_t ptid)
70 process_info *process = find_process_pid (ptid.pid ());
71 if (process == nullptr)
72 return nullptr;
74 auto &thread_map = process->thread_map ();
76 if (auto it = thread_map.find (ptid);
77 it != thread_map.end ())
78 return it->second;
80 return nullptr;
83 /* Find a thread associated with the given PROCESS, or NULL if no
84 such thread exists. */
86 static struct thread_info *
87 find_thread_process (const struct process_info *const process)
89 return find_any_thread_of_pid (process->pid);
92 /* See gdbthread.h. */
94 struct thread_info *
95 find_any_thread_of_pid (int pid)
97 return find_thread (pid, [] (thread_info *thread) {
98 return true;
99 });
102 void
103 process_info::remove_thread (thread_info *thread)
105 if (thread->btrace != NULL)
106 target_disable_btrace (thread->btrace);
108 discard_queued_stop_replies (thread->id);
110 if (current_thread == thread)
111 switch_to_thread (nullptr);
113 /* We should not try to remove a thread that was not added. */
114 gdb_assert (thread->process () == this);
115 int num_erased = m_ptid_thread_map.erase (thread->id);
116 gdb_assert (num_erased > 0);
118 m_thread_list.erase (m_thread_list.iterator_to (*thread));
121 void *
122 thread_target_data (struct thread_info *thread)
124 return thread->target_data;
127 struct regcache *
128 thread_regcache_data (struct thread_info *thread)
130 return thread->regcache_data;
133 void
134 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
136 thread->regcache_data = data;
139 void
140 clear_inferiors (void)
142 for_each_process ([] (process_info *process)
144 process->thread_list ().clear ();
145 process->thread_map ().clear ();
148 clear_dlls ();
150 switch_to_thread (nullptr);
151 current_process_ = nullptr;
154 struct process_info *
155 add_process (int pid, int attached)
157 return &all_processes.emplace_back (pid, attached);
160 /* Remove a process from the common process list and free the memory
161 allocated for it.
162 The caller is responsible for freeing private data first. */
164 void
165 remove_process (struct process_info *process)
167 clear_symbol_cache (&process->symbol_cache);
168 free_all_breakpoints (process);
169 gdb_assert (find_thread_process (process) == NULL);
170 if (current_process () == process)
171 switch_to_process (nullptr);
173 all_processes.erase (all_processes.iterator_to (*process));
176 process_info *
177 find_process_pid (int pid)
179 return find_process ([&] (process_info *process) {
180 return process->pid == pid;
184 /* Get the first process in the process list, or NULL if the list is empty. */
186 process_info *
187 get_first_process (void)
189 if (!all_processes.empty ())
190 return &all_processes.front ();
191 else
192 return NULL;
195 /* Return non-zero if there are any inferiors that we have created
196 (as opposed to attached-to). */
199 have_started_inferiors_p (void)
201 return find_process ([] (process_info *process) {
202 return !process->attached;
203 }) != NULL;
206 /* Return non-zero if there are any inferiors that we have attached to. */
209 have_attached_inferiors_p (void)
211 return find_process ([] (process_info *process) {
212 return process->attached;
213 }) != NULL;
216 struct process_info *
217 current_process (void)
219 return current_process_;
222 /* See inferiors.h. */
224 void
225 for_each_process (gdb::function_view<void (process_info *)> func)
227 owning_intrusive_list<process_info>::iterator next, cur
228 = all_processes.begin ();
230 while (cur != all_processes.end ())
232 next = cur;
233 next++;
234 func (&*cur);
235 cur = next;
239 /* See inferiors.h. */
241 process_info *
242 find_process (gdb::function_view<bool (process_info *)> func)
244 for (process_info &process : all_processes)
245 if (func (&process))
246 return &process;
248 return NULL;
251 /* See inferiors.h. */
253 thread_info *
254 process_info::find_thread (gdb::function_view<bool (thread_info *)> func)
256 for (thread_info &thread : m_thread_list)
257 if (func (&thread))
258 return &thread;
260 return nullptr;
263 /* See gdbthread.h. */
265 thread_info *
266 find_thread (gdb::function_view<bool (thread_info *)> func)
268 for (process_info &process : all_processes)
269 if (thread_info *thread = process.find_thread (func);
270 thread != nullptr)
271 return thread;
273 return NULL;
276 /* See gdbthread.h. */
278 thread_info *
279 find_thread (int pid, gdb::function_view<bool (thread_info *)> func)
281 process_info *process = find_process_pid (pid);
282 if (process == nullptr)
283 return nullptr;
285 return process->find_thread (func);
288 /* See gdbthread.h. */
290 thread_info *
291 find_thread (ptid_t filter, gdb::function_view<bool (thread_info *)> func)
293 if (filter == minus_one_ptid)
294 return find_thread (func);
296 process_info *process = find_process_pid (filter.pid ());
297 if (process == nullptr)
298 return nullptr;
300 if (filter.is_pid ())
301 return process->find_thread (func);
303 auto &thread_map = process->thread_map ();
305 if (auto it = thread_map.find (filter);
306 it != thread_map.end () && func (it->second))
307 return it->second;
309 return nullptr;
312 /* See gdbthread.h. */
314 void
315 for_each_thread (gdb::function_view<void (thread_info *)> func)
317 for_each_process ([&] (process_info *proc)
319 proc->for_each_thread (func);
323 /* See inferiors.h. */
325 void
326 process_info::for_each_thread (gdb::function_view<void (thread_info *)> func)
328 owning_intrusive_list<thread_info>::iterator next, cur
329 = m_thread_list.begin ();
331 while (cur != m_thread_list.end ())
333 /* FUNC may alter the current iterator. */
334 next = cur;
335 next++;
336 func (&*cur);
337 cur = next;
341 /* See gdbthread.h. */
343 void
344 for_each_thread (ptid_t ptid, gdb::function_view<void (thread_info *)> func)
346 if (ptid == minus_one_ptid)
347 for_each_thread (func);
348 else if (ptid.is_pid ())
350 process_info *process = find_process_pid (ptid.pid ());
352 if (process != nullptr)
353 process->for_each_thread (func);
355 else
356 find_thread (ptid, [func] (thread_info *thread)
358 func (thread);
359 return false;
363 /* See gdbthread.h. */
365 thread_info *
366 find_thread_in_random (ptid_t ptid,
367 gdb::function_view<bool (thread_info *)> func)
369 int count = 0;
370 int random_selector;
372 /* First count how many interesting entries we have. */
373 for_each_thread (ptid, [&] (thread_info *thread)
375 if (func (thread))
376 count++;
379 if (count == 0)
380 return nullptr;
382 /* Now randomly pick an entry out of those. */
383 random_selector = (int)
384 ((count * (double) rand ()) / (RAND_MAX + 1.0));
386 thread_info *thread = find_thread (ptid, [&] (thread_info *thr_arg)
388 return func (thr_arg) && (random_selector-- == 0);
391 gdb_assert (thread != NULL);
393 return thread;
396 /* See gdbthread.h. */
398 thread_info *
399 find_thread_in_random (gdb::function_view<bool (thread_info *)> func)
401 return find_thread_in_random (minus_one_ptid, func);
404 /* See gdbsupport/common-gdbthread.h. */
406 void
407 switch_to_thread (process_stratum_target *ops, ptid_t ptid)
409 gdb_assert (ptid != minus_one_ptid);
410 switch_to_thread (find_thread_ptid (ptid));
413 /* See gdbthread.h. */
415 void
416 switch_to_thread (thread_info *thread)
418 if (thread != nullptr)
419 current_process_ = thread->process ();
420 else
421 current_process_ = nullptr;
422 current_thread = thread;
425 /* See inferiors.h. */
427 void
428 switch_to_process (process_info *proc)
430 current_process_ = proc;
431 current_thread = nullptr;
434 /* See gdbsupport/common-inferior.h. */
436 const std::string &
437 get_inferior_cwd ()
439 return current_inferior_cwd;
442 /* See inferiors.h. */
444 void
445 set_inferior_cwd (std::string cwd)
447 current_inferior_cwd = std::move (cwd);
450 scoped_restore_current_thread::scoped_restore_current_thread ()
452 m_process = current_process_;
453 m_thread = current_thread;
456 scoped_restore_current_thread::~scoped_restore_current_thread ()
458 if (m_dont_restore)
459 return;
461 if (m_thread != nullptr)
462 switch_to_thread (m_thread);
463 else
464 switch_to_process (m_process);