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
;
65 void init_python_plugin_provider(void) {}
66 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
67 static GModule
*python_plugin_provider_module
;
69 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
72 void init_python_plugin_provider(void) {
73 if (bt_plugin_python_create_all_from_file_sym
!= NULL
) {
77 BT_LOGD_STR("Loading Python plugin provider module.");
78 python_plugin_provider_module
=
79 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
, 0);
80 if (!python_plugin_provider_module
) {
81 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
82 PYTHON_PLUGIN_PROVIDER_FILENAME
, g_module_error());
86 if (!g_module_symbol(python_plugin_provider_module
,
87 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
88 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
89 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
90 "file=\"%s\", symbol=\"%s\"",
91 PYTHON_PLUGIN_PROVIDER_FILENAME
,
92 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
96 BT_LOGI("Loaded Python plugin provider module: addr=%p",
97 python_plugin_provider_module
);
100 __attribute__((destructor
)) static
101 void fini_python_plugin_provider(void) {
102 if (python_plugin_provider_module
) {
103 BT_LOGD("Unloading Python plugin provider module.");
105 if (!g_module_close(python_plugin_provider_module
)) {
106 BT_LOGE("Failed to close the Python plugin provider module: %s.",
110 python_plugin_provider_module
= NULL
;
116 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
121 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
125 count
= (int64_t) plugin_set
->plugins
->len
;
132 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
135 struct bt_plugin
*plugin
= NULL
;
138 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
142 if (index
>= plugin_set
->plugins
->len
) {
143 BT_LOGW("Invalid parameter: index is out of bounds: "
144 "addr=%p, index=%" PRIu64
", count=%u",
145 plugin_set
, index
, plugin_set
->plugins
->len
);
149 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
155 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
157 /* bt_plugin_so_create_all_from_static() logs errors */
158 return bt_plugin_so_create_all_from_static();
161 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
163 struct bt_plugin_set
*plugin_set
= NULL
;
166 BT_LOGW_STR("Invalid parameter: path is NULL.");
170 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
172 /* Try shared object plugins */
173 plugin_set
= bt_plugin_so_create_all_from_file(path
);
178 /* Try Python plugins if support is available */
179 init_python_plugin_provider();
180 if (bt_plugin_python_create_all_from_file_sym
) {
181 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
189 BT_LOGD("Created %u plugins from file: "
190 "path=\"%s\", count=%u, plugin-set-addr=%p",
191 plugin_set
->plugins
->len
, path
,
192 plugin_set
->plugins
->len
, plugin_set
);
194 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
200 static void destroy_gstring(void *data
)
202 g_string_free(data
, TRUE
);
205 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
207 const char *system_plugin_dir
;
208 char *home_plugin_dir
= NULL
;
210 struct bt_plugin
*plugin
= NULL
;
211 struct bt_plugin_set
*plugin_set
= NULL
;
212 GPtrArray
*dirs
= NULL
;
217 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
221 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
222 "name=\"%s\"", plugin_name
);
223 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
225 BT_LOGE_STR("Failed to allocate a GPtrArray.");
232 * 1. BABELTRACE_PLUGIN_PATH environment variable
233 * (colon-separated list of directories)
234 * 2. ~/.local/lib/babeltrace/plugins
235 * 3. Default system directory for Babeltrace plugins, usually
236 * /usr/lib/babeltrace/plugins or
237 * /usr/local/lib/babeltrace/plugins if installed
239 * 4. Built-in plugins (static)
241 * Directories are searched non-recursively.
243 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
245 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
247 BT_LOGE_STR("Failed to append plugin path to array of directories.");
252 home_plugin_dir
= bt_common_get_home_plugin_path();
253 if (home_plugin_dir
) {
254 GString
*home_plugin_dir_str
=
255 g_string_new(home_plugin_dir
);
257 if (!home_plugin_dir_str
) {
258 BT_LOGE_STR("Failed to allocate a GString.");
262 g_ptr_array_add(dirs
, home_plugin_dir_str
);
265 system_plugin_dir
= bt_common_get_system_plugin_path();
266 if (system_plugin_dir
) {
267 GString
*system_plugin_dir_str
=
268 g_string_new(system_plugin_dir
);
270 if (!system_plugin_dir_str
) {
271 BT_LOGE_STR("Failed to allocate a GString.");
275 g_ptr_array_add(dirs
, system_plugin_dir_str
);
278 for (i
= 0; i
< dirs
->len
; i
++) {
279 GString
*dir
= g_ptr_array_index(dirs
, i
);
284 * Skip this if the directory does not exist because
285 * bt_plugin_create_all_from_dir() would log a warning.
287 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
288 BT_LOGV("Skipping nonexistent directory path: "
289 "path=\"%s\"", dir
->str
);
293 /* bt_plugin_create_all_from_dir() logs details/errors */
294 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
296 BT_LOGD("No plugins found in directory: path=\"%s\"",
301 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
302 struct bt_plugin
*candidate_plugin
=
303 g_ptr_array_index(plugin_set
->plugins
, j
);
305 if (strcmp(bt_plugin_get_name(candidate_plugin
),
307 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
308 plugin_name
, dir
->str
);
309 plugin
= bt_get(candidate_plugin
);
314 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
315 plugin_name
, dir
->str
);
319 plugin_set
= bt_plugin_create_all_from_static();
321 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
322 struct bt_plugin
*candidate_plugin
=
323 g_ptr_array_index(plugin_set
->plugins
, j
);
325 if (strcmp(bt_plugin_get_name(candidate_plugin
),
327 BT_LOGD("Plugin found in built-in plugins: "
328 "name=\"%s\"", plugin_name
);
329 plugin
= bt_get(candidate_plugin
);
336 free(home_plugin_dir
);
340 g_ptr_array_free(dirs
, TRUE
);
344 BT_LOGD("Found plugin in standard directories and built-in plugins: "
345 "addr=%p, name=\"%s\", path=\"%s\"",
346 plugin
, plugin_name
, bt_plugin_get_path(plugin
));
348 BT_LOGD("No plugin found in standard directories and built-in plugins: "
349 "name=\"%s\"", plugin_name
);
355 struct bt_component_class
*bt_plugin_find_component_class(
356 const char *plugin_name
, const char *comp_cls_name
,
357 enum bt_component_class_type comp_cls_type
)
359 struct bt_plugin
*plugin
= NULL
;
360 struct bt_component_class
*comp_cls
= NULL
;
363 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
367 if (!comp_cls_name
) {
368 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
372 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
373 "plugin-name=\"%s\", comp-class-name=\"%s\"",
374 plugin_name
, comp_cls_name
);
375 plugin
= bt_plugin_find(plugin_name
);
377 BT_LOGD_STR("Plugin not found.");
381 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
382 plugin
, comp_cls_name
, comp_cls_type
);
384 BT_LOGD("Component class not found in plugin: "
385 "plugin-addr=%p, plugin-path=\"%s\"",
386 plugin
, bt_plugin_get_path(plugin
));
395 pthread_mutex_t lock
;
396 struct bt_plugin_set
*plugin_set
;
398 } append_all_from_dir_info
= {
399 .lock
= PTHREAD_MUTEX_INITIALIZER
403 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
407 const char *name
= file
+ s
->base
;
409 /* Check for recursion */
410 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
417 struct bt_plugin_set
*plugins_from_file
;
419 if (name
[0] == '.') {
420 /* Skip hidden files */
421 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
425 plugins_from_file
= bt_plugin_create_all_from_file(file
);
427 if (plugins_from_file
) {
430 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
431 struct bt_plugin
*plugin
=
432 g_ptr_array_index(plugins_from_file
->plugins
, j
);
434 BT_LOGD("Adding plugin to plugin set: "
435 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
436 file
, plugin
, bt_plugin_get_name(plugin
));
437 bt_plugin_set_add_plugin(append_all_from_dir_info
.plugin_set
, plugin
);
440 bt_put(plugins_from_file
);
445 /* Continue to next file / directory. */
446 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
449 /* Continue to next file / directory. */
450 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
459 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
460 struct bt_plugin_set
*plugin_set
, const char *path
,
463 int nftw_flags
= FTW_PHYS
;
465 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
468 BT_LOGW_STR("Invalid parameter: path is NULL.");
469 ret
= BT_PLUGIN_STATUS_ERROR
;
473 path_len
= strlen(path
);
474 if (path_len
>= PATH_MAX
) {
475 BT_LOGW("Invalid parameter: path length is too large: "
476 "path-length=%zu", path_len
);
477 ret
= BT_PLUGIN_STATUS_ERROR
;
481 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
483 append_all_from_dir_info
.plugin_set
= plugin_set
;
484 append_all_from_dir_info
.recurse
= recurse
;
485 ret
= nftw(path
, nftw_append_all_from_dir
,
486 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
488 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
491 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
492 strerror(errno
), path
, errno
);
493 ret
= BT_PLUGIN_STATUS_ERROR
;
500 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
503 struct bt_plugin_set
*plugin_set
;
504 enum bt_plugin_status status
;
506 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
508 plugin_set
= bt_plugin_set_create();
510 BT_LOGE_STR("Cannot create empty plugin set.");
514 /* Append found plugins to array */
515 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
518 BT_LOGW("Cannot append plugins found in directory: "
519 "path=\"%s\", status=%s",
520 path
, bt_plugin_status_string(status
));
524 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
525 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
535 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
537 const char *val
= NULL
;
540 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
544 if (plugin
->info
.name_set
) {
545 val
= plugin
->info
.name
->str
;
552 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
554 const char *val
= NULL
;
557 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
561 if (plugin
->info
.author_set
) {
562 val
= plugin
->info
.author
->str
;
569 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
571 const char *val
= NULL
;
574 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
578 if (plugin
->info
.license_set
) {
579 val
= plugin
->info
.license
->str
;
586 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
588 const char *val
= NULL
;
591 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
595 if (plugin
->info
.path_set
) {
596 val
= plugin
->info
.path
->str
;
603 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
605 const char *val
= NULL
;
608 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
612 if (plugin
->info
.description_set
) {
613 val
= plugin
->info
.description
->str
;
620 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
621 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
624 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
627 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
628 status
= BT_PLUGIN_STATUS_ERROR
;
632 if (!plugin
->info
.version_set
) {
633 BT_LOGV("Plugin's version is not set: addr=%p", plugin
);
634 status
= BT_PLUGIN_STATUS_ERROR
;
639 *major
= plugin
->info
.version
.major
;
643 *minor
= plugin
->info
.version
.minor
;
647 *patch
= plugin
->info
.version
.patch
;
651 *extra
= plugin
->info
.version
.extra
->str
;
658 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
660 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
663 struct bt_component_class
*bt_plugin_get_component_class_by_index(
664 struct bt_plugin
*plugin
, uint64_t index
)
666 struct bt_component_class
*comp_class
= NULL
;
669 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
673 if (index
>= plugin
->comp_classes
->len
) {
674 BT_LOGW("Invalid parameter: index is out of bounds: "
675 "addr=%p, index=%" PRIu64
", count=%u",
676 plugin
, index
, plugin
->comp_classes
->len
);
680 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
691 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
692 struct bt_plugin
*plugin
, const char *name
,
693 enum bt_component_class_type type
)
695 struct bt_component_class
*comp_class
= NULL
;
699 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
704 BT_LOGW_STR("Invalid parameter: name is NULL.");
708 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
709 struct bt_component_class
*comp_class_candidate
=
710 g_ptr_array_index(plugin
->comp_classes
, i
);
711 const char *comp_class_cand_name
=
712 bt_component_class_get_name(comp_class_candidate
);
713 enum bt_component_class_type comp_class_cand_type
=
714 bt_component_class_get_type(comp_class_candidate
);
716 assert(comp_class_cand_name
);
717 assert(comp_class_cand_type
>= 0);
719 if (strcmp(name
, comp_class_cand_name
) == 0 &&
720 comp_class_cand_type
== type
) {
721 comp_class
= bt_get(comp_class_candidate
);
735 enum bt_plugin_status
bt_plugin_add_component_class(
736 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
738 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
739 struct bt_component_class
*comp_class_dup
= NULL
;
740 int comp_class_index
= -1;
743 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
748 BT_LOGW_STR("Invalid parameter: component class is NULL.");
752 if (plugin
->frozen
) {
753 BT_LOGW("Invalid parameter: plugin is frozen: "
754 "addr=%p, name=\"%s\"", plugin
,
755 bt_plugin_get_name(plugin
));
759 /* Check for duplicate */
760 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
761 bt_component_class_get_name(comp_class
),
762 bt_component_class_get_type(comp_class
));
763 if (comp_class_dup
) {
764 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
765 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
766 "comp-class-name=\"%s\", comp-class-type=%s",
767 plugin
, bt_plugin_get_name(plugin
),
768 bt_plugin_get_path(plugin
),
769 bt_component_class_get_name(comp_class
),
770 bt_component_class_type_string(
771 bt_component_class_get_type(comp_class
)));
775 /* Add new component class */
776 comp_class_index
= plugin
->comp_classes
->len
;
777 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
779 /* Special case for a shared object plugin */
780 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
781 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
784 BT_LOGD("Added component class to plugin: "
785 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
786 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
787 plugin
, bt_plugin_get_name(plugin
),
788 bt_plugin_get_path(plugin
),
790 bt_component_class_get_name(comp_class
),
791 bt_component_class_type_string(
792 bt_component_class_get_type(comp_class
)));
796 /* Remove entry from plugin's component classes (if added) */
797 if (comp_class_index
>= 0) {
798 g_ptr_array_remove_index(plugin
->comp_classes
,
802 status
= BT_PLUGIN_STATUS_ERROR
;
805 bt_put(comp_class_dup
);