2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_LOG_TAG "PLUGIN"
27 #include <babeltrace/lib-logging-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/object.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/plugin/plugin-internal.h>
34 #include <babeltrace/plugin/plugin-so-internal.h>
35 #include <babeltrace/plugin/plugin-const.h>
36 #include <babeltrace/graph/component-class-const.h>
37 #include <babeltrace/graph/component-class-internal.h>
38 #include <babeltrace/types.h>
39 #include <babeltrace/assert-internal.h>
40 #include <babeltrace/assert-pre-internal.h>
50 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
51 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
52 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
54 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
56 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
57 #include <babeltrace/plugin/python-plugin-provider-internal.h>
60 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
61 bt_plugin_python_create_all_from_file
;
64 void init_python_plugin_provider(void) {}
65 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
66 static GModule
*python_plugin_provider_module
;
68 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
71 void init_python_plugin_provider(void) {
72 if (bt_plugin_python_create_all_from_file_sym
!= NULL
) {
76 BT_LOGD_STR("Loading Python plugin provider module.");
77 python_plugin_provider_module
=
78 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
, 0);
79 if (!python_plugin_provider_module
) {
80 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
81 PYTHON_PLUGIN_PROVIDER_FILENAME
, g_module_error());
85 if (!g_module_symbol(python_plugin_provider_module
,
86 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
87 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
88 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
89 "file=\"%s\", symbol=\"%s\"",
90 PYTHON_PLUGIN_PROVIDER_FILENAME
,
91 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
95 BT_LOGI("Loaded Python plugin provider module: addr=%p",
96 python_plugin_provider_module
);
99 __attribute__((destructor
)) static
100 void fini_python_plugin_provider(void) {
101 if (python_plugin_provider_module
) {
102 BT_LOGD("Unloading Python plugin provider module.");
104 if (!g_module_close(python_plugin_provider_module
)) {
105 BT_LOGE("Failed to close the Python plugin provider module: %s.",
109 python_plugin_provider_module
= NULL
;
114 uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
116 BT_ASSERT_PRE_NON_NULL(plugin_set
, "Plugin set");
117 return (uint64_t) plugin_set
->plugins
->len
;
120 const struct bt_plugin
*bt_plugin_set_borrow_plugin_by_index_const(
121 const struct bt_plugin_set
*plugin_set
, uint64_t index
)
123 BT_ASSERT_PRE_NON_NULL(plugin_set
, "Plugin set");
124 BT_ASSERT_PRE_VALID_INDEX(index
, plugin_set
->plugins
->len
);
125 return g_ptr_array_index(plugin_set
->plugins
, index
);
128 const struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
130 /* bt_plugin_so_create_all_from_static() logs errors */
131 return bt_plugin_so_create_all_from_static();
134 const struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
136 struct bt_plugin_set
*plugin_set
= NULL
;
138 BT_ASSERT_PRE_NON_NULL(path
, "Path");
139 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
141 /* Try shared object plugins */
142 plugin_set
= bt_plugin_so_create_all_from_file(path
);
147 /* Try Python plugins if support is available */
148 init_python_plugin_provider();
149 if (bt_plugin_python_create_all_from_file_sym
) {
150 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
158 BT_LOGD("Created %u plugins from file: "
159 "path=\"%s\", count=%u, plugin-set-addr=%p",
160 plugin_set
->plugins
->len
, path
,
161 plugin_set
->plugins
->len
, plugin_set
);
163 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
169 static void destroy_gstring(void *data
)
171 g_string_free(data
, TRUE
);
174 const struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
176 const char *system_plugin_dir
;
177 char *home_plugin_dir
= NULL
;
179 const struct bt_plugin
*plugin
= NULL
;
180 const struct bt_plugin_set
*plugin_set
= NULL
;
181 GPtrArray
*dirs
= NULL
;
185 BT_ASSERT_PRE_NON_NULL(plugin_name
, "Name");
186 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
187 "name=\"%s\"", plugin_name
);
188 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
190 BT_LOGE_STR("Failed to allocate a GPtrArray.");
197 * 1. BABELTRACE_PLUGIN_PATH environment variable
198 * (colon-separated list of directories)
199 * 2. ~/.local/lib/babeltrace/plugins
200 * 3. Default system directory for Babeltrace plugins, usually
201 * /usr/lib/babeltrace/plugins or
202 * /usr/local/lib/babeltrace/plugins if installed
204 * 4. Built-in plugins (static)
206 * Directories are searched non-recursively.
208 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
210 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
212 BT_LOGE_STR("Failed to append plugin path to array of directories.");
217 home_plugin_dir
= bt_common_get_home_plugin_path();
218 if (home_plugin_dir
) {
219 GString
*home_plugin_dir_str
=
220 g_string_new(home_plugin_dir
);
222 if (!home_plugin_dir_str
) {
223 BT_LOGE_STR("Failed to allocate a GString.");
227 g_ptr_array_add(dirs
, home_plugin_dir_str
);
230 system_plugin_dir
= bt_common_get_system_plugin_path();
231 if (system_plugin_dir
) {
232 GString
*system_plugin_dir_str
=
233 g_string_new(system_plugin_dir
);
235 if (!system_plugin_dir_str
) {
236 BT_LOGE_STR("Failed to allocate a GString.");
240 g_ptr_array_add(dirs
, system_plugin_dir_str
);
243 for (i
= 0; i
< dirs
->len
; i
++) {
244 GString
*dir
= g_ptr_array_index(dirs
, i
);
246 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
249 * Skip this if the directory does not exist because
250 * bt_plugin_create_all_from_dir() would log a warning.
252 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
253 BT_LOGV("Skipping nonexistent directory path: "
254 "path=\"%s\"", dir
->str
);
258 /* bt_plugin_create_all_from_dir() logs details/errors */
259 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
261 BT_LOGD("No plugins found in directory: path=\"%s\"",
266 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
267 const struct bt_plugin
*candidate_plugin
=
268 g_ptr_array_index(plugin_set
->plugins
, j
);
270 if (strcmp(bt_plugin_get_name(candidate_plugin
),
272 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
273 plugin_name
, dir
->str
);
274 plugin
= candidate_plugin
;
275 bt_object_get_no_null_check(plugin
);
280 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
281 plugin_name
, dir
->str
);
284 bt_object_put_ref(plugin_set
);
285 plugin_set
= bt_plugin_create_all_from_static();
287 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
288 const struct bt_plugin
*candidate_plugin
=
289 g_ptr_array_index(plugin_set
->plugins
, j
);
291 if (strcmp(bt_plugin_get_name(candidate_plugin
),
293 BT_LOGD("Plugin found in built-in plugins: "
294 "name=\"%s\"", plugin_name
);
295 plugin
= candidate_plugin
;
296 bt_object_get_no_null_check(plugin
);
303 free(home_plugin_dir
);
304 bt_object_put_ref(plugin_set
);
307 g_ptr_array_free(dirs
, TRUE
);
311 BT_LIB_LOGD("Found plugin in standard directories and built-in plugins: "
314 BT_LOGD("No plugin found in standard directories and built-in plugins: "
315 "name=\"%s\"", plugin_name
);
322 pthread_mutex_t lock
;
323 struct bt_plugin_set
*plugin_set
;
325 } append_all_from_dir_info
= {
326 .lock
= PTHREAD_MUTEX_INITIALIZER
330 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
334 const char *name
= file
+ s
->base
;
336 /* Check for recursion */
337 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
344 const struct bt_plugin_set
*plugins_from_file
;
346 if (name
[0] == '.') {
347 /* Skip hidden files */
348 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
352 plugins_from_file
= bt_plugin_create_all_from_file(file
);
354 if (plugins_from_file
) {
357 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
358 struct bt_plugin
*plugin
=
359 g_ptr_array_index(plugins_from_file
->plugins
, j
);
361 BT_LIB_LOGD("Adding plugin to plugin set: "
362 "plugin-path=\"%s\", %![plugin-]+l",
364 bt_plugin_set_add_plugin(
365 append_all_from_dir_info
.plugin_set
,
369 bt_object_put_ref(plugins_from_file
);
374 /* Continue to next file / directory. */
375 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
378 /* Continue to next file / directory. */
379 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
388 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
389 struct bt_plugin_set
*plugin_set
, const char *path
,
392 int nftw_flags
= FTW_PHYS
;
393 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
395 BT_ASSERT(plugin_set
);
397 BT_ASSERT(strlen(path
) < PATH_MAX
);
398 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
399 append_all_from_dir_info
.plugin_set
= plugin_set
;
400 append_all_from_dir_info
.recurse
= recurse
;
401 ret
= nftw(path
, nftw_append_all_from_dir
,
402 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
403 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
405 BT_LOGW_ERRNO("Cannot open directory", ": path=\"%s\"", path
);
406 ret
= BT_PLUGIN_STATUS_ERROR
;
412 const struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
415 struct bt_plugin_set
*plugin_set
;
416 enum bt_plugin_status status
;
418 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
420 plugin_set
= bt_plugin_set_create();
422 BT_LOGE_STR("Cannot create empty plugin set.");
426 /* Append found plugins to array */
427 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
430 BT_LOGW("Cannot append plugins found in directory: "
431 "path=\"%s\", status=%s",
432 path
, bt_plugin_status_string(status
));
436 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
437 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
441 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
447 const char *bt_plugin_get_name(const struct bt_plugin
*plugin
)
449 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
450 return plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
453 const char *bt_plugin_get_author(const struct bt_plugin
*plugin
)
455 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
456 return plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
459 const char *bt_plugin_get_license(const struct bt_plugin
*plugin
)
461 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
462 return plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
465 const char *bt_plugin_get_path(const struct bt_plugin
*plugin
)
467 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
468 return plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
471 const char *bt_plugin_get_description(const struct bt_plugin
*plugin
)
473 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
474 return plugin
->info
.description_set
?
475 plugin
->info
.description
->str
: NULL
;
478 enum bt_property_availability
bt_plugin_get_version(const struct bt_plugin
*plugin
,
479 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
482 enum bt_property_availability avail
=
483 BT_PROPERTY_AVAILABILITY_AVAILABLE
;
485 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
487 if (!plugin
->info
.version_set
) {
488 BT_LIB_LOGV("Plugin's version is not set: %!+l", plugin
);
489 avail
= BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
494 *major
= plugin
->info
.version
.major
;
498 *minor
= plugin
->info
.version
.minor
;
502 *patch
= plugin
->info
.version
.patch
;
506 *extra
= plugin
->info
.version
.extra
->str
;
513 uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin
*plugin
)
515 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
516 return (uint64_t) plugin
->src_comp_classes
->len
;
519 uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin
*plugin
)
521 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
522 return (uint64_t) plugin
->flt_comp_classes
->len
;
525 uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin
*plugin
)
527 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
528 return (uint64_t) plugin
->sink_comp_classes
->len
;
532 struct bt_component_class
*borrow_component_class_by_index(
533 const struct bt_plugin
*plugin
, GPtrArray
*comp_classes
,
536 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
537 BT_ASSERT_PRE_VALID_INDEX(index
, comp_classes
->len
);
538 return g_ptr_array_index(comp_classes
, index
);
541 const struct bt_component_class_source
*
542 bt_plugin_borrow_source_component_class_by_index_const_const(
543 const struct bt_plugin
*plugin
, uint64_t index
)
545 return (const void *) borrow_component_class_by_index(plugin
,
546 plugin
->src_comp_classes
, index
);
549 const struct bt_component_class_filter
*
550 bt_plugin_borrow_filter_component_class_by_index_const(
551 const struct bt_plugin
*plugin
, uint64_t index
)
553 return (const void *) borrow_component_class_by_index(plugin
,
554 plugin
->flt_comp_classes
, index
);
557 const struct bt_component_class_sink
*
558 bt_plugin_borrow_sink_component_class_by_index_const(
559 const struct bt_plugin
*plugin
, uint64_t index
)
561 return (const void *) borrow_component_class_by_index(plugin
,
562 plugin
->sink_comp_classes
, index
);
566 struct bt_component_class
*borrow_component_class_by_name(
567 const struct bt_plugin
*plugin
, GPtrArray
*comp_classes
,
570 struct bt_component_class
*comp_class
= NULL
;
573 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
574 BT_ASSERT_PRE_NON_NULL(name
, "Name");
576 for (i
= 0; i
< comp_classes
->len
; i
++) {
577 struct bt_component_class
*comp_class_candidate
=
578 g_ptr_array_index(comp_classes
, i
);
579 const char *comp_class_cand_name
=
580 bt_component_class_get_name(comp_class_candidate
);
582 BT_ASSERT(comp_class_cand_name
);
584 if (strcmp(name
, comp_class_cand_name
) == 0) {
585 comp_class
= comp_class_candidate
;
593 const struct bt_component_class_source
*
594 bt_plugin_borrow_source_component_class_by_name_const(
595 const struct bt_plugin
*plugin
, const char *name
)
597 return (const void *) borrow_component_class_by_name(plugin
,
598 plugin
->src_comp_classes
, name
);
601 const struct bt_component_class_filter
*
602 bt_plugin_borrow_filter_component_class_by_name_const(
603 const struct bt_plugin
*plugin
, const char *name
)
605 return (const void *) borrow_component_class_by_name(plugin
,
606 plugin
->flt_comp_classes
, name
);
609 const struct bt_component_class_sink
*
610 bt_plugin_borrow_sink_component_class_by_name_const(
611 const struct bt_plugin
*plugin
, const char *name
)
613 return (const void *) borrow_component_class_by_name(plugin
,
614 plugin
->sink_comp_classes
, name
);