disasm: split dump_insns
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2015 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 struct inferior_list all_processes;
26 struct inferior_list all_threads;
27
28 struct thread_info *current_thread;
29
30 #define get_thread(inf) ((struct thread_info *)(inf))
31
32 void
33 add_inferior_to_list (struct inferior_list *list,
34 struct inferior_list_entry *new_inferior)
35 {
36 new_inferior->next = NULL;
37 if (list->tail != NULL)
38 list->tail->next = new_inferior;
39 else
40 list->head = new_inferior;
41 list->tail = new_inferior;
42 }
43
44 /* Invoke ACTION for each inferior in LIST. */
45
46 void
47 for_each_inferior (struct inferior_list *list,
48 void (*action) (struct inferior_list_entry *))
49 {
50 struct inferior_list_entry *cur = list->head, *next;
51
52 while (cur != NULL)
53 {
54 next = cur->next;
55 (*action) (cur);
56 cur = next;
57 }
58 }
59
60 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
61
62 void
63 for_each_inferior_with_data (struct inferior_list *list,
64 void (*action) (struct inferior_list_entry *,
65 void *),
66 void *data)
67 {
68 struct inferior_list_entry *cur = list->head, *next;
69
70 while (cur != NULL)
71 {
72 next = cur->next;
73 (*action) (cur, data);
74 cur = next;
75 }
76 }
77
78 void
79 remove_inferior (struct inferior_list *list,
80 struct inferior_list_entry *entry)
81 {
82 struct inferior_list_entry **cur;
83
84 if (list->head == entry)
85 {
86 list->head = entry->next;
87 if (list->tail == entry)
88 list->tail = list->head;
89 return;
90 }
91
92 cur = &list->head;
93 while (*cur && (*cur)->next != entry)
94 cur = &(*cur)->next;
95
96 if (*cur == NULL)
97 return;
98
99 (*cur)->next = entry->next;
100
101 if (list->tail == entry)
102 list->tail = *cur;
103 }
104
105 struct thread_info *
106 add_thread (ptid_t thread_id, void *target_data)
107 {
108 struct thread_info *new_thread = XCNEW (struct thread_info);
109
110 new_thread->entry.id = thread_id;
111 new_thread->last_resume_kind = resume_continue;
112 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
113
114 add_inferior_to_list (&all_threads, &new_thread->entry);
115
116 if (current_thread == NULL)
117 current_thread = new_thread;
118
119 new_thread->target_data = target_data;
120
121 return new_thread;
122 }
123
124 ptid_t
125 thread_to_gdb_id (struct thread_info *thread)
126 {
127 return thread->entry.id;
128 }
129
130 /* Wrapper around get_first_inferior to return a struct thread_info *. */
131
132 struct thread_info *
133 get_first_thread (void)
134 {
135 return (struct thread_info *) get_first_inferior (&all_threads);
136 }
137
138 struct thread_info *
139 find_thread_ptid (ptid_t ptid)
140 {
141 return (struct thread_info *) find_inferior_id (&all_threads, ptid);
142 }
143
144 /* Predicate function for matching thread entry's pid to the given
145 pid value passed by address in ARGS. */
146
147 static int
148 thread_pid_matches_callback (struct inferior_list_entry *entry, void *args)
149 {
150 return (ptid_get_pid (entry->id) == *(pid_t *)args);
151 }
152
153 /* Find a thread associated with the given PROCESS, or NULL if no
154 such thread exists. */
155
156 static struct thread_info *
157 find_thread_process (const struct process_info *const process)
158 {
159 pid_t pid = ptid_get_pid (ptid_of (process));
160
161 return (struct thread_info *)
162 find_inferior (&all_threads, thread_pid_matches_callback, &pid);
163 }
164
165 ptid_t
166 gdb_id_to_thread_id (ptid_t gdb_id)
167 {
168 struct thread_info *thread = find_thread_ptid (gdb_id);
169
170 return thread ? thread->entry.id : null_ptid;
171 }
172
173 static void
174 free_one_thread (struct inferior_list_entry *inf)
175 {
176 struct thread_info *thread = get_thread (inf);
177 free_register_cache (inferior_regcache_data (thread));
178 free (thread);
179 }
180
181 void
182 remove_thread (struct thread_info *thread)
183 {
184 if (thread->btrace != NULL)
185 target_disable_btrace (thread->btrace);
186
187 discard_queued_stop_replies (ptid_of (thread));
188 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
189 free_one_thread (&thread->entry);
190 if (current_thread == thread)
191 current_thread = NULL;
192 }
193
194 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
195 This is for cases where the caller needs a thread, but doesn't care
196 which one. */
197
198 struct inferior_list_entry *
199 get_first_inferior (struct inferior_list *list)
200 {
201 if (list->head != NULL)
202 return list->head;
203 return NULL;
204 }
205
206 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
207 returns non-zero. If no entry is found then return NULL. */
208
209 struct inferior_list_entry *
210 find_inferior (struct inferior_list *list,
211 int (*func) (struct inferior_list_entry *, void *), void *arg)
212 {
213 struct inferior_list_entry *inf = list->head;
214
215 while (inf != NULL)
216 {
217 struct inferior_list_entry *next;
218
219 next = inf->next;
220 if ((*func) (inf, arg))
221 return inf;
222 inf = next;
223 }
224
225 return NULL;
226 }
227
228 struct inferior_list_entry *
229 find_inferior_id (struct inferior_list *list, ptid_t id)
230 {
231 struct inferior_list_entry *inf = list->head;
232
233 while (inf != NULL)
234 {
235 if (ptid_equal (inf->id, id))
236 return inf;
237 inf = inf->next;
238 }
239
240 return NULL;
241 }
242
243 void *
244 inferior_target_data (struct thread_info *inferior)
245 {
246 return inferior->target_data;
247 }
248
249 void
250 set_inferior_target_data (struct thread_info *inferior, void *data)
251 {
252 inferior->target_data = data;
253 }
254
255 struct regcache *
256 inferior_regcache_data (struct thread_info *inferior)
257 {
258 return inferior->regcache_data;
259 }
260
261 void
262 set_inferior_regcache_data (struct thread_info *inferior, struct regcache *data)
263 {
264 inferior->regcache_data = data;
265 }
266
267 /* Return true if LIST has exactly one entry. */
268
269 int
270 one_inferior_p (struct inferior_list *list)
271 {
272 return list->head != NULL && list->head == list->tail;
273 }
274
275 /* Reset head,tail of LIST, assuming all entries have already been freed. */
276
277 void
278 clear_inferior_list (struct inferior_list *list)
279 {
280 list->head = NULL;
281 list->tail = NULL;
282 }
283
284 void
285 clear_inferiors (void)
286 {
287 for_each_inferior (&all_threads, free_one_thread);
288 clear_inferior_list (&all_threads);
289
290 clear_dlls ();
291
292 current_thread = NULL;
293 }
294
295 struct process_info *
296 add_process (int pid, int attached)
297 {
298 struct process_info *process = XCNEW (struct process_info);
299
300 process->entry.id = pid_to_ptid (pid);
301 process->attached = attached;
302
303 add_inferior_to_list (&all_processes, &process->entry);
304
305 return process;
306 }
307
308 /* Remove a process from the common process list and free the memory
309 allocated for it.
310 The caller is responsible for freeing private data first. */
311
312 void
313 remove_process (struct process_info *process)
314 {
315 clear_symbol_cache (&process->symbol_cache);
316 free_all_breakpoints (process);
317 gdb_assert (find_thread_process (process) == NULL);
318 remove_inferior (&all_processes, &process->entry);
319 free (process);
320 }
321
322 struct process_info *
323 find_process_pid (int pid)
324 {
325 return (struct process_info *)
326 find_inferior_id (&all_processes, pid_to_ptid (pid));
327 }
328
329 /* Wrapper around get_first_inferior to return a struct process_info *. */
330
331 struct process_info *
332 get_first_process (void)
333 {
334 return (struct process_info *) get_first_inferior (&all_processes);
335 }
336
337 /* Return non-zero if INF, a struct process_info, was started by us,
338 i.e. not attached to. */
339
340 static int
341 started_inferior_callback (struct inferior_list_entry *entry, void *args)
342 {
343 struct process_info *process = (struct process_info *) entry;
344
345 return ! process->attached;
346 }
347
348 /* Return non-zero if there are any inferiors that we have created
349 (as opposed to attached-to). */
350
351 int
352 have_started_inferiors_p (void)
353 {
354 return (find_inferior (&all_processes, started_inferior_callback, NULL)
355 != NULL);
356 }
357
358 /* Return non-zero if INF, a struct process_info, was attached to. */
359
360 static int
361 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
362 {
363 struct process_info *process = (struct process_info *) entry;
364
365 return process->attached;
366 }
367
368 /* Return non-zero if there are any inferiors that we have attached to. */
369
370 int
371 have_attached_inferiors_p (void)
372 {
373 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
374 != NULL);
375 }
376
377 struct process_info *
378 get_thread_process (struct thread_info *thread)
379 {
380 int pid = ptid_get_pid (thread->entry.id);
381 return find_process_pid (pid);
382 }
383
384 struct process_info *
385 current_process (void)
386 {
387 gdb_assert (current_thread != NULL);
388 return get_thread_process (current_thread);
389 }
This page took 0.038421 seconds and 4 git commands to generate.