1 #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2 #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #include "common/babeltrace.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 "lib/lib-logging.h"
37 #include "plugin-so.h"
40 BT_PLUGIN_TYPE_SO
= 0,
41 BT_PLUGIN_TYPE_PYTHON
= 1,
44 enum bt_plugin_status
{
45 BT_PLUGIN_STATUS_OK
= 0,
46 BT_PLUGIN_STATUS_ERROR
= -1,
47 BT_PLUGIN_STATUS_NOMEM
= -12,
51 struct bt_object base
;
52 enum bt_plugin_type type
;
54 /* Arrays of `struct bt_component_class *` (owned by this) */
55 GPtrArray
*src_comp_classes
;
56 GPtrArray
*flt_comp_classes
;
57 GPtrArray
*sink_comp_classes
;
59 /* Info (owned by this) */
80 /* Value depends on the specific plugin type */
82 void (*destroy_spec_data
)(struct bt_plugin
*);
85 struct bt_plugin_set
{
86 struct bt_object base
;
88 /* Array of struct bt_plugin * */
93 const char *bt_plugin_status_string(enum bt_plugin_status status
)
96 case BT_PLUGIN_STATUS_OK
:
97 return "BT_PLUGIN_STATUS_OK";
98 case BT_PLUGIN_STATUS_ERROR
:
99 return "BT_PLUGIN_STATUS_ERROR";
100 case BT_PLUGIN_STATUS_NOMEM
:
101 return "BT_PLUGIN_STATUS_NOMEM";
108 const char *bt_plugin_type_string(enum bt_plugin_type type
)
111 case BT_PLUGIN_TYPE_SO
:
112 return "BT_PLUGIN_TYPE_SO";
113 case BT_PLUGIN_TYPE_PYTHON
:
114 return "BT_PLUGIN_TYPE_PYTHON";
121 void bt_plugin_destroy(struct bt_object
*obj
)
123 struct bt_plugin
*plugin
;
126 plugin
= container_of(obj
, struct bt_plugin
, base
);
127 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin
);
129 if (plugin
->destroy_spec_data
) {
130 plugin
->destroy_spec_data(plugin
);
133 if (plugin
->src_comp_classes
) {
134 BT_LOGD_STR("Putting source component classes.");
135 g_ptr_array_free(plugin
->src_comp_classes
, TRUE
);
136 plugin
->src_comp_classes
= NULL
;
139 if (plugin
->flt_comp_classes
) {
140 BT_LOGD_STR("Putting filter component classes.");
141 g_ptr_array_free(plugin
->flt_comp_classes
, TRUE
);
142 plugin
->flt_comp_classes
= NULL
;
145 if (plugin
->sink_comp_classes
) {
146 BT_LOGD_STR("Putting sink component classes.");
147 g_ptr_array_free(plugin
->sink_comp_classes
, TRUE
);
148 plugin
->sink_comp_classes
= NULL
;
151 if (plugin
->info
.name
) {
152 g_string_free(plugin
->info
.name
, TRUE
);
153 plugin
->info
.name
= NULL
;
156 if (plugin
->info
.path
) {
157 g_string_free(plugin
->info
.path
, TRUE
);
158 plugin
->info
.path
= NULL
;
161 if (plugin
->info
.description
) {
162 g_string_free(plugin
->info
.description
, TRUE
);
163 plugin
->info
.description
= NULL
;
166 if (plugin
->info
.author
) {
167 g_string_free(plugin
->info
.author
, TRUE
);
168 plugin
->info
.author
= NULL
;
171 if (plugin
->info
.license
) {
172 g_string_free(plugin
->info
.license
, TRUE
);
173 plugin
->info
.license
= NULL
;
176 if (plugin
->info
.version
.extra
) {
177 g_string_free(plugin
->info
.version
.extra
, TRUE
);
178 plugin
->info
.version
.extra
= NULL
;
185 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
187 struct bt_plugin
*plugin
= NULL
;
189 BT_LOGD("Creating empty plugin object: type=%s",
190 bt_plugin_type_string(type
));
192 plugin
= g_new0(struct bt_plugin
, 1);
194 BT_LOGE_STR("Failed to allocate one plugin.");
198 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
201 /* Create empty arrays of component classes */
202 plugin
->src_comp_classes
=
203 g_ptr_array_new_with_free_func(
204 (GDestroyNotify
) bt_object_put_ref
);
205 if (!plugin
->src_comp_classes
) {
206 BT_LOGE_STR("Failed to allocate a GPtrArray.");
210 plugin
->flt_comp_classes
=
211 g_ptr_array_new_with_free_func(
212 (GDestroyNotify
) bt_object_put_ref
);
213 if (!plugin
->flt_comp_classes
) {
214 BT_LOGE_STR("Failed to allocate a GPtrArray.");
218 plugin
->sink_comp_classes
=
219 g_ptr_array_new_with_free_func(
220 (GDestroyNotify
) bt_object_put_ref
);
221 if (!plugin
->sink_comp_classes
) {
222 BT_LOGE_STR("Failed to allocate a GPtrArray.");
226 /* Create empty info */
227 plugin
->info
.name
= g_string_new(NULL
);
228 if (!plugin
->info
.name
) {
229 BT_LOGE_STR("Failed to allocate a GString.");
233 plugin
->info
.path
= g_string_new(NULL
);
234 if (!plugin
->info
.path
) {
235 BT_LOGE_STR("Failed to allocate a GString.");
239 plugin
->info
.description
= g_string_new(NULL
);
240 if (!plugin
->info
.description
) {
241 BT_LOGE_STR("Failed to allocate a GString.");
245 plugin
->info
.author
= g_string_new(NULL
);
246 if (!plugin
->info
.author
) {
247 BT_LOGE_STR("Failed to allocate a GString.");
251 plugin
->info
.license
= g_string_new(NULL
);
252 if (!plugin
->info
.license
) {
253 BT_LOGE_STR("Failed to allocate a GString.");
257 plugin
->info
.version
.extra
= g_string_new(NULL
);
258 if (!plugin
->info
.version
.extra
) {
259 BT_LOGE_STR("Failed to allocate a GString.");
263 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
267 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
274 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *path
)
278 g_string_assign(plugin
->info
.path
, path
);
279 plugin
->info
.path_set
= BT_TRUE
;
280 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
285 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *name
)
289 g_string_assign(plugin
->info
.name
, name
);
290 plugin
->info
.name_set
= BT_TRUE
;
291 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
296 void bt_plugin_set_description(struct bt_plugin
*plugin
,
297 const char *description
)
300 BT_ASSERT(description
);
301 g_string_assign(plugin
->info
.description
, description
);
302 plugin
->info
.description_set
= BT_TRUE
;
303 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin
);
307 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *author
)
311 g_string_assign(plugin
->info
.author
, author
);
312 plugin
->info
.author_set
= BT_TRUE
;
313 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
318 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *license
)
322 g_string_assign(plugin
->info
.license
, license
);
323 plugin
->info
.license_set
= BT_TRUE
;
324 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
329 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
330 unsigned int minor
, unsigned int patch
, const char *extra
)
333 plugin
->info
.version
.major
= major
;
334 plugin
->info
.version
.minor
= minor
;
335 plugin
->info
.version
.patch
= patch
;
338 g_string_assign(plugin
->info
.version
.extra
, extra
);
341 plugin
->info
.version_set
= BT_TRUE
;
342 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
343 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
344 plugin
, major
, minor
, patch
, extra
);
348 enum bt_plugin_status
bt_plugin_add_component_class(
349 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
351 GPtrArray
*comp_classes
;
354 BT_ASSERT(comp_class
);
356 switch (comp_class
->type
) {
357 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
358 comp_classes
= plugin
->src_comp_classes
;
360 case BT_COMPONENT_CLASS_TYPE_FILTER
:
361 comp_classes
= plugin
->flt_comp_classes
;
363 case BT_COMPONENT_CLASS_TYPE_SINK
:
364 comp_classes
= plugin
->sink_comp_classes
;
370 /* Add new component class */
371 bt_object_get_ref(comp_class
);
372 g_ptr_array_add(comp_classes
, comp_class
);
374 /* Special case for a shared object plugin */
375 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
376 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
379 BT_LIB_LOGD("Added component class to plugin: "
380 "%![plugin-]+l, %![cc-]+C", plugin
, comp_class
);
381 return BT_PLUGIN_STATUS_OK
;
385 void bt_plugin_set_destroy(struct bt_object
*obj
)
387 struct bt_plugin_set
*plugin_set
=
388 container_of(obj
, struct bt_plugin_set
, base
);
394 BT_LOGD("Destroying plugin set: addr=%p", plugin_set
);
396 if (plugin_set
->plugins
) {
397 BT_LOGD_STR("Putting plugins.");
398 g_ptr_array_free(plugin_set
->plugins
, TRUE
);
405 struct bt_plugin_set
*bt_plugin_set_create(void)
407 struct bt_plugin_set
*plugin_set
= g_new0(struct bt_plugin_set
, 1);
413 BT_LOGD_STR("Creating empty plugin set.");
414 bt_object_init_shared(&plugin_set
->base
, bt_plugin_set_destroy
);
416 plugin_set
->plugins
= g_ptr_array_new_with_free_func(
417 (GDestroyNotify
) bt_object_put_ref
);
418 if (!plugin_set
->plugins
) {
419 BT_LOGE_STR("Failed to allocate a GPtrArray.");
420 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
424 BT_LOGD("Created empty plugin set: addr=%p", plugin_set
);
431 void bt_plugin_set_add_plugin(struct bt_plugin_set
*plugin_set
,
432 struct bt_plugin
*plugin
)
434 BT_ASSERT(plugin_set
);
436 bt_object_get_ref(plugin
);
437 g_ptr_array_add(plugin_set
->plugins
, plugin
);
438 BT_LIB_LOGV("Added plugin to plugin set: "
439 "plugin-set-addr=%p, %![plugin-]+l",
443 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */