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/compiler.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/plugin/plugin-internal.h>
34 #include <babeltrace/plugin/plugin-so-internal.h>
41 #ifdef WITH_PYTHON_PLUGINS
42 # include <babeltrace/plugin/plugin-python-enabled-internal.h>
44 # include <babeltrace/plugin/plugin-python-disabled-internal.h>
48 void bt_plugin_destroy(struct bt_object
*obj
)
50 struct bt_plugin
*plugin
;
53 plugin
= container_of(obj
, struct bt_plugin
, base
);
55 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
56 bt_plugin_so_destroy_spec_data(plugin
);
57 } else if (plugin
->type
== BT_PLUGIN_TYPE_PYTHON
) {
58 bt_plugin_python_destroy_spec_data(plugin
);
63 if (plugin
->comp_classes
) {
64 g_ptr_array_free(plugin
->comp_classes
, TRUE
);
67 if (plugin
->info
.name
) {
68 g_string_free(plugin
->info
.name
, TRUE
);
71 if (plugin
->info
.path
) {
72 g_string_free(plugin
->info
.path
, TRUE
);
75 if (plugin
->info
.description
) {
76 g_string_free(plugin
->info
.description
, TRUE
);
79 if (plugin
->info
.author
) {
80 g_string_free(plugin
->info
.author
, TRUE
);
83 if (plugin
->info
.license
) {
84 g_string_free(plugin
->info
.license
, TRUE
);
87 if (plugin
->info
.version
.extra
) {
88 g_string_free(plugin
->info
.version
.extra
, TRUE
);
95 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
97 struct bt_plugin
*plugin
= NULL
;
99 plugin
= g_new0(struct bt_plugin
, 1);
104 bt_object_init(plugin
, bt_plugin_destroy
);
107 /* Create empty array of component classes */
108 plugin
->comp_classes
=
109 g_ptr_array_new_with_free_func((GDestroyNotify
) bt_put
);
110 if (!plugin
->comp_classes
) {
114 /* Create empty info */
115 plugin
->info
.name
= g_string_new(NULL
);
116 if (!plugin
->info
.name
) {
120 plugin
->info
.path
= g_string_new(NULL
);
121 if (!plugin
->info
.path
) {
125 plugin
->info
.description
= g_string_new(NULL
);
126 if (!plugin
->info
.description
) {
130 plugin
->info
.author
= g_string_new(NULL
);
131 if (!plugin
->info
.author
) {
135 plugin
->info
.license
= g_string_new(NULL
);
136 if (!plugin
->info
.license
) {
140 plugin
->info
.version
.extra
= g_string_new(NULL
);
141 if (!plugin
->info
.version
.extra
) {
154 struct bt_plugin
**bt_plugin_create_all_from_static(void)
156 return bt_plugin_so_create_all_from_static();
159 struct bt_plugin
**bt_plugin_create_all_from_file(const char *path
)
161 struct bt_plugin
**plugins
= NULL
;
167 printf_verbose("Trying to load plugins from `%s`\n", path
);
169 /* Try shared object plugins */
170 plugins
= bt_plugin_so_create_all_from_file(path
);
175 plugins
= bt_plugin_python_create_all_from_file(path
);
184 static void destroy_gstring(void *data
)
186 g_string_free(data
, TRUE
);
189 void free_plugins(struct bt_plugin
**plugins
) {
191 struct bt_plugin
**cur_plugin
= plugins
;
193 while (*cur_plugin
) {
202 struct bt_plugin
*bt_plugin_create_from_name(const char *plugin_name
)
204 const char *system_plugin_dir
;
205 char *home_plugin_dir
= NULL
;
207 struct bt_plugin
*plugin
= NULL
;
208 struct bt_plugin
**plugins
= NULL
;
209 struct bt_plugin
**cur_plugin
;
210 GPtrArray
*dirs
= NULL
;
218 dirs
= g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_gstring
);
226 * 1. BABELTRACE_PLUGIN_PATH environment variable
227 * (colon-separated list of directories)
228 * 2. ~/.local/lib/babeltrace/plugins
229 * 3. Default system directory for Babeltrace plugins, usually
230 * /usr/lib/babeltrace/plugins or
231 * /usr/local/lib/babeltrace/plugins if installed
233 * 4. Built-in plugins (static)
235 * Directories are searched non-recursively.
237 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
239 ret
= bt_common_append_plugin_path_dirs(envvar
, dirs
);
245 home_plugin_dir
= bt_common_get_home_plugin_path();
246 if (home_plugin_dir
) {
247 GString
*home_plugin_dir_str
=
248 g_string_new(home_plugin_dir
);
250 if (!home_plugin_dir_str
) {
254 g_ptr_array_add(dirs
, home_plugin_dir_str
);
257 system_plugin_dir
= bt_common_get_system_plugin_path();
258 if (system_plugin_dir
) {
259 GString
*system_plugin_dir_str
=
260 g_string_new(system_plugin_dir
);
262 if (!system_plugin_dir_str
) {
266 g_ptr_array_add(dirs
, system_plugin_dir_str
);
269 for (i
= 0; i
< dirs
->len
; i
++) {
270 GString
*dir
= g_ptr_array_index(dirs
, i
);
272 printf_verbose("Trying to load plugins from directory `%s`\n",
274 free_plugins(plugins
);
275 plugins
= bt_plugin_create_all_from_dir(dir
->str
, false);
280 cur_plugin
= plugins
;
282 while (*cur_plugin
) {
283 if (strcmp(bt_plugin_get_name(*cur_plugin
), plugin_name
)
285 plugin
= bt_get(*cur_plugin
);
293 free_plugins(plugins
);
294 plugins
= bt_plugin_create_all_from_static();
295 cur_plugin
= plugins
;
297 while (*cur_plugin
) {
298 if (strcmp(bt_plugin_get_name(*cur_plugin
), plugin_name
) == 0) {
299 plugin
= bt_get(*cur_plugin
);
307 free(home_plugin_dir
);
308 free_plugins(plugins
);
311 g_ptr_array_free(dirs
, TRUE
);
317 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
319 struct dirent
*alloc_dirent(const char *path
)
323 struct dirent
*entry
;
325 name_max
= pathconf(path
, _PC_NAME_MAX
);
326 if (name_max
== -1) {
329 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
330 entry
= zmalloc(len
);
335 enum bt_plugin_status
bt_plugin_create_append_all_from_dir(
336 GPtrArray
*plugins
, const char *path
, bool recurse
)
338 DIR *directory
= NULL
;
339 struct dirent
*entry
= NULL
, *result
= NULL
;
340 char *file_path
= NULL
;
342 enum bt_plugin_status ret
= BT_PLUGIN_STATUS_OK
;
345 ret
= BT_PLUGIN_STATUS_ERROR
;
349 path_len
= strlen(path
);
351 if (path_len
>= PATH_MAX
) {
352 ret
= BT_PLUGIN_STATUS_ERROR
;
356 entry
= alloc_dirent(path
);
358 ret
= BT_PLUGIN_STATUS_ERROR
;
362 file_path
= zmalloc(PATH_MAX
);
364 ret
= BT_PLUGIN_STATUS_NOMEM
;
368 strncpy(file_path
, path
, path_len
);
369 /* Append a trailing '/' to the path */
370 if (file_path
[path_len
- 1] != '/') {
371 file_path
[path_len
++] = '/';
374 directory
= opendir(file_path
);
376 perror("Failed to open plug-in directory");
377 ret
= BT_PLUGIN_STATUS_ERROR
;
381 /* Recursively walk directory */
382 while (!readdir_r(directory
, entry
, &result
) && result
) {
385 size_t file_name_len
;
387 if (result
->d_name
[0] == '.') {
388 /* Skip hidden files, . and .. */
392 file_name_len
= strlen(result
->d_name
);
394 if (path_len
+ file_name_len
>= PATH_MAX
) {
398 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
399 file_path
[path_len
+ file_name_len
] = '\0';
400 stat_ret
= stat(file_path
, &st
);
402 /* Continue to next file / directory. */
403 printf_perror("Failed to stat() plugin file\n");
407 if (S_ISDIR(st
.st_mode
) && recurse
) {
408 ret
= bt_plugin_create_append_all_from_dir(plugins
,
413 } else if (S_ISREG(st
.st_mode
)) {
414 struct bt_plugin
**plugins_from_file
=
415 bt_plugin_create_all_from_file(file_path
);
417 if (plugins_from_file
) {
418 struct bt_plugin
**plugin
;
420 for (plugin
= plugins_from_file
; *plugin
; plugin
++) {
421 /* Transfer ownership to array */
422 g_ptr_array_add(plugins
, *plugin
);
425 free(plugins_from_file
);
431 if (closedir(directory
)) {
433 * We don't want to override the error since there is
436 perror("Failed to close plug-in directory");
444 struct bt_plugin
**bt_plugin_create_all_from_dir(const char *path
,
447 GPtrArray
*plugins_array
= g_ptr_array_new();
448 struct bt_plugin
**plugins
= NULL
;
449 enum bt_plugin_status status
;
451 /* Append found plugins to array */
452 status
= bt_plugin_create_append_all_from_dir(plugins_array
, path
,
458 /* Add sentinel to array */
459 g_ptr_array_add(plugins_array
, NULL
);
460 plugins
= (struct bt_plugin
**) plugins_array
->pdata
;
465 g_ptr_array_free(plugins_array
, TRUE
);
466 plugins_array
= NULL
;
471 g_ptr_array_free(plugins_array
, FALSE
);
477 const char *bt_plugin_get_name(struct bt_plugin
*plugin
)
479 return plugin
&& plugin
->info
.name_set
? plugin
->info
.name
->str
: NULL
;
482 const char *bt_plugin_get_author(struct bt_plugin
*plugin
)
484 return plugin
&& plugin
->info
.author_set
? plugin
->info
.author
->str
: NULL
;
487 const char *bt_plugin_get_license(struct bt_plugin
*plugin
)
489 return plugin
&& plugin
->info
.license_set
? plugin
->info
.license
->str
: NULL
;
492 const char *bt_plugin_get_path(struct bt_plugin
*plugin
)
494 return plugin
&& plugin
->info
.path_set
? plugin
->info
.path
->str
: NULL
;
497 const char *bt_plugin_get_description(struct bt_plugin
*plugin
)
499 return plugin
&& plugin
->info
.description_set
? plugin
->info
.description
->str
: NULL
;
502 enum bt_plugin_status
bt_plugin_get_version(struct bt_plugin
*plugin
,
503 unsigned int *major
, unsigned int *minor
, unsigned int *patch
,
506 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
508 if (!plugin
|| !plugin
->info
.version_set
) {
509 status
= BT_PLUGIN_STATUS_ERROR
;
514 *major
= plugin
->info
.version
.major
;
518 *minor
= plugin
->info
.version
.minor
;
522 *patch
= plugin
->info
.version
.patch
;
526 *extra
= plugin
->info
.version
.extra
->str
;
533 int bt_plugin_get_component_class_count(struct bt_plugin
*plugin
)
535 return plugin
? plugin
->comp_classes
->len
: -1;
538 struct bt_component_class
*bt_plugin_get_component_class(
539 struct bt_plugin
*plugin
, size_t index
)
541 struct bt_component_class
*comp_class
= NULL
;
543 if (!plugin
|| index
>= plugin
->comp_classes
->len
) {
547 comp_class
= g_ptr_array_index(plugin
->comp_classes
, index
);
558 struct bt_component_class
*bt_plugin_get_component_class_by_name_and_type(
559 struct bt_plugin
*plugin
, const char *name
,
560 enum bt_component_class_type type
)
562 struct bt_component_class
*comp_class
= NULL
;
565 if (!plugin
|| !name
) {
569 for (i
= 0; i
< plugin
->comp_classes
->len
; i
++) {
570 struct bt_component_class
*comp_class_candidate
=
571 g_ptr_array_index(plugin
->comp_classes
, i
);
572 const char *comp_class_cand_name
=
573 bt_component_class_get_name(comp_class_candidate
);
574 enum bt_component_class_type comp_class_cand_type
=
575 bt_component_class_get_type(comp_class_candidate
);
577 assert(comp_class_cand_name
);
578 assert(comp_class_cand_type
>= 0);
580 if (strcmp(name
, comp_class_cand_name
) == 0 &&
581 comp_class_cand_type
== type
) {
582 comp_class
= bt_get(comp_class_candidate
);
596 enum bt_plugin_status
bt_plugin_add_component_class(
597 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
599 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
600 struct bt_component_class
*comp_class_dup
= NULL
;
602 int comp_class_index
= -1;
604 if (!plugin
|| !comp_class
|| plugin
->frozen
) {
608 /* Check for duplicate */
609 comp_class_dup
= bt_plugin_get_component_class_by_name_and_type(plugin
,
610 bt_component_class_get_name(comp_class
),
611 bt_component_class_get_type(comp_class
));
612 if (comp_class_dup
) {
613 printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
614 bt_plugin_get_name(plugin
),
615 bt_component_class_get_name(comp_class
),
616 bt_component_class_get_type(comp_class
));
620 /* Add new component class */
621 comp_class_index
= plugin
->comp_classes
->len
;
622 g_ptr_array_add(plugin
->comp_classes
, bt_get(comp_class
));
624 /* Special case for a shared object plugin */
625 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
626 ret
= bt_plugin_so_on_add_component_class(plugin
, comp_class
);
635 /* Remove entry from plugin's component classes (if added) */
636 if (comp_class_index
>= 0) {
637 g_ptr_array_remove_index(plugin
->comp_classes
,
641 status
= BT_PLUGIN_STATUS_ERROR
;
644 bt_put(comp_class_dup
);