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