2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_LOG_TAG "PLUGIN-SO"
27 #include <babeltrace/lib-logging-internal.h>
29 #include <babeltrace/assert-internal.h>
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/plugin/plugin-internal.h>
33 #include <babeltrace/plugin/plugin-so-internal.h>
34 #include <babeltrace/plugin/plugin-dev.h>
35 #include <babeltrace/plugin/plugin-internal.h>
36 #include <babeltrace/graph/component-class-internal.h>
37 #include <babeltrace/graph/component-class.h>
38 #include <babeltrace/graph/component-class-source.h>
39 #include <babeltrace/graph/component-class-filter.h>
40 #include <babeltrace/graph/component-class-sink.h>
41 #include <babeltrace/types.h>
42 #include <babeltrace/list-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_find_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
);
102 BT_LOGD_STR("Released references from all component classes to shared library handles.");
106 const char *bt_self_plugin_status_string(enum bt_self_plugin_status status
)
109 case BT_SELF_PLUGIN_STATUS_OK
:
110 return "BT_SELF_PLUGIN_STATUS_OK";
111 case BT_SELF_PLUGIN_STATUS_ERROR
:
112 return "BT_SELF_PLUGIN_STATUS_ERROR";
113 case BT_SELF_PLUGIN_STATUS_NOMEM
:
114 return "BT_SELF_PLUGIN_STATUS_NOMEM";
121 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
123 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
126 shared_lib_handle
= container_of(obj
,
127 struct bt_plugin_so_shared_lib_handle
, base
);
128 const char *path
= shared_lib_handle
->path
?
129 shared_lib_handle
->path
->str
: NULL
;
131 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
132 shared_lib_handle
, path
);
134 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
135 BT_LOGD_STR("Calling user's plugin exit function.");
136 shared_lib_handle
->exit();
137 BT_LOGD_STR("User function returned.");
140 if (shared_lib_handle
->module
) {
143 * Valgrind shows incomplete stack traces when
144 * dynamically loaded libraries are closed before it
145 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
146 * build to avoid this.
148 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
150 if (!var
|| strcmp(var
, "1") != 0) {
152 BT_LOGD("Closing GModule: path=\"%s\"", path
);
154 if (!g_module_close(shared_lib_handle
->module
)) {
155 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
156 g_module_error(), path
);
159 shared_lib_handle
->module
= NULL
;
162 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
163 "path=\"%s\"", path
);
168 if (shared_lib_handle
->path
) {
169 g_string_free(shared_lib_handle
->path
, TRUE
);
170 shared_lib_handle
->path
= NULL
;
173 g_free(shared_lib_handle
);
177 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
180 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
182 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
183 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
184 if (!shared_lib_handle
) {
185 BT_LOGE_STR("Failed to allocate one shared library handle.");
189 bt_object_init_shared(&shared_lib_handle
->base
,
190 bt_plugin_so_shared_lib_handle_destroy
);
196 shared_lib_handle
->path
= g_string_new(path
);
197 if (!shared_lib_handle
->path
) {
198 BT_LOGE_STR("Failed to allocate a GString.");
202 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
203 if (!shared_lib_handle
->module
) {
205 * DEBUG-level logging because we're only _trying_ to
206 * open this file as a Babeltrace plugin: if it's not,
207 * it's not an error. And because this can be tried
208 * during bt_plugin_find_all_from_dir(), it's not even
211 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
212 g_module_error(), path
);
219 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
222 if (shared_lib_handle
) {
223 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
224 path
, shared_lib_handle
);
227 return shared_lib_handle
;
231 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
233 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
235 if (!plugin
->spec_data
) {
239 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
241 BT_OBJECT_PUT_REF_AND_RESET(spec
->shared_lib_handle
);
242 g_free(plugin
->spec_data
);
243 plugin
->spec_data
= NULL
;
247 * This function does the following:
249 * 1. Iterate on the plugin descriptor attributes section and set the
250 * plugin's attributes depending on the attribute types. This
251 * includes the name of the plugin, its description, and its
252 * initialization function, for example.
254 * 2. Iterate on the component class descriptors section and create one
255 * "full descriptor" (temporary structure) for each one that is found
256 * and attached to our plugin descriptor.
258 * 3. Iterate on the component class descriptor attributes section and
259 * set the corresponding full descriptor's attributes depending on
260 * the attribute types. This includes the description of the
261 * component class, as well as its initialization and destroy
264 * 4. Call the user's plugin initialization function, if any is
267 * 5. For each full component class descriptor, create a component class
268 * object, set its optional attributes, and add it to the plugin
271 * 6. Freeze the plugin object.
274 enum bt_plugin_status
bt_plugin_so_init(
275 struct bt_plugin
*plugin
,
276 const struct __bt_plugin_descriptor
*descriptor
,
277 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
278 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
279 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
280 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
281 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
282 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
285 * This structure's members point to the plugin's memory
288 struct comp_class_full_descriptor
{
289 const struct __bt_plugin_component_class_descriptor
*descriptor
;
290 const char *description
;
295 bt_component_class_source_init_method init
;
296 bt_component_class_source_finalize_method finalize
;
297 bt_component_class_source_query_method query
;
298 bt_component_class_source_accept_output_port_connection_method accept_output_port_connection
;
299 bt_component_class_source_output_port_connected_method output_port_connected
;
300 bt_component_class_source_output_port_disconnected_method output_port_disconnected
;
301 bt_component_class_source_message_iterator_init_method msg_iter_init
;
302 bt_component_class_source_message_iterator_finalize_method msg_iter_finalize
;
306 bt_component_class_filter_init_method init
;
307 bt_component_class_filter_finalize_method finalize
;
308 bt_component_class_filter_query_method query
;
309 bt_component_class_filter_accept_input_port_connection_method accept_input_port_connection
;
310 bt_component_class_filter_accept_output_port_connection_method accept_output_port_connection
;
311 bt_component_class_filter_input_port_connected_method input_port_connected
;
312 bt_component_class_filter_output_port_connected_method output_port_connected
;
313 bt_component_class_filter_input_port_disconnected_method input_port_disconnected
;
314 bt_component_class_filter_output_port_disconnected_method output_port_disconnected
;
315 bt_component_class_filter_message_iterator_init_method msg_iter_init
;
316 bt_component_class_filter_message_iterator_finalize_method msg_iter_finalize
;
320 bt_component_class_sink_init_method init
;
321 bt_component_class_sink_finalize_method finalize
;
322 bt_component_class_sink_query_method query
;
323 bt_component_class_sink_accept_input_port_connection_method accept_input_port_connection
;
324 bt_component_class_sink_input_port_connected_method input_port_connected
;
325 bt_component_class_sink_input_port_disconnected_method input_port_disconnected
;
330 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
331 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
332 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
333 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
334 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
335 GArray
*comp_class_full_descriptors
;
339 BT_LOGD("Initializing plugin object from descriptors found in sections: "
340 "plugin-addr=%p, plugin-path=\"%s\", "
341 "attrs-begin-addr=%p, attrs-end-addr=%p, "
342 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
343 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
345 spec
->shared_lib_handle
->path
?
346 spec
->shared_lib_handle
->path
->str
: NULL
,
347 attrs_begin
, attrs_end
,
348 cc_descriptors_begin
, cc_descriptors_end
,
349 cc_descr_attrs_begin
, cc_descr_attrs_end
);
350 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
351 sizeof(struct comp_class_full_descriptor
));
352 if (!comp_class_full_descriptors
) {
353 BT_LOGE_STR("Failed to allocate a GArray.");
354 status
= BT_PLUGIN_STATUS_ERROR
;
358 /* Set mandatory attributes */
359 spec
->descriptor
= descriptor
;
360 bt_plugin_set_name(plugin
, descriptor
->name
);
363 * Find and set optional attributes attached to this plugin
366 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
367 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
370 if (cur_attr
== NULL
) {
374 if (cur_attr
->plugin_descriptor
!= descriptor
) {
378 switch (cur_attr
->type
) {
379 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
380 spec
->init
= cur_attr
->value
.init
;
382 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
383 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
385 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
386 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
388 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
389 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
391 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
392 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
394 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
395 bt_plugin_set_version(plugin
,
396 (unsigned int) cur_attr
->value
.version
.major
,
397 (unsigned int) cur_attr
->value
.version
.minor
,
398 (unsigned int) cur_attr
->value
.version
.patch
,
399 cur_attr
->value
.version
.extra
);
403 * WARN-level logging because this should not
404 * happen with the appropriate ABI version. If
405 * we're here, we know that for the reported
406 * version of the ABI, this attribute is
409 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
410 "plugin-path=\"%s\", plugin-name=\"%s\", "
411 "attr-type-name=\"%s\", attr-type-id=%d",
412 spec
->shared_lib_handle
->path
?
413 spec
->shared_lib_handle
->path
->str
:
415 descriptor
->name
, cur_attr
->type_name
,
422 * Find component class descriptors attached to this plugin
423 * descriptor and initialize corresponding full component class
424 * descriptors in the array.
426 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
427 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
429 struct comp_class_full_descriptor full_descriptor
= {0};
431 if (cur_cc_descr
== NULL
) {
435 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
439 full_descriptor
.descriptor
= cur_cc_descr
;
440 g_array_append_val(comp_class_full_descriptors
,
445 * Find component class descriptor attributes attached to this
446 * plugin descriptor and update corresponding full component
447 * class descriptors in the array.
449 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
450 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
451 *cur_cc_descr_attr_ptr
;
452 enum bt_component_class_type cc_type
;
454 if (cur_cc_descr_attr
== NULL
) {
458 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
463 cc_type
= cur_cc_descr_attr
->comp_class_descriptor
->type
;
465 /* Find the corresponding component class descriptor entry */
466 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
467 struct comp_class_full_descriptor
*cc_full_descr
=
468 &g_array_index(comp_class_full_descriptors
,
469 struct comp_class_full_descriptor
, i
);
471 if (cur_cc_descr_attr
->comp_class_descriptor
!=
472 cc_full_descr
->descriptor
) {
476 switch (cur_cc_descr_attr
->type
) {
477 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
478 cc_full_descr
->description
=
479 cur_cc_descr_attr
->value
.description
;
481 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
482 cc_full_descr
->help
=
483 cur_cc_descr_attr
->value
.help
;
485 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
487 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
488 cc_full_descr
->methods
.source
.init
=
489 cur_cc_descr_attr
->value
.source_init_method
;
491 case BT_COMPONENT_CLASS_TYPE_FILTER
:
492 cc_full_descr
->methods
.filter
.init
=
493 cur_cc_descr_attr
->value
.filter_init_method
;
495 case BT_COMPONENT_CLASS_TYPE_SINK
:
496 cc_full_descr
->methods
.sink
.init
=
497 cur_cc_descr_attr
->value
.sink_init_method
;
503 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
505 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
506 cc_full_descr
->methods
.source
.finalize
=
507 cur_cc_descr_attr
->value
.source_finalize_method
;
509 case BT_COMPONENT_CLASS_TYPE_FILTER
:
510 cc_full_descr
->methods
.filter
.finalize
=
511 cur_cc_descr_attr
->value
.filter_finalize_method
;
513 case BT_COMPONENT_CLASS_TYPE_SINK
:
514 cc_full_descr
->methods
.sink
.finalize
=
515 cur_cc_descr_attr
->value
.sink_finalize_method
;
521 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
523 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
524 cc_full_descr
->methods
.source
.query
=
525 cur_cc_descr_attr
->value
.source_query_method
;
527 case BT_COMPONENT_CLASS_TYPE_FILTER
:
528 cc_full_descr
->methods
.filter
.query
=
529 cur_cc_descr_attr
->value
.filter_query_method
;
531 case BT_COMPONENT_CLASS_TYPE_SINK
:
532 cc_full_descr
->methods
.sink
.query
=
533 cur_cc_descr_attr
->value
.sink_query_method
;
539 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_INPUT_PORT_CONNECTION_METHOD
:
541 case BT_COMPONENT_CLASS_TYPE_FILTER
:
542 cc_full_descr
->methods
.filter
.accept_input_port_connection
=
543 cur_cc_descr_attr
->value
.filter_accept_input_port_connection_method
;
545 case BT_COMPONENT_CLASS_TYPE_SINK
:
546 cc_full_descr
->methods
.sink
.accept_input_port_connection
=
547 cur_cc_descr_attr
->value
.sink_accept_input_port_connection_method
;
553 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_OUTPUT_PORT_CONNECTION_METHOD
:
555 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
556 cc_full_descr
->methods
.source
.accept_output_port_connection
=
557 cur_cc_descr_attr
->value
.source_accept_output_port_connection_method
;
559 case BT_COMPONENT_CLASS_TYPE_FILTER
:
560 cc_full_descr
->methods
.filter
.accept_output_port_connection
=
561 cur_cc_descr_attr
->value
.filter_accept_output_port_connection_method
;
567 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD
:
569 case BT_COMPONENT_CLASS_TYPE_FILTER
:
570 cc_full_descr
->methods
.filter
.input_port_connected
=
571 cur_cc_descr_attr
->value
.filter_input_port_connected_method
;
573 case BT_COMPONENT_CLASS_TYPE_SINK
:
574 cc_full_descr
->methods
.sink
.input_port_connected
=
575 cur_cc_descr_attr
->value
.sink_input_port_connected_method
;
581 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD
:
583 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
584 cc_full_descr
->methods
.source
.output_port_connected
=
585 cur_cc_descr_attr
->value
.source_output_port_connected_method
;
587 case BT_COMPONENT_CLASS_TYPE_FILTER
:
588 cc_full_descr
->methods
.filter
.output_port_connected
=
589 cur_cc_descr_attr
->value
.filter_output_port_connected_method
;
595 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_DISCONNECTED_METHOD
:
597 case BT_COMPONENT_CLASS_TYPE_FILTER
:
598 cc_full_descr
->methods
.filter
.input_port_disconnected
=
599 cur_cc_descr_attr
->value
.filter_input_port_disconnected_method
;
601 case BT_COMPONENT_CLASS_TYPE_SINK
:
602 cc_full_descr
->methods
.sink
.input_port_disconnected
=
603 cur_cc_descr_attr
->value
.sink_input_port_disconnected_method
;
609 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_DISCONNECTED_METHOD
:
611 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
612 cc_full_descr
->methods
.source
.output_port_disconnected
=
613 cur_cc_descr_attr
->value
.source_output_port_disconnected_method
;
615 case BT_COMPONENT_CLASS_TYPE_FILTER
:
616 cc_full_descr
->methods
.filter
.output_port_disconnected
=
617 cur_cc_descr_attr
->value
.filter_output_port_disconnected_method
;
623 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INIT_METHOD
:
625 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
626 cc_full_descr
->methods
.source
.msg_iter_init
=
627 cur_cc_descr_attr
->value
.source_msg_iter_init_method
;
629 case BT_COMPONENT_CLASS_TYPE_FILTER
:
630 cc_full_descr
->methods
.filter
.msg_iter_init
=
631 cur_cc_descr_attr
->value
.filter_msg_iter_init_method
;
637 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD
:
639 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
640 cc_full_descr
->methods
.source
.msg_iter_finalize
=
641 cur_cc_descr_attr
->value
.source_msg_iter_finalize_method
;
643 case BT_COMPONENT_CLASS_TYPE_FILTER
:
644 cc_full_descr
->methods
.filter
.msg_iter_finalize
=
645 cur_cc_descr_attr
->value
.filter_msg_iter_finalize_method
;
653 * WARN-level logging because this
654 * should not happen with the
655 * appropriate ABI version. If we're
656 * here, we know that for the reported
657 * version of the ABI, this attribute is
660 BT_LOGW("Ignoring unknown component class descriptor attribute: "
661 "plugin-path=\"%s\", "
662 "plugin-name=\"%s\", "
663 "comp-class-name=\"%s\", "
664 "comp-class-type=%s, "
665 "attr-type-name=\"%s\", "
667 spec
->shared_lib_handle
->path
?
668 spec
->shared_lib_handle
->path
->str
:
671 cur_cc_descr_attr
->comp_class_descriptor
->name
,
672 bt_component_class_type_string(
673 cur_cc_descr_attr
->comp_class_descriptor
->type
),
674 cur_cc_descr_attr
->type_name
,
675 cur_cc_descr_attr
->type
);
681 /* Initialize plugin */
683 enum bt_self_plugin_status init_status
;
685 BT_LOGD_STR("Calling user's plugin initialization function.");
686 init_status
= spec
->init((void *) plugin
);
687 BT_LOGD("User function returned: %s",
688 bt_self_plugin_status_string(init_status
));
690 if (init_status
< 0) {
691 BT_LOGW_STR("User's plugin initialization function failed.");
692 status
= BT_PLUGIN_STATUS_ERROR
;
697 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
699 /* Add described component classes to plugin */
700 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
701 struct comp_class_full_descriptor
*cc_full_descr
=
702 &g_array_index(comp_class_full_descriptors
,
703 struct comp_class_full_descriptor
, i
);
704 struct bt_component_class
*comp_class
= NULL
;
705 struct bt_component_class_source
*src_comp_class
= NULL
;
706 struct bt_component_class_filter
*flt_comp_class
= NULL
;
707 struct bt_component_class_sink
*sink_comp_class
= NULL
;
709 BT_LOGD("Creating and setting properties of plugin's component class: "
710 "plugin-path=\"%s\", plugin-name=\"%s\", "
711 "comp-class-name=\"%s\", comp-class-type=%s",
712 spec
->shared_lib_handle
->path
?
713 spec
->shared_lib_handle
->path
->str
:
716 cc_full_descr
->descriptor
->name
,
717 bt_component_class_type_string(
718 cc_full_descr
->descriptor
->type
));
720 switch (cc_full_descr
->descriptor
->type
) {
721 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
722 src_comp_class
= bt_component_class_source_create(
723 cc_full_descr
->descriptor
->name
,
724 cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
);
725 comp_class
= bt_component_class_source_as_component_class(
728 case BT_COMPONENT_CLASS_TYPE_FILTER
:
729 flt_comp_class
= bt_component_class_filter_create(
730 cc_full_descr
->descriptor
->name
,
731 cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
);
732 comp_class
= bt_component_class_filter_as_component_class(
735 case BT_COMPONENT_CLASS_TYPE_SINK
:
736 sink_comp_class
= bt_component_class_sink_create(
737 cc_full_descr
->descriptor
->name
,
738 cc_full_descr
->descriptor
->methods
.sink
.consume
);
739 comp_class
= bt_component_class_sink_as_component_class(
744 * WARN-level logging because this should not
745 * happen with the appropriate ABI version. If
746 * we're here, we know that for the reported
747 * version of the ABI, this component class type
750 BT_LOGW("Ignoring unknown component class type: "
751 "plugin-path=\"%s\", plugin-name=\"%s\", "
752 "comp-class-name=\"%s\", comp-class-type=%d",
753 spec
->shared_lib_handle
->path
->str
?
754 spec
->shared_lib_handle
->path
->str
:
757 cc_full_descr
->descriptor
->name
,
758 cc_full_descr
->descriptor
->type
);
763 BT_LOGE_STR("Cannot create component class.");
764 status
= BT_PLUGIN_STATUS_ERROR
;
768 if (cc_full_descr
->description
) {
769 ret
= bt_component_class_set_description(
770 comp_class
, cc_full_descr
->description
);
772 BT_LOGE_STR("Cannot set component class's description.");
773 status
= BT_PLUGIN_STATUS_ERROR
;
774 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
779 if (cc_full_descr
->help
) {
780 ret
= bt_component_class_set_help(comp_class
,
781 cc_full_descr
->help
);
783 BT_LOGE_STR("Cannot set component class's help string.");
784 status
= BT_PLUGIN_STATUS_ERROR
;
785 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
790 switch (cc_full_descr
->descriptor
->type
) {
791 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
792 if (cc_full_descr
->methods
.source
.init
) {
793 ret
= bt_component_class_source_set_init_method(
795 cc_full_descr
->methods
.source
.init
);
797 BT_LOGE_STR("Cannot set source component class's initialization method.");
798 status
= BT_PLUGIN_STATUS_ERROR
;
799 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
804 if (cc_full_descr
->methods
.source
.finalize
) {
805 ret
= bt_component_class_source_set_finalize_method(
807 cc_full_descr
->methods
.source
.finalize
);
809 BT_LOGE_STR("Cannot set source component class's finalization method.");
810 status
= BT_PLUGIN_STATUS_ERROR
;
811 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
816 if (cc_full_descr
->methods
.source
.query
) {
817 ret
= bt_component_class_source_set_query_method(
819 cc_full_descr
->methods
.source
.query
);
821 BT_LOGE_STR("Cannot set source component class's query method.");
822 status
= BT_PLUGIN_STATUS_ERROR
;
823 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
828 if (cc_full_descr
->methods
.source
.accept_output_port_connection
) {
829 ret
= bt_component_class_source_set_accept_output_port_connection_method(
831 cc_full_descr
->methods
.source
.accept_output_port_connection
);
833 BT_LOGE_STR("Cannot set source component class's \"accept input output connection\" method.");
834 status
= BT_PLUGIN_STATUS_ERROR
;
835 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
840 if (cc_full_descr
->methods
.source
.output_port_connected
) {
841 ret
= bt_component_class_source_set_output_port_connected_method(
843 cc_full_descr
->methods
.source
.output_port_connected
);
845 BT_LOGE_STR("Cannot set source component class's \"output port connected\" method.");
846 status
= BT_PLUGIN_STATUS_ERROR
;
847 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
852 if (cc_full_descr
->methods
.source
.output_port_disconnected
) {
853 ret
= bt_component_class_source_set_output_port_disconnected_method(
855 cc_full_descr
->methods
.source
.output_port_disconnected
);
857 BT_LOGE_STR("Cannot set source component class's \"output port disconnected\" method.");
858 status
= BT_PLUGIN_STATUS_ERROR
;
859 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
864 if (cc_full_descr
->methods
.source
.msg_iter_init
) {
865 ret
= bt_component_class_source_set_message_iterator_init_method(
867 cc_full_descr
->methods
.source
.msg_iter_init
);
869 BT_LOGE_STR("Cannot set source component class's message iterator initialization method.");
870 status
= BT_PLUGIN_STATUS_ERROR
;
871 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
876 if (cc_full_descr
->methods
.source
.msg_iter_finalize
) {
877 ret
= bt_component_class_source_set_message_iterator_finalize_method(
879 cc_full_descr
->methods
.source
.msg_iter_finalize
);
881 BT_LOGE_STR("Cannot set source component class's message iterator finalization method.");
882 status
= BT_PLUGIN_STATUS_ERROR
;
883 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
889 case BT_COMPONENT_CLASS_TYPE_FILTER
:
890 if (cc_full_descr
->methods
.filter
.init
) {
891 ret
= bt_component_class_filter_set_init_method(
893 cc_full_descr
->methods
.filter
.init
);
895 BT_LOGE_STR("Cannot set filter component class's initialization method.");
896 status
= BT_PLUGIN_STATUS_ERROR
;
897 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
902 if (cc_full_descr
->methods
.filter
.finalize
) {
903 ret
= bt_component_class_filter_set_finalize_method(
905 cc_full_descr
->methods
.filter
.finalize
);
907 BT_LOGE_STR("Cannot set filter component class's finalization method.");
908 status
= BT_PLUGIN_STATUS_ERROR
;
909 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
914 if (cc_full_descr
->methods
.filter
.query
) {
915 ret
= bt_component_class_filter_set_query_method(
917 cc_full_descr
->methods
.filter
.query
);
919 BT_LOGE_STR("Cannot set filter component class's query method.");
920 status
= BT_PLUGIN_STATUS_ERROR
;
921 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
926 if (cc_full_descr
->methods
.filter
.accept_input_port_connection
) {
927 ret
= bt_component_class_filter_set_accept_input_port_connection_method(
929 cc_full_descr
->methods
.filter
.accept_input_port_connection
);
931 BT_LOGE_STR("Cannot set filter component class's \"accept input port connection\" method.");
932 status
= BT_PLUGIN_STATUS_ERROR
;
933 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
938 if (cc_full_descr
->methods
.filter
.accept_output_port_connection
) {
939 ret
= bt_component_class_filter_set_accept_output_port_connection_method(
941 cc_full_descr
->methods
.filter
.accept_output_port_connection
);
943 BT_LOGE_STR("Cannot set filter component class's \"accept input output connection\" method.");
944 status
= BT_PLUGIN_STATUS_ERROR
;
945 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
950 if (cc_full_descr
->methods
.filter
.input_port_connected
) {
951 ret
= bt_component_class_filter_set_input_port_connected_method(
953 cc_full_descr
->methods
.filter
.input_port_connected
);
955 BT_LOGE_STR("Cannot set filter component class's \"input port connected\" method.");
956 status
= BT_PLUGIN_STATUS_ERROR
;
957 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
962 if (cc_full_descr
->methods
.filter
.output_port_connected
) {
963 ret
= bt_component_class_filter_set_output_port_connected_method(
965 cc_full_descr
->methods
.filter
.output_port_connected
);
967 BT_LOGE_STR("Cannot set filter component class's \"output port connected\" method.");
968 status
= BT_PLUGIN_STATUS_ERROR
;
969 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
974 if (cc_full_descr
->methods
.filter
.input_port_disconnected
) {
975 ret
= bt_component_class_filter_set_input_port_disconnected_method(
977 cc_full_descr
->methods
.filter
.input_port_disconnected
);
979 BT_LOGE_STR("Cannot set filter component class's \"input port disconnected\" method.");
980 status
= BT_PLUGIN_STATUS_ERROR
;
981 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
986 if (cc_full_descr
->methods
.filter
.output_port_disconnected
) {
987 ret
= bt_component_class_filter_set_output_port_disconnected_method(
989 cc_full_descr
->methods
.filter
.output_port_disconnected
);
991 BT_LOGE_STR("Cannot set filter component class's \"output port disconnected\" method.");
992 status
= BT_PLUGIN_STATUS_ERROR
;
993 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
998 if (cc_full_descr
->methods
.filter
.msg_iter_init
) {
999 ret
= bt_component_class_filter_set_message_iterator_init_method(
1001 cc_full_descr
->methods
.filter
.msg_iter_init
);
1003 BT_LOGE_STR("Cannot set filter component class's message iterator initialization method.");
1004 status
= BT_PLUGIN_STATUS_ERROR
;
1005 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1010 if (cc_full_descr
->methods
.filter
.msg_iter_finalize
) {
1011 ret
= bt_component_class_filter_set_message_iterator_finalize_method(
1013 cc_full_descr
->methods
.filter
.msg_iter_finalize
);
1015 BT_LOGE_STR("Cannot set filter component class's message iterator finalization method.");
1016 status
= BT_PLUGIN_STATUS_ERROR
;
1017 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1023 case BT_COMPONENT_CLASS_TYPE_SINK
:
1024 if (cc_full_descr
->methods
.sink
.init
) {
1025 ret
= bt_component_class_sink_set_init_method(
1027 cc_full_descr
->methods
.sink
.init
);
1029 BT_LOGE_STR("Cannot set sink component class's initialization method.");
1030 status
= BT_PLUGIN_STATUS_ERROR
;
1031 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1036 if (cc_full_descr
->methods
.sink
.finalize
) {
1037 ret
= bt_component_class_sink_set_finalize_method(
1039 cc_full_descr
->methods
.sink
.finalize
);
1041 BT_LOGE_STR("Cannot set sink component class's finalization method.");
1042 status
= BT_PLUGIN_STATUS_ERROR
;
1043 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1048 if (cc_full_descr
->methods
.sink
.query
) {
1049 ret
= bt_component_class_sink_set_query_method(
1051 cc_full_descr
->methods
.sink
.query
);
1053 BT_LOGE_STR("Cannot set sink component class's query method.");
1054 status
= BT_PLUGIN_STATUS_ERROR
;
1055 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1060 if (cc_full_descr
->methods
.sink
.accept_input_port_connection
) {
1061 ret
= bt_component_class_sink_set_accept_input_port_connection_method(
1063 cc_full_descr
->methods
.sink
.accept_input_port_connection
);
1065 BT_LOGE_STR("Cannot set sink component class's \"accept input port connection\" method.");
1066 status
= BT_PLUGIN_STATUS_ERROR
;
1067 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1072 if (cc_full_descr
->methods
.sink
.input_port_connected
) {
1073 ret
= bt_component_class_sink_set_input_port_connected_method(
1075 cc_full_descr
->methods
.sink
.input_port_connected
);
1077 BT_LOGE_STR("Cannot set sink component class's \"input port connected\" method.");
1078 status
= BT_PLUGIN_STATUS_ERROR
;
1079 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1084 if (cc_full_descr
->methods
.sink
.input_port_disconnected
) {
1085 ret
= bt_component_class_sink_set_input_port_disconnected_method(
1087 cc_full_descr
->methods
.sink
.input_port_disconnected
);
1089 BT_LOGE_STR("Cannot set sink component class's \"input port disconnected\" method.");
1090 status
= BT_PLUGIN_STATUS_ERROR
;
1091 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1102 * Add component class to the plugin object.
1104 * This will call back
1105 * bt_plugin_so_on_add_component_class() so that we can
1106 * add a mapping in the component class list when we
1107 * know the component class is successfully added.
1109 status
= bt_plugin_add_component_class(plugin
,
1110 (void *) comp_class
);
1111 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
1113 BT_LOGE("Cannot add component class to plugin.");
1119 g_array_free(comp_class_full_descriptors
, TRUE
);
1124 struct bt_plugin
*bt_plugin_so_create_empty(
1125 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
1127 struct bt_plugin
*plugin
;
1128 struct bt_plugin_so_spec_data
*spec
;
1130 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
1135 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
1136 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
1137 if (!plugin
->spec_data
) {
1138 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
1142 spec
= plugin
->spec_data
;
1143 spec
->shared_lib_handle
= shared_lib_handle
;
1144 bt_object_get_no_null_check(spec
->shared_lib_handle
);
1148 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1155 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
1158 const int * const *begin_int
= (const int * const *) begin
;
1159 const int * const *end_int
= (const int * const *) end
;
1160 const int * const *iter
;
1162 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
1172 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
1173 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
1174 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
1175 struct __bt_plugin_descriptor
const * const *descriptors_end
,
1176 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
1177 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
1178 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
1179 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
1180 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
1181 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
1183 size_t descriptor_count
;
1185 size_t cc_descriptors_count
;
1186 size_t cc_descr_attrs_count
;
1188 struct bt_plugin_set
*plugin_set
= NULL
;
1190 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
1191 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
1192 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
1193 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
1195 BT_LOGD("Creating all SO plugins from sections: "
1196 "plugin-path=\"%s\", "
1197 "descr-begin-addr=%p, descr-end-addr=%p, "
1198 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1199 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1200 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1201 "descr-count=%zu, attrs-count=%zu, "
1202 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1203 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
1204 descriptors_begin
, descriptors_end
,
1205 attrs_begin
, attrs_end
,
1206 cc_descriptors_begin
, cc_descriptors_end
,
1207 cc_descr_attrs_begin
, cc_descr_attrs_end
,
1208 descriptor_count
, attrs_count
,
1209 cc_descriptors_count
, cc_descr_attrs_count
);
1210 plugin_set
= bt_plugin_set_create();
1212 BT_LOGE_STR("Cannot create empty plugin set.");
1216 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
1217 enum bt_plugin_status status
;
1218 const struct __bt_plugin_descriptor
*descriptor
=
1219 descriptors_begin
[i
];
1220 struct bt_plugin
*plugin
;
1222 if (descriptor
== NULL
) {
1226 BT_LOGD("Creating plugin object for plugin: "
1227 "name=\"%s\", abi-major=%d, abi-minor=%d",
1228 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
1230 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
1232 * DEBUG-level logging because we're only
1233 * _trying_ to open this file as a compatible
1234 * Babeltrace plugin: if it's not, it's not an
1235 * error. And because this can be tried during
1236 * bt_plugin_find_all_from_dir(), it's not
1239 BT_LOGD("Unknown ABI major version: abi-major=%d",
1244 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
1246 BT_LOGE_STR("Cannot create empty shared library handle.");
1250 if (shared_lib_handle
&& shared_lib_handle
->path
) {
1251 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
1254 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
1255 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
1256 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1259 * DEBUG-level logging because we're only
1260 * _trying_ to open this file as a compatible
1261 * Babeltrace plugin: if it's not, it's not an
1262 * error. And because this can be tried during
1263 * bt_plugin_find_all_from_dir(), it's not
1266 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
1267 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1271 /* Add to plugin set */
1272 bt_plugin_set_add_plugin(plugin_set
, plugin
);
1273 bt_object_put_ref(plugin
);
1279 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
1286 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
1288 struct bt_plugin_set
*plugin_set
= NULL
;
1289 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
1290 bt_plugin_so_shared_lib_handle_create(NULL
);
1292 if (!shared_lib_handle
) {
1296 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
1297 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1298 __bt_get_begin_section_plugin_descriptors(),
1299 __bt_get_end_section_plugin_descriptors(),
1300 __bt_get_begin_section_plugin_descriptor_attributes(),
1301 __bt_get_end_section_plugin_descriptor_attributes(),
1302 __bt_get_begin_section_component_class_descriptors(),
1303 __bt_get_end_section_component_class_descriptors(),
1304 __bt_get_begin_section_component_class_descriptor_attributes(),
1305 __bt_get_end_section_component_class_descriptor_attributes());
1308 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1314 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
1317 struct bt_plugin_set
*plugin_set
= NULL
;
1318 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
1319 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
1320 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
1321 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
1322 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
1323 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
1324 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
1325 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
1326 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
1327 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
1328 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
1329 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
1330 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
1331 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
1332 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
1333 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
1334 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
1335 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1338 BT_LOGW_STR("Invalid parameter: path is NULL.");
1342 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
1343 path_len
= strlen(path
);
1344 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
1345 BT_LOGW("Invalid parameter: path length is too short: "
1346 "path-length=%zu", path_len
);
1352 * Check if the file ends with a known plugin file type suffix (i.e. .so
1355 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
1356 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
1357 LIBTOOL_PLUGIN_SUFFIX_LEN
);
1358 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
1359 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
1360 NATIVE_PLUGIN_SUFFIX_LEN
);
1361 if (!is_shared_object
&& !is_libtool_wrapper
) {
1362 /* Name indicates this is not a plugin file; not an error */
1363 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
1367 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
1368 if (!shared_lib_handle
) {
1369 BT_LOGD_STR("Cannot create shared library handle.");
1373 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1374 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1375 descriptors_begin
= get_begin_section_plugin_descriptors();
1377 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1378 "symbol=\"%s\"", path
,
1379 "__bt_get_begin_section_plugin_descriptors");
1383 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1384 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1385 descriptors_end
= get_end_section_plugin_descriptors();
1387 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1388 "symbol=\"%s\"", path
,
1389 "__bt_get_end_section_plugin_descriptors");
1393 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1394 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1395 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1397 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1398 "symbol=\"%s\"", path
,
1399 "__bt_get_begin_section_plugin_descriptor_attributes");
1402 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1403 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1404 attrs_end
= get_end_section_plugin_descriptor_attributes();
1406 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1407 "symbol=\"%s\"", path
,
1408 "__bt_get_end_section_plugin_descriptor_attributes");
1411 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1412 BT_LOGD("Found section start or end symbol, but not both: "
1413 "path=\"%s\", symbol-start=\"%s\", "
1414 "symbol-end=\"%s\", symbol-start-addr=%p, "
1415 "symbol-end-addr=%p",
1416 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1417 "__bt_get_end_section_plugin_descriptor_attributes",
1418 attrs_begin
, attrs_end
);
1422 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1423 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1424 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1426 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1427 "symbol=\"%s\"", path
,
1428 "__bt_get_begin_section_component_class_descriptors");
1431 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1432 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1433 cc_descriptors_end
= get_end_section_component_class_descriptors();
1435 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1436 "symbol=\"%s\"", path
,
1437 "__bt_get_end_section_component_class_descriptors");
1440 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1441 BT_LOGD("Found section start or end symbol, but not both: "
1442 "path=\"%s\", symbol-start=\"%s\", "
1443 "symbol-end=\"%s\", symbol-start-addr=%p, "
1444 "symbol-end-addr=%p",
1445 path
, "__bt_get_begin_section_component_class_descriptors",
1446 "__bt_get_end_section_component_class_descriptors",
1447 cc_descriptors_begin
, cc_descriptors_end
);
1451 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1452 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1453 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1455 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1456 "symbol=\"%s\"", path
,
1457 "__bt_get_begin_section_component_class_descriptor_attributes");
1460 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1461 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1462 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1464 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1465 "symbol=\"%s\"", path
,
1466 "__bt_get_end_section_component_class_descriptor_attributes");
1469 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1470 BT_LOGD("Found section start or end symbol, but not both: "
1471 "path=\"%s\", symbol-start=\"%s\", "
1472 "symbol-end=\"%s\", symbol-start-addr=%p, "
1473 "symbol-end-addr=%p",
1474 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1475 "__bt_get_end_section_component_class_descriptor_attributes",
1476 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1480 /* Initialize plugin */
1481 BT_LOGD_STR("Initializing plugin object.");
1482 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1483 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1484 cc_descriptors_begin
, cc_descriptors_end
,
1485 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1488 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1493 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1496 bt_list_del(&comp_class
->node
);
1497 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
1498 BT_LOGV("Component class destroyed: removed entry from list: "
1499 "comp-cls-addr=%p", comp_class
);
1503 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1504 struct bt_component_class
*comp_class
)
1506 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1508 BT_ASSERT(plugin
->spec_data
);
1509 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1511 bt_list_add(&comp_class
->node
, &component_class_list
);
1512 comp_class
->so_handle
= spec
->shared_lib_handle
;
1513 bt_object_get_no_null_check(comp_class
->so_handle
);
1515 /* Add our custom destroy listener */
1516 bt_component_class_add_destroy_listener(comp_class
,
1517 plugin_comp_class_destroy_listener
, NULL
);