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