Remove find_inferior
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
CommitLineData
ce3a066d 1/* Inferior process information for the remote server for GDB.
61baf725 2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
ce3a066d
DJ
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
ce3a066d
DJ
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce3a066d 20
ce3a066d 21#include "server.h"
623b6bdf 22#include "gdbthread.h"
799cdc37 23#include "dll.h"
ce3a066d 24
9179355e 25std::list<process_info *> all_processes;
9c80ecd6 26std::list<thread_info *> all_threads;
0d62e5e8 27
0bfdf32f 28struct thread_info *current_thread;
0d62e5e8 29
d092c5a2
SDJ
30/* The current working directory used to start the inferior. */
31static const char *current_inferior_cwd = NULL;
32
0d62e5e8 33void
9c80ecd6
SM
34for_each_inferior (std::list<thread_info *> *thread_list,
35 void (*action) (thread_info *))
0d62e5e8 36{
9c80ecd6 37 gdb_assert (thread_list == &all_threads);
0d62e5e8 38
9c80ecd6
SM
39 for_each_thread ([&] (thread_info *thread) {
40 action (thread);
41 });
42}
ce3a066d 43
9c80ecd6
SM
44void
45for_each_inferior_with_data (std::list<thread_info *> *thread_list,
46 void (*action) (thread_info *, void *),
47 void *data)
48{
49 gdb_assert (thread_list == &all_threads);
0d62e5e8 50
9c80ecd6
SM
51 for_each_thread ([&] (thread_info *thread) {
52 action (thread, data);
53 });
0d62e5e8
DJ
54}
55
f7667f0d 56struct thread_info *
95954743 57add_thread (ptid_t thread_id, void *target_data)
0d62e5e8 58{
8d749320 59 struct thread_info *new_thread = XCNEW (struct thread_info);
0d62e5e8 60
9c80ecd6 61 new_thread->id = thread_id;
8336d594 62 new_thread->last_resume_kind = resume_continue;
fc7238bb 63 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
0d62e5e8 64
9c80ecd6 65 all_threads.push_back (new_thread);
255e7678 66
0bfdf32f
GB
67 if (current_thread == NULL)
68 current_thread = new_thread;
ce3a066d 69
0d62e5e8 70 new_thread->target_data = target_data;
f7667f0d
DE
71
72 return new_thread;
a06660f7
DJ
73}
74
9c80ecd6 75/* See gdbthread.h. */
649ebbca 76
dae5f5cf 77struct thread_info *
649ebbca 78get_first_thread (void)
a06660f7 79{
9c80ecd6
SM
80 if (!all_threads.empty ())
81 return all_threads.front ();
82 else
83 return NULL;
649ebbca 84}
a06660f7 85
649ebbca
DE
86struct thread_info *
87find_thread_ptid (ptid_t ptid)
88{
8dc7b443
SM
89 return find_thread ([&] (thread_info *thread) {
90 return thread->id == ptid;
91 });
dae5f5cf
DJ
92}
93
96e7a1eb
AR
94/* Find a thread associated with the given PROCESS, or NULL if no
95 such thread exists. */
96
97static struct thread_info *
98find_thread_process (const struct process_info *const process)
99{
9179355e 100 return find_any_thread_of_pid (process->pid);
96e7a1eb
AR
101}
102
34c65914
PA
103/* See gdbthread.h. */
104
105struct thread_info *
106find_any_thread_of_pid (int pid)
107{
4d3bb80e
SM
108 return find_thread (pid, [] (thread_info *thread) {
109 return true;
110 });
34c65914
PA
111}
112
0d62e5e8 113static void
9c80ecd6 114free_one_thread (thread_info *thread)
0d62e5e8 115{
6afd337d 116 free_register_cache (thread_regcache_data (thread));
0d62e5e8
DJ
117 free (thread);
118}
119
120void
121remove_thread (struct thread_info *thread)
122{
9accd112
MM
123 if (thread->btrace != NULL)
124 target_disable_btrace (thread->btrace);
125
465a859e 126 discard_queued_stop_replies (ptid_of (thread));
9c80ecd6
SM
127 all_threads.remove (thread);
128 free_one_thread (thread);
96e7a1eb
AR
129 if (current_thread == thread)
130 current_thread = NULL;
ce3a066d
DJ
131}
132
611cb4a5 133void *
6afd337d 134thread_target_data (struct thread_info *thread)
611cb4a5 135{
6afd337d 136 return thread->target_data;
611cb4a5
DJ
137}
138
a44892be 139struct regcache *
6afd337d 140thread_regcache_data (struct thread_info *thread)
c04a1aa8 141{
6afd337d 142 return thread->regcache_data;
c04a1aa8
DJ
143}
144
145void
6afd337d 146set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
c04a1aa8 147{
6afd337d 148 thread->regcache_data = data;
c04a1aa8 149}
255e7678 150
255e7678
DJ
151void
152clear_inferiors (void)
153{
154 for_each_inferior (&all_threads, free_one_thread);
9c80ecd6 155 all_threads.clear ();
bf4c19f7
YQ
156
157 clear_dlls ();
7284e1be 158
0bfdf32f 159 current_thread = NULL;
255e7678 160}
24a09b5f 161
95954743
PA
162struct process_info *
163add_process (int pid, int attached)
164{
f27866ba 165 process_info *process = new process_info (pid, attached);
95954743 166
9179355e 167 all_processes.push_back (process);
95954743
PA
168
169 return process;
170}
171
5091eb23
DE
172/* Remove a process from the common process list and free the memory
173 allocated for it.
174 The caller is responsible for freeing private data first. */
175
95954743
PA
176void
177remove_process (struct process_info *process)
178{
179 clear_symbol_cache (&process->symbol_cache);
180 free_all_breakpoints (process);
96e7a1eb 181 gdb_assert (find_thread_process (process) == NULL);
9179355e 182 all_processes.remove (process);
f27866ba 183 delete process;
95954743
PA
184}
185
9179355e 186process_info *
95954743
PA
187find_process_pid (int pid)
188{
9179355e
SM
189 return find_process ([&] (process_info *process) {
190 return process->pid == pid;
191 });
95954743
PA
192}
193
9179355e 194/* Get the first process in the process list, or NULL if the list is empty. */
3d40fbb5 195
9179355e 196process_info *
3d40fbb5
PA
197get_first_process (void)
198{
9179355e
SM
199 if (!all_processes.empty ())
200 return all_processes.front ();
201 else
202 return NULL;
9f767825
DE
203}
204
205/* Return non-zero if there are any inferiors that we have created
206 (as opposed to attached-to). */
207
208int
209have_started_inferiors_p (void)
210{
9179355e
SM
211 return find_process ([] (process_info *process) {
212 return !process->attached;
213 }) != NULL;
9f767825
DE
214}
215
216/* Return non-zero if there are any inferiors that we have attached to. */
217
218int
219have_attached_inferiors_p (void)
220{
9179355e
SM
221 return find_process ([] (process_info *process) {
222 return process->attached;
223 }) != NULL;
9f767825
DE
224}
225
7fe519cb 226struct process_info *
63c40ec7 227get_thread_process (const struct thread_info *thread)
95954743 228{
9c80ecd6 229 return find_process_pid (thread->id.pid ());
95954743
PA
230}
231
232struct process_info *
233current_process (void)
234{
0bfdf32f
GB
235 gdb_assert (current_thread != NULL);
236 return get_thread_process (current_thread);
95954743 237}
984a2c04
YQ
238
239static void
240do_restore_current_thread_cleanup (void *arg)
241{
242 current_thread = (struct thread_info *) arg;
243}
244
245struct cleanup *
246make_cleanup_restore_current_thread (void)
247{
248 return make_cleanup (do_restore_current_thread_cleanup, current_thread);
249}
043a4934
SDJ
250
251/* See common/common-gdbthread.h. */
252
253void
254switch_to_thread (ptid_t ptid)
255{
75352e28
SDJ
256 gdb_assert (ptid != minus_one_ptid);
257 current_thread = find_thread_ptid (ptid);
043a4934 258}
d092c5a2
SDJ
259
260/* See common/common-inferior.h. */
261
262const char *
263get_inferior_cwd ()
264{
265 return current_inferior_cwd;
266}
bc3b087d
SDJ
267
268/* See common/common-inferior.h. */
269
270void
271set_inferior_cwd (const char *cwd)
272{
273 xfree ((void *) current_inferior_cwd);
274 if (cwd != NULL)
275 current_inferior_cwd = xstrdup (cwd);
276 else
277 current_inferior_cwd = NULL;
278}
This page took 1.186341 seconds and 4 git commands to generate.