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