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