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