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
**(*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
**(*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
;
83 struct bt_plugin
**bt_plugin_create_all_from_static(void)
85 return bt_plugin_so_create_all_from_static();
88 struct bt_plugin
**bt_plugin_create_all_from_file(const char *path
)
90 struct bt_plugin
**plugins
= NULL
;
96 printf_verbose("Trying to load plugins from `%s`\n", path
);
98 /* Try shared object plugins */
99 plugins
= bt_plugin_so_create_all_from_file(path
);
104 /* Try Python plugins if support is available */
105 if (bt_plugin_python_create_all_from_file_sym
) {
106 plugins
= bt_plugin_python_create_all_from_file_sym(path
);
116 static void destroy_gstring(void *data
)
118 g_string_free(data
, TRUE
);
121 void free_plugins(struct bt_plugin
**plugins
) {
123 struct bt_plugin
**cur_plugin
= plugins
;
125 while (*cur_plugin
) {
134 struct bt_plugin
*bt_plugin_find(const char *plugin_name
)
136 const char *system_plugin_dir
;
137 char *home_plugin_dir
= NULL
;
139 struct bt_plugin
*plugin
= NULL
;
140 struct bt_plugin
**plugins
= NULL
;
141 struct bt_plugin
**cur_plugin
;
142 GPtrArray
*dirs
= NULL
;
150 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
158 * 1. BABELTRACE_PLUGIN_PATH environment variable
159 * (colon-separated list of directories)
160 * 2. ~/.local/lib/babeltrace/plugins
161 * 3. Default system directory for Babeltrace plugins, usually
162 * /usr/lib/babeltrace/plugins or
163 * /usr/local/lib/babeltrace/plugins if installed
165 * 4. Built-in plugins (static)
167 * Directories are searched non-recursively.
169 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
171 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
177 home_plugin_dir
= bt_common_get_home_plugin_path();
178 if (home_plugin_dir
) {
179 GString
*home_plugin_dir_str
=
180 g_string_new(home_plugin_dir
);
182 if (!home_plugin_dir_str
) {
186 g_ptr_array_add(dirs
, home_plugin_dir_str
);
189 system_plugin_dir
= bt_common_get_system_plugin_path();
190 if (system_plugin_dir
) {
191 GString
*system_plugin_dir_str
=
192 g_string_new(system_plugin_dir
);
194 if (!system_plugin_dir_str
) {
198 g_ptr_array_add(dirs
, system_plugin_dir_str
);
201 for (i
= 0; i
< dirs
->len
; i
++) {
202 GString
*dir
= g_ptr_array_index(dirs
, i
);
204 printf_verbose("Trying to load plugins from directory `%s`\n",
206 free_plugins(plugins
);
207 plugins
= bt_plugin_create_all_from_dir(dir
->str
, false);
212 cur_plugin
= plugins
;
214 while (*cur_plugin
) {
215 if (strcmp(bt_plugin_get_name(*cur_plugin
), plugin_name
)
217 plugin
= bt_get(*cur_plugin
);
225 free_plugins(plugins
);
226 plugins
= bt_plugin_create_all_from_static();
227 cur_plugin
= plugins
;
229 while (*cur_plugin
) {
230 if (strcmp(bt_plugin_get_name(*cur_plugin
), plugin_name
) == 0) {
231 plugin
= bt_get(*cur_plugin
);
239 free(home_plugin_dir
);
240 free_plugins(plugins
);
243 g_ptr_array_free(dirs
, TRUE
);
249 struct bt_component_class
*bt_plugin_find_component_class(
250 const char *plugin_name
, const char *comp_cls_name
,
251 enum bt_component_class_type comp_cls_type
)
253 struct bt_plugin
*plugin
= NULL
;
254 struct bt_component_class
*comp_cls
= NULL
;
256 if (!plugin_name
|| !comp_cls_name
) {
260 plugin
= bt_plugin_find(plugin_name
);
265 comp_cls
= bt_plugin_get_component_class_by_name_and_type(
266 plugin
, comp_cls_name
, comp_cls_type
);
273 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
275 struct dirent
*alloc_dirent(const char *path
)
279 struct dirent
*entry
;
281 name_max
= pathconf(path
, _PC_NAME_MAX
);
282 if (name_max
== -1) {
285 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
286 entry
= zmalloc(len
);
291 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
292 GPtrArray
*plugins
, const char *path
, bool recurse
)
294 DIR *directory
= NULL
;
295 struct dirent
*entry
= NULL
, *result
= NULL
;
296 char *file_path
= NULL
;
298 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
301 ret
= BT_PLUGIN_STATUS_ERROR
;
305 path_len
= strlen(path
);
307 if (path_len
>= PATH_MAX
) {
308 ret
= BT_PLUGIN_STATUS_ERROR
;
312 entry
= alloc_dirent(path
);
314 ret
= BT_PLUGIN_STATUS_ERROR
;
318 file_path
= zmalloc(PATH_MAX
);
320 ret
= BT_PLUGIN_STATUS_NOMEM
;
324 strncpy(file_path
, path
, path_len
);
325 /* Append a trailing '/' to the path */
326 if (file_path
[path_len
- 1] != '/') {
327 file_path
[path_len
++] = '/';
330 directory
= opendir(file_path
);
332 printf_verbose("Failed to open plugin directory \"%s\"\n",
334 ret
= BT_PLUGIN_STATUS_ERROR
;
338 /* Recursively walk directory */
339 while (!readdir_r(directory
, entry
, &result
) && result
) {
342 size_t file_name_len
;
344 if (result
->d_name
[0] == '.') {
345 /* Skip hidden files, . and .. */
349 file_name_len
= strlen(result
->d_name
);
351 if (path_len
+ file_name_len
>= PATH_MAX
) {
355 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
356 file_path
[path_len
+ file_name_len
] = '\0';
357 stat_ret
= stat(file_path
, &st
);
359 /* Continue to next file / directory. */
360 printf_perror("Failed to stat() plugin file\n");
364 if (S_ISDIR(st
.st_mode
) && recurse
) {
365 ret
= bt_plugin_create_append_all_from_dir(plugins
,
370 } else if (S_ISREG(st
.st_mode
)) {
371 struct bt_plugin
**plugins_from_file
=
372 bt_plugin_create_all_from_file(file_path
);
374 if (plugins_from_file
) {
375 struct bt_plugin
**plugin
;
377 for (plugin
= plugins_from_file
; *plugin
; plugin
++) {
378 /* Transfer ownership to array */
379 g_ptr_array_add(plugins
, *plugin
);
382 free(plugins_from_file
);
388 if (closedir(directory
)) {
390 * We don't want to override the error since there is
393 perror("Failed to close plug-in directory");
401 struct bt_plugin
**bt_plugin_create_all_from_dir(const char *path
,
404 GPtrArray
*plugins_array
= g_ptr_array_new();
405 struct bt_plugin
**plugins
= NULL
;
406 enum bt_plugin_status status
;
408 /* Append found plugins to array */
409 status
= bt_plugin_create_append_all_from_dir(plugins_array
, path
,
415 /* Add sentinel to array */
416 g_ptr_array_add(plugins_array
, NULL
);
417 plugins
= (struct bt_plugin
**) plugins_array
->pdata
;
422 g_ptr_array_free(plugins_array
, TRUE
);
423 plugins_array
= NULL
;
428 g_ptr_array_free(plugins_array
, FALSE
);
434 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
436 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
439 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
441 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
444 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
446 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
449 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
451 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
454 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
456 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
459 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
460 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
463 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
465 if (!plugin
|| !plugin
->info
.version_set
) {
466 status
= BT_PLUGIN_STATUS_ERROR
;
471 *major
= plugin
->info
.version
.major
;
475 *minor
= plugin
->info
.version
.minor
;
479 *patch
= plugin
->info
.version
.patch
;
483 *extra
= plugin
->info
.version
.extra
->str
;
490 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
492 return plugin
? plugin
->comp_classes
->len
: -1;
495 struct bt_component_class
*bt_plugin_get_component_class(
496 struct bt_plugin
*plugin
, size_t index
)
498 struct bt_component_class
*comp_class
= NULL
;
500 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
504 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
515 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
516 struct bt_plugin
*plugin
, const char *name
,
517 enum bt_component_class_type type
)
519 struct bt_component_class
*comp_class
= NULL
;
522 if (!plugin
|| !name
) {
526 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
527 struct bt_component_class
*comp_class_candidate
=
528 g_ptr_array_index(plugin
->comp_classes
, i
);
529 const char *comp_class_cand_name
=
530 bt_component_class_get_name(comp_class_candidate
);
531 enum bt_component_class_type comp_class_cand_type
=
532 bt_component_class_get_type(comp_class_candidate
);
534 assert(comp_class_cand_name
);
535 assert(comp_class_cand_type
>= 0);
537 if (strcmp(name
, comp_class_cand_name
) == 0 &&
538 comp_class_cand_type
== type
) {
539 comp_class
= bt_get(comp_class_candidate
);
553 enum bt_plugin_status
bt_plugin_add_component_class(
554 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
556 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
557 struct bt_component_class
*comp_class_dup
= NULL
;
558 int comp_class_index
= -1;
560 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
564 /* Check for duplicate */
565 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
566 bt_component_class_get_name(comp_class
),
567 bt_component_class_get_type(comp_class
));
568 if (comp_class_dup
) {
569 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
570 bt_plugin_get_name(plugin
),
571 bt_component_class_get_name(comp_class
),
572 bt_component_class_get_type(comp_class
));
576 /* Add new component class */
577 comp_class_index
= plugin
->comp_classes
->len
;
578 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
580 /* Special case for a shared object plugin */
581 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
582 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
588 /* Remove entry from plugin's component classes (if added) */
589 if (comp_class_index
>= 0) {
590 g_ptr_array_remove_index(plugin
->comp_classes
,
594 status
= BT_PLUGIN_STATUS_ERROR
;
597 bt_put(comp_class_dup
);