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
;
62 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
65 for_each_inferior_with_data (struct inferior_list
*list
,
66 void (*action
) (struct inferior_list_entry
*,
70 struct inferior_list_entry
*cur
= list
->head
, *next
;
75 (*action
) (cur
, data
);
81 remove_inferior (struct inferior_list
*list
,
82 struct inferior_list_entry
*entry
)
84 struct inferior_list_entry
**cur
;
86 if (list
->head
== entry
)
88 list
->head
= entry
->next
;
89 if (list
->tail
== entry
)
90 list
->tail
= list
->head
;
95 while (*cur
&& (*cur
)->next
!= entry
)
101 (*cur
)->next
= entry
->next
;
103 if (list
->tail
== entry
)
108 add_thread (ptid_t thread_id
, void *target_data
)
110 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
112 memset (new_thread
, 0, sizeof (*new_thread
));
114 new_thread
->entry
.id
= thread_id
;
115 new_thread
->last_resume_kind
= resume_continue
;
116 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
118 add_inferior_to_list (&all_threads
, &new_thread
->entry
);
120 if (current_inferior
== NULL
)
121 current_inferior
= new_thread
;
123 new_thread
->target_data
= target_data
;
129 thread_to_gdb_id (struct thread_info
*thread
)
131 return thread
->entry
.id
;
134 /* Wrapper around get_first_inferior to return a struct thread_info *. */
137 get_first_thread (void)
139 return (struct thread_info
*) get_first_inferior (&all_threads
);
143 find_thread_ptid (ptid_t ptid
)
145 return (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
149 gdb_id_to_thread_id (ptid_t gdb_id
)
151 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
153 return thread
? thread
->entry
.id
: null_ptid
;
157 free_one_thread (struct inferior_list_entry
*inf
)
159 struct thread_info
*thread
= get_thread (inf
);
160 free_register_cache (inferior_regcache_data (thread
));
165 remove_thread (struct thread_info
*thread
)
167 if (thread
->btrace
!= NULL
)
168 target_disable_btrace (thread
->btrace
);
170 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
171 free_one_thread (&thread
->entry
);
174 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
175 This is for cases where the caller needs a thread, but doesn't care
178 struct inferior_list_entry
*
179 get_first_inferior (struct inferior_list
*list
)
181 if (list
->head
!= NULL
)
186 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
187 returns non-zero. If no entry is found then return NULL. */
189 struct inferior_list_entry
*
190 find_inferior (struct inferior_list
*list
,
191 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
193 struct inferior_list_entry
*inf
= list
->head
;
197 struct inferior_list_entry
*next
;
200 if ((*func
) (inf
, arg
))
208 struct inferior_list_entry
*
209 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
211 struct inferior_list_entry
*inf
= list
->head
;
215 if (ptid_equal (inf
->id
, id
))
224 inferior_target_data (struct thread_info
*inferior
)
226 return inferior
->target_data
;
230 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
232 inferior
->target_data
= data
;
236 inferior_regcache_data (struct thread_info
*inferior
)
238 return inferior
->regcache_data
;
242 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
244 inferior
->regcache_data
= data
;
247 /* Return true if LIST has exactly one entry. */
250 one_inferior_p (struct inferior_list
*list
)
252 return list
->head
!= NULL
&& list
->head
== list
->tail
;
255 /* Reset head,tail of LIST, assuming all entries have already been freed. */
258 clear_inferior_list (struct inferior_list
*list
)
265 clear_inferiors (void)
267 for_each_inferior (&all_threads
, free_one_thread
);
268 clear_inferior_list (&all_threads
);
272 current_inferior
= NULL
;
275 struct process_info
*
276 add_process (int pid
, int attached
)
278 struct process_info
*process
;
280 process
= xcalloc (1, sizeof (*process
));
282 process
->entry
.id
= pid_to_ptid (pid
);
283 process
->attached
= attached
;
285 add_inferior_to_list (&all_processes
, &process
->entry
);
290 /* Remove a process from the common process list and free the memory
292 The caller is responsible for freeing private data first. */
295 remove_process (struct process_info
*process
)
297 clear_symbol_cache (&process
->symbol_cache
);
298 free_all_breakpoints (process
);
299 remove_inferior (&all_processes
, &process
->entry
);
303 struct process_info
*
304 find_process_pid (int pid
)
306 return (struct process_info
*)
307 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
310 /* Return non-zero if INF, a struct process_info, was started by us,
311 i.e. not attached to. */
314 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
316 struct process_info
*process
= (struct process_info
*) entry
;
318 return ! process
->attached
;
321 /* Return non-zero if there are any inferiors that we have created
322 (as opposed to attached-to). */
325 have_started_inferiors_p (void)
327 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
331 /* Return non-zero if INF, a struct process_info, was attached to. */
334 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
336 struct process_info
*process
= (struct process_info
*) entry
;
338 return process
->attached
;
341 /* Return non-zero if there are any inferiors that we have attached to. */
344 have_attached_inferiors_p (void)
346 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
350 struct process_info
*
351 get_thread_process (struct thread_info
*thread
)
353 int pid
= ptid_get_pid (thread
->entry
.id
);
354 return find_process_pid (pid
);
357 struct process_info
*
358 current_process (void)
360 if (current_inferior
== NULL
)
361 fatal ("Current inferior requested, but current_inferior is NULL\n");
363 return get_thread_process (current_inferior
);