/*
* plugin.c
*
- * Babeltrace Plugin
+ * Babeltrace Plugin (generic)
*
* Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
* Copyright 2017 Philippe Proulx <pproulx@efficios.com>
* SOFTWARE.
*/
-#include <babeltrace/compiler.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/compiler-internal.h>
#include <babeltrace/ref.h>
-#include <babeltrace/plugin/plugin-dev.h>
+#include <babeltrace/common-internal.h>
#include <babeltrace/plugin/plugin-internal.h>
-#include <babeltrace/component/component-class-internal.h>
-#include <string.h>
-#include <stdbool.h>
+#include <babeltrace/plugin/plugin-so-internal.h>
+#include <babeltrace/types.h>
#include <glib.h>
-#include <gmodule.h>
#include <unistd.h>
#include <stdlib.h>
+#include <stdint.h>
#include <sys/stat.h>
#include <dirent.h>
-#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"
-#define PLUGIN_SYMBOL_DESCRIPTION "__bt_plugin_description"
-#define NATIVE_PLUGIN_SUFFIX ".so"
-#define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
-#define LIBTOOL_PLUGIN_SUFFIX ".la"
-#define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
-
-#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
- sizeof(LIBTOOL_PLUGIN_SUFFIX))
-
-#define SECTION_BEGIN(_name) &__start_##_name
-#define SECTION_END(_name) &__stop_##_name
-#define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name))
-
-#define DECLARE_SECTION(_type, _name) \
- extern _type const __start_##_name __attribute((weak)); \
- extern _type const __stop_##_name __attribute((weak))
-
-DECLARE_SECTION(bt_plugin_init_func, __bt_plugin_init_funcs);
-DECLARE_SECTION(bt_plugin_exit_func, __bt_plugin_exit_funcs);
-DECLARE_SECTION(const char *, __bt_plugin_names);
-DECLARE_SECTION(const char *, __bt_plugin_authors);
-DECLARE_SECTION(const char *, __bt_plugin_licenses);
-DECLARE_SECTION(const char *, __bt_plugin_descriptions);
-
-#define PRINT_SECTION(_printer, _name) \
- do { \
- _printer("Section " #_name " [%p - %p], (%zu elements)\n", \
- SECTION_BEGIN(_name), SECTION_END(_name), \
- SECTION_ELEMENT_COUNT(_name)); \
- } while (0)
-
-#define PRINT_PLUG_IN_SECTIONS(_printer) \
- do { \
- PRINT_SECTION(_printer, __bt_plugin_init_funcs); \
- PRINT_SECTION(_printer, __bt_plugin_exit_funcs); \
- PRINT_SECTION(_printer, __bt_plugin_names); \
- PRINT_SECTION(_printer, __bt_plugin_authors); \
- PRINT_SECTION(_printer, __bt_plugin_licenses); \
- PRINT_SECTION(_printer, __bt_plugin_descriptions); \
- } while (0)
+#define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
+#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
+#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
-/*
- * This hash table, global to the library, maps component class pointers
- * to shared library handles.
- *
- * The keys (component classes) are NOT owned by this hash table, whereas
- * the values (shared library handles) are owned by this hash table.
- *
- * The keys are the component classes created with
- * bt_plugin_add_component_class(). They keep the shared library handle
- * object created by their plugin alive so that the plugin's code is
- * not discarded when it could still be in use by living components
- * created from those component classes:
- *
- * [component] --ref-> [component class] --through this HT-> [shlib handle]
- *
- * This hash table exists for two reasons:
- *
- * 1. To allow this application:
- *
- * my_plugin = bt_plugin_create_from_file("/path/to/my-plugin.so");
- * // instantiate components from the plugin's component classes
- * BT_PUT(my_plugin);
- * // user code of instantiated components still exists
- *
- * 2. To decouple the plugin subsystem from the component subsystem:
- * while plugins objects need to know component class objects, the
- * opposite is not necessary, thus it makes no sense for a component
- * class to keep a reference to the plugin object from which it was
- * created.
- *
- * An entry is removed from this HT when a component class is destroyed
- * thanks to a custom destroy listener. When the entry is removed, the
- * GLib function calls the value destroy notifier of the HT, which is
- * bt_put(). This decreases the reference count of the mapped shared
- * library handle. Assuming the original plugin object which contained
- * some component classes is put first, when the last component class is
- * removed from this HT, the shared library handle object's reference
- * count falls to zero and the shared library is finally closed.
- */
+#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
+#include <babeltrace/plugin/python-plugin-provider-internal.h>
+static
+struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) =
+ bt_plugin_python_create_all_from_file;
+#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
+static GModule *python_plugin_provider_module;
static
-GHashTable *comp_classes_to_shlib_handles;
+struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path);
__attribute__((constructor)) static
-void init_comp_classes_to_shlib_handles(void) {
- comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash,
- g_direct_equal, NULL, bt_put);
- assert(comp_classes_to_shlib_handles);
+void init_python_plugin_provider(void) {
+ python_plugin_provider_module =
+ g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
+ G_MODULE_BIND_LOCAL);
+ if (!python_plugin_provider_module) {
+ printf_verbose("Cannot find `%s`: Python plugin support is disabled\n",
+ PYTHON_PLUGIN_PROVIDER_FILENAME);
+ return;
+ }
+
+ if (!g_module_symbol(python_plugin_provider_module,
+ PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
+ (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
+ printf_verbose("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
+ }
}
__attribute__((destructor)) static
-void fini_comp_classes_to_shlib_handles(void) {
- if (comp_classes_to_shlib_handles) {
- g_hash_table_destroy(comp_classes_to_shlib_handles);
+void fini_python_plugin_provider(void) {
+ if (python_plugin_provider_module) {
+ (void) g_module_close(python_plugin_provider_module);
+ python_plugin_provider_module = NULL;
}
}
+#endif
-static
-void bt_plugin_shared_lib_handle_destroy(struct bt_object *obj)
+extern
+int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
{
- struct bt_plugin_shared_lib_handle *shared_lib_handle;
+ int64_t count = -1;
- assert(obj);
- shared_lib_handle = container_of(obj,
- struct bt_plugin_shared_lib_handle, base);
+ if (!plugin_set) {
+ goto end;
+ }
- if (shared_lib_handle->init_called && shared_lib_handle->exit) {
- enum bt_plugin_status status = shared_lib_handle->exit();
+ count = (int64_t) plugin_set->plugins->len;
- if (status < 0) {
- printf_verbose("Plugin `%s` exited with error %d\n",
- shared_lib_handle->name, status);
- }
- }
+end:
+ return count;
+}
- if (shared_lib_handle->module) {
- if (!g_module_close(shared_lib_handle->module)) {
- printf_error("Module close error: %s\n",
- g_module_error());
- }
- }
+extern
+struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
+ uint64_t index)
+{
+ struct bt_plugin *plugin = NULL;
- if (shared_lib_handle->path) {
- g_string_free(shared_lib_handle->path, TRUE);
+ if (!plugin_set || index >= plugin_set->plugins->len) {
+ goto end;
}
- g_free(shared_lib_handle);
+ plugin = bt_get(g_ptr_array_index(plugin_set->plugins, index));
+
+end:
+ return plugin;
}
-static
-struct bt_plugin_shared_lib_handle *bt_plugin_shared_lib_handle_create(
- const char *path)
+struct bt_plugin_set *bt_plugin_create_all_from_static(void)
{
- struct bt_plugin_shared_lib_handle *shared_lib_handle = NULL;
- gpointer symbol = NULL;
-
- shared_lib_handle = g_new0(struct bt_plugin_shared_lib_handle, 1);
- if (!shared_lib_handle) {
- goto error;
- }
+ return bt_plugin_so_create_all_from_static();
+}
- bt_object_init(shared_lib_handle, bt_plugin_shared_lib_handle_destroy);
+struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
+{
+ struct bt_plugin_set *plugin_set = NULL;
if (!path) {
goto end;
}
- shared_lib_handle->path = g_string_new(path);
- if (!shared_lib_handle->path) {
- goto error;
- }
+ printf_verbose("Trying to load plugins from `%s`\n", path);
- shared_lib_handle->module = g_module_open(path, 0);
- if (!shared_lib_handle->module) {
- printf_verbose("Module open error: %s\n", g_module_error());
- goto error;
+ /* Try shared object plugins */
+ plugin_set = bt_plugin_so_create_all_from_file(path);
+ if (plugin_set) {
+ goto end;
}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_NAME,
- (gpointer *) &shared_lib_handle->name)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_NAME,
- g_module_name(shared_lib_handle->module));
- goto error;
+ /* Try Python plugins if support is available */
+ if (bt_plugin_python_create_all_from_file_sym) {
+ plugin_set = bt_plugin_python_create_all_from_file_sym(path);
+ if (plugin_set) {
+ goto end;
+ }
}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_LICENSE,
- (gpointer *) &shared_lib_handle->license)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_LICENSE,
- g_module_name(shared_lib_handle->module));
- goto error;
- }
+end:
+ return plugin_set;
+}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_AUTHOR,
- (gpointer *) &shared_lib_handle->author)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_AUTHOR,
- g_module_name(shared_lib_handle->module));
- goto error;
+static void destroy_gstring(void *data)
+{
+ g_string_free(data, TRUE);
+}
+
+struct bt_plugin *bt_plugin_find(const char *plugin_name)
+{
+ const char *system_plugin_dir;
+ char *home_plugin_dir = NULL;
+ const char *envvar;
+ struct bt_plugin *plugin = NULL;
+ struct bt_plugin_set *plugin_set = NULL;
+ GPtrArray *dirs = NULL;
+ int ret;
+ size_t i, j;
+
+ if (!plugin_name) {
+ goto end;
}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_DESCRIPTION,
- (gpointer *) &shared_lib_handle->description)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_DESCRIPTION,
- g_module_name(shared_lib_handle->module));
- goto error;
+ dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
+ if (!dirs) {
+ goto end;
}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_INIT,
- &symbol)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_INIT,
- g_module_name(shared_lib_handle->module));
- goto error;
- } else {
- shared_lib_handle->init = *((bt_plugin_init_func *) symbol);
- if (!shared_lib_handle->init) {
- printf_verbose("NULL %s symbol target\n",
- PLUGIN_SYMBOL_INIT);
- goto error;
+ /*
+ * Search order is:
+ *
+ * 1. BABELTRACE_PLUGIN_PATH environment variable
+ * (colon-separated list of directories)
+ * 2. ~/.local/lib/babeltrace/plugins
+ * 3. Default system directory for Babeltrace plugins, usually
+ * /usr/lib/babeltrace/plugins or
+ * /usr/local/lib/babeltrace/plugins if installed
+ * locally
+ * 4. Built-in plugins (static)
+ *
+ * Directories are searched non-recursively.
+ */
+ envvar = getenv("BABELTRACE_PLUGIN_PATH");
+ if (envvar) {
+ ret = bt_common_append_plugin_path_dirs(envvar, dirs);
+ if (ret) {
+ goto end;
}
}
- if (!g_module_symbol(shared_lib_handle->module, PLUGIN_SYMBOL_EXIT,
- &symbol)) {
- printf_verbose("Unable to resolve plugin symbol %s from %s\n",
- PLUGIN_SYMBOL_EXIT,
- g_module_name(shared_lib_handle->module));
- goto error;
- } else {
- shared_lib_handle->exit = *((bt_plugin_exit_func *) symbol);
- if (!shared_lib_handle->exit) {
- printf_verbose("NULL %s symbol target\n",
- PLUGIN_SYMBOL_EXIT);
- goto error;
+ home_plugin_dir = bt_common_get_home_plugin_path();
+ if (home_plugin_dir) {
+ GString *home_plugin_dir_str =
+ g_string_new(home_plugin_dir);
+
+ if (!home_plugin_dir_str) {
+ goto end;
}
+
+ g_ptr_array_add(dirs, home_plugin_dir_str);
}
- goto end;
+ system_plugin_dir = bt_common_get_system_plugin_path();
+ if (system_plugin_dir) {
+ GString *system_plugin_dir_str =
+ g_string_new(system_plugin_dir);
-error:
- BT_PUT(shared_lib_handle);
+ if (!system_plugin_dir_str) {
+ goto end;
+ }
-end:
- return shared_lib_handle;
-}
+ g_ptr_array_add(dirs, system_plugin_dir_str);
+ }
-static
-void bt_plugin_destroy(struct bt_object *obj)
-{
- struct bt_plugin *plugin;
+ for (i = 0; i < dirs->len; i++) {
+ GString *dir = g_ptr_array_index(dirs, i);
- assert(obj);
- plugin = container_of(obj, struct bt_plugin, base);
+ printf_verbose("Trying to load plugins from directory `%s`\n",
+ dir->str);
+ BT_PUT(plugin_set);
+ plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
+ if (!plugin_set) {
+ continue;
+ }
- BT_PUT(plugin->shared_lib_handle);
+ for (j = 0; j < plugin_set->plugins->len; j++) {
+ struct bt_plugin *candidate_plugin =
+ g_ptr_array_index(plugin_set->plugins, j);
- if (plugin->comp_classes) {
- g_ptr_array_free(plugin->comp_classes, TRUE);
+ if (strcmp(bt_plugin_get_name(candidate_plugin),
+ plugin_name) == 0) {
+ plugin = bt_get(candidate_plugin);
+ goto end;
+ }
+ }
}
- g_free(plugin);
-}
-
-static
-enum bt_plugin_status init_plugin(struct bt_plugin *plugin)
-{
- enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
+ bt_put(plugin_set);
+ plugin_set = bt_plugin_create_all_from_static();
- if (plugin->shared_lib_handle->init) {
- status = plugin->shared_lib_handle->init(plugin);
+ for (j = 0; j < plugin_set->plugins->len; j++) {
+ struct bt_plugin *candidate_plugin =
+ g_ptr_array_index(plugin_set->plugins, j);
- if (status < 0) {
- printf_verbose("Plugin `%s` initialization error: %d\n",
- plugin->shared_lib_handle->name, status);
+ if (strcmp(bt_plugin_get_name(candidate_plugin),
+ plugin_name) == 0) {
+ plugin = bt_get(candidate_plugin);
goto end;
}
}
- plugin->shared_lib_handle->init_called = true;
+end:
+ free(home_plugin_dir);
+ bt_put(plugin_set);
- /*
- * The initialization function should have added the component
- * classes at this point. We freeze the plugin so that it's not
- * possible to add component classes to this plugin object after
- * this stage (plugin object becomes immutable).
- */
- plugin->frozen = true;
+ if (dirs) {
+ g_ptr_array_free(dirs, TRUE);
+ }
-end:
- return status;
+ return plugin;
}
-struct bt_plugin *bt_plugin_create_from_file(const char *path)
+struct bt_component_class *bt_plugin_find_component_class(
+ const char *plugin_name, const char *comp_cls_name,
+ enum bt_component_class_type comp_cls_type)
{
- size_t path_len;
struct bt_plugin *plugin = NULL;
- bool is_libtool_wrapper = false, is_shared_object = false;
-
- if (!path) {
- goto error;
- }
+ struct bt_component_class *comp_cls = NULL;
- path_len = strlen(path);
- if (path_len <= PLUGIN_SUFFIX_LEN) {
- goto error;
- }
-
- path_len++;
- /*
- * Check if the file ends with a known plugin file type suffix (i.e. .so
- * or .la on Linux).
- */
- is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
- path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
- LIBTOOL_PLUGIN_SUFFIX_LEN);
- is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
- path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
- NATIVE_PLUGIN_SUFFIX_LEN);
- if (!is_shared_object && !is_libtool_wrapper) {
- /* Name indicates that this is not a plugin file. */
- goto error;
+ if (!plugin_name || !comp_cls_name) {
+ goto end;
}
- plugin = g_new0(struct bt_plugin, 1);
+ plugin = bt_plugin_find(plugin_name);
if (!plugin) {
- goto error;
- }
-
- bt_object_init(plugin, bt_plugin_destroy);
-
- /* Create shared lib handle */
- plugin->shared_lib_handle = bt_plugin_shared_lib_handle_create(path);
- if (!plugin->shared_lib_handle) {
- printf_verbose("Failed to create a shared library handle (path `%s`)\n",
- path);
- goto error;
- }
-
- /* Create empty array of component classes */
- plugin->comp_classes =
- g_ptr_array_new_with_free_func((GDestroyNotify) bt_put);
- if (!plugin->comp_classes) {
- goto error;
- }
-
- /* Initialize plugin */
- if (init_plugin(plugin) < 0) {
- goto error;
+ goto end;
}
- goto end;
-
-error:
- BT_PUT(plugin);
+ comp_cls = bt_plugin_get_component_class_by_name_and_type(
+ plugin, comp_cls_name, comp_cls_type);
end:
- return plugin;
+ bt_put(plugin);
+ return comp_cls;
}
/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
static
enum bt_plugin_status bt_plugin_create_append_all_from_dir(
- GPtrArray *plugins, const char *path, bool recurse)
+ struct bt_plugin_set *plugin_set, const char *path,
+ bt_bool recurse)
{
DIR *directory = NULL;
struct dirent *entry = NULL, *result = NULL;
char *file_path = NULL;
- size_t path_len = strlen(path);
+ size_t path_len;
enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
+ if (!path) {
+ ret = BT_PLUGIN_STATUS_ERROR;
+ goto end;
+ }
+
+ path_len = strlen(path);
+
if (path_len >= PATH_MAX) {
ret = BT_PLUGIN_STATUS_ERROR;
goto end;
directory = opendir(file_path);
if (!directory) {
- perror("Failed to open plug-in directory");
+ printf_verbose("Failed to open plugin directory \"%s\"\n",
+ file_path);
ret = BT_PLUGIN_STATUS_ERROR;
goto end;
}
strncpy(file_path + path_len, result->d_name, file_name_len);
file_path[path_len + file_name_len] = '\0';
-
stat_ret = stat(file_path, &st);
if (stat_ret < 0) {
/* Continue to next file / directory. */
}
if (S_ISDIR(st.st_mode) && recurse) {
- ret = bt_plugin_create_append_all_from_dir(plugins,
- file_path, true);
+ ret = bt_plugin_create_append_all_from_dir(plugin_set,
+ file_path, BT_TRUE);
if (ret < 0) {
goto end;
}
} else if (S_ISREG(st.st_mode)) {
- struct bt_plugin *plugin = bt_plugin_create_from_file(file_path);
+ struct bt_plugin_set *plugins_from_file =
+ bt_plugin_create_all_from_file(file_path);
+
+ if (plugins_from_file) {
+ size_t j;
- if (plugin) {
- /* Transfer ownership to array */
- g_ptr_array_add(plugins, plugin);
+ for (j = 0; j < plugins_from_file->plugins->len; j++) {
+ struct bt_plugin *plugin =
+ g_ptr_array_index(plugins_from_file->plugins, j);
+
+ bt_plugin_set_add_plugin(plugin_set,
+ plugin);
+ }
+
+ bt_put(plugins_from_file);
}
}
}
return ret;
}
-struct bt_plugin **bt_plugin_create_all_from_dir(const char *path,
- bool recurse)
+struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
+ bt_bool recurse)
{
- GPtrArray *plugins_array = NULL;
- struct bt_plugin **plugins = NULL;
+ struct bt_plugin_set *plugin_set;
enum bt_plugin_status status;
- if (!path) {
- goto error;
- }
-
- plugins_array = g_ptr_array_new();
- if (!plugins_array) {
+ plugin_set = bt_plugin_set_create();
+ if (!plugin_set) {
goto error;
}
/* Append found plugins to array */
- status = bt_plugin_create_append_all_from_dir(plugins_array, path,
+ status = bt_plugin_create_append_all_from_dir(plugin_set, path,
recurse);
if (status < 0) {
goto error;
}
- /* Add sentinel to array */
- g_ptr_array_add(plugins_array, NULL);
- plugins = (struct bt_plugin **) plugins_array->pdata;
- goto end;
-
-error:
- if (plugins_array) {
- g_ptr_array_free(plugins_array, TRUE);
- plugins_array = NULL;
- }
-
-end:
- if (plugins_array) {
- g_ptr_array_free(plugins_array, FALSE);
- }
-
- return plugins;
-}
-
-static
-struct bt_plugin *bt_plugin_create_from_static_at_index(size_t i)
-{
- struct bt_plugin *plugin = NULL;
-
- plugin = g_new0(struct bt_plugin, 1);
- if (!plugin) {
- goto error;
- }
-
- bt_object_init(plugin, bt_plugin_destroy);
-
- /* Create shared lib handle */
- plugin->shared_lib_handle = bt_plugin_shared_lib_handle_create(NULL);
- if (!plugin->shared_lib_handle) {
- goto error;
- }
-
- /* Fill shared lib handle */
- plugin->shared_lib_handle->init =
- (SECTION_BEGIN(__bt_plugin_init_funcs))[i];
- if (!plugin->shared_lib_handle->init) {
- goto error;
- }
-
- plugin->shared_lib_handle->exit =
- (SECTION_BEGIN(__bt_plugin_exit_funcs))[i];
- if (!plugin->shared_lib_handle->exit) {
- goto error;
- }
-
- plugin->shared_lib_handle->name = (SECTION_BEGIN(__bt_plugin_names))[i];
- plugin->shared_lib_handle->author =
- (SECTION_BEGIN(__bt_plugin_authors))[i];
- plugin->shared_lib_handle->license =
- (SECTION_BEGIN(__bt_plugin_licenses))[i];
- plugin->shared_lib_handle->description =
- (SECTION_BEGIN(__bt_plugin_descriptions))[i];
-
- /* Create empty array of component classes */
- plugin->comp_classes =
- g_ptr_array_new_with_free_func((GDestroyNotify) bt_put);
- if (!plugin->comp_classes) {
- goto error;
- }
-
- /* Initialize plugin */
- if (init_plugin(plugin) < 0) {
- goto error;
- }
-
- goto end;
-
-error:
- BT_PUT(plugin);
-
-end:
- return plugin;
-}
-
-struct bt_plugin **bt_plugin_create_all_from_static(void)
-{
- size_t count, i;
- struct bt_plugin **plugins = NULL;
-
- PRINT_PLUG_IN_SECTIONS(printf_verbose);
- count = SECTION_ELEMENT_COUNT(__bt_plugin_init_funcs);
- if (SECTION_ELEMENT_COUNT(__bt_plugin_exit_funcs) != count ||
- SECTION_ELEMENT_COUNT(__bt_plugin_names) != count ||
- SECTION_ELEMENT_COUNT(__bt_plugin_authors) != count ||
- SECTION_ELEMENT_COUNT(__bt_plugin_licenses) != count ||
- SECTION_ELEMENT_COUNT(__bt_plugin_descriptions) != count) {
- printf_error("Some statically-linked plug-ins do not define all the mandatory symbols\n");
- goto error;
- }
-
- printf_verbose("Detected %zu statically-linked plug-ins\n", count);
- plugins = g_new0(struct bt_plugin *, count + 1);
- if (!plugins) {
- goto error;
- }
-
- for (i = 0; i < count; i++) {
- struct bt_plugin *plugin =
- bt_plugin_create_from_static_at_index(i);
-
- if (!plugin) {
- printf_error("Cannot create statically-linked plug-in at index %zu\n",
- i);
- goto error;
- }
-
- /* Transfer ownership to the array */
- plugins[i] = plugin;
- }
-
goto end;
error:
- g_free(plugins);
+ BT_PUT(plugin_set);
end:
- return plugins;
+ return plugin_set;
}
const char *bt_plugin_get_name(struct bt_plugin *plugin)
{
- return plugin ? plugin->shared_lib_handle->name : NULL;
+ return plugin && plugin->info.name_set ? plugin->info.name->str : NULL;
}
const char *bt_plugin_get_author(struct bt_plugin *plugin)
{
- return plugin ? plugin->shared_lib_handle->author : NULL;
+ return plugin && plugin->info.author_set ? plugin->info.author->str : NULL;
}
const char *bt_plugin_get_license(struct bt_plugin *plugin)
{
- return plugin ? plugin->shared_lib_handle->license : NULL;
+ return plugin && plugin->info.license_set ? plugin->info.license->str : NULL;
}
const char *bt_plugin_get_path(struct bt_plugin *plugin)
{
- return (plugin && plugin->shared_lib_handle->path) ?
- plugin->shared_lib_handle->path->str : NULL;
+ return plugin && plugin->info.path_set ? plugin->info.path->str : NULL;
}
const char *bt_plugin_get_description(struct bt_plugin *plugin)
{
- return plugin ? plugin->shared_lib_handle->description : NULL;
+ return plugin && plugin->info.description_set ? plugin->info.description->str : NULL;
}
-int bt_plugin_get_component_class_count(struct bt_plugin *plugin)
+enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
+ unsigned int *major, unsigned int *minor, unsigned int *patch,
+ const char **extra)
{
- return plugin ? plugin->comp_classes->len : -1;
+ enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
+
+ if (!plugin || !plugin->info.version_set) {
+ status = BT_PLUGIN_STATUS_ERROR;
+ goto end;
+ }
+
+ if (major) {
+ *major = plugin->info.version.major;
+ }
+
+ if (minor) {
+ *minor = plugin->info.version.minor;
+ }
+
+ if (patch) {
+ *patch = plugin->info.version.patch;
+ }
+
+ if (extra) {
+ *extra = plugin->info.version.extra->str;
+ }
+
+end:
+ return status;
}
-struct bt_component_class *bt_plugin_get_component_class(
- struct bt_plugin *plugin, size_t index)
+int64_t bt_plugin_get_component_class_count(struct bt_plugin *plugin)
+{
+ return plugin ? plugin->comp_classes->len : (int64_t) -1;
+}
+
+struct bt_component_class *bt_plugin_get_component_class_by_index(
+ struct bt_plugin *plugin, uint64_t index)
{
struct bt_component_class *comp_class = NULL;
struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
struct bt_plugin *plugin, const char *name,
- enum bt_component_type type)
+ enum bt_component_class_type type)
{
struct bt_component_class *comp_class = NULL;
size_t i;
g_ptr_array_index(plugin->comp_classes, i);
const char *comp_class_cand_name =
bt_component_class_get_name(comp_class_candidate);
- enum bt_component_type comp_class_cand_type =
+ enum bt_component_class_type comp_class_cand_type =
bt_component_class_get_type(comp_class_candidate);
assert(comp_class_cand_name);
return comp_class;
}
-static
-void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
- void *data)
-{
- gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles,
- comp_class);
- assert(exists);
-}
-
enum bt_plugin_status bt_plugin_add_component_class(
struct bt_plugin *plugin, struct bt_component_class *comp_class)
{
enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
struct bt_component_class *comp_class_dup = NULL;
- int ret;
int comp_class_index = -1;
if (!plugin || !comp_class || plugin->frozen) {
bt_component_class_get_type(comp_class));
if (comp_class_dup) {
printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
- plugin->shared_lib_handle->name,
+ bt_plugin_get_name(plugin),
bt_component_class_get_name(comp_class),
bt_component_class_get_type(comp_class));
goto error;
comp_class_index = plugin->comp_classes->len;
g_ptr_array_add(plugin->comp_classes, bt_get(comp_class));
- /* Map component class pointer to shared lib handle in global HT */
- g_hash_table_insert(comp_classes_to_shlib_handles, comp_class,
- bt_get(plugin->shared_lib_handle));
-
- /* Add our custom destroy listener */
- ret = bt_component_class_add_destroy_listener(comp_class,
- plugin_comp_class_destroy_listener, NULL);
- if (ret) {
- goto error;
+ /* Special case for a shared object plugin */
+ if (plugin->type == BT_PLUGIN_TYPE_SO) {
+ bt_plugin_so_on_add_component_class(plugin, comp_class);
}
goto end;
error:
- /* Remove entry from global hash table (if exists) */
- g_hash_table_remove(comp_classes_to_shlib_handles,
- comp_class);
-
/* Remove entry from plugin's component classes (if added) */
if (comp_class_index >= 0) {
g_ptr_array_remove_index(plugin->comp_classes,