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/object.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>
42 #include <babeltrace/assert-internal.h>
48 #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
49 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
50 #define LIBTOOL_PLUGIN_SUFFIX ".la"
51 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
53 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
54 sizeof(LIBTOOL_PLUGIN_SUFFIX))
59 * This list, global to the library, keeps all component classes that
60 * have a reference to their shared library handles. It allows iteration
61 * on all component classes still present when the destructor executes
62 * to release the shared library handle references they might still have.
64 * The list items are the component classes created with
65 * bt_plugin_add_component_class(). They keep the shared library handle
66 * object created by their plugin alive so that the plugin's code is
67 * not discarded when it could still be in use by living components
68 * created from those component classes:
70 * [component] --ref-> [component class]-> [shlib handle]
72 * It allows this use-case:
74 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
75 * // instantiate components from a plugin's component classes
76 * // put plugins and free my_plugins here
77 * // user code of instantiated components still exists
79 * An entry is removed from this list when a component class is
80 * destroyed thanks to a custom destroy listener. When the entry is
81 * removed, the entry is removed from the list, and we release the
82 * reference on the shlib handle. Assuming the original plugin object
83 * which contained some component classes is put first, when the last
84 * component class is removed from this list, the shared library handle
85 * object's reference count falls to zero and the shared library is
90 BT_LIST_HEAD(component_class_list
);
92 __attribute__((destructor
)) static
93 void fini_comp_class_list(void)
95 struct bt_component_class
*comp_class
, *tmp
;
97 bt_list_for_each_entry_safe(comp_class
, tmp
, &component_class_list
, node
) {
98 bt_list_del(&comp_class
->node
);
99 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
101 BT_LOGD_STR("Released references from all component classes to shared library handles.");
105 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
107 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
110 shared_lib_handle
= container_of(obj
,
111 struct bt_plugin_so_shared_lib_handle
, base
);
112 const char *path
= shared_lib_handle
->path
?
113 shared_lib_handle
->path
->str
: NULL
;
115 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
116 shared_lib_handle
, path
);
118 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
119 enum bt_plugin_status status
;
121 BT_LOGD_STR("Calling user's plugin exit function.");
122 status
= shared_lib_handle
->exit();
123 BT_LOGD("User function returned: %s",
124 bt_plugin_status_string(status
));
127 BT_LOGW("User's plugin exit function failed: "
128 "path=\"%s\"", path
);
132 if (shared_lib_handle
->module
) {
135 * Valgrind shows incomplete stack traces when
136 * dynamically loaded libraries are closed before it
137 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
138 * build to avoid this.
140 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
142 if (!var
|| strcmp(var
, "1") != 0) {
144 BT_LOGD("Closing GModule: path=\"%s\"", path
);
146 if (!g_module_close(shared_lib_handle
->module
)) {
147 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
148 g_module_error(), path
);
152 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
153 "path=\"%s\"", path
);
158 if (shared_lib_handle
->path
) {
159 g_string_free(shared_lib_handle
->path
, TRUE
);
162 g_free(shared_lib_handle
);
166 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
169 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
171 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
172 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
173 if (!shared_lib_handle
) {
174 BT_LOGE_STR("Failed to allocate one shared library handle.");
178 bt_object_init_shared(&shared_lib_handle
->base
,
179 bt_plugin_so_shared_lib_handle_destroy
);
185 shared_lib_handle
->path
= g_string_new(path
);
186 if (!shared_lib_handle
->path
) {
187 BT_LOGE_STR("Failed to allocate a GString.");
191 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
192 if (!shared_lib_handle
->module
) {
194 * DEBUG-level logging because we're only _trying_ to
195 * open this file as a Babeltrace plugin: if it's not,
196 * it's not an error. And because this can be tried
197 * during bt_plugin_create_all_from_dir(), it's not even
200 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
201 g_module_error(), path
);
208 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
211 if (shared_lib_handle
) {
212 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
213 path
, shared_lib_handle
);
216 return shared_lib_handle
;
220 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
222 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
224 if (!plugin
->spec_data
) {
228 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
230 BT_OBJECT_PUT_REF_AND_RESET(spec
->shared_lib_handle
);
231 g_free(plugin
->spec_data
);
232 plugin
->spec_data
= NULL
;
236 * This function does the following:
238 * 1. Iterate on the plugin descriptor attributes section and set the
239 * plugin's attributes depending on the attribute types. This
240 * includes the name of the plugin, its description, and its
241 * initialization function, for example.
243 * 2. Iterate on the component class descriptors section and create one
244 * "full descriptor" (temporary structure) for each one that is found
245 * and attached to our plugin descriptor.
247 * 3. Iterate on the component class descriptor attributes section and
248 * set the corresponding full descriptor's attributes depending on
249 * the attribute types. This includes the description of the
250 * component class, as well as its initialization and destroy
253 * 4. Call the user's plugin initialization function, if any is
256 * 5. For each full component class descriptor, create a component class
257 * object, set its optional attributes, and add it to the plugin
260 * 6. Freeze the plugin object.
263 enum bt_plugin_status
bt_plugin_so_init(
264 struct bt_plugin
*plugin
,
265 const struct __bt_plugin_descriptor
*descriptor
,
266 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
267 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
268 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
269 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
270 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
271 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
274 * This structure's members point to the plugin's memory
277 struct comp_class_full_descriptor
{
278 const struct __bt_plugin_component_class_descriptor
*descriptor
;
279 const char *description
;
281 bt_component_class_init_method init_method
;
282 bt_component_class_finalize_method finalize_method
;
283 bt_component_class_query_method query_method
;
284 bt_component_class_accept_port_connection_method accept_port_connection_method
;
285 bt_component_class_port_connected_method port_connected_method
;
286 bt_component_class_port_disconnected_method port_disconnected_method
;
287 struct bt_component_class_notification_iterator_methods iterator_methods
;
290 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
291 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
292 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
293 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
294 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
295 GArray
*comp_class_full_descriptors
;
299 BT_LOGD("Initializing plugin object from descriptors found in sections: "
300 "plugin-addr=%p, plugin-path=\"%s\", "
301 "attrs-begin-addr=%p, attrs-end-addr=%p, "
302 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
303 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
305 spec
->shared_lib_handle
->path
?
306 spec
->shared_lib_handle
->path
->str
: NULL
,
307 attrs_begin
, attrs_end
,
308 cc_descriptors_begin
, cc_descriptors_end
,
309 cc_descr_attrs_begin
, cc_descr_attrs_end
);
310 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
311 sizeof(struct comp_class_full_descriptor
));
312 if (!comp_class_full_descriptors
) {
313 BT_LOGE_STR("Failed to allocate a GArray.");
314 status
= BT_PLUGIN_STATUS_ERROR
;
318 /* Set mandatory attributes */
319 spec
->descriptor
= descriptor
;
320 bt_plugin_set_name(plugin
, descriptor
->name
);
323 * Find and set optional attributes attached to this plugin
326 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
327 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
330 if (cur_attr
== NULL
) {
334 if (cur_attr
->plugin_descriptor
!= descriptor
) {
338 switch (cur_attr
->type
) {
339 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
340 spec
->init
= cur_attr
->value
.init
;
342 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
343 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
345 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
346 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
348 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
349 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
351 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
352 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
354 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
355 bt_plugin_set_version(plugin
,
356 (unsigned int) cur_attr
->value
.version
.major
,
357 (unsigned int) cur_attr
->value
.version
.minor
,
358 (unsigned int) cur_attr
->value
.version
.patch
,
359 cur_attr
->value
.version
.extra
);
363 * WARN-level logging because this should not
364 * happen with the appropriate ABI version. If
365 * we're here, we know that for the reported
366 * version of the ABI, this attribute is
369 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
370 "plugin-path=\"%s\", plugin-name=\"%s\", "
371 "attr-type-name=\"%s\", attr-type-id=%d",
372 spec
->shared_lib_handle
->path
?
373 spec
->shared_lib_handle
->path
->str
:
375 descriptor
->name
, cur_attr
->type_name
,
382 * Find component class descriptors attached to this plugin
383 * descriptor and initialize corresponding full component class
384 * descriptors in the array.
386 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
387 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
389 struct comp_class_full_descriptor full_descriptor
= {0};
391 if (cur_cc_descr
== NULL
) {
395 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
399 full_descriptor
.descriptor
= cur_cc_descr
;
400 g_array_append_val(comp_class_full_descriptors
,
405 * Find component class descriptor attributes attached to this
406 * plugin descriptor and update corresponding full component
407 * class descriptors in the array.
409 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
410 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
411 *cur_cc_descr_attr_ptr
;
413 if (cur_cc_descr_attr
== NULL
) {
417 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
422 /* Find the corresponding component class descriptor entry */
423 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
424 struct comp_class_full_descriptor
*cc_full_descr
=
425 &g_array_index(comp_class_full_descriptors
,
426 struct comp_class_full_descriptor
, i
);
428 if (cur_cc_descr_attr
->comp_class_descriptor
==
429 cc_full_descr
->descriptor
) {
430 switch (cur_cc_descr_attr
->type
) {
431 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
432 cc_full_descr
->description
=
433 cur_cc_descr_attr
->value
.description
;
435 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
436 cc_full_descr
->help
=
437 cur_cc_descr_attr
->value
.help
;
439 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
440 cc_full_descr
->init_method
=
441 cur_cc_descr_attr
->value
.init_method
;
443 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
444 cc_full_descr
->finalize_method
=
445 cur_cc_descr_attr
->value
.finalize_method
;
447 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
448 cc_full_descr
->query_method
=
449 cur_cc_descr_attr
->value
.query_method
;
451 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
452 cc_full_descr
->accept_port_connection_method
=
453 cur_cc_descr_attr
->value
.accept_port_connection_method
;
455 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD
:
456 cc_full_descr
->port_connected_method
=
457 cur_cc_descr_attr
->value
.port_connected_method
;
459 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
460 cc_full_descr
->port_disconnected_method
=
461 cur_cc_descr_attr
->value
.port_disconnected_method
;
463 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
464 cc_full_descr
->iterator_methods
.init
=
465 cur_cc_descr_attr
->value
.notif_iter_init_method
;
467 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
468 cc_full_descr
->iterator_methods
.finalize
=
469 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
473 * WARN-level logging because
474 * this should not happen with
475 * the appropriate ABI version.
476 * If we're here, we know that
477 * for the reported version of
478 * the ABI, this attribute is
481 BT_LOGW("Ignoring unknown component class descriptor attribute: "
482 "plugin-path=\"%s\", "
483 "plugin-name=\"%s\", "
484 "comp-class-name=\"%s\", "
485 "comp-class-type=%s, "
486 "attr-type-name=\"%s\", "
488 spec
->shared_lib_handle
->path
?
489 spec
->shared_lib_handle
->path
->str
:
492 cur_cc_descr_attr
->comp_class_descriptor
->name
,
493 bt_component_class_type_string(
494 cur_cc_descr_attr
->comp_class_descriptor
->type
),
495 cur_cc_descr_attr
->type_name
,
496 cur_cc_descr_attr
->type
);
503 /* Initialize plugin */
505 BT_LOGD_STR("Calling user's plugin initialization function.");
506 status
= spec
->init(plugin
);
507 BT_LOGD("User function returned: %s",
508 bt_plugin_status_string(status
));
511 BT_LOGW_STR("User's plugin initialization function failed.");
516 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
518 /* Add described component classes to plugin */
519 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
520 struct comp_class_full_descriptor
*cc_full_descr
=
521 &g_array_index(comp_class_full_descriptors
,
522 struct comp_class_full_descriptor
, i
);
523 struct bt_component_class
*comp_class
;
525 BT_LOGD("Creating and setting properties of plugin's component class: "
526 "plugin-path=\"%s\", plugin-name=\"%s\", "
527 "comp-class-name=\"%s\", comp-class-type=%s",
528 spec
->shared_lib_handle
->path
?
529 spec
->shared_lib_handle
->path
->str
:
532 cc_full_descr
->descriptor
->name
,
533 bt_component_class_type_string(
534 cc_full_descr
->descriptor
->type
));
536 switch (cc_full_descr
->descriptor
->type
) {
537 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
538 comp_class
= bt_component_class_source_create(
539 cc_full_descr
->descriptor
->name
,
540 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
542 case BT_COMPONENT_CLASS_TYPE_FILTER
:
543 comp_class
= bt_component_class_filter_create(
544 cc_full_descr
->descriptor
->name
,
545 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
547 case BT_COMPONENT_CLASS_TYPE_SINK
:
548 comp_class
= bt_component_class_sink_create(
549 cc_full_descr
->descriptor
->name
,
550 cc_full_descr
->descriptor
->methods
.sink
.consume
);
554 * WARN-level logging because this should not
555 * happen with the appropriate ABI version. If
556 * we're here, we know that for the reported
557 * version of the ABI, this component class type
560 BT_LOGW("Ignoring unknown component class type: "
561 "plugin-path=\"%s\", plugin-name=\"%s\", "
562 "comp-class-name=\"%s\", comp-class-type=%d",
563 spec
->shared_lib_handle
->path
->str
?
564 spec
->shared_lib_handle
->path
->str
:
567 cc_full_descr
->descriptor
->name
,
568 cc_full_descr
->descriptor
->type
);
573 BT_LOGE_STR("Cannot create component class.");
574 status
= BT_PLUGIN_STATUS_ERROR
;
578 if (cc_full_descr
->description
) {
579 ret
= bt_component_class_set_description(comp_class
,
580 cc_full_descr
->description
);
582 BT_LOGE_STR("Cannot set component class's description.");
583 status
= BT_PLUGIN_STATUS_ERROR
;
584 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
589 if (cc_full_descr
->help
) {
590 ret
= bt_component_class_set_help(comp_class
,
591 cc_full_descr
->help
);
593 BT_LOGE_STR("Cannot set component class's help string.");
594 status
= BT_PLUGIN_STATUS_ERROR
;
595 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
600 if (cc_full_descr
->init_method
) {
601 ret
= bt_component_class_set_init_method(comp_class
,
602 cc_full_descr
->init_method
);
604 BT_LOGE_STR("Cannot set component class's initialization method.");
605 status
= BT_PLUGIN_STATUS_ERROR
;
606 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
611 if (cc_full_descr
->finalize_method
) {
612 ret
= bt_component_class_set_finalize_method(comp_class
,
613 cc_full_descr
->finalize_method
);
615 BT_LOGE_STR("Cannot set component class's finalization method.");
616 status
= BT_PLUGIN_STATUS_ERROR
;
617 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
622 if (cc_full_descr
->query_method
) {
623 ret
= bt_component_class_set_query_method(
624 comp_class
, cc_full_descr
->query_method
);
626 BT_LOGE_STR("Cannot set component class's query method.");
627 status
= BT_PLUGIN_STATUS_ERROR
;
628 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
633 if (cc_full_descr
->accept_port_connection_method
) {
634 ret
= bt_component_class_set_accept_port_connection_method(
635 comp_class
, cc_full_descr
->accept_port_connection_method
);
637 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
638 status
= BT_PLUGIN_STATUS_ERROR
;
639 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
644 if (cc_full_descr
->port_connected_method
) {
645 ret
= bt_component_class_set_port_connected_method(
646 comp_class
, cc_full_descr
->port_connected_method
);
648 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
649 status
= BT_PLUGIN_STATUS_ERROR
;
650 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
655 if (cc_full_descr
->port_disconnected_method
) {
656 ret
= bt_component_class_set_port_disconnected_method(
657 comp_class
, cc_full_descr
->port_disconnected_method
);
659 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
660 status
= BT_PLUGIN_STATUS_ERROR
;
661 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
666 switch (cc_full_descr
->descriptor
->type
) {
667 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
668 if (cc_full_descr
->iterator_methods
.init
) {
669 ret
= bt_component_class_source_set_notification_iterator_init_method(
671 cc_full_descr
->iterator_methods
.init
);
673 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
674 status
= BT_PLUGIN_STATUS_ERROR
;
675 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
680 if (cc_full_descr
->iterator_methods
.finalize
) {
681 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
683 cc_full_descr
->iterator_methods
.finalize
);
685 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
686 status
= BT_PLUGIN_STATUS_ERROR
;
687 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
692 case BT_COMPONENT_CLASS_TYPE_FILTER
:
693 if (cc_full_descr
->iterator_methods
.init
) {
694 ret
= bt_component_class_filter_set_notification_iterator_init_method(
696 cc_full_descr
->iterator_methods
.init
);
698 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
699 status
= BT_PLUGIN_STATUS_ERROR
;
700 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
705 if (cc_full_descr
->iterator_methods
.finalize
) {
706 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
708 cc_full_descr
->iterator_methods
.finalize
);
710 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
711 status
= BT_PLUGIN_STATUS_ERROR
;
712 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
717 case BT_COMPONENT_CLASS_TYPE_SINK
:
724 * Add component class to the plugin object.
726 * This will call back
727 * bt_plugin_so_on_add_component_class() so that we can
728 * add a mapping in the component class list when
729 * we know the component class is successfully added.
731 status
= bt_plugin_add_component_class(plugin
,
733 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
735 BT_LOGE("Cannot add component class to plugin.");
741 * All the plugin's component classes should be added at this
742 * point. We freeze the plugin so that it's not possible to add
743 * component classes to this plugin object after this stage
744 * (plugin object becomes immutable).
746 bt_plugin_freeze(plugin
);
749 g_array_free(comp_class_full_descriptors
, TRUE
);
754 struct bt_plugin
*bt_plugin_so_create_empty(
755 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
757 struct bt_plugin
*plugin
;
758 struct bt_plugin_so_spec_data
*spec
;
760 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
765 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
766 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
767 if (!plugin
->spec_data
) {
768 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
772 spec
= plugin
->spec_data
;
773 spec
->shared_lib_handle
= bt_object_get_ref(shared_lib_handle
);
777 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
784 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
787 const int * const *begin_int
= (const int * const *) begin
;
788 const int * const *end_int
= (const int * const *) end
;
789 const int * const *iter
;
791 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
801 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
802 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
803 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
804 struct __bt_plugin_descriptor
const * const *descriptors_end
,
805 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
806 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
807 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
808 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
809 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
810 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
812 size_t descriptor_count
;
814 size_t cc_descriptors_count
;
815 size_t cc_descr_attrs_count
;
817 struct bt_plugin_set
*plugin_set
= NULL
;
819 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
820 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
821 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
822 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
824 BT_LOGD("Creating all SO plugins from sections: "
825 "plugin-path=\"%s\", "
826 "descr-begin-addr=%p, descr-end-addr=%p, "
827 "attrs-begin-addr=%p, attrs-end-addr=%p, "
828 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
829 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
830 "descr-count=%zu, attrs-count=%zu, "
831 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
832 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
833 descriptors_begin
, descriptors_end
,
834 attrs_begin
, attrs_end
,
835 cc_descriptors_begin
, cc_descriptors_end
,
836 cc_descr_attrs_begin
, cc_descr_attrs_end
,
837 descriptor_count
, attrs_count
,
838 cc_descriptors_count
, cc_descr_attrs_count
);
839 plugin_set
= bt_plugin_set_create();
841 BT_LOGE_STR("Cannot create empty plugin set.");
845 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
846 enum bt_plugin_status status
;
847 const struct __bt_plugin_descriptor
*descriptor
=
848 descriptors_begin
[i
];
849 struct bt_plugin
*plugin
;
851 if (descriptor
== NULL
) {
855 BT_LOGD("Creating plugin object for plugin: "
856 "name=\"%s\", abi-major=%d, abi-minor=%d",
857 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
859 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
861 * DEBUG-level logging because we're only
862 * _trying_ to open this file as a compatible
863 * Babeltrace plugin: if it's not, it's not an
864 * error. And because this can be tried during
865 * bt_plugin_create_all_from_dir(), it's not
868 BT_LOGD("Unknown ABI major version: abi-major=%d",
873 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
875 BT_LOGE_STR("Cannot create empty shared library handle.");
879 if (shared_lib_handle
&& shared_lib_handle
->path
) {
880 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
883 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
884 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
885 cc_descr_attrs_begin
, cc_descr_attrs_end
);
888 * DEBUG-level logging because we're only
889 * _trying_ to open this file as a compatible
890 * Babeltrace plugin: if it's not, it's not an
891 * error. And because this can be tried during
892 * bt_plugin_create_all_from_dir(), it's not
895 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
896 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
900 /* Add to plugin set */
901 bt_plugin_set_add_plugin(plugin_set
, plugin
);
902 bt_object_put_ref(plugin
);
908 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
915 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
917 struct bt_plugin_set
*plugin_set
= NULL
;
918 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
919 bt_plugin_so_shared_lib_handle_create(NULL
);
921 if (!shared_lib_handle
) {
925 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
926 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
927 __bt_get_begin_section_plugin_descriptors(),
928 __bt_get_end_section_plugin_descriptors(),
929 __bt_get_begin_section_plugin_descriptor_attributes(),
930 __bt_get_end_section_plugin_descriptor_attributes(),
931 __bt_get_begin_section_component_class_descriptors(),
932 __bt_get_end_section_component_class_descriptors(),
933 __bt_get_begin_section_component_class_descriptor_attributes(),
934 __bt_get_end_section_component_class_descriptor_attributes());
937 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
943 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
946 struct bt_plugin_set
*plugin_set
= NULL
;
947 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
948 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
949 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
950 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
951 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
952 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
953 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
954 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
955 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
956 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
957 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
958 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
959 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
960 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
961 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
962 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
963 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
964 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
967 BT_LOGW_STR("Invalid parameter: path is NULL.");
971 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
972 path_len
= strlen(path
);
973 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
974 BT_LOGW("Invalid parameter: path length is too short: "
975 "path-length=%zu", path_len
);
981 * Check if the file ends with a known plugin file type suffix (i.e. .so
984 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
985 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
986 LIBTOOL_PLUGIN_SUFFIX_LEN
);
987 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
988 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
989 NATIVE_PLUGIN_SUFFIX_LEN
);
990 if (!is_shared_object
&& !is_libtool_wrapper
) {
991 /* Name indicates this is not a plugin file; not an error */
992 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
996 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
997 if (!shared_lib_handle
) {
998 BT_LOGD_STR("Cannot create shared library handle.");
1002 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1003 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1004 descriptors_begin
= get_begin_section_plugin_descriptors();
1006 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1007 "symbol=\"%s\"", path
,
1008 "__bt_get_begin_section_plugin_descriptors");
1012 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1013 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1014 descriptors_end
= get_end_section_plugin_descriptors();
1016 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1017 "symbol=\"%s\"", path
,
1018 "__bt_get_end_section_plugin_descriptors");
1022 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1023 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1024 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1026 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1027 "symbol=\"%s\"", path
,
1028 "__bt_get_begin_section_plugin_descriptor_attributes");
1031 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1032 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1033 attrs_end
= get_end_section_plugin_descriptor_attributes();
1035 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1036 "symbol=\"%s\"", path
,
1037 "__bt_get_end_section_plugin_descriptor_attributes");
1040 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1041 BT_LOGD("Found section start or end symbol, but not both: "
1042 "path=\"%s\", symbol-start=\"%s\", "
1043 "symbol-end=\"%s\", symbol-start-addr=%p, "
1044 "symbol-end-addr=%p",
1045 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1046 "__bt_get_end_section_plugin_descriptor_attributes",
1047 attrs_begin
, attrs_end
);
1051 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1052 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1053 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1055 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1056 "symbol=\"%s\"", path
,
1057 "__bt_get_begin_section_component_class_descriptors");
1060 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1061 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1062 cc_descriptors_end
= get_end_section_component_class_descriptors();
1064 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1065 "symbol=\"%s\"", path
,
1066 "__bt_get_end_section_component_class_descriptors");
1069 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1070 BT_LOGD("Found section start or end symbol, but not both: "
1071 "path=\"%s\", symbol-start=\"%s\", "
1072 "symbol-end=\"%s\", symbol-start-addr=%p, "
1073 "symbol-end-addr=%p",
1074 path
, "__bt_get_begin_section_component_class_descriptors",
1075 "__bt_get_end_section_component_class_descriptors",
1076 cc_descriptors_begin
, cc_descriptors_end
);
1080 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1081 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1082 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1084 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1085 "symbol=\"%s\"", path
,
1086 "__bt_get_begin_section_component_class_descriptor_attributes");
1089 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1090 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1091 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1093 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1094 "symbol=\"%s\"", path
,
1095 "__bt_get_end_section_component_class_descriptor_attributes");
1098 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1099 BT_LOGD("Found section start or end symbol, but not both: "
1100 "path=\"%s\", symbol-start=\"%s\", "
1101 "symbol-end=\"%s\", symbol-start-addr=%p, "
1102 "symbol-end-addr=%p",
1103 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1104 "__bt_get_end_section_component_class_descriptor_attributes",
1105 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1109 /* Initialize plugin */
1110 BT_LOGD_STR("Initializing plugin object.");
1111 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1112 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1113 cc_descriptors_begin
, cc_descriptors_end
,
1114 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1117 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1122 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1125 bt_list_del(&comp_class
->node
);
1126 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
1127 BT_LOGV("Component class destroyed: removed entry from list: "
1128 "comp-cls-addr=%p", comp_class
);
1132 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1133 struct bt_component_class
*comp_class
)
1135 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1137 BT_ASSERT(plugin
->spec_data
);
1138 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1140 bt_list_add(&comp_class
->node
, &component_class_list
);
1141 comp_class
->so_handle
= bt_object_get_ref(spec
->shared_lib_handle
);
1143 /* Add our custom destroy listener */
1144 bt_component_class_add_destroy_listener(comp_class
,
1145 plugin_comp_class_destroy_listener
, NULL
);