Commit | Line | Data |
---|---|---|
743138a3 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
743138a3 SM |
3 | * |
4 | * Copyright 2016-2019 EfficiOS Inc. | |
5 | * | |
0235b0db | 6 | * Babeltrace trace converter - CLI tool's configuration |
743138a3 SM |
7 | */ |
8 | ||
3c729b9a | 9 | #define BT_LOG_TAG "CLI/PLUGINS" |
743138a3 SM |
10 | #include "logging.h" |
11 | ||
12 | #include "babeltrace2-plugins.h" | |
13 | ||
4b3b8e4a | 14 | #include <stdbool.h> |
743138a3 SM |
15 | #include <babeltrace2/babeltrace.h> |
16 | ||
17 | /* Array of bt_plugin * */ | |
18 | static GPtrArray *loaded_plugins; | |
19 | ||
20 | void init_loaded_plugins(void) | |
21 | { | |
22 | loaded_plugins = g_ptr_array_new_with_free_func( | |
23 | (GDestroyNotify) bt_plugin_put_ref); | |
24 | } | |
25 | ||
26 | void fini_loaded_plugins(void) | |
27 | { | |
28 | g_ptr_array_free(loaded_plugins, TRUE); | |
29 | } | |
30 | ||
97512754 | 31 | const bt_plugin *borrow_loaded_plugin_by_name(const char *name) |
743138a3 SM |
32 | { |
33 | int i; | |
34 | const bt_plugin *plugin = NULL; | |
35 | ||
36 | BT_ASSERT(name); | |
37 | BT_LOGI("Finding plugin: name=\"%s\"", name); | |
38 | ||
39 | for (i = 0; i < loaded_plugins->len; i++) { | |
40 | plugin = g_ptr_array_index(loaded_plugins, i); | |
41 | ||
42 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { | |
43 | break; | |
44 | } | |
45 | ||
46 | plugin = NULL; | |
47 | } | |
48 | ||
49 | if (plugin) { | |
50 | BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p", | |
51 | name, plugin); | |
52 | } else { | |
53 | BT_LOGI("Cannot find plugin: name=\"%s\"", name); | |
54 | } | |
55 | ||
743138a3 SM |
56 | return plugin; |
57 | } | |
58 | ||
59 | size_t get_loaded_plugins_count(void) | |
60 | { | |
61 | return loaded_plugins->len; | |
62 | } | |
63 | ||
97010665 SM |
64 | const bt_plugin **borrow_loaded_plugins(void) |
65 | { | |
66 | return (const bt_plugin **) loaded_plugins->pdata; | |
67 | } | |
68 | ||
97512754 | 69 | const bt_plugin *borrow_loaded_plugin_by_index(size_t index) |
743138a3 SM |
70 | { |
71 | BT_ASSERT(index < loaded_plugins->len); | |
72 | return g_ptr_array_index(loaded_plugins, index); | |
73 | } | |
74 | ||
75 | static | |
76 | void add_to_loaded_plugins(const bt_plugin_set *plugin_set) | |
77 | { | |
78 | int64_t i; | |
79 | int64_t count; | |
80 | ||
81 | count = bt_plugin_set_get_plugin_count(plugin_set); | |
82 | BT_ASSERT(count >= 0); | |
83 | ||
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 = | |
97512754 | 88 | borrow_loaded_plugin_by_name(bt_plugin_get_name(plugin)); |
743138a3 SM |
89 | |
90 | BT_ASSERT(plugin); | |
91 | ||
92 | if (loaded_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)); | |
743138a3 SM |
99 | } else { |
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); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | static | |
110 | int load_dynamic_plugins(const bt_value *plugin_paths) | |
111 | { | |
112 | int nr_paths, i, ret = 0; | |
113 | ||
393729a6 | 114 | nr_paths = bt_value_array_get_length(plugin_paths); |
f80e9ec1 FD |
115 | if (nr_paths == 0) { |
116 | BT_LOGI_STR("No dynamic plugin path."); | |
743138a3 SM |
117 | goto end; |
118 | } | |
119 | ||
120 | BT_LOGI_STR("Loading dynamic plugins."); | |
121 | ||
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; | |
127 | ||
128 | plugin_path_value = | |
129 | bt_value_array_borrow_element_by_index_const( | |
130 | plugin_paths, i); | |
131 | plugin_path = bt_value_string_get(plugin_path_value); | |
132 | ||
133 | /* | |
134 | * Skip this if the directory does not exist because | |
135 | * bt_plugin_find_all_from_dir() expects an existing | |
136 | * directory. | |
137 | */ | |
138 | if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) { | |
139 | BT_LOGI("Skipping nonexistent directory path: " | |
140 | "path=\"%s\"", plugin_path); | |
141 | continue; | |
142 | } | |
143 | ||
144 | status = bt_plugin_find_all_from_dir(plugin_path, BT_FALSE, | |
be05cb0e | 145 | BT_TRUE, &plugin_set); |
743138a3 | 146 | if (status < 0) { |
70dfe5e4 SM |
147 | BT_CLI_LOGE_APPEND_CAUSE( |
148 | "Unable to load dynamic plugins from directory: " | |
743138a3 | 149 | "path=\"%s\"", plugin_path); |
70dfe5e4 SM |
150 | ret = status; |
151 | goto end; | |
743138a3 SM |
152 | } else if (status == |
153 | BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND) { | |
154 | BT_LOGI("No plugins found in directory: path=\"%s\"", | |
155 | plugin_path); | |
156 | continue; | |
157 | } | |
158 | ||
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); | |
163 | } | |
164 | end: | |
165 | return ret; | |
166 | } | |
167 | ||
168 | static | |
169 | int load_static_plugins(void) | |
170 | { | |
171 | int ret = 0; | |
172 | const bt_plugin_set *plugin_set; | |
173 | bt_plugin_find_all_from_static_status status; | |
174 | ||
175 | BT_LOGI("Loading static plugins."); | |
176 | status = bt_plugin_find_all_from_static(BT_FALSE, &plugin_set); | |
177 | if (status < 0) { | |
178 | BT_LOGE("Unable to load static plugins."); | |
179 | ret = -1; | |
180 | goto end; | |
181 | } else if (status == | |
182 | BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND) { | |
183 | BT_LOGI("No static plugins found."); | |
184 | goto end; | |
185 | } | |
186 | ||
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); | |
191 | ||
192 | end: | |
193 | return ret; | |
194 | } | |
195 | ||
73760435 | 196 | int require_loaded_plugins(const bt_value *plugin_paths) |
743138a3 SM |
197 | { |
198 | static bool loaded = false; | |
199 | static int ret = 0; | |
200 | ||
201 | if (loaded) { | |
202 | goto end; | |
203 | } | |
204 | ||
205 | loaded = true; | |
206 | ||
207 | if (load_dynamic_plugins(plugin_paths)) { | |
208 | ret = -1; | |
209 | goto end; | |
210 | } | |
211 | ||
212 | if (load_static_plugins()) { | |
213 | ret = -1; | |
214 | goto end; | |
215 | } | |
216 | ||
217 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); | |
218 | ||
219 | end: | |
220 | return ret; | |
221 | } |