4 * Babeltrace Plugin (shared object)
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-SO"
31 #include <babeltrace/lib-logging-internal.h>
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/plugin/plugin-internal.h>
36 #include <babeltrace/plugin/plugin-so-internal.h>
37 #include <babeltrace/plugin/plugin-dev.h>
38 #include <babeltrace/plugin/plugin-internal.h>
39 #include <babeltrace/graph/component-class-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/list-internal.h>
47 #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
48 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
49 #define LIBTOOL_PLUGIN_SUFFIX ".la"
50 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
52 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
53 sizeof(LIBTOOL_PLUGIN_SUFFIX))
58 * This list, global to the library, keeps all component classes that
59 * have a reference to their shared library handles. It allows iteration
60 * on all component classes still present when the destructor executes
61 * to release the shared library handle references they might still have.
63 * The list items are the component classes created with
64 * bt_plugin_add_component_class(). They keep the shared library handle
65 * object created by their plugin alive so that the plugin's code is
66 * not discarded when it could still be in use by living components
67 * created from those component classes:
69 * [component] --ref-> [component class]-> [shlib handle]
71 * It allows this use-case:
73 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
74 * // instantiate components from a plugin's component classes
75 * // put plugins and free my_plugins here
76 * // user code of instantiated components still exists
78 * An entry is removed from this list when a component class is
79 * destroyed thanks to a custom destroy listener. When the entry is
80 * removed, the entry is removed from the list, and we release the
81 * reference on the shlib handle. Assuming the original plugin object
82 * which contained some component classes is put first, when the last
83 * component class is removed from this list, the shared library handle
84 * object's reference count falls to zero and the shared library is
89 BT_LIST_HEAD(component_class_list
);
91 __attribute__((destructor
)) static
92 void fini_comp_class_list(void)
94 struct bt_component_class
*comp_class
, *tmp
;
96 bt_list_for_each_entry_safe(comp_class
, tmp
, &component_class_list
, node
) {
97 bt_list_del(&comp_class
->node
);
98 BT_PUT(comp_class
->so_handle
);
100 BT_LOGD_STR("Released references from all component classes to shared library handles.");
104 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
106 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
109 shared_lib_handle
= container_of(obj
,
110 struct bt_plugin_so_shared_lib_handle
, base
);
111 const char *path
= shared_lib_handle
->path
?
112 shared_lib_handle
->path
->str
: NULL
;
114 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
115 shared_lib_handle
, path
);
117 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
118 enum bt_plugin_status status
;
120 BT_LOGD_STR("Calling user's plugin exit function.");
121 status
= shared_lib_handle
->exit();
122 BT_LOGD("User function returned: %s",
123 bt_plugin_status_string(status
));
126 BT_LOGW("User's plugin exit function failed: "
127 "path=\"%s\"", path
);
131 if (shared_lib_handle
->module
) {
134 * Valgrind shows incomplete stack traces when
135 * dynamically loaded libraries are closed before it
136 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
137 * build to avoid this.
139 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
141 if (!var
|| strcmp(var
, "1") != 0) {
143 BT_LOGD("Closing GModule: path=\"%s\"", path
);
145 if (!g_module_close(shared_lib_handle
->module
)) {
146 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
147 g_module_error(), path
);
151 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
152 "path=\"%s\"", path
);
157 if (shared_lib_handle
->path
) {
158 g_string_free(shared_lib_handle
->path
, TRUE
);
161 g_free(shared_lib_handle
);
165 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
168 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
170 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
171 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
172 if (!shared_lib_handle
) {
173 BT_LOGE_STR("Failed to allocate one shared library handle.");
177 bt_object_init(shared_lib_handle
, bt_plugin_so_shared_lib_handle_destroy
);
183 shared_lib_handle
->path
= g_string_new(path
);
184 if (!shared_lib_handle
->path
) {
185 BT_LOGE_STR("Failed to allocate a GString.");
189 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
190 if (!shared_lib_handle
->module
) {
192 * DEBUG-level logging because we're only _trying_ to
193 * open this file as a Babeltrace plugin: if it's not,
194 * it's not an error. And because this can be tried
195 * during bt_plugin_create_all_from_dir(), it's not even
198 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
199 g_module_error(), path
);
206 BT_PUT(shared_lib_handle
);
209 if (shared_lib_handle
) {
210 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
211 path
, shared_lib_handle
);
214 return shared_lib_handle
;
218 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
220 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
222 if (!plugin
->spec_data
) {
226 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
228 BT_PUT(spec
->shared_lib_handle
);
229 g_free(plugin
->spec_data
);
230 plugin
->spec_data
= NULL
;
234 * This function does the following:
236 * 1. Iterate on the plugin descriptor attributes section and set the
237 * plugin's attributes depending on the attribute types. This
238 * includes the name of the plugin, its description, and its
239 * initialization function, for example.
241 * 2. Iterate on the component class descriptors section and create one
242 * "full descriptor" (temporary structure) for each one that is found
243 * and attached to our plugin descriptor.
245 * 3. Iterate on the component class descriptor attributes section and
246 * set the corresponding full descriptor's attributes depending on
247 * the attribute types. This includes the description of the
248 * component class, as well as its initialization and destroy
251 * 4. Call the user's plugin initialization function, if any is
254 * 5. For each full component class descriptor, create a component class
255 * object, set its optional attributes, and add it to the plugin
258 * 6. Freeze the plugin object.
261 enum bt_plugin_status
bt_plugin_so_init(
262 struct bt_plugin
*plugin
,
263 const struct __bt_plugin_descriptor
*descriptor
,
264 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
265 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
266 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
267 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
268 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
269 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
272 * This structure's members point to the plugin's memory
275 struct comp_class_full_descriptor
{
276 const struct __bt_plugin_component_class_descriptor
*descriptor
;
277 const char *description
;
279 bt_component_class_init_method init_method
;
280 bt_component_class_finalize_method finalize_method
;
281 bt_component_class_query_method query_method
;
282 bt_component_class_accept_port_connection_method accept_port_connection_method
;
283 bt_component_class_port_connected_method port_connected_method
;
284 bt_component_class_port_disconnected_method port_disconnected_method
;
285 struct bt_component_class_iterator_methods iterator_methods
;
288 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
289 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
290 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
291 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
292 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
293 GArray
*comp_class_full_descriptors
;
297 BT_LOGD("Initializing plugin object from descriptors found in sections: "
298 "plugin-addr=%p, plugin-path=\"%s\", "
299 "attrs-begin-addr=%p, attrs-end-addr=%p, "
300 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
301 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
303 spec
->shared_lib_handle
->path
?
304 spec
->shared_lib_handle
->path
->str
: NULL
,
305 attrs_begin
, attrs_end
,
306 cc_descriptors_begin
, cc_descriptors_end
,
307 cc_descr_attrs_begin
, cc_descr_attrs_end
);
308 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
309 sizeof(struct comp_class_full_descriptor
));
310 if (!comp_class_full_descriptors
) {
311 BT_LOGE_STR("Failed to allocate a GArray.");
312 status
= BT_PLUGIN_STATUS_ERROR
;
316 /* Set mandatory attributes */
317 spec
->descriptor
= descriptor
;
318 bt_plugin_set_name(plugin
, descriptor
->name
);
321 * Find and set optional attributes attached to this plugin
324 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
325 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
328 if (cur_attr
== NULL
) {
332 if (cur_attr
->plugin_descriptor
!= descriptor
) {
336 switch (cur_attr
->type
) {
337 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
338 spec
->init
= cur_attr
->value
.init
;
340 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
341 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
343 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
344 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
346 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
347 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
349 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
350 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
352 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
353 bt_plugin_set_version(plugin
,
354 (unsigned int) cur_attr
->value
.version
.major
,
355 (unsigned int) cur_attr
->value
.version
.minor
,
356 (unsigned int) cur_attr
->value
.version
.patch
,
357 cur_attr
->value
.version
.extra
);
361 * WARN-level logging because this should not
362 * happen with the appropriate ABI version. If
363 * we're here, we know that for the reported
364 * version of the ABI, this attribute is
367 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
368 "plugin-path=\"%s\", plugin-name=\"%s\", "
369 "attr-type-name=\"%s\", attr-type-id=%d",
370 spec
->shared_lib_handle
->path
?
371 spec
->shared_lib_handle
->path
->str
:
373 descriptor
->name
, cur_attr
->type_name
,
380 * Find component class descriptors attached to this plugin
381 * descriptor and initialize corresponding full component class
382 * descriptors in the array.
384 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
385 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
387 struct comp_class_full_descriptor full_descriptor
= {0};
389 if (cur_cc_descr
== NULL
) {
393 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
397 full_descriptor
.descriptor
= cur_cc_descr
;
398 g_array_append_val(comp_class_full_descriptors
,
403 * Find component class descriptor attributes attached to this
404 * plugin descriptor and update corresponding full component
405 * class descriptors in the array.
407 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
408 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
409 *cur_cc_descr_attr_ptr
;
411 if (cur_cc_descr_attr
== NULL
) {
415 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
420 /* Find the corresponding component class descriptor entry */
421 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
422 struct comp_class_full_descriptor
*cc_full_descr
=
423 &g_array_index(comp_class_full_descriptors
,
424 struct comp_class_full_descriptor
, i
);
426 if (cur_cc_descr_attr
->comp_class_descriptor
==
427 cc_full_descr
->descriptor
) {
428 switch (cur_cc_descr_attr
->type
) {
429 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
430 cc_full_descr
->description
=
431 cur_cc_descr_attr
->value
.description
;
433 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
434 cc_full_descr
->help
=
435 cur_cc_descr_attr
->value
.help
;
437 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
438 cc_full_descr
->init_method
=
439 cur_cc_descr_attr
->value
.init_method
;
441 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
442 cc_full_descr
->finalize_method
=
443 cur_cc_descr_attr
->value
.finalize_method
;
445 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
446 cc_full_descr
->query_method
=
447 cur_cc_descr_attr
->value
.query_method
;
449 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
450 cc_full_descr
->accept_port_connection_method
=
451 cur_cc_descr_attr
->value
.accept_port_connection_method
;
453 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD
:
454 cc_full_descr
->port_connected_method
=
455 cur_cc_descr_attr
->value
.port_connected_method
;
457 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
458 cc_full_descr
->port_disconnected_method
=
459 cur_cc_descr_attr
->value
.port_disconnected_method
;
461 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
462 cc_full_descr
->iterator_methods
.init
=
463 cur_cc_descr_attr
->value
.notif_iter_init_method
;
465 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
466 cc_full_descr
->iterator_methods
.finalize
=
467 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
469 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD
:
470 cc_full_descr
->iterator_methods
.seek_time
=
471 cur_cc_descr_attr
->value
.notif_iter_seek_time_method
;
475 * WARN-level logging because
476 * this should not happen with
477 * the appropriate ABI version.
478 * If we're here, we know that
479 * for the reported version of
480 * the ABI, this attribute is
483 BT_LOGW("Ignoring unknown component class descriptor attribute: "
484 "plugin-path=\"%s\", "
485 "plugin-name=\"%s\", "
486 "comp-class-name=\"%s\", "
487 "comp-class-type=%s, "
488 "attr-type-name=\"%s\", "
490 spec
->shared_lib_handle
->path
?
491 spec
->shared_lib_handle
->path
->str
:
494 cur_cc_descr_attr
->comp_class_descriptor
->name
,
495 bt_component_class_type_string(
496 cur_cc_descr_attr
->comp_class_descriptor
->type
),
497 cur_cc_descr_attr
->type_name
,
498 cur_cc_descr_attr
->type
);
505 /* Initialize plugin */
507 BT_LOGD_STR("Calling user's plugin initialization function.");
508 status
= spec
->init(plugin
);
509 BT_LOGD("User function returned: %s",
510 bt_plugin_status_string(status
));
513 BT_LOGW_STR("User's plugin initialization function failed.");
518 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
520 /* Add described component classes to plugin */
521 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
522 struct comp_class_full_descriptor
*cc_full_descr
=
523 &g_array_index(comp_class_full_descriptors
,
524 struct comp_class_full_descriptor
, i
);
525 struct bt_component_class
*comp_class
;
527 BT_LOGD("Creating and setting properties of plugin's component class: "
528 "plugin-path=\"%s\", plugin-name=\"%s\", "
529 "comp-class-name=\"%s\", comp-class-type=%s",
530 spec
->shared_lib_handle
->path
?
531 spec
->shared_lib_handle
->path
->str
:
534 cc_full_descr
->descriptor
->name
,
535 bt_component_class_type_string(
536 cc_full_descr
->descriptor
->type
));
538 switch (cc_full_descr
->descriptor
->type
) {
539 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
540 comp_class
= bt_component_class_source_create(
541 cc_full_descr
->descriptor
->name
,
542 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
544 case BT_COMPONENT_CLASS_TYPE_FILTER
:
545 comp_class
= bt_component_class_filter_create(
546 cc_full_descr
->descriptor
->name
,
547 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
549 case BT_COMPONENT_CLASS_TYPE_SINK
:
550 comp_class
= bt_component_class_sink_create(
551 cc_full_descr
->descriptor
->name
,
552 cc_full_descr
->descriptor
->methods
.sink
.consume
);
556 * WARN-level logging because this should not
557 * happen with the appropriate ABI version. If
558 * we're here, we know that for the reported
559 * version of the ABI, this component class type
562 BT_LOGW("Ignoring unknown component class type: "
563 "plugin-path=\"%s\", plugin-name=\"%s\", "
564 "comp-class-name=\"%s\", comp-class-type=%d",
565 spec
->shared_lib_handle
->path
->str
?
566 spec
->shared_lib_handle
->path
->str
:
569 cc_full_descr
->descriptor
->name
,
570 cc_full_descr
->descriptor
->type
);
575 BT_LOGE_STR("Cannot create component class.");
576 status
= BT_PLUGIN_STATUS_ERROR
;
580 if (cc_full_descr
->description
) {
581 ret
= bt_component_class_set_description(comp_class
,
582 cc_full_descr
->description
);
584 BT_LOGE_STR("Cannot set component class's description.");
585 status
= BT_PLUGIN_STATUS_ERROR
;
591 if (cc_full_descr
->help
) {
592 ret
= bt_component_class_set_help(comp_class
,
593 cc_full_descr
->help
);
595 BT_LOGE_STR("Cannot set component class's help string.");
596 status
= BT_PLUGIN_STATUS_ERROR
;
602 if (cc_full_descr
->init_method
) {
603 ret
= bt_component_class_set_init_method(comp_class
,
604 cc_full_descr
->init_method
);
606 BT_LOGE_STR("Cannot set component class's initialization method.");
607 status
= BT_PLUGIN_STATUS_ERROR
;
613 if (cc_full_descr
->finalize_method
) {
614 ret
= bt_component_class_set_finalize_method(comp_class
,
615 cc_full_descr
->finalize_method
);
617 BT_LOGE_STR("Cannot set component class's finalization method.");
618 status
= BT_PLUGIN_STATUS_ERROR
;
624 if (cc_full_descr
->query_method
) {
625 ret
= bt_component_class_set_query_method(
626 comp_class
, cc_full_descr
->query_method
);
628 BT_LOGE_STR("Cannot set component class's query method.");
629 status
= BT_PLUGIN_STATUS_ERROR
;
635 if (cc_full_descr
->accept_port_connection_method
) {
636 ret
= bt_component_class_set_accept_port_connection_method(
637 comp_class
, cc_full_descr
->accept_port_connection_method
);
639 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
640 status
= BT_PLUGIN_STATUS_ERROR
;
646 if (cc_full_descr
->port_connected_method
) {
647 ret
= bt_component_class_set_port_connected_method(
648 comp_class
, cc_full_descr
->port_connected_method
);
650 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
651 status
= BT_PLUGIN_STATUS_ERROR
;
657 if (cc_full_descr
->port_disconnected_method
) {
658 ret
= bt_component_class_set_port_disconnected_method(
659 comp_class
, cc_full_descr
->port_disconnected_method
);
661 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
662 status
= BT_PLUGIN_STATUS_ERROR
;
668 switch (cc_full_descr
->descriptor
->type
) {
669 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
670 if (cc_full_descr
->iterator_methods
.init
) {
671 ret
= bt_component_class_source_set_notification_iterator_init_method(
673 cc_full_descr
->iterator_methods
.init
);
675 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
676 status
= BT_PLUGIN_STATUS_ERROR
;
682 if (cc_full_descr
->iterator_methods
.finalize
) {
683 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
685 cc_full_descr
->iterator_methods
.finalize
);
687 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
688 status
= BT_PLUGIN_STATUS_ERROR
;
694 if (cc_full_descr
->iterator_methods
.seek_time
) {
695 ret
= bt_component_class_source_set_notification_iterator_seek_time_method(
697 cc_full_descr
->iterator_methods
.seek_time
);
699 BT_LOGE_STR("Cannot set source component class's notification iterator seek to time method.");
700 status
= BT_PLUGIN_STATUS_ERROR
;
706 case BT_COMPONENT_CLASS_TYPE_FILTER
:
707 if (cc_full_descr
->iterator_methods
.init
) {
708 ret
= bt_component_class_filter_set_notification_iterator_init_method(
710 cc_full_descr
->iterator_methods
.init
);
712 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
713 status
= BT_PLUGIN_STATUS_ERROR
;
719 if (cc_full_descr
->iterator_methods
.finalize
) {
720 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
722 cc_full_descr
->iterator_methods
.finalize
);
724 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
725 status
= BT_PLUGIN_STATUS_ERROR
;
731 if (cc_full_descr
->iterator_methods
.seek_time
) {
732 ret
= bt_component_class_filter_set_notification_iterator_seek_time_method(
734 cc_full_descr
->iterator_methods
.seek_time
);
736 BT_LOGE_STR("Cannot set filter component class's notification iterator seek to time method.");
737 status
= BT_PLUGIN_STATUS_ERROR
;
743 case BT_COMPONENT_CLASS_TYPE_SINK
:
750 * Add component class to the plugin object.
752 * This will call back
753 * bt_plugin_so_on_add_component_class() so that we can
754 * add a mapping in the component class list when
755 * we know the component class is successfully added.
757 status
= bt_plugin_add_component_class(plugin
,
761 BT_LOGE("Cannot add component class to plugin.");
767 * All the plugin's component classes should be added at this
768 * point. We freeze the plugin so that it's not possible to add
769 * component classes to this plugin object after this stage
770 * (plugin object becomes immutable).
772 bt_plugin_freeze(plugin
);
775 g_array_free(comp_class_full_descriptors
, TRUE
);
780 struct bt_plugin
*bt_plugin_so_create_empty(
781 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
783 struct bt_plugin
*plugin
;
784 struct bt_plugin_so_spec_data
*spec
;
786 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
791 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
792 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
793 if (!plugin
->spec_data
) {
794 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
798 spec
= plugin
->spec_data
;
799 spec
->shared_lib_handle
= bt_get(shared_lib_handle
);
810 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
813 const int * const *begin_int
= (const int * const *) begin
;
814 const int * const *end_int
= (const int * const *) end
;
815 const int * const *iter
;
817 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
827 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
828 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
829 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
830 struct __bt_plugin_descriptor
const * const *descriptors_end
,
831 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
832 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
833 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
834 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
835 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
836 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
838 size_t descriptor_count
;
840 size_t cc_descriptors_count
;
841 size_t cc_descr_attrs_count
;
843 struct bt_plugin_set
*plugin_set
= NULL
;
845 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
846 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
847 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
848 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
850 BT_LOGD("Creating all SO plugins from sections: "
851 "plugin-path=\"%s\", "
852 "descr-begin-addr=%p, descr-end-addr=%p, "
853 "attrs-begin-addr=%p, attrs-end-addr=%p, "
854 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
855 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
856 "descr-count=%zu, attrs-count=%zu, "
857 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
858 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
859 descriptors_begin
, descriptors_end
,
860 attrs_begin
, attrs_end
,
861 cc_descriptors_begin
, cc_descriptors_end
,
862 cc_descr_attrs_begin
, cc_descr_attrs_end
,
863 descriptor_count
, attrs_count
,
864 cc_descriptors_count
, cc_descr_attrs_count
);
865 plugin_set
= bt_plugin_set_create();
867 BT_LOGE_STR("Cannot create empty plugin set.");
871 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
872 enum bt_plugin_status status
;
873 const struct __bt_plugin_descriptor
*descriptor
=
874 descriptors_begin
[i
];
875 struct bt_plugin
*plugin
;
877 if (descriptor
== NULL
) {
881 BT_LOGD("Creating plugin object for plugin: "
882 "name=\"%s\", abi-major=%d, abi-minor=%d",
883 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
885 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
887 * DEBUG-level logging because we're only
888 * _trying_ to open this file as a compatible
889 * Babeltrace plugin: if it's not, it's not an
890 * error. And because this can be tried during
891 * bt_plugin_create_all_from_dir(), it's not
894 BT_LOGD("Unknown ABI major version: abi-major=%d",
899 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
901 BT_LOGE_STR("Cannot create empty shared library handle.");
905 if (shared_lib_handle
&& shared_lib_handle
->path
) {
906 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
909 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
910 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
911 cc_descr_attrs_begin
, cc_descr_attrs_end
);
914 * DEBUG-level logging because we're only
915 * _trying_ to open this file as a compatible
916 * Babeltrace plugin: if it's not, it's not an
917 * error. And because this can be tried during
918 * bt_plugin_create_all_from_dir(), it's not
921 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
926 /* Add to plugin set */
927 bt_plugin_set_add_plugin(plugin_set
, plugin
);
941 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
943 struct bt_plugin_set
*plugin_set
= NULL
;
944 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
945 bt_plugin_so_shared_lib_handle_create(NULL
);
947 if (!shared_lib_handle
) {
951 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
952 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
953 __bt_get_begin_section_plugin_descriptors(),
954 __bt_get_end_section_plugin_descriptors(),
955 __bt_get_begin_section_plugin_descriptor_attributes(),
956 __bt_get_end_section_plugin_descriptor_attributes(),
957 __bt_get_begin_section_component_class_descriptors(),
958 __bt_get_end_section_component_class_descriptors(),
959 __bt_get_begin_section_component_class_descriptor_attributes(),
960 __bt_get_end_section_component_class_descriptor_attributes());
963 BT_PUT(shared_lib_handle
);
969 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
972 struct bt_plugin_set
*plugin_set
= NULL
;
973 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
974 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
975 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
976 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
977 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
978 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
979 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
980 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
981 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
982 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
983 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
984 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
985 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
986 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
987 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
988 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
989 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
990 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
993 BT_LOGW_STR("Invalid parameter: path is NULL.");
997 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
998 path_len
= strlen(path
);
999 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
1000 BT_LOGW("Invalid parameter: path length is too short: "
1001 "path-length=%zu", path_len
);
1007 * Check if the file ends with a known plugin file type suffix (i.e. .so
1010 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
1011 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
1012 LIBTOOL_PLUGIN_SUFFIX_LEN
);
1013 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
1014 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
1015 NATIVE_PLUGIN_SUFFIX_LEN
);
1016 if (!is_shared_object
&& !is_libtool_wrapper
) {
1017 /* Name indicates this is not a plugin file; not an error */
1018 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
1022 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
1023 if (!shared_lib_handle
) {
1024 BT_LOGD_STR("Cannot create shared library handle.");
1028 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1029 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1030 descriptors_begin
= get_begin_section_plugin_descriptors();
1032 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1033 "symbol=\"%s\"", path
,
1034 "__bt_get_begin_section_plugin_descriptors");
1038 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1039 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1040 descriptors_end
= get_end_section_plugin_descriptors();
1042 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1043 "symbol=\"%s\"", path
,
1044 "__bt_get_end_section_plugin_descriptors");
1048 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1049 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1050 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1052 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1053 "symbol=\"%s\"", path
,
1054 "__bt_get_begin_section_plugin_descriptor_attributes");
1057 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1058 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1059 attrs_end
= get_end_section_plugin_descriptor_attributes();
1061 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1062 "symbol=\"%s\"", path
,
1063 "__bt_get_end_section_plugin_descriptor_attributes");
1066 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1067 BT_LOGD("Found section start or end symbol, but not both: "
1068 "path=\"%s\", symbol-start=\"%s\", "
1069 "symbol-end=\"%s\", symbol-start-addr=%p, "
1070 "symbol-end-addr=%p",
1071 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1072 "__bt_get_end_section_plugin_descriptor_attributes",
1073 attrs_begin
, attrs_end
);
1077 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1078 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1079 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1081 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1082 "symbol=\"%s\"", path
,
1083 "__bt_get_begin_section_component_class_descriptors");
1086 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1087 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1088 cc_descriptors_end
= get_end_section_component_class_descriptors();
1090 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1091 "symbol=\"%s\"", path
,
1092 "__bt_get_end_section_component_class_descriptors");
1095 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1096 BT_LOGD("Found section start or end symbol, but not both: "
1097 "path=\"%s\", symbol-start=\"%s\", "
1098 "symbol-end=\"%s\", symbol-start-addr=%p, "
1099 "symbol-end-addr=%p",
1100 path
, "__bt_get_begin_section_component_class_descriptors",
1101 "__bt_get_end_section_component_class_descriptors",
1102 cc_descriptors_begin
, cc_descriptors_end
);
1106 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1107 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1108 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1110 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1111 "symbol=\"%s\"", path
,
1112 "__bt_get_begin_section_component_class_descriptor_attributes");
1115 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1116 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1117 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1119 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1120 "symbol=\"%s\"", path
,
1121 "__bt_get_end_section_component_class_descriptor_attributes");
1124 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1125 BT_LOGD("Found section start or end symbol, but not both: "
1126 "path=\"%s\", symbol-start=\"%s\", "
1127 "symbol-end=\"%s\", symbol-start-addr=%p, "
1128 "symbol-end-addr=%p",
1129 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1130 "__bt_get_end_section_component_class_descriptor_attributes",
1131 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1135 /* Initialize plugin */
1136 BT_LOGD_STR("Initializing plugin object.");
1137 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1138 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1139 cc_descriptors_begin
, cc_descriptors_end
,
1140 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1143 BT_PUT(shared_lib_handle
);
1148 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1151 bt_list_del(&comp_class
->node
);
1152 BT_PUT(comp_class
->so_handle
);
1153 BT_LOGV("Component class destroyed: removed entry from list: "
1154 "comp-cls-addr=%p", comp_class
);
1158 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1159 struct bt_component_class
*comp_class
)
1161 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1163 assert(plugin
->spec_data
);
1164 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1166 bt_list_add(&comp_class
->node
, &component_class_list
);
1167 comp_class
->so_handle
= bt_get(spec
->shared_lib_handle
);
1169 /* Add our custom destroy listener */
1170 bt_component_class_add_destroy_listener(comp_class
,
1171 plugin_comp_class_destroy_listener
, NULL
);