Home | History | Annotate | Line # | Download | only in common
sim-module.c revision 1.9
      1 /* Module support.
      2 
      3    Copyright 1996-2020 Free Software Foundation, Inc.
      4 
      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
     11 the Free Software Foundation; either version 3 of the License, or
     12 (at your option) any later version.
     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 
     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/>.  */
     21 
     22 #include "config.h"
     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 
     32 #ifdef HAVE_DV_SOCKSER
     33 /* TODO: Shouldn't have device models here.  */
     34 #include "dv-sockser.h"
     35 #endif
     36 
     37 #include "libiberty.h"
     38 
     39 #include <stdlib.h>
     40 
     41 /* List of all modules.  */
     42 static MODULE_INSTALL_FN * const modules[] = {
     43   standard_install,
     44   sim_events_install,
     45   sim_model_install,
     46   sim_engine_install,
     47 #if WITH_TRACE_ANY_P
     48   trace_install,
     49 #endif
     50 #if WITH_PROFILE
     51   profile_install,
     52 #endif
     53   sim_core_install,
     54   sim_memopt_install,
     55   sim_watchpoint_install,
     56 #if WITH_SCACHE
     57   scache_install,
     58 #endif
     59 #if WITH_HW
     60   sim_hw_install,
     61 #endif
     62 #ifdef HAVE_DV_SOCKSER
     63   /* TODO: Shouldn't have device models here.  */
     64   dv_sockser_install,
     65 #endif
     66   0
     67 };
     68 
     69 /* Functions called from sim_open.  */
     71 
     72 /* Initialize common parts before argument processing.  */
     73 
     74 SIM_RC
     75 sim_pre_argv_init (SIM_DESC sd, const char *myname)
     76 {
     77   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
     78   SIM_ASSERT (STATE_MODULES (sd) == NULL);
     79 
     80   STATE_MY_NAME (sd) = lbasename (myname);
     81 
     82   /* Set the cpu names to default values.  */
     83   {
     84     int i;
     85     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
     86       {
     87 	char *name;
     88 	if (asprintf (&name, "cpu%d", i) < 0)
     89 	  return SIM_RC_FAIL;
     90 	CPU_NAME (STATE_CPU (sd, i)) = name;
     91       }
     92   }
     93 
     94   sim_config_default (sd);
     95 
     96   /* Install all configured in modules.  */
     97   if (sim_module_install (sd) != SIM_RC_OK)
     98     return SIM_RC_FAIL;
     99 
    100   return SIM_RC_OK;
    101 }
    102 
    103 /* Initialize common parts after argument processing.  */
    104 
    105 SIM_RC
    106 sim_post_argv_init (SIM_DESC sd)
    107 {
    108   int i;
    109   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    110   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    111 
    112   /* Set the cpu->state backlinks for each cpu.  */
    113   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    114     {
    115       CPU_STATE (STATE_CPU (sd, i)) = sd;
    116       CPU_INDEX (STATE_CPU (sd, i)) = i;
    117     }
    118 
    119   if (sim_module_init (sd) != SIM_RC_OK)
    120     return SIM_RC_FAIL;
    121 
    122   return SIM_RC_OK;
    123 }
    124 
    125 /* 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;
    229 	free (d);
    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;
    239 	free (d);
    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;
    249 	free (d);
    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;
    259 	free (d);
    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;
    269 	free (d);
    270       }
    271   }
    272 
    273   free (modules);
    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 
    294 /* Add FN to the init handler list.
    296    init in the same order as the install. */
    297 
    298 void
    299 sim_module_add_init_fn (SIM_DESC sd, MODULE_INIT_FN fn)
    300 {
    301   struct module_list *modules = STATE_MODULES (sd);
    302   MODULE_INIT_LIST *l = ZALLOC (MODULE_INIT_LIST);
    303   MODULE_INIT_LIST **last;
    304 
    305   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    306   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    307 
    308   last = &modules->init_list;
    309   while (*last != NULL)
    310     last = &((*last)->next);
    311 
    312   l->fn = fn;
    313   l->next = NULL;
    314   *last = l;
    315 }
    316 
    317 /* Add FN to the resume handler list.
    318    resume in the same order as the install. */
    319 
    320 void
    321 sim_module_add_resume_fn (SIM_DESC sd, MODULE_RESUME_FN fn)
    322 {
    323   struct module_list *modules = STATE_MODULES (sd);
    324   MODULE_RESUME_LIST *l = ZALLOC (MODULE_RESUME_LIST);
    325   MODULE_RESUME_LIST **last;
    326 
    327   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    328   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    329 
    330   last = &modules->resume_list;
    331   while (*last != NULL)
    332     last = &((*last)->next);
    333 
    334   l->fn = fn;
    335   l->next = NULL;
    336   *last = l;
    337 }
    338 
    339 /* Add FN to the init handler list.
    340    suspend in the reverse order to install. */
    341 
    342 void
    343 sim_module_add_suspend_fn (SIM_DESC sd, MODULE_SUSPEND_FN fn)
    344 {
    345   struct module_list *modules = STATE_MODULES (sd);
    346   MODULE_SUSPEND_LIST *l = ZALLOC (MODULE_SUSPEND_LIST);
    347   MODULE_SUSPEND_LIST **last;
    348 
    349   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    350   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    351 
    352   last = &modules->suspend_list;
    353   while (*last != NULL)
    354     last = &((*last)->next);
    355 
    356   l->fn = fn;
    357   l->next = modules->suspend_list;
    358   modules->suspend_list = l;
    359 }
    360 
    361 /* Add FN to the uninstall handler list.
    362    Uninstall in reverse order to install.  */
    363 
    364 void
    365 sim_module_add_uninstall_fn (SIM_DESC sd, MODULE_UNINSTALL_FN fn)
    366 {
    367   struct module_list *modules = STATE_MODULES (sd);
    368   MODULE_UNINSTALL_LIST *l = ZALLOC (MODULE_UNINSTALL_LIST);
    369 
    370   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    371   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    372 
    373   l->fn = fn;
    374   l->next = modules->uninstall_list;
    375   modules->uninstall_list = l;
    376 }
    377 
    378 /* Add FN to the info handler list.
    379    Report info in the same order as the install. */
    380 
    381 void
    382 sim_module_add_info_fn (SIM_DESC sd, MODULE_INFO_FN fn)
    383 {
    384   struct module_list *modules = STATE_MODULES (sd);
    385   MODULE_INFO_LIST *l = ZALLOC (MODULE_INFO_LIST);
    386   MODULE_INFO_LIST **last;
    387 
    388   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
    389   SIM_ASSERT (STATE_MODULES (sd) != NULL);
    390 
    391   last = &modules->info_list;
    392   while (*last != NULL)
    393     last = &((*last)->next);
    394 
    395   l->fn = fn;
    396   l->next = NULL;
    397   *last = l;
    398 }
    399