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 <babeltrace/babeltrace-internal.h>
28 #include <babeltrace/graph/component-class-internal.h>
29 #include <babeltrace/plugin/plugin-const.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/plugin/plugin-so-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/object.h>
34 #include <babeltrace/types.h>
35 #include <babeltrace/assert-internal.h>
39 BT_PLUGIN_TYPE_SO
= 0,
40 BT_PLUGIN_TYPE_PYTHON
= 1,
44 struct bt_object base
;
45 enum bt_plugin_type type
;
47 /* Arrays of `struct bt_component_class *` (owned by this) */
48 GPtrArray
*src_comp_classes
;
49 GPtrArray
*flt_comp_classes
;
50 GPtrArray
*sink_comp_classes
;
52 /* Info (owned by this) */
73 /* Value depends on the specific plugin type */
75 void (*destroy_spec_data
)(struct bt_plugin
*);
78 struct bt_plugin_set
{
79 struct bt_object base
;
81 /* Array of struct bt_plugin * */
86 const char *bt_plugin_status_string(enum bt_plugin_status status
)
89 case BT_PLUGIN_STATUS_OK
:
90 return "BT_PLUGIN_STATUS_OK";
91 case BT_PLUGIN_STATUS_ERROR
:
92 return "BT_PLUGIN_STATUS_ERROR";
93 case BT_PLUGIN_STATUS_NOMEM
:
94 return "BT_PLUGIN_STATUS_NOMEM";
101 const char *bt_plugin_type_string(enum bt_plugin_type type
)
104 case BT_PLUGIN_TYPE_SO
:
105 return "BT_PLUGIN_TYPE_SO";
106 case BT_PLUGIN_TYPE_PYTHON
:
107 return "BT_PLUGIN_TYPE_PYTHON";
114 void bt_plugin_destroy(struct bt_object
*obj
)
116 struct bt_plugin
*plugin
;
119 plugin
= container_of(obj
, struct bt_plugin
, base
);
120 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin
);
122 if (plugin
->destroy_spec_data
) {
123 plugin
->destroy_spec_data(plugin
);
126 if (plugin
->src_comp_classes
) {
127 BT_LOGD_STR("Putting source component classes.");
128 g_ptr_array_free(plugin
->src_comp_classes
, TRUE
);
129 plugin
->src_comp_classes
= NULL
;
132 if (plugin
->flt_comp_classes
) {
133 BT_LOGD_STR("Putting filter component classes.");
134 g_ptr_array_free(plugin
->flt_comp_classes
, TRUE
);
135 plugin
->flt_comp_classes
= NULL
;
138 if (plugin
->sink_comp_classes
) {
139 BT_LOGD_STR("Putting sink component classes.");
140 g_ptr_array_free(plugin
->sink_comp_classes
, TRUE
);
141 plugin
->sink_comp_classes
= NULL
;
144 if (plugin
->info
.name
) {
145 g_string_free(plugin
->info
.name
, TRUE
);
146 plugin
->info
.name
= NULL
;
149 if (plugin
->info
.path
) {
150 g_string_free(plugin
->info
.path
, TRUE
);
151 plugin
->info
.path
= NULL
;
154 if (plugin
->info
.description
) {
155 g_string_free(plugin
->info
.description
, TRUE
);
156 plugin
->info
.description
= NULL
;
159 if (plugin
->info
.author
) {
160 g_string_free(plugin
->info
.author
, TRUE
);
161 plugin
->info
.author
= NULL
;
164 if (plugin
->info
.license
) {
165 g_string_free(plugin
->info
.license
, TRUE
);
166 plugin
->info
.license
= NULL
;
169 if (plugin
->info
.version
.extra
) {
170 g_string_free(plugin
->info
.version
.extra
, TRUE
);
171 plugin
->info
.version
.extra
= NULL
;
178 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
180 struct bt_plugin
*plugin
= NULL
;
182 BT_LOGD("Creating empty plugin object: type=%s",
183 bt_plugin_type_string(type
));
185 plugin
= g_new0(struct bt_plugin
, 1);
187 BT_LOGE_STR("Failed to allocate one plugin.");
191 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
194 /* Create empty arrays of component classes */
195 plugin
->src_comp_classes
=
196 g_ptr_array_new_with_free_func(
197 (GDestroyNotify
) bt_object_put_ref
);
198 if (!plugin
->src_comp_classes
) {
199 BT_LOGE_STR("Failed to allocate a GPtrArray.");
203 plugin
->flt_comp_classes
=
204 g_ptr_array_new_with_free_func(
205 (GDestroyNotify
) bt_object_put_ref
);
206 if (!plugin
->flt_comp_classes
) {
207 BT_LOGE_STR("Failed to allocate a GPtrArray.");
211 plugin
->sink_comp_classes
=
212 g_ptr_array_new_with_free_func(
213 (GDestroyNotify
) bt_object_put_ref
);
214 if (!plugin
->sink_comp_classes
) {
215 BT_LOGE_STR("Failed to allocate a GPtrArray.");
219 /* Create empty info */
220 plugin
->info
.name
= g_string_new(NULL
);
221 if (!plugin
->info
.name
) {
222 BT_LOGE_STR("Failed to allocate a GString.");
226 plugin
->info
.path
= g_string_new(NULL
);
227 if (!plugin
->info
.path
) {
228 BT_LOGE_STR("Failed to allocate a GString.");
232 plugin
->info
.description
= g_string_new(NULL
);
233 if (!plugin
->info
.description
) {
234 BT_LOGE_STR("Failed to allocate a GString.");
238 plugin
->info
.author
= g_string_new(NULL
);
239 if (!plugin
->info
.author
) {
240 BT_LOGE_STR("Failed to allocate a GString.");
244 plugin
->info
.license
= g_string_new(NULL
);
245 if (!plugin
->info
.license
) {
246 BT_LOGE_STR("Failed to allocate a GString.");
250 plugin
->info
.version
.extra
= g_string_new(NULL
);
251 if (!plugin
->info
.version
.extra
) {
252 BT_LOGE_STR("Failed to allocate a GString.");
256 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
260 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
267 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *path
)
271 g_string_assign(plugin
->info
.path
, path
);
272 plugin
->info
.path_set
= BT_TRUE
;
273 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
278 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *name
)
282 g_string_assign(plugin
->info
.name
, name
);
283 plugin
->info
.name_set
= BT_TRUE
;
284 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
289 void bt_plugin_set_description(struct bt_plugin
*plugin
,
290 const char *description
)
293 BT_ASSERT(description
);
294 g_string_assign(plugin
->info
.description
, description
);
295 plugin
->info
.description_set
= BT_TRUE
;
296 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin
);
300 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *author
)
304 g_string_assign(plugin
->info
.author
, author
);
305 plugin
->info
.author_set
= BT_TRUE
;
306 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
311 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *license
)
315 g_string_assign(plugin
->info
.license
, license
);
316 plugin
->info
.license_set
= BT_TRUE
;
317 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
322 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
323 unsigned int minor
, unsigned int patch
, const char *extra
)
326 plugin
->info
.version
.major
= major
;
327 plugin
->info
.version
.minor
= minor
;
328 plugin
->info
.version
.patch
= patch
;
331 g_string_assign(plugin
->info
.version
.extra
, extra
);
334 plugin
->info
.version_set
= BT_TRUE
;
335 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
336 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
337 plugin
, major
, minor
, patch
, extra
);
341 enum bt_plugin_status
bt_plugin_add_component_class(
342 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
344 GPtrArray
*comp_classes
;
347 BT_ASSERT(comp_class
);
349 switch (comp_class
->type
) {
350 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
351 comp_classes
= plugin
->src_comp_classes
;
353 case BT_COMPONENT_CLASS_TYPE_FILTER
:
354 comp_classes
= plugin
->flt_comp_classes
;
356 case BT_COMPONENT_CLASS_TYPE_SINK
:
357 comp_classes
= plugin
->sink_comp_classes
;
363 /* Add new component class */
364 bt_object_get_ref(comp_class
);
365 g_ptr_array_add(comp_classes
, 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 bt_object_get_ref(plugin
);
430 g_ptr_array_add(plugin_set
->plugins
, plugin
);
431 BT_LIB_LOGV("Added plugin to plugin set: "
432 "plugin-set-addr=%p, %![plugin-]+l",
436 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */