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>
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 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
55 #include <babeltrace/plugin/python-plugin-provider-internal.h>
57 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
58 bt_plugin_python_create_all_from_file
;
59 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
60 static GModule
*python_plugin_provider_module
;
62 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
64 __attribute__((constructor
)) static
65 void init_python_plugin_provider(void) {
66 BT_LOGD_STR("Loading Python plugin provider module.");
67 python_plugin_provider_module
=
68 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
,
70 if (!python_plugin_provider_module
) {
71 BT_LOGI("Cannot find `%s`: continuing without Python plugin support.",
72 PYTHON_PLUGIN_PROVIDER_FILENAME
);
76 if (!g_module_symbol(python_plugin_provider_module
,
77 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
78 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
79 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
80 "file=\"%s\", symbol=\"%s\"",
81 PYTHON_PLUGIN_PROVIDER_FILENAME
,
82 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
86 BT_LOGD("Loaded Python plugin provider module: addr=%p",
87 python_plugin_provider_module
);
90 __attribute__((destructor
)) static
91 void fini_python_plugin_provider(void) {
92 if (python_plugin_provider_module
) {
93 BT_LOGD("Unloading Python plugin provider module.");
95 if (!g_module_close(python_plugin_provider_module
)) {
96 BT_LOGE("Failed to close the Python plugin provider module: %s.",
100 python_plugin_provider_module
= NULL
;
106 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
111 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
115 count
= (int64_t) plugin_set
->plugins
->len
;
122 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
125 struct bt_plugin
*plugin
= NULL
;
128 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
132 if (index
>= plugin_set
->plugins
->len
) {
133 BT_LOGW("Invalid parameter: index is out of bounds: "
134 "addr=%p, index=%" PRIu64
", count=%u",
135 plugin_set
, index
, plugin_set
->plugins
->len
);
139 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
145 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
147 /* bt_plugin_so_create_all_from_static() logs errors */
148 return bt_plugin_so_create_all_from_static();
151 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
153 struct bt_plugin_set
*plugin_set
= NULL
;
156 BT_LOGW_STR("Invalid parameter: path is NULL.");
160 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
162 /* Try shared object plugins */
163 plugin_set
= bt_plugin_so_create_all_from_file(path
);
168 /* Try Python plugins if support is available */
169 if (bt_plugin_python_create_all_from_file_sym
) {
170 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
178 BT_LOGD("Created %u plugins from file: "
179 "path=\"%s\", count=%u, plugin-set-addr=%p",
180 plugin_set
->plugins
->len
, path
,
181 plugin_set
->plugins
->len
, plugin_set
);
183 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
189 static void destroy_gstring(void *data
)
191 g_string_free(data
, TRUE
);
194 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
196 const char *system_plugin_dir
;
197 char *home_plugin_dir
= NULL
;
199 struct bt_plugin
*plugin
= NULL
;
200 struct bt_plugin_set
*plugin_set
= NULL
;
201 GPtrArray
*dirs
= NULL
;
206 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
210 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
211 "name=\"%s\"", plugin_name
);
212 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
214 BT_LOGE_STR("Failed to allocate a GPtrArray.");
221 * 1. BABELTRACE_PLUGIN_PATH environment variable
222 * (colon-separated list of directories)
223 * 2. ~/.local/lib/babeltrace/plugins
224 * 3. Default system directory for Babeltrace plugins, usually
225 * /usr/lib/babeltrace/plugins or
226 * /usr/local/lib/babeltrace/plugins if installed
228 * 4. Built-in plugins (static)
230 * Directories are searched non-recursively.
232 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
234 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
236 BT_LOGE_STR("Failed to append plugin path to array of directories.");
241 home_plugin_dir
= bt_common_get_home_plugin_path();
242 if (home_plugin_dir
) {
243 GString
*home_plugin_dir_str
=
244 g_string_new(home_plugin_dir
);
246 if (!home_plugin_dir_str
) {
247 BT_LOGE_STR("Failed to allocate a GString.");
251 g_ptr_array_add(dirs
, home_plugin_dir_str
);
254 system_plugin_dir
= bt_common_get_system_plugin_path();
255 if (system_plugin_dir
) {
256 GString
*system_plugin_dir_str
=
257 g_string_new(system_plugin_dir
);
259 if (!system_plugin_dir_str
) {
260 BT_LOGE_STR("Failed to allocate a GString.");
264 g_ptr_array_add(dirs
, system_plugin_dir_str
);
267 for (i
= 0; i
< dirs
->len
; i
++) {
268 GString
*dir
= g_ptr_array_index(dirs
, i
);
272 /* bt_plugin_create_all_from_dir() logs details/errors */
273 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
275 BT_LOGD("No plugins found in directory: path=\"%s\"",
280 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
281 struct bt_plugin
*candidate_plugin
=
282 g_ptr_array_index(plugin_set
->plugins
, j
);
284 if (strcmp(bt_plugin_get_name(candidate_plugin
),
286 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
287 plugin_name
, dir
->str
);
288 plugin
= bt_get(candidate_plugin
);
293 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
294 plugin_name
, dir
->str
);
298 plugin_set
= bt_plugin_create_all_from_static();
300 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
301 struct bt_plugin
*candidate_plugin
=
302 g_ptr_array_index(plugin_set
->plugins
, j
);
304 if (strcmp(bt_plugin_get_name(candidate_plugin
),
306 BT_LOGD("Plugin found in built-in plugins: "
307 "name=\"%s\"", plugin_name
);
308 plugin
= bt_get(candidate_plugin
);
315 free(home_plugin_dir
);
319 g_ptr_array_free(dirs
, TRUE
);
323 BT_LOGD("Found plugin in standard directories and built-in plugins: "
324 "addr=%p, name=\"%s\", path=\"%s\"",
325 plugin
, plugin_name
, bt_plugin_get_path(plugin
));
327 BT_LOGD("No plugin found in standard directories and built-in plugins: "
328 "name=\"%s\"", plugin_name
);
334 struct bt_component_class
*bt_plugin_find_component_class(
335 const char *plugin_name
, const char *comp_cls_name
,
336 enum bt_component_class_type comp_cls_type
)
338 struct bt_plugin
*plugin
= NULL
;
339 struct bt_component_class
*comp_cls
= NULL
;
342 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
346 if (!comp_cls_name
) {
347 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
351 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
352 "plugin-name=\"%s\", comp-class-name=\"%s\"",
353 plugin_name
, comp_cls_name
);
354 plugin
= bt_plugin_find(plugin_name
);
356 BT_LOGD_STR("Plugin not found.");
360 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
361 plugin
, comp_cls_name
, comp_cls_type
);
363 BT_LOGD("Component class not found in plugin: "
364 "plugin-addr=%p, plugin-path=\"%s\"",
365 plugin
, bt_plugin_get_path(plugin
));
373 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
375 struct dirent
*alloc_dirent(const char *path
)
379 struct dirent
*entry
;
381 name_max
= pathconf(path
, _PC_NAME_MAX
);
382 if (name_max
== -1) {
385 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
386 entry
= zmalloc(len
);
391 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
392 struct bt_plugin_set
*plugin_set
, const char *path
,
395 DIR *directory
= NULL
;
396 struct dirent
*entry
= NULL
, *result
= NULL
;
397 char *file_path
= NULL
;
399 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
402 BT_LOGW_STR("Invalid parameter: path is NULL.");
403 ret
= BT_PLUGIN_STATUS_ERROR
;
407 path_len
= strlen(path
);
408 if (path_len
>= PATH_MAX
) {
409 BT_LOGW("Invalid parameter: path length is too large: "
410 "path-length=%zu", path_len
);
411 ret
= BT_PLUGIN_STATUS_ERROR
;
415 entry
= alloc_dirent(path
);
417 BT_LOGE_STR("Failed to allocate a directory entry.");
418 ret
= BT_PLUGIN_STATUS_ERROR
;
422 file_path
= zmalloc(PATH_MAX
);
424 BT_LOGE("Failed to allocate %zu bytes.", (size_t) PATH_MAX
);
425 ret
= BT_PLUGIN_STATUS_NOMEM
;
429 strncpy(file_path
, path
, path_len
);
430 /* Append a trailing '/' to the path */
431 if (file_path
[path_len
- 1] != '/') {
432 file_path
[path_len
++] = '/';
435 directory
= opendir(file_path
);
437 if (errno
== EACCES
) {
438 BT_LOGD("Cannot open directory: %s: continuing: "
439 "path=\"%s\", errno=%d",
440 strerror(errno
), file_path
, errno
);
444 BT_LOGE("Cannot open directory: %s: "
445 "path=\"%s\", errno=%d",
446 strerror(errno
), file_path
, errno
);
447 ret
= BT_PLUGIN_STATUS_ERROR
;
451 /* Recursively walk directory */
452 while (!readdir_r(directory
, entry
, &result
) && result
) {
455 size_t file_name_len
;
457 if (strcmp(result
->d_name
, ".") == 0 ||
458 strcmp(result
->d_name
, "..") == 0) {
462 if (result
->d_name
[0] == '.') {
463 /* Skip hidden files, . and .. */
464 BT_LOGV("Skipping hidden file: path=\"%s/%s\"",
465 path
, result
->d_name
);
469 file_name_len
= strlen(result
->d_name
);
471 if (path_len
+ file_name_len
>= PATH_MAX
) {
472 BT_LOGD("Skipping file because its path length is too large: continuing: "
473 "path=\"%s/%s\", length=%zu",
474 path
, result
->d_name
,
475 (size_t) (path_len
+ file_name_len
));
479 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
480 file_path
[path_len
+ file_name_len
] = '\0';
481 stat_ret
= stat(file_path
, &st
);
483 /* Continue to next file / directory. */
484 BT_LOGD("Cannot get file information: %s: continuing: "
485 "path=\"%s\", errno=%d",
486 strerror(errno
), file_path
, errno
);
490 if (S_ISDIR(st
.st_mode
) && recurse
) {
491 ret
= bt_plugin_create_append_all_from_dir(plugin_set
,
494 BT_LOGW("Cannot recurse into directory to find plugins: "
495 "path=\"%s\", ret=%d", file_path
, ret
);
498 } else if (S_ISREG(st
.st_mode
)) {
499 struct bt_plugin_set
*plugins_from_file
=
500 bt_plugin_create_all_from_file(file_path
);
502 if (plugins_from_file
) {
505 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
506 struct bt_plugin
*plugin
=
507 g_ptr_array_index(plugins_from_file
->plugins
, j
);
509 BT_LOGD("Adding plugin to plugin set: "
510 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
512 bt_plugin_get_name(plugin
));
513 bt_plugin_set_add_plugin(plugin_set
,
517 bt_put(plugins_from_file
);
524 if (closedir(directory
)) {
526 * We don't want to override the error since there is
529 BT_LOGE("Cannot close directory entry: %s: "
530 "path=\"%s\", errno=%d",
531 strerror(errno
), path
, errno
);
540 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
543 struct bt_plugin_set
*plugin_set
;
544 enum bt_plugin_status status
;
546 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
548 plugin_set
= bt_plugin_set_create();
550 BT_LOGE_STR("Cannot create empty plugin set.");
554 /* Append found plugins to array */
555 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
558 BT_LOGW("Cannot append plugins found in directory: "
559 "status=%s", bt_plugin_status_string(status
));
563 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
564 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
574 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
576 const char *val
= NULL
;
579 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
583 if (plugin
->info
.name_set
) {
584 val
= plugin
->info
.name
->str
;
591 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
593 const char *val
= NULL
;
596 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
600 if (plugin
->info
.author_set
) {
601 val
= plugin
->info
.author
->str
;
608 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
610 const char *val
= NULL
;
613 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
617 if (plugin
->info
.license_set
) {
618 val
= plugin
->info
.license
->str
;
625 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
627 const char *val
= NULL
;
630 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
634 if (plugin
->info
.path_set
) {
635 val
= plugin
->info
.path
->str
;
642 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
644 const char *val
= NULL
;
647 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
651 if (plugin
->info
.description_set
) {
652 val
= plugin
->info
.description
->str
;
659 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
660 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
663 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
666 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
667 status
= BT_PLUGIN_STATUS_ERROR
;
671 if (!plugin
->info
.version_set
) {
672 BT_LOGV("Plugin's version is not set: addr=%p", plugin
);
673 status
= BT_PLUGIN_STATUS_ERROR
;
678 *major
= plugin
->info
.version
.major
;
682 *minor
= plugin
->info
.version
.minor
;
686 *patch
= plugin
->info
.version
.patch
;
690 *extra
= plugin
->info
.version
.extra
->str
;
697 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
699 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
702 struct bt_component_class
*bt_plugin_get_component_class_by_index(
703 struct bt_plugin
*plugin
, uint64_t index
)
705 struct bt_component_class
*comp_class
= NULL
;
708 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
712 if (index
>= plugin
->comp_classes
->len
) {
713 BT_LOGW("Invalid parameter: index is out of bounds: "
714 "addr=%p, index=%" PRIu64
", count=%u",
715 plugin
, index
, plugin
->comp_classes
->len
);
719 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
730 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
731 struct bt_plugin
*plugin
, const char *name
,
732 enum bt_component_class_type type
)
734 struct bt_component_class
*comp_class
= NULL
;
738 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
743 BT_LOGW_STR("Invalid parameter: name is NULL.");
747 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
748 struct bt_component_class
*comp_class_candidate
=
749 g_ptr_array_index(plugin
->comp_classes
, i
);
750 const char *comp_class_cand_name
=
751 bt_component_class_get_name(comp_class_candidate
);
752 enum bt_component_class_type comp_class_cand_type
=
753 bt_component_class_get_type(comp_class_candidate
);
755 assert(comp_class_cand_name
);
756 assert(comp_class_cand_type
>= 0);
758 if (strcmp(name
, comp_class_cand_name
) == 0 &&
759 comp_class_cand_type
== type
) {
760 comp_class
= bt_get(comp_class_candidate
);
774 enum bt_plugin_status
bt_plugin_add_component_class(
775 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
777 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
778 struct bt_component_class
*comp_class_dup
= NULL
;
779 int comp_class_index
= -1;
782 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
787 BT_LOGW_STR("Invalid parameter: component class is NULL.");
791 if (plugin
->frozen
) {
792 BT_LOGW("Invalid parameter: plugin is frozen: "
793 "addr=%p, name=\"%s\"", plugin
,
794 bt_plugin_get_name(plugin
));
798 /* Check for duplicate */
799 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
800 bt_component_class_get_name(comp_class
),
801 bt_component_class_get_type(comp_class
));
802 if (comp_class_dup
) {
803 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
804 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
805 "comp-class-name=\"%s\", comp-class-type=%s",
806 plugin
, bt_plugin_get_name(plugin
),
807 bt_plugin_get_path(plugin
),
808 bt_component_class_get_name(comp_class
),
809 bt_component_class_type_string(
810 bt_component_class_get_type(comp_class
)));
814 /* Add new component class */
815 comp_class_index
= plugin
->comp_classes
->len
;
816 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
818 /* Special case for a shared object plugin */
819 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
820 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
823 BT_LOGD("Added component class to plugin: "
824 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
825 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
826 plugin
, bt_plugin_get_name(plugin
),
827 bt_plugin_get_path(plugin
),
829 bt_component_class_get_name(comp_class
),
830 bt_component_class_type_string(
831 bt_component_class_get_type(comp_class
)));
835 /* Remove entry from plugin's component classes (if added) */
836 if (comp_class_index
>= 0) {
837 g_ptr_array_remove_index(plugin
->comp_classes
,
841 status
= BT_PLUGIN_STATUS_ERROR
;
844 bt_put(comp_class_dup
);