S390: Add target descriptions for vector register sets
[binutils-gdb.git] / gdb / gdbserver / inferiors.c
blob7c5a1e0eac56a4f26c4e139689e7bdd3d97bae5a
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "server.h"
22 #include "gdbthread.h"
23 #include "dll.h"
25 struct inferior_list all_processes;
26 struct inferior_list all_threads;
28 struct thread_info *current_thread;
30 #define get_thread(inf) ((struct thread_info *)(inf))
32 void
33 add_inferior_to_list (struct inferior_list *list,
34 struct inferior_list_entry *new_inferior)
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;
44 /* Invoke ACTION for each inferior in LIST. */
46 void
47 for_each_inferior (struct inferior_list *list,
48 void (*action) (struct inferior_list_entry *))
50 struct inferior_list_entry *cur = list->head, *next;
52 while (cur != NULL)
54 next = cur->next;
55 (*action) (cur);
56 cur = next;
60 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
62 void
63 for_each_inferior_with_data (struct inferior_list *list,
64 void (*action) (struct inferior_list_entry *,
65 void *),
66 void *data)
68 struct inferior_list_entry *cur = list->head, *next;
70 while (cur != NULL)
72 next = cur->next;
73 (*action) (cur, data);
74 cur = next;
78 void
79 remove_inferior (struct inferior_list *list,
80 struct inferior_list_entry *entry)
82 struct inferior_list_entry **cur;
84 if (list->head == entry)
86 list->head = entry->next;
87 if (list->tail == entry)
88 list->tail = list->head;
89 return;
92 cur = &list->head;
93 while (*cur && (*cur)->next != entry)
94 cur = &(*cur)->next;
96 if (*cur == NULL)
97 return;
99 (*cur)->next = entry->next;
101 if (list->tail == entry)
102 list->tail = *cur;
105 struct thread_info *
106 add_thread (ptid_t thread_id, void *target_data)
108 struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
110 memset (new_thread, 0, sizeof (*new_thread));
112 new_thread->entry.id = thread_id;
113 new_thread->last_resume_kind = resume_continue;
114 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
116 add_inferior_to_list (&all_threads, &new_thread->entry);
118 if (current_thread == NULL)
119 current_thread = new_thread;
121 new_thread->target_data = target_data;
123 return new_thread;
126 ptid_t
127 thread_to_gdb_id (struct thread_info *thread)
129 return thread->entry.id;
132 /* Wrapper around get_first_inferior to return a struct thread_info *. */
134 struct thread_info *
135 get_first_thread (void)
137 return (struct thread_info *) get_first_inferior (&all_threads);
140 struct thread_info *
141 find_thread_ptid (ptid_t ptid)
143 return (struct thread_info *) find_inferior_id (&all_threads, ptid);
146 ptid_t
147 gdb_id_to_thread_id (ptid_t gdb_id)
149 struct thread_info *thread = find_thread_ptid (gdb_id);
151 return thread ? thread->entry.id : null_ptid;
154 static void
155 free_one_thread (struct inferior_list_entry *inf)
157 struct thread_info *thread = get_thread (inf);
158 free_register_cache (inferior_regcache_data (thread));
159 free (thread);
162 void
163 remove_thread (struct thread_info *thread)
165 if (thread->btrace != NULL)
166 target_disable_btrace (thread->btrace);
168 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
169 free_one_thread (&thread->entry);
172 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
173 This is for cases where the caller needs a thread, but doesn't care
174 which one. */
176 struct inferior_list_entry *
177 get_first_inferior (struct inferior_list *list)
179 if (list->head != NULL)
180 return list->head;
181 return NULL;
184 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
185 returns non-zero. If no entry is found then return NULL. */
187 struct inferior_list_entry *
188 find_inferior (struct inferior_list *list,
189 int (*func) (struct inferior_list_entry *, void *), void *arg)
191 struct inferior_list_entry *inf = list->head;
193 while (inf != NULL)
195 struct inferior_list_entry *next;
197 next = inf->next;
198 if ((*func) (inf, arg))
199 return inf;
200 inf = next;
203 return NULL;
206 struct inferior_list_entry *
207 find_inferior_id (struct inferior_list *list, ptid_t id)
209 struct inferior_list_entry *inf = list->head;
211 while (inf != NULL)
213 if (ptid_equal (inf->id, id))
214 return inf;
215 inf = inf->next;
218 return NULL;
221 void *
222 inferior_target_data (struct thread_info *inferior)
224 return inferior->target_data;
227 void
228 set_inferior_target_data (struct thread_info *inferior, void *data)
230 inferior->target_data = data;
233 void *
234 inferior_regcache_data (struct thread_info *inferior)
236 return inferior->regcache_data;
239 void
240 set_inferior_regcache_data (struct thread_info *inferior, void *data)
242 inferior->regcache_data = data;
245 /* Return true if LIST has exactly one entry. */
248 one_inferior_p (struct inferior_list *list)
250 return list->head != NULL && list->head == list->tail;
253 /* Reset head,tail of LIST, assuming all entries have already been freed. */
255 void
256 clear_inferior_list (struct inferior_list *list)
258 list->head = NULL;
259 list->tail = NULL;
262 void
263 clear_inferiors (void)
265 for_each_inferior (&all_threads, free_one_thread);
266 clear_inferior_list (&all_threads);
268 clear_dlls ();
270 current_thread = NULL;
273 struct process_info *
274 add_process (int pid, int attached)
276 struct process_info *process;
278 process = xcalloc (1, sizeof (*process));
280 process->entry.id = pid_to_ptid (pid);
281 process->attached = attached;
283 add_inferior_to_list (&all_processes, &process->entry);
285 return process;
288 /* Remove a process from the common process list and free the memory
289 allocated for it.
290 The caller is responsible for freeing private data first. */
292 void
293 remove_process (struct process_info *process)
295 clear_symbol_cache (&process->symbol_cache);
296 free_all_breakpoints (process);
297 remove_inferior (&all_processes, &process->entry);
298 free (process);
301 struct process_info *
302 find_process_pid (int pid)
304 return (struct process_info *)
305 find_inferior_id (&all_processes, pid_to_ptid (pid));
308 /* Return non-zero if INF, a struct process_info, was started by us,
309 i.e. not attached to. */
311 static int
312 started_inferior_callback (struct inferior_list_entry *entry, void *args)
314 struct process_info *process = (struct process_info *) entry;
316 return ! process->attached;
319 /* Return non-zero if there are any inferiors that we have created
320 (as opposed to attached-to). */
323 have_started_inferiors_p (void)
325 return (find_inferior (&all_processes, started_inferior_callback, NULL)
326 != NULL);
329 /* Return non-zero if INF, a struct process_info, was attached to. */
331 static int
332 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
334 struct process_info *process = (struct process_info *) entry;
336 return process->attached;
339 /* Return non-zero if there are any inferiors that we have attached to. */
342 have_attached_inferiors_p (void)
344 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
345 != NULL);
348 struct process_info *
349 get_thread_process (struct thread_info *thread)
351 int pid = ptid_get_pid (thread->entry.id);
352 return find_process_pid (pid);
355 struct process_info *
356 current_process (void)
358 gdb_assert (current_thread != NULL);
359 return get_thread_process (current_thread);