* plugin.c
*
* Babeltrace Plugin
- *
+ *
* Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
* Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
* SOFTWARE.
*/
-const char *bt_plugin_get_name(struct bt_plugin *plugin)
-{
- const char *ret = NULL;
-
- if (!plugin) {
- goto end;
- }
+#include <babeltrace/compiler.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/plugin/plugin-internal.h>
+#include <glib.h>
- ret = plugin->name->str;
-end:
- return ret;
-}
+#define PLUGIN_SYMBOL_NAME "__bt_plugin_name"
+#define PLUGIN_SYMBOL_AUTHOR "__bt_plugin_author"
+#define PLUGIN_SYMBOL_LICENSE "__bt_plugin_license"
+#define PLUGIN_SYMBOL_INIT "__bt_plugin_init"
+#define PLUGIN_SYMBOL_EXIT "__bt_plugin_exit"
-enum bt_plugin_status bt_plugin_set_name(struct bt_plugin *plugin,
- const char *name)
+static
+void bt_plugin_destroy(struct bt_object *obj)
{
- enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
+ struct bt_plugin *plugin;
- if (!plugin || !name || name[0] == '\0') {
- ret = BT_PLUGIN_STATUS_INVAL;
- goto end;
- }
+ assert(obj);
+ plugin = container_of(obj, struct bt_plugin, base);
- g_string_assign(plugin->name, name);
-end:
- return ret;
-}
+ if (plugin->module) {
+ if (!g_module_close(plugin->module)) {
+ printf_error("Module close error: %s",
+ g_module_error());
-enum bt_plugin_type bt_plugin_get_type(struct bt_plugin *plugin)
-{
- enum bt_plugin_type type = BT_PLUGIN_TYPE_UNKNOWN;
-
- if (!plugin) {
- goto end;
+ }
}
-
- type = plugin->type;
-end:
- return type;
+ g_free(plugin);
}
-enum bt_plugin_status bt_plugin_set_error_stream(
- struct bt_plugin *plugin, FILE *stream)
+BT_HIDDEN
+struct bt_plugin *bt_plugin_create(GModule *module)
{
- enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
+ struct bt_plugin *plugin = NULL;
- if (!plugin) {
- ret = BT_PLUGIN_STATUS_INVAL;
- goto end;
+ if (!module) {
+ goto error;
}
- plugin->error_stream = stream;
-end:
- return ret;
-}
-
-void bt_plugin_get(struct bt_plugin *plugin)
-{
+ plugin = g_new0(struct bt_plugin, 1);
if (!plugin) {
- return;
+ goto error;
}
- bt_ctf_ref_get(&plugin->ref_count);
-}
-
-void bt_plugin_put(struct bt_plugin *plugin)
-{
- if (!plugin) {
- return;
+ bt_object_init(plugin, bt_plugin_destroy);
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_NAME,
+ (gpointer *) &plugin->name))
+ {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_NAME, g_module_name(module));
+ goto error;
}
- bt_ctf_ref_put(&plugin->ref_count, bt_plugin_destroy);
-}
-
-BT_HIDDEN
-enum bt_plugin_status bt_plugin_init(struct bt_plugin *plugin, const char *name,
- void *user_data, bt_plugin_destroy_func user_destroy_func,
- enum bt_plugin_type plugin_type,
- bt_plugin_destroy_func plugin_destroy)
-{
- enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
-
- if (!plugin || !name || name[0] == '\0' ||
- !destroy_func || !private_data) {
- ret = BT_PLUGIN_STATUS_INVAL;
- goto end;
+ printf("Loaded plugin with name %s\n", plugin->name);
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_LICENSE,
+ (gpointer *) &plugin->license))
+ {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_LICENSE, g_module_name(module));
+ goto error;
+ }
+ if (!g_module_symbol(module, PLUGIN_SYMBOL_INIT,
+ (gpointer *) &plugin->init))
+ {
+ printf_error("Unable to resolve plugin symbol %s from %s",
+ PLUGIN_SYMBOL_INIT, g_module_name(module));
+ goto error;
}
- bt_ctf_ref_init(&plugin->ref_count);
- plugin->type = plugin_type;
- plugin->user_data = user_data;
- plugin->user_data_destroy = user_destroy_func;
- plugin->destroy = plugin_destroy;
+ /* Optional symbols */
+ g_module_symbol(module, PLUGIN_SYMBOL_EXIT, (gpointer *) &plugin->exit);
+ g_module_symbol(module, PLUGIN_SYMBOL_AUTHOR,
+ (gpointer *) &plugin->author);
- plugin->name = g_string_new(name);
- if (!plugin->name) {
- ret = BT_PLUGIN_STATUS_NOMEM;
- goto end;
- }
-end:
- return ret;
+ return plugin;
+error:
+ BT_PUT(plugin);
+ return plugin;
}
-static
-void bt_plugin_destroy(struct bt_ctf_ref *ref)
+BT_HIDDEN
+enum bt_component_status bt_plugin_register_component_classes(
+ struct bt_plugin *plugin, struct bt_component_factory *factory)
{
- struct bt_ctf_plugin *plugin = NULL;
-
- if (!ref) {
- return;
- }
-
- plugin = container_of(ref, struct bt_plugin, parent);
-
- /**
- * Destroy user data, base and source/filter/sink last since
- * it will deallocate the plugin.
- */
- assert(plugin->user_destroy_func);
- plugin->user_destroy_func(plugin->user_data);
-
- g_string_free(plugin->name, TRUE);
-
- assert(plugin->destroy);
- plugin->destroy(plugin);
+ assert(plugin && factory);
+ return plugin->init(factory);
}