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 struct process_info
*
243 add_process (int pid
, int attached
)
245 struct process_info
*process
;
247 process
= xcalloc (1, sizeof (*process
));
249 process
->head
.id
= pid_to_ptid (pid
);
250 process
->attached
= attached
;
252 add_inferior_to_list (&all_processes
, &process
->head
);
257 /* Remove a process from the common process list and free the memory
259 The caller is responsible for freeing private data first. */
262 remove_process (struct process_info
*process
)
264 clear_symbol_cache (&process
->symbol_cache
);
265 free_all_breakpoints (process
);
266 remove_inferior (&all_processes
, &process
->head
);
270 struct process_info
*
271 find_process_pid (int pid
)
273 return (struct process_info
*)
274 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
277 /* Return non-zero if INF, a struct process_info, was started by us,
278 i.e. not attached to. */
281 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
283 struct process_info
*process
= (struct process_info
*) entry
;
285 return ! process
->attached
;
288 /* Return non-zero if there are any inferiors that we have created
289 (as opposed to attached-to). */
292 have_started_inferiors_p (void)
294 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
298 /* Return non-zero if INF, a struct process_info, was attached to. */
301 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
303 struct process_info
*process
= (struct process_info
*) entry
;
305 return process
->attached
;
308 /* Return non-zero if there are any inferiors that we have attached to. */
311 have_attached_inferiors_p (void)
313 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
317 struct process_info
*
318 get_thread_process (struct thread_info
*thread
)
320 int pid
= ptid_get_pid (thread
->entry
.id
);
321 return find_process_pid (pid
);
324 struct process_info
*
325 current_process (void)
327 if (current_inferior
== NULL
)
328 fatal ("Current inferior requested, but current_inferior is NULL\n");
330 return get_thread_process (current_inferior
);