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>
42 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
43 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
44 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
46 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
47 #include <babeltrace/plugin/python-plugin-provider-internal.h>
49 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
50 bt_plugin_python_create_all_from_file
;
51 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
52 static GModule
*python_plugin_provider_module
;
54 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
56 __attribute__((constructor
)) static
57 void init_python_plugin_provider(void) {
58 python_plugin_provider_module
=
59 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
,
61 if (!python_plugin_provider_module
) {
62 printf_warning("Cannot find `%s`: Python plugin support is disabled\n",
63 PYTHON_PLUGIN_PROVIDER_FILENAME
);
67 if (!g_module_symbol(python_plugin_provider_module
,
68 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
69 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
70 printf_warning("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
74 __attribute__((destructor
)) static
75 void fini_python_plugin_provider(void) {
76 if (python_plugin_provider_module
) {
77 (void) g_module_close(python_plugin_provider_module
);
78 python_plugin_provider_module
= NULL
;
84 int bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
92 count
= plugin_set
->plugins
->len
;
99 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
102 struct bt_plugin
*plugin
= NULL
;
104 if (!plugin_set
|| index
>= plugin_set
->plugins
->len
) {
108 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
114 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
116 return bt_plugin_so_create_all_from_static();
119 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
121 struct bt_plugin_set
*plugin_set
= NULL
;
127 printf_verbose("Trying to load plugins from `%s`\n", path
);
129 /* Try shared object plugins */
130 plugin_set
= bt_plugin_so_create_all_from_file(path
);
135 /* Try Python plugins if support is available */
136 if (bt_plugin_python_create_all_from_file_sym
) {
137 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
147 static void destroy_gstring(void *data
)
149 g_string_free(data
, TRUE
);
152 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
154 const char *system_plugin_dir
;
155 char *home_plugin_dir
= NULL
;
157 struct bt_plugin
*plugin
= NULL
;
158 struct bt_plugin_set
*plugin_set
= NULL
;
159 GPtrArray
*dirs
= NULL
;
167 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
175 * 1. BABELTRACE_PLUGIN_PATH environment variable
176 * (colon-separated list of directories)
177 * 2. ~/.local/lib/babeltrace/plugins
178 * 3. Default system directory for Babeltrace plugins, usually
179 * /usr/lib/babeltrace/plugins or
180 * /usr/local/lib/babeltrace/plugins if installed
182 * 4. Built-in plugins (static)
184 * Directories are searched non-recursively.
186 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
188 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
194 home_plugin_dir
= bt_common_get_home_plugin_path();
195 if (home_plugin_dir
) {
196 GString
*home_plugin_dir_str
=
197 g_string_new(home_plugin_dir
);
199 if (!home_plugin_dir_str
) {
203 g_ptr_array_add(dirs
, home_plugin_dir_str
);
206 system_plugin_dir
= bt_common_get_system_plugin_path();
207 if (system_plugin_dir
) {
208 GString
*system_plugin_dir_str
=
209 g_string_new(system_plugin_dir
);
211 if (!system_plugin_dir_str
) {
215 g_ptr_array_add(dirs
, system_plugin_dir_str
);
218 for (i
= 0; i
< dirs
->len
; i
++) {
219 GString
*dir
= g_ptr_array_index(dirs
, i
);
221 printf_verbose("Trying to load plugins from directory `%s`\n",
224 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, false);
229 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
230 struct bt_plugin
*candidate_plugin
=
231 g_ptr_array_index(plugin_set
->plugins
, j
);
233 if (strcmp(bt_plugin_get_name(candidate_plugin
),
235 plugin
= bt_get(candidate_plugin
);
242 plugin_set
= bt_plugin_create_all_from_static();
244 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
245 struct bt_plugin
*candidate_plugin
=
246 g_ptr_array_index(plugin_set
->plugins
, j
);
248 if (strcmp(bt_plugin_get_name(candidate_plugin
),
250 plugin
= bt_get(candidate_plugin
);
256 free(home_plugin_dir
);
260 g_ptr_array_free(dirs
, TRUE
);
266 struct bt_component_class
*bt_plugin_find_component_class(
267 const char *plugin_name
, const char *comp_cls_name
,
268 enum bt_component_class_type comp_cls_type
)
270 struct bt_plugin
*plugin
= NULL
;
271 struct bt_component_class
*comp_cls
= NULL
;
273 if (!plugin_name
|| !comp_cls_name
) {
277 plugin
= bt_plugin_find(plugin_name
);
282 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
283 plugin
, comp_cls_name
, comp_cls_type
);
290 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
292 struct dirent
*alloc_dirent(const char *path
)
296 struct dirent
*entry
;
298 name_max
= pathconf(path
, _PC_NAME_MAX
);
299 if (name_max
== -1) {
302 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
303 entry
= zmalloc(len
);
308 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
309 struct bt_plugin_set
*plugin_set
, const char *path
,
312 DIR *directory
= NULL
;
313 struct dirent
*entry
= NULL
, *result
= NULL
;
314 char *file_path
= NULL
;
316 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
319 ret
= BT_PLUGIN_STATUS_ERROR
;
323 path_len
= strlen(path
);
325 if (path_len
>= PATH_MAX
) {
326 ret
= BT_PLUGIN_STATUS_ERROR
;
330 entry
= alloc_dirent(path
);
332 ret
= BT_PLUGIN_STATUS_ERROR
;
336 file_path
= zmalloc(PATH_MAX
);
338 ret
= BT_PLUGIN_STATUS_NOMEM
;
342 strncpy(file_path
, path
, path_len
);
343 /* Append a trailing '/' to the path */
344 if (file_path
[path_len
- 1] != '/') {
345 file_path
[path_len
++] = '/';
348 directory
= opendir(file_path
);
350 printf_verbose("Failed to open plugin directory \"%s\"\n",
352 ret
= BT_PLUGIN_STATUS_ERROR
;
356 /* Recursively walk directory */
357 while (!readdir_r(directory
, entry
, &result
) && result
) {
360 size_t file_name_len
;
362 if (result
->d_name
[0] == '.') {
363 /* Skip hidden files, . and .. */
367 file_name_len
= strlen(result
->d_name
);
369 if (path_len
+ file_name_len
>= PATH_MAX
) {
373 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
374 file_path
[path_len
+ file_name_len
] = '\0';
375 stat_ret
= stat(file_path
, &st
);
377 /* Continue to next file / directory. */
378 printf_perror("Failed to stat() plugin file\n");
382 if (S_ISDIR(st
.st_mode
) && recurse
) {
383 ret
= bt_plugin_create_append_all_from_dir(plugin_set
,
388 } else if (S_ISREG(st
.st_mode
)) {
389 struct bt_plugin_set
*plugins_from_file
=
390 bt_plugin_create_all_from_file(file_path
);
392 if (plugins_from_file
) {
395 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
396 struct bt_plugin
*plugin
=
397 g_ptr_array_index(plugins_from_file
->plugins
, j
);
399 bt_plugin_set_add_plugin(plugin_set
,
403 bt_put(plugins_from_file
);
409 if (closedir(directory
)) {
411 * We don't want to override the error since there is
414 perror("Failed to close plug-in directory");
422 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
425 struct bt_plugin_set
*plugin_set
;
426 enum bt_plugin_status status
;
428 plugin_set
= bt_plugin_set_create();
433 /* Append found plugins to array */
434 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
449 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
451 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
454 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
456 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
459 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
461 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
464 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
466 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
469 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
471 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
474 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
475 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
478 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
480 if (!plugin
|| !plugin
->info
.version_set
) {
481 status
= BT_PLUGIN_STATUS_ERROR
;
486 *major
= plugin
->info
.version
.major
;
490 *minor
= plugin
->info
.version
.minor
;
494 *patch
= plugin
->info
.version
.patch
;
498 *extra
= plugin
->info
.version
.extra
->str
;
505 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
507 return plugin
? plugin
->comp_classes
->len
: -1;
510 struct bt_component_class
*bt_plugin_get_component_class(
511 struct bt_plugin
*plugin
, size_t index
)
513 struct bt_component_class
*comp_class
= NULL
;
515 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
519 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
530 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
531 struct bt_plugin
*plugin
, const char *name
,
532 enum bt_component_class_type type
)
534 struct bt_component_class
*comp_class
= NULL
;
537 if (!plugin
|| !name
) {
541 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
542 struct bt_component_class
*comp_class_candidate
=
543 g_ptr_array_index(plugin
->comp_classes
, i
);
544 const char *comp_class_cand_name
=
545 bt_component_class_get_name(comp_class_candidate
);
546 enum bt_component_class_type comp_class_cand_type
=
547 bt_component_class_get_type(comp_class_candidate
);
549 assert(comp_class_cand_name
);
550 assert(comp_class_cand_type
>= 0);
552 if (strcmp(name
, comp_class_cand_name
) == 0 &&
553 comp_class_cand_type
== type
) {
554 comp_class
= bt_get(comp_class_candidate
);
568 enum bt_plugin_status
bt_plugin_add_component_class(
569 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
571 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
572 struct bt_component_class
*comp_class_dup
= NULL
;
573 int comp_class_index
= -1;
575 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
579 /* Check for duplicate */
580 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
581 bt_component_class_get_name(comp_class
),
582 bt_component_class_get_type(comp_class
));
583 if (comp_class_dup
) {
584 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
585 bt_plugin_get_name(plugin
),
586 bt_component_class_get_name(comp_class
),
587 bt_component_class_get_type(comp_class
));
591 /* Add new component class */
592 comp_class_index
= plugin
->comp_classes
->len
;
593 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
595 /* Special case for a shared object plugin */
596 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
597 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
603 /* Remove entry from plugin's component classes (if added) */
604 if (comp_class_index
>= 0) {
605 g_ptr_array_remove_index(plugin
->comp_classes
,
609 status
= BT_PLUGIN_STATUS_ERROR
;
612 bt_put(comp_class_dup
);