5f8e6b11dbd0650ea1dfe132712ef0ebb8d54de3
[babeltrace.git] / src / lib / plugin / plugin.h
1 #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2 #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
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
27 #include "common/macros.h"
28 #include "common/common.h"
29 #include "lib/graph/component-class.h"
30 #include <babeltrace2/plugin/plugin-loading.h>
31 #include <babeltrace2/plugin/plugin-dev.h>
32 #include "lib/object.h"
33 #include <babeltrace2/types.h>
34 #include "common/assert.h"
35 #include <glib.h>
36 #include <stdbool.h>
37
38 #include "plugin-so.h"
39 #include "lib/func-status.h"
40
41 /* Protection: this file uses BT_LIB_LOG*() macros directly */
42 #ifndef BT_LIB_LOG_SUPPORTED
43 # error Please include "lib/logging.h" before including this file.
44 #endif
45
46 enum bt_plugin_type {
47 BT_PLUGIN_TYPE_SO = 0,
48 BT_PLUGIN_TYPE_PYTHON = 1,
49 };
50
51 struct bt_plugin {
52 struct bt_object base;
53 enum bt_plugin_type type;
54
55 /* Arrays of `struct bt_component_class *` (owned by this) */
56 GPtrArray *src_comp_classes;
57 GPtrArray *flt_comp_classes;
58 GPtrArray *sink_comp_classes;
59
60 /* Info (owned by this) */
61 struct {
62 GString *path;
63 GString *name;
64 GString *author;
65 GString *license;
66 GString *description;
67 struct {
68 unsigned int major;
69 unsigned int minor;
70 unsigned int patch;
71 GString *extra;
72 } version;
73 bool path_set;
74 bool name_set;
75 bool author_set;
76 bool license_set;
77 bool description_set;
78 bool version_set;
79 } info;
80
81 /* Value depends on the specific plugin type */
82 void *spec_data;
83 void (*destroy_spec_data)(struct bt_plugin *);
84 };
85
86 struct bt_plugin_set {
87 struct bt_object base;
88
89 /* Array of struct bt_plugin * */
90 GPtrArray *plugins;
91 };
92
93 static inline
94 const char *bt_plugin_type_string(enum bt_plugin_type type)
95 {
96 switch (type) {
97 case BT_PLUGIN_TYPE_SO:
98 return "SO";
99 case BT_PLUGIN_TYPE_PYTHON:
100 return "PYTHON";
101 default:
102 return "(unknown)";
103 }
104 }
105
106 static inline
107 void bt_plugin_destroy(struct bt_object *obj)
108 {
109 struct bt_plugin *plugin;
110
111 BT_ASSERT(obj);
112 plugin = container_of(obj, struct bt_plugin, base);
113 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin);
114
115 if (plugin->destroy_spec_data) {
116 plugin->destroy_spec_data(plugin);
117 }
118
119 if (plugin->src_comp_classes) {
120 BT_LOGD_STR("Putting source component classes.");
121 g_ptr_array_free(plugin->src_comp_classes, TRUE);
122 plugin->src_comp_classes = NULL;
123 }
124
125 if (plugin->flt_comp_classes) {
126 BT_LOGD_STR("Putting filter component classes.");
127 g_ptr_array_free(plugin->flt_comp_classes, TRUE);
128 plugin->flt_comp_classes = NULL;
129 }
130
131 if (plugin->sink_comp_classes) {
132 BT_LOGD_STR("Putting sink component classes.");
133 g_ptr_array_free(plugin->sink_comp_classes, TRUE);
134 plugin->sink_comp_classes = NULL;
135 }
136
137 if (plugin->info.name) {
138 g_string_free(plugin->info.name, TRUE);
139 plugin->info.name = NULL;
140 }
141
142 if (plugin->info.path) {
143 g_string_free(plugin->info.path, TRUE);
144 plugin->info.path = NULL;
145 }
146
147 if (plugin->info.description) {
148 g_string_free(plugin->info.description, TRUE);
149 plugin->info.description = NULL;
150 }
151
152 if (plugin->info.author) {
153 g_string_free(plugin->info.author, TRUE);
154 plugin->info.author = NULL;
155 }
156
157 if (plugin->info.license) {
158 g_string_free(plugin->info.license, TRUE);
159 plugin->info.license = NULL;
160 }
161
162 if (plugin->info.version.extra) {
163 g_string_free(plugin->info.version.extra, TRUE);
164 plugin->info.version.extra = NULL;
165 }
166
167 g_free(plugin);
168 }
169
170 static inline
171 struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
172 {
173 struct bt_plugin *plugin = NULL;
174
175 BT_LOGD("Creating empty plugin object: type=%s",
176 bt_plugin_type_string(type));
177
178 plugin = g_new0(struct bt_plugin, 1);
179 if (!plugin) {
180 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one plugin.");
181 goto error;
182 }
183
184 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
185 plugin->type = type;
186
187 /* Create empty arrays of component classes */
188 plugin->src_comp_classes =
189 g_ptr_array_new_with_free_func(
190 (GDestroyNotify) bt_object_put_ref);
191 if (!plugin->src_comp_classes) {
192 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
193 goto error;
194 }
195
196 plugin->flt_comp_classes =
197 g_ptr_array_new_with_free_func(
198 (GDestroyNotify) bt_object_put_ref);
199 if (!plugin->flt_comp_classes) {
200 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
201 goto error;
202 }
203
204 plugin->sink_comp_classes =
205 g_ptr_array_new_with_free_func(
206 (GDestroyNotify) bt_object_put_ref);
207 if (!plugin->sink_comp_classes) {
208 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
209 goto error;
210 }
211
212 /* Create empty info */
213 plugin->info.name = g_string_new(NULL);
214 if (!plugin->info.name) {
215 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
216 goto error;
217 }
218
219 plugin->info.path = g_string_new(NULL);
220 if (!plugin->info.path) {
221 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
222 goto error;
223 }
224
225 plugin->info.description = g_string_new(NULL);
226 if (!plugin->info.description) {
227 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
228 goto error;
229 }
230
231 plugin->info.author = g_string_new(NULL);
232 if (!plugin->info.author) {
233 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
234 goto error;
235 }
236
237 plugin->info.license = g_string_new(NULL);
238 if (!plugin->info.license) {
239 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
240 goto error;
241 }
242
243 plugin->info.version.extra = g_string_new(NULL);
244 if (!plugin->info.version.extra) {
245 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
246 goto error;
247 }
248
249 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
250 goto end;
251
252 error:
253 BT_OBJECT_PUT_REF_AND_RESET(plugin);
254
255 end:
256 return plugin;
257 }
258
259 static inline
260 void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
261 {
262 BT_ASSERT(plugin);
263 BT_ASSERT(path);
264 g_string_assign(plugin->info.path, path);
265 plugin->info.path_set = BT_TRUE;
266 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, path=\"%s\"",
267 plugin, path);
268 }
269
270 static inline
271 void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
272 {
273 BT_ASSERT(plugin);
274 BT_ASSERT(name);
275 g_string_assign(plugin->info.name, name);
276 plugin->info.name_set = BT_TRUE;
277 BT_LIB_LOGD("Set plugin's name: %![plugin-]+l, name=\"%s\"",
278 plugin, name);
279 }
280
281 static inline
282 void bt_plugin_set_description(struct bt_plugin *plugin,
283 const char *description)
284 {
285 BT_ASSERT(plugin);
286 BT_ASSERT(description);
287 g_string_assign(plugin->info.description, description);
288 plugin->info.description_set = BT_TRUE;
289 BT_LIB_LOGD("Set plugin's description: %![plugin-]+l", plugin);
290 }
291
292 static inline
293 void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
294 {
295 BT_ASSERT(plugin);
296 BT_ASSERT(author);
297 g_string_assign(plugin->info.author, author);
298 plugin->info.author_set = BT_TRUE;
299 BT_LIB_LOGD("Set plugin's author: %![plugin-]+l, author=\"%s\"",
300 plugin, author);
301 }
302
303 static inline
304 void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
305 {
306 BT_ASSERT(plugin);
307 BT_ASSERT(license);
308 g_string_assign(plugin->info.license, license);
309 plugin->info.license_set = BT_TRUE;
310 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, license=\"%s\"",
311 plugin, license);
312 }
313
314 static inline
315 void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
316 unsigned int minor, unsigned int patch, const char *extra)
317 {
318 BT_ASSERT(plugin);
319 plugin->info.version.major = major;
320 plugin->info.version.minor = minor;
321 plugin->info.version.patch = patch;
322
323 if (extra) {
324 g_string_assign(plugin->info.version.extra, extra);
325 }
326
327 plugin->info.version_set = BT_TRUE;
328 BT_LIB_LOGD("Set plugin's version: %![plugin-]+l, "
329 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
330 plugin, major, minor, patch, extra);
331 }
332
333 static inline
334 int bt_plugin_add_component_class(
335 struct bt_plugin *plugin, struct bt_component_class *comp_class)
336 {
337 GPtrArray *comp_classes;
338
339 BT_ASSERT(plugin);
340 BT_ASSERT(comp_class);
341
342 switch (comp_class->type) {
343 case BT_COMPONENT_CLASS_TYPE_SOURCE:
344 comp_classes = plugin->src_comp_classes;
345 break;
346 case BT_COMPONENT_CLASS_TYPE_FILTER:
347 comp_classes = plugin->flt_comp_classes;
348 break;
349 case BT_COMPONENT_CLASS_TYPE_SINK:
350 comp_classes = plugin->sink_comp_classes;
351 break;
352 default:
353 bt_common_abort();
354 }
355
356 /* Set component class's original plugin name */
357 BT_ASSERT(comp_class->plugin_name);
358 BT_ASSERT(plugin->info.name);
359 g_string_assign(comp_class->plugin_name, plugin->info.name->str);
360
361 /* Add new component class */
362 bt_object_get_ref(comp_class);
363 g_ptr_array_add(comp_classes, comp_class);
364
365 /* Special case for a shared object plugin */
366 if (plugin->type == BT_PLUGIN_TYPE_SO) {
367 bt_plugin_so_on_add_component_class(plugin, comp_class);
368 }
369
370 BT_LIB_LOGD("Added component class to plugin: "
371 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
372 return BT_FUNC_STATUS_OK;
373 }
374
375 static
376 void bt_plugin_set_destroy(struct bt_object *obj)
377 {
378 struct bt_plugin_set *plugin_set =
379 container_of(obj, struct bt_plugin_set, base);
380
381 if (!plugin_set) {
382 return;
383 }
384
385 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
386
387 if (plugin_set->plugins) {
388 BT_LOGD_STR("Putting plugins.");
389 g_ptr_array_free(plugin_set->plugins, TRUE);
390 }
391
392 g_free(plugin_set);
393 }
394
395 static inline
396 struct bt_plugin_set *bt_plugin_set_create(void)
397 {
398 struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1);
399
400 if (!plugin_set) {
401 goto end;
402 }
403
404 BT_LOGD_STR("Creating empty plugin set.");
405 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
406
407 plugin_set->plugins = g_ptr_array_new_with_free_func(
408 (GDestroyNotify) bt_object_put_ref);
409 if (!plugin_set->plugins) {
410 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
411 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
412 goto end;
413 }
414
415 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
416
417 end:
418 return plugin_set;
419 }
420
421 static inline
422 bool bt_plugin_set_contains_plugin(struct bt_plugin_set *plugin_set,
423 const char *name)
424 {
425 uint64_t i;
426 bool contains = false;
427
428 BT_ASSERT(plugin_set);
429 BT_ASSERT(name);
430
431 for (i = 0; i < plugin_set->plugins->len; i++) {
432 const struct bt_plugin *plugin = plugin_set->plugins->pdata[i];
433
434 if (strcmp(plugin->info.name->str, name) == 0) {
435 contains = true;
436 goto end;
437 }
438 }
439
440 end:
441 return contains;
442 }
443
444 static inline
445 void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
446 struct bt_plugin *plugin)
447 {
448 BT_ASSERT(plugin_set);
449 BT_ASSERT(plugin);
450
451 if (bt_plugin_set_contains_plugin(plugin_set,
452 plugin->info.name->str)) {
453 goto end;
454 }
455
456 bt_object_get_ref(plugin);
457 g_ptr_array_add(plugin_set->plugins, plugin);
458 BT_LIB_LOGD("Added plugin to plugin set: "
459 "plugin-set-addr=%p, %![plugin-]+l",
460 plugin_set, plugin);
461
462 end:
463 return;
464 }
465
466 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.040257 seconds and 3 git commands to generate.