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 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/plugin/plugin-internal.h>
35 #include <babeltrace/plugin/plugin-so-internal.h>
43 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
44 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
45 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
47 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
48 #include <babeltrace/plugin/python-plugin-provider-internal.h>
50 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
51 bt_plugin_python_create_all_from_file
;
52 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
53 static GModule
*python_plugin_provider_module
;
55 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
57 __attribute__((constructor
)) static
58 void init_python_plugin_provider(void) {
59 python_plugin_provider_module
=
60 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
,
62 if (!python_plugin_provider_module
) {
63 printf_warning("Cannot find `%s`: Python plugin support is disabled\n",
64 PYTHON_PLUGIN_PROVIDER_FILENAME
);
68 if (!g_module_symbol(python_plugin_provider_module
,
69 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
70 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
71 printf_warning("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
75 __attribute__((destructor
)) static
76 void fini_python_plugin_provider(void) {
77 if (python_plugin_provider_module
) {
78 (void) g_module_close(python_plugin_provider_module
);
79 python_plugin_provider_module
= NULL
;
85 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
93 count
= (int64_t) plugin_set
->plugins
->len
;
100 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
103 struct bt_plugin
*plugin
= NULL
;
105 if (!plugin_set
|| index
>= plugin_set
->plugins
->len
) {
109 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
115 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
117 return bt_plugin_so_create_all_from_static();
120 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
122 struct bt_plugin_set
*plugin_set
= NULL
;
128 printf_verbose("Trying to load plugins from `%s`\n", path
);
130 /* Try shared object plugins */
131 plugin_set
= bt_plugin_so_create_all_from_file(path
);
136 /* Try Python plugins if support is available */
137 if (bt_plugin_python_create_all_from_file_sym
) {
138 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
148 static void destroy_gstring(void *data
)
150 g_string_free(data
, TRUE
);
153 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
155 const char *system_plugin_dir
;
156 char *home_plugin_dir
= NULL
;
158 struct bt_plugin
*plugin
= NULL
;
159 struct bt_plugin_set
*plugin_set
= NULL
;
160 GPtrArray
*dirs
= NULL
;
168 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
176 * 1. BABELTRACE_PLUGIN_PATH environment variable
177 * (colon-separated list of directories)
178 * 2. ~/.local/lib/babeltrace/plugins
179 * 3. Default system directory for Babeltrace plugins, usually
180 * /usr/lib/babeltrace/plugins or
181 * /usr/local/lib/babeltrace/plugins if installed
183 * 4. Built-in plugins (static)
185 * Directories are searched non-recursively.
187 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
189 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
195 home_plugin_dir
= bt_common_get_home_plugin_path();
196 if (home_plugin_dir
) {
197 GString
*home_plugin_dir_str
=
198 g_string_new(home_plugin_dir
);
200 if (!home_plugin_dir_str
) {
204 g_ptr_array_add(dirs
, home_plugin_dir_str
);
207 system_plugin_dir
= bt_common_get_system_plugin_path();
208 if (system_plugin_dir
) {
209 GString
*system_plugin_dir_str
=
210 g_string_new(system_plugin_dir
);
212 if (!system_plugin_dir_str
) {
216 g_ptr_array_add(dirs
, system_plugin_dir_str
);
219 for (i
= 0; i
< dirs
->len
; i
++) {
220 GString
*dir
= g_ptr_array_index(dirs
, i
);
222 printf_verbose("Trying to load plugins from directory `%s`\n",
225 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, false);
230 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
231 struct bt_plugin
*candidate_plugin
=
232 g_ptr_array_index(plugin_set
->plugins
, j
);
234 if (strcmp(bt_plugin_get_name(candidate_plugin
),
236 plugin
= bt_get(candidate_plugin
);
243 plugin_set
= bt_plugin_create_all_from_static();
245 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
246 struct bt_plugin
*candidate_plugin
=
247 g_ptr_array_index(plugin_set
->plugins
, j
);
249 if (strcmp(bt_plugin_get_name(candidate_plugin
),
251 plugin
= bt_get(candidate_plugin
);
257 free(home_plugin_dir
);
261 g_ptr_array_free(dirs
, TRUE
);
267 struct bt_component_class
*bt_plugin_find_component_class(
268 const char *plugin_name
, const char *comp_cls_name
,
269 enum bt_component_class_type comp_cls_type
)
271 struct bt_plugin
*plugin
= NULL
;
272 struct bt_component_class
*comp_cls
= NULL
;
274 if (!plugin_name
|| !comp_cls_name
) {
278 plugin
= bt_plugin_find(plugin_name
);
283 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
284 plugin
, comp_cls_name
, comp_cls_type
);
291 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
293 struct dirent
*alloc_dirent(const char *path
)
297 struct dirent
*entry
;
299 name_max
= pathconf(path
, _PC_NAME_MAX
);
300 if (name_max
== -1) {
303 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
304 entry
= zmalloc(len
);
309 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
310 struct bt_plugin_set
*plugin_set
, const char *path
,
313 DIR *directory
= NULL
;
314 struct dirent
*entry
= NULL
, *result
= NULL
;
315 char *file_path
= NULL
;
317 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
320 ret
= BT_PLUGIN_STATUS_ERROR
;
324 path_len
= strlen(path
);
326 if (path_len
>= PATH_MAX
) {
327 ret
= BT_PLUGIN_STATUS_ERROR
;
331 entry
= alloc_dirent(path
);
333 ret
= BT_PLUGIN_STATUS_ERROR
;
337 file_path
= zmalloc(PATH_MAX
);
339 ret
= BT_PLUGIN_STATUS_NOMEM
;
343 strncpy(file_path
, path
, path_len
);
344 /* Append a trailing '/' to the path */
345 if (file_path
[path_len
- 1] != '/') {
346 file_path
[path_len
++] = '/';
349 directory
= opendir(file_path
);
351 printf_verbose("Failed to open plugin directory \"%s\"\n",
353 ret
= BT_PLUGIN_STATUS_ERROR
;
357 /* Recursively walk directory */
358 while (!readdir_r(directory
, entry
, &result
) && result
) {
361 size_t file_name_len
;
363 if (result
->d_name
[0] == '.') {
364 /* Skip hidden files, . and .. */
368 file_name_len
= strlen(result
->d_name
);
370 if (path_len
+ file_name_len
>= PATH_MAX
) {
374 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
375 file_path
[path_len
+ file_name_len
] = '\0';
376 stat_ret
= stat(file_path
, &st
);
378 /* Continue to next file / directory. */
379 printf_perror("Failed to stat() plugin file\n");
383 if (S_ISDIR(st
.st_mode
) && recurse
) {
384 ret
= bt_plugin_create_append_all_from_dir(plugin_set
,
389 } else if (S_ISREG(st
.st_mode
)) {
390 struct bt_plugin_set
*plugins_from_file
=
391 bt_plugin_create_all_from_file(file_path
);
393 if (plugins_from_file
) {
396 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
397 struct bt_plugin
*plugin
=
398 g_ptr_array_index(plugins_from_file
->plugins
, j
);
400 bt_plugin_set_add_plugin(plugin_set
,
404 bt_put(plugins_from_file
);
410 if (closedir(directory
)) {
412 * We don't want to override the error since there is
415 perror("Failed to close plug-in directory");
423 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
426 struct bt_plugin_set
*plugin_set
;
427 enum bt_plugin_status status
;
429 plugin_set
= bt_plugin_set_create();
434 /* Append found plugins to array */
435 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
450 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
452 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
455 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
457 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
460 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
462 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
465 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
467 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
470 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
472 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
475 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
476 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
479 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
481 if (!plugin
|| !plugin
->info
.version_set
) {
482 status
= BT_PLUGIN_STATUS_ERROR
;
487 *major
= plugin
->info
.version
.major
;
491 *minor
= plugin
->info
.version
.minor
;
495 *patch
= plugin
->info
.version
.patch
;
499 *extra
= plugin
->info
.version
.extra
->str
;
506 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
508 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
511 struct bt_component_class
*bt_plugin_get_component_class_by_index(
512 struct bt_plugin
*plugin
, uint64_t index
)
514 struct bt_component_class
*comp_class
= NULL
;
516 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
520 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
531 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
532 struct bt_plugin
*plugin
, const char *name
,
533 enum bt_component_class_type type
)
535 struct bt_component_class
*comp_class
= NULL
;
538 if (!plugin
|| !name
) {
542 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
543 struct bt_component_class
*comp_class_candidate
=
544 g_ptr_array_index(plugin
->comp_classes
, i
);
545 const char *comp_class_cand_name
=
546 bt_component_class_get_name(comp_class_candidate
);
547 enum bt_component_class_type comp_class_cand_type
=
548 bt_component_class_get_type(comp_class_candidate
);
550 assert(comp_class_cand_name
);
551 assert(comp_class_cand_type
>= 0);
553 if (strcmp(name
, comp_class_cand_name
) == 0 &&
554 comp_class_cand_type
== type
) {
555 comp_class
= bt_get(comp_class_candidate
);
569 enum bt_plugin_status
bt_plugin_add_component_class(
570 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
572 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
573 struct bt_component_class
*comp_class_dup
= NULL
;
574 int comp_class_index
= -1;
576 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
580 /* Check for duplicate */
581 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
582 bt_component_class_get_name(comp_class
),
583 bt_component_class_get_type(comp_class
));
584 if (comp_class_dup
) {
585 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
586 bt_plugin_get_name(plugin
),
587 bt_component_class_get_name(comp_class
),
588 bt_component_class_get_type(comp_class
));
592 /* Add new component class */
593 comp_class_index
= plugin
->comp_classes
->len
;
594 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
596 /* Special case for a shared object plugin */
597 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
598 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
604 /* Remove entry from plugin's component classes (if added) */
605 if (comp_class_index
>= 0) {
606 g_ptr_array_remove_index(plugin
->comp_classes
,
610 status
= BT_PLUGIN_STATUS_ERROR
;
613 bt_put(comp_class_dup
);