4 * Babeltrace Plugin (generic)
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN"
31 #include <babeltrace/lib-logging-internal.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/common-internal.h>
37 #include <babeltrace/plugin/plugin-internal.h>
38 #include <babeltrace/plugin/plugin-so-internal.h>
39 #include <babeltrace/graph/component-class.h>
40 #include <babeltrace/graph/component-class-internal.h>
41 #include <babeltrace/types.h>
51 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
52 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
53 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
55 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
57 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
58 #include <babeltrace/plugin/python-plugin-provider-internal.h>
61 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
62 bt_plugin_python_create_all_from_file
;
63 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
64 static GModule
*python_plugin_provider_module
;
66 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
68 __attribute__((constructor
)) static
69 void init_python_plugin_provider(void) {
70 BT_LOGD_STR("Loading Python plugin provider module.");
71 python_plugin_provider_module
=
72 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
,
74 if (!python_plugin_provider_module
) {
75 BT_LOGI("Cannot find `%s`: continuing without Python plugin support.",
76 PYTHON_PLUGIN_PROVIDER_FILENAME
);
80 if (!g_module_symbol(python_plugin_provider_module
,
81 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
82 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
83 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
84 "file=\"%s\", symbol=\"%s\"",
85 PYTHON_PLUGIN_PROVIDER_FILENAME
,
86 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
90 BT_LOGI("Loaded Python plugin provider module: addr=%p",
91 python_plugin_provider_module
);
94 __attribute__((destructor
)) static
95 void fini_python_plugin_provider(void) {
96 if (python_plugin_provider_module
) {
97 BT_LOGD("Unloading Python plugin provider module.");
99 if (!g_module_close(python_plugin_provider_module
)) {
100 BT_LOGE("Failed to close the Python plugin provider module: %s.",
104 python_plugin_provider_module
= NULL
;
110 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
115 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
119 count
= (int64_t) plugin_set
->plugins
->len
;
126 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
129 struct bt_plugin
*plugin
= NULL
;
132 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
136 if (index
>= plugin_set
->plugins
->len
) {
137 BT_LOGW("Invalid parameter: index is out of bounds: "
138 "addr=%p, index=%" PRIu64
", count=%u",
139 plugin_set
, index
, plugin_set
->plugins
->len
);
143 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
149 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
151 /* bt_plugin_so_create_all_from_static() logs errors */
152 return bt_plugin_so_create_all_from_static();
155 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
157 struct bt_plugin_set
*plugin_set
= NULL
;
160 BT_LOGW_STR("Invalid parameter: path is NULL.");
164 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
166 /* Try shared object plugins */
167 plugin_set
= bt_plugin_so_create_all_from_file(path
);
172 /* Try Python plugins if support is available */
173 if (bt_plugin_python_create_all_from_file_sym
) {
174 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
182 BT_LOGD("Created %u plugins from file: "
183 "path=\"%s\", count=%u, plugin-set-addr=%p",
184 plugin_set
->plugins
->len
, path
,
185 plugin_set
->plugins
->len
, plugin_set
);
187 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
193 static void destroy_gstring(void *data
)
195 g_string_free(data
, TRUE
);
198 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
200 const char *system_plugin_dir
;
201 char *home_plugin_dir
= NULL
;
203 struct bt_plugin
*plugin
= NULL
;
204 struct bt_plugin_set
*plugin_set
= NULL
;
205 GPtrArray
*dirs
= NULL
;
210 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
214 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
215 "name=\"%s\"", plugin_name
);
216 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
218 BT_LOGE_STR("Failed to allocate a GPtrArray.");
225 * 1. BABELTRACE_PLUGIN_PATH environment variable
226 * (colon-separated list of directories)
227 * 2. ~/.local/lib/babeltrace/plugins
228 * 3. Default system directory for Babeltrace plugins, usually
229 * /usr/lib/babeltrace/plugins or
230 * /usr/local/lib/babeltrace/plugins if installed
232 * 4. Built-in plugins (static)
234 * Directories are searched non-recursively.
236 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
238 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
240 BT_LOGE_STR("Failed to append plugin path to array of directories.");
245 home_plugin_dir
= bt_common_get_home_plugin_path();
246 if (home_plugin_dir
) {
247 GString
*home_plugin_dir_str
=
248 g_string_new(home_plugin_dir
);
250 if (!home_plugin_dir_str
) {
251 BT_LOGE_STR("Failed to allocate a GString.");
255 g_ptr_array_add(dirs
, home_plugin_dir_str
);
258 system_plugin_dir
= bt_common_get_system_plugin_path();
259 if (system_plugin_dir
) {
260 GString
*system_plugin_dir_str
=
261 g_string_new(system_plugin_dir
);
263 if (!system_plugin_dir_str
) {
264 BT_LOGE_STR("Failed to allocate a GString.");
268 g_ptr_array_add(dirs
, system_plugin_dir_str
);
271 for (i
= 0; i
< dirs
->len
; i
++) {
272 GString
*dir
= g_ptr_array_index(dirs
, i
);
277 * Skip this if the directory does not exist because
278 * bt_plugin_create_all_from_dir() would log a warning.
280 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
281 BT_LOGV("Skipping nonexistent directory path: "
282 "path=\"%s\"", dir
->str
);
286 /* bt_plugin_create_all_from_dir() logs details/errors */
287 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
289 BT_LOGD("No plugins found in directory: path=\"%s\"",
294 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
295 struct bt_plugin
*candidate_plugin
=
296 g_ptr_array_index(plugin_set
->plugins
, j
);
298 if (strcmp(bt_plugin_get_name(candidate_plugin
),
300 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
301 plugin_name
, dir
->str
);
302 plugin
= bt_get(candidate_plugin
);
307 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
308 plugin_name
, dir
->str
);
312 plugin_set
= bt_plugin_create_all_from_static();
314 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
315 struct bt_plugin
*candidate_plugin
=
316 g_ptr_array_index(plugin_set
->plugins
, j
);
318 if (strcmp(bt_plugin_get_name(candidate_plugin
),
320 BT_LOGD("Plugin found in built-in plugins: "
321 "name=\"%s\"", plugin_name
);
322 plugin
= bt_get(candidate_plugin
);
329 free(home_plugin_dir
);
333 g_ptr_array_free(dirs
, TRUE
);
337 BT_LOGD("Found plugin in standard directories and built-in plugins: "
338 "addr=%p, name=\"%s\", path=\"%s\"",
339 plugin
, plugin_name
, bt_plugin_get_path(plugin
));
341 BT_LOGD("No plugin found in standard directories and built-in plugins: "
342 "name=\"%s\"", plugin_name
);
348 struct bt_component_class
*bt_plugin_find_component_class(
349 const char *plugin_name
, const char *comp_cls_name
,
350 enum bt_component_class_type comp_cls_type
)
352 struct bt_plugin
*plugin
= NULL
;
353 struct bt_component_class
*comp_cls
= NULL
;
356 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
360 if (!comp_cls_name
) {
361 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
365 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
366 "plugin-name=\"%s\", comp-class-name=\"%s\"",
367 plugin_name
, comp_cls_name
);
368 plugin
= bt_plugin_find(plugin_name
);
370 BT_LOGD_STR("Plugin not found.");
374 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
375 plugin
, comp_cls_name
, comp_cls_type
);
377 BT_LOGD("Component class not found in plugin: "
378 "plugin-addr=%p, plugin-path=\"%s\"",
379 plugin
, bt_plugin_get_path(plugin
));
388 pthread_mutex_t lock
;
389 struct bt_plugin_set
*plugin_set
;
391 } append_all_from_dir_info
= {
392 .lock
= PTHREAD_MUTEX_INITIALIZER
396 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
400 const char *name
= file
+ s
->base
;
402 /* Check for recursion */
403 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
409 if (name
[0] == '.') {
410 /* Skip hidden files */
411 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
414 struct bt_plugin_set
*plugins_from_file
=
415 bt_plugin_create_all_from_file(file
);
417 if (plugins_from_file
) {
420 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
421 struct bt_plugin
*plugin
=
422 g_ptr_array_index(plugins_from_file
->plugins
, j
);
424 BT_LOGD("Adding plugin to plugin set: "
425 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
426 file
, plugin
, bt_plugin_get_name(plugin
));
427 bt_plugin_set_add_plugin(append_all_from_dir_info
.plugin_set
, plugin
);
430 bt_put(plugins_from_file
);
434 /* Continue to next file / directory. */
435 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
438 /* Continue to next file / directory. */
439 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
448 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
449 struct bt_plugin_set
*plugin_set
, const char *path
,
452 int nftw_flags
= FTW_PHYS
;
454 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
457 BT_LOGW_STR("Invalid parameter: path is NULL.");
458 ret
= BT_PLUGIN_STATUS_ERROR
;
462 path_len
= strlen(path
);
463 if (path_len
>= PATH_MAX
) {
464 BT_LOGW("Invalid parameter: path length is too large: "
465 "path-length=%zu", path_len
);
466 ret
= BT_PLUGIN_STATUS_ERROR
;
470 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
472 append_all_from_dir_info
.plugin_set
= plugin_set
;
473 append_all_from_dir_info
.recurse
= recurse
;
474 ret
= nftw(path
, nftw_append_all_from_dir
,
475 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
477 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
480 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
481 strerror(errno
), path
, errno
);
482 ret
= BT_PLUGIN_STATUS_ERROR
;
489 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
492 struct bt_plugin_set
*plugin_set
;
493 enum bt_plugin_status status
;
495 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
497 plugin_set
= bt_plugin_set_create();
499 BT_LOGE_STR("Cannot create empty plugin set.");
503 /* Append found plugins to array */
504 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
507 BT_LOGW("Cannot append plugins found in directory: "
508 "path=\"%s\", status=%s",
509 path
, bt_plugin_status_string(status
));
513 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
514 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
524 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
526 const char *val
= NULL
;
529 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
533 if (plugin
->info
.name_set
) {
534 val
= plugin
->info
.name
->str
;
541 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
543 const char *val
= NULL
;
546 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
550 if (plugin
->info
.author_set
) {
551 val
= plugin
->info
.author
->str
;
558 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
560 const char *val
= NULL
;
563 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
567 if (plugin
->info
.license_set
) {
568 val
= plugin
->info
.license
->str
;
575 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
577 const char *val
= NULL
;
580 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
584 if (plugin
->info
.path_set
) {
585 val
= plugin
->info
.path
->str
;
592 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
594 const char *val
= NULL
;
597 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
601 if (plugin
->info
.description_set
) {
602 val
= plugin
->info
.description
->str
;
609 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
610 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
613 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
616 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
617 status
= BT_PLUGIN_STATUS_ERROR
;
621 if (!plugin
->info
.version_set
) {
622 BT_LOGV("Plugin's version is not set: addr=%p", plugin
);
623 status
= BT_PLUGIN_STATUS_ERROR
;
628 *major
= plugin
->info
.version
.major
;
632 *minor
= plugin
->info
.version
.minor
;
636 *patch
= plugin
->info
.version
.patch
;
640 *extra
= plugin
->info
.version
.extra
->str
;
647 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
649 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
652 struct bt_component_class
*bt_plugin_get_component_class_by_index(
653 struct bt_plugin
*plugin
, uint64_t index
)
655 struct bt_component_class
*comp_class
= NULL
;
658 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
662 if (index
>= plugin
->comp_classes
->len
) {
663 BT_LOGW("Invalid parameter: index is out of bounds: "
664 "addr=%p, index=%" PRIu64
", count=%u",
665 plugin
, index
, plugin
->comp_classes
->len
);
669 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
680 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
681 struct bt_plugin
*plugin
, const char *name
,
682 enum bt_component_class_type type
)
684 struct bt_component_class
*comp_class
= NULL
;
688 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
693 BT_LOGW_STR("Invalid parameter: name is NULL.");
697 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
698 struct bt_component_class
*comp_class_candidate
=
699 g_ptr_array_index(plugin
->comp_classes
, i
);
700 const char *comp_class_cand_name
=
701 bt_component_class_get_name(comp_class_candidate
);
702 enum bt_component_class_type comp_class_cand_type
=
703 bt_component_class_get_type(comp_class_candidate
);
705 assert(comp_class_cand_name
);
706 assert(comp_class_cand_type
>= 0);
708 if (strcmp(name
, comp_class_cand_name
) == 0 &&
709 comp_class_cand_type
== type
) {
710 comp_class
= bt_get(comp_class_candidate
);
724 enum bt_plugin_status
bt_plugin_add_component_class(
725 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
727 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
728 struct bt_component_class
*comp_class_dup
= NULL
;
729 int comp_class_index
= -1;
732 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
737 BT_LOGW_STR("Invalid parameter: component class is NULL.");
741 if (plugin
->frozen
) {
742 BT_LOGW("Invalid parameter: plugin is frozen: "
743 "addr=%p, name=\"%s\"", plugin
,
744 bt_plugin_get_name(plugin
));
748 /* Check for duplicate */
749 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
750 bt_component_class_get_name(comp_class
),
751 bt_component_class_get_type(comp_class
));
752 if (comp_class_dup
) {
753 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
754 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
755 "comp-class-name=\"%s\", comp-class-type=%s",
756 plugin
, bt_plugin_get_name(plugin
),
757 bt_plugin_get_path(plugin
),
758 bt_component_class_get_name(comp_class
),
759 bt_component_class_type_string(
760 bt_component_class_get_type(comp_class
)));
764 /* Add new component class */
765 comp_class_index
= plugin
->comp_classes
->len
;
766 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
768 /* Special case for a shared object plugin */
769 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
770 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
773 BT_LOGD("Added component class to plugin: "
774 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
775 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
776 plugin
, bt_plugin_get_name(plugin
),
777 bt_plugin_get_path(plugin
),
779 bt_component_class_get_name(comp_class
),
780 bt_component_class_type_string(
781 bt_component_class_get_type(comp_class
)));
785 /* Remove entry from plugin's component classes (if added) */
786 if (comp_class_index
>= 0) {
787 g_ptr_array_remove_index(plugin
->comp_classes
,
791 status
= BT_PLUGIN_STATUS_ERROR
;
794 bt_put(comp_class_dup
);