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
, 0);
73 if (!python_plugin_provider_module
) {
74 BT_LOGI("Cannot open `%s`: %s: continuing without Python plugin support.",
75 PYTHON_PLUGIN_PROVIDER_FILENAME
, g_module_error());
79 if (!g_module_symbol(python_plugin_provider_module
,
80 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
81 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
82 BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
83 "file=\"%s\", symbol=\"%s\"",
84 PYTHON_PLUGIN_PROVIDER_FILENAME
,
85 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
);
89 BT_LOGI("Loaded Python plugin provider module: addr=%p",
90 python_plugin_provider_module
);
93 __attribute__((destructor
)) static
94 void fini_python_plugin_provider(void) {
95 if (python_plugin_provider_module
) {
96 BT_LOGD("Unloading Python plugin provider module.");
98 if (!g_module_close(python_plugin_provider_module
)) {
99 BT_LOGE("Failed to close the Python plugin provider module: %s.",
103 python_plugin_provider_module
= NULL
;
109 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
114 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
118 count
= (int64_t) plugin_set
->plugins
->len
;
125 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
128 struct bt_plugin
*plugin
= NULL
;
131 BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
135 if (index
>= plugin_set
->plugins
->len
) {
136 BT_LOGW("Invalid parameter: index is out of bounds: "
137 "addr=%p, index=%" PRIu64
", count=%u",
138 plugin_set
, index
, plugin_set
->plugins
->len
);
142 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
148 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
150 /* bt_plugin_so_create_all_from_static() logs errors */
151 return bt_plugin_so_create_all_from_static();
154 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
156 struct bt_plugin_set
*plugin_set
= NULL
;
159 BT_LOGW_STR("Invalid parameter: path is NULL.");
163 BT_LOGD("Creating plugins from file: path=\"%s\"", path
);
165 /* Try shared object plugins */
166 plugin_set
= bt_plugin_so_create_all_from_file(path
);
171 /* Try Python plugins if support is available */
172 if (bt_plugin_python_create_all_from_file_sym
) {
173 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
181 BT_LOGD("Created %u plugins from file: "
182 "path=\"%s\", count=%u, plugin-set-addr=%p",
183 plugin_set
->plugins
->len
, path
,
184 plugin_set
->plugins
->len
, plugin_set
);
186 BT_LOGD("Found no plugins in file: path=\"%s\"", path
);
192 static void destroy_gstring(void *data
)
194 g_string_free(data
, TRUE
);
197 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
199 const char *system_plugin_dir
;
200 char *home_plugin_dir
= NULL
;
202 struct bt_plugin
*plugin
= NULL
;
203 struct bt_plugin_set
*plugin_set
= NULL
;
204 GPtrArray
*dirs
= NULL
;
209 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
213 BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
214 "name=\"%s\"", plugin_name
);
215 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
217 BT_LOGE_STR("Failed to allocate a GPtrArray.");
224 * 1. BABELTRACE_PLUGIN_PATH environment variable
225 * (colon-separated list of directories)
226 * 2. ~/.local/lib/babeltrace/plugins
227 * 3. Default system directory for Babeltrace plugins, usually
228 * /usr/lib/babeltrace/plugins or
229 * /usr/local/lib/babeltrace/plugins if installed
231 * 4. Built-in plugins (static)
233 * Directories are searched non-recursively.
235 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
237 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
239 BT_LOGE_STR("Failed to append plugin path to array of directories.");
244 home_plugin_dir
= bt_common_get_home_plugin_path();
245 if (home_plugin_dir
) {
246 GString
*home_plugin_dir_str
=
247 g_string_new(home_plugin_dir
);
249 if (!home_plugin_dir_str
) {
250 BT_LOGE_STR("Failed to allocate a GString.");
254 g_ptr_array_add(dirs
, home_plugin_dir_str
);
257 system_plugin_dir
= bt_common_get_system_plugin_path();
258 if (system_plugin_dir
) {
259 GString
*system_plugin_dir_str
=
260 g_string_new(system_plugin_dir
);
262 if (!system_plugin_dir_str
) {
263 BT_LOGE_STR("Failed to allocate a GString.");
267 g_ptr_array_add(dirs
, system_plugin_dir_str
);
270 for (i
= 0; i
< dirs
->len
; i
++) {
271 GString
*dir
= g_ptr_array_index(dirs
, i
);
276 * Skip this if the directory does not exist because
277 * bt_plugin_create_all_from_dir() would log a warning.
279 if (!g_file_test(dir
->str
, G_FILE_TEST_IS_DIR
)) {
280 BT_LOGV("Skipping nonexistent directory path: "
281 "path=\"%s\"", dir
->str
);
285 /* bt_plugin_create_all_from_dir() logs details/errors */
286 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
288 BT_LOGD("No plugins found in directory: path=\"%s\"",
293 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
294 struct bt_plugin
*candidate_plugin
=
295 g_ptr_array_index(plugin_set
->plugins
, j
);
297 if (strcmp(bt_plugin_get_name(candidate_plugin
),
299 BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
300 plugin_name
, dir
->str
);
301 plugin
= bt_get(candidate_plugin
);
306 BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
307 plugin_name
, dir
->str
);
311 plugin_set
= bt_plugin_create_all_from_static();
313 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
314 struct bt_plugin
*candidate_plugin
=
315 g_ptr_array_index(plugin_set
->plugins
, j
);
317 if (strcmp(bt_plugin_get_name(candidate_plugin
),
319 BT_LOGD("Plugin found in built-in plugins: "
320 "name=\"%s\"", plugin_name
);
321 plugin
= bt_get(candidate_plugin
);
328 free(home_plugin_dir
);
332 g_ptr_array_free(dirs
, TRUE
);
336 BT_LOGD("Found plugin in standard directories and built-in plugins: "
337 "addr=%p, name=\"%s\", path=\"%s\"",
338 plugin
, plugin_name
, bt_plugin_get_path(plugin
));
340 BT_LOGD("No plugin found in standard directories and built-in plugins: "
341 "name=\"%s\"", plugin_name
);
347 struct bt_component_class
*bt_plugin_find_component_class(
348 const char *plugin_name
, const char *comp_cls_name
,
349 enum bt_component_class_type comp_cls_type
)
351 struct bt_plugin
*plugin
= NULL
;
352 struct bt_component_class
*comp_cls
= NULL
;
355 BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
359 if (!comp_cls_name
) {
360 BT_LOGW_STR("Invalid parameter: component class name is NULL.");
364 BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
365 "plugin-name=\"%s\", comp-class-name=\"%s\"",
366 plugin_name
, comp_cls_name
);
367 plugin
= bt_plugin_find(plugin_name
);
369 BT_LOGD_STR("Plugin not found.");
373 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
374 plugin
, comp_cls_name
, comp_cls_type
);
376 BT_LOGD("Component class not found in plugin: "
377 "plugin-addr=%p, plugin-path=\"%s\"",
378 plugin
, bt_plugin_get_path(plugin
));
387 pthread_mutex_t lock
;
388 struct bt_plugin_set
*plugin_set
;
390 } append_all_from_dir_info
= {
391 .lock
= PTHREAD_MUTEX_INITIALIZER
395 int nftw_append_all_from_dir(const char *file
, const struct stat
*sb
, int flag
,
399 const char *name
= file
+ s
->base
;
401 /* Check for recursion */
402 if (!append_all_from_dir_info
.recurse
&& s
->level
> 1) {
409 struct bt_plugin_set
*plugins_from_file
;
411 if (name
[0] == '.') {
412 /* Skip hidden files */
413 BT_LOGV("Skipping hidden file: path=\"%s\"", file
);
417 plugins_from_file
= bt_plugin_create_all_from_file(file
);
419 if (plugins_from_file
) {
422 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
423 struct bt_plugin
*plugin
=
424 g_ptr_array_index(plugins_from_file
->plugins
, j
);
426 BT_LOGD("Adding plugin to plugin set: "
427 "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
428 file
, plugin
, bt_plugin_get_name(plugin
));
429 bt_plugin_set_add_plugin(append_all_from_dir_info
.plugin_set
, plugin
);
432 bt_put(plugins_from_file
);
437 /* Continue to next file / directory. */
438 BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file
);
441 /* Continue to next file / directory. */
442 BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file
);
451 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
452 struct bt_plugin_set
*plugin_set
, const char *path
,
455 int nftw_flags
= FTW_PHYS
;
457 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
460 BT_LOGW_STR("Invalid parameter: path is NULL.");
461 ret
= BT_PLUGIN_STATUS_ERROR
;
465 path_len
= strlen(path
);
466 if (path_len
>= PATH_MAX
) {
467 BT_LOGW("Invalid parameter: path length is too large: "
468 "path-length=%zu", path_len
);
469 ret
= BT_PLUGIN_STATUS_ERROR
;
473 pthread_mutex_lock(&append_all_from_dir_info
.lock
);
475 append_all_from_dir_info
.plugin_set
= plugin_set
;
476 append_all_from_dir_info
.recurse
= recurse
;
477 ret
= nftw(path
, nftw_append_all_from_dir
,
478 APPEND_ALL_FROM_DIR_NFDOPEN_MAX
, nftw_flags
);
480 pthread_mutex_unlock(&append_all_from_dir_info
.lock
);
483 BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d",
484 strerror(errno
), path
, errno
);
485 ret
= BT_PLUGIN_STATUS_ERROR
;
492 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
495 struct bt_plugin_set
*plugin_set
;
496 enum bt_plugin_status status
;
498 BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
500 plugin_set
= bt_plugin_set_create();
502 BT_LOGE_STR("Cannot create empty plugin set.");
506 /* Append found plugins to array */
507 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
510 BT_LOGW("Cannot append plugins found in directory: "
511 "path=\"%s\", status=%s",
512 path
, bt_plugin_status_string(status
));
516 BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
517 plugin_set
->plugins
->len
, plugin_set
->plugins
->len
, path
);
527 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
529 const char *val
= NULL
;
532 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
536 if (plugin
->info
.name_set
) {
537 val
= plugin
->info
.name
->str
;
544 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
546 const char *val
= NULL
;
549 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
553 if (plugin
->info
.author_set
) {
554 val
= plugin
->info
.author
->str
;
561 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
563 const char *val
= NULL
;
566 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
570 if (plugin
->info
.license_set
) {
571 val
= plugin
->info
.license
->str
;
578 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
580 const char *val
= NULL
;
583 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
587 if (plugin
->info
.path_set
) {
588 val
= plugin
->info
.path
->str
;
595 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
597 const char *val
= NULL
;
600 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
604 if (plugin
->info
.description_set
) {
605 val
= plugin
->info
.description
->str
;
612 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
613 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
616 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
619 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
620 status
= BT_PLUGIN_STATUS_ERROR
;
624 if (!plugin
->info
.version_set
) {
625 BT_LOGV("Plugin's version is not set: addr=%p", plugin
);
626 status
= BT_PLUGIN_STATUS_ERROR
;
631 *major
= plugin
->info
.version
.major
;
635 *minor
= plugin
->info
.version
.minor
;
639 *patch
= plugin
->info
.version
.patch
;
643 *extra
= plugin
->info
.version
.extra
->str
;
650 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
652 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
655 struct bt_component_class
*bt_plugin_get_component_class_by_index(
656 struct bt_plugin
*plugin
, uint64_t index
)
658 struct bt_component_class
*comp_class
= NULL
;
661 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
665 if (index
>= plugin
->comp_classes
->len
) {
666 BT_LOGW("Invalid parameter: index is out of bounds: "
667 "addr=%p, index=%" PRIu64
", count=%u",
668 plugin
, index
, plugin
->comp_classes
->len
);
672 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
683 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
684 struct bt_plugin
*plugin
, const char *name
,
685 enum bt_component_class_type type
)
687 struct bt_component_class
*comp_class
= NULL
;
691 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
696 BT_LOGW_STR("Invalid parameter: name is NULL.");
700 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
701 struct bt_component_class
*comp_class_candidate
=
702 g_ptr_array_index(plugin
->comp_classes
, i
);
703 const char *comp_class_cand_name
=
704 bt_component_class_get_name(comp_class_candidate
);
705 enum bt_component_class_type comp_class_cand_type
=
706 bt_component_class_get_type(comp_class_candidate
);
708 assert(comp_class_cand_name
);
709 assert(comp_class_cand_type
>= 0);
711 if (strcmp(name
, comp_class_cand_name
) == 0 &&
712 comp_class_cand_type
== type
) {
713 comp_class
= bt_get(comp_class_candidate
);
727 enum bt_plugin_status
bt_plugin_add_component_class(
728 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
730 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
731 struct bt_component_class
*comp_class_dup
= NULL
;
732 int comp_class_index
= -1;
735 BT_LOGW_STR("Invalid parameter: plugin is NULL.");
740 BT_LOGW_STR("Invalid parameter: component class is NULL.");
744 if (plugin
->frozen
) {
745 BT_LOGW("Invalid parameter: plugin is frozen: "
746 "addr=%p, name=\"%s\"", plugin
,
747 bt_plugin_get_name(plugin
));
751 /* Check for duplicate */
752 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
753 bt_component_class_get_name(comp_class
),
754 bt_component_class_get_type(comp_class
));
755 if (comp_class_dup
) {
756 BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
757 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
758 "comp-class-name=\"%s\", comp-class-type=%s",
759 plugin
, bt_plugin_get_name(plugin
),
760 bt_plugin_get_path(plugin
),
761 bt_component_class_get_name(comp_class
),
762 bt_component_class_type_string(
763 bt_component_class_get_type(comp_class
)));
767 /* Add new component class */
768 comp_class_index
= plugin
->comp_classes
->len
;
769 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
771 /* Special case for a shared object plugin */
772 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
773 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
776 BT_LOGD("Added component class to plugin: "
777 "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
778 "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
779 plugin
, bt_plugin_get_name(plugin
),
780 bt_plugin_get_path(plugin
),
782 bt_component_class_get_name(comp_class
),
783 bt_component_class_type_string(
784 bt_component_class_get_type(comp_class
)));
788 /* Remove entry from plugin's component classes (if added) */
789 if (comp_class_index
>= 0) {
790 g_ptr_array_remove_index(plugin
->comp_classes
,
794 status
= BT_PLUGIN_STATUS_ERROR
;
797 bt_put(comp_class_dup
);