lib: bt_plugin_find*(): return status code; add "fail on load error" param
[babeltrace.git] / src / lib / plugin / plugin.h
CommitLineData
33b34c43
PP
1#ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2#define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
fb2dcc52
JG
3
4/*
e2f7325d 5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
fb2dcc52
JG
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
fb2dcc52
JG
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
91d81473 27#include "common/macros.h"
578e048b 28#include "lib/graph/component-class.h"
3fadfbc0
MJ
29#include <babeltrace2/plugin/plugin-const.h>
30#include <babeltrace2/plugin/plugin-dev.h>
578e048b 31#include "lib/object.h"
3fadfbc0 32#include <babeltrace2/types.h>
578e048b 33#include "common/assert.h"
55bb57e0 34#include <glib.h>
fb2dcc52 35
578e048b
MJ
36#include "plugin-so.h"
37
d3207ebf 38/* Protection: this file uses BT_LIB_LOG*() macros directly */
7151fb67 39#ifndef BT_LIB_LOG_SUPPORTED
c2d9d9cf 40# error Please include "lib/logging.h" before including this file.
d3207ebf
PP
41#endif
42
55bb57e0
PP
43enum bt_plugin_type {
44 BT_PLUGIN_TYPE_SO = 0,
45 BT_PLUGIN_TYPE_PYTHON = 1,
fb2dcc52
JG
46};
47
33b34c43
PP
48struct bt_plugin {
49 struct bt_object base;
55bb57e0 50 enum bt_plugin_type type;
cba174d5 51
d94d92ac
PP
52 /* Arrays of `struct bt_component_class *` (owned by this) */
53 GPtrArray *src_comp_classes;
54 GPtrArray *flt_comp_classes;
55 GPtrArray *sink_comp_classes;
6ba0b073 56
55bb57e0
PP
57 /* Info (owned by this) */
58 struct {
59 GString *path;
60 GString *name;
61 GString *author;
62 GString *license;
63 GString *description;
64 struct {
65 unsigned int major;
66 unsigned int minor;
67 unsigned int patch;
68 GString *extra;
69 } version;
d94d92ac
PP
70 bool path_set;
71 bool name_set;
72 bool author_set;
73 bool license_set;
74 bool description_set;
75 bool version_set;
55bb57e0
PP
76 } info;
77
78 /* Value depends on the specific plugin type */
79 void *spec_data;
6fbd4105 80 void (*destroy_spec_data)(struct bt_plugin *);
33b34c43 81};
fb2dcc52 82
a8ff38ef
PP
83struct bt_plugin_set {
84 struct bt_object base;
85
86 /* Array of struct bt_plugin * */
87 GPtrArray *plugins;
88};
89
3fe0bf43
PP
90static inline
91const char *bt_plugin_status_string(enum bt_plugin_status status)
92{
93 switch (status) {
94 case BT_PLUGIN_STATUS_OK:
95 return "BT_PLUGIN_STATUS_OK";
9736d991
PP
96 case BT_PLUGIN_STATUS_NOT_FOUND:
97 return "BT_PLUGIN_STATUS_NOT_FOUND";
3fe0bf43
PP
98 case BT_PLUGIN_STATUS_ERROR:
99 return "BT_PLUGIN_STATUS_ERROR";
9736d991
PP
100 case BT_PLUGIN_STATUS_LOADING_ERROR:
101 return "BT_PLUGIN_STATUS_LOADING_ERROR";
3fe0bf43
PP
102 case BT_PLUGIN_STATUS_NOMEM:
103 return "BT_PLUGIN_STATUS_NOMEM";
104 default:
105 return "(unknown)";
106 }
107}
108
109static inline
110const char *bt_plugin_type_string(enum bt_plugin_type type)
111{
112 switch (type) {
113 case BT_PLUGIN_TYPE_SO:
114 return "BT_PLUGIN_TYPE_SO";
115 case BT_PLUGIN_TYPE_PYTHON:
116 return "BT_PLUGIN_TYPE_PYTHON";
117 default:
118 return "(unknown)";
119 }
120}
121
6fbd4105
PP
122static inline
123void bt_plugin_destroy(struct bt_object *obj)
124{
125 struct bt_plugin *plugin;
126
f6ccaed9 127 BT_ASSERT(obj);
6fbd4105 128 plugin = container_of(obj, struct bt_plugin, base);
3f7d4d90 129 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin);
6fbd4105
PP
130
131 if (plugin->destroy_spec_data) {
132 plugin->destroy_spec_data(plugin);
133 }
134
d94d92ac
PP
135 if (plugin->src_comp_classes) {
136 BT_LOGD_STR("Putting source component classes.");
137 g_ptr_array_free(plugin->src_comp_classes, TRUE);
db5504f9 138 plugin->src_comp_classes = NULL;
d94d92ac
PP
139 }
140
141 if (plugin->flt_comp_classes) {
142 BT_LOGD_STR("Putting filter component classes.");
143 g_ptr_array_free(plugin->flt_comp_classes, TRUE);
db5504f9 144 plugin->flt_comp_classes = NULL;
d94d92ac
PP
145 }
146
147 if (plugin->sink_comp_classes) {
148 BT_LOGD_STR("Putting sink component classes.");
149 g_ptr_array_free(plugin->sink_comp_classes, TRUE);
db5504f9 150 plugin->sink_comp_classes = NULL;
6fbd4105
PP
151 }
152
153 if (plugin->info.name) {
154 g_string_free(plugin->info.name, TRUE);
db5504f9 155 plugin->info.name = NULL;
6fbd4105
PP
156 }
157
158 if (plugin->info.path) {
159 g_string_free(plugin->info.path, TRUE);
db5504f9 160 plugin->info.path = NULL;
6fbd4105
PP
161 }
162
163 if (plugin->info.description) {
164 g_string_free(plugin->info.description, TRUE);
db5504f9 165 plugin->info.description = NULL;
6fbd4105
PP
166 }
167
168 if (plugin->info.author) {
169 g_string_free(plugin->info.author, TRUE);
db5504f9 170 plugin->info.author = NULL;
6fbd4105
PP
171 }
172
173 if (plugin->info.license) {
174 g_string_free(plugin->info.license, TRUE);
db5504f9 175 plugin->info.license = NULL;
6fbd4105
PP
176 }
177
178 if (plugin->info.version.extra) {
179 g_string_free(plugin->info.version.extra, TRUE);
db5504f9 180 plugin->info.version.extra = NULL;
6fbd4105
PP
181 }
182
183 g_free(plugin);
184}
185
186static inline
187struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
188{
189 struct bt_plugin *plugin = NULL;
190
3fe0bf43
PP
191 BT_LOGD("Creating empty plugin object: type=%s",
192 bt_plugin_type_string(type));
193
6fbd4105
PP
194 plugin = g_new0(struct bt_plugin, 1);
195 if (!plugin) {
3fe0bf43 196 BT_LOGE_STR("Failed to allocate one plugin.");
6fbd4105
PP
197 goto error;
198 }
199
3fea54f6 200 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
6fbd4105
PP
201 plugin->type = type;
202
d94d92ac
PP
203 /* Create empty arrays of component classes */
204 plugin->src_comp_classes =
205 g_ptr_array_new_with_free_func(
206 (GDestroyNotify) bt_object_put_ref);
207 if (!plugin->src_comp_classes) {
208 BT_LOGE_STR("Failed to allocate a GPtrArray.");
209 goto error;
210 }
211
212 plugin->flt_comp_classes =
213 g_ptr_array_new_with_free_func(
214 (GDestroyNotify) bt_object_put_ref);
215 if (!plugin->flt_comp_classes) {
216 BT_LOGE_STR("Failed to allocate a GPtrArray.");
217 goto error;
218 }
219
220 plugin->sink_comp_classes =
221 g_ptr_array_new_with_free_func(
222 (GDestroyNotify) bt_object_put_ref);
223 if (!plugin->sink_comp_classes) {
3fe0bf43 224 BT_LOGE_STR("Failed to allocate a GPtrArray.");
6fbd4105
PP
225 goto error;
226 }
227
228 /* Create empty info */
229 plugin->info.name = g_string_new(NULL);
230 if (!plugin->info.name) {
3fe0bf43 231 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
232 goto error;
233 }
234
235 plugin->info.path = g_string_new(NULL);
236 if (!plugin->info.path) {
3fe0bf43 237 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
238 goto error;
239 }
240
241 plugin->info.description = g_string_new(NULL);
242 if (!plugin->info.description) {
3fe0bf43 243 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
244 goto error;
245 }
246
247 plugin->info.author = g_string_new(NULL);
248 if (!plugin->info.author) {
3fe0bf43 249 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
250 goto error;
251 }
252
253 plugin->info.license = g_string_new(NULL);
254 if (!plugin->info.license) {
3fe0bf43 255 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
256 goto error;
257 }
258
259 plugin->info.version.extra = g_string_new(NULL);
260 if (!plugin->info.version.extra) {
3fe0bf43 261 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
262 goto error;
263 }
264
d94d92ac 265 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
6fbd4105
PP
266 goto end;
267
268error:
65300d60 269 BT_OBJECT_PUT_REF_AND_RESET(plugin);
6fbd4105
PP
270
271end:
272 return plugin;
273}
55bb57e0
PP
274
275static inline
276void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
277{
f6ccaed9
PP
278 BT_ASSERT(plugin);
279 BT_ASSERT(path);
55bb57e0 280 g_string_assign(plugin->info.path, path);
c55a9f58 281 plugin->info.path_set = BT_TRUE;
3f7d4d90 282 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, path=\"%s\"",
d94d92ac 283 plugin, path);
55bb57e0
PP
284}
285
286static inline
287void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
288{
f6ccaed9
PP
289 BT_ASSERT(plugin);
290 BT_ASSERT(name);
55bb57e0 291 g_string_assign(plugin->info.name, name);
c55a9f58 292 plugin->info.name_set = BT_TRUE;
3f7d4d90 293 BT_LIB_LOGD("Set plugin's name: %![plugin-]+l, name=\"%s\"",
3fe0bf43 294 plugin, name);
55bb57e0
PP
295}
296
297static inline
298void bt_plugin_set_description(struct bt_plugin *plugin,
299 const char *description)
300{
f6ccaed9
PP
301 BT_ASSERT(plugin);
302 BT_ASSERT(description);
55bb57e0 303 g_string_assign(plugin->info.description, description);
c55a9f58 304 plugin->info.description_set = BT_TRUE;
3f7d4d90 305 BT_LIB_LOGD("Set plugin's description: %![plugin-]+l", plugin);
55bb57e0
PP
306}
307
308static inline
309void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
310{
f6ccaed9
PP
311 BT_ASSERT(plugin);
312 BT_ASSERT(author);
55bb57e0 313 g_string_assign(plugin->info.author, author);
c55a9f58 314 plugin->info.author_set = BT_TRUE;
3f7d4d90 315 BT_LIB_LOGD("Set plugin's author: %![plugin-]+l, author=\"%s\"",
d94d92ac 316 plugin, author);
55bb57e0
PP
317}
318
319static inline
320void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
321{
f6ccaed9
PP
322 BT_ASSERT(plugin);
323 BT_ASSERT(license);
55bb57e0 324 g_string_assign(plugin->info.license, license);
c55a9f58 325 plugin->info.license_set = BT_TRUE;
3f7d4d90 326 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, license=\"%s\"",
d94d92ac 327 plugin, license);
55bb57e0
PP
328}
329
330static inline
331void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
332 unsigned int minor, unsigned int patch, const char *extra)
333{
f6ccaed9 334 BT_ASSERT(plugin);
55bb57e0
PP
335 plugin->info.version.major = major;
336 plugin->info.version.minor = minor;
337 plugin->info.version.patch = patch;
338
339 if (extra) {
340 g_string_assign(plugin->info.version.extra, extra);
341 }
342
c55a9f58 343 plugin->info.version_set = BT_TRUE;
3f7d4d90 344 BT_LIB_LOGD("Set plugin's version: %![plugin-]+l, "
3fe0bf43 345 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
d94d92ac 346 plugin, major, minor, patch, extra);
55bb57e0
PP
347}
348
349static inline
d94d92ac
PP
350enum bt_plugin_status bt_plugin_add_component_class(
351 struct bt_plugin *plugin, struct bt_component_class *comp_class)
55bb57e0 352{
d94d92ac
PP
353 GPtrArray *comp_classes;
354
f6ccaed9 355 BT_ASSERT(plugin);
d94d92ac
PP
356 BT_ASSERT(comp_class);
357
358 switch (comp_class->type) {
359 case BT_COMPONENT_CLASS_TYPE_SOURCE:
360 comp_classes = plugin->src_comp_classes;
361 break;
362 case BT_COMPONENT_CLASS_TYPE_FILTER:
363 comp_classes = plugin->flt_comp_classes;
364 break;
365 case BT_COMPONENT_CLASS_TYPE_SINK:
366 comp_classes = plugin->sink_comp_classes;
367 break;
368 default:
369 abort();
370 }
3fe0bf43 371
d94d92ac 372 /* Add new component class */
398454ed
PP
373 bt_object_get_ref(comp_class);
374 g_ptr_array_add(comp_classes, comp_class);
d94d92ac
PP
375
376 /* Special case for a shared object plugin */
377 if (plugin->type == BT_PLUGIN_TYPE_SO) {
378 bt_plugin_so_on_add_component_class(plugin, comp_class);
3fe0bf43
PP
379 }
380
d94d92ac
PP
381 BT_LIB_LOGD("Added component class to plugin: "
382 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
383 return BT_PLUGIN_STATUS_OK;
55bb57e0
PP
384}
385
a8ff38ef
PP
386static
387void bt_plugin_set_destroy(struct bt_object *obj)
388{
389 struct bt_plugin_set *plugin_set =
390 container_of(obj, struct bt_plugin_set, base);
391
392 if (!plugin_set) {
393 return;
394 }
395
3fe0bf43
PP
396 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
397
a8ff38ef 398 if (plugin_set->plugins) {
3fe0bf43 399 BT_LOGD_STR("Putting plugins.");
a8ff38ef
PP
400 g_ptr_array_free(plugin_set->plugins, TRUE);
401 }
402
403 g_free(plugin_set);
404}
405
406static inline
407struct bt_plugin_set *bt_plugin_set_create(void)
408{
409 struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1);
410
411 if (!plugin_set) {
412 goto end;
413 }
414
3fe0bf43 415 BT_LOGD_STR("Creating empty plugin set.");
3fea54f6 416 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
a8ff38ef
PP
417
418 plugin_set->plugins = g_ptr_array_new_with_free_func(
65300d60 419 (GDestroyNotify) bt_object_put_ref);
a8ff38ef 420 if (!plugin_set->plugins) {
3fe0bf43 421 BT_LOGE_STR("Failed to allocate a GPtrArray.");
65300d60 422 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
a8ff38ef
PP
423 goto end;
424 }
425
3fe0bf43
PP
426 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
427
a8ff38ef
PP
428end:
429 return plugin_set;
430}
431
432static inline
433void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
434 struct bt_plugin *plugin)
435{
f6ccaed9
PP
436 BT_ASSERT(plugin_set);
437 BT_ASSERT(plugin);
398454ed
PP
438 bt_object_get_ref(plugin);
439 g_ptr_array_add(plugin_set->plugins, plugin);
3f7d4d90 440 BT_LIB_LOGD("Added plugin to plugin set: "
d94d92ac
PP
441 "plugin-set-addr=%p, %![plugin-]+l",
442 plugin_set, plugin);
8c1a3187
PP
443}
444
33b34c43 445#endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.076304 seconds and 4 git commands to generate.