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