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