gdbserver: Remove thread_to_gdb_id
[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 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 /* Wrapper around get_first_inferior to return a struct thread_info *. */
125
126 struct thread_info *
127 get_first_thread (void)
128 {
129 return (struct thread_info *) get_first_inferior (&all_threads);
130 }
131
132 struct thread_info *
133 find_thread_ptid (ptid_t ptid)
134 {
135 return (struct thread_info *) find_inferior_id (&all_threads, ptid);
136 }
137
138 /* Find a thread associated with the given PROCESS, or NULL if no
139 such thread exists. */
140
141 static struct thread_info *
142 find_thread_process (const struct process_info *const process)
143 {
144 return find_any_thread_of_pid (process->entry.id.pid ());
145 }
146
147 /* Helper for find_any_thread_of_pid. Returns true if a thread
148 matches a PID. */
149
150 static int
151 thread_of_pid (struct inferior_list_entry *entry, void *pid_p)
152 {
153 int pid = *(int *) pid_p;
154
155 return (ptid_get_pid (entry->id) == pid);
156 }
157
158 /* See gdbthread.h. */
159
160 struct thread_info *
161 find_any_thread_of_pid (int pid)
162 {
163 struct inferior_list_entry *entry;
164
165 entry = find_inferior (&all_threads, thread_of_pid, &pid);
166
167 return (struct thread_info *) entry;
168 }
169
170 static void
171 free_one_thread (struct inferior_list_entry *inf)
172 {
173 struct thread_info *thread = get_thread (inf);
174 free_register_cache (thread_regcache_data (thread));
175 free (thread);
176 }
177
178 void
179 remove_thread (struct thread_info *thread)
180 {
181 if (thread->btrace != NULL)
182 target_disable_btrace (thread->btrace);
183
184 discard_queued_stop_replies (ptid_of (thread));
185 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
186 free_one_thread (&thread->entry);
187 if (current_thread == thread)
188 current_thread = NULL;
189 }
190
191 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
192 This is for cases where the caller needs a thread, but doesn't care
193 which one. */
194
195 struct inferior_list_entry *
196 get_first_inferior (struct inferior_list *list)
197 {
198 if (list->head != NULL)
199 return list->head;
200 return NULL;
201 }
202
203 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
204 returns non-zero. If no entry is found then return NULL. */
205
206 struct inferior_list_entry *
207 find_inferior (struct inferior_list *list,
208 int (*func) (struct inferior_list_entry *, void *), void *arg)
209 {
210 struct inferior_list_entry *inf = list->head;
211
212 while (inf != NULL)
213 {
214 struct inferior_list_entry *next;
215
216 next = inf->next;
217 if ((*func) (inf, arg))
218 return inf;
219 inf = next;
220 }
221
222 return NULL;
223 }
224
225 /* Find the random inferior_list_entry E in LIST for which FUNC (E, ARG)
226 returns non-zero. If no entry is found then return NULL. */
227
228 struct inferior_list_entry *
229 find_inferior_in_random (struct inferior_list *list,
230 int (*func) (struct inferior_list_entry *, void *),
231 void *arg)
232 {
233 struct inferior_list_entry *inf = list->head;
234 int count = 0;
235 int random_selector;
236
237 /* First count how many interesting entries we have. */
238 while (inf != NULL)
239 {
240 struct inferior_list_entry *next;
241
242 next = inf->next;
243 if ((*func) (inf, arg))
244 count++;
245 inf = next;
246 }
247
248 if (count == 0)
249 return NULL;
250
251 /* Now randomly pick an entry out of those. */
252 random_selector = (int)
253 ((count * (double) rand ()) / (RAND_MAX + 1.0));
254
255 inf = list->head;
256 while (inf != NULL)
257 {
258 struct inferior_list_entry *next;
259
260 next = inf->next;
261 if ((*func) (inf, arg) && (random_selector-- == 0))
262 return inf;
263 inf = next;
264 }
265
266 gdb_assert_not_reached ("failed to find an inferior in random.");
267 return NULL;
268 }
269
270 struct inferior_list_entry *
271 find_inferior_id (struct inferior_list *list, ptid_t id)
272 {
273 struct inferior_list_entry *inf = list->head;
274
275 while (inf != NULL)
276 {
277 if (ptid_equal (inf->id, id))
278 return inf;
279 inf = inf->next;
280 }
281
282 return NULL;
283 }
284
285 void *
286 thread_target_data (struct thread_info *thread)
287 {
288 return thread->target_data;
289 }
290
291 struct regcache *
292 thread_regcache_data (struct thread_info *thread)
293 {
294 return thread->regcache_data;
295 }
296
297 void
298 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
299 {
300 thread->regcache_data = data;
301 }
302
303 /* Return true if LIST has exactly one entry. */
304
305 int
306 one_inferior_p (struct inferior_list *list)
307 {
308 return list->head != NULL && list->head == list->tail;
309 }
310
311 /* Reset head,tail of LIST, assuming all entries have already been freed. */
312
313 void
314 clear_inferior_list (struct inferior_list *list)
315 {
316 list->head = NULL;
317 list->tail = NULL;
318 }
319
320 void
321 clear_inferiors (void)
322 {
323 for_each_inferior (&all_threads, free_one_thread);
324 clear_inferior_list (&all_threads);
325
326 clear_dlls ();
327
328 current_thread = NULL;
329 }
330
331 struct process_info *
332 add_process (int pid, int attached)
333 {
334 struct process_info *process = XCNEW (struct process_info);
335
336 process->entry.id = pid_to_ptid (pid);
337 process->attached = attached;
338
339 add_inferior_to_list (&all_processes, &process->entry);
340
341 return process;
342 }
343
344 /* Remove a process from the common process list and free the memory
345 allocated for it.
346 The caller is responsible for freeing private data first. */
347
348 void
349 remove_process (struct process_info *process)
350 {
351 clear_symbol_cache (&process->symbol_cache);
352 free_all_breakpoints (process);
353 gdb_assert (find_thread_process (process) == NULL);
354 remove_inferior (&all_processes, &process->entry);
355 VEC_free (int, process->syscalls_to_catch);
356 free (process);
357 }
358
359 struct process_info *
360 find_process_pid (int pid)
361 {
362 return (struct process_info *)
363 find_inferior_id (&all_processes, pid_to_ptid (pid));
364 }
365
366 /* Wrapper around get_first_inferior to return a struct process_info *. */
367
368 struct process_info *
369 get_first_process (void)
370 {
371 return (struct process_info *) get_first_inferior (&all_processes);
372 }
373
374 /* Return non-zero if INF, a struct process_info, was started by us,
375 i.e. not attached to. */
376
377 static int
378 started_inferior_callback (struct inferior_list_entry *entry, void *args)
379 {
380 struct process_info *process = (struct process_info *) entry;
381
382 return ! process->attached;
383 }
384
385 /* Return non-zero if there are any inferiors that we have created
386 (as opposed to attached-to). */
387
388 int
389 have_started_inferiors_p (void)
390 {
391 return (find_inferior (&all_processes, started_inferior_callback, NULL)
392 != NULL);
393 }
394
395 /* Return non-zero if INF, a struct process_info, was attached to. */
396
397 static int
398 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
399 {
400 struct process_info *process = (struct process_info *) entry;
401
402 return process->attached;
403 }
404
405 /* Return non-zero if there are any inferiors that we have attached to. */
406
407 int
408 have_attached_inferiors_p (void)
409 {
410 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
411 != NULL);
412 }
413
414 struct process_info *
415 get_thread_process (const struct thread_info *thread)
416 {
417 int pid = ptid_get_pid (thread->entry.id);
418 return find_process_pid (pid);
419 }
420
421 struct process_info *
422 current_process (void)
423 {
424 gdb_assert (current_thread != NULL);
425 return get_thread_process (current_thread);
426 }
427
428 static void
429 do_restore_current_thread_cleanup (void *arg)
430 {
431 current_thread = (struct thread_info *) arg;
432 }
433
434 struct cleanup *
435 make_cleanup_restore_current_thread (void)
436 {
437 return make_cleanup (do_restore_current_thread_cleanup, current_thread);
438 }
439
440 /* See common/common-gdbthread.h. */
441
442 void
443 switch_to_thread (ptid_t ptid)
444 {
445 if (!ptid_equal (ptid, minus_one_ptid))
446 current_thread = find_thread_ptid (ptid);
447 }
This page took 0.039011 seconds and 5 git commands to generate.