cli: fix bt_plugin leak when using `-i ctf`
[babeltrace.git] / src / cli / babeltrace2-plugins.c
CommitLineData
2e72e107
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
bf403eb2 25#define BT_LOG_TAG "CLI/PLUGINS"
2e72e107
SM
26#include "logging.h"
27
28#include "babeltrace2-plugins.h"
29
109730d9 30#include <stdbool.h>
2e72e107
SM
31#include <babeltrace2/babeltrace.h>
32
33/* Array of bt_plugin * */
34static GPtrArray *loaded_plugins;
35
36void init_loaded_plugins(void)
37{
38 loaded_plugins = g_ptr_array_new_with_free_func(
39 (GDestroyNotify) bt_plugin_put_ref);
40}
41
42void fini_loaded_plugins(void)
43{
44 g_ptr_array_free(loaded_plugins, TRUE);
45}
46
13aae0f4 47const bt_plugin *borrow_loaded_plugin_by_name(const char *name)
2e72e107
SM
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
2e72e107
SM
72 return plugin;
73}
74
75size_t get_loaded_plugins_count(void)
76{
77 return loaded_plugins->len;
78}
79
77a5caaf
SM
80const bt_plugin **borrow_loaded_plugins(void)
81{
82 return (const bt_plugin **) loaded_plugins->pdata;
83}
84
13aae0f4 85const bt_plugin *borrow_loaded_plugin_by_index(size_t index)
2e72e107
SM
86{
87 BT_ASSERT(index < loaded_plugins->len);
88 return g_ptr_array_index(loaded_plugins, index);
89}
90
91static
92void add_to_loaded_plugins(const bt_plugin_set *plugin_set)
93{
94 int64_t i;
95 int64_t count;
96
97 count = bt_plugin_set_get_plugin_count(plugin_set);
98 BT_ASSERT(count >= 0);
99
100 for (i = 0; i < count; i++) {
101 const bt_plugin *plugin =
102 bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i);
103 const bt_plugin *loaded_plugin =
13aae0f4 104 borrow_loaded_plugin_by_name(bt_plugin_get_name(plugin));
2e72e107
SM
105
106 BT_ASSERT(plugin);
107
108 if (loaded_plugin) {
109 BT_LOGI("Not using plugin: another one already exists with the same name: "
110 "plugin-name=\"%s\", plugin-path=\"%s\", "
111 "existing-plugin-path=\"%s\"",
112 bt_plugin_get_name(plugin),
113 bt_plugin_get_path(plugin),
114 bt_plugin_get_path(loaded_plugin));
2e72e107
SM
115 } else {
116 /* Add to global array. */
117 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
118 bt_plugin_get_name(plugin));
119 bt_plugin_get_ref(plugin);
120 g_ptr_array_add(loaded_plugins, (void *) plugin);
121 }
122 }
123}
124
125static
126int load_dynamic_plugins(const bt_value *plugin_paths)
127{
128 int nr_paths, i, ret = 0;
129
1270d0e9 130 nr_paths = bt_value_array_get_length(plugin_paths);
b602018b
FD
131 if (nr_paths == 0) {
132 BT_LOGI_STR("No dynamic plugin path.");
2e72e107
SM
133 goto end;
134 }
135
136 BT_LOGI_STR("Loading dynamic plugins.");
137
138 for (i = 0; i < nr_paths; i++) {
139 const bt_value *plugin_path_value = NULL;
140 const char *plugin_path;
141 const bt_plugin_set *plugin_set = NULL;
142 bt_plugin_find_all_from_dir_status status;
143
144 plugin_path_value =
145 bt_value_array_borrow_element_by_index_const(
146 plugin_paths, i);
147 plugin_path = bt_value_string_get(plugin_path_value);
148
149 /*
150 * Skip this if the directory does not exist because
151 * bt_plugin_find_all_from_dir() expects an existing
152 * directory.
153 */
154 if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
155 BT_LOGI("Skipping nonexistent directory path: "
156 "path=\"%s\"", plugin_path);
157 continue;
158 }
159
160 status = bt_plugin_find_all_from_dir(plugin_path, BT_FALSE,
0394dc06 161 BT_TRUE, &plugin_set);
2e72e107 162 if (status < 0) {
7d5464fb
SM
163 BT_CLI_LOGE_APPEND_CAUSE(
164 "Unable to load dynamic plugins from directory: "
2e72e107 165 "path=\"%s\"", plugin_path);
7d5464fb
SM
166 ret = status;
167 goto end;
2e72e107
SM
168 } else if (status ==
169 BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND) {
170 BT_LOGI("No plugins found in directory: path=\"%s\"",
171 plugin_path);
172 continue;
173 }
174
175 BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK);
176 BT_ASSERT(plugin_set);
177 add_to_loaded_plugins(plugin_set);
178 bt_plugin_set_put_ref(plugin_set);
179 }
180end:
181 return ret;
182}
183
184static
185int load_static_plugins(void)
186{
187 int ret = 0;
188 const bt_plugin_set *plugin_set;
189 bt_plugin_find_all_from_static_status status;
190
191 BT_LOGI("Loading static plugins.");
192 status = bt_plugin_find_all_from_static(BT_FALSE, &plugin_set);
193 if (status < 0) {
194 BT_LOGE("Unable to load static plugins.");
195 ret = -1;
196 goto end;
197 } else if (status ==
198 BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND) {
199 BT_LOGI("No static plugins found.");
200 goto end;
201 }
202
203 BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK);
204 BT_ASSERT(plugin_set);
205 add_to_loaded_plugins(plugin_set);
206 bt_plugin_set_put_ref(plugin_set);
207
208end:
209 return ret;
210}
211
a1040187 212int require_loaded_plugins(const bt_value *plugin_paths)
2e72e107
SM
213{
214 static bool loaded = false;
215 static int ret = 0;
216
217 if (loaded) {
218 goto end;
219 }
220
221 loaded = true;
222
223 if (load_dynamic_plugins(plugin_paths)) {
224 ret = -1;
225 goto end;
226 }
227
228 if (load_static_plugins()) {
229 ret = -1;
230 goto end;
231 }
232
233 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
234
235end:
236 return ret;
237}
This page took 0.046155 seconds and 4 git commands to generate.