2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2019 EfficiOS Inc.
6 * Babeltrace trace converter - CLI tool's configuration
9 #define BT_LOG_TAG "CLI/PLUGINS"
12 #include "babeltrace2-plugins.h"
15 #include <babeltrace2/babeltrace.h>
17 /* Array of bt_plugin * */
18 static GPtrArray
*loaded_plugins
;
20 void init_loaded_plugins(void)
22 loaded_plugins
= g_ptr_array_new_with_free_func(
23 (GDestroyNotify
) bt_plugin_put_ref
);
26 void fini_loaded_plugins(void)
28 g_ptr_array_free(loaded_plugins
, TRUE
);
31 const bt_plugin
*borrow_loaded_plugin_by_name(const char *name
)
34 const bt_plugin
*plugin
= NULL
;
37 BT_LOGI("Finding plugin: name=\"%s\"", name
);
39 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
40 plugin
= g_ptr_array_index(loaded_plugins
, i
);
42 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
50 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
53 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
59 size_t get_loaded_plugins_count(void)
61 return loaded_plugins
->len
;
64 const bt_plugin
**borrow_loaded_plugins(void)
66 return (const bt_plugin
**) loaded_plugins
->pdata
;
69 const bt_plugin
*borrow_loaded_plugin_by_index(size_t index
)
71 BT_ASSERT(index
< loaded_plugins
->len
);
72 return g_ptr_array_index(loaded_plugins
, index
);
76 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
81 count
= bt_plugin_set_get_plugin_count(plugin_set
);
82 BT_ASSERT(count
>= 0);
84 for (i
= 0; i
< count
; i
++) {
85 const bt_plugin
*plugin
=
86 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
87 const bt_plugin
*loaded_plugin
=
88 borrow_loaded_plugin_by_name(bt_plugin_get_name(plugin
));
93 BT_LOGI("Not using plugin: another one already exists with the same name: "
94 "plugin-name=\"%s\", plugin-path=\"%s\", "
95 "existing-plugin-path=\"%s\"",
96 bt_plugin_get_name(plugin
),
97 bt_plugin_get_path(plugin
),
98 bt_plugin_get_path(loaded_plugin
));
100 /* Add to global array. */
101 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
102 bt_plugin_get_name(plugin
));
103 bt_plugin_get_ref(plugin
);
104 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
110 int load_dynamic_plugins(const bt_value
*plugin_paths
)
112 int nr_paths
, i
, ret
= 0;
114 nr_paths
= bt_value_array_get_length(plugin_paths
);
116 BT_LOGI_STR("No dynamic plugin path.");
120 BT_LOGI_STR("Loading dynamic plugins.");
122 for (i
= 0; i
< nr_paths
; i
++) {
123 const bt_value
*plugin_path_value
= NULL
;
124 const char *plugin_path
;
125 const bt_plugin_set
*plugin_set
= NULL
;
126 bt_plugin_find_all_from_dir_status status
;
129 bt_value_array_borrow_element_by_index_const(
131 plugin_path
= bt_value_string_get(plugin_path_value
);
134 * Skip this if the directory does not exist because
135 * bt_plugin_find_all_from_dir() expects an existing
138 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
139 BT_LOGI("Skipping nonexistent directory path: "
140 "path=\"%s\"", plugin_path
);
144 status
= bt_plugin_find_all_from_dir(plugin_path
, BT_FALSE
,
145 BT_TRUE
, &plugin_set
);
147 BT_CLI_LOGE_APPEND_CAUSE(
148 "Unable to load dynamic plugins from directory: "
149 "path=\"%s\"", plugin_path
);
153 BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND
) {
154 BT_LOGI("No plugins found in directory: path=\"%s\"",
159 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK
);
160 BT_ASSERT(plugin_set
);
161 add_to_loaded_plugins(plugin_set
);
162 bt_plugin_set_put_ref(plugin_set
);
169 int load_static_plugins(void)
172 const bt_plugin_set
*plugin_set
;
173 bt_plugin_find_all_from_static_status status
;
175 BT_LOGI("Loading static plugins.");
176 status
= bt_plugin_find_all_from_static(BT_FALSE
, &plugin_set
);
178 BT_LOGE("Unable to load static plugins.");
182 BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND
) {
183 BT_LOGI("No static plugins found.");
187 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK
);
188 BT_ASSERT(plugin_set
);
189 add_to_loaded_plugins(plugin_set
);
190 bt_plugin_set_put_ref(plugin_set
);
196 int require_loaded_plugins(const bt_value
*plugin_paths
)
198 static bool loaded
= false;
207 if (load_dynamic_plugins(plugin_paths
)) {
212 if (load_static_plugins()) {
217 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);