2 * Babeltrace trace converter - CLI tool's configuration
4 * Copyright 2016-2019 EfficiOS Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include "babeltrace2-plugins.h"
30 #include <babeltrace2/babeltrace.h>
32 /* Array of bt_plugin * */
33 static GPtrArray
*loaded_plugins
;
35 void init_loaded_plugins(void)
37 loaded_plugins
= g_ptr_array_new_with_free_func(
38 (GDestroyNotify
) bt_plugin_put_ref
);
41 void fini_loaded_plugins(void)
43 g_ptr_array_free(loaded_plugins
, TRUE
);
46 const bt_plugin
*find_loaded_plugin(const char *name
)
49 const bt_plugin
*plugin
= NULL
;
52 BT_LOGI("Finding plugin: name=\"%s\"", name
);
54 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
55 plugin
= g_ptr_array_index(loaded_plugins
, i
);
57 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
65 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
68 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
71 bt_plugin_get_ref(plugin
);
75 size_t get_loaded_plugins_count(void)
77 return loaded_plugins
->len
;
80 const bt_plugin
*borrow_loaded_plugin(size_t index
)
82 BT_ASSERT(index
< loaded_plugins
->len
);
83 return g_ptr_array_index(loaded_plugins
, index
);
87 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
92 count
= bt_plugin_set_get_plugin_count(plugin_set
);
93 BT_ASSERT(count
>= 0);
95 for (i
= 0; i
< count
; i
++) {
96 const bt_plugin
*plugin
=
97 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
98 const bt_plugin
*loaded_plugin
=
99 find_loaded_plugin(bt_plugin_get_name(plugin
));
104 BT_LOGI("Not using plugin: another one already exists with the same name: "
105 "plugin-name=\"%s\", plugin-path=\"%s\", "
106 "existing-plugin-path=\"%s\"",
107 bt_plugin_get_name(plugin
),
108 bt_plugin_get_path(plugin
),
109 bt_plugin_get_path(loaded_plugin
));
110 bt_plugin_put_ref(loaded_plugin
);
112 /* Add to global array. */
113 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
114 bt_plugin_get_name(plugin
));
115 bt_plugin_get_ref(plugin
);
116 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
122 int load_dynamic_plugins(const bt_value
*plugin_paths
)
124 int nr_paths
, i
, ret
= 0;
126 nr_paths
= bt_value_array_get_size(plugin_paths
);
128 BT_CLI_LOGE_APPEND_CAUSE(
129 "Cannot load dynamic plugins: no plugin path.");
134 BT_LOGI_STR("Loading dynamic plugins.");
136 for (i
= 0; i
< nr_paths
; i
++) {
137 const bt_value
*plugin_path_value
= NULL
;
138 const char *plugin_path
;
139 const bt_plugin_set
*plugin_set
= NULL
;
140 bt_plugin_find_all_from_dir_status status
;
143 bt_value_array_borrow_element_by_index_const(
145 plugin_path
= bt_value_string_get(plugin_path_value
);
148 * Skip this if the directory does not exist because
149 * bt_plugin_find_all_from_dir() expects an existing
152 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
153 BT_LOGI("Skipping nonexistent directory path: "
154 "path=\"%s\"", plugin_path
);
158 status
= bt_plugin_find_all_from_dir(plugin_path
, BT_FALSE
,
159 BT_FALSE
, &plugin_set
);
161 BT_LOGE("Unable to load dynamic plugins from directory: "
162 "path=\"%s\"", plugin_path
);
165 BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND
) {
166 BT_LOGI("No plugins found in directory: path=\"%s\"",
171 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK
);
172 BT_ASSERT(plugin_set
);
173 add_to_loaded_plugins(plugin_set
);
174 bt_plugin_set_put_ref(plugin_set
);
181 int load_static_plugins(void)
184 const bt_plugin_set
*plugin_set
;
185 bt_plugin_find_all_from_static_status status
;
187 BT_LOGI("Loading static plugins.");
188 status
= bt_plugin_find_all_from_static(BT_FALSE
, &plugin_set
);
190 BT_LOGE("Unable to load static plugins.");
194 BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND
) {
195 BT_LOGI("No static plugins found.");
199 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK
);
200 BT_ASSERT(plugin_set
);
201 add_to_loaded_plugins(plugin_set
);
202 bt_plugin_set_put_ref(plugin_set
);
208 int require_loaded_plugins(const bt_value
*plugin_paths
)
210 static bool loaded
= false;
219 if (load_dynamic_plugins(plugin_paths
)) {
224 if (load_static_plugins()) {
229 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);