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>
36 #include <babeltrace/types.h>
44 #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX
45 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file
46 #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME)
48 #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT
49 #include <babeltrace/plugin/python-plugin-provider-internal.h>
51 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
) =
52 bt_plugin_python_create_all_from_file
;
53 #else /* BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT */
54 static GModule
*python_plugin_provider_module
;
56 struct bt_plugin_set
*(*bt_plugin_python_create_all_from_file_sym
)(const char *path
);
58 __attribute__((constructor
)) static
59 void init_python_plugin_provider(void) {
60 python_plugin_provider_module
=
61 g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME
,
63 if (!python_plugin_provider_module
) {
64 printf_verbose("Cannot find `%s`: Python plugin support is disabled\n",
65 PYTHON_PLUGIN_PROVIDER_FILENAME
);
69 if (!g_module_symbol(python_plugin_provider_module
,
70 PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR
,
71 (gpointer
) &bt_plugin_python_create_all_from_file_sym
)) {
72 printf_verbose("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
76 __attribute__((destructor
)) static
77 void fini_python_plugin_provider(void) {
78 if (python_plugin_provider_module
) {
79 (void) g_module_close(python_plugin_provider_module
);
80 python_plugin_provider_module
= NULL
;
86 int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set
*plugin_set
)
94 count
= (int64_t) plugin_set
->plugins
->len
;
101 struct bt_plugin
*bt_plugin_set_get_plugin(struct bt_plugin_set
*plugin_set
,
104 struct bt_plugin
*plugin
= NULL
;
106 if (!plugin_set
|| index
>= plugin_set
->plugins
->len
) {
110 plugin
= bt_get(g_ptr_array_index(plugin_set
->plugins
, index
));
116 struct bt_plugin_set
*bt_plugin_create_all_from_static(void)
118 return bt_plugin_so_create_all_from_static();
121 struct bt_plugin_set
*bt_plugin_create_all_from_file(const char *path
)
123 struct bt_plugin_set
*plugin_set
= NULL
;
129 printf_verbose("Trying to load plugins from `%s`\n", path
);
131 /* Try shared object plugins */
132 plugin_set
= bt_plugin_so_create_all_from_file(path
);
137 /* Try Python plugins if support is available */
138 if (bt_plugin_python_create_all_from_file_sym
) {
139 plugin_set
= bt_plugin_python_create_all_from_file_sym(path
);
149 static void destroy_gstring(void *data
)
151 g_string_free(data
, TRUE
);
154 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
156 const char *system_plugin_dir
;
157 char *home_plugin_dir
= NULL
;
159 struct bt_plugin
*plugin
= NULL
;
160 struct bt_plugin_set
*plugin_set
= NULL
;
161 GPtrArray
*dirs
= NULL
;
169 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
177 * 1. BABELTRACE_PLUGIN_PATH environment variable
178 * (colon-separated list of directories)
179 * 2. ~/.local/lib/babeltrace/plugins
180 * 3. Default system directory for Babeltrace plugins, usually
181 * /usr/lib/babeltrace/plugins or
182 * /usr/local/lib/babeltrace/plugins if installed
184 * 4. Built-in plugins (static)
186 * Directories are searched non-recursively.
188 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
190 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
196 home_plugin_dir
= bt_common_get_home_plugin_path();
197 if (home_plugin_dir
) {
198 GString
*home_plugin_dir_str
=
199 g_string_new(home_plugin_dir
);
201 if (!home_plugin_dir_str
) {
205 g_ptr_array_add(dirs
, home_plugin_dir_str
);
208 system_plugin_dir
= bt_common_get_system_plugin_path();
209 if (system_plugin_dir
) {
210 GString
*system_plugin_dir_str
=
211 g_string_new(system_plugin_dir
);
213 if (!system_plugin_dir_str
) {
217 g_ptr_array_add(dirs
, system_plugin_dir_str
);
220 for (i
= 0; i
< dirs
->len
; i
++) {
221 GString
*dir
= g_ptr_array_index(dirs
, i
);
223 printf_verbose("Trying to load plugins from directory `%s`\n",
226 plugin_set
= bt_plugin_create_all_from_dir(dir
->str
, BT_FALSE
);
231 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
232 struct bt_plugin
*candidate_plugin
=
233 g_ptr_array_index(plugin_set
->plugins
, j
);
235 if (strcmp(bt_plugin_get_name(candidate_plugin
),
237 plugin
= bt_get(candidate_plugin
);
244 plugin_set
= bt_plugin_create_all_from_static();
246 for (j
= 0; j
< plugin_set
->plugins
->len
; j
++) {
247 struct bt_plugin
*candidate_plugin
=
248 g_ptr_array_index(plugin_set
->plugins
, j
);
250 if (strcmp(bt_plugin_get_name(candidate_plugin
),
252 plugin
= bt_get(candidate_plugin
);
258 free(home_plugin_dir
);
262 g_ptr_array_free(dirs
, TRUE
);
268 struct bt_component_class
*bt_plugin_find_component_class(
269 const char *plugin_name
, const char *comp_cls_name
,
270 enum bt_component_class_type comp_cls_type
)
272 struct bt_plugin
*plugin
= NULL
;
273 struct bt_component_class
*comp_cls
= NULL
;
275 if (!plugin_name
|| !comp_cls_name
) {
279 plugin
= bt_plugin_find(plugin_name
);
284 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
285 plugin
, comp_cls_name
, comp_cls_type
);
292 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
294 struct dirent
*alloc_dirent(const char *path
)
298 struct dirent
*entry
;
300 name_max
= pathconf(path
, _PC_NAME_MAX
);
301 if (name_max
== -1) {
304 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
305 entry
= zmalloc(len
);
310 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
311 struct bt_plugin_set
*plugin_set
, const char *path
,
314 DIR *directory
= NULL
;
315 struct dirent
*entry
= NULL
, *result
= NULL
;
316 char *file_path
= NULL
;
318 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
321 ret
= BT_PLUGIN_STATUS_ERROR
;
325 path_len
= strlen(path
);
327 if (path_len
>= PATH_MAX
) {
328 ret
= BT_PLUGIN_STATUS_ERROR
;
332 entry
= alloc_dirent(path
);
334 ret
= BT_PLUGIN_STATUS_ERROR
;
338 file_path
= zmalloc(PATH_MAX
);
340 ret
= BT_PLUGIN_STATUS_NOMEM
;
344 strncpy(file_path
, path
, path_len
);
345 /* Append a trailing '/' to the path */
346 if (file_path
[path_len
- 1] != '/') {
347 file_path
[path_len
++] = '/';
350 directory
= opendir(file_path
);
352 printf_verbose("Failed to open plugin directory \"%s\"\n",
354 ret
= BT_PLUGIN_STATUS_ERROR
;
358 /* Recursively walk directory */
359 while (!readdir_r(directory
, entry
, &result
) && result
) {
362 size_t file_name_len
;
364 if (result
->d_name
[0] == '.') {
365 /* Skip hidden files, . and .. */
369 file_name_len
= strlen(result
->d_name
);
371 if (path_len
+ file_name_len
>= PATH_MAX
) {
375 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
376 file_path
[path_len
+ file_name_len
] = '\0';
377 stat_ret
= stat(file_path
, &st
);
379 /* Continue to next file / directory. */
380 printf_perror("Failed to stat() plugin file\n");
384 if (S_ISDIR(st
.st_mode
) && recurse
) {
385 ret
= bt_plugin_create_append_all_from_dir(plugin_set
,
390 } else if (S_ISREG(st
.st_mode
)) {
391 struct bt_plugin_set
*plugins_from_file
=
392 bt_plugin_create_all_from_file(file_path
);
394 if (plugins_from_file
) {
397 for (j
= 0; j
< plugins_from_file
->plugins
->len
; j
++) {
398 struct bt_plugin
*plugin
=
399 g_ptr_array_index(plugins_from_file
->plugins
, j
);
401 bt_plugin_set_add_plugin(plugin_set
,
405 bt_put(plugins_from_file
);
411 if (closedir(directory
)) {
413 * We don't want to override the error since there is
416 perror("Failed to close plug-in directory");
424 struct bt_plugin_set
*bt_plugin_create_all_from_dir(const char *path
,
427 struct bt_plugin_set
*plugin_set
;
428 enum bt_plugin_status status
;
430 plugin_set
= bt_plugin_set_create();
435 /* Append found plugins to array */
436 status
= bt_plugin_create_append_all_from_dir(plugin_set
, path
,
451 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
453 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
456 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
458 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
461 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
463 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
466 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
468 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
471 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
473 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
476 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
477 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
480 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
482 if (!plugin
|| !plugin
->info
.version_set
) {
483 status
= BT_PLUGIN_STATUS_ERROR
;
488 *major
= plugin
->info
.version
.major
;
492 *minor
= plugin
->info
.version
.minor
;
496 *patch
= plugin
->info
.version
.patch
;
500 *extra
= plugin
->info
.version
.extra
->str
;
507 int64_t bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
509 return plugin
? plugin
->comp_classes
->len
: (int64_t) -1;
512 struct bt_component_class
*bt_plugin_get_component_class_by_index(
513 struct bt_plugin
*plugin
, uint64_t index
)
515 struct bt_component_class
*comp_class
= NULL
;
517 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
521 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
532 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
533 struct bt_plugin
*plugin
, const char *name
,
534 enum bt_component_class_type type
)
536 struct bt_component_class
*comp_class
= NULL
;
539 if (!plugin
|| !name
) {
543 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
544 struct bt_component_class
*comp_class_candidate
=
545 g_ptr_array_index(plugin
->comp_classes
, i
);
546 const char *comp_class_cand_name
=
547 bt_component_class_get_name(comp_class_candidate
);
548 enum bt_component_class_type comp_class_cand_type
=
549 bt_component_class_get_type(comp_class_candidate
);
551 assert(comp_class_cand_name
);
552 assert(comp_class_cand_type
>= 0);
554 if (strcmp(name
, comp_class_cand_name
) == 0 &&
555 comp_class_cand_type
== type
) {
556 comp_class
= bt_get(comp_class_candidate
);
570 enum bt_plugin_status
bt_plugin_add_component_class(
571 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
573 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
574 struct bt_component_class
*comp_class_dup
= NULL
;
575 int comp_class_index
= -1;
577 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
581 /* Check for duplicate */
582 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
583 bt_component_class_get_name(comp_class
),
584 bt_component_class_get_type(comp_class
));
585 if (comp_class_dup
) {
586 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
587 bt_plugin_get_name(plugin
),
588 bt_component_class_get_name(comp_class
),
589 bt_component_class_get_type(comp_class
));
593 /* Add new component class */
594 comp_class_index
= plugin
->comp_classes
->len
;
595 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
597 /* Special case for a shared object plugin */
598 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
599 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
605 /* Remove entry from plugin's component classes (if added) */
606 if (comp_class_index
>= 0) {
607 g_ptr_array_remove_index(plugin
->comp_classes
,
611 status
= BT_PLUGIN_STATUS_ERROR
;
614 bt_put(comp_class_dup
);