]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-module.c
3 Copyright 1996-2021 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
7 This file is part of GDB, the GNU debugger.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
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/>. */
22 /* This must come before any other includes. */
27 #include "sim-options.h"
28 #include "sim-assert.h"
30 #include "libiberty.h"
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
[] = {
49 sim_watchpoint_install
,
54 static int early_modules_len
= ARRAY_SIZE (early_modules
);
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
;
60 /* Functions called from sim_open. */
62 /* Initialize common parts before argument processing. */
65 sim_pre_argv_init (SIM_DESC sd
, const char *myname
)
67 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
68 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
70 STATE_MY_NAME (sd
) = lbasename (myname
);
72 /* Set the cpu names to default values. */
75 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
78 if (asprintf (&name
, "cpu%d", i
) < 0)
80 CPU_NAME (STATE_CPU (sd
, i
)) = name
;
84 sim_config_default (sd
);
86 /* Install all early configured-in modules. */
87 if (sim_module_install (sd
) != SIM_RC_OK
)
90 /* Install all remaining dynamically detected modules. */
91 return sim_module_install_list (sd
, sim_modules_detected
,
92 sim_modules_detected_len
);
95 /* Initialize common parts after argument processing. */
98 sim_post_argv_init (SIM_DESC sd
)
101 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
102 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
104 /* Set the cpu->state backlinks for each cpu. */
105 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
107 CPU_STATE (STATE_CPU (sd
, i
)) = sd
;
108 CPU_INDEX (STATE_CPU (sd
, i
)) = i
;
111 if (sim_module_init (sd
) != SIM_RC_OK
)
117 /* Install a list of modules.
118 If this fails, no modules are left installed. */
120 sim_module_install_list (SIM_DESC sd
, MODULE_INSTALL_FN
* const *modules
,
125 for (i
= 0; i
< modules_len
; ++i
)
127 MODULE_INSTALL_FN
*modp
= modules
[i
];
129 if (modp
!= NULL
&& modp (sd
) != SIM_RC_OK
)
131 sim_module_uninstall (sd
);
132 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
140 /* Install all modules.
141 If this fails, no modules are left installed. */
144 sim_module_install (SIM_DESC sd
)
146 MODULE_INSTALL_FN
* const *modp
;
148 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
149 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
151 STATE_MODULES (sd
) = ZALLOC (struct module_list
);
152 return sim_module_install_list (sd
, early_modules
, early_modules_len
);
155 /* Called after all modules have been installed and after argv
156 has been processed. */
159 sim_module_init (SIM_DESC sd
)
161 struct module_list
*modules
= STATE_MODULES (sd
);
162 MODULE_INIT_LIST
*modp
;
164 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
165 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
167 for (modp
= modules
->init_list
; modp
!= NULL
; modp
= modp
->next
)
169 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
175 /* Called when ever the simulator is resumed */
178 sim_module_resume (SIM_DESC sd
)
180 struct module_list
*modules
= STATE_MODULES (sd
);
181 MODULE_RESUME_LIST
*modp
;
183 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
184 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
186 for (modp
= modules
->resume_list
; modp
!= NULL
; modp
= modp
->next
)
188 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
194 /* Called when ever the simulator is suspended */
197 sim_module_suspend (SIM_DESC sd
)
199 struct module_list
*modules
= STATE_MODULES (sd
);
200 MODULE_SUSPEND_LIST
*modp
;
202 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
203 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
205 for (modp
= modules
->suspend_list
; modp
!= NULL
; modp
= modp
->next
)
207 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
213 /* Uninstall installed modules, called by sim_close. */
216 sim_module_uninstall (SIM_DESC sd
)
218 struct module_list
*modules
= STATE_MODULES (sd
);
219 MODULE_UNINSTALL_LIST
*modp
;
221 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
222 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
224 /* Uninstall the modules. */
225 for (modp
= modules
->uninstall_list
; modp
!= NULL
; modp
= modp
->next
)
228 /* clean-up init list */
230 MODULE_INIT_LIST
*n
, *d
;
231 for (d
= modules
->init_list
; d
!= NULL
; d
= n
)
238 /* clean-up resume list */
240 MODULE_RESUME_LIST
*n
, *d
;
241 for (d
= modules
->resume_list
; d
!= NULL
; d
= n
)
248 /* clean-up suspend list */
250 MODULE_SUSPEND_LIST
*n
, *d
;
251 for (d
= modules
->suspend_list
; d
!= NULL
; d
= n
)
258 /* clean-up uninstall list */
260 MODULE_UNINSTALL_LIST
*n
, *d
;
261 for (d
= modules
->uninstall_list
; d
!= NULL
; d
= n
)
268 /* clean-up info list */
270 MODULE_INFO_LIST
*n
, *d
;
271 for (d
= modules
->info_list
; d
!= NULL
; d
= n
)
279 STATE_MODULES (sd
) = NULL
;
282 /* Called when ever simulator info is needed */
285 sim_module_info (SIM_DESC sd
, int verbose
)
287 struct module_list
*modules
= STATE_MODULES (sd
);
288 MODULE_INFO_LIST
*modp
;
290 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
291 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
293 for (modp
= modules
->info_list
; modp
!= NULL
; modp
= modp
->next
)
295 (*modp
->fn
) (sd
, verbose
);
299 /* Add FN to the init handler list.
300 init in the same order as the install. */
303 sim_module_add_init_fn (SIM_DESC sd
, MODULE_INIT_FN fn
)
305 struct module_list
*modules
= STATE_MODULES (sd
);
306 MODULE_INIT_LIST
*l
= ZALLOC (MODULE_INIT_LIST
);
307 MODULE_INIT_LIST
**last
;
309 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
310 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
312 last
= &modules
->init_list
;
313 while (*last
!= NULL
)
314 last
= &((*last
)->next
);
321 /* Add FN to the resume handler list.
322 resume in the same order as the install. */
325 sim_module_add_resume_fn (SIM_DESC sd
, MODULE_RESUME_FN fn
)
327 struct module_list
*modules
= STATE_MODULES (sd
);
328 MODULE_RESUME_LIST
*l
= ZALLOC (MODULE_RESUME_LIST
);
329 MODULE_RESUME_LIST
**last
;
331 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
332 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
334 last
= &modules
->resume_list
;
335 while (*last
!= NULL
)
336 last
= &((*last
)->next
);
343 /* Add FN to the init handler list.
344 suspend in the reverse order to install. */
347 sim_module_add_suspend_fn (SIM_DESC sd
, MODULE_SUSPEND_FN fn
)
349 struct module_list
*modules
= STATE_MODULES (sd
);
350 MODULE_SUSPEND_LIST
*l
= ZALLOC (MODULE_SUSPEND_LIST
);
351 MODULE_SUSPEND_LIST
**last
;
353 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
354 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
356 last
= &modules
->suspend_list
;
357 while (*last
!= NULL
)
358 last
= &((*last
)->next
);
361 l
->next
= modules
->suspend_list
;
362 modules
->suspend_list
= l
;
365 /* Add FN to the uninstall handler list.
366 Uninstall in reverse order to install. */
369 sim_module_add_uninstall_fn (SIM_DESC sd
, MODULE_UNINSTALL_FN fn
)
371 struct module_list
*modules
= STATE_MODULES (sd
);
372 MODULE_UNINSTALL_LIST
*l
= ZALLOC (MODULE_UNINSTALL_LIST
);
374 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
375 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
378 l
->next
= modules
->uninstall_list
;
379 modules
->uninstall_list
= l
;
382 /* Add FN to the info handler list.
383 Report info in the same order as the install. */
386 sim_module_add_info_fn (SIM_DESC sd
, MODULE_INFO_FN fn
)
388 struct module_list
*modules
= STATE_MODULES (sd
);
389 MODULE_INFO_LIST
*l
= ZALLOC (MODULE_INFO_LIST
);
390 MODULE_INFO_LIST
**last
;
392 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
393 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
395 last
= &modules
->info_list
;
396 while (*last
!= NULL
)
397 last
= &((*last
)->next
);