46b2b117be7b9b089db11687f2fad155c4c88b0f
[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 "lib/graph/component-class.h"
29 #include <babeltrace2/plugin/plugin-const.h>
30 #include <babeltrace2/plugin/plugin-dev.h>
31 #include "lib/object.h"
32 #include <babeltrace2/types.h>
33 #include "common/assert.h"
34 #include <glib.h>
35
36 #include "plugin-so.h"
37
38 /* Protection: this file uses BT_LIB_LOG*() macros directly */
39 #ifndef BT_LIB_LOG_SUPPORTED
40 # error Please include "lib/logging.h" before including this file.
41 #endif
42
43 enum bt_plugin_type {
44 BT_PLUGIN_TYPE_SO = 0,
45 BT_PLUGIN_TYPE_PYTHON = 1,
46 };
47
48 struct bt_plugin {
49 struct bt_object base;
50 enum bt_plugin_type type;
51
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;
56
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;
70 bool path_set;
71 bool name_set;
72 bool author_set;
73 bool license_set;
74 bool description_set;
75 bool version_set;
76 } info;
77
78 /* Value depends on the specific plugin type */
79 void *spec_data;
80 void (*destroy_spec_data)(struct bt_plugin *);
81 };
82
83 struct bt_plugin_set {
84 struct bt_object base;
85
86 /* Array of struct bt_plugin * */
87 GPtrArray *plugins;
88 };
89
90 static inline
91 const 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";
96 case BT_PLUGIN_STATUS_NOT_FOUND:
97 return "BT_PLUGIN_STATUS_NOT_FOUND";
98 case BT_PLUGIN_STATUS_ERROR:
99 return "BT_PLUGIN_STATUS_ERROR";
100 case BT_PLUGIN_STATUS_LOADING_ERROR:
101 return "BT_PLUGIN_STATUS_LOADING_ERROR";
102 case BT_PLUGIN_STATUS_NOMEM:
103 return "BT_PLUGIN_STATUS_NOMEM";
104 default:
105 return "(unknown)";
106 }
107 }
108
109 static inline
110 const 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
122 static inline
123 void bt_plugin_destroy(struct bt_object *obj)
124 {
125 struct bt_plugin *plugin;
126
127 BT_ASSERT(obj);
128 plugin = container_of(obj, struct bt_plugin, base);
129 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin);
130
131 if (plugin->destroy_spec_data) {
132 plugin->destroy_spec_data(plugin);
133 }
134
135 if (plugin->src_comp_classes) {
136 BT_LOGD_STR("Putting source component classes.");
137 g_ptr_array_free(plugin->src_comp_classes, TRUE);
138 plugin->src_comp_classes = NULL;
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);
144 plugin->flt_comp_classes = NULL;
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);
150 plugin->sink_comp_classes = NULL;
151 }
152
153 if (plugin->info.name) {
154 g_string_free(plugin->info.name, TRUE);
155 plugin->info.name = NULL;
156 }
157
158 if (plugin->info.path) {
159 g_string_free(plugin->info.path, TRUE);
160 plugin->info.path = NULL;
161 }
162
163 if (plugin->info.description) {
164 g_string_free(plugin->info.description, TRUE);
165 plugin->info.description = NULL;
166 }
167
168 if (plugin->info.author) {
169 g_string_free(plugin->info.author, TRUE);
170 plugin->info.author = NULL;
171 }
172
173 if (plugin->info.license) {
174 g_string_free(plugin->info.license, TRUE);
175 plugin->info.license = NULL;
176 }
177
178 if (plugin->info.version.extra) {
179 g_string_free(plugin->info.version.extra, TRUE);
180 plugin->info.version.extra = NULL;
181 }
182
183 g_free(plugin);
184 }
185
186 static inline
187 struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
188 {
189 struct bt_plugin *plugin = NULL;
190
191 BT_LOGD("Creating empty plugin object: type=%s",
192 bt_plugin_type_string(type));
193
194 plugin = g_new0(struct bt_plugin, 1);
195 if (!plugin) {
196 BT_LOGE_STR("Failed to allocate one plugin.");
197 goto error;
198 }
199
200 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
201 plugin->type = type;
202
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) {
224 BT_LOGE_STR("Failed to allocate a GPtrArray.");
225 goto error;
226 }
227
228 /* Create empty info */
229 plugin->info.name = g_string_new(NULL);
230 if (!plugin->info.name) {
231 BT_LOGE_STR("Failed to allocate a GString.");
232 goto error;
233 }
234
235 plugin->info.path = g_string_new(NULL);
236 if (!plugin->info.path) {
237 BT_LOGE_STR("Failed to allocate a GString.");
238 goto error;
239 }
240
241 plugin->info.description = g_string_new(NULL);
242 if (!plugin->info.description) {
243 BT_LOGE_STR("Failed to allocate a GString.");
244 goto error;
245 }
246
247 plugin->info.author = g_string_new(NULL);
248 if (!plugin->info.author) {
249 BT_LOGE_STR("Failed to allocate a GString.");
250 goto error;
251 }
252
253 plugin->info.license = g_string_new(NULL);
254 if (!plugin->info.license) {
255 BT_LOGE_STR("Failed to allocate a GString.");
256 goto error;
257 }
258
259 plugin->info.version.extra = g_string_new(NULL);
260 if (!plugin->info.version.extra) {
261 BT_LOGE_STR("Failed to allocate a GString.");
262 goto error;
263 }
264
265 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
266 goto end;
267
268 error:
269 BT_OBJECT_PUT_REF_AND_RESET(plugin);
270
271 end:
272 return plugin;
273 }
274
275 static inline
276 void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
277 {
278 BT_ASSERT(plugin);
279 BT_ASSERT(path);
280 g_string_assign(plugin->info.path, path);
281 plugin->info.path_set = BT_TRUE;
282 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, path=\"%s\"",
283 plugin, path);
284 }
285
286 static inline
287 void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
288 {
289 BT_ASSERT(plugin);
290 BT_ASSERT(name);
291 g_string_assign(plugin->info.name, name);
292 plugin->info.name_set = BT_TRUE;
293 BT_LIB_LOGD("Set plugin's name: %![plugin-]+l, name=\"%s\"",
294 plugin, name);
295 }
296
297 static inline
298 void bt_plugin_set_description(struct bt_plugin *plugin,
299 const char *description)
300 {
301 BT_ASSERT(plugin);
302 BT_ASSERT(description);
303 g_string_assign(plugin->info.description, description);
304 plugin->info.description_set = BT_TRUE;
305 BT_LIB_LOGD("Set plugin's description: %![plugin-]+l", plugin);
306 }
307
308 static inline
309 void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
310 {
311 BT_ASSERT(plugin);
312 BT_ASSERT(author);
313 g_string_assign(plugin->info.author, author);
314 plugin->info.author_set = BT_TRUE;
315 BT_LIB_LOGD("Set plugin's author: %![plugin-]+l, author=\"%s\"",
316 plugin, author);
317 }
318
319 static inline
320 void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
321 {
322 BT_ASSERT(plugin);
323 BT_ASSERT(license);
324 g_string_assign(plugin->info.license, license);
325 plugin->info.license_set = BT_TRUE;
326 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, license=\"%s\"",
327 plugin, license);
328 }
329
330 static inline
331 void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
332 unsigned int minor, unsigned int patch, const char *extra)
333 {
334 BT_ASSERT(plugin);
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
343 plugin->info.version_set = BT_TRUE;
344 BT_LIB_LOGD("Set plugin's version: %![plugin-]+l, "
345 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
346 plugin, major, minor, patch, extra);
347 }
348
349 static inline
350 enum bt_plugin_status bt_plugin_add_component_class(
351 struct bt_plugin *plugin, struct bt_component_class *comp_class)
352 {
353 GPtrArray *comp_classes;
354
355 BT_ASSERT(plugin);
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 }
371
372 /* Add new component class */
373 bt_object_get_ref(comp_class);
374 g_ptr_array_add(comp_classes, comp_class);
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);
379 }
380
381 BT_LIB_LOGD("Added component class to plugin: "
382 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
383 return BT_PLUGIN_STATUS_OK;
384 }
385
386 static
387 void 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
396 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
397
398 if (plugin_set->plugins) {
399 BT_LOGD_STR("Putting plugins.");
400 g_ptr_array_free(plugin_set->plugins, TRUE);
401 }
402
403 g_free(plugin_set);
404 }
405
406 static inline
407 struct 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
415 BT_LOGD_STR("Creating empty plugin set.");
416 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
417
418 plugin_set->plugins = g_ptr_array_new_with_free_func(
419 (GDestroyNotify) bt_object_put_ref);
420 if (!plugin_set->plugins) {
421 BT_LOGE_STR("Failed to allocate a GPtrArray.");
422 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
423 goto end;
424 }
425
426 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
427
428 end:
429 return plugin_set;
430 }
431
432 static inline
433 void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
434 struct bt_plugin *plugin)
435 {
436 BT_ASSERT(plugin_set);
437 BT_ASSERT(plugin);
438 bt_object_get_ref(plugin);
439 g_ptr_array_add(plugin_set->plugins, plugin);
440 BT_LIB_LOGD("Added plugin to plugin set: "
441 "plugin-set-addr=%p, %![plugin-]+l",
442 plugin_set, plugin);
443 }
444
445 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.03699 seconds and 3 git commands to generate.