aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-module.c
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2016-01-02 10:50:46 -0500
committerMike Frysinger <vapier@gentoo.org>2021-05-01 20:47:14 -0400
commit8e5f15165784296f5b182f486817cd5945960c44 (patch)
treef3d50c796ae717a3242514848a6d49e92f21be5f /sim/common/sim-module.c
parentd113096b47e4cce45e0f2ea4bb588b8a86835648 (diff)
downloadgdb-8e5f15165784296f5b182f486817cd5945960c44.zip
gdb-8e5f15165784296f5b182f486817cd5945960c44.tar.gz
gdb-8e5f15165784296f5b182f486817cd5945960c44.tar.bz2
sim: add framework for declaring init callbacks locally
To facilitate decentralized module initialization/registration with an eye towards multi-target support, add a framework to detect init calls declared in the source and automatically call them. This is akin to gdb's _initialize_xxx framework for letting modules autodiscover.
Diffstat (limited to 'sim/common/sim-module.c')
-rw-r--r--sim/common/sim-module.c51
1 files changed, 36 insertions, 15 deletions
diff --git a/sim/common/sim-module.c b/sim/common/sim-module.c
index ea43690..a776a08 100644
--- a/sim/common/sim-module.c
+++ b/sim/common/sim-module.c
@@ -38,8 +38,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.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,
sim_model_install,
@@ -63,8 +64,12 @@ static MODULE_INSTALL_FN * const modules[] = {
/* TODO: Shouldn't have device models here. */
dv_sockser_install,
#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;
/* Functions called from sim_open. */
@@ -92,11 +97,13 @@ sim_pre_argv_init (SIM_DESC sd, const char *myname)
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. */
@@ -121,30 +128,44 @@ sim_post_argv_init (SIM_DESC sd)
return SIM_RC_OK;
}
-/* 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;
+ size_t i;
- SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
- SIM_ASSERT (STATE_MODULES (sd) == NULL);
-
- 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)
+{
+ MODULE_INSTALL_FN * const *modp;
+
+ 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. */