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