3 Copyright 1996-2024 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 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
137 SIM_ASSERT (STATE_MODULES (sd
) == NULL
);
139 STATE_MODULES (sd
) = ZALLOC (struct module_list
);
140 return sim_module_install_list (sd
, early_modules
, early_modules_len
);
143 /* Called after all modules have been installed and after argv
144 has been processed. */
147 sim_module_init (SIM_DESC sd
)
149 struct module_list
*modules
= STATE_MODULES (sd
);
150 MODULE_INIT_LIST
*modp
;
152 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
153 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
155 for (modp
= modules
->init_list
; modp
!= NULL
; modp
= modp
->next
)
157 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
163 /* Called when ever the simulator is resumed */
166 sim_module_resume (SIM_DESC sd
)
168 struct module_list
*modules
= STATE_MODULES (sd
);
169 MODULE_RESUME_LIST
*modp
;
171 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
172 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
174 for (modp
= modules
->resume_list
; modp
!= NULL
; modp
= modp
->next
)
176 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
182 /* Called when ever the simulator is suspended */
185 sim_module_suspend (SIM_DESC sd
)
187 struct module_list
*modules
= STATE_MODULES (sd
);
188 MODULE_SUSPEND_LIST
*modp
;
190 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
191 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
193 for (modp
= modules
->suspend_list
; modp
!= NULL
; modp
= modp
->next
)
195 if ((*modp
->fn
) (sd
) != SIM_RC_OK
)
201 /* Uninstall installed modules, called by sim_close. */
204 sim_module_uninstall (SIM_DESC sd
)
206 struct module_list
*modules
= STATE_MODULES (sd
);
207 MODULE_UNINSTALL_LIST
*modp
;
209 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
210 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
212 /* Uninstall the modules. */
213 for (modp
= modules
->uninstall_list
; modp
!= NULL
; modp
= modp
->next
)
216 /* clean-up init list */
218 MODULE_INIT_LIST
*n
, *d
;
219 for (d
= modules
->init_list
; d
!= NULL
; d
= n
)
226 /* clean-up resume list */
228 MODULE_RESUME_LIST
*n
, *d
;
229 for (d
= modules
->resume_list
; d
!= NULL
; d
= n
)
236 /* clean-up suspend list */
238 MODULE_SUSPEND_LIST
*n
, *d
;
239 for (d
= modules
->suspend_list
; d
!= NULL
; d
= n
)
246 /* clean-up uninstall list */
248 MODULE_UNINSTALL_LIST
*n
, *d
;
249 for (d
= modules
->uninstall_list
; d
!= NULL
; d
= n
)
256 /* clean-up info list */
258 MODULE_INFO_LIST
*n
, *d
;
259 for (d
= modules
->info_list
; d
!= NULL
; d
= n
)
267 STATE_MODULES (sd
) = NULL
;
270 /* Called when ever simulator info is needed */
273 sim_module_info (SIM_DESC sd
, bool verbose
)
275 struct module_list
*modules
= STATE_MODULES (sd
);
276 MODULE_INFO_LIST
*modp
;
278 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
279 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
281 for (modp
= modules
->info_list
; modp
!= NULL
; modp
= modp
->next
)
283 (*modp
->fn
) (sd
, verbose
);
287 /* Add FN to the init handler list.
288 init in the same order as the install. */
291 sim_module_add_init_fn (SIM_DESC sd
, MODULE_INIT_FN fn
)
293 struct module_list
*modules
= STATE_MODULES (sd
);
294 MODULE_INIT_LIST
*l
= ZALLOC (MODULE_INIT_LIST
);
295 MODULE_INIT_LIST
**last
;
297 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
298 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
300 last
= &modules
->init_list
;
301 while (*last
!= NULL
)
302 last
= &((*last
)->next
);
309 /* Add FN to the resume handler list.
310 resume in the same order as the install. */
313 sim_module_add_resume_fn (SIM_DESC sd
, MODULE_RESUME_FN fn
)
315 struct module_list
*modules
= STATE_MODULES (sd
);
316 MODULE_RESUME_LIST
*l
= ZALLOC (MODULE_RESUME_LIST
);
317 MODULE_RESUME_LIST
**last
;
319 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
320 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
322 last
= &modules
->resume_list
;
323 while (*last
!= NULL
)
324 last
= &((*last
)->next
);
331 /* Add FN to the init handler list.
332 suspend in the reverse order to install. */
335 sim_module_add_suspend_fn (SIM_DESC sd
, MODULE_SUSPEND_FN fn
)
337 struct module_list
*modules
= STATE_MODULES (sd
);
338 MODULE_SUSPEND_LIST
*l
= ZALLOC (MODULE_SUSPEND_LIST
);
339 MODULE_SUSPEND_LIST
**last
;
341 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
342 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
344 last
= &modules
->suspend_list
;
345 while (*last
!= NULL
)
346 last
= &((*last
)->next
);
349 l
->next
= modules
->suspend_list
;
350 modules
->suspend_list
= l
;
353 /* Add FN to the uninstall handler list.
354 Uninstall in reverse order to install. */
357 sim_module_add_uninstall_fn (SIM_DESC sd
, MODULE_UNINSTALL_FN fn
)
359 struct module_list
*modules
= STATE_MODULES (sd
);
360 MODULE_UNINSTALL_LIST
*l
= ZALLOC (MODULE_UNINSTALL_LIST
);
362 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
363 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
366 l
->next
= modules
->uninstall_list
;
367 modules
->uninstall_list
= l
;
370 /* Add FN to the info handler list.
371 Report info in the same order as the install. */
374 sim_module_add_info_fn (SIM_DESC sd
, MODULE_INFO_FN fn
)
376 struct module_list
*modules
= STATE_MODULES (sd
);
377 MODULE_INFO_LIST
*l
= ZALLOC (MODULE_INFO_LIST
);
378 MODULE_INFO_LIST
**last
;
380 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
381 SIM_ASSERT (STATE_MODULES (sd
) != NULL
);
383 last
= &modules
->info_list
;
384 while (*last
!= NULL
)
385 last
= &((*last
)->next
);