gdb: make inferior::m_cwd an std::string
[binutils-gdb.git] / gdbserver / inferiors.cc
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "gdbsupport/common-inferior.h"
23 #include "gdbthread.h"
24 #include "dll.h"
25
26 std::list<process_info *> all_processes;
27 std::list<thread_info *> all_threads;
28
29 struct thread_info *current_thread;
30
31 /* The current working directory used to start the inferior.
32
33 Empty if not specified. */
34 static std::string current_inferior_cwd;
35
36 struct thread_info *
37 add_thread (ptid_t thread_id, void *target_data)
38 {
39 struct thread_info *new_thread = XCNEW (struct thread_info);
40
41 new_thread->id = thread_id;
42 new_thread->last_resume_kind = resume_continue;
43 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
44
45 all_threads.push_back (new_thread);
46
47 if (current_thread == NULL)
48 current_thread = new_thread;
49
50 new_thread->target_data = target_data;
51
52 return new_thread;
53 }
54
55 /* See gdbthread.h. */
56
57 struct thread_info *
58 get_first_thread (void)
59 {
60 if (!all_threads.empty ())
61 return all_threads.front ();
62 else
63 return NULL;
64 }
65
66 struct thread_info *
67 find_thread_ptid (ptid_t ptid)
68 {
69 return find_thread ([&] (thread_info *thread) {
70 return thread->id == ptid;
71 });
72 }
73
74 /* Find a thread associated with the given PROCESS, or NULL if no
75 such thread exists. */
76
77 static struct thread_info *
78 find_thread_process (const struct process_info *const process)
79 {
80 return find_any_thread_of_pid (process->pid);
81 }
82
83 /* See gdbthread.h. */
84
85 struct thread_info *
86 find_any_thread_of_pid (int pid)
87 {
88 return find_thread (pid, [] (thread_info *thread) {
89 return true;
90 });
91 }
92
93 static void
94 free_one_thread (thread_info *thread)
95 {
96 free_register_cache (thread_regcache_data (thread));
97 free (thread);
98 }
99
100 void
101 remove_thread (struct thread_info *thread)
102 {
103 if (thread->btrace != NULL)
104 target_disable_btrace (thread->btrace);
105
106 discard_queued_stop_replies (ptid_of (thread));
107 all_threads.remove (thread);
108 if (current_thread == thread)
109 current_thread = NULL;
110 free_one_thread (thread);
111 }
112
113 void *
114 thread_target_data (struct thread_info *thread)
115 {
116 return thread->target_data;
117 }
118
119 struct regcache *
120 thread_regcache_data (struct thread_info *thread)
121 {
122 return thread->regcache_data;
123 }
124
125 void
126 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
127 {
128 thread->regcache_data = data;
129 }
130
131 void
132 clear_inferiors (void)
133 {
134 for_each_thread (free_one_thread);
135 all_threads.clear ();
136
137 clear_dlls ();
138
139 current_thread = NULL;
140 }
141
142 struct process_info *
143 add_process (int pid, int attached)
144 {
145 process_info *process = new process_info (pid, attached);
146
147 all_processes.push_back (process);
148
149 return process;
150 }
151
152 /* Remove a process from the common process list and free the memory
153 allocated for it.
154 The caller is responsible for freeing private data first. */
155
156 void
157 remove_process (struct process_info *process)
158 {
159 clear_symbol_cache (&process->symbol_cache);
160 free_all_breakpoints (process);
161 gdb_assert (find_thread_process (process) == NULL);
162 all_processes.remove (process);
163 delete process;
164 }
165
166 process_info *
167 find_process_pid (int pid)
168 {
169 return find_process ([&] (process_info *process) {
170 return process->pid == pid;
171 });
172 }
173
174 /* Get the first process in the process list, or NULL if the list is empty. */
175
176 process_info *
177 get_first_process (void)
178 {
179 if (!all_processes.empty ())
180 return all_processes.front ();
181 else
182 return NULL;
183 }
184
185 /* Return non-zero if there are any inferiors that we have created
186 (as opposed to attached-to). */
187
188 int
189 have_started_inferiors_p (void)
190 {
191 return find_process ([] (process_info *process) {
192 return !process->attached;
193 }) != NULL;
194 }
195
196 /* Return non-zero if there are any inferiors that we have attached to. */
197
198 int
199 have_attached_inferiors_p (void)
200 {
201 return find_process ([] (process_info *process) {
202 return process->attached;
203 }) != NULL;
204 }
205
206 struct process_info *
207 get_thread_process (const struct thread_info *thread)
208 {
209 return find_process_pid (thread->id.pid ());
210 }
211
212 struct process_info *
213 current_process (void)
214 {
215 gdb_assert (current_thread != NULL);
216 return get_thread_process (current_thread);
217 }
218
219 /* See gdbsupport/common-gdbthread.h. */
220
221 void
222 switch_to_thread (process_stratum_target *ops, ptid_t ptid)
223 {
224 gdb_assert (ptid != minus_one_ptid);
225 current_thread = find_thread_ptid (ptid);
226 }
227
228 /* See inferiors.h. */
229
230 void
231 switch_to_process (process_info *proc)
232 {
233 int pid = pid_of (proc);
234
235 current_thread = find_any_thread_of_pid (pid);
236 }
237
238 /* See gdbsupport/common-inferior.h. */
239
240 const std::string &
241 get_inferior_cwd ()
242 {
243 return current_inferior_cwd;
244 }
245
246 /* See inferiors.h. */
247
248 void
249 set_inferior_cwd (std::string cwd)
250 {
251 current_inferior_cwd = std::move (cwd);
252 }