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 std::list
<process_info
*> all_processes
;
26 struct inferior_list all_threads
;
28 struct thread_info
*current_thread
;
30 #define get_thread(inf) ((struct thread_info *)(inf))
32 /* The current working directory used to start the inferior. */
33 static const char *current_inferior_cwd
= NULL
;
36 add_inferior_to_list (struct inferior_list
*list
,
37 struct inferior_list_entry
*new_inferior
)
39 new_inferior
->next
= NULL
;
40 if (list
->tail
!= NULL
)
41 list
->tail
->next
= new_inferior
;
43 list
->head
= new_inferior
;
44 list
->tail
= new_inferior
;
47 /* Invoke ACTION for each inferior in LIST. */
50 for_each_inferior (struct inferior_list
*list
,
51 void (*action
) (struct inferior_list_entry
*))
53 struct inferior_list_entry
*cur
= list
->head
, *next
;
63 /* Invoke ACTION for each inferior in LIST, passing DATA to ACTION. */
66 for_each_inferior_with_data (struct inferior_list
*list
,
67 void (*action
) (struct inferior_list_entry
*,
71 struct inferior_list_entry
*cur
= list
->head
, *next
;
76 (*action
) (cur
, data
);
82 remove_inferior (struct inferior_list
*list
,
83 struct inferior_list_entry
*entry
)
85 struct inferior_list_entry
**cur
;
87 if (list
->head
== entry
)
89 list
->head
= entry
->next
;
90 if (list
->tail
== entry
)
91 list
->tail
= list
->head
;
96 while (*cur
&& (*cur
)->next
!= entry
)
102 (*cur
)->next
= entry
->next
;
104 if (list
->tail
== entry
)
109 add_thread (ptid_t thread_id
, void *target_data
)
111 struct thread_info
*new_thread
= XCNEW (struct thread_info
);
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_thread
== NULL
)
120 current_thread
= new_thread
;
122 new_thread
->target_data
= target_data
;
127 /* Wrapper around get_first_inferior to return a struct thread_info *. */
130 get_first_thread (void)
132 return (struct thread_info
*) get_first_inferior (&all_threads
);
136 find_thread_ptid (ptid_t ptid
)
138 return (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
141 /* Find a thread associated with the given PROCESS, or NULL if no
142 such thread exists. */
144 static struct thread_info
*
145 find_thread_process (const struct process_info
*const process
)
147 return find_any_thread_of_pid (process
->pid
);
150 /* Helper for find_any_thread_of_pid. Returns true if a thread
154 thread_of_pid (struct inferior_list_entry
*entry
, void *pid_p
)
156 int pid
= *(int *) pid_p
;
158 return (ptid_get_pid (entry
->id
) == pid
);
161 /* See gdbthread.h. */
164 find_any_thread_of_pid (int pid
)
166 struct inferior_list_entry
*entry
;
168 entry
= find_inferior (&all_threads
, thread_of_pid
, &pid
);
170 return (struct thread_info
*) entry
;
174 free_one_thread (struct inferior_list_entry
*inf
)
176 struct thread_info
*thread
= get_thread (inf
);
177 free_register_cache (thread_regcache_data (thread
));
182 remove_thread (struct thread_info
*thread
)
184 if (thread
->btrace
!= NULL
)
185 target_disable_btrace (thread
->btrace
);
187 discard_queued_stop_replies (ptid_of (thread
));
188 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
189 free_one_thread (&thread
->entry
);
190 if (current_thread
== thread
)
191 current_thread
= NULL
;
194 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
195 This is for cases where the caller needs a thread, but doesn't care
198 struct inferior_list_entry
*
199 get_first_inferior (struct inferior_list
*list
)
201 if (list
->head
!= NULL
)
206 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
207 returns non-zero. If no entry is found then return NULL. */
209 struct inferior_list_entry
*
210 find_inferior (struct inferior_list
*list
,
211 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
213 struct inferior_list_entry
*inf
= list
->head
;
217 struct inferior_list_entry
*next
;
220 if ((*func
) (inf
, arg
))
228 /* Find the random inferior_list_entry E in LIST for which FUNC (E, ARG)
229 returns non-zero. If no entry is found then return NULL. */
231 struct inferior_list_entry
*
232 find_inferior_in_random (struct inferior_list
*list
,
233 int (*func
) (struct inferior_list_entry
*, void *),
236 struct inferior_list_entry
*inf
= list
->head
;
240 /* First count how many interesting entries we have. */
243 struct inferior_list_entry
*next
;
246 if ((*func
) (inf
, arg
))
254 /* Now randomly pick an entry out of those. */
255 random_selector
= (int)
256 ((count
* (double) rand ()) / (RAND_MAX
+ 1.0));
261 struct inferior_list_entry
*next
;
264 if ((*func
) (inf
, arg
) && (random_selector
-- == 0))
269 gdb_assert_not_reached ("failed to find an inferior in random.");
273 struct inferior_list_entry
*
274 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
276 struct inferior_list_entry
*inf
= list
->head
;
280 if (ptid_equal (inf
->id
, id
))
289 thread_target_data (struct thread_info
*thread
)
291 return thread
->target_data
;
295 thread_regcache_data (struct thread_info
*thread
)
297 return thread
->regcache_data
;
301 set_thread_regcache_data (struct thread_info
*thread
, struct regcache
*data
)
303 thread
->regcache_data
= data
;
306 /* Return true if LIST has exactly one entry. */
309 one_inferior_p (struct inferior_list
*list
)
311 return list
->head
!= NULL
&& list
->head
== list
->tail
;
314 /* Reset head,tail of LIST, assuming all entries have already been freed. */
317 clear_inferior_list (struct inferior_list
*list
)
324 clear_inferiors (void)
326 for_each_inferior (&all_threads
, free_one_thread
);
327 clear_inferior_list (&all_threads
);
331 current_thread
= NULL
;
334 struct process_info
*
335 add_process (int pid
, int attached
)
337 struct process_info
*process
= XCNEW (struct process_info
);
340 process
->attached
= attached
;
342 all_processes
.push_back (process
);
347 /* Remove a process from the common process list and free the memory
349 The caller is responsible for freeing private data first. */
352 remove_process (struct process_info
*process
)
354 clear_symbol_cache (&process
->symbol_cache
);
355 free_all_breakpoints (process
);
356 gdb_assert (find_thread_process (process
) == NULL
);
357 all_processes
.remove (process
);
358 VEC_free (int, process
->syscalls_to_catch
);
363 find_process_pid (int pid
)
365 return find_process ([&] (process_info
*process
) {
366 return process
->pid
== pid
;
370 /* Get the first process in the process list, or NULL if the list is empty. */
373 get_first_process (void)
375 if (!all_processes
.empty ())
376 return all_processes
.front ();
381 /* Return non-zero if there are any inferiors that we have created
382 (as opposed to attached-to). */
385 have_started_inferiors_p (void)
387 return find_process ([] (process_info
*process
) {
388 return !process
->attached
;
392 /* Return non-zero if there are any inferiors that we have attached to. */
395 have_attached_inferiors_p (void)
397 return find_process ([] (process_info
*process
) {
398 return process
->attached
;
402 struct process_info
*
403 get_thread_process (const struct thread_info
*thread
)
405 int pid
= ptid_get_pid (thread
->entry
.id
);
406 return find_process_pid (pid
);
409 struct process_info
*
410 current_process (void)
412 gdb_assert (current_thread
!= NULL
);
413 return get_thread_process (current_thread
);
417 do_restore_current_thread_cleanup (void *arg
)
419 current_thread
= (struct thread_info
*) arg
;
423 make_cleanup_restore_current_thread (void)
425 return make_cleanup (do_restore_current_thread_cleanup
, current_thread
);
428 /* See common/common-gdbthread.h. */
431 switch_to_thread (ptid_t ptid
)
433 gdb_assert (ptid
!= minus_one_ptid
);
434 current_thread
= find_thread_ptid (ptid
);
437 /* See common/common-inferior.h. */
442 return current_inferior_cwd
;
445 /* See common/common-inferior.h. */
448 set_inferior_cwd (const char *cwd
)
450 xfree ((void *) current_inferior_cwd
);
452 current_inferior_cwd
= xstrdup (cwd
);
454 current_inferior_cwd
= NULL
;