]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/common/sim-module.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / sim / common / sim-module.c
index 970e7258233c73eb536dec86ba06c9681ccf4c36..25d1e17ac23335e57659d4b418099762b2e8e6d3 100644 (file)
@@ -1,77 +1,51 @@
 /* Module support.
-   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
+
+   Copyright 1996-2024 Free Software Foundation, Inc.
+
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* This must come before any other includes.  */
+#include "defs.h"
+
+#include <stdlib.h>
+
+#include "libiberty.h"
 
 #include "sim-main.h"
 #include "sim-io.h"
 #include "sim-options.h"
 #include "sim-assert.h"
 
-/* start-sanitize-am30 */
-#if WITH_HW
-#include "sim-hw.h"
-#endif
-/* end-sanitize-am30 */
-
-#include "libiberty.h"
-
-/* List of all modules.  */
-static MODULE_INSTALL_FN * const modules[] = {
+/* List of all early/core modules.
+   TODO: Should trim this list by converting to sim_install_* framework.  */
+static MODULE_INSTALL_FN * const early_modules[] = {
   standard_install,
   sim_events_install,
-#if WITH_ENGINE
-  sim_engine_install,
-#endif
-#if WITH_TRACE
-  trace_install,
-#endif
-#if WITH_PROFILE
-  profile_install,
-#endif
+  sim_model_install,
   sim_core_install,
-#ifndef SIM_HAVE_FLATMEM
-  /* FIXME: should handle flatmem as well FLATMEM */
   sim_memopt_install,
-#endif
-#if WITH_WATCHPOINTS
   sim_watchpoint_install,
-#endif
-#if WITH_SCACHE
-  scache_install,
-#endif
-#ifdef SIM_HAVE_MODEL
-  sim_model_install,
-#endif
-#ifdef SIM_HAVE_BREAKPOINTS
-  sim_break_install,
-#endif
-  /* start-sanitize-am30 */
-#if WITH_HW
-  sim_hw_install,
-#endif
-  /* end-sanitize-am30 */
-  /* Configured in [simulator specific] additional modules.  */
-#ifdef MODULE_LIST
-  MODULE_LIST
-#endif
-  0
 };
+static int early_modules_len = ARRAY_SIZE (early_modules);
+
+/* List of dynamically detected modules.  Declared in generated modules.c.  */
+extern MODULE_INSTALL_FN * const sim_modules_detected[];
+extern const int sim_modules_detected_len;
 \f
 /* Functions called from sim_open.  */
 
@@ -83,9 +57,7 @@ sim_pre_argv_init (SIM_DESC sd, const char *myname)
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   SIM_ASSERT (STATE_MODULES (sd) == NULL);
 
-  STATE_MY_NAME (sd) = myname + strlen (myname);
-  while (STATE_MY_NAME (sd) > myname && STATE_MY_NAME (sd)[-1] != '/')
-    --STATE_MY_NAME (sd);
+  STATE_MY_NAME (sd) = lbasename (myname);
 
   /* Set the cpu names to default values.  */
   {
@@ -93,18 +65,21 @@ sim_pre_argv_init (SIM_DESC sd, const char *myname)
     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
       {
        char *name;
-       asprintf (&name, "cpu%d", i);
+       if (asprintf (&name, "cpu%d", i) < 0)
+         return SIM_RC_FAIL;
        CPU_NAME (STATE_CPU (sd, i)) = name;
       }
   }
 
   sim_config_default (sd);
 
-  /* Install all configured in modules.  */
+  /* Install all early configured-in modules.  */
   if (sim_module_install (sd) != SIM_RC_OK)
     return SIM_RC_FAIL;
 
-  return SIM_RC_OK;
+  /* Install all remaining dynamically detected modules.  */
+  return sim_module_install_list (sd, sim_modules_detected,
+                                 sim_modules_detected_len);
 }
 
 /* Initialize common parts after argument processing.  */
@@ -116,9 +91,6 @@ sim_post_argv_init (SIM_DESC sd)
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   SIM_ASSERT (STATE_MODULES (sd) != NULL);
 
-  if (sim_module_init (sd) != SIM_RC_OK)
-    return SIM_RC_FAIL;
-
   /* Set the cpu->state backlinks for each cpu.  */
   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
     {
@@ -126,33 +98,48 @@ sim_post_argv_init (SIM_DESC sd)
       CPU_INDEX (STATE_CPU (sd, i)) = i;
     }
 
+  if (sim_module_init (sd) != SIM_RC_OK)
+    return SIM_RC_FAIL;
+
   return SIM_RC_OK;
 }
 \f
-/* Install all modules.
+/* Install a list of modules.
    If this fails, no modules are left installed.  */
-
 SIM_RC
-sim_module_install (SIM_DESC sd)
+sim_module_install_list (SIM_DESC sd, MODULE_INSTALL_FN * const *modules,
+                        size_t modules_len)
 {
-  MODULE_INSTALL_FN * const *modp;
-
-  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
-  SIM_ASSERT (STATE_MODULES (sd) == NULL);
+  size_t i;
 
-  STATE_MODULES (sd) = ZALLOC (struct module_list);
-  for (modp = modules; *modp != NULL; ++modp)
+  for (i = 0; i < modules_len; ++i)
     {
-      if ((*modp) (sd) != SIM_RC_OK)
+      MODULE_INSTALL_FN *modp = modules[i];
+
+      if (modp != NULL && modp (sd) != SIM_RC_OK)
        {
          sim_module_uninstall (sd);
          SIM_ASSERT (STATE_MODULES (sd) == NULL);
          return SIM_RC_FAIL;
        }
     }
+
   return SIM_RC_OK;
 }
 
+/* Install all modules.
+   If this fails, no modules are left installed.  */
+
+SIM_RC
+sim_module_install (SIM_DESC sd)
+{
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
+  SIM_ASSERT (STATE_MODULES (sd) == NULL);
+
+  STATE_MODULES (sd) = ZALLOC (struct module_list);
+  return sim_module_install_list (sd, early_modules, early_modules_len);
+}
+
 /* Called after all modules have been installed and after argv
    has been processed.  */
 
@@ -232,7 +219,7 @@ sim_module_uninstall (SIM_DESC sd)
     for (d = modules->init_list; d != NULL; d = n)
       {
        n = d->next;
-       zfree (d);
+       free (d);
       }
   }
 
@@ -242,7 +229,7 @@ sim_module_uninstall (SIM_DESC sd)
     for (d = modules->resume_list; d != NULL; d = n)
       {
        n = d->next;
-       zfree (d);
+       free (d);
       }
   }
 
@@ -252,7 +239,7 @@ sim_module_uninstall (SIM_DESC sd)
     for (d = modules->suspend_list; d != NULL; d = n)
       {
        n = d->next;
-       zfree (d);
+       free (d);
       }
   }
 
@@ -262,7 +249,7 @@ sim_module_uninstall (SIM_DESC sd)
     for (d = modules->uninstall_list; d != NULL; d = n)
       {
        n = d->next;
-       zfree (d);
+       free (d);
       }
   }
 
@@ -272,18 +259,18 @@ sim_module_uninstall (SIM_DESC sd)
     for (d = modules->info_list; d != NULL; d = n)
       {
        n = d->next;
-       zfree (d);
+       free (d);
       }
   }
 
-  zfree (modules);
+  free (modules);
   STATE_MODULES (sd) = NULL;
 }
 
 /* Called when ever simulator info is needed */
 
 void
-sim_module_info (SIM_DESC sd, int verbose)
+sim_module_info (SIM_DESC sd, bool verbose)
 {
   struct module_list *modules = STATE_MODULES (sd);
   MODULE_INFO_LIST *modp;