GDBserver

inferiors.c
Go to the documentation of this file.
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2018 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 "gdbthread.h"
23 #include "dll.h"
24
25 std::list<process_info *> all_processes;
26 std::list<thread_info *> all_threads;
27
28 struct thread_info *current_thread;
29
30 /* The current working directory used to start the inferior. */
31 static const char *current_inferior_cwd = NULL;
32
33 struct thread_info *
34 add_thread (ptid_t thread_id, void *target_data)
35 {
36 struct thread_info *new_thread = XCNEW (struct thread_info);
37
38 new_thread->id = thread_id;
39 new_thread->last_resume_kind = resume_continue;
40 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
41
42 all_threads.push_back (new_thread);
43
44 if (current_thread == NULL)
45 current_thread = new_thread;
46
47 new_thread->target_data = target_data;
48
49 return new_thread;
50 }
51
52 /* See gdbthread.h. */
53
54 struct thread_info *
55 get_first_thread (void)
56 {
57 if (!all_threads.empty ())
58 return all_threads.front ();
59 else
60 return NULL;
61 }
62
63 struct thread_info *
64 find_thread_ptid (ptid_t ptid)
65 {
66 return find_thread ([&] (thread_info *thread) {
67 return thread->id == ptid;
68 });
69 }
70
71 /* Find a thread associated with the given PROCESS, or NULL if no
72 such thread exists. */
73
74 static struct thread_info *
75 find_thread_process (const struct process_info *const process)
76 {
77 return find_any_thread_of_pid (process->pid);
78 }
79
80 /* See gdbthread.h. */
81
82 struct thread_info *
83 find_any_thread_of_pid (int pid)
84 {
85 return find_thread (pid, [] (thread_info *thread) {
86 return true;
87 });
88 }
89
90 static void
91 free_one_thread (thread_info *thread)
92 {
93 free_register_cache (thread_regcache_data (thread));
94 free (thread);
95 }
96
97 void
98 remove_thread (struct thread_info *thread)
99 {
100 if (thread->btrace != NULL)
101 target_disable_btrace (thread->btrace);
102
103 discard_queued_stop_replies (ptid_of (thread));
104 all_threads.remove (thread);
105 free_one_thread (thread);
106 if (current_thread == thread)
107 current_thread = NULL;
108 }
109
110 void *
111 thread_target_data (struct thread_info *thread)
112 {
113 return thread->target_data;
114 }
115
116 struct regcache *
117 thread_regcache_data (struct thread_info *thread)
118 {
119 return thread->regcache_data;
120 }
121
122 void
123 set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
124 {
125 thread->regcache_data = data;
126 }
127
128 void
129 clear_inferiors (void)
130 {
131 for_each_thread (free_one_thread);
132 all_threads.clear ();
133
134 clear_dlls ();
135
136 current_thread = NULL;
137 }
138
139 struct process_info *
140 add_process (int pid, int attached)
141 {
142 process_info *process = new process_info (pid, attached);
143
144 all_processes.push_back (process);
145
146 return process;
147 }
148
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
153 void
154 remove_process (struct process_info *process)
155 {
156 clear_symbol_cache (&process->symbol_cache);
157 free_all_breakpoints (process);
158 gdb_assert (find_thread_process (process) == NULL);
159 all_processes.remove (process);
160 delete process;
161 }
162
163 process_info *
164 find_process_pid (int pid)
165 {
166 return find_process ([&] (process_info *process) {
167 return process->pid == pid;
168 });
169 }
170
171 /* Get the first process in the process list, or NULL if the list is empty. */
172
173 process_info *
174 get_first_process (void)
175 {
176 if (!all_processes.empty ())
177 return all_processes.front ();
178 else
179 return NULL;
180 }
181
182 /* Return non-zero if there are any inferiors that we have created
183 (as opposed to attached-to). */
184
185 int
186 have_started_inferiors_p (void)
187 {
188 return find_process ([] (process_info *process) {
189 return !process->attached;
190 }) != NULL;
191 }
192
193 /* Return non-zero if there are any inferiors that we have attached to. */
194
195 int
196 have_attached_inferiors_p (void)
197 {
198 return find_process ([] (process_info *process) {
199 return process->attached;
200 }) != NULL;
201 }
202
203 struct process_info *
204 get_thread_process (const struct thread_info *thread)
205 {
206 return find_process_pid (thread->id.pid ());
207 }
208
209 struct process_info *
210 current_process (void)
211 {
212 gdb_assert (current_thread != NULL);
213 return get_thread_process (current_thread);
214 }
215
216 static void
217 do_restore_current_thread_cleanup (void *arg)
218 {
219 current_thread = (struct thread_info *) arg;
220 }
221
222 struct cleanup *
223 make_cleanup_restore_current_thread (void)
224 {
225 return make_cleanup (do_restore_current_thread_cleanup, current_thread);
226 }
227
228 /* See common/common-gdbthread.h. */
229
230 void
231 switch_to_thread (ptid_t ptid)
232 {
233 gdb_assert (ptid != minus_one_ptid);
234 current_thread = find_thread_ptid (ptid);
235 }
236
237 /* See common/common-inferior.h. */
238
239 const char *
240 get_inferior_cwd ()
241 {
242 return current_inferior_cwd;
243 }
244
245 /* See common/common-inferior.h. */
246
247 void
248 set_inferior_cwd (const char *cwd)
249 {
250 xfree ((void *) current_inferior_cwd);
251 if (cwd != NULL)
252 current_inferior_cwd = xstrdup (cwd);
253 else
254 current_inferior_cwd = NULL;
255 }
current_thread
struct thread_info * current_thread
Definition: inferiors.c:28
ptid_t::pid
constexpr int pid() const
Definition: ptid.h:53
add_process
struct process_info * add_process(int pid, int attached)
Definition: inferiors.c:140
xfree
static void xfree(T *ptr)
Definition: common-utils.h:54
thread_info::btrace
struct btrace_target_info * btrace
Definition: gdbthread.h:73
add_thread
struct thread_info * add_thread(ptid_t thread_id, void *target_data)
Definition: inferiors.c:34
clear_symbol_cache
void clear_symbol_cache(struct sym_cache **symcache_p)
Definition: remote-utils.c:1417
set_thread_regcache_data
void set_thread_regcache_data(struct thread_info *thread, struct regcache *data)
Definition: inferiors.c:123
set_inferior_cwd
void set_inferior_cwd(const char *cwd)
Definition: inferiors.c:248
ptid_of
static ptid_t ptid_of(const thread_info *thread)
Definition: gdbthread.h:206
get_first_thread
struct thread_info * get_first_thread(void)
Definition: inferiors.c:55
process_info
Definition: inferiors.h:34
clear_dlls
void clear_dlls(void)
Definition: dll.c:77
current_inferior_cwd
static const char * current_inferior_cwd
Definition: inferiors.c:31
find_thread
static thread_info * find_thread(Func func)
Definition: gdbthread.h:96
thread_regcache_data
struct regcache * thread_regcache_data(struct thread_info *thread)
Definition: inferiors.c:117
free_all_breakpoints
void free_all_breakpoints(struct process_info *proc)
Definition: mem-break.c:2117
cleanup
Definition: cleanups.c:38
have_attached_inferiors_p
int have_attached_inferiors_p(void)
Definition: inferiors.c:196
for_each_thread
static void for_each_thread(Func func)
Definition: gdbthread.h:142
get_first_process
process_info * get_first_process(void)
Definition: inferiors.c:174
process_info::attached
int attached
Definition: inferiors.h:45
free_one_thread
static void free_one_thread(thread_info *thread)
Definition: inferiors.c:91
regcache
Definition: regcache.h:31
thread_info::last_resume_kind
enum resume_kind last_resume_kind
Definition: gdbthread.h:39
xstrdup
char * xstrdup(const char *s)
Definition: utils.c:44
thread_info
Definition: gdbthread.h:30
thread_target_data
void * thread_target_data(struct thread_info *thread)
Definition: inferiors.c:111
make_cleanup_restore_current_thread
struct cleanup * make_cleanup_restore_current_thread(void)
Definition: inferiors.c:223
thread_info::target_data
void * target_data
Definition: gdbthread.h:35
find_process_pid
process_info * find_process_pid(int pid)
Definition: inferiors.c:164
ptid_t
Definition: ptid.h:35
find_thread_process
static struct thread_info * find_thread_process(const struct process_info *const process)
Definition: inferiors.c:75
all_processes
std::list< process_info * > all_processes
Definition: inferiors.c:25
do_restore_current_thread_cleanup
static void do_restore_current_thread_cleanup(void *arg)
Definition: inferiors.c:217
gdbthread.h
make_cleanup
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
free
void free(T *ptr)=delete
server.h
remove_process
void remove_process(struct process_info *process)
Definition: inferiors.c:154
current_process
struct process_info * current_process(void)
Definition: inferiors.c:210
thread_info::regcache_data
struct regcache * regcache_data
Definition: gdbthread.h:36
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
switch_to_thread
void switch_to_thread(ptid_t ptid)
Definition: inferiors.c:231
process_info::symbol_cache
struct sym_cache * symbol_cache
Definition: inferiors.h:52
thread_info::id
ptid_t id
Definition: gdbthread.h:33
process_info::pid
int pid
Definition: inferiors.h:41
process_info::process_info
process_info(int pid_, int attached_)
Definition: inferiors.h:36
XCNEW
#define XCNEW(T)
Definition: poison.h:121
find_any_thread_of_pid
struct thread_info * find_any_thread_of_pid(int pid)
Definition: inferiors.c:83
remove_thread
void remove_thread(struct thread_info *thread)
Definition: inferiors.c:98
thread_info::last_status
struct target_waitstatus last_status
Definition: gdbthread.h:42
have_started_inferiors_p
int have_started_inferiors_p(void)
Definition: inferiors.c:186
free_register_cache
void free_register_cache(struct regcache *regcache)
Definition: regcache.c:170
find_process
static process_info * find_process(Func func)
Definition: inferiors.h:112
get_inferior_cwd
const char * get_inferior_cwd()
Definition: inferiors.c:240
discard_queued_stop_replies
void discard_queued_stop_replies(ptid_t ptid)
Definition: server.c:198
dll.h
find_thread_ptid
struct thread_info * find_thread_ptid(ptid_t ptid)
Definition: inferiors.c:64
clear_inferiors
void clear_inferiors(void)
Definition: inferiors.c:129
get_thread_process
struct process_info * get_thread_process(const struct thread_info *thread)
Definition: inferiors.c:204
minus_one_ptid
ptid_t minus_one_ptid
Definition: ptid.c:26
all_threads
std::list< thread_info * > all_threads
Definition: inferiors.c:26
target_disable_btrace
#define target_disable_btrace(tinfo)
Definition: target.h:633