diff mbox series

[6/8] plugins: implement QPP import function

Message ID 20221213213757.4123265-7-fasano@mit.edu
State New
Headers show
Series Inter-plugin interactions with QPP | expand

Commit Message

Andrew Fasano Dec. 13, 2022, 9:37 p.m. UTC
From: Elysia Witham <elysia.witham@ll.mit.edu>

Plugins can export functions or import functions from other
plugins using their name and the function name. This is also
described in <qemu-plugin.h>.

Signed-off-by: Elysia Witham <elysia.witham@ll.mit.edu>
Signed-off-by: Andrew Fasano <fasano@mit.edu>
---
 include/qemu/qemu-plugin.h   | 10 ++++++++++
 plugins/api.c                | 21 +++++++++++++++++++++
 plugins/qemu-plugins.symbols |  1 +
 3 files changed, 32 insertions(+)

Comments

Alex Bennée March 9, 2023, 4:26 p.m. UTC | #1
Andrew Fasano <fasano@mit.edu> writes:

> From: Elysia Witham <elysia.witham@ll.mit.edu>
>
> Plugins can export functions or import functions from other
> plugins using their name and the function name. This is also
> described in <qemu-plugin.h>.
>
> Signed-off-by: Elysia Witham <elysia.witham@ll.mit.edu>
> Signed-off-by: Andrew Fasano <fasano@mit.edu>
> ---
>  include/qemu/qemu-plugin.h   | 10 ++++++++++
>  plugins/api.c                | 21 +++++++++++++++++++++
>  plugins/qemu-plugins.symbols |  1 +
>  3 files changed, 32 insertions(+)
>
> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
> index 4221545015..a0516e9a0e 100644
> --- a/include/qemu/qemu-plugin.h
> +++ b/include/qemu/qemu-plugin.h
> @@ -354,6 +354,16 @@ size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb);
>   */
>  uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb);
>  
> +/**
> + * qemu_plugin_import_function() - return pointer to a function in another
> + * plugin
> + * @plugin: plugin name
> + * @function: function name
> + *
> + * Returns: NULL on failure, function pointer on success
> + */
> +gpointer qemu_plugin_import_function(const char *plugin, const char *function);
> +
>  /**
>   * qemu_plugin_create_callback() - create a new cb with given name
>   * @id: unique plugin id
> diff --git a/plugins/api.c b/plugins/api.c
> index 1f7ea718dc..a998df6942 100644
> --- a/plugins/api.c
> +++ b/plugins/api.c
> @@ -400,6 +400,27 @@ bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
>      return name && value && qapi_bool_parse(name, value, ret, NULL);
>  }
>  
> +/*
> + * QPP: inter-plugin function resolution and callbacks
> + */
> +
> +gpointer qemu_plugin_import_function(const char *target_plugin,
> +                                     const char *function) {
> +    gpointer function_pointer = NULL;
> +    struct qemu_plugin_ctx *ctx = plugin_name_to_ctx_locked(target_plugin);
> +    if (ctx == NULL) {
> +        error_report("Unable to load plugin %s by name", target_plugin);
> +    } else if (g_module_symbol(ctx->handle, function,
> +               (gpointer *)&function_pointer)) {
> +        return function_pointer;
> +    } else {
> +      error_report("function: %s not found in plugin: %s", function,
> +                   target_plugin);
> +    }
> +    abort();
> +    return NULL;

Hmm when does __attribute__ ((constructor)) get called during the
g_module_open() of the plugin? I think aborting is a is a poor failure
mode in this case as you can bring down the whole translator with a poor
plugin load. I'd rather fail gracefully and uninstall the plugin.

> +}
> +
>  bool qemu_plugin_create_callback(qemu_plugin_id_t id, const char *cb_name)
>  {
>      struct qemu_plugin_ctx *ctx = plugin_id_to_ctx_locked(id);
> diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols
> index b7013980cf..70a518839d 100644
> --- a/plugins/qemu-plugins.symbols
> +++ b/plugins/qemu-plugins.symbols
> @@ -3,6 +3,7 @@
>    qemu_plugin_end_code;
>    qemu_plugin_entry_code;
>    qemu_plugin_get_hwaddr;
> +  qemu_plugin_import_function;
>    qemu_plugin_create_callback;
>    qemu_plugin_run_callback;
>    qemu_plugin_reg_callback;
diff mbox series

Patch

diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 4221545015..a0516e9a0e 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -354,6 +354,16 @@  size_t qemu_plugin_tb_n_insns(const struct qemu_plugin_tb *tb);
  */
 uint64_t qemu_plugin_tb_vaddr(const struct qemu_plugin_tb *tb);
 
+/**
+ * qemu_plugin_import_function() - return pointer to a function in another
+ * plugin
+ * @plugin: plugin name
+ * @function: function name
+ *
+ * Returns: NULL on failure, function pointer on success
+ */
+gpointer qemu_plugin_import_function(const char *plugin, const char *function);
+
 /**
  * qemu_plugin_create_callback() - create a new cb with given name
  * @id: unique plugin id
diff --git a/plugins/api.c b/plugins/api.c
index 1f7ea718dc..a998df6942 100644
--- a/plugins/api.c
+++ b/plugins/api.c
@@ -400,6 +400,27 @@  bool qemu_plugin_bool_parse(const char *name, const char *value, bool *ret)
     return name && value && qapi_bool_parse(name, value, ret, NULL);
 }
 
+/*
+ * QPP: inter-plugin function resolution and callbacks
+ */
+
+gpointer qemu_plugin_import_function(const char *target_plugin,
+                                     const char *function) {
+    gpointer function_pointer = NULL;
+    struct qemu_plugin_ctx *ctx = plugin_name_to_ctx_locked(target_plugin);
+    if (ctx == NULL) {
+        error_report("Unable to load plugin %s by name", target_plugin);
+    } else if (g_module_symbol(ctx->handle, function,
+               (gpointer *)&function_pointer)) {
+        return function_pointer;
+    } else {
+      error_report("function: %s not found in plugin: %s", function,
+                   target_plugin);
+    }
+    abort();
+    return NULL;
+}
+
 bool qemu_plugin_create_callback(qemu_plugin_id_t id, const char *cb_name)
 {
     struct qemu_plugin_ctx *ctx = plugin_id_to_ctx_locked(id);
diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols
index b7013980cf..70a518839d 100644
--- a/plugins/qemu-plugins.symbols
+++ b/plugins/qemu-plugins.symbols
@@ -3,6 +3,7 @@ 
   qemu_plugin_end_code;
   qemu_plugin_entry_code;
   qemu_plugin_get_hwaddr;
+  qemu_plugin_import_function;
   qemu_plugin_create_callback;
   qemu_plugin_run_callback;
   qemu_plugin_reg_callback;