No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / sim / common / sim-module.c
blob8f01723318526860ccf93029129d8e958851b17c
1 /* Module support.
3 Copyright 1996, 1997, 1998, 2003 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 2, or (at your option)
12 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 along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "sim-main.h"
24 #include "sim-io.h"
25 #include "sim-options.h"
26 #include "sim-assert.h"
28 #if WITH_HW
29 #include "sim-hw.h"
30 #endif
32 #include "libiberty.h"
34 /* List of all modules. */
35 static MODULE_INSTALL_FN * const modules[] = {
36 standard_install,
37 sim_events_install,
38 #ifdef SIM_HAVE_MODEL
39 sim_model_install,
40 #endif
41 #if WITH_ENGINE
42 sim_engine_install,
43 #endif
44 #if WITH_TRACE
45 trace_install,
46 #endif
47 #if WITH_PROFILE
48 profile_install,
49 #endif
50 sim_core_install,
51 #ifndef SIM_HAVE_FLATMEM
52 /* FIXME: should handle flatmem as well FLATMEM */
53 sim_memopt_install,
54 #endif
55 #if WITH_WATCHPOINTS
56 sim_watchpoint_install,
57 #endif
58 #if WITH_SCACHE
59 scache_install,
60 #endif
61 #if WITH_HW
62 sim_hw_install,
63 #endif
64 /* Configured in [simulator specific] additional modules. */
65 #ifdef MODULE_LIST
66 MODULE_LIST
67 #endif
71 /* Functions called from sim_open. */
73 /* Initialize common parts before argument processing. */
75 SIM_RC
76 sim_pre_argv_init (SIM_DESC sd, const char *myname)
78 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
79 SIM_ASSERT (STATE_MODULES (sd) == NULL);
81 STATE_MY_NAME (sd) = myname + strlen (myname);
82 while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/')
83 --STATE_MY_NAME (sd);
85 /* Set the cpu names to default values. */
87 int i;
88 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
90 char *name;
91 asprintf (&name, "cpu%d", i);
92 CPU_NAME (STATE_CPU (sd, i)) = name;
96 sim_config_default (sd);
98 /* Install all configured in modules. */
99 if (sim_module_install (sd) != SIM_RC_OK)
100 return SIM_RC_FAIL;
102 return SIM_RC_OK;
105 /* Initialize common parts after argument processing. */
107 SIM_RC
108 sim_post_argv_init (SIM_DESC sd)
110 int i;
111 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
112 SIM_ASSERT (STATE_MODULES (sd) != NULL);
114 /* Set the cpu->state backlinks for each cpu. */
115 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
117 CPU_STATE (STATE_CPU (sd, i)) = sd;
118 CPU_INDEX (STATE_CPU (sd, i)) = i;
121 if (sim_module_init (sd) != SIM_RC_OK)
122 return SIM_RC_FAIL;
124 return SIM_RC_OK;
127 /* Install all modules.
128 If this fails, no modules are left installed. */
130 SIM_RC
131 sim_module_install (SIM_DESC sd)
133 MODULE_INSTALL_FN * const *modp;
135 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
136 SIM_ASSERT (STATE_MODULES (sd) == NULL);
138 STATE_MODULES (sd) = ZALLOC (struct module_list);
139 for (modp = modules; *modp != NULL; ++modp)
141 if ((*modp) (sd) != SIM_RC_OK)
143 sim_module_uninstall (sd);
144 SIM_ASSERT (STATE_MODULES (sd) == NULL);
145 return SIM_RC_FAIL;
148 return SIM_RC_OK;
151 /* Called after all modules have been installed and after argv
152 has been processed. */
154 SIM_RC
155 sim_module_init (SIM_DESC sd)
157 struct module_list *modules = STATE_MODULES (sd);
158 MODULE_INIT_LIST *modp;
160 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
161 SIM_ASSERT (STATE_MODULES (sd) != NULL);
163 for (modp = modules->init_list; modp != NULL; modp = modp->next)
165 if ((*modp->fn) (sd) != SIM_RC_OK)
166 return SIM_RC_FAIL;
168 return SIM_RC_OK;
171 /* Called when ever the simulator is resumed */
173 SIM_RC
174 sim_module_resume (SIM_DESC sd)
176 struct module_list *modules = STATE_MODULES (sd);
177 MODULE_RESUME_LIST *modp;
179 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
180 SIM_ASSERT (STATE_MODULES (sd) != NULL);
182 for (modp = modules->resume_list; modp != NULL; modp = modp->next)
184 if ((*modp->fn) (sd) != SIM_RC_OK)
185 return SIM_RC_FAIL;
187 return SIM_RC_OK;
190 /* Called when ever the simulator is suspended */
192 SIM_RC
193 sim_module_suspend (SIM_DESC sd)
195 struct module_list *modules = STATE_MODULES (sd);
196 MODULE_SUSPEND_LIST *modp;
198 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
199 SIM_ASSERT (STATE_MODULES (sd) != NULL);
201 for (modp = modules->suspend_list; modp != NULL; modp = modp->next)
203 if ((*modp->fn) (sd) != SIM_RC_OK)
204 return SIM_RC_FAIL;
206 return SIM_RC_OK;
209 /* Uninstall installed modules, called by sim_close. */
211 void
212 sim_module_uninstall (SIM_DESC sd)
214 struct module_list *modules = STATE_MODULES (sd);
215 MODULE_UNINSTALL_LIST *modp;
217 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
218 SIM_ASSERT (STATE_MODULES (sd) != NULL);
220 /* Uninstall the modules. */
221 for (modp = modules->uninstall_list; modp != NULL; modp = modp->next)
222 (*modp->fn) (sd);
224 /* clean-up init list */
226 MODULE_INIT_LIST *n, *d;
227 for (d = modules->init_list; d != NULL; d = n)
229 n = d->next;
230 zfree (d);
234 /* clean-up resume list */
236 MODULE_RESUME_LIST *n, *d;
237 for (d = modules->resume_list; d != NULL; d = n)
239 n = d->next;
240 zfree (d);
244 /* clean-up suspend list */
246 MODULE_SUSPEND_LIST *n, *d;
247 for (d = modules->suspend_list; d != NULL; d = n)
249 n = d->next;
250 zfree (d);
254 /* clean-up uninstall list */
256 MODULE_UNINSTALL_LIST *n, *d;
257 for (d = modules->uninstall_list; d != NULL; d = n)
259 n = d->next;
260 zfree (d);
264 /* clean-up info list */
266 MODULE_INFO_LIST *n, *d;
267 for (d = modules->info_list; d != NULL; d = n)
269 n = d->next;
270 zfree (d);
274 zfree (modules);
275 STATE_MODULES (sd) = NULL;
278 /* Called when ever simulator info is needed */
280 void
281 sim_module_info (SIM_DESC sd, int verbose)
283 struct module_list *modules = STATE_MODULES (sd);
284 MODULE_INFO_LIST *modp;
286 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
287 SIM_ASSERT (STATE_MODULES (sd) != NULL);
289 for (modp = modules->info_list; modp != NULL; modp = modp->next)
291 (*modp->fn) (sd, verbose);
295 /* Add FN to the init handler list.
296 init in the same order as the install. */
298 void
299 sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn)
301 struct module_list *modules = STATE_MODULES (sd);
302 MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST);
303 MODULE_INIT_LIST **last;
305 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
306 SIM_ASSERT (STATE_MODULES (sd) != NULL);
308 last = &modules->init_list;
309 while (*last != NULL)
310 last = &((*last)->next);
312 l->fn = fn;
313 l->next = NULL;
314 *last = l;
317 /* Add FN to the resume handler list.
318 resume in the same order as the install. */
320 void
321 sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn)
323 struct module_list *modules = STATE_MODULES (sd);
324 MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST);
325 MODULE_RESUME_LIST **last;
327 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
328 SIM_ASSERT (STATE_MODULES (sd) != NULL);
330 last = &modules->resume_list;
331 while (*last != NULL)
332 last = &((*last)->next);
334 l->fn = fn;
335 l->next = NULL;
336 *last = l;
339 /* Add FN to the init handler list.
340 suspend in the reverse order to install. */
342 void
343 sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn)
345 struct module_list *modules = STATE_MODULES (sd);
346 MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST);
347 MODULE_SUSPEND_LIST **last;
349 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
350 SIM_ASSERT (STATE_MODULES (sd) != NULL);
352 last = &modules->suspend_list;
353 while (*last != NULL)
354 last = &((*last)->next);
356 l->fn = fn;
357 l->next = modules->suspend_list;
358 modules->suspend_list = l;
361 /* Add FN to the uninstall handler list.
362 Uninstall in reverse order to install. */
364 void
365 sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn)
367 struct module_list *modules = STATE_MODULES (sd);
368 MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST);
370 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
371 SIM_ASSERT (STATE_MODULES (sd) != NULL);
373 l->fn = fn;
374 l->next = modules->uninstall_list;
375 modules->uninstall_list = l;
378 /* Add FN to the info handler list.
379 Report info in the same order as the install. */
381 void
382 sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn)
384 struct module_list *modules = STATE_MODULES (sd);
385 MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST);
386 MODULE_INFO_LIST **last;
388 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
389 SIM_ASSERT (STATE_MODULES (sd) != NULL);
391 last = &modules->info_list;
392 while (*last != NULL)
393 last = &((*last)->next);
395 l->fn = fn;
396 l->next = NULL;
397 *last = l;