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 "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"
36 #include "plugin-so.h"
37 #include "lib/func-status.h"
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.
45 BT_PLUGIN_TYPE_SO
= 0,
46 BT_PLUGIN_TYPE_PYTHON
= 1,
50 struct bt_object base
;
51 enum bt_plugin_type type
;
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
;
58 /* Info (owned by this) */
79 /* Value depends on the specific plugin type */
81 void (*destroy_spec_data
)(struct bt_plugin
*);
84 struct bt_plugin_set
{
85 struct bt_object base
;
87 /* Array of struct bt_plugin * */
92 const char *bt_plugin_type_string(enum bt_plugin_type 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";
105 void bt_plugin_destroy(struct bt_object
*obj
)
107 struct bt_plugin
*plugin
;
110 plugin
= container_of(obj
, struct bt_plugin
, base
);
111 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin
);
113 if (plugin
->destroy_spec_data
) {
114 plugin
->destroy_spec_data(plugin
);
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
;
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
;
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
;
135 if (plugin
->info
.name
) {
136 g_string_free(plugin
->info
.name
, TRUE
);
137 plugin
->info
.name
= NULL
;
140 if (plugin
->info
.path
) {
141 g_string_free(plugin
->info
.path
, TRUE
);
142 plugin
->info
.path
= NULL
;
145 if (plugin
->info
.description
) {
146 g_string_free(plugin
->info
.description
, TRUE
);
147 plugin
->info
.description
= NULL
;
150 if (plugin
->info
.author
) {
151 g_string_free(plugin
->info
.author
, TRUE
);
152 plugin
->info
.author
= NULL
;
155 if (plugin
->info
.license
) {
156 g_string_free(plugin
->info
.license
, TRUE
);
157 plugin
->info
.license
= NULL
;
160 if (plugin
->info
.version
.extra
) {
161 g_string_free(plugin
->info
.version
.extra
, TRUE
);
162 plugin
->info
.version
.extra
= NULL
;
169 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
171 struct bt_plugin
*plugin
= NULL
;
173 BT_LOGD("Creating empty plugin object: type=%s",
174 bt_plugin_type_string(type
));
176 plugin
= g_new0(struct bt_plugin
, 1);
178 BT_LOGE_STR("Failed to allocate one plugin.");
182 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
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_LOGE_STR("Failed to allocate a GPtrArray.");
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_LOGE_STR("Failed to allocate a GPtrArray.");
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_LOGE_STR("Failed to allocate a GPtrArray.");
210 /* Create empty info */
211 plugin
->info
.name
= g_string_new(NULL
);
212 if (!plugin
->info
.name
) {
213 BT_LOGE_STR("Failed to allocate a GString.");
217 plugin
->info
.path
= g_string_new(NULL
);
218 if (!plugin
->info
.path
) {
219 BT_LOGE_STR("Failed to allocate a GString.");
223 plugin
->info
.description
= g_string_new(NULL
);
224 if (!plugin
->info
.description
) {
225 BT_LOGE_STR("Failed to allocate a GString.");
229 plugin
->info
.author
= g_string_new(NULL
);
230 if (!plugin
->info
.author
) {
231 BT_LOGE_STR("Failed to allocate a GString.");
235 plugin
->info
.license
= g_string_new(NULL
);
236 if (!plugin
->info
.license
) {
237 BT_LOGE_STR("Failed to allocate a GString.");
241 plugin
->info
.version
.extra
= g_string_new(NULL
);
242 if (!plugin
->info
.version
.extra
) {
243 BT_LOGE_STR("Failed to allocate a GString.");
247 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
251 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
258 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *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\"",
269 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *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\"",
280 void bt_plugin_set_description(struct bt_plugin
*plugin
,
281 const char *description
)
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
);
291 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *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\"",
302 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *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\"",
313 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
314 unsigned int minor
, unsigned int patch
, const char *extra
)
317 plugin
->info
.version
.major
= major
;
318 plugin
->info
.version
.minor
= minor
;
319 plugin
->info
.version
.patch
= patch
;
322 g_string_assign(plugin
->info
.version
.extra
, extra
);
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
);
332 int bt_plugin_add_component_class(
333 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
335 GPtrArray
*comp_classes
;
338 BT_ASSERT(comp_class
);
340 switch (comp_class
->type
) {
341 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
342 comp_classes
= plugin
->src_comp_classes
;
344 case BT_COMPONENT_CLASS_TYPE_FILTER
:
345 comp_classes
= plugin
->flt_comp_classes
;
347 case BT_COMPONENT_CLASS_TYPE_SINK
:
348 comp_classes
= plugin
->sink_comp_classes
;
354 /* Add new component class */
355 bt_object_get_ref(comp_class
);
356 g_ptr_array_add(comp_classes
, comp_class
);
358 /* Special case for a shared object plugin */
359 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
360 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
363 BT_LIB_LOGD("Added component class to plugin: "
364 "%![plugin-]+l, %![cc-]+C", plugin
, comp_class
);
365 return BT_FUNC_STATUS_OK
;
369 void bt_plugin_set_destroy(struct bt_object
*obj
)
371 struct bt_plugin_set
*plugin_set
=
372 container_of(obj
, struct bt_plugin_set
, base
);
378 BT_LOGD("Destroying plugin set: addr=%p", plugin_set
);
380 if (plugin_set
->plugins
) {
381 BT_LOGD_STR("Putting plugins.");
382 g_ptr_array_free(plugin_set
->plugins
, TRUE
);
389 struct bt_plugin_set
*bt_plugin_set_create(void)
391 struct bt_plugin_set
*plugin_set
= g_new0(struct bt_plugin_set
, 1);
397 BT_LOGD_STR("Creating empty plugin set.");
398 bt_object_init_shared(&plugin_set
->base
, bt_plugin_set_destroy
);
400 plugin_set
->plugins
= g_ptr_array_new_with_free_func(
401 (GDestroyNotify
) bt_object_put_ref
);
402 if (!plugin_set
->plugins
) {
403 BT_LOGE_STR("Failed to allocate a GPtrArray.");
404 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
408 BT_LOGD("Created empty plugin set: addr=%p", plugin_set
);
415 void bt_plugin_set_add_plugin(struct bt_plugin_set
*plugin_set
,
416 struct bt_plugin
*plugin
)
418 BT_ASSERT(plugin_set
);
420 bt_object_get_ref(plugin
);
421 g_ptr_array_add(plugin_set
->plugins
, plugin
);
422 BT_LIB_LOGD("Added plugin to plugin set: "
423 "plugin-set-addr=%p, %![plugin-]+l",
427 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */