Commit | Line | Data |
---|---|---|
ce3a066d | 1 | /* Inferior process information for the remote server for GDB. |
61baf725 | 2 | Copyright (C) 2002-2017 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 | 25 | std::list<process_info *> all_processes; |
9c80ecd6 | 26 | std::list<thread_info *> all_threads; |
0d62e5e8 | 27 | |
0bfdf32f | 28 | struct thread_info *current_thread; |
0d62e5e8 | 29 | |
d092c5a2 SDJ |
30 | /* The current working directory used to start the inferior. */ |
31 | static const char *current_inferior_cwd = NULL; | |
32 | ||
9c80ecd6 SM |
33 | thread_info * |
34 | find_inferior (std::list<thread_info *> *thread_list, | |
35 | int (*func) (thread_info *, void *), | |
36 | void *arg) | |
0d62e5e8 | 37 | { |
9c80ecd6 | 38 | gdb_assert (thread_list == &all_threads); |
0d62e5e8 | 39 | |
9c80ecd6 SM |
40 | return find_thread ([&] (thread_info *thread) { |
41 | return func (thread, arg); | |
42 | }); | |
43 | } | |
9f767825 | 44 | |
9c80ecd6 SM |
45 | thread_info * |
46 | find_inferior_in_random (std::list<thread_info *> *thread_list, | |
47 | int (*func) (thread_info *, void *), | |
48 | void *arg) | |
649ebbca | 49 | { |
9c80ecd6 SM |
50 | gdb_assert (thread_list == &all_threads); |
51 | ||
52 | return find_thread_in_random ([&] (thread_info *thread) { | |
53 | return func (thread, arg); | |
54 | }); | |
649ebbca DE |
55 | } |
56 | ||
0d62e5e8 | 57 | void |
9c80ecd6 SM |
58 | for_each_inferior (std::list<thread_info *> *thread_list, |
59 | void (*action) (thread_info *)) | |
0d62e5e8 | 60 | { |
9c80ecd6 | 61 | gdb_assert (thread_list == &all_threads); |
0d62e5e8 | 62 | |
9c80ecd6 SM |
63 | for_each_thread ([&] (thread_info *thread) { |
64 | action (thread); | |
65 | }); | |
66 | } | |
ce3a066d | 67 | |
9c80ecd6 SM |
68 | void |
69 | for_each_inferior_with_data (std::list<thread_info *> *thread_list, | |
70 | void (*action) (thread_info *, void *), | |
71 | void *data) | |
72 | { | |
73 | gdb_assert (thread_list == &all_threads); | |
0d62e5e8 | 74 | |
9c80ecd6 SM |
75 | for_each_thread ([&] (thread_info *thread) { |
76 | action (thread, data); | |
77 | }); | |
0d62e5e8 DJ |
78 | } |
79 | ||
f7667f0d | 80 | struct thread_info * |
95954743 | 81 | add_thread (ptid_t thread_id, void *target_data) |
0d62e5e8 | 82 | { |
8d749320 | 83 | struct thread_info *new_thread = XCNEW (struct thread_info); |
0d62e5e8 | 84 | |
9c80ecd6 | 85 | new_thread->id = thread_id; |
8336d594 | 86 | new_thread->last_resume_kind = resume_continue; |
fc7238bb | 87 | new_thread->last_status.kind = TARGET_WAITKIND_IGNORE; |
0d62e5e8 | 88 | |
9c80ecd6 | 89 | all_threads.push_back (new_thread); |
255e7678 | 90 | |
0bfdf32f GB |
91 | if (current_thread == NULL) |
92 | current_thread = new_thread; | |
ce3a066d | 93 | |
0d62e5e8 | 94 | new_thread->target_data = target_data; |
f7667f0d DE |
95 | |
96 | return new_thread; | |
a06660f7 DJ |
97 | } |
98 | ||
9c80ecd6 | 99 | /* See gdbthread.h. */ |
649ebbca | 100 | |
dae5f5cf | 101 | struct thread_info * |
649ebbca | 102 | get_first_thread (void) |
a06660f7 | 103 | { |
9c80ecd6 SM |
104 | if (!all_threads.empty ()) |
105 | return all_threads.front (); | |
106 | else | |
107 | return NULL; | |
649ebbca | 108 | } |
a06660f7 | 109 | |
649ebbca DE |
110 | struct thread_info * |
111 | find_thread_ptid (ptid_t ptid) | |
112 | { | |
8dc7b443 SM |
113 | return find_thread ([&] (thread_info *thread) { |
114 | return thread->id == ptid; | |
115 | }); | |
dae5f5cf DJ |
116 | } |
117 | ||
96e7a1eb AR |
118 | /* Find a thread associated with the given PROCESS, or NULL if no |
119 | such thread exists. */ | |
120 | ||
121 | static struct thread_info * | |
122 | find_thread_process (const struct process_info *const process) | |
123 | { | |
9179355e | 124 | return find_any_thread_of_pid (process->pid); |
96e7a1eb AR |
125 | } |
126 | ||
34c65914 PA |
127 | /* See gdbthread.h. */ |
128 | ||
129 | struct thread_info * | |
130 | find_any_thread_of_pid (int pid) | |
131 | { | |
4d3bb80e SM |
132 | return find_thread (pid, [] (thread_info *thread) { |
133 | return true; | |
134 | }); | |
34c65914 PA |
135 | } |
136 | ||
0d62e5e8 | 137 | static void |
9c80ecd6 | 138 | free_one_thread (thread_info *thread) |
0d62e5e8 | 139 | { |
6afd337d | 140 | free_register_cache (thread_regcache_data (thread)); |
0d62e5e8 DJ |
141 | free (thread); |
142 | } | |
143 | ||
144 | void | |
145 | remove_thread (struct thread_info *thread) | |
146 | { | |
9accd112 MM |
147 | if (thread->btrace != NULL) |
148 | target_disable_btrace (thread->btrace); | |
149 | ||
465a859e | 150 | discard_queued_stop_replies (ptid_of (thread)); |
9c80ecd6 SM |
151 | all_threads.remove (thread); |
152 | free_one_thread (thread); | |
96e7a1eb AR |
153 | if (current_thread == thread) |
154 | current_thread = NULL; | |
ce3a066d DJ |
155 | } |
156 | ||
611cb4a5 | 157 | void * |
6afd337d | 158 | thread_target_data (struct thread_info *thread) |
611cb4a5 | 159 | { |
6afd337d | 160 | return thread->target_data; |
611cb4a5 DJ |
161 | } |
162 | ||
a44892be | 163 | struct regcache * |
6afd337d | 164 | thread_regcache_data (struct thread_info *thread) |
c04a1aa8 | 165 | { |
6afd337d | 166 | return thread->regcache_data; |
c04a1aa8 DJ |
167 | } |
168 | ||
169 | void | |
6afd337d | 170 | set_thread_regcache_data (struct thread_info *thread, struct regcache *data) |
c04a1aa8 | 171 | { |
6afd337d | 172 | thread->regcache_data = data; |
c04a1aa8 | 173 | } |
255e7678 | 174 | |
255e7678 DJ |
175 | void |
176 | clear_inferiors (void) | |
177 | { | |
178 | for_each_inferior (&all_threads, free_one_thread); | |
9c80ecd6 | 179 | all_threads.clear (); |
bf4c19f7 YQ |
180 | |
181 | clear_dlls (); | |
7284e1be | 182 | |
0bfdf32f | 183 | current_thread = NULL; |
255e7678 | 184 | } |
24a09b5f | 185 | |
95954743 PA |
186 | struct process_info * |
187 | add_process (int pid, int attached) | |
188 | { | |
f27866ba | 189 | process_info *process = new process_info (pid, attached); |
95954743 | 190 | |
9179355e | 191 | all_processes.push_back (process); |
95954743 PA |
192 | |
193 | return process; | |
194 | } | |
195 | ||
5091eb23 DE |
196 | /* Remove a process from the common process list and free the memory |
197 | allocated for it. | |
198 | The caller is responsible for freeing private data first. */ | |
199 | ||
95954743 PA |
200 | void |
201 | remove_process (struct process_info *process) | |
202 | { | |
203 | clear_symbol_cache (&process->symbol_cache); | |
204 | free_all_breakpoints (process); | |
96e7a1eb | 205 | gdb_assert (find_thread_process (process) == NULL); |
9179355e | 206 | all_processes.remove (process); |
f27866ba | 207 | delete process; |
95954743 PA |
208 | } |
209 | ||
9179355e | 210 | process_info * |
95954743 PA |
211 | find_process_pid (int pid) |
212 | { | |
9179355e SM |
213 | return find_process ([&] (process_info *process) { |
214 | return process->pid == pid; | |
215 | }); | |
95954743 PA |
216 | } |
217 | ||
9179355e | 218 | /* Get the first process in the process list, or NULL if the list is empty. */ |
3d40fbb5 | 219 | |
9179355e | 220 | process_info * |
3d40fbb5 PA |
221 | get_first_process (void) |
222 | { | |
9179355e SM |
223 | if (!all_processes.empty ()) |
224 | return all_processes.front (); | |
225 | else | |
226 | return NULL; | |
9f767825 DE |
227 | } |
228 | ||
229 | /* Return non-zero if there are any inferiors that we have created | |
230 | (as opposed to attached-to). */ | |
231 | ||
232 | int | |
233 | have_started_inferiors_p (void) | |
234 | { | |
9179355e SM |
235 | return find_process ([] (process_info *process) { |
236 | return !process->attached; | |
237 | }) != NULL; | |
9f767825 DE |
238 | } |
239 | ||
240 | /* Return non-zero if there are any inferiors that we have attached to. */ | |
241 | ||
242 | int | |
243 | have_attached_inferiors_p (void) | |
244 | { | |
9179355e SM |
245 | return find_process ([] (process_info *process) { |
246 | return process->attached; | |
247 | }) != NULL; | |
9f767825 DE |
248 | } |
249 | ||
7fe519cb | 250 | struct process_info * |
63c40ec7 | 251 | get_thread_process (const struct thread_info *thread) |
95954743 | 252 | { |
9c80ecd6 | 253 | return find_process_pid (thread->id.pid ()); |
95954743 PA |
254 | } |
255 | ||
256 | struct process_info * | |
257 | current_process (void) | |
258 | { | |
0bfdf32f GB |
259 | gdb_assert (current_thread != NULL); |
260 | return get_thread_process (current_thread); | |
95954743 | 261 | } |
984a2c04 YQ |
262 | |
263 | static void | |
264 | do_restore_current_thread_cleanup (void *arg) | |
265 | { | |
266 | current_thread = (struct thread_info *) arg; | |
267 | } | |
268 | ||
269 | struct cleanup * | |
270 | make_cleanup_restore_current_thread (void) | |
271 | { | |
272 | return make_cleanup (do_restore_current_thread_cleanup, current_thread); | |
273 | } | |
043a4934 SDJ |
274 | |
275 | /* See common/common-gdbthread.h. */ | |
276 | ||
277 | void | |
278 | switch_to_thread (ptid_t ptid) | |
279 | { | |
75352e28 SDJ |
280 | gdb_assert (ptid != minus_one_ptid); |
281 | current_thread = find_thread_ptid (ptid); | |
043a4934 | 282 | } |
d092c5a2 SDJ |
283 | |
284 | /* See common/common-inferior.h. */ | |
285 | ||
286 | const char * | |
287 | get_inferior_cwd () | |
288 | { | |
289 | return current_inferior_cwd; | |
290 | } | |
bc3b087d SDJ |
291 | |
292 | /* See common/common-inferior.h. */ | |
293 | ||
294 | void | |
295 | set_inferior_cwd (const char *cwd) | |
296 | { | |
297 | xfree ((void *) current_inferior_cwd); | |
298 | if (cwd != NULL) | |
299 | current_inferior_cwd = xstrdup (cwd); | |
300 | else | |
301 | current_inferior_cwd = NULL; | |
302 | } |