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/>. */
24 #include "gdbthread.h"
27 struct inferior_list all_processes
;
28 struct inferior_list all_threads
;
30 struct thread_info
*current_inferior
;
32 #define get_thread(inf) ((struct thread_info *)(inf))
35 add_inferior_to_list (struct inferior_list
*list
,
36 struct inferior_list_entry
*new_inferior
)
38 new_inferior
->next
= NULL
;
39 if (list
->tail
!= NULL
)
40 list
->tail
->next
= new_inferior
;
42 list
->head
= new_inferior
;
43 list
->tail
= new_inferior
;
46 /* Invoke ACTION for each inferior in LIST. */
49 for_each_inferior (struct inferior_list
*list
,
50 void (*action
) (struct inferior_list_entry
*))
52 struct inferior_list_entry
*cur
= list
->head
, *next
;
63 remove_inferior (struct inferior_list
*list
,
64 struct inferior_list_entry
*entry
)
66 struct inferior_list_entry
**cur
;
68 if (list
->head
== entry
)
70 list
->head
= entry
->next
;
71 if (list
->tail
== entry
)
72 list
->tail
= list
->head
;
77 while (*cur
&& (*cur
)->next
!= entry
)
83 (*cur
)->next
= entry
->next
;
85 if (list
->tail
== entry
)
90 add_thread (ptid_t thread_id
, void *target_data
)
92 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
94 memset (new_thread
, 0, sizeof (*new_thread
));
96 new_thread
->entry
.id
= thread_id
;
97 new_thread
->last_resume_kind
= resume_continue
;
98 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
100 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
102 if (current_inferior
== NULL
)
103 current_inferior
= new_thread
;
105 new_thread
->target_data
= target_data
;
109 thread_to_gdb_id (struct thread_info
*thread
)
111 return thread
->entry
.id
;
115 find_thread_ptid (ptid_t ptid
)
117 struct inferior_list_entry
*inf
= all_threads
.head
;
121 struct thread_info
*thread
= get_thread (inf
);
122 if (ptid_equal (thread
->entry
.id
, ptid
))
131 gdb_id_to_thread_id (ptid_t gdb_id
)
133 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
135 return thread
? thread
->entry
.id
: null_ptid
;
139 free_one_thread (struct inferior_list_entry
*inf
)
141 struct thread_info
*thread
= get_thread (inf
);
142 free_register_cache (inferior_regcache_data (thread
));
147 remove_thread (struct thread_info
*thread
)
149 if (thread
->btrace
!= NULL
)
150 target_disable_btrace (thread
->btrace
);
152 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
153 free_one_thread (&thread
->entry
);
156 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
157 returns non-zero. If no entry is found then return NULL. */
159 struct inferior_list_entry
*
160 find_inferior (struct inferior_list
*list
,
161 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
163 struct inferior_list_entry
*inf
= list
->head
;
167 struct inferior_list_entry
*next
;
170 if ((*func
) (inf
, arg
))
178 struct inferior_list_entry
*
179 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
181 struct inferior_list_entry
*inf
= list
->head
;
185 if (ptid_equal (inf
->id
, id
))
194 inferior_target_data (struct thread_info
*inferior
)
196 return inferior
->target_data
;
200 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
202 inferior
->target_data
= data
;
206 inferior_regcache_data (struct thread_info
*inferior
)
208 return inferior
->regcache_data
;
212 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
214 inferior
->regcache_data
= data
;
217 #define clear_list(LIST) \
218 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
221 clear_inferiors (void)
223 for_each_inferior (&all_threads
, free_one_thread
);
224 clear_list (&all_threads
);
228 current_inferior
= NULL
;
231 struct process_info
*
232 add_process (int pid
, int attached
)
234 struct process_info
*process
;
236 process
= xcalloc (1, sizeof (*process
));
238 process
->head
.id
= pid_to_ptid (pid
);
239 process
->attached
= attached
;
241 add_inferior_to_list (&all_processes
, &process
->head
);
246 /* Remove a process from the common process list and free the memory
248 The caller is responsible for freeing private data first. */
251 remove_process (struct process_info
*process
)
253 clear_symbol_cache (&process
->symbol_cache
);
254 free_all_breakpoints (process
);
255 remove_inferior (&all_processes
, &process
->head
);
259 struct process_info
*
260 find_process_pid (int pid
)
262 return (struct process_info
*)
263 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
266 /* Return non-zero if INF, a struct process_info, was started by us,
267 i.e. not attached to. */
270 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
272 struct process_info
*process
= (struct process_info
*) entry
;
274 return ! process
->attached
;
277 /* Return non-zero if there are any inferiors that we have created
278 (as opposed to attached-to). */
281 have_started_inferiors_p (void)
283 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
287 /* Return non-zero if INF, a struct process_info, was attached to. */
290 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
292 struct process_info
*process
= (struct process_info
*) entry
;
294 return process
->attached
;
297 /* Return non-zero if there are any inferiors that we have attached to. */
300 have_attached_inferiors_p (void)
302 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
306 struct process_info
*
307 get_thread_process (struct thread_info
*thread
)
309 int pid
= ptid_get_pid (thread
->entry
.id
);
310 return find_process_pid (pid
);
313 struct process_info
*
314 current_process (void)
316 if (current_inferior
== NULL
)
317 fatal ("Current inferior requested, but current_inferior is NULL\n");
319 return get_thread_process (current_inferior
);