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>
42 #include <babeltrace/assert-internal.h>
52 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
53 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
54 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
56 #define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8
58 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
59 #include <babeltrace/plugin/python-plugin-provider-internal.h>
62 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
63 bt_plugin_python_create_all_from_file
;
66 void init_python_plugin_provider(void) {}
67 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
68 static GModule
*python_plugin_provider_module
;
70 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
73 void init_python_plugin_provider(void) {
74 if (bt_plugin_python_create_all_from_file_sym
!= NULL
) {
78 BT_LOGD_STR("Loading Python plugin provider module.");
79 python_plugin_provider_module
=
80 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
, 0);
81 if (!python_plugin_provider_module
) {
82 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
83 PYTHON_PLUGIN_PROVIDER_FILENAME
, g_module_error());
87 if (!g_module_symbol(python_plugin_provider_module
,
88 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
89 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
90 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
91 "file=\"%s\", symbol=\"%s\"",
92 PYTHON_PLUGIN_PROVIDER_FILENAME
,
93 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
97 BT_LOGI("Loaded Python plugin provider module: addr=%p",
98 python_plugin_provider_module
);
101 __attribute__((destructor
)) static
102 void fini_python_plugin_provider(void) {
103 if (python_plugin_provider_module
) {
104 BT_LOGD("Unloading Python plugin provider module.");
106 if (!g_module_close(python_plugin_provider_module
)) {
107 BT_LOGE("Failed to close the Python plugin provider module: %s.",
111 python_plugin_provider_module
= NULL
;
117 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
122 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
126 count
= (int64_t) plugin_set
->plugins
->len
;
133 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
136 struct bt_plugin
*plugin
= NULL
;
139 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
143 if (index
>= plugin_set
->plugins
->len
) {
144 BT_LOGW("Invalid parameter: index is out of bounds: "
145 "addr=%p, index=%" PRIu64
", count=%u",
146 plugin_set
, index
, plugin_set
->plugins
->len
);
150 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
156 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
158 /* bt_plugin_so_create_all_from_static() logs errors */
159 return bt_plugin_so_create_all_from_static();
162 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
164 struct bt_plugin_set
*plugin_set
= NULL
;
167 BT_LOGW_STR("Invalid parameter: path is NULL.");
171 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
173 /* Try shared object plugins */
174 plugin_set
= bt_plugin_so_create_all_from_file(path
);
179 /* Try Python plugins if support is available */
180 init_python_plugin_provider();
181 if (bt_plugin_python_create_all_from_file_sym
) {
182 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
190 BT_LOGD("Created %u plugins from file: "
191 "path=\"%s\", count=%u, plugin-set-addr=%p",
192 plugin_set
->plugins
->len
, path
,
193 plugin_set
->plugins
->len
, plugin_set
);
195 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
201 static void destroy_gstring(void *data
)
203 g_string_free(data
, TRUE
);
206 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
208 const char *system_plugin_dir
;
209 char *home_plugin_dir
= NULL
;
211 struct bt_plugin
*plugin
= NULL
;
212 struct bt_plugin_set
*plugin_set
= NULL
;
213 GPtrArray
*dirs
= NULL
;
218 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
222 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
223 "name=\"%s\"", plugin_name
);
224 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
226 BT_LOGE_STR("Failed to allocate a GPtrArray.");
233 * 1. BABELTRACE_PLUGIN_PATH environment variable
234 * (colon-separated list of directories)
235 * 2. ~/.local/lib/babeltrace/plugins
236 * 3. Default system directory for Babeltrace plugins, usually
237 * /usr/lib/babeltrace/plugins or
238 * /usr/local/lib/babeltrace/plugins if installed
240 * 4. Built-in plugins (static)
242 * Directories are searched non-recursively.
244 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
246 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
248 BT_LOGE_STR("Failed to append plugin path to array of directories.");
253 home_plugin_dir
= bt_common_get_home_plugin_path();
254 if (home_plugin_dir
) {
255 GString
*home_plugin_dir_str
=
256 g_string_new(home_plugin_dir
);
258 if (!home_plugin_dir_str
) {
259 BT_LOGE_STR("Failed to allocate a GString.");
263 g_ptr_array_add(dirs
, home_plugin_dir_str
);
266 system_plugin_dir
= bt_common_get_system_plugin_path();
267 if (system_plugin_dir
) {
268 GString
*system_plugin_dir_str
=
269 g_string_new(system_plugin_dir
);
271 if (!system_plugin_dir_str
) {
272 BT_LOGE_STR("Failed to allocate a GString.");
276 g_ptr_array_add(dirs
, system_plugin_dir_str
);
279 for (i
= 0; i
< dirs
->len
; i
++) {
280 GString
*dir
= g_ptr_array_index(dirs
, i
);
285 * Skip this if the directory does not exist because
286 * bt_plugin_create_all_from_dir() would log a warning.
288 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
289 BT_LOGV("Skipping nonexistent directory path: "
290 "path=\"%s\"", dir
->str
);
294 /* bt_plugin_create_all_from_dir() logs details/errors */
295 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
297 BT_LOGD("No plugins found in directory: path=\"%s\"",
302 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
303 struct bt_plugin
*candidate_plugin
=
304 g_ptr_array_index(plugin_set
->plugins
, j
);
306 if (strcmp(bt_plugin_get_name(candidate_plugin
),
308 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
309 plugin_name
, dir
->str
);
310 plugin
= bt_get(candidate_plugin
);
315 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
316 plugin_name
, dir
->str
);
320 plugin_set
= bt_plugin_create_all_from_static();
322 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
323 struct bt_plugin
*candidate_plugin
=
324 g_ptr_array_index(plugin_set
->plugins
, j
);
326 if (strcmp(bt_plugin_get_name(candidate_plugin
),
328 BT_LOGD("Plugin found in built-in plugins: "
329 "name=\"%s\"", plugin_name
);
330 plugin
= bt_get(candidate_plugin
);
337 free(home_plugin_dir
);
341 g_ptr_array_free(dirs
, TRUE
);
345 BT_LOGD("Found plugin in standard directories and built-in plugins: "
346 "addr=%p, name=\"%s\", path=\"%s\"",
347 plugin
, plugin_name
, bt_plugin_get_path(plugin
));
349 BT_LOGD("No plugin found in standard directories and built-in plugins: "
350 "name=\"%s\"", plugin_name
);
356 struct bt_component_class
*bt_plugin_find_component_class(
357 const char *plugin_name
, const char *comp_cls_name
,
358 enum bt_component_class_type comp_cls_type
)
360 struct bt_plugin
*plugin
= NULL
;
361 struct bt_component_class
*comp_cls
= NULL
;
364 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
368 if (!comp_cls_name
) {
369 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
373 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
374 "plugin-name=\"%s\", comp-class-name=\"%s\"",
375 plugin_name
, comp_cls_name
);
376 plugin
= bt_plugin_find(plugin_name
);
378 BT_LOGD_STR("Plugin not found.");
382 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
383 plugin
, comp_cls_name
, comp_cls_type
);
385 BT_LOGD("Component class not found in plugin: "
386 "plugin-addr=%p, plugin-path=\"%s\"",
387 plugin
, bt_plugin_get_path(plugin
));
396 pthread_mutex_t lock
;
397 struct bt_plugin_set
*plugin_set
;
399 } append_all_from_dir_info
= {
400 .lock
= PTHREAD_MUTEX_INITIALIZER
404 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
408 const char *name
= file
+ s
->base
;
410 /* Check for recursion */
411 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
418 struct bt_plugin_set
*plugins_from_file
;
420 if (name
[0] == '.') {
421 /* Skip hidden files */
422 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
426 plugins_from_file
= bt_plugin_create_all_from_file(file
);
428 if (plugins_from_file
) {
431 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
432 struct bt_plugin
*plugin
=
433 g_ptr_array_index(plugins_from_file
->plugins
, j
);
435 BT_LOGD("Adding plugin to plugin set: "
436 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
437 file
, plugin
, bt_plugin_get_name(plugin
));
438 bt_plugin_set_add_plugin(append_all_from_dir_info
.plugin_set
, plugin
);
441 bt_put(plugins_from_file
);
446 /* Continue to next file / directory. */
447 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
450 /* Continue to next file / directory. */
451 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
460 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
461 struct bt_plugin_set
*plugin_set
, const char *path
,
464 int nftw_flags
= FTW_PHYS
;
466 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
469 BT_LOGW_STR("Invalid parameter: path is NULL.");
470 ret
= BT_PLUGIN_STATUS_ERROR
;
474 path_len
= strlen(path
);
475 if (path_len
>= PATH_MAX
) {
476 BT_LOGW("Invalid parameter: path length is too large: "
477 "path-length=%zu", path_len
);
478 ret
= BT_PLUGIN_STATUS_ERROR
;
482 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
484 append_all_from_dir_info
.plugin_set
= plugin_set
;
485 append_all_from_dir_info
.recurse
= recurse
;
486 ret
= nftw(path
, nftw_append_all_from_dir
,
487 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
489 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
492 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
493 strerror(errno
), path
, errno
);
494 ret
= BT_PLUGIN_STATUS_ERROR
;
501 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
504 struct bt_plugin_set
*plugin_set
;
505 enum bt_plugin_status status
;
507 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
509 plugin_set
= bt_plugin_set_create();
511 BT_LOGE_STR("Cannot create empty plugin set.");
515 /* Append found plugins to array */
516 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
519 BT_LOGW("Cannot append plugins found in directory: "
520 "path=\"%s\", status=%s",
521 path
, bt_plugin_status_string(status
));
525 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
526 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
536 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
538 const char *val
= NULL
;
541 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
545 if (plugin
->info
.name_set
) {
546 val
= plugin
->info
.name
->str
;
553 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
555 const char *val
= NULL
;
558 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
562 if (plugin
->info
.author_set
) {
563 val
= plugin
->info
.author
->str
;
570 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
572 const char *val
= NULL
;
575 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
579 if (plugin
->info
.license_set
) {
580 val
= plugin
->info
.license
->str
;
587 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
589 const char *val
= NULL
;
592 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
596 if (plugin
->info
.path_set
) {
597 val
= plugin
->info
.path
->str
;
604 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
606 const char *val
= NULL
;
609 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
613 if (plugin
->info
.description_set
) {
614 val
= plugin
->info
.description
->str
;
621 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
622 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
625 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
628 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
629 status
= BT_PLUGIN_STATUS_ERROR
;
633 if (!plugin
->info
.version_set
) {
634 BT_LOGV("Plugin's version is not set: addr=%p", plugin
);
635 status
= BT_PLUGIN_STATUS_ERROR
;
640 *major
= plugin
->info
.version
.major
;
644 *minor
= plugin
->info
.version
.minor
;
648 *patch
= plugin
->info
.version
.patch
;
652 *extra
= plugin
->info
.version
.extra
->str
;
659 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
661 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
664 struct bt_component_class
*bt_plugin_get_component_class_by_index(
665 struct bt_plugin
*plugin
, uint64_t index
)
667 struct bt_component_class
*comp_class
= NULL
;
670 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
674 if (index
>= plugin
->comp_classes
->len
) {
675 BT_LOGW("Invalid parameter: index is out of bounds: "
676 "addr=%p, index=%" PRIu64
", count=%u",
677 plugin
, index
, plugin
->comp_classes
->len
);
681 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
692 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
693 struct bt_plugin
*plugin
, const char *name
,
694 enum bt_component_class_type type
)
696 struct bt_component_class
*comp_class
= NULL
;
700 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
705 BT_LOGW_STR("Invalid parameter: name is NULL.");
709 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
710 struct bt_component_class
*comp_class_candidate
=
711 g_ptr_array_index(plugin
->comp_classes
, i
);
712 const char *comp_class_cand_name
=
713 bt_component_class_get_name(comp_class_candidate
);
714 enum bt_component_class_type comp_class_cand_type
=
715 bt_component_class_get_type(comp_class_candidate
);
717 BT_ASSERT(comp_class_cand_name
);
718 BT_ASSERT(comp_class_cand_type
>= 0);
720 if (strcmp(name
, comp_class_cand_name
) == 0 &&
721 comp_class_cand_type
== type
) {
722 comp_class
= bt_get(comp_class_candidate
);
736 enum bt_plugin_status
bt_plugin_add_component_class(
737 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
739 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
740 struct bt_component_class
*comp_class_dup
= NULL
;
741 int comp_class_index
= -1;
744 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
749 BT_LOGW_STR("Invalid parameter: component class is NULL.");
753 if (plugin
->frozen
) {
754 BT_LOGW("Invalid parameter: plugin is frozen: "
755 "addr=%p, name=\"%s\"", plugin
,
756 bt_plugin_get_name(plugin
));
760 /* Check for duplicate */
761 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
762 bt_component_class_get_name(comp_class
),
763 bt_component_class_get_type(comp_class
));
764 if (comp_class_dup
) {
765 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
766 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
767 "comp-class-name=\"%s\", comp-class-type=%s",
768 plugin
, bt_plugin_get_name(plugin
),
769 bt_plugin_get_path(plugin
),
770 bt_component_class_get_name(comp_class
),
771 bt_component_class_type_string(
772 bt_component_class_get_type(comp_class
)));
776 /* Add new component class */
777 comp_class_index
= plugin
->comp_classes
->len
;
778 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
780 /* Special case for a shared object plugin */
781 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
782 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
785 BT_LOGD("Added component class to plugin: "
786 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
787 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
788 plugin
, bt_plugin_get_name(plugin
),
789 bt_plugin_get_path(plugin
),
791 bt_component_class_get_name(comp_class
),
792 bt_component_class_type_string(
793 bt_component_class_get_type(comp_class
)));
797 /* Remove entry from plugin's component classes (if added) */
798 if (comp_class_index
>= 0) {
799 g_ptr_array_remove_index(plugin
->comp_classes
,
803 status
= BT_PLUGIN_STATUS_ERROR
;
806 bt_put(comp_class_dup
);