1 1.1 christos /* Module support. 2 1.1 christos 3 1.11 christos Copyright 1996-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos Contributed by Cygnus Support. 6 1.1 christos 7 1.1 christos This file is part of GDB, the GNU debugger. 8 1.1 christos 9 1.1 christos This program is free software; you can redistribute it and/or modify 10 1.1 christos it under the terms of the GNU General Public License as published by 11 1.1 christos the Free Software Foundation; either version 3 of the License, or 12 1.1 christos (at your option) any later version. 13 1.1 christos 14 1.1 christos This program is distributed in the hope that it will be useful, 15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 1.1 christos GNU General Public License for more details. 18 1.1 christos 19 1.1 christos You should have received a copy of the GNU General Public License 20 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 1.1 christos 22 1.10 christos /* This must come before any other includes. */ 23 1.10 christos #include "defs.h" 24 1.10 christos 25 1.10 christos #include <stdlib.h> 26 1.10 christos 27 1.10 christos #include "libiberty.h" 28 1.10 christos 29 1.1 christos #include "sim-main.h" 30 1.1 christos #include "sim-io.h" 31 1.1 christos #include "sim-options.h" 32 1.1 christos #include "sim-assert.h" 33 1.1 christos 34 1.10 christos /* List of all early/core modules. 35 1.10 christos TODO: Should trim this list by converting to sim_install_* framework. */ 36 1.10 christos static MODULE_INSTALL_FN * const early_modules[] = { 37 1.1 christos standard_install, 38 1.1 christos sim_events_install, 39 1.1 christos sim_model_install, 40 1.1 christos sim_core_install, 41 1.1 christos sim_memopt_install, 42 1.1 christos sim_watchpoint_install, 43 1.1 christos }; 44 1.10 christos static int early_modules_len = ARRAY_SIZE (early_modules); 45 1.10 christos 46 1.10 christos /* List of dynamically detected modules. Declared in generated modules.c. */ 47 1.10 christos extern MODULE_INSTALL_FN * const sim_modules_detected[]; 48 1.10 christos extern const int sim_modules_detected_len; 49 1.1 christos 50 1.1 christos /* Functions called from sim_open. */ 52 1.1 christos 53 1.1 christos /* Initialize common parts before argument processing. */ 54 1.1 christos 55 1.1 christos SIM_RC 56 1.1 christos sim_pre_argv_init (SIM_DESC sd, const char *myname) 57 1.1 christos { 58 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 59 1.1 christos SIM_ASSERT (STATE_MODULES (sd) == NULL); 60 1.6 christos 61 1.1 christos STATE_MY_NAME (sd) = lbasename (myname); 62 1.1 christos 63 1.1 christos /* Set the cpu names to default values. */ 64 1.1 christos { 65 1.1 christos int i; 66 1.1 christos for (i = 0; i < MAX_NR_PROCESSORS; ++i) 67 1.1 christos { 68 1.1 christos char *name; 69 1.1 christos if (asprintf (&name, "cpu%d", i) < 0) 70 1.1 christos return SIM_RC_FAIL; 71 1.1 christos CPU_NAME (STATE_CPU (sd, i)) = name; 72 1.1 christos } 73 1.1 christos } 74 1.1 christos 75 1.1 christos sim_config_default (sd); 76 1.10 christos 77 1.1 christos /* Install all early configured-in modules. */ 78 1.1 christos if (sim_module_install (sd) != SIM_RC_OK) 79 1.1 christos return SIM_RC_FAIL; 80 1.10 christos 81 1.10 christos /* Install all remaining dynamically detected modules. */ 82 1.10 christos return sim_module_install_list (sd, sim_modules_detected, 83 1.1 christos sim_modules_detected_len); 84 1.1 christos } 85 1.1 christos 86 1.1 christos /* Initialize common parts after argument processing. */ 87 1.1 christos 88 1.1 christos SIM_RC 89 1.1 christos sim_post_argv_init (SIM_DESC sd) 90 1.1 christos { 91 1.1 christos int i; 92 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 93 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 94 1.1 christos 95 1.1 christos /* Set the cpu->state backlinks for each cpu. */ 96 1.1 christos for (i = 0; i < MAX_NR_PROCESSORS; ++i) 97 1.1 christos { 98 1.1 christos CPU_STATE (STATE_CPU (sd, i)) = sd; 99 1.1 christos CPU_INDEX (STATE_CPU (sd, i)) = i; 100 1.1 christos } 101 1.1 christos 102 1.1 christos if (sim_module_init (sd) != SIM_RC_OK) 103 1.1 christos return SIM_RC_FAIL; 104 1.1 christos 105 1.1 christos return SIM_RC_OK; 106 1.1 christos } 107 1.10 christos 108 1.10 christos /* Install a list of modules. 110 1.10 christos If this fails, no modules are left installed. */ 111 1.10 christos SIM_RC 112 1.10 christos sim_module_install_list (SIM_DESC sd, MODULE_INSTALL_FN * const *modules, 113 1.10 christos size_t modules_len) 114 1.10 christos { 115 1.10 christos size_t i; 116 1.10 christos 117 1.10 christos for (i = 0; i < modules_len; ++i) 118 1.10 christos { 119 1.10 christos MODULE_INSTALL_FN *modp = modules[i]; 120 1.10 christos 121 1.10 christos if (modp != NULL && modp (sd) != SIM_RC_OK) 122 1.10 christos { 123 1.10 christos sim_module_uninstall (sd); 124 1.10 christos SIM_ASSERT (STATE_MODULES (sd) == NULL); 125 1.10 christos return SIM_RC_FAIL; 126 1.10 christos } 127 1.10 christos } 128 1.10 christos 129 1.10 christos return SIM_RC_OK; 130 1.1 christos } 131 1.1 christos 132 1.1 christos /* Install all modules. 133 1.1 christos If this fails, no modules are left installed. */ 134 1.1 christos 135 1.1 christos SIM_RC 136 1.1 christos sim_module_install (SIM_DESC sd) 137 1.1 christos { 138 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 139 1.1 christos SIM_ASSERT (STATE_MODULES (sd) == NULL); 140 1.10 christos 141 1.1 christos STATE_MODULES (sd) = ZALLOC (struct module_list); 142 1.1 christos return sim_module_install_list (sd, early_modules, early_modules_len); 143 1.1 christos } 144 1.1 christos 145 1.1 christos /* Called after all modules have been installed and after argv 146 1.1 christos has been processed. */ 147 1.1 christos 148 1.1 christos SIM_RC 149 1.1 christos sim_module_init (SIM_DESC sd) 150 1.1 christos { 151 1.1 christos struct module_list *modules = STATE_MODULES (sd); 152 1.1 christos MODULE_INIT_LIST *modp; 153 1.1 christos 154 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 155 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 156 1.1 christos 157 1.1 christos for (modp = modules->init_list; modp != NULL; modp = modp->next) 158 1.1 christos { 159 1.1 christos if ((*modp->fn) (sd) != SIM_RC_OK) 160 1.1 christos return SIM_RC_FAIL; 161 1.1 christos } 162 1.1 christos return SIM_RC_OK; 163 1.1 christos } 164 1.1 christos 165 1.1 christos /* Called when ever the simulator is resumed */ 166 1.1 christos 167 1.1 christos SIM_RC 168 1.1 christos sim_module_resume (SIM_DESC sd) 169 1.1 christos { 170 1.1 christos struct module_list *modules = STATE_MODULES (sd); 171 1.1 christos MODULE_RESUME_LIST *modp; 172 1.1 christos 173 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 174 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 175 1.1 christos 176 1.1 christos for (modp = modules->resume_list; modp != NULL; modp = modp->next) 177 1.1 christos { 178 1.1 christos if ((*modp->fn) (sd) != SIM_RC_OK) 179 1.1 christos return SIM_RC_FAIL; 180 1.1 christos } 181 1.1 christos return SIM_RC_OK; 182 1.1 christos } 183 1.1 christos 184 1.1 christos /* Called when ever the simulator is suspended */ 185 1.1 christos 186 1.1 christos SIM_RC 187 1.1 christos sim_module_suspend (SIM_DESC sd) 188 1.1 christos { 189 1.1 christos struct module_list *modules = STATE_MODULES (sd); 190 1.1 christos MODULE_SUSPEND_LIST *modp; 191 1.1 christos 192 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 193 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 194 1.1 christos 195 1.1 christos for (modp = modules->suspend_list; modp != NULL; modp = modp->next) 196 1.1 christos { 197 1.1 christos if ((*modp->fn) (sd) != SIM_RC_OK) 198 1.1 christos return SIM_RC_FAIL; 199 1.1 christos } 200 1.1 christos return SIM_RC_OK; 201 1.1 christos } 202 1.1 christos 203 1.1 christos /* Uninstall installed modules, called by sim_close. */ 204 1.1 christos 205 1.1 christos void 206 1.1 christos sim_module_uninstall (SIM_DESC sd) 207 1.1 christos { 208 1.1 christos struct module_list *modules = STATE_MODULES (sd); 209 1.1 christos MODULE_UNINSTALL_LIST *modp; 210 1.1 christos 211 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 212 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 213 1.1 christos 214 1.1 christos /* Uninstall the modules. */ 215 1.1 christos for (modp = modules->uninstall_list; modp != NULL; modp = modp->next) 216 1.1 christos (*modp->fn) (sd); 217 1.1 christos 218 1.1 christos /* clean-up init list */ 219 1.1 christos { 220 1.1 christos MODULE_INIT_LIST *n, *d; 221 1.1 christos for (d = modules->init_list; d != NULL; d = n) 222 1.1 christos { 223 1.1 christos n = d->next; 224 1.1 christos free (d); 225 1.1 christos } 226 1.1 christos } 227 1.1 christos 228 1.1 christos /* clean-up resume list */ 229 1.1 christos { 230 1.1 christos MODULE_RESUME_LIST *n, *d; 231 1.1 christos for (d = modules->resume_list; d != NULL; d = n) 232 1.1 christos { 233 1.1 christos n = d->next; 234 1.1 christos free (d); 235 1.1 christos } 236 1.1 christos } 237 1.1 christos 238 1.1 christos /* clean-up suspend list */ 239 1.1 christos { 240 1.1 christos MODULE_SUSPEND_LIST *n, *d; 241 1.1 christos for (d = modules->suspend_list; d != NULL; d = n) 242 1.1 christos { 243 1.1 christos n = d->next; 244 1.1 christos free (d); 245 1.1 christos } 246 1.1 christos } 247 1.1 christos 248 1.1 christos /* clean-up uninstall list */ 249 1.1 christos { 250 1.1 christos MODULE_UNINSTALL_LIST *n, *d; 251 1.1 christos for (d = modules->uninstall_list; d != NULL; d = n) 252 1.1 christos { 253 1.1 christos n = d->next; 254 1.1 christos free (d); 255 1.1 christos } 256 1.1 christos } 257 1.1 christos 258 1.1 christos /* clean-up info list */ 259 1.1 christos { 260 1.1 christos MODULE_INFO_LIST *n, *d; 261 1.1 christos for (d = modules->info_list; d != NULL; d = n) 262 1.1 christos { 263 1.1 christos n = d->next; 264 1.1 christos free (d); 265 1.1 christos } 266 1.1 christos } 267 1.1 christos 268 1.1 christos free (modules); 269 1.1 christos STATE_MODULES (sd) = NULL; 270 1.1 christos } 271 1.1 christos 272 1.1 christos /* Called when ever simulator info is needed */ 273 1.11 christos 274 1.1 christos void 275 1.1 christos sim_module_info (SIM_DESC sd, bool verbose) 276 1.1 christos { 277 1.1 christos struct module_list *modules = STATE_MODULES (sd); 278 1.1 christos MODULE_INFO_LIST *modp; 279 1.1 christos 280 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 281 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 282 1.1 christos 283 1.1 christos for (modp = modules->info_list; modp != NULL; modp = modp->next) 284 1.1 christos { 285 1.1 christos (*modp->fn) (sd, verbose); 286 1.1 christos } 287 1.1 christos } 288 1.1 christos 289 1.1 christos /* Add FN to the init handler list. 291 1.1 christos init in the same order as the install. */ 292 1.1 christos 293 1.1 christos void 294 1.1 christos sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn) 295 1.1 christos { 296 1.1 christos struct module_list *modules = STATE_MODULES (sd); 297 1.1 christos MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST); 298 1.1 christos MODULE_INIT_LIST **last; 299 1.1 christos 300 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 301 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 302 1.1 christos 303 1.1 christos last = &modules->init_list; 304 1.1 christos while (*last != NULL) 305 1.1 christos last = &((*last)->next); 306 1.1 christos 307 1.1 christos l->fn = fn; 308 1.1 christos l->next = NULL; 309 1.1 christos *last = l; 310 1.1 christos } 311 1.1 christos 312 1.1 christos /* Add FN to the resume handler list. 313 1.1 christos resume in the same order as the install. */ 314 1.1 christos 315 1.1 christos void 316 1.1 christos sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn) 317 1.1 christos { 318 1.1 christos struct module_list *modules = STATE_MODULES (sd); 319 1.1 christos MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST); 320 1.1 christos MODULE_RESUME_LIST **last; 321 1.1 christos 322 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 323 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 324 1.1 christos 325 1.1 christos last = &modules->resume_list; 326 1.1 christos while (*last != NULL) 327 1.1 christos last = &((*last)->next); 328 1.1 christos 329 1.1 christos l->fn = fn; 330 1.1 christos l->next = NULL; 331 1.1 christos *last = l; 332 1.1 christos } 333 1.1 christos 334 1.1 christos /* Add FN to the init handler list. 335 1.1 christos suspend in the reverse order to install. */ 336 1.1 christos 337 1.1 christos void 338 1.1 christos sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn) 339 1.1 christos { 340 1.1 christos struct module_list *modules = STATE_MODULES (sd); 341 1.1 christos MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST); 342 1.1 christos MODULE_SUSPEND_LIST **last; 343 1.1 christos 344 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 345 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 346 1.1 christos 347 1.1 christos last = &modules->suspend_list; 348 1.1 christos while (*last != NULL) 349 1.1 christos last = &((*last)->next); 350 1.1 christos 351 1.1 christos l->fn = fn; 352 1.1 christos l->next = modules->suspend_list; 353 1.1 christos modules->suspend_list = l; 354 1.1 christos } 355 1.1 christos 356 1.1 christos /* Add FN to the uninstall handler list. 357 1.1 christos Uninstall in reverse order to install. */ 358 1.1 christos 359 1.1 christos void 360 1.1 christos sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn) 361 1.1 christos { 362 1.1 christos struct module_list *modules = STATE_MODULES (sd); 363 1.1 christos MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST); 364 1.1 christos 365 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 366 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 367 1.1 christos 368 1.1 christos l->fn = fn; 369 1.1 christos l->next = modules->uninstall_list; 370 1.1 christos modules->uninstall_list = l; 371 1.1 christos } 372 1.1 christos 373 1.1 christos /* Add FN to the info handler list. 374 1.1 christos Report info in the same order as the install. */ 375 1.1 christos 376 1.1 christos void 377 1.1 christos sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn) 378 1.1 christos { 379 1.1 christos struct module_list *modules = STATE_MODULES (sd); 380 1.1 christos MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST); 381 1.1 christos MODULE_INFO_LIST **last; 382 1.1 christos 383 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 384 1.1 christos SIM_ASSERT (STATE_MODULES (sd) != NULL); 385 1.1 christos 386 1.1 christos last = &modules->info_list; 387 1.1 christos while (*last != NULL) 388 1.1 christos last = &((*last)->next); 389 1.1 christos 390 1.1 christos l->fn = fn; 391 l->next = NULL; 392 *last = l; 393 } 394