1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2017 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/>. */
22 #include "gdbthread.h"
25 struct inferior_list all_processes
;
26 struct inferior_list all_threads
;
28 struct thread_info
*current_thread
;
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
;
60 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
63 for_each_inferior_with_data (struct inferior_list
*list
,
64 void (*action
) (struct inferior_list_entry
*,
68 struct inferior_list_entry
*cur
= list
->head
, *next
;
73 (*action
) (cur
, data
);
79 remove_inferior (struct inferior_list
*list
,
80 struct inferior_list_entry
*entry
)
82 struct inferior_list_entry
**cur
;
84 if (list
->head
== entry
)
86 list
->head
= entry
->next
;
87 if (list
->tail
== entry
)
88 list
->tail
= list
->head
;
93 while (*cur
&& (*cur
)->next
!= entry
)
99 (*cur
)->next
= entry
->next
;
101 if (list
->tail
== entry
)
106 add_thread (ptid_t thread_id
, void *target_data
)
108 struct thread_info
*new_thread
= XCNEW (struct thread_info
);
110 new_thread
->entry
.id
= thread_id
;
111 new_thread
->last_resume_kind
= resume_continue
;
112 new_thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
114 add_inferior_to_list (&all_threads
, &new_thread
->entry
);
116 if (current_thread
== NULL
)
117 current_thread
= new_thread
;
119 new_thread
->target_data
= target_data
;
125 thread_to_gdb_id (struct thread_info
*thread
)
127 return thread
->entry
.id
;
130 /* Wrapper around get_first_inferior to return a struct thread_info *. */
133 get_first_thread (void)
135 return (struct thread_info
*) get_first_inferior (&all_threads
);
139 find_thread_ptid (ptid_t ptid
)
141 return (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
144 /* Predicate function for matching thread entry's pid to the given
145 pid value passed by address in ARGS. */
148 thread_pid_matches_callback (struct inferior_list_entry
*entry
, void *args
)
150 return (ptid_get_pid (entry
->id
) == *(pid_t
*)args
);
153 /* Find a thread associated with the given PROCESS, or NULL if no
154 such thread exists. */
156 static struct thread_info
*
157 find_thread_process (const struct process_info
*const process
)
159 pid_t pid
= ptid_get_pid (ptid_of (process
));
161 return (struct thread_info
*)
162 find_inferior (&all_threads
, thread_pid_matches_callback
, &pid
);
165 /* Helper for find_any_thread_of_pid. Returns true if a thread
169 thread_of_pid (struct inferior_list_entry
*entry
, void *pid_p
)
171 int pid
= *(int *) pid_p
;
173 return (ptid_get_pid (entry
->id
) == pid
);
176 /* See gdbthread.h. */
179 find_any_thread_of_pid (int pid
)
181 struct inferior_list_entry
*entry
;
183 entry
= find_inferior (&all_threads
, thread_of_pid
, &pid
);
185 return (struct thread_info
*) entry
;
189 gdb_id_to_thread_id (ptid_t gdb_id
)
191 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
193 return thread
? thread
->entry
.id
: null_ptid
;
197 free_one_thread (struct inferior_list_entry
*inf
)
199 struct thread_info
*thread
= get_thread (inf
);
200 free_register_cache (inferior_regcache_data (thread
));
205 remove_thread (struct thread_info
*thread
)
207 if (thread
->btrace
!= NULL
)
208 target_disable_btrace (thread
->btrace
);
210 discard_queued_stop_replies (ptid_of (thread
));
211 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
212 free_one_thread (&thread
->entry
);
213 if (current_thread
== thread
)
214 current_thread
= NULL
;
217 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
218 This is for cases where the caller needs a thread, but doesn't care
221 struct inferior_list_entry
*
222 get_first_inferior (struct inferior_list
*list
)
224 if (list
->head
!= NULL
)
229 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
230 returns non-zero. If no entry is found then return NULL. */
232 struct inferior_list_entry
*
233 find_inferior (struct inferior_list
*list
,
234 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
236 struct inferior_list_entry
*inf
= list
->head
;
240 struct inferior_list_entry
*next
;
243 if ((*func
) (inf
, arg
))
251 /* Find the random inferior_list_entry E in LIST for which FUNC (E, ARG)
252 returns non-zero. If no entry is found then return NULL. */
254 struct inferior_list_entry
*
255 find_inferior_in_random (struct inferior_list
*list
,
256 int (*func
) (struct inferior_list_entry
*, void *),
259 struct inferior_list_entry
*inf
= list
->head
;
263 /* First count how many interesting entries we have. */
266 struct inferior_list_entry
*next
;
269 if ((*func
) (inf
, arg
))
277 /* Now randomly pick an entry out of those. */
278 random_selector
= (int)
279 ((count
* (double) rand ()) / (RAND_MAX
+ 1.0));
284 struct inferior_list_entry
*next
;
287 if ((*func
) (inf
, arg
) && (random_selector
-- == 0))
292 gdb_assert_not_reached ("failed to find an inferior in random.");
296 struct inferior_list_entry
*
297 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
299 struct inferior_list_entry
*inf
= list
->head
;
303 if (ptid_equal (inf
->id
, id
))
312 inferior_target_data (struct thread_info
*inferior
)
314 return inferior
->target_data
;
318 inferior_regcache_data (struct thread_info
*inferior
)
320 return inferior
->regcache_data
;
324 set_inferior_regcache_data (struct thread_info
*inferior
, struct regcache
*data
)
326 inferior
->regcache_data
= data
;
329 /* Return true if LIST has exactly one entry. */
332 one_inferior_p (struct inferior_list
*list
)
334 return list
->head
!= NULL
&& list
->head
== list
->tail
;
337 /* Reset head,tail of LIST, assuming all entries have already been freed. */
340 clear_inferior_list (struct inferior_list
*list
)
347 clear_inferiors (void)
349 for_each_inferior (&all_threads
, free_one_thread
);
350 clear_inferior_list (&all_threads
);
354 current_thread
= NULL
;
357 struct process_info
*
358 add_process (int pid
, int attached
)
360 struct process_info
*process
= XCNEW (struct process_info
);
362 process
->entry
.id
= pid_to_ptid (pid
);
363 process
->attached
= attached
;
365 add_inferior_to_list (&all_processes
, &process
->entry
);
370 /* Remove a process from the common process list and free the memory
372 The caller is responsible for freeing private data first. */
375 remove_process (struct process_info
*process
)
377 clear_symbol_cache (&process
->symbol_cache
);
378 free_all_breakpoints (process
);
379 gdb_assert (find_thread_process (process
) == NULL
);
380 remove_inferior (&all_processes
, &process
->entry
);
381 VEC_free (int, process
->syscalls_to_catch
);
385 struct process_info
*
386 find_process_pid (int pid
)
388 return (struct process_info
*)
389 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
392 /* Wrapper around get_first_inferior to return a struct process_info *. */
394 struct process_info
*
395 get_first_process (void)
397 return (struct process_info
*) get_first_inferior (&all_processes
);
400 /* Return non-zero if INF, a struct process_info, was started by us,
401 i.e. not attached to. */
404 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
406 struct process_info
*process
= (struct process_info
*) entry
;
408 return ! process
->attached
;
411 /* Return non-zero if there are any inferiors that we have created
412 (as opposed to attached-to). */
415 have_started_inferiors_p (void)
417 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
421 /* Return non-zero if INF, a struct process_info, was attached to. */
424 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
426 struct process_info
*process
= (struct process_info
*) entry
;
428 return process
->attached
;
431 /* Return non-zero if there are any inferiors that we have attached to. */
434 have_attached_inferiors_p (void)
436 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
440 struct process_info
*
441 get_thread_process (const struct thread_info
*thread
)
443 int pid
= ptid_get_pid (thread
->entry
.id
);
444 return find_process_pid (pid
);
447 struct process_info
*
448 current_process (void)
450 gdb_assert (current_thread
!= NULL
);
451 return get_thread_process (current_thread
);
455 do_restore_current_thread_cleanup (void *arg
)
457 current_thread
= (struct thread_info
*) arg
;
461 make_cleanup_restore_current_thread (void)
463 return make_cleanup (do_restore_current_thread_cleanup
, current_thread
);
466 /* See common/common-gdbthread.h. */
469 switch_to_thread (ptid_t ptid
)
471 if (!ptid_equal (ptid
, minus_one_ptid
))
472 current_thread
= find_thread_ptid (ptid
);