2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@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/assert-internal.h>
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <babeltrace/compiler-internal.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/plugin/plugin-internal.h>
35 #include <babeltrace/plugin/plugin-so-internal.h>
36 #include <babeltrace/plugin/plugin-const.h>
37 #include <babeltrace/graph/component-class-const.h>
38 #include <babeltrace/graph/component-class-internal.h>
39 #include <babeltrace/types.h>
49 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
50 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
51 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
53 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
55 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
56 #include <babeltrace/plugin/python-plugin-provider-internal.h>
59 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
60 bt_plugin_python_create_all_from_file
;
63 void init_python_plugin_provider(void) {}
64 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
65 static GModule
*python_plugin_provider_module
;
67 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
70 void init_python_plugin_provider(void) {
71 if (bt_plugin_python_create_all_from_file_sym
!= NULL
) {
75 BT_LOGD_STR("Loading Python plugin provider module.");
76 python_plugin_provider_module
=
77 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
, 0);
78 if (!python_plugin_provider_module
) {
79 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
80 PYTHON_PLUGIN_PROVIDER_FILENAME
, g_module_error());
84 if (!g_module_symbol(python_plugin_provider_module
,
85 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
86 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
87 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
88 "file=\"%s\", symbol=\"%s\"",
89 PYTHON_PLUGIN_PROVIDER_FILENAME
,
90 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
94 BT_LOGI("Loaded Python plugin provider module: addr=%p",
95 python_plugin_provider_module
);
98 __attribute__((destructor
)) static
99 void fini_python_plugin_provider(void) {
100 if (python_plugin_provider_module
) {
101 BT_LOGD("Unloading Python plugin provider module.");
103 if (!g_module_close(python_plugin_provider_module
)) {
104 BT_LOGE("Failed to close the Python plugin provider module: %s.",
108 python_plugin_provider_module
= NULL
;
113 uint64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
115 BT_ASSERT_PRE_NON_NULL(plugin_set
, "Plugin set");
116 return (uint64_t) plugin_set
->plugins
->len
;
119 const struct bt_plugin
*bt_plugin_set_borrow_plugin_by_index_const(
120 const struct bt_plugin_set
*plugin_set
, uint64_t index
)
122 BT_ASSERT_PRE_NON_NULL(plugin_set
, "Plugin set");
123 BT_ASSERT_PRE_VALID_INDEX(index
, plugin_set
->plugins
->len
);
124 return g_ptr_array_index(plugin_set
->plugins
, index
);
127 const struct bt_plugin_set
*bt_plugin_find_all_from_static(void)
129 /* bt_plugin_so_create_all_from_static() logs errors */
130 return bt_plugin_so_create_all_from_static();
133 const struct bt_plugin_set
*bt_plugin_find_all_from_file(const char *path
)
135 struct bt_plugin_set
*plugin_set
= NULL
;
137 BT_ASSERT_PRE_NON_NULL(path
, "Path");
138 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
140 /* Try shared object plugins */
141 plugin_set
= bt_plugin_so_create_all_from_file(path
);
146 /* Try Python plugins if support is available */
147 init_python_plugin_provider();
148 if (bt_plugin_python_create_all_from_file_sym
) {
149 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
157 BT_LOGD("Created %u plugins from file: "
158 "path=\"%s\", count=%u, plugin-set-addr=%p",
159 plugin_set
->plugins
->len
, path
,
160 plugin_set
->plugins
->len
, plugin_set
);
162 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
168 static void destroy_gstring(void *data
)
170 g_string_free(data
, TRUE
);
173 const struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
175 const char *system_plugin_dir
;
176 char *home_plugin_dir
= NULL
;
178 const struct bt_plugin
*plugin
= NULL
;
179 const struct bt_plugin_set
*plugin_set
= NULL
;
180 GPtrArray
*dirs
= NULL
;
184 BT_ASSERT_PRE_NON_NULL(plugin_name
, "Name");
185 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
186 "name=\"%s\"", plugin_name
);
187 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
189 BT_LOGE_STR("Failed to allocate a GPtrArray.");
196 * 1. BABELTRACE_PLUGIN_PATH environment variable
197 * (colon-separated list of directories)
198 * 2. ~/.local/lib/babeltrace/plugins
199 * 3. Default system directory for Babeltrace plugins, usually
200 * /usr/lib/babeltrace/plugins or
201 * /usr/local/lib/babeltrace/plugins if installed
203 * 4. Built-in plugins (static)
205 * Directories are searched non-recursively.
207 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
209 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
211 BT_LOGE_STR("Failed to append plugin path to array of directories.");
216 home_plugin_dir
= bt_common_get_home_plugin_path();
217 if (home_plugin_dir
) {
218 GString
*home_plugin_dir_str
=
219 g_string_new(home_plugin_dir
);
221 if (!home_plugin_dir_str
) {
222 BT_LOGE_STR("Failed to allocate a GString.");
226 g_ptr_array_add(dirs
, home_plugin_dir_str
);
229 system_plugin_dir
= bt_common_get_system_plugin_path();
230 if (system_plugin_dir
) {
231 GString
*system_plugin_dir_str
=
232 g_string_new(system_plugin_dir
);
234 if (!system_plugin_dir_str
) {
235 BT_LOGE_STR("Failed to allocate a GString.");
239 g_ptr_array_add(dirs
, system_plugin_dir_str
);
242 for (i
= 0; i
< dirs
->len
; i
++) {
243 GString
*dir
= g_ptr_array_index(dirs
, i
);
245 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
248 * Skip this if the directory does not exist because
249 * bt_plugin_find_all_from_dir() would log a warning.
251 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
252 BT_LOGV("Skipping nonexistent directory path: "
253 "path=\"%s\"", dir
->str
);
257 /* bt_plugin_find_all_from_dir() logs details/errors */
258 plugin_set
= bt_plugin_find_all_from_dir(dir
->str
, BT_FALSE
);
260 BT_LOGD("No plugins found in directory: path=\"%s\"",
265 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
266 const struct bt_plugin
*candidate_plugin
=
267 g_ptr_array_index(plugin_set
->plugins
, j
);
269 if (strcmp(bt_plugin_get_name(candidate_plugin
),
271 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
272 plugin_name
, dir
->str
);
273 plugin
= candidate_plugin
;
274 bt_object_get_no_null_check(plugin
);
279 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
280 plugin_name
, dir
->str
);
283 bt_object_put_ref(plugin_set
);
284 plugin_set
= bt_plugin_find_all_from_static();
286 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
287 const struct bt_plugin
*candidate_plugin
=
288 g_ptr_array_index(plugin_set
->plugins
, j
);
290 if (strcmp(bt_plugin_get_name(candidate_plugin
),
292 BT_LOGD("Plugin found in built-in plugins: "
293 "name=\"%s\"", plugin_name
);
294 plugin
= candidate_plugin
;
295 bt_object_get_no_null_check(plugin
);
302 free(home_plugin_dir
);
303 bt_object_put_ref(plugin_set
);
306 g_ptr_array_free(dirs
, TRUE
);
310 BT_LIB_LOGD("Found plugin in standard directories and built-in plugins: "
313 BT_LOGD("No plugin found in standard directories and built-in plugins: "
314 "name=\"%s\"", plugin_name
);
321 pthread_mutex_t lock
;
322 struct bt_plugin_set
*plugin_set
;
324 } append_all_from_dir_info
= {
325 .lock
= PTHREAD_MUTEX_INITIALIZER
329 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
333 const char *name
= file
+ s
->base
;
335 /* Check for recursion */
336 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
343 const struct bt_plugin_set
*plugins_from_file
;
345 if (name
[0] == '.') {
346 /* Skip hidden files */
347 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
351 plugins_from_file
= bt_plugin_find_all_from_file(file
);
353 if (plugins_from_file
) {
356 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
357 struct bt_plugin
*plugin
=
358 g_ptr_array_index(plugins_from_file
->plugins
, j
);
360 BT_LIB_LOGD("Adding plugin to plugin set: "
361 "plugin-path=\"%s\", %![plugin-]+l",
363 bt_plugin_set_add_plugin(
364 append_all_from_dir_info
.plugin_set
,
368 bt_object_put_ref(plugins_from_file
);
373 /* Continue to next file / directory. */
374 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
377 /* Continue to next file / directory. */
378 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
387 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
388 struct bt_plugin_set
*plugin_set
, const char *path
,
391 int nftw_flags
= FTW_PHYS
;
392 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
394 BT_ASSERT(plugin_set
);
396 BT_ASSERT(strlen(path
) < PATH_MAX
);
397 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
398 append_all_from_dir_info
.plugin_set
= plugin_set
;
399 append_all_from_dir_info
.recurse
= recurse
;
400 ret
= nftw(path
, nftw_append_all_from_dir
,
401 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
402 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
404 BT_LOGW_ERRNO("Cannot open directory", ": path=\"%s\"", path
);
405 ret
= BT_PLUGIN_STATUS_ERROR
;
411 const struct bt_plugin_set
*bt_plugin_find_all_from_dir(const char *path
,
414 struct bt_plugin_set
*plugin_set
;
415 enum bt_plugin_status status
;
417 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
419 plugin_set
= bt_plugin_set_create();
421 BT_LOGE_STR("Cannot create empty plugin set.");
425 /* Append found plugins to array */
426 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
429 BT_LOGW("Cannot append plugins found in directory: "
430 "path=\"%s\", status=%s",
431 path
, bt_plugin_status_string(status
));
435 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
436 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
440 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
446 const char *bt_plugin_get_name(const struct bt_plugin
*plugin
)
448 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
449 return plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
452 const char *bt_plugin_get_author(const struct bt_plugin
*plugin
)
454 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
455 return plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
458 const char *bt_plugin_get_license(const struct bt_plugin
*plugin
)
460 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
461 return plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
464 const char *bt_plugin_get_path(const struct bt_plugin
*plugin
)
466 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
467 return plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
470 const char *bt_plugin_get_description(const struct bt_plugin
*plugin
)
472 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
473 return plugin
->info
.description_set
?
474 plugin
->info
.description
->str
: NULL
;
477 enum bt_property_availability
bt_plugin_get_version(const struct bt_plugin
*plugin
,
478 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
481 enum bt_property_availability avail
=
482 BT_PROPERTY_AVAILABILITY_AVAILABLE
;
484 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
486 if (!plugin
->info
.version_set
) {
487 BT_LIB_LOGV("Plugin's version is not set: %!+l", plugin
);
488 avail
= BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
493 *major
= plugin
->info
.version
.major
;
497 *minor
= plugin
->info
.version
.minor
;
501 *patch
= plugin
->info
.version
.patch
;
505 *extra
= plugin
->info
.version
.extra
->str
;
512 uint64_t bt_plugin_get_source_component_class_count(const struct bt_plugin
*plugin
)
514 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
515 return (uint64_t) plugin
->src_comp_classes
->len
;
518 uint64_t bt_plugin_get_filter_component_class_count(const struct bt_plugin
*plugin
)
520 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
521 return (uint64_t) plugin
->flt_comp_classes
->len
;
524 uint64_t bt_plugin_get_sink_component_class_count(const struct bt_plugin
*plugin
)
526 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
527 return (uint64_t) plugin
->sink_comp_classes
->len
;
531 struct bt_component_class
*borrow_component_class_by_index(
532 const struct bt_plugin
*plugin
, GPtrArray
*comp_classes
,
535 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
536 BT_ASSERT_PRE_VALID_INDEX(index
, comp_classes
->len
);
537 return g_ptr_array_index(comp_classes
, index
);
540 const struct bt_component_class_source
*
541 bt_plugin_borrow_source_component_class_by_index_const(
542 const struct bt_plugin
*plugin
, uint64_t index
)
544 return (const void *) borrow_component_class_by_index(plugin
,
545 plugin
->src_comp_classes
, index
);
548 const struct bt_component_class_filter
*
549 bt_plugin_borrow_filter_component_class_by_index_const(
550 const struct bt_plugin
*plugin
, uint64_t index
)
552 return (const void *) borrow_component_class_by_index(plugin
,
553 plugin
->flt_comp_classes
, index
);
556 const struct bt_component_class_sink
*
557 bt_plugin_borrow_sink_component_class_by_index_const(
558 const struct bt_plugin
*plugin
, uint64_t index
)
560 return (const void *) borrow_component_class_by_index(plugin
,
561 plugin
->sink_comp_classes
, index
);
565 struct bt_component_class
*borrow_component_class_by_name(
566 const struct bt_plugin
*plugin
, GPtrArray
*comp_classes
,
569 struct bt_component_class
*comp_class
= NULL
;
572 BT_ASSERT_PRE_NON_NULL(plugin
, "Plugin");
573 BT_ASSERT_PRE_NON_NULL(name
, "Name");
575 for (i
= 0; i
< comp_classes
->len
; i
++) {
576 struct bt_component_class
*comp_class_candidate
=
577 g_ptr_array_index(comp_classes
, i
);
578 const char *comp_class_cand_name
=
579 bt_component_class_get_name(comp_class_candidate
);
581 BT_ASSERT(comp_class_cand_name
);
583 if (strcmp(name
, comp_class_cand_name
) == 0) {
584 comp_class
= comp_class_candidate
;
592 const struct bt_component_class_source
*
593 bt_plugin_borrow_source_component_class_by_name_const(
594 const struct bt_plugin
*plugin
, const char *name
)
596 return (const void *) borrow_component_class_by_name(plugin
,
597 plugin
->src_comp_classes
, name
);
600 const struct bt_component_class_filter
*
601 bt_plugin_borrow_filter_component_class_by_name_const(
602 const struct bt_plugin
*plugin
, const char *name
)
604 return (const void *) borrow_component_class_by_name(plugin
,
605 plugin
->flt_comp_classes
, name
);
608 const struct bt_component_class_sink
*
609 bt_plugin_borrow_sink_component_class_by_name_const(
610 const struct bt_plugin
*plugin
, const char *name
)
612 return (const void *) borrow_component_class_by_name(plugin
,
613 plugin
->sink_comp_classes
, name
);
616 void bt_plugin_get_ref(const struct bt_plugin
*plugin
)
618 bt_object_get_ref(plugin
);
621 void bt_plugin_put_ref(const struct bt_plugin
*plugin
)
623 bt_object_put_ref(plugin
);
626 void bt_plugin_set_get_ref(const struct bt_plugin_set
*plugin_set
)
628 bt_object_get_ref(plugin_set
);
631 void bt_plugin_set_put_ref(const struct bt_plugin_set
*plugin_set
)
633 bt_object_put_ref(plugin_set
);