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