1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2013 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/>. */
24 #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
;
62 remove_inferior (struct inferior_list
*list
,
63 struct inferior_list_entry
*entry
)
65 struct inferior_list_entry
**cur
;
67 if (list
->head
== entry
)
69 list
->head
= entry
->next
;
70 if (list
->tail
== entry
)
71 list
->tail
= list
->head
;
76 while (*cur
&& (*cur
)->next
!= entry
)
82 (*cur
)->next
= entry
->next
;
84 if (list
->tail
== entry
)
89 add_thread (ptid_t thread_id
, void *target_data
)
91 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
93 memset (new_thread
, 0, sizeof (*new_thread
));
95 new_thread
->entry
.id
= thread_id
;
96 new_thread
->last_resume_kind
= resume_continue
;
97 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
99 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
101 if (current_inferior
== NULL
)
102 current_inferior
= new_thread
;
104 new_thread
->target_data
= target_data
;
105 set_inferior_regcache_data (new_thread
, new_register_cache ());
109 thread_id_to_gdb_id (ptid_t thread_id
)
111 struct inferior_list_entry
*inf
= all_threads
.head
;
115 if (ptid_equal (inf
->id
, thread_id
))
124 thread_to_gdb_id (struct thread_info
*thread
)
126 return thread
->entry
.id
;
130 find_thread_ptid (ptid_t ptid
)
132 struct inferior_list_entry
*inf
= all_threads
.head
;
136 struct thread_info
*thread
= get_thread (inf
);
137 if (ptid_equal (thread
->entry
.id
, ptid
))
146 gdb_id_to_thread_id (ptid_t gdb_id
)
148 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
150 return thread
? thread
->entry
.id
: null_ptid
;
154 free_one_thread (struct inferior_list_entry
*inf
)
156 struct thread_info
*thread
= get_thread (inf
);
157 free_register_cache (inferior_regcache_data (thread
));
162 remove_thread (struct thread_info
*thread
)
164 if (thread
->btrace
!= NULL
)
165 target_disable_btrace (thread
->btrace
);
167 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
168 free_one_thread (&thread
->entry
);
171 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
172 returns non-zero. If no entry is found then return NULL. */
174 struct inferior_list_entry
*
175 find_inferior (struct inferior_list
*list
,
176 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
178 struct inferior_list_entry
*inf
= list
->head
;
182 struct inferior_list_entry
*next
;
185 if ((*func
) (inf
, arg
))
193 struct inferior_list_entry
*
194 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
196 struct inferior_list_entry
*inf
= list
->head
;
200 if (ptid_equal (inf
->id
, id
))
209 inferior_target_data (struct thread_info
*inferior
)
211 return inferior
->target_data
;
215 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
217 inferior
->target_data
= data
;
221 inferior_regcache_data (struct thread_info
*inferior
)
223 return inferior
->regcache_data
;
227 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
229 inferior
->regcache_data
= data
;
232 #define clear_list(LIST) \
233 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
236 clear_inferiors (void)
238 for_each_inferior (&all_threads
, free_one_thread
);
239 clear_list (&all_threads
);
243 current_inferior
= NULL
;
246 struct process_info
*
247 add_process (int pid
, int attached
)
249 struct process_info
*process
;
251 process
= xcalloc (1, sizeof (*process
));
253 process
->head
.id
= pid_to_ptid (pid
);
254 process
->attached
= attached
;
256 add_inferior_to_list (&all_processes
, &process
->head
);
261 /* Remove a process from the common process list and free the memory
263 The caller is responsible for freeing private data first. */
266 remove_process (struct process_info
*process
)
268 clear_symbol_cache (&process
->symbol_cache
);
269 free_all_breakpoints (process
);
270 remove_inferior (&all_processes
, &process
->head
);
274 struct process_info
*
275 find_process_pid (int pid
)
277 return (struct process_info
*)
278 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
281 /* Return non-zero if INF, a struct process_info, was started by us,
282 i.e. not attached to. */
285 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
287 struct process_info
*process
= (struct process_info
*) entry
;
289 return ! process
->attached
;
292 /* Return non-zero if there are any inferiors that we have created
293 (as opposed to attached-to). */
296 have_started_inferiors_p (void)
298 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
302 /* Return non-zero if INF, a struct process_info, was attached to. */
305 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
307 struct process_info
*process
= (struct process_info
*) entry
;
309 return process
->attached
;
312 /* Return non-zero if there are any inferiors that we have attached to. */
315 have_attached_inferiors_p (void)
317 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
321 struct process_info
*
322 get_thread_process (struct thread_info
*thread
)
324 int pid
= ptid_get_pid (thread
->entry
.id
);
325 return find_process_pid (pid
);
328 struct process_info
*
329 current_process (void)
331 if (current_inferior
== NULL
)
332 fatal ("Current inferior requested, but current_inferior is NULL\n");
334 return get_thread_process (current_inferior
);