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/types.h>
34 #include <babeltrace/assert-internal.h>
38 BT_PLUGIN_TYPE_SO
= 0,
39 BT_PLUGIN_TYPE_PYTHON
= 1,
43 struct bt_object base
;
44 enum bt_plugin_type type
;
46 /* Arrays of `struct bt_component_class *` (owned by this) */
47 GPtrArray
*src_comp_classes
;
48 GPtrArray
*flt_comp_classes
;
49 GPtrArray
*sink_comp_classes
;
51 /* Info (owned by this) */
72 /* Value depends on the specific plugin type */
74 void (*destroy_spec_data
)(struct bt_plugin
*);
77 struct bt_plugin_set
{
78 struct bt_object base
;
80 /* Array of struct bt_plugin * */
85 const char *bt_plugin_status_string(enum bt_plugin_status status
)
88 case BT_PLUGIN_STATUS_OK
:
89 return "BT_PLUGIN_STATUS_OK";
90 case BT_PLUGIN_STATUS_ERROR
:
91 return "BT_PLUGIN_STATUS_ERROR";
92 case BT_PLUGIN_STATUS_NOMEM
:
93 return "BT_PLUGIN_STATUS_NOMEM";
100 const char *bt_plugin_type_string(enum bt_plugin_type type
)
103 case BT_PLUGIN_TYPE_SO
:
104 return "BT_PLUGIN_TYPE_SO";
105 case BT_PLUGIN_TYPE_PYTHON
:
106 return "BT_PLUGIN_TYPE_PYTHON";
113 void bt_plugin_destroy(struct bt_object
*obj
)
115 struct bt_plugin
*plugin
;
118 plugin
= container_of(obj
, struct bt_plugin
, base
);
119 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin
);
121 if (plugin
->destroy_spec_data
) {
122 plugin
->destroy_spec_data(plugin
);
125 if (plugin
->src_comp_classes
) {
126 BT_LOGD_STR("Putting source component classes.");
127 g_ptr_array_free(plugin
->src_comp_classes
, TRUE
);
128 plugin
->src_comp_classes
= NULL
;
131 if (plugin
->flt_comp_classes
) {
132 BT_LOGD_STR("Putting filter component classes.");
133 g_ptr_array_free(plugin
->flt_comp_classes
, TRUE
);
134 plugin
->flt_comp_classes
= NULL
;
137 if (plugin
->sink_comp_classes
) {
138 BT_LOGD_STR("Putting sink component classes.");
139 g_ptr_array_free(plugin
->sink_comp_classes
, TRUE
);
140 plugin
->sink_comp_classes
= NULL
;
143 if (plugin
->info
.name
) {
144 g_string_free(plugin
->info
.name
, TRUE
);
145 plugin
->info
.name
= NULL
;
148 if (plugin
->info
.path
) {
149 g_string_free(plugin
->info
.path
, TRUE
);
150 plugin
->info
.path
= NULL
;
153 if (plugin
->info
.description
) {
154 g_string_free(plugin
->info
.description
, TRUE
);
155 plugin
->info
.description
= NULL
;
158 if (plugin
->info
.author
) {
159 g_string_free(plugin
->info
.author
, TRUE
);
160 plugin
->info
.author
= NULL
;
163 if (plugin
->info
.license
) {
164 g_string_free(plugin
->info
.license
, TRUE
);
165 plugin
->info
.license
= NULL
;
168 if (plugin
->info
.version
.extra
) {
169 g_string_free(plugin
->info
.version
.extra
, TRUE
);
170 plugin
->info
.version
.extra
= NULL
;
177 struct bt_plugin
*bt_plugin_create_empty(enum bt_plugin_type type
)
179 struct bt_plugin
*plugin
= NULL
;
181 BT_LOGD("Creating empty plugin object: type=%s",
182 bt_plugin_type_string(type
));
184 plugin
= g_new0(struct bt_plugin
, 1);
186 BT_LOGE_STR("Failed to allocate one plugin.");
190 bt_object_init_shared(&plugin
->base
, bt_plugin_destroy
);
193 /* Create empty arrays of component classes */
194 plugin
->src_comp_classes
=
195 g_ptr_array_new_with_free_func(
196 (GDestroyNotify
) bt_object_put_ref
);
197 if (!plugin
->src_comp_classes
) {
198 BT_LOGE_STR("Failed to allocate a GPtrArray.");
202 plugin
->flt_comp_classes
=
203 g_ptr_array_new_with_free_func(
204 (GDestroyNotify
) bt_object_put_ref
);
205 if (!plugin
->flt_comp_classes
) {
206 BT_LOGE_STR("Failed to allocate a GPtrArray.");
210 plugin
->sink_comp_classes
=
211 g_ptr_array_new_with_free_func(
212 (GDestroyNotify
) bt_object_put_ref
);
213 if (!plugin
->sink_comp_classes
) {
214 BT_LOGE_STR("Failed to allocate a GPtrArray.");
218 /* Create empty info */
219 plugin
->info
.name
= g_string_new(NULL
);
220 if (!plugin
->info
.name
) {
221 BT_LOGE_STR("Failed to allocate a GString.");
225 plugin
->info
.path
= g_string_new(NULL
);
226 if (!plugin
->info
.path
) {
227 BT_LOGE_STR("Failed to allocate a GString.");
231 plugin
->info
.description
= g_string_new(NULL
);
232 if (!plugin
->info
.description
) {
233 BT_LOGE_STR("Failed to allocate a GString.");
237 plugin
->info
.author
= g_string_new(NULL
);
238 if (!plugin
->info
.author
) {
239 BT_LOGE_STR("Failed to allocate a GString.");
243 plugin
->info
.license
= g_string_new(NULL
);
244 if (!plugin
->info
.license
) {
245 BT_LOGE_STR("Failed to allocate a GString.");
249 plugin
->info
.version
.extra
= g_string_new(NULL
);
250 if (!plugin
->info
.version
.extra
) {
251 BT_LOGE_STR("Failed to allocate a GString.");
255 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin
);
259 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
266 void bt_plugin_set_path(struct bt_plugin
*plugin
, const char *path
)
270 g_string_assign(plugin
->info
.path
, path
);
271 plugin
->info
.path_set
= BT_TRUE
;
272 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
277 void bt_plugin_set_name(struct bt_plugin
*plugin
, const char *name
)
281 g_string_assign(plugin
->info
.name
, name
);
282 plugin
->info
.name_set
= BT_TRUE
;
283 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
288 void bt_plugin_set_description(struct bt_plugin
*plugin
,
289 const char *description
)
292 BT_ASSERT(description
);
293 g_string_assign(plugin
->info
.description
, description
);
294 plugin
->info
.description_set
= BT_TRUE
;
295 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin
);
299 void bt_plugin_set_author(struct bt_plugin
*plugin
, const char *author
)
303 g_string_assign(plugin
->info
.author
, author
);
304 plugin
->info
.author_set
= BT_TRUE
;
305 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
310 void bt_plugin_set_license(struct bt_plugin
*plugin
, const char *license
)
314 g_string_assign(plugin
->info
.license
, license
);
315 plugin
->info
.license_set
= BT_TRUE
;
316 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
321 void bt_plugin_set_version(struct bt_plugin
*plugin
, unsigned int major
,
322 unsigned int minor
, unsigned int patch
, const char *extra
)
325 plugin
->info
.version
.major
= major
;
326 plugin
->info
.version
.minor
= minor
;
327 plugin
->info
.version
.patch
= patch
;
330 g_string_assign(plugin
->info
.version
.extra
, extra
);
333 plugin
->info
.version_set
= BT_TRUE
;
334 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
335 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
336 plugin
, major
, minor
, patch
, extra
);
340 enum bt_plugin_status
bt_plugin_add_component_class(
341 struct bt_plugin
*plugin
, struct bt_component_class
*comp_class
)
343 GPtrArray
*comp_classes
;
346 BT_ASSERT(comp_class
);
348 switch (comp_class
->type
) {
349 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
350 comp_classes
= plugin
->src_comp_classes
;
352 case BT_COMPONENT_CLASS_TYPE_FILTER
:
353 comp_classes
= plugin
->flt_comp_classes
;
355 case BT_COMPONENT_CLASS_TYPE_SINK
:
356 comp_classes
= plugin
->sink_comp_classes
;
362 /* Add new component class */
363 bt_object_get_ref(comp_class
);
364 g_ptr_array_add(comp_classes
, comp_class
);
366 /* Special case for a shared object plugin */
367 if (plugin
->type
== BT_PLUGIN_TYPE_SO
) {
368 bt_plugin_so_on_add_component_class(plugin
, comp_class
);
371 BT_LIB_LOGD("Added component class to plugin: "
372 "%![plugin-]+l, %![cc-]+C", plugin
, comp_class
);
373 return BT_PLUGIN_STATUS_OK
;
377 void bt_plugin_set_destroy(struct bt_object
*obj
)
379 struct bt_plugin_set
*plugin_set
=
380 container_of(obj
, struct bt_plugin_set
, base
);
386 BT_LOGD("Destroying plugin set: addr=%p", plugin_set
);
388 if (plugin_set
->plugins
) {
389 BT_LOGD_STR("Putting plugins.");
390 g_ptr_array_free(plugin_set
->plugins
, TRUE
);
397 struct bt_plugin_set
*bt_plugin_set_create(void)
399 struct bt_plugin_set
*plugin_set
= g_new0(struct bt_plugin_set
, 1);
405 BT_LOGD_STR("Creating empty plugin set.");
406 bt_object_init_shared(&plugin_set
->base
, bt_plugin_set_destroy
);
408 plugin_set
->plugins
= g_ptr_array_new_with_free_func(
409 (GDestroyNotify
) bt_object_put_ref
);
410 if (!plugin_set
->plugins
) {
411 BT_LOGE_STR("Failed to allocate a GPtrArray.");
412 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
416 BT_LOGD("Created empty plugin set: addr=%p", plugin_set
);
423 void bt_plugin_set_add_plugin(struct bt_plugin_set
*plugin_set
,
424 struct bt_plugin
*plugin
)
426 BT_ASSERT(plugin_set
);
428 bt_object_get_ref(plugin
);
429 g_ptr_array_add(plugin_set
->plugins
, plugin
);
430 BT_LIB_LOGV("Added plugin to plugin set: "
431 "plugin-set-addr=%p, %![plugin-]+l",
435 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */