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.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_create_from_name(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 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
251 struct dirent
*alloc_dirent(const char *path
)
255 struct dirent
*entry
;
257 name_max
= pathconf(path
, _PC_NAME_MAX
);
258 if (name_max
== -1) {
261 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
262 entry
= zmalloc(len
);
267 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
268 GPtrArray
*plugins
, const char *path
, bool recurse
)
270 DIR *directory
= NULL
;
271 struct dirent
*entry
= NULL
, *result
= NULL
;
272 char *file_path
= NULL
;
274 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
277 ret
= BT_PLUGIN_STATUS_ERROR
;
281 path_len
= strlen(path
);
283 if (path_len
>= PATH_MAX
) {
284 ret
= BT_PLUGIN_STATUS_ERROR
;
288 entry
= alloc_dirent(path
);
290 ret
= BT_PLUGIN_STATUS_ERROR
;
294 file_path
= zmalloc(PATH_MAX
);
296 ret
= BT_PLUGIN_STATUS_NOMEM
;
300 strncpy(file_path
, path
, path_len
);
301 /* Append a trailing '/' to the path */
302 if (file_path
[path_len
- 1] != '/') {
303 file_path
[path_len
++] = '/';
306 directory
= opendir(file_path
);
308 perror("Failed to open plug-in directory");
309 ret
= BT_PLUGIN_STATUS_ERROR
;
313 /* Recursively walk directory */
314 while (!readdir_r(directory
, entry
, &result
) && result
) {
317 size_t file_name_len
;
319 if (result
->d_name
[0] == '.') {
320 /* Skip hidden files, . and .. */
324 file_name_len
= strlen(result
->d_name
);
326 if (path_len
+ file_name_len
>= PATH_MAX
) {
330 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
331 file_path
[path_len
+ file_name_len
] = '\0';
332 stat_ret
= stat(file_path
, &st
);
334 /* Continue to next file / directory. */
335 printf_perror("Failed to stat() plugin file\n");
339 if (S_ISDIR(st
.st_mode
) && recurse
) {
340 ret
= bt_plugin_create_append_all_from_dir(plugins
,
345 } else if (S_ISREG(st
.st_mode
)) {
346 struct bt_plugin
**plugins_from_file
=
347 bt_plugin_create_all_from_file(file_path
);
349 if (plugins_from_file
) {
350 struct bt_plugin
**plugin
;
352 for (plugin
= plugins_from_file
; *plugin
; plugin
++) {
353 /* Transfer ownership to array */
354 g_ptr_array_add(plugins
, *plugin
);
357 free(plugins_from_file
);
363 if (closedir(directory
)) {
365 * We don't want to override the error since there is
368 perror("Failed to close plug-in directory");
376 struct bt_plugin
**bt_plugin_create_all_from_dir(const char *path
,
379 GPtrArray
*plugins_array
= g_ptr_array_new();
380 struct bt_plugin
**plugins
= NULL
;
381 enum bt_plugin_status status
;
383 /* Append found plugins to array */
384 status
= bt_plugin_create_append_all_from_dir(plugins_array
, path
,
390 /* Add sentinel to array */
391 g_ptr_array_add(plugins_array
, NULL
);
392 plugins
= (struct bt_plugin
**) plugins_array
->pdata
;
397 g_ptr_array_free(plugins_array
, TRUE
);
398 plugins_array
= NULL
;
403 g_ptr_array_free(plugins_array
, FALSE
);
409 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
411 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
414 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
416 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
419 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
421 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
424 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
426 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
429 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
431 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
434 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
435 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
438 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
440 if (!plugin
|| !plugin
->info
.version_set
) {
441 status
= BT_PLUGIN_STATUS_ERROR
;
446 *major
= plugin
->info
.version
.major
;
450 *minor
= plugin
->info
.version
.minor
;
454 *patch
= plugin
->info
.version
.patch
;
458 *extra
= plugin
->info
.version
.extra
->str
;
465 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
467 return plugin
? plugin
->comp_classes
->len
: -1;
470 struct bt_component_class
*bt_plugin_get_component_class(
471 struct bt_plugin
*plugin
, size_t index
)
473 struct bt_component_class
*comp_class
= NULL
;
475 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
479 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
490 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
491 struct bt_plugin
*plugin
, const char *name
,
492 enum bt_component_class_type type
)
494 struct bt_component_class
*comp_class
= NULL
;
497 if (!plugin
|| !name
) {
501 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
502 struct bt_component_class
*comp_class_candidate
=
503 g_ptr_array_index(plugin
->comp_classes
, i
);
504 const char *comp_class_cand_name
=
505 bt_component_class_get_name(comp_class_candidate
);
506 enum bt_component_class_type comp_class_cand_type
=
507 bt_component_class_get_type(comp_class_candidate
);
509 assert(comp_class_cand_name
);
510 assert(comp_class_cand_type
>= 0);
512 if (strcmp(name
, comp_class_cand_name
) == 0 &&
513 comp_class_cand_type
== type
) {
514 comp_class
= bt_get(comp_class_candidate
);
528 enum bt_plugin_status
bt_plugin_add_component_class(
529 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
531 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
532 struct bt_component_class
*comp_class_dup
= NULL
;
534 int comp_class_index
= -1;
536 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
540 /* Check for duplicate */
541 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
542 bt_component_class_get_name(comp_class
),
543 bt_component_class_get_type(comp_class
));
544 if (comp_class_dup
) {
545 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
546 bt_plugin_get_name(plugin
),
547 bt_component_class_get_name(comp_class
),
548 bt_component_class_get_type(comp_class
));
552 /* Add new component class */
553 comp_class_index
= plugin
->comp_classes
->len
;
554 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
556 /* Special case for a shared object plugin */
557 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
558 ret
= bt_plugin_so_on_add_component_class(plugin
, comp_class
);
567 /* Remove entry from plugin's component classes (if added) */
568 if (comp_class_index
>= 0) {
569 g_ptr_array_remove_index(plugin
->comp_classes
,
573 status
= BT_PLUGIN_STATUS_ERROR
;
576 bt_put(comp_class_dup
);