1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2014 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/>. */
23 #include "gdbthread.h"
26 struct inferior_list all_processes
;
27 struct inferior_list all_threads
;
29 struct thread_info
*current_inferior
;
31 #define get_thread(inf) ((struct thread_info *)(inf))
34 add_inferior_to_list (struct inferior_list
*list
,
35 struct inferior_list_entry
*new_inferior
)
37 new_inferior
->next
= NULL
;
38 if (list
->tail
!= NULL
)
39 list
->tail
->next
= new_inferior
;
41 list
->head
= new_inferior
;
42 list
->tail
= new_inferior
;
45 /* Invoke ACTION for each inferior in LIST. */
48 for_each_inferior (struct inferior_list
*list
,
49 void (*action
) (struct inferior_list_entry
*))
51 struct inferior_list_entry
*cur
= list
->head
, *next
;
61 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
64 for_each_inferior_with_data (struct inferior_list
*list
,
65 void (*action
) (struct inferior_list_entry
*,
69 struct inferior_list_entry
*cur
= list
->head
, *next
;
74 (*action
) (cur
, data
);
80 remove_inferior (struct inferior_list
*list
,
81 struct inferior_list_entry
*entry
)
83 struct inferior_list_entry
**cur
;
85 if (list
->head
== entry
)
87 list
->head
= entry
->next
;
88 if (list
->tail
== entry
)
89 list
->tail
= list
->head
;
94 while (*cur
&& (*cur
)->next
!= entry
)
100 (*cur
)->next
= entry
->next
;
102 if (list
->tail
== entry
)
107 add_thread (ptid_t thread_id
, void *target_data
)
109 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
111 memset (new_thread
, 0, sizeof (*new_thread
));
113 new_thread
->entry
.id
= thread_id
;
114 new_thread
->last_resume_kind
= resume_continue
;
115 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
117 add_inferior_to_list (&all_threads
, &new_thread
->entry
);
119 if (current_inferior
== NULL
)
120 current_inferior
= new_thread
;
122 new_thread
->target_data
= target_data
;
128 thread_to_gdb_id (struct thread_info
*thread
)
130 return thread
->entry
.id
;
133 /* Wrapper around get_first_inferior to return a struct thread_info *. */
136 get_first_thread (void)
138 return (struct thread_info
*) get_first_inferior (&all_threads
);
142 find_thread_ptid (ptid_t ptid
)
144 return (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
148 gdb_id_to_thread_id (ptid_t gdb_id
)
150 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
152 return thread
? thread
->entry
.id
: null_ptid
;
156 free_one_thread (struct inferior_list_entry
*inf
)
158 struct thread_info
*thread
= get_thread (inf
);
159 free_register_cache (inferior_regcache_data (thread
));
164 remove_thread (struct thread_info
*thread
)
166 if (thread
->btrace
!= NULL
)
167 target_disable_btrace (thread
->btrace
);
169 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
170 free_one_thread (&thread
->entry
);
173 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
174 This is for cases where the caller needs a thread, but doesn't care
177 struct inferior_list_entry
*
178 get_first_inferior (struct inferior_list
*list
)
180 if (list
->head
!= NULL
)
185 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
186 returns non-zero. If no entry is found then return NULL. */
188 struct inferior_list_entry
*
189 find_inferior (struct inferior_list
*list
,
190 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
192 struct inferior_list_entry
*inf
= list
->head
;
196 struct inferior_list_entry
*next
;
199 if ((*func
) (inf
, arg
))
207 struct inferior_list_entry
*
208 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
210 struct inferior_list_entry
*inf
= list
->head
;
214 if (ptid_equal (inf
->id
, id
))
223 inferior_target_data (struct thread_info
*inferior
)
225 return inferior
->target_data
;
229 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
231 inferior
->target_data
= data
;
235 inferior_regcache_data (struct thread_info
*inferior
)
237 return inferior
->regcache_data
;
241 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
243 inferior
->regcache_data
= data
;
246 /* Return true if LIST has exactly one entry. */
249 one_inferior_p (struct inferior_list
*list
)
251 return list
->head
!= NULL
&& list
->head
== list
->tail
;
254 /* Reset head,tail of LIST, assuming all entries have already been freed. */
257 clear_inferior_list (struct inferior_list
*list
)
264 clear_inferiors (void)
266 for_each_inferior (&all_threads
, free_one_thread
);
267 clear_inferior_list (&all_threads
);
271 current_inferior
= NULL
;
274 struct process_info
*
275 add_process (int pid
, int attached
)
277 struct process_info
*process
;
279 process
= xcalloc (1, sizeof (*process
));
281 process
->entry
.id
= pid_to_ptid (pid
);
282 process
->attached
= attached
;
284 add_inferior_to_list (&all_processes
, &process
->entry
);
289 /* Remove a process from the common process list and free the memory
291 The caller is responsible for freeing private data first. */
294 remove_process (struct process_info
*process
)
296 clear_symbol_cache (&process
->symbol_cache
);
297 free_all_breakpoints (process
);
298 remove_inferior (&all_processes
, &process
->entry
);
302 struct process_info
*
303 find_process_pid (int pid
)
305 return (struct process_info
*)
306 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
309 /* Return non-zero if INF, a struct process_info, was started by us,
310 i.e. not attached to. */
313 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
315 struct process_info
*process
= (struct process_info
*) entry
;
317 return ! process
->attached
;
320 /* Return non-zero if there are any inferiors that we have created
321 (as opposed to attached-to). */
324 have_started_inferiors_p (void)
326 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
330 /* Return non-zero if INF, a struct process_info, was attached to. */
333 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
335 struct process_info
*process
= (struct process_info
*) entry
;
337 return process
->attached
;
340 /* Return non-zero if there are any inferiors that we have attached to. */
343 have_attached_inferiors_p (void)
345 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
349 struct process_info
*
350 get_thread_process (struct thread_info
*thread
)
352 int pid
= ptid_get_pid (thread
->entry
.id
);
353 return find_process_pid (pid
);
356 struct process_info
*
357 current_process (void)
359 if (current_inferior
== NULL
)
360 fatal ("Current inferior requested, but current_inferior is NULL\n");
362 return get_thread_process (current_inferior
);