]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbserver/inferiors.cc
gdbsupport: constify some return values in print-utils.{h,cc}
[thirdparty/binutils-gdb.git] / gdbserver / inferiors.cc
CommitLineData
ce3a066d 1/* Inferior process information for the remote server for GDB.
1d506c26 2 Copyright (C) 2002-2024 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
89e94ec9 21#include "gdbsupport/common-inferior.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
7f8acede
PA
28/* The current process. */
29static process_info *current_process_;
30
31/* The current thread. This is either a thread of CURRENT_PROCESS, or
32 NULL. */
0bfdf32f 33struct thread_info *current_thread;
0d62e5e8 34
11bd012e
SM
35/* The current working directory used to start the inferior.
36
37 Empty if not specified. */
38static std::string current_inferior_cwd;
d092c5a2 39
f7667f0d 40struct thread_info *
95954743 41add_thread (ptid_t thread_id, void *target_data)
0d62e5e8 42{
d2f325df 43 thread_info *new_thread = new thread_info (thread_id, target_data);
0d62e5e8 44
9c80ecd6 45 all_threads.push_back (new_thread);
255e7678 46
0bfdf32f 47 if (current_thread == NULL)
24583e45 48 switch_to_thread (new_thread);
ce3a066d 49
f7667f0d 50 return new_thread;
a06660f7
DJ
51}
52
9c80ecd6 53/* See gdbthread.h. */
649ebbca 54
dae5f5cf 55struct thread_info *
649ebbca 56get_first_thread (void)
a06660f7 57{
9c80ecd6
SM
58 if (!all_threads.empty ())
59 return all_threads.front ();
60 else
61 return NULL;
649ebbca 62}
a06660f7 63
649ebbca
DE
64struct thread_info *
65find_thread_ptid (ptid_t ptid)
66{
8dc7b443
SM
67 return find_thread ([&] (thread_info *thread) {
68 return thread->id == ptid;
69 });
dae5f5cf
DJ
70}
71
96e7a1eb
AR
72/* Find a thread associated with the given PROCESS, or NULL if no
73 such thread exists. */
74
75static struct thread_info *
76find_thread_process (const struct process_info *const process)
77{
9179355e 78 return find_any_thread_of_pid (process->pid);
96e7a1eb
AR
79}
80
34c65914
PA
81/* See gdbthread.h. */
82
83struct thread_info *
84find_any_thread_of_pid (int pid)
85{
4d3bb80e
SM
86 return find_thread (pid, [] (thread_info *thread) {
87 return true;
88 });
34c65914
PA
89}
90
0d62e5e8 91static void
9c80ecd6 92free_one_thread (thread_info *thread)
0d62e5e8 93{
d2f325df 94 delete thread;
0d62e5e8
DJ
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 104 all_threads.remove (thread);
96e7a1eb 105 if (current_thread == thread)
24583e45 106 switch_to_thread (nullptr);
a9b45cb7 107 free_one_thread (thread);
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
24583e45 136 switch_to_thread (nullptr);
7f8acede 137 current_process_ = nullptr;
255e7678 138}
24a09b5f 139
95954743
PA
140struct process_info *
141add_process (int pid, int attached)
142{
f27866ba 143 process_info *process = new process_info (pid, attached);
95954743 144
9179355e 145 all_processes.push_back (process);
95954743
PA
146
147 return process;
148}
149
5091eb23
DE
150/* Remove a process from the common process list and free the memory
151 allocated for it.
152 The caller is responsible for freeing private data first. */
153
95954743
PA
154void
155remove_process (struct process_info *process)
156{
157 clear_symbol_cache (&process->symbol_cache);
158 free_all_breakpoints (process);
96e7a1eb 159 gdb_assert (find_thread_process (process) == NULL);
9179355e 160 all_processes.remove (process);
7f8acede
PA
161 if (current_process () == process)
162 switch_to_process (nullptr);
f27866ba 163 delete process;
95954743
PA
164}
165
9179355e 166process_info *
95954743
PA
167find_process_pid (int pid)
168{
9179355e
SM
169 return find_process ([&] (process_info *process) {
170 return process->pid == pid;
171 });
95954743
PA
172}
173
9179355e 174/* Get the first process in the process list, or NULL if the list is empty. */
3d40fbb5 175
9179355e 176process_info *
3d40fbb5
PA
177get_first_process (void)
178{
9179355e
SM
179 if (!all_processes.empty ())
180 return all_processes.front ();
181 else
182 return NULL;
9f767825
DE
183}
184
185/* Return non-zero if there are any inferiors that we have created
186 (as opposed to attached-to). */
187
188int
189have_started_inferiors_p (void)
190{
9179355e
SM
191 return find_process ([] (process_info *process) {
192 return !process->attached;
193 }) != NULL;
9f767825
DE
194}
195
196/* Return non-zero if there are any inferiors that we have attached to. */
197
198int
199have_attached_inferiors_p (void)
200{
9179355e
SM
201 return find_process ([] (process_info *process) {
202 return process->attached;
203 }) != NULL;
9f767825
DE
204}
205
7fe519cb 206struct process_info *
63c40ec7 207get_thread_process (const struct thread_info *thread)
95954743 208{
9c80ecd6 209 return find_process_pid (thread->id.pid ());
95954743
PA
210}
211
212struct process_info *
213current_process (void)
214{
7f8acede 215 return current_process_;
95954743 216}
984a2c04 217
268a13a5 218/* See gdbsupport/common-gdbthread.h. */
043a4934
SDJ
219
220void
5b6d1e4f 221switch_to_thread (process_stratum_target *ops, ptid_t ptid)
043a4934 222{
75352e28 223 gdb_assert (ptid != minus_one_ptid);
24583e45 224 switch_to_thread (find_thread_ptid (ptid));
043a4934 225}
d092c5a2 226
f24791b7
TBA
227/* See gdbthread.h. */
228
229void
230switch_to_thread (thread_info *thread)
231{
7f8acede
PA
232 if (thread != nullptr)
233 current_process_ = get_thread_process (thread);
234 else
235 current_process_ = nullptr;
f24791b7
TBA
236 current_thread = thread;
237}
238
028a4603
PA
239/* See inferiors.h. */
240
241void
242switch_to_process (process_info *proc)
243{
7f8acede
PA
244 current_process_ = proc;
245 current_thread = nullptr;
028a4603
PA
246}
247
268a13a5 248/* See gdbsupport/common-inferior.h. */
d092c5a2 249
11bd012e 250const std::string &
d092c5a2
SDJ
251get_inferior_cwd ()
252{
253 return current_inferior_cwd;
254}
bc3b087d 255
5b8bf2e7 256/* See inferiors.h. */
bc3b087d
SDJ
257
258void
11bd012e 259set_inferior_cwd (std::string cwd)
bc3b087d 260{
11bd012e 261 current_inferior_cwd = std::move (cwd);
bc3b087d 262}
f24791b7
TBA
263
264scoped_restore_current_thread::scoped_restore_current_thread ()
265{
7f8acede 266 m_process = current_process_;
f24791b7
TBA
267 m_thread = current_thread;
268}
269
270scoped_restore_current_thread::~scoped_restore_current_thread ()
271{
272 if (m_dont_restore)
273 return;
274
7f8acede
PA
275 if (m_thread != nullptr)
276 switch_to_thread (m_thread);
277 else
278 switch_to_process (m_process);
f24791b7 279}