]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Module support. |
836cc9f4 | 2 | |
32d0add0 | 3 | Copyright 1996-2015 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 | |
8406bb59 | 22 | #include "config.h" |
c906108c SS |
23 | #include "sim-main.h" |
24 | #include "sim-io.h" | |
25 | #include "sim-options.h" | |
26 | #include "sim-assert.h" | |
27 | ||
28 | #if WITH_HW | |
29 | #include "sim-hw.h" | |
30 | #endif | |
31 | ||
8406bb59 MF |
32 | #ifdef HAVE_DV_SOCKSER |
33 | /* TODO: Shouldn't have device models here. */ | |
34 | #include "dv-sockser.h" | |
35 | #endif | |
36 | ||
c906108c SS |
37 | #include "libiberty.h" |
38 | ||
39 | /* List of all modules. */ | |
40 | static MODULE_INSTALL_FN * const modules[] = { | |
41 | standard_install, | |
42 | sim_events_install, | |
c906108c | 43 | sim_model_install, |
c906108c SS |
44 | #if WITH_ENGINE |
45 | sim_engine_install, | |
46 | #endif | |
bffcfec8 | 47 | #if WITH_TRACE_ANY_P |
c906108c SS |
48 | trace_install, |
49 | #endif | |
50 | #if WITH_PROFILE | |
51 | profile_install, | |
52 | #endif | |
53 | sim_core_install, | |
c906108c | 54 | sim_memopt_install, |
c906108c | 55 | sim_watchpoint_install, |
c906108c SS |
56 | #if WITH_SCACHE |
57 | scache_install, | |
58 | #endif | |
c906108c SS |
59 | #if WITH_HW |
60 | sim_hw_install, | |
8406bb59 MF |
61 | #endif |
62 | #ifdef HAVE_DV_SOCKSER | |
63 | /* TODO: Shouldn't have device models here. */ | |
64 | dv_sockser_install, | |
c906108c SS |
65 | #endif |
66 | 0 | |
67 | }; | |
68 | \f | |
69 | /* Functions called from sim_open. */ | |
70 | ||
71 | /* Initialize common parts before argument processing. */ | |
72 | ||
73 | SIM_RC | |
74 | sim_pre_argv_init (SIM_DESC sd, const char *myname) | |
75 | { | |
76 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
77 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
78 | ||
79 | STATE_MY_NAME (sd) = myname + strlen (myname); | |
80 | while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/') | |
81 | --STATE_MY_NAME (sd); | |
82 | ||
83 | /* Set the cpu names to default values. */ | |
84 | { | |
85 | int i; | |
86 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
87 | { | |
88 | char *name; | |
39a3ae0a MF |
89 | if (asprintf (&name, "cpu%d", i) < 0) |
90 | return SIM_RC_FAIL; | |
c906108c SS |
91 | CPU_NAME (STATE_CPU (sd, i)) = name; |
92 | } | |
93 | } | |
94 | ||
95 | sim_config_default (sd); | |
96 | ||
97 | /* Install all configured in modules. */ | |
98 | if (sim_module_install (sd) != SIM_RC_OK) | |
99 | return SIM_RC_FAIL; | |
100 | ||
101 | return SIM_RC_OK; | |
102 | } | |
103 | ||
104 | /* Initialize common parts after argument processing. */ | |
105 | ||
106 | SIM_RC | |
107 | sim_post_argv_init (SIM_DESC sd) | |
108 | { | |
109 | int i; | |
110 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
111 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
112 | ||
113 | /* Set the cpu->state backlinks for each cpu. */ | |
114 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
115 | { | |
116 | CPU_STATE (STATE_CPU (sd, i)) = sd; | |
117 | CPU_INDEX (STATE_CPU (sd, i)) = i; | |
118 | } | |
119 | ||
120 | if (sim_module_init (sd) != SIM_RC_OK) | |
121 | return SIM_RC_FAIL; | |
122 | ||
123 | return SIM_RC_OK; | |
124 | } | |
125 | \f | |
126 | /* Install all modules. | |
127 | If this fails, no modules are left installed. */ | |
128 | ||
129 | SIM_RC | |
130 | sim_module_install (SIM_DESC sd) | |
131 | { | |
132 | MODULE_INSTALL_FN * const *modp; | |
133 | ||
134 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
135 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
136 | ||
137 | STATE_MODULES (sd) = ZALLOC (struct module_list); | |
138 | for (modp = modules; *modp != NULL; ++modp) | |
139 | { | |
140 | if ((*modp) (sd) != SIM_RC_OK) | |
141 | { | |
142 | sim_module_uninstall (sd); | |
143 | SIM_ASSERT (STATE_MODULES (sd) == NULL); | |
144 | return SIM_RC_FAIL; | |
145 | } | |
146 | } | |
147 | return SIM_RC_OK; | |
148 | } | |
149 | ||
150 | /* Called after all modules have been installed and after argv | |
151 | has been processed. */ | |
152 | ||
153 | SIM_RC | |
154 | sim_module_init (SIM_DESC sd) | |
155 | { | |
156 | struct module_list *modules = STATE_MODULES (sd); | |
157 | MODULE_INIT_LIST *modp; | |
158 | ||
159 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
160 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
161 | ||
162 | for (modp = modules->init_list; modp != NULL; modp = modp->next) | |
163 | { | |
164 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
165 | return SIM_RC_FAIL; | |
166 | } | |
167 | return SIM_RC_OK; | |
168 | } | |
169 | ||
170 | /* Called when ever the simulator is resumed */ | |
171 | ||
172 | SIM_RC | |
173 | sim_module_resume (SIM_DESC sd) | |
174 | { | |
175 | struct module_list *modules = STATE_MODULES (sd); | |
176 | MODULE_RESUME_LIST *modp; | |
177 | ||
178 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
179 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
180 | ||
181 | for (modp = modules->resume_list; modp != NULL; modp = modp->next) | |
182 | { | |
183 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
184 | return SIM_RC_FAIL; | |
185 | } | |
186 | return SIM_RC_OK; | |
187 | } | |
188 | ||
189 | /* Called when ever the simulator is suspended */ | |
190 | ||
191 | SIM_RC | |
192 | sim_module_suspend (SIM_DESC sd) | |
193 | { | |
194 | struct module_list *modules = STATE_MODULES (sd); | |
195 | MODULE_SUSPEND_LIST *modp; | |
196 | ||
197 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
198 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
199 | ||
200 | for (modp = modules->suspend_list; modp != NULL; modp = modp->next) | |
201 | { | |
202 | if ((*modp->fn) (sd) != SIM_RC_OK) | |
203 | return SIM_RC_FAIL; | |
204 | } | |
205 | return SIM_RC_OK; | |
206 | } | |
207 | ||
208 | /* Uninstall installed modules, called by sim_close. */ | |
209 | ||
210 | void | |
211 | sim_module_uninstall (SIM_DESC sd) | |
212 | { | |
213 | struct module_list *modules = STATE_MODULES (sd); | |
214 | MODULE_UNINSTALL_LIST *modp; | |
215 | ||
216 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
217 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
218 | ||
219 | /* Uninstall the modules. */ | |
220 | for (modp = modules->uninstall_list; modp != NULL; modp = modp->next) | |
221 | (*modp->fn) (sd); | |
222 | ||
223 | /* clean-up init list */ | |
224 | { | |
225 | MODULE_INIT_LIST *n, *d; | |
226 | for (d = modules->init_list; d != NULL; d = n) | |
227 | { | |
228 | n = d->next; | |
d79fe0d6 | 229 | free (d); |
c906108c SS |
230 | } |
231 | } | |
232 | ||
233 | /* clean-up resume list */ | |
234 | { | |
235 | MODULE_RESUME_LIST *n, *d; | |
236 | for (d = modules->resume_list; d != NULL; d = n) | |
237 | { | |
238 | n = d->next; | |
d79fe0d6 | 239 | free (d); |
c906108c SS |
240 | } |
241 | } | |
242 | ||
243 | /* clean-up suspend list */ | |
244 | { | |
245 | MODULE_SUSPEND_LIST *n, *d; | |
246 | for (d = modules->suspend_list; d != NULL; d = n) | |
247 | { | |
248 | n = d->next; | |
d79fe0d6 | 249 | free (d); |
c906108c SS |
250 | } |
251 | } | |
252 | ||
253 | /* clean-up uninstall list */ | |
254 | { | |
255 | MODULE_UNINSTALL_LIST *n, *d; | |
256 | for (d = modules->uninstall_list; d != NULL; d = n) | |
257 | { | |
258 | n = d->next; | |
d79fe0d6 | 259 | free (d); |
c906108c SS |
260 | } |
261 | } | |
262 | ||
263 | /* clean-up info list */ | |
264 | { | |
265 | MODULE_INFO_LIST *n, *d; | |
266 | for (d = modules->info_list; d != NULL; d = n) | |
267 | { | |
268 | n = d->next; | |
d79fe0d6 | 269 | free (d); |
c906108c SS |
270 | } |
271 | } | |
272 | ||
d79fe0d6 | 273 | free (modules); |
c906108c SS |
274 | STATE_MODULES (sd) = NULL; |
275 | } | |
276 | ||
277 | /* Called when ever simulator info is needed */ | |
278 | ||
279 | void | |
280 | sim_module_info (SIM_DESC sd, int verbose) | |
281 | { | |
282 | struct module_list *modules = STATE_MODULES (sd); | |
283 | MODULE_INFO_LIST *modp; | |
284 | ||
285 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
286 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
287 | ||
288 | for (modp = modules->info_list; modp != NULL; modp = modp->next) | |
289 | { | |
290 | (*modp->fn) (sd, verbose); | |
291 | } | |
292 | } | |
293 | \f | |
294 | /* Add FN to the init handler list. | |
295 | init in the same order as the install. */ | |
296 | ||
297 | void | |
298 | sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn) | |
299 | { | |
300 | struct module_list *modules = STATE_MODULES (sd); | |
301 | MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST); | |
302 | MODULE_INIT_LIST **last; | |
303 | ||
304 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
305 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
306 | ||
307 | last = &modules->init_list; | |
308 | while (*last != NULL) | |
309 | last = &((*last)->next); | |
310 | ||
311 | l->fn = fn; | |
312 | l->next = NULL; | |
313 | *last = l; | |
314 | } | |
315 | ||
316 | /* Add FN to the resume handler list. | |
317 | resume in the same order as the install. */ | |
318 | ||
319 | void | |
320 | sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn) | |
321 | { | |
322 | struct module_list *modules = STATE_MODULES (sd); | |
323 | MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST); | |
324 | MODULE_RESUME_LIST **last; | |
325 | ||
326 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
327 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
328 | ||
329 | last = &modules->resume_list; | |
330 | while (*last != NULL) | |
331 | last = &((*last)->next); | |
332 | ||
333 | l->fn = fn; | |
334 | l->next = NULL; | |
335 | *last = l; | |
336 | } | |
337 | ||
338 | /* Add FN to the init handler list. | |
339 | suspend in the reverse order to install. */ | |
340 | ||
341 | void | |
342 | sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn) | |
343 | { | |
344 | struct module_list *modules = STATE_MODULES (sd); | |
345 | MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST); | |
346 | MODULE_SUSPEND_LIST **last; | |
347 | ||
348 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
349 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
350 | ||
351 | last = &modules->suspend_list; | |
352 | while (*last != NULL) | |
353 | last = &((*last)->next); | |
354 | ||
355 | l->fn = fn; | |
356 | l->next = modules->suspend_list; | |
357 | modules->suspend_list = l; | |
358 | } | |
359 | ||
360 | /* Add FN to the uninstall handler list. | |
361 | Uninstall in reverse order to install. */ | |
362 | ||
363 | void | |
364 | sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn) | |
365 | { | |
366 | struct module_list *modules = STATE_MODULES (sd); | |
367 | MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST); | |
368 | ||
369 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
370 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
371 | ||
372 | l->fn = fn; | |
373 | l->next = modules->uninstall_list; | |
374 | modules->uninstall_list = l; | |
375 | } | |
376 | ||
377 | /* Add FN to the info handler list. | |
378 | Report info in the same order as the install. */ | |
379 | ||
380 | void | |
381 | sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn) | |
382 | { | |
383 | struct module_list *modules = STATE_MODULES (sd); | |
384 | MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST); | |
385 | MODULE_INFO_LIST **last; | |
386 | ||
387 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
388 | SIM_ASSERT (STATE_MODULES (sd) != NULL); | |
389 | ||
390 | last = &modules->info_list; | |
391 | while (*last != NULL) | |
392 | last = &((*last)->next); | |
393 | ||
394 | l->fn = fn; | |
395 | l->next = NULL; | |
396 | *last = l; | |
397 | } |