]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Module support. |
836cc9f4 | 2 | |
3666a048 | 3 | Copyright 1996-2021 Free Software Foundation, Inc. |
836cc9f4 | 4 | |
c906108c SS |
5 | Contributed by Cygnus Support. |
6 | ||
7 | This file is part of GDB, the GNU debugger. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
4744ac1b JB |
11 | the Free Software Foundation; either version 3 of the License, or |
12 | (at your option) any later version. | |
c906108c SS |
13 | |
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
4744ac1b JB |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
c906108c | 21 | |
6df01ab8 MF |
22 | /* This must come before any other includes. */ |
23 | #include "defs.h" | |
24 | ||
c906108c SS |
25 | #include "sim-main.h" |
26 | #include "sim-io.h" | |
27 | #include "sim-options.h" | |
28 | #include "sim-assert.h" | |
29 | ||
c906108c SS |
30 | #include "libiberty.h" |
31 | ||
b760fb3a SM |
32 | #include <stdlib.h> |
33 | ||
8e5f1516 MF |
34 | /* List of all early/core modules. |
35 | TODO: Should trim this list by converting to sim_install_* framework. */ | |
36 | static MODULE_INSTALL_FN * const early_modules[] = { | |
c906108c SS |
37 | standard_install, |
38 | sim_events_install, | |
c906108c | 39 | sim_model_install, |
c906108c | 40 | sim_engine_install, |
bffcfec8 | 41 | #if WITH_TRACE_ANY_P |
c906108c SS |
42 | trace_install, |
43 | #endif | |
44 | #if WITH_PROFILE | |
45 | profile_install, | |
46 | #endif | |
47 | sim_core_install, | |
c906108c | 48 | sim_memopt_install, |
c906108c | 49 | sim_watchpoint_install, |
c906108c SS |
50 | #if WITH_SCACHE |
51 | scache_install, | |
52 | #endif | |
c906108c | 53 | }; |
8e5f1516 MF |
54 | static int early_modules_len = ARRAY_SIZE (early_modules); |
55 | ||
56 | /* List of dynamically detected modules. Declared in generated modules.c. */ | |
57 | extern MODULE_INSTALL_FN * const sim_modules_detected[]; | |
58 | extern const int sim_modules_detected_len; | |
c906108c SS |
59 | \f |
60 | /* Functions called from sim_open. */ | |
61 | ||
62 | /* Initialize common parts before argument processing. */ | |
63 | ||
64 | SIM_RC | |
65 | sim_pre_argv_init (SIM_DESC sd, const char *myname) | |
66 | { | |
67 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
68 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
69 | ||
9950eccb | 70 | STATE_MY_NAME (sd) = lbasename (myname); |
c906108c SS |
71 | |
72 | /* Set the cpu names to default values. */ | |
73 | { | |
74 | int i; | |
75 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
76 | { | |
77 | char *name; | |
39a3ae0a MF |
78 | if (asprintf (&name, "cpu%d", i) < 0) |
79 | return SIM_RC_FAIL; | |
c906108c SS |
80 | CPU_NAME (STATE_CPU (sd, i)) = name; |
81 | } | |
82 | } | |
83 | ||
84 | sim_config_default (sd); | |
85 | ||
8e5f1516 | 86 | /* Install all early configured-in modules. */ |
c906108c SS |
87 | if (sim_module_install (sd) != SIM_RC_OK) |
88 | return SIM_RC_FAIL; | |
89 | ||
8e5f1516 MF |
90 | /* Install all remaining dynamically detected modules. */ |
91 | return sim_module_install_list (sd, sim_modules_detected, | |
92 | sim_modules_detected_len); | |
c906108c SS |
93 | } |
94 | ||
95 | /* Initialize common parts after argument processing. */ | |
96 | ||
97 | SIM_RC | |
98 | sim_post_argv_init (SIM_DESC sd) | |
99 | { | |
100 | int i; | |
101 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
102 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
103 | ||
104 | /* Set the cpu->state backlinks for each cpu. */ | |
105 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
106 | { | |
107 | CPU_STATE (STATE_CPU (sd, i)) = sd; | |
108 | CPU_INDEX (STATE_CPU (sd, i)) = i; | |
109 | } | |
110 | ||
111 | if (sim_module_init (sd) != SIM_RC_OK) | |
112 | return SIM_RC_FAIL; | |
113 | ||
114 | return SIM_RC_OK; | |
115 | } | |
116 | \f | |
8e5f1516 | 117 | /* Install a list of modules. |
c906108c | 118 | If this fails, no modules are left installed. */ |
c906108c | 119 | SIM_RC |
8e5f1516 MF |
120 | sim_module_install_list (SIM_DESC sd, MODULE_INSTALL_FN * const *modules, |
121 | size_t modules_len) | |
c906108c | 122 | { |
8e5f1516 | 123 | size_t i; |
c906108c | 124 | |
8e5f1516 | 125 | for (i = 0; i < modules_len; ++i) |
c906108c | 126 | { |
8e5f1516 MF |
127 | MODULE_INSTALL_FN *modp = modules[i]; |
128 | ||
129 | if (modp != NULL && modp (sd) != SIM_RC_OK) | |
c906108c SS |
130 | { |
131 | sim_module_uninstall (sd); | |
132 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
133 | return SIM_RC_FAIL; | |
134 | } | |
135 | } | |
8e5f1516 | 136 | |
c906108c SS |
137 | return SIM_RC_OK; |
138 | } | |
139 | ||
8e5f1516 MF |
140 | /* Install all modules. |
141 | If this fails, no modules are left installed. */ | |
142 | ||
143 | SIM_RC | |
144 | sim_module_install (SIM_DESC sd) | |
145 | { | |
146 | MODULE_INSTALL_FN * const *modp; | |
147 | ||
148 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
149 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
150 | ||
151 | STATE_MODULES (sd) = ZALLOC (struct module_list); | |
152 | return sim_module_install_list (sd, early_modules, early_modules_len); | |
153 | } | |
154 | ||
c906108c SS |
155 | /* Called after all modules have been installed and after argv |
156 | has been processed. */ | |
157 | ||
158 | SIM_RC | |
159 | sim_module_init (SIM_DESC sd) | |
160 | { | |
161 | struct module_list *modules = STATE_MODULES (sd); | |
162 | MODULE_INIT_LIST *modp; | |
163 | ||
164 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
165 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
166 | ||
167 | for (modp = modules->init_list; modp != NULL; modp = modp->next) | |
168 | { | |
169 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
170 | return SIM_RC_FAIL; | |
171 | } | |
172 | return SIM_RC_OK; | |
173 | } | |
174 | ||
175 | /* Called when ever the simulator is resumed */ | |
176 | ||
177 | SIM_RC | |
178 | sim_module_resume (SIM_DESC sd) | |
179 | { | |
180 | struct module_list *modules = STATE_MODULES (sd); | |
181 | MODULE_RESUME_LIST *modp; | |
182 | ||
183 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
184 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
185 | ||
186 | for (modp = modules->resume_list; modp != NULL; modp = modp->next) | |
187 | { | |
188 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
189 | return SIM_RC_FAIL; | |
190 | } | |
191 | return SIM_RC_OK; | |
192 | } | |
193 | ||
194 | /* Called when ever the simulator is suspended */ | |
195 | ||
196 | SIM_RC | |
197 | sim_module_suspend (SIM_DESC sd) | |
198 | { | |
199 | struct module_list *modules = STATE_MODULES (sd); | |
200 | MODULE_SUSPEND_LIST *modp; | |
201 | ||
202 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
203 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
204 | ||
205 | for (modp = modules->suspend_list; modp != NULL; modp = modp->next) | |
206 | { | |
207 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
208 | return SIM_RC_FAIL; | |
209 | } | |
210 | return SIM_RC_OK; | |
211 | } | |
212 | ||
213 | /* Uninstall installed modules, called by sim_close. */ | |
214 | ||
215 | void | |
216 | sim_module_uninstall (SIM_DESC sd) | |
217 | { | |
218 | struct module_list *modules = STATE_MODULES (sd); | |
219 | MODULE_UNINSTALL_LIST *modp; | |
220 | ||
221 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
222 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
223 | ||
224 | /* Uninstall the modules. */ | |
225 | for (modp = modules->uninstall_list; modp != NULL; modp = modp->next) | |
226 | (*modp->fn) (sd); | |
227 | ||
228 | /* clean-up init list */ | |
229 | { | |
230 | MODULE_INIT_LIST *n, *d; | |
231 | for (d = modules->init_list; d != NULL; d = n) | |
232 | { | |
233 | n = d->next; | |
d79fe0d6 | 234 | free (d); |
c906108c SS |
235 | } |
236 | } | |
237 | ||
238 | /* clean-up resume list */ | |
239 | { | |
240 | MODULE_RESUME_LIST *n, *d; | |
241 | for (d = modules->resume_list; d != NULL; d = n) | |
242 | { | |
243 | n = d->next; | |
d79fe0d6 | 244 | free (d); |
c906108c SS |
245 | } |
246 | } | |
247 | ||
248 | /* clean-up suspend list */ | |
249 | { | |
250 | MODULE_SUSPEND_LIST *n, *d; | |
251 | for (d = modules->suspend_list; d != NULL; d = n) | |
252 | { | |
253 | n = d->next; | |
d79fe0d6 | 254 | free (d); |
c906108c SS |
255 | } |
256 | } | |
257 | ||
258 | /* clean-up uninstall list */ | |
259 | { | |
260 | MODULE_UNINSTALL_LIST *n, *d; | |
261 | for (d = modules->uninstall_list; d != NULL; d = n) | |
262 | { | |
263 | n = d->next; | |
d79fe0d6 | 264 | free (d); |
c906108c SS |
265 | } |
266 | } | |
267 | ||
268 | /* clean-up info list */ | |
269 | { | |
270 | MODULE_INFO_LIST *n, *d; | |
271 | for (d = modules->info_list; d != NULL; d = n) | |
272 | { | |
273 | n = d->next; | |
d79fe0d6 | 274 | free (d); |
c906108c SS |
275 | } |
276 | } | |
277 | ||
d79fe0d6 | 278 | free (modules); |
c906108c SS |
279 | STATE_MODULES (sd) = NULL; |
280 | } | |
281 | ||
282 | /* Called when ever simulator info is needed */ | |
283 | ||
284 | void | |
285 | sim_module_info (SIM_DESC sd, int verbose) | |
286 | { | |
287 | struct module_list *modules = STATE_MODULES (sd); | |
288 | MODULE_INFO_LIST *modp; | |
289 | ||
290 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
291 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
292 | ||
293 | for (modp = modules->info_list; modp != NULL; modp = modp->next) | |
294 | { | |
295 | (*modp->fn) (sd, verbose); | |
296 | } | |
297 | } | |
298 | \f | |
299 | /* Add FN to the init handler list. | |
300 | init in the same order as the install. */ | |
301 | ||
302 | void | |
303 | sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn) | |
304 | { | |
305 | struct module_list *modules = STATE_MODULES (sd); | |
306 | MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST); | |
307 | MODULE_INIT_LIST **last; | |
308 | ||
309 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
310 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
311 | ||
312 | last = &modules->init_list; | |
313 | while (*last != NULL) | |
314 | last = &((*last)->next); | |
315 | ||
316 | l->fn = fn; | |
317 | l->next = NULL; | |
318 | *last = l; | |
319 | } | |
320 | ||
321 | /* Add FN to the resume handler list. | |
322 | resume in the same order as the install. */ | |
323 | ||
324 | void | |
325 | sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn) | |
326 | { | |
327 | struct module_list *modules = STATE_MODULES (sd); | |
328 | MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST); | |
329 | MODULE_RESUME_LIST **last; | |
330 | ||
331 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
332 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
333 | ||
334 | last = &modules->resume_list; | |
335 | while (*last != NULL) | |
336 | last = &((*last)->next); | |
337 | ||
338 | l->fn = fn; | |
339 | l->next = NULL; | |
340 | *last = l; | |
341 | } | |
342 | ||
343 | /* Add FN to the init handler list. | |
344 | suspend in the reverse order to install. */ | |
345 | ||
346 | void | |
347 | sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn) | |
348 | { | |
349 | struct module_list *modules = STATE_MODULES (sd); | |
350 | MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST); | |
351 | MODULE_SUSPEND_LIST **last; | |
352 | ||
353 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
354 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
355 | ||
356 | last = &modules->suspend_list; | |
357 | while (*last != NULL) | |
358 | last = &((*last)->next); | |
359 | ||
360 | l->fn = fn; | |
361 | l->next = modules->suspend_list; | |
362 | modules->suspend_list = l; | |
363 | } | |
364 | ||
365 | /* Add FN to the uninstall handler list. | |
366 | Uninstall in reverse order to install. */ | |
367 | ||
368 | void | |
369 | sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn) | |
370 | { | |
371 | struct module_list *modules = STATE_MODULES (sd); | |
372 | MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST); | |
373 | ||
374 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
375 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
376 | ||
377 | l->fn = fn; | |
378 | l->next = modules->uninstall_list; | |
379 | modules->uninstall_list = l; | |
380 | } | |
381 | ||
382 | /* Add FN to the info handler list. | |
383 | Report info in the same order as the install. */ | |
384 | ||
385 | void | |
386 | sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn) | |
387 | { | |
388 | struct module_list *modules = STATE_MODULES (sd); | |
389 | MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST); | |
390 | MODULE_INFO_LIST **last; | |
391 | ||
392 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
393 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
394 | ||
395 | last = &modules->info_list; | |
396 | while (*last != NULL) | |
397 | last = &((*last)->next); | |
398 | ||
399 | l->fn = fn; | |
400 | l->next = NULL; | |
401 | *last = l; | |
402 | } |