gdb: Make GMP a required dependency for building GDB
[binutils-gdb.git] / gdbserver / inferiors.cc
blob9a1280d039b42f847fe51bd87a3e77345fad7222
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2020 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 "server.h"
22 #include "gdbsupport/common-inferior.h"
23 #include "gdbthread.h"
24 #include "dll.h"
26 std::list<process_info *> all_processes;
27 std::list<thread_info *> all_threads;
29 struct thread_info *current_thread;
31 /* The current working directory used to start the inferior. */
32 static const char *current_inferior_cwd = NULL;
34 struct thread_info *
35 add_thread (ptid_t thread_id, void *target_data)
37 struct thread_info *new_thread = XCNEW (struct thread_info);
39 new_thread->id = thread_id;
40 new_thread->last_resume_kind = resume_continue;
41 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
43 all_threads.push_back (new_thread);
45 if (current_thread == NULL)
46 current_thread = new_thread;
48 new_thread->target_data = target_data;
50 return new_thread;
53 /* See gdbthread.h. */
55 struct thread_info *
56 get_first_thread (void)
58 if (!all_threads.empty ())
59 return all_threads.front ();
60 else
61 return NULL;
64 struct thread_info *
65 find_thread_ptid (ptid_t ptid)
67 return find_thread ([&] (thread_info *thread) {
68 return thread->id == ptid;
69 });
72 /* Find a thread associated with the given PROCESS, or NULL if no
73 such thread exists. */
75 static struct thread_info *
76 find_thread_process (const struct process_info *const process)
78 return find_any_thread_of_pid (process->pid);
81 /* See gdbthread.h. */
83 struct thread_info *
84 find_any_thread_of_pid (int pid)
86 return find_thread (pid, [] (thread_info *thread) {
87 return true;
88 });
91 static void
92 free_one_thread (thread_info *thread)
94 free_register_cache (thread_regcache_data (thread));
95 free (thread);
98 void
99 remove_thread (struct thread_info *thread)
101 if (thread->btrace != NULL)
102 target_disable_btrace (thread->btrace);
104 discard_queued_stop_replies (ptid_of (thread));
105 all_threads.remove (thread);
106 if (current_thread == thread)
107 current_thread = NULL;
108 free_one_thread (thread);
111 void *
112 thread_target_data (struct thread_info *thread)
114 return thread->target_data;
117 struct regcache *
118 thread_regcache_data (struct thread_info *thread)
120 return thread->regcache_data;
123 void
124 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
126 thread->regcache_data = data;
129 void
130 clear_inferiors (void)
132 for_each_thread (free_one_thread);
133 all_threads.clear ();
135 clear_dlls ();
137 current_thread = NULL;
140 struct process_info *
141 add_process (int pid, int attached)
143 process_info *process = new process_info (pid, attached);
145 all_processes.push_back (process);
147 return process;
150 /* Remove a process from the common process list and free the memory
151 allocated for it.
152 The caller is responsible for freeing private data first. */
154 void
155 remove_process (struct process_info *process)
157 clear_symbol_cache (&process->symbol_cache);
158 free_all_breakpoints (process);
159 gdb_assert (find_thread_process (process) == NULL);
160 all_processes.remove (process);
161 delete process;
164 process_info *
165 find_process_pid (int pid)
167 return find_process ([&] (process_info *process) {
168 return process->pid == pid;
172 /* Get the first process in the process list, or NULL if the list is empty. */
174 process_info *
175 get_first_process (void)
177 if (!all_processes.empty ())
178 return all_processes.front ();
179 else
180 return NULL;
183 /* Return non-zero if there are any inferiors that we have created
184 (as opposed to attached-to). */
187 have_started_inferiors_p (void)
189 return find_process ([] (process_info *process) {
190 return !process->attached;
191 }) != NULL;
194 /* Return non-zero if there are any inferiors that we have attached to. */
197 have_attached_inferiors_p (void)
199 return find_process ([] (process_info *process) {
200 return process->attached;
201 }) != NULL;
204 struct process_info *
205 get_thread_process (const struct thread_info *thread)
207 return find_process_pid (thread->id.pid ());
210 struct process_info *
211 current_process (void)
213 gdb_assert (current_thread != NULL);
214 return get_thread_process (current_thread);
217 /* See gdbsupport/common-gdbthread.h. */
219 void
220 switch_to_thread (process_stratum_target *ops, ptid_t ptid)
222 gdb_assert (ptid != minus_one_ptid);
223 current_thread = find_thread_ptid (ptid);
226 /* See inferiors.h. */
228 void
229 switch_to_process (process_info *proc)
231 int pid = pid_of (proc);
233 current_thread = find_any_thread_of_pid (pid);
236 /* See gdbsupport/common-inferior.h. */
238 const char *
239 get_inferior_cwd ()
241 return current_inferior_cwd;
244 /* See gdbsupport/common-inferior.h. */
246 void
247 set_inferior_cwd (const char *cwd)
249 xfree ((void *) current_inferior_cwd);
250 if (cwd != NULL)
251 current_inferior_cwd = xstrdup (cwd);
252 else
253 current_inferior_cwd = NULL;