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