1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007-2012 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/>. */
25 struct inferior_list all_processes;
26 struct inferior_list all_threads;
28 struct thread_info *current_inferior;
30 #define get_thread(inf) ((struct thread_info *)(inf))
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;
40 list->head = new_inferior;
41 list->tail = new_inferior;
44 /* Invoke ACTION for each inferior in LIST. */
47 for_each_inferior (struct inferior_list *list,
48 void (*action) (struct inferior_list_entry *))
50 struct inferior_list_entry *cur = list->head, *next;
61 remove_inferior (struct inferior_list *list,
62 struct inferior_list_entry *entry)
64 struct inferior_list_entry **cur;
66 if (list->head == entry)
68 list->head = entry->next;
69 if (list->tail == entry)
70 list->tail = list->head;
75 while (*cur && (*cur)->next != entry)
81 (*cur)->next = entry->next;
83 if (list->tail == entry)
88 add_thread (ptid_t thread_id, void *target_data)
90 struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
92 memset (new_thread, 0, sizeof (*new_thread));
94 new_thread->entry.id = thread_id;
95 new_thread->last_resume_kind = resume_continue;
96 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
98 add_inferior_to_list (&all_threads, & new_thread->entry);
100 if (current_inferior == NULL)
101 current_inferior = new_thread;
103 new_thread->target_data = target_data;
104 set_inferior_regcache_data (new_thread, new_register_cache ());
108 thread_id_to_gdb_id (ptid_t thread_id)
110 struct inferior_list_entry *inf = all_threads.head;
114 if (ptid_equal (inf->id, thread_id))
123 thread_to_gdb_id (struct thread_info *thread)
125 return thread->entry.id;
129 find_thread_ptid (ptid_t ptid)
131 struct inferior_list_entry *inf = all_threads.head;
135 struct thread_info *thread = get_thread (inf);
136 if (ptid_equal (thread->entry.id, ptid))
145 gdb_id_to_thread_id (ptid_t gdb_id)
147 struct thread_info *thread = find_thread_ptid (gdb_id);
149 return thread ? thread->entry.id : null_ptid;
153 free_one_thread (struct inferior_list_entry *inf)
155 struct thread_info *thread = get_thread (inf);
156 free_register_cache (inferior_regcache_data (thread));
161 remove_thread (struct thread_info *thread)
163 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
164 free_one_thread (&thread->entry);
167 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
168 returns non-zero. If no entry is found then return NULL. */
170 struct inferior_list_entry *
171 find_inferior (struct inferior_list *list,
172 int (*func) (struct inferior_list_entry *, void *), void *arg)
174 struct inferior_list_entry *inf = list->head;
178 struct inferior_list_entry *next;
181 if ((*func) (inf, arg))
189 struct inferior_list_entry *
190 find_inferior_id (struct inferior_list *list, ptid_t id)
192 struct inferior_list_entry *inf = list->head;
196 if (ptid_equal (inf->id, id))
205 inferior_target_data (struct thread_info *inferior)
207 return inferior->target_data;
211 set_inferior_target_data (struct thread_info *inferior, void *data)
213 inferior->target_data = data;
217 inferior_regcache_data (struct thread_info *inferior)
219 return inferior->regcache_data;
223 set_inferior_regcache_data (struct thread_info *inferior, void *data)
225 inferior->regcache_data = data;
228 #define clear_list(LIST) \
229 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
232 clear_inferiors (void)
234 for_each_inferior (&all_threads, free_one_thread);
235 clear_list (&all_threads);
239 current_inferior = NULL;
242 /* Two utility functions for a truly degenerate inferior_list: a simple
246 add_pid_to_list (struct inferior_list *list, unsigned long pid)
248 struct inferior_list_entry *new_entry;
250 new_entry = xmalloc (sizeof (struct inferior_list_entry));
251 new_entry->id = pid_to_ptid (pid);
252 add_inferior_to_list (list, new_entry);
256 pull_pid_from_list (struct inferior_list *list, unsigned long pid)
258 struct inferior_list_entry *new_entry;
260 new_entry = find_inferior_id (list, pid_to_ptid (pid));
261 if (new_entry == NULL)
265 remove_inferior (list, new_entry);
271 struct process_info *
272 add_process (int pid, int attached)
274 struct process_info *process;
276 process = xcalloc (1, sizeof (*process));
278 process->head.id = pid_to_ptid (pid);
279 process->attached = attached;
281 add_inferior_to_list (&all_processes, &process->head);
286 /* Remove a process from the common process list and free the memory
288 The caller is responsible for freeing private data first. */
291 remove_process (struct process_info *process)
293 clear_symbol_cache (&process->symbol_cache);
294 free_all_breakpoints (process);
295 remove_inferior (&all_processes, &process->head);
299 struct process_info *
300 find_process_pid (int pid)
302 return (struct process_info *)
303 find_inferior_id (&all_processes, pid_to_ptid (pid));
306 /* Return non-zero if INF, a struct process_info, was started by us,
307 i.e. not attached to. */
310 started_inferior_callback (struct inferior_list_entry *entry, void *args)
312 struct process_info *process = (struct process_info *) entry;
314 return ! process->attached;
317 /* Return non-zero if there are any inferiors that we have created
318 (as opposed to attached-to). */
321 have_started_inferiors_p (void)
323 return (find_inferior (&all_processes, started_inferior_callback, NULL)
327 /* Return non-zero if INF, a struct process_info, was attached to. */
330 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
332 struct process_info *process = (struct process_info *) entry;
334 return process->attached;
337 /* Return non-zero if there are any inferiors that we have attached to. */
340 have_attached_inferiors_p (void)
342 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
346 struct process_info *
347 get_thread_process (struct thread_info *thread)
349 int pid = ptid_get_pid (thread->entry.id);
350 return find_process_pid (pid);
353 struct process_info *
354 current_process (void)
356 if (current_inferior == NULL)
357 fatal ("Current inferior requested, but current_inferior is NULL\n");
359 return get_thread_process (current_inferior);