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/macros.h"
28 #include "common/common.h"
29 #include "lib/graph/component-class.h"
30 #include <babeltrace2/plugin/plugin-const.h>
31 #include <babeltrace2/plugin/plugin-dev.h>
32 #include "lib/object.h"
33 #include <babeltrace2/types.h>
34 #include "common/assert.h"
38 #include "plugin-so.h"
39 #include "lib/func-status.h"
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.
47 BT_PLUGIN_TYPE_SO
= 0,
48 BT_PLUGIN_TYPE_PYTHON
= 1,
52 struct bt_object base
;
53 enum bt_plugin_type type
;
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
;
60 /* Info (owned by this) */
81 /* Value depends on the specific plugin type */
83 void (*destroy_spec_data
)(struct bt_plugin
*);
86 struct bt_plugin_set
{
87 struct bt_object base
;
89 /* Array of struct bt_plugin * */
94 const char *bt_plugin_type_string(enum bt_plugin_type type
)
97 case BT_PLUGIN_TYPE_SO
:
99 case BT_PLUGIN_TYPE_PYTHON
:
107 void bt_plugin_destroy(struct bt_object
*obj
)
109 struct bt_plugin
*plugin
;
112 plugin
= container_of(obj
, struct bt_plugin
, base
);
113 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin
);
115 if (plugin
->destroy_spec_data
) {
116 plugin
->destroy_spec_data(plugin
);
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
;
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
;
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
;
137 if (plugin
->info
.name
) {
138 g_string_free(plugin
->info
.name
, TRUE
);
139 plugin
->info
.name
= NULL
;
142 if (plugin
->info
.path
) {
143 g_string_free(plugin
->info
.path
, TRUE
);
144 plugin
->info
.path
= NULL
;
147 if (plugin
->info
.description
) {
148 g_string_free(plugin
->info
.description
, TRUE
);
149 plugin
->info
.description
= NULL
;
152 if (plugin
->info
.author
) {
153 g_string_free(plugin
->info
.author
, TRUE
);
154 plugin
->info
.author
= NULL
;
157 if (plugin
->info
.license
) {
158 g_string_free(plugin
->info
.license
, TRUE
);
159 plugin
->info
.license
= NULL
;
162 if (plugin
->info
.version
.extra
) {
163 g_string_free(plugin
->info
.version
.extra
, TRUE
);
164 plugin
->info
.version
.extra
= NULL
;
171 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
173 struct bt_plugin
*plugin
= NULL
;
175 BT_LOGD("Creating empty plugin object: type=%s",
176 bt_plugin_type_string(type
));
178 plugin
= g_new0(struct bt_plugin
, 1);
180 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one plugin.");
184 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
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.");
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.");
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.");
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.");
219 plugin
->info
.path
= g_string_new(NULL
);
220 if (!plugin
->info
.path
) {
221 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
225 plugin
->info
.description
= g_string_new(NULL
);
226 if (!plugin
->info
.description
) {
227 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
231 plugin
->info
.author
= g_string_new(NULL
);
232 if (!plugin
->info
.author
) {
233 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
237 plugin
->info
.license
= g_string_new(NULL
);
238 if (!plugin
->info
.license
) {
239 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
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.");
249 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
253 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
260 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *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\"",
271 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *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\"",
282 void bt_plugin_set_description(struct bt_plugin
*plugin
,
283 const char *description
)
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
);
293 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *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\"",
304 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *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\"",
315 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
316 unsigned int minor
, unsigned int patch
, const char *extra
)
319 plugin
->info
.version
.major
= major
;
320 plugin
->info
.version
.minor
= minor
;
321 plugin
->info
.version
.patch
= patch
;
324 g_string_assign(plugin
->info
.version
.extra
, extra
);
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
);
334 int bt_plugin_add_component_class(
335 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
337 GPtrArray
*comp_classes
;
340 BT_ASSERT(comp_class
);
342 switch (comp_class
->type
) {
343 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
344 comp_classes
= plugin
->src_comp_classes
;
346 case BT_COMPONENT_CLASS_TYPE_FILTER
:
347 comp_classes
= plugin
->flt_comp_classes
;
349 case BT_COMPONENT_CLASS_TYPE_SINK
:
350 comp_classes
= plugin
->sink_comp_classes
;
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
);
361 /* Add new component class */
362 bt_object_get_ref(comp_class
);
363 g_ptr_array_add(comp_classes
, comp_class
);
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
);
370 BT_LIB_LOGD("Added component class to plugin: "
371 "%![plugin-]+l, %![cc-]+C", plugin
, comp_class
);
372 return BT_FUNC_STATUS_OK
;
376 void bt_plugin_set_destroy(struct bt_object
*obj
)
378 struct bt_plugin_set
*plugin_set
=
379 container_of(obj
, struct bt_plugin_set
, base
);
385 BT_LOGD("Destroying plugin set: addr=%p", plugin_set
);
387 if (plugin_set
->plugins
) {
388 BT_LOGD_STR("Putting plugins.");
389 g_ptr_array_free(plugin_set
->plugins
, TRUE
);
396 struct bt_plugin_set
*bt_plugin_set_create(void)
398 struct bt_plugin_set
*plugin_set
= g_new0(struct bt_plugin_set
, 1);
404 BT_LOGD_STR("Creating empty plugin set.");
405 bt_object_init_shared(&plugin_set
->base
, bt_plugin_set_destroy
);
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
);
415 BT_LOGD("Created empty plugin set: addr=%p", plugin_set
);
422 bool bt_plugin_set_contains_plugin(struct bt_plugin_set
*plugin_set
,
426 bool contains
= false;
428 BT_ASSERT(plugin_set
);
431 for (i
= 0; i
< plugin_set
->plugins
->len
; i
++) {
432 const struct bt_plugin
*plugin
= plugin_set
->plugins
->pdata
[i
];
434 if (strcmp(plugin
->info
.name
->str
, name
) == 0) {
445 void bt_plugin_set_add_plugin(struct bt_plugin_set
*plugin_set
,
446 struct bt_plugin
*plugin
)
448 BT_ASSERT(plugin_set
);
451 if (bt_plugin_set_contains_plugin(plugin_set
,
452 plugin
->info
.name
->str
)) {
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",
466 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */