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