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