1 #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2 #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <babeltrace/babeltrace-internal.h>
29 #include <babeltrace/graph/component-class-internal.h>
30 #include <babeltrace/plugin/plugin.h>
31 #include <babeltrace/plugin/plugin-dev.h>
32 #include <babeltrace/plugin/plugin-so-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/object.h>
35 #include <babeltrace/types.h>
36 #include <babeltrace/assert-internal.h>
40 BT_PLUGIN_TYPE_SO
= 0,
41 BT_PLUGIN_TYPE_PYTHON
= 1,
45 struct bt_object base
;
46 enum bt_plugin_type type
;
48 /* Arrays of `struct bt_component_class *` (owned by this) */
49 GPtrArray
*src_comp_classes
;
50 GPtrArray
*flt_comp_classes
;
51 GPtrArray
*sink_comp_classes
;
53 /* Info (owned by this) */
74 /* Value depends on the specific plugin type */
76 void (*destroy_spec_data
)(struct bt_plugin
*);
79 struct bt_plugin_set
{
80 struct bt_object base
;
82 /* Array of struct bt_plugin * */
87 const char *bt_plugin_status_string(enum bt_plugin_status status
)
90 case BT_PLUGIN_STATUS_OK
:
91 return "BT_PLUGIN_STATUS_OK";
92 case BT_PLUGIN_STATUS_ERROR
:
93 return "BT_PLUGIN_STATUS_ERROR";
94 case BT_PLUGIN_STATUS_NOMEM
:
95 return "BT_PLUGIN_STATUS_NOMEM";
102 const char *bt_plugin_type_string(enum bt_plugin_type type
)
105 case BT_PLUGIN_TYPE_SO
:
106 return "BT_PLUGIN_TYPE_SO";
107 case BT_PLUGIN_TYPE_PYTHON
:
108 return "BT_PLUGIN_TYPE_PYTHON";
115 void bt_plugin_destroy(struct bt_object
*obj
)
117 struct bt_plugin
*plugin
;
120 plugin
= container_of(obj
, struct bt_plugin
, base
);
121 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin
);
123 if (plugin
->destroy_spec_data
) {
124 plugin
->destroy_spec_data(plugin
);
127 if (plugin
->src_comp_classes
) {
128 BT_LOGD_STR("Putting source component classes.");
129 g_ptr_array_free(plugin
->src_comp_classes
, TRUE
);
130 plugin
->src_comp_classes
= NULL
;
133 if (plugin
->flt_comp_classes
) {
134 BT_LOGD_STR("Putting filter component classes.");
135 g_ptr_array_free(plugin
->flt_comp_classes
, TRUE
);
136 plugin
->flt_comp_classes
= NULL
;
139 if (plugin
->sink_comp_classes
) {
140 BT_LOGD_STR("Putting sink component classes.");
141 g_ptr_array_free(plugin
->sink_comp_classes
, TRUE
);
142 plugin
->sink_comp_classes
= NULL
;
145 if (plugin
->info
.name
) {
146 g_string_free(plugin
->info
.name
, TRUE
);
147 plugin
->info
.name
= NULL
;
150 if (plugin
->info
.path
) {
151 g_string_free(plugin
->info
.path
, TRUE
);
152 plugin
->info
.path
= NULL
;
155 if (plugin
->info
.description
) {
156 g_string_free(plugin
->info
.description
, TRUE
);
157 plugin
->info
.description
= NULL
;
160 if (plugin
->info
.author
) {
161 g_string_free(plugin
->info
.author
, TRUE
);
162 plugin
->info
.author
= NULL
;
165 if (plugin
->info
.license
) {
166 g_string_free(plugin
->info
.license
, TRUE
);
167 plugin
->info
.license
= NULL
;
170 if (plugin
->info
.version
.extra
) {
171 g_string_free(plugin
->info
.version
.extra
, TRUE
);
172 plugin
->info
.version
.extra
= NULL
;
179 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
181 struct bt_plugin
*plugin
= NULL
;
183 BT_LOGD("Creating empty plugin object: type=%s",
184 bt_plugin_type_string(type
));
186 plugin
= g_new0(struct bt_plugin
, 1);
188 BT_LOGE_STR("Failed to allocate one plugin.");
192 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
195 /* Create empty arrays of component classes */
196 plugin
->src_comp_classes
=
197 g_ptr_array_new_with_free_func(
198 (GDestroyNotify
) bt_object_put_ref
);
199 if (!plugin
->src_comp_classes
) {
200 BT_LOGE_STR("Failed to allocate a GPtrArray.");
204 plugin
->flt_comp_classes
=
205 g_ptr_array_new_with_free_func(
206 (GDestroyNotify
) bt_object_put_ref
);
207 if (!plugin
->flt_comp_classes
) {
208 BT_LOGE_STR("Failed to allocate a GPtrArray.");
212 plugin
->sink_comp_classes
=
213 g_ptr_array_new_with_free_func(
214 (GDestroyNotify
) bt_object_put_ref
);
215 if (!plugin
->sink_comp_classes
) {
216 BT_LOGE_STR("Failed to allocate a GPtrArray.");
220 /* Create empty info */
221 plugin
->info
.name
= g_string_new(NULL
);
222 if (!plugin
->info
.name
) {
223 BT_LOGE_STR("Failed to allocate a GString.");
227 plugin
->info
.path
= g_string_new(NULL
);
228 if (!plugin
->info
.path
) {
229 BT_LOGE_STR("Failed to allocate a GString.");
233 plugin
->info
.description
= g_string_new(NULL
);
234 if (!plugin
->info
.description
) {
235 BT_LOGE_STR("Failed to allocate a GString.");
239 plugin
->info
.author
= g_string_new(NULL
);
240 if (!plugin
->info
.author
) {
241 BT_LOGE_STR("Failed to allocate a GString.");
245 plugin
->info
.license
= g_string_new(NULL
);
246 if (!plugin
->info
.license
) {
247 BT_LOGE_STR("Failed to allocate a GString.");
251 plugin
->info
.version
.extra
= g_string_new(NULL
);
252 if (!plugin
->info
.version
.extra
) {
253 BT_LOGE_STR("Failed to allocate a GString.");
257 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
261 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
268 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *path
)
272 g_string_assign(plugin
->info
.path
, path
);
273 plugin
->info
.path_set
= BT_TRUE
;
274 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
279 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *name
)
283 g_string_assign(plugin
->info
.name
, name
);
284 plugin
->info
.name_set
= BT_TRUE
;
285 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
290 void bt_plugin_set_description(struct bt_plugin
*plugin
,
291 const char *description
)
294 BT_ASSERT(description
);
295 g_string_assign(plugin
->info
.description
, description
);
296 plugin
->info
.description_set
= BT_TRUE
;
297 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin
);
301 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *author
)
305 g_string_assign(plugin
->info
.author
, author
);
306 plugin
->info
.author_set
= BT_TRUE
;
307 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
312 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *license
)
316 g_string_assign(plugin
->info
.license
, license
);
317 plugin
->info
.license_set
= BT_TRUE
;
318 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
323 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
324 unsigned int minor
, unsigned int patch
, const char *extra
)
327 plugin
->info
.version
.major
= major
;
328 plugin
->info
.version
.minor
= minor
;
329 plugin
->info
.version
.patch
= patch
;
332 g_string_assign(plugin
->info
.version
.extra
, extra
);
335 plugin
->info
.version_set
= BT_TRUE
;
336 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
337 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
338 plugin
, major
, minor
, patch
, extra
);
342 enum bt_plugin_status
bt_plugin_add_component_class(
343 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
345 GPtrArray
*comp_classes
;
348 BT_ASSERT(comp_class
);
350 switch (comp_class
->type
) {
351 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
352 comp_classes
= plugin
->src_comp_classes
;
354 case BT_COMPONENT_CLASS_TYPE_FILTER
:
355 comp_classes
= plugin
->flt_comp_classes
;
357 case BT_COMPONENT_CLASS_TYPE_SINK
:
358 comp_classes
= plugin
->sink_comp_classes
;
364 /* Add new component class */
365 g_ptr_array_add(comp_classes
, bt_object_get_ref(comp_class
));
367 /* Special case for a shared object plugin */
368 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
369 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
372 BT_LIB_LOGD("Added component class to plugin: "
373 "%![plugin-]+l, %![cc-]+C", plugin
, comp_class
);
374 return BT_PLUGIN_STATUS_OK
;
378 void bt_plugin_set_destroy(struct bt_object
*obj
)
380 struct bt_plugin_set
*plugin_set
=
381 container_of(obj
, struct bt_plugin_set
, base
);
387 BT_LOGD("Destroying plugin set: addr=%p", plugin_set
);
389 if (plugin_set
->plugins
) {
390 BT_LOGD_STR("Putting plugins.");
391 g_ptr_array_free(plugin_set
->plugins
, TRUE
);
398 struct bt_plugin_set
*bt_plugin_set_create(void)
400 struct bt_plugin_set
*plugin_set
= g_new0(struct bt_plugin_set
, 1);
406 BT_LOGD_STR("Creating empty plugin set.");
407 bt_object_init_shared(&plugin_set
->base
, bt_plugin_set_destroy
);
409 plugin_set
->plugins
= g_ptr_array_new_with_free_func(
410 (GDestroyNotify
) bt_object_put_ref
);
411 if (!plugin_set
->plugins
) {
412 BT_LOGE_STR("Failed to allocate a GPtrArray.");
413 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
417 BT_LOGD("Created empty plugin set: addr=%p", plugin_set
);
424 void bt_plugin_set_add_plugin(struct bt_plugin_set
*plugin_set
,
425 struct bt_plugin
*plugin
)
427 BT_ASSERT(plugin_set
);
429 g_ptr_array_add(plugin_set
->plugins
, bt_object_get_ref(plugin
));
430 BT_LIB_LOGV("Added plugin to plugin set: "
431 "plugin-set-addr=%p, %![plugin-]+l",
435 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */