#include <babeltrace2/types.h>
#include <glib.h>
#include <unistd.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include "plugin-so.h"
#include "lib/func-status.h"
-#define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace2-python-plugin-provider." G_MODULE_SUFFIX
+#define PYTHON_PLUGIN_PROVIDER_FILENAME "babeltrace2-python-plugin-provider." G_MODULE_SUFFIX
+#define PYTHON_PLUGIN_PROVIDER_DIR BABELTRACE_PLUGIN_PROVIDERS_DIR
#define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
#define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR G_STRINGIFY(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
+/* Declare here to make sure definition in both ifdef branches are in sync. */
+static
+int init_python_plugin_provider(void);
+typedef int (*create_all_from_file_sym_type)(
+ const char *path,
+ bool fail_on_load_error,
+ struct bt_plugin_set **plugin_set_out);
+
#ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
#include <plugin/python-plugin-provider.h>
static
-int (*bt_plugin_python_create_all_from_file_sym)(
- const char *path, bool fail_on_load_error,
- struct bt_plugin_set **plugin_set_out) =
- bt_plugin_python_create_all_from_file;
+create_all_from_file_sym_type
+ bt_plugin_python_create_all_from_file_sym =
+ bt_plugin_python_create_all_from_file;
static
-enum bt_plugin_status init_python_plugin_provider(void)
+int init_python_plugin_provider(void)
{
}
#else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
static GModule *python_plugin_provider_module;
static
-int (*bt_plugin_python_create_all_from_file_sym)(
- const char *path, bool fail_on_load_error,
- struct bt_plugin_set **plugin_set_out);
+create_all_from_file_sym_type bt_plugin_python_create_all_from_file_sym;
static
int init_python_plugin_provider(void) {
int status = BT_FUNC_STATUS_OK;
+ const char *provider_dir_envvar;
+ static const char * const provider_dir_envvar_name = "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR";
+ char *provider_path = NULL;
- if (bt_plugin_python_create_all_from_file_sym != NULL) {
+ if (bt_plugin_python_create_all_from_file_sym) {
goto end;
}
BT_LOGI_STR("Loading Python plugin provider module.");
- python_plugin_provider_module =
- g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME, 0);
+
+ provider_dir_envvar = getenv(provider_dir_envvar_name);
+ if (provider_dir_envvar) {
+ provider_path = g_build_filename(provider_dir_envvar,
+ PYTHON_PLUGIN_PROVIDER_FILENAME, NULL);
+ BT_LOGI("Using `%s` environment variable to find the Python "
+ "plugin provider: path=\"%s\"", provider_dir_envvar_name,
+ provider_path);
+ } else {
+ provider_path = g_build_filename(PYTHON_PLUGIN_PROVIDER_DIR,
+ PYTHON_PLUGIN_PROVIDER_FILENAME, NULL);
+ BT_LOGI("Using default path (`%s` environment variable is not "
+ "set) to find the Python plugin provider: path=\"%s\"",
+ provider_dir_envvar_name, provider_path);
+ }
+
+ python_plugin_provider_module = g_module_open(provider_path, 0);
if (!python_plugin_provider_module) {
/*
* This is not an error. The whole point of having an
* missing and the Babeltrace library still works.
*/
BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
- PYTHON_PLUGIN_PROVIDER_FILENAME, g_module_error());
+ provider_path, g_module_error());
goto end;
}
"%s: continuing without Python plugin support: "
"file=\"%s\", symbol=\"%s\"",
g_module_error(),
- PYTHON_PLUGIN_PROVIDER_FILENAME,
+ provider_path,
PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
status = BT_FUNC_STATUS_ERROR;
goto end;
python_plugin_provider_module);
end:
+ g_free(provider_path);
+
return status;
}
}
#endif
-uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
+uint64_t bt_plugin_set_get_plugin_count(const struct bt_plugin_set *plugin_set)
{
BT_ASSERT_PRE_DEV_NON_NULL(plugin_set, "Plugin set");
return (uint64_t) plugin_set->plugins->len;
return status;
}
-static void destroy_gstring(void *data)
+static
+void destroy_gstring(void *data)
{
g_string_free(data, TRUE);
}
-enum bt_plugin_find_status bt_plugin_find(const char *plugin_name,
- bt_bool fail_on_load_error, const struct bt_plugin **plugin_out)
+enum bt_plugin_find_all_status bt_plugin_find_all(bt_bool find_in_std_env_var,
+ bt_bool find_in_user_dir, bt_bool find_in_sys_dir,
+ bt_bool find_in_static, bt_bool fail_on_load_error,
+ const struct bt_plugin_set **plugin_set_out)
{
- const char *system_plugin_dir;
char *home_plugin_dir = NULL;
- const char *envvar;
- const struct bt_plugin *plugin = NULL;
const struct bt_plugin_set *plugin_set = NULL;
GPtrArray *dirs = NULL;
int ret;
int status = BT_FUNC_STATUS_OK;
- size_t i, j;
+ uint64_t dir_i, plugin_i;
- BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
- BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)");
- BT_LOGI("Finding named plugin in standard directories and built-in plugins: "
- "name=\"%s\"", plugin_name);
+ BT_ASSERT_PRE_NON_NULL(plugin_set_out, "Plugin set (output)");
+ BT_LOGI("Finding all plugins in standard directories and built-in plugins: "
+ "find-in-std-env-var=%d, find-in-user-dir=%d, "
+ "find-in-sys-dir=%d, find-in-static=%d",
+ find_in_std_env_var, find_in_user_dir, find_in_sys_dir,
+ find_in_static);
dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
if (!dirs) {
BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
goto end;
}
+ *plugin_set_out = bt_plugin_set_create();
+ if (!*plugin_set_out) {
+ BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
+ status = BT_FUNC_STATUS_MEMORY_ERROR;
+ goto end;
+ }
+
/*
* Search order is:
*
- * 1. BABELTRACE_PLUGIN_PATH environment variable
+ * 1. `BABELTRACE_PLUGIN_PATH` environment variable
* (colon-separated list of directories)
- * 2. ~/.local/lib/babeltrace2/plugins
+ * 2. `~/.local/lib/babeltrace2/plugins`
* 3. Default system directory for Babeltrace plugins, usually
- * /usr/lib/babeltrace2/plugins or
- * /usr/local/lib/babeltrace2/plugins if installed
- * locally
+ * `/usr/lib/babeltrace2/plugins` or
+ * `/usr/local/lib/babeltrace2/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) {
- BT_LIB_LOGE_APPEND_CAUSE(
- "Failed to append plugin path to array of directories.");
- status = BT_FUNC_STATUS_MEMORY_ERROR;
- goto end;
+ if (find_in_std_env_var) {
+ const char *envvar = getenv("BABELTRACE_PLUGIN_PATH");
+
+ if (envvar) {
+ ret = bt_common_append_plugin_path_dirs(envvar, dirs);
+ if (ret) {
+ BT_LIB_LOGE_APPEND_CAUSE(
+ "Failed to append plugin path to array of directories.");
+ status = BT_FUNC_STATUS_MEMORY_ERROR;
+ goto end;
+ }
}
}
- home_plugin_dir = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
- if (home_plugin_dir) {
- GString *home_plugin_dir_str = g_string_new(home_plugin_dir);
+ if (find_in_user_dir) {
+ home_plugin_dir = bt_common_get_home_plugin_path(
+ BT_LOG_OUTPUT_LEVEL);
+ if (home_plugin_dir) {
+ GString *home_plugin_dir_str = g_string_new(
+ home_plugin_dir);
- if (!home_plugin_dir_str) {
- BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
- status = BT_FUNC_STATUS_MEMORY_ERROR;
- goto end;
- }
+ if (!home_plugin_dir_str) {
+ BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
+ status = BT_FUNC_STATUS_MEMORY_ERROR;
+ goto end;
+ }
- g_ptr_array_add(dirs, home_plugin_dir_str);
+ g_ptr_array_add(dirs, home_plugin_dir_str);
+ }
}
- system_plugin_dir = bt_common_get_system_plugin_path();
- if (system_plugin_dir) {
- GString *system_plugin_dir_str =
- g_string_new(system_plugin_dir);
+ if (find_in_sys_dir) {
+ const char *system_plugin_dir =
+ bt_common_get_system_plugin_path();
- if (!system_plugin_dir_str) {
- BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
- status = BT_FUNC_STATUS_MEMORY_ERROR;
- goto end;
- }
+ if (system_plugin_dir) {
+ GString *system_plugin_dir_str =
+ g_string_new(system_plugin_dir);
+
+ if (!system_plugin_dir_str) {
+ BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
+ status = BT_FUNC_STATUS_MEMORY_ERROR;
+ goto end;
+ }
- g_ptr_array_add(dirs, system_plugin_dir_str);
+ g_ptr_array_add(dirs, system_plugin_dir_str);
+ }
}
- for (i = 0; i < dirs->len; i++) {
- GString *dir = g_ptr_array_index(dirs, i);
+ for (dir_i = 0; dir_i < dirs->len; dir_i++) {
+ GString *dir = dirs->pdata[dir_i];
BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
BT_ASSERT(status == BT_FUNC_STATUS_OK);
BT_ASSERT(plugin_set);
+ BT_LOGI("Found plugins in directory: path=\"%s\", count=%u",
+ dir->str, plugin_set->plugins->len);
- for (j = 0; j < plugin_set->plugins->len; j++) {
- const struct bt_plugin *candidate_plugin =
- g_ptr_array_index(plugin_set->plugins, j);
+ for (plugin_i = 0; plugin_i < plugin_set->plugins->len;
+ plugin_i++) {
+ bt_plugin_set_add_plugin((void *) *plugin_set_out,
+ plugin_set->plugins->pdata[plugin_i]);
+ }
+ }
- if (strcmp(bt_plugin_get_name(candidate_plugin),
- plugin_name) == 0) {
- BT_LOGI("Plugin found in directory: name=\"%s\", path=\"%s\"",
- plugin_name, dir->str);
- *plugin_out = candidate_plugin;
- bt_object_get_no_null_check(*plugin_out);
- goto end;
- }
+ if (find_in_static) {
+ BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
+ status = bt_plugin_find_all_from_static(fail_on_load_error,
+ &plugin_set);
+ if (status < 0) {
+ BT_ASSERT(!plugin_set);
+ goto end;
+ } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
+ BT_ASSERT(!plugin_set);
+ BT_LOGI_STR("No plugins found in built-in plugins.");
+ goto end;
}
- BT_LOGI("Plugin not found in directory: name=\"%s\", path=\"%s\"",
- plugin_name, dir->str);
+ BT_ASSERT(status == BT_FUNC_STATUS_OK);
+ BT_ASSERT(plugin_set);
+ BT_LOGI("Found built-in plugins: count=%u",
+ plugin_set->plugins->len);
+
+ for (plugin_i = 0; plugin_i < plugin_set->plugins->len;
+ plugin_i++) {
+ bt_plugin_set_add_plugin((void *) *plugin_set_out,
+ plugin_set->plugins->pdata[plugin_i]);
+ }
+ }
+
+end:
+ free(home_plugin_dir);
+ bt_object_put_ref(plugin_set);
+
+ if (dirs) {
+ g_ptr_array_free(dirs, TRUE);
}
- BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
- status = bt_plugin_find_all_from_static(fail_on_load_error,
- &plugin_set);
if (status < 0) {
+ BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
+ } else {
+ BT_ASSERT(*plugin_set_out);
+
+ if ((*plugin_set_out)->plugins->len > 0) {
+ BT_LOGI("Found plugins in standard directories and built-in plugins: "
+ "count=%u", (*plugin_set_out)->plugins->len);
+ status = BT_FUNC_STATUS_OK;
+ } else {
+ BT_LOGI_STR("No plugins found in standard directories and built-in plugins.");
+ status = BT_FUNC_STATUS_NOT_FOUND;
+ BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
+ }
+ }
+
+ return status;
+}
+
+enum bt_plugin_find_status bt_plugin_find(const char *plugin_name,
+ bt_bool find_in_std_env_var, bt_bool find_in_user_dir,
+ bt_bool find_in_sys_dir, bt_bool find_in_static,
+ bt_bool fail_on_load_error, const struct bt_plugin **plugin_out)
+{
+ enum bt_plugin_find_status status;
+ const struct bt_plugin_set *plugin_set = NULL;
+ uint64_t i;
+
+ BT_ASSERT_PRE_NON_NULL(plugin_name, "Name");
+ BT_ASSERT_PRE_NON_NULL(plugin_out, "Plugin (output)");
+ BT_LOGI("Finding named plugin in standard directories and built-in plugins: "
+ "name=\"%s\", find-in-std-env-var=%d, find-in-user-dir=%d, "
+ "find-in-sys-dir=%d, find-in-static=%d",
+ plugin_name, find_in_std_env_var, find_in_user_dir,
+ find_in_sys_dir, find_in_static);
+ status = (enum bt_plugin_find_status) bt_plugin_find_all(find_in_std_env_var, find_in_user_dir,
+ find_in_sys_dir, find_in_static, fail_on_load_error,
+ &plugin_set);
+ if (status != BT_FUNC_STATUS_OK) {
BT_ASSERT(!plugin_set);
goto end;
- } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
- BT_ASSERT(!plugin_set);
- BT_LOGI_STR("No plugins found in built-in plugins.");
- goto end;
}
- BT_ASSERT(status == BT_FUNC_STATUS_OK);
BT_ASSERT(plugin_set);
- for (j = 0; j < plugin_set->plugins->len; j++) {
- const struct bt_plugin *candidate_plugin =
- g_ptr_array_index(plugin_set->plugins, j);
+ for (i = 0; i < plugin_set->plugins->len; i++) {
+ const struct bt_plugin *plugin = plugin_set->plugins->pdata[i];
- if (strcmp(bt_plugin_get_name(candidate_plugin),
- plugin_name) == 0) {
- BT_LOGI("Plugin found in built-in plugins: "
- "name=\"%s\"", plugin_name);
- *plugin_out = candidate_plugin;
- bt_object_get_no_null_check(*plugin_out);
+ if (strcmp(plugin->info.name->str, plugin_name) == 0) {
+ *plugin_out = plugin;
+ bt_object_get_ref_no_null_check(*plugin_out);
goto end;
}
}
status = BT_FUNC_STATUS_NOT_FOUND;
end:
- free(home_plugin_dir);
- bt_object_put_ref(plugin_set);
-
- if (dirs) {
- g_ptr_array_free(dirs, TRUE);
- }
-
if (status == BT_FUNC_STATUS_OK) {
+ BT_ASSERT(*plugin_out);
BT_LIB_LOGI("Found plugin in standard directories and built-in plugins: "
- "%!+l", plugin);
- } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
+ "%!+l", *plugin_out);
+ } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
BT_LOGI("No plugin found in standard directories and built-in plugins: "
"name=\"%s\"", plugin_name);
}
+ bt_plugin_set_put_ref(plugin_set);
+
return status;
}
const char *comp_class_cand_name =
bt_component_class_get_name(comp_class_candidate);
- BT_ASSERT(comp_class_cand_name);
+ BT_ASSERT_DBG(comp_class_cand_name);
if (strcmp(name, comp_class_cand_name) == 0) {
comp_class = comp_class_candidate;