3 Copyright 1996-2023 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 "libiberty.h"
31 #include "sim-options.h"
32 #include "sim-assert.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
[] = {
42 sim_watchpoint_install
,
44 static int early_modules_len
= ARRAY_SIZE (early_modules
);
46 /* List of dynamically detected modules. Declared in generated modules.c. */
47 extern MODULE_INSTALL_FN
* const sim_modules_detected
[];
48 extern const int sim_modules_detected_len
;
50 /* Functions called from sim_open. */
52 /* Initialize common parts before argument processing. */
55 sim_pre_argv_init (SIM_DESC sd
, const char *myname
)
57 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
58 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
60 STATE_MY_NAME (sd
) = lbasename (myname
);
62 /* Set the cpu names to default values. */
65 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
68 if (asprintf (&name
, "cpu%d", i
) < 0)
70 CPU_NAME (STATE_CPU (sd
, i
)) = name
;
74 sim_config_default (sd
);
76 /* Install all early configured-in modules. */
77 if (sim_module_install (sd
) != SIM_RC_OK
)
80 /* Install all remaining dynamically detected modules. */
81 return sim_module_install_list (sd
, sim_modules_detected
,
82 sim_modules_detected_len
);
85 /* Initialize common parts after argument processing. */
88 sim_post_argv_init (SIM_DESC sd
)
91 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
92 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
94 /* Set the cpu->state backlinks for each cpu. */
95 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
97 CPU_STATE (STATE_CPU (sd
, i
)) = sd
;
98 CPU_INDEX (STATE_CPU (sd
, i
)) = i
;
101 if (sim_module_init (sd
) != SIM_RC_OK
)
107 /* Install a list of modules.
108 If this fails, no modules are left installed. */
110 sim_module_install_list (SIM_DESC sd
, MODULE_INSTALL_FN
* const *modules
,
115 for (i
= 0; i
< modules_len
; ++i
)
117 MODULE_INSTALL_FN
*modp
= modules
[i
];
119 if (modp
!= NULL
&& modp (sd
) != SIM_RC_OK
)
121 sim_module_uninstall (sd
);
122 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
130 /* Install all modules.
131 If this fails, no modules are left installed. */
134 sim_module_install (SIM_DESC sd
)
136 MODULE_INSTALL_FN
* const *modp
;
138 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
139 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
141 STATE_MODULES (sd
) = ZALLOC (struct module_list
);
142 return sim_module_install_list (sd
, early_modules
, early_modules_len
);
145 /* Called after all modules have been installed and after argv
146 has been processed. */
149 sim_module_init (SIM_DESC sd
)
151 struct module_list
*modules
= STATE_MODULES (sd
);
152 MODULE_INIT_LIST
*modp
;
154 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
155 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
157 for (modp
= modules
->init_list
; modp
!= NULL
; modp
= modp
->next
)
159 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
165 /* Called when ever the simulator is resumed */
168 sim_module_resume (SIM_DESC sd
)
170 struct module_list
*modules
= STATE_MODULES (sd
);
171 MODULE_RESUME_LIST
*modp
;
173 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
174 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
176 for (modp
= modules
->resume_list
; modp
!= NULL
; modp
= modp
->next
)
178 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
184 /* Called when ever the simulator is suspended */
187 sim_module_suspend (SIM_DESC sd
)
189 struct module_list
*modules
= STATE_MODULES (sd
);
190 MODULE_SUSPEND_LIST
*modp
;
192 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
193 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
195 for (modp
= modules
->suspend_list
; modp
!= NULL
; modp
= modp
->next
)
197 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
203 /* Uninstall installed modules, called by sim_close. */
206 sim_module_uninstall (SIM_DESC sd
)
208 struct module_list
*modules
= STATE_MODULES (sd
);
209 MODULE_UNINSTALL_LIST
*modp
;
211 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
212 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
214 /* Uninstall the modules. */
215 for (modp
= modules
->uninstall_list
; modp
!= NULL
; modp
= modp
->next
)
218 /* clean-up init list */
220 MODULE_INIT_LIST
*n
, *d
;
221 for (d
= modules
->init_list
; d
!= NULL
; d
= n
)
228 /* clean-up resume list */
230 MODULE_RESUME_LIST
*n
, *d
;
231 for (d
= modules
->resume_list
; d
!= NULL
; d
= n
)
238 /* clean-up suspend list */
240 MODULE_SUSPEND_LIST
*n
, *d
;
241 for (d
= modules
->suspend_list
; d
!= NULL
; d
= n
)
248 /* clean-up uninstall list */
250 MODULE_UNINSTALL_LIST
*n
, *d
;
251 for (d
= modules
->uninstall_list
; d
!= NULL
; d
= n
)
258 /* clean-up info list */
260 MODULE_INFO_LIST
*n
, *d
;
261 for (d
= modules
->info_list
; d
!= NULL
; d
= n
)
269 STATE_MODULES (sd
) = NULL
;
272 /* Called when ever simulator info is needed */
275 sim_module_info (SIM_DESC sd
, bool verbose
)
277 struct module_list
*modules
= STATE_MODULES (sd
);
278 MODULE_INFO_LIST
*modp
;
280 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
281 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
283 for (modp
= modules
->info_list
; modp
!= NULL
; modp
= modp
->next
)
285 (*modp
->fn
) (sd
, verbose
);
289 /* Add FN to the init handler list.
290 init in the same order as the install. */
293 sim_module_add_init_fn (SIM_DESC sd
, MODULE_INIT_FN fn
)
295 struct module_list
*modules
= STATE_MODULES (sd
);
296 MODULE_INIT_LIST
*l
= ZALLOC (MODULE_INIT_LIST
);
297 MODULE_INIT_LIST
**last
;
299 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
300 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
302 last
= &modules
->init_list
;
303 while (*last
!= NULL
)
304 last
= &((*last
)->next
);
311 /* Add FN to the resume handler list.
312 resume in the same order as the install. */
315 sim_module_add_resume_fn (SIM_DESC sd
, MODULE_RESUME_FN fn
)
317 struct module_list
*modules
= STATE_MODULES (sd
);
318 MODULE_RESUME_LIST
*l
= ZALLOC (MODULE_RESUME_LIST
);
319 MODULE_RESUME_LIST
**last
;
321 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
322 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
324 last
= &modules
->resume_list
;
325 while (*last
!= NULL
)
326 last
= &((*last
)->next
);
333 /* Add FN to the init handler list.
334 suspend in the reverse order to install. */
337 sim_module_add_suspend_fn (SIM_DESC sd
, MODULE_SUSPEND_FN fn
)
339 struct module_list
*modules
= STATE_MODULES (sd
);
340 MODULE_SUSPEND_LIST
*l
= ZALLOC (MODULE_SUSPEND_LIST
);
341 MODULE_SUSPEND_LIST
**last
;
343 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
344 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
346 last
= &modules
->suspend_list
;
347 while (*last
!= NULL
)
348 last
= &((*last
)->next
);
351 l
->next
= modules
->suspend_list
;
352 modules
->suspend_list
= l
;
355 /* Add FN to the uninstall handler list.
356 Uninstall in reverse order to install. */
359 sim_module_add_uninstall_fn (SIM_DESC sd
, MODULE_UNINSTALL_FN fn
)
361 struct module_list
*modules
= STATE_MODULES (sd
);
362 MODULE_UNINSTALL_LIST
*l
= ZALLOC (MODULE_UNINSTALL_LIST
);
364 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
365 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
368 l
->next
= modules
->uninstall_list
;
369 modules
->uninstall_list
= l
;
372 /* Add FN to the info handler list.
373 Report info in the same order as the install. */
376 sim_module_add_info_fn (SIM_DESC sd
, MODULE_INFO_FN fn
)
378 struct module_list
*modules
= STATE_MODULES (sd
);
379 MODULE_INFO_LIST
*l
= ZALLOC (MODULE_INFO_LIST
);
380 MODULE_INFO_LIST
**last
;
382 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
383 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
385 last
= &modules
->info_list
;
386 while (*last
!= NULL
)
387 last
= &((*last
)->next
);