Commit | Line | Data |
---|---|---|
743138a3 SM |
1 | /* |
2 | * Babeltrace trace converter - CLI tool's configuration | |
3 | * | |
4 | * Copyright 2016-2019 EfficiOS Inc. | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
3c729b9a | 25 | #define BT_LOG_TAG "CLI/PLUGINS" |
743138a3 SM |
26 | #include "logging.h" |
27 | ||
28 | #include "babeltrace2-plugins.h" | |
29 | ||
4b3b8e4a | 30 | #include <stdbool.h> |
743138a3 SM |
31 | #include <babeltrace2/babeltrace.h> |
32 | ||
33 | /* Array of bt_plugin * */ | |
34 | static GPtrArray *loaded_plugins; | |
35 | ||
36 | void init_loaded_plugins(void) | |
37 | { | |
38 | loaded_plugins = g_ptr_array_new_with_free_func( | |
39 | (GDestroyNotify) bt_plugin_put_ref); | |
40 | } | |
41 | ||
42 | void fini_loaded_plugins(void) | |
43 | { | |
44 | g_ptr_array_free(loaded_plugins, TRUE); | |
45 | } | |
46 | ||
47 | const bt_plugin *find_loaded_plugin(const char *name) | |
48 | { | |
49 | int i; | |
50 | const bt_plugin *plugin = NULL; | |
51 | ||
52 | BT_ASSERT(name); | |
53 | BT_LOGI("Finding plugin: name=\"%s\"", name); | |
54 | ||
55 | for (i = 0; i < loaded_plugins->len; i++) { | |
56 | plugin = g_ptr_array_index(loaded_plugins, i); | |
57 | ||
58 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { | |
59 | break; | |
60 | } | |
61 | ||
62 | plugin = NULL; | |
63 | } | |
64 | ||
65 | if (plugin) { | |
66 | BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p", | |
67 | name, plugin); | |
68 | } else { | |
69 | BT_LOGI("Cannot find plugin: name=\"%s\"", name); | |
70 | } | |
71 | ||
72 | bt_plugin_get_ref(plugin); | |
73 | return plugin; | |
74 | } | |
75 | ||
76 | size_t get_loaded_plugins_count(void) | |
77 | { | |
78 | return loaded_plugins->len; | |
79 | } | |
80 | ||
97010665 SM |
81 | const bt_plugin **borrow_loaded_plugins(void) |
82 | { | |
83 | return (const bt_plugin **) loaded_plugins->pdata; | |
84 | } | |
85 | ||
743138a3 SM |
86 | const bt_plugin *borrow_loaded_plugin(size_t index) |
87 | { | |
88 | BT_ASSERT(index < loaded_plugins->len); | |
89 | return g_ptr_array_index(loaded_plugins, index); | |
90 | } | |
91 | ||
92 | static | |
93 | void add_to_loaded_plugins(const bt_plugin_set *plugin_set) | |
94 | { | |
95 | int64_t i; | |
96 | int64_t count; | |
97 | ||
98 | count = bt_plugin_set_get_plugin_count(plugin_set); | |
99 | BT_ASSERT(count >= 0); | |
100 | ||
101 | for (i = 0; i < count; i++) { | |
102 | const bt_plugin *plugin = | |
103 | bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); | |
104 | const bt_plugin *loaded_plugin = | |
105 | find_loaded_plugin(bt_plugin_get_name(plugin)); | |
106 | ||
107 | BT_ASSERT(plugin); | |
108 | ||
109 | if (loaded_plugin) { | |
110 | BT_LOGI("Not using plugin: another one already exists with the same name: " | |
111 | "plugin-name=\"%s\", plugin-path=\"%s\", " | |
112 | "existing-plugin-path=\"%s\"", | |
113 | bt_plugin_get_name(plugin), | |
114 | bt_plugin_get_path(plugin), | |
115 | bt_plugin_get_path(loaded_plugin)); | |
116 | bt_plugin_put_ref(loaded_plugin); | |
117 | } else { | |
118 | /* Add to global array. */ | |
119 | BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"", | |
120 | bt_plugin_get_name(plugin)); | |
121 | bt_plugin_get_ref(plugin); | |
122 | g_ptr_array_add(loaded_plugins, (void *) plugin); | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | static | |
128 | int load_dynamic_plugins(const bt_value *plugin_paths) | |
129 | { | |
130 | int nr_paths, i, ret = 0; | |
131 | ||
393729a6 | 132 | nr_paths = bt_value_array_get_length(plugin_paths); |
743138a3 SM |
133 | if (nr_paths < 0) { |
134 | BT_CLI_LOGE_APPEND_CAUSE( | |
135 | "Cannot load dynamic plugins: no plugin path."); | |
136 | ret = -1; | |
137 | goto end; | |
138 | } | |
139 | ||
140 | BT_LOGI_STR("Loading dynamic plugins."); | |
141 | ||
142 | for (i = 0; i < nr_paths; i++) { | |
143 | const bt_value *plugin_path_value = NULL; | |
144 | const char *plugin_path; | |
145 | const bt_plugin_set *plugin_set = NULL; | |
146 | bt_plugin_find_all_from_dir_status status; | |
147 | ||
148 | plugin_path_value = | |
149 | bt_value_array_borrow_element_by_index_const( | |
150 | plugin_paths, i); | |
151 | plugin_path = bt_value_string_get(plugin_path_value); | |
152 | ||
153 | /* | |
154 | * Skip this if the directory does not exist because | |
155 | * bt_plugin_find_all_from_dir() expects an existing | |
156 | * directory. | |
157 | */ | |
158 | if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) { | |
159 | BT_LOGI("Skipping nonexistent directory path: " | |
160 | "path=\"%s\"", plugin_path); | |
161 | continue; | |
162 | } | |
163 | ||
164 | status = bt_plugin_find_all_from_dir(plugin_path, BT_FALSE, | |
be05cb0e | 165 | BT_TRUE, &plugin_set); |
743138a3 | 166 | if (status < 0) { |
70dfe5e4 SM |
167 | BT_CLI_LOGE_APPEND_CAUSE( |
168 | "Unable to load dynamic plugins from directory: " | |
743138a3 | 169 | "path=\"%s\"", plugin_path); |
70dfe5e4 SM |
170 | ret = status; |
171 | goto end; | |
743138a3 SM |
172 | } else if (status == |
173 | BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND) { | |
174 | BT_LOGI("No plugins found in directory: path=\"%s\"", | |
175 | plugin_path); | |
176 | continue; | |
177 | } | |
178 | ||
179 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK); | |
180 | BT_ASSERT(plugin_set); | |
181 | add_to_loaded_plugins(plugin_set); | |
182 | bt_plugin_set_put_ref(plugin_set); | |
183 | } | |
184 | end: | |
185 | return ret; | |
186 | } | |
187 | ||
188 | static | |
189 | int load_static_plugins(void) | |
190 | { | |
191 | int ret = 0; | |
192 | const bt_plugin_set *plugin_set; | |
193 | bt_plugin_find_all_from_static_status status; | |
194 | ||
195 | BT_LOGI("Loading static plugins."); | |
196 | status = bt_plugin_find_all_from_static(BT_FALSE, &plugin_set); | |
197 | if (status < 0) { | |
198 | BT_LOGE("Unable to load static plugins."); | |
199 | ret = -1; | |
200 | goto end; | |
201 | } else if (status == | |
202 | BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND) { | |
203 | BT_LOGI("No static plugins found."); | |
204 | goto end; | |
205 | } | |
206 | ||
207 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK); | |
208 | BT_ASSERT(plugin_set); | |
209 | add_to_loaded_plugins(plugin_set); | |
210 | bt_plugin_set_put_ref(plugin_set); | |
211 | ||
212 | end: | |
213 | return ret; | |
214 | } | |
215 | ||
73760435 | 216 | int require_loaded_plugins(const bt_value *plugin_paths) |
743138a3 SM |
217 | { |
218 | static bool loaded = false; | |
219 | static int ret = 0; | |
220 | ||
221 | if (loaded) { | |
222 | goto end; | |
223 | } | |
224 | ||
225 | loaded = true; | |
226 | ||
227 | if (load_dynamic_plugins(plugin_paths)) { | |
228 | ret = -1; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | if (load_static_plugins()) { | |
233 | ret = -1; | |
234 | goto end; | |
235 | } | |
236 | ||
237 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); | |
238 | ||
239 | end: | |
240 | return ret; | |
241 | } |