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 "LIB/PLUGIN-SO"
27 #include "lib/logging.h"
29 #include "common/assert.h"
30 #include "lib/assert-pre.h"
31 #include "compat/compiler.h"
32 #include <babeltrace2/plugin/plugin-dev.h>
33 #include "lib/graph/component-class.h"
34 #include <babeltrace2/graph/component-class.h>
35 #include <babeltrace2/graph/component-class-source.h>
36 #include <babeltrace2/graph/component-class-filter.h>
37 #include <babeltrace2/graph/component-class-sink.h>
38 #include <babeltrace2/types.h>
39 #include "common/list.h"
46 #include "plugin-so.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 bt_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_LOGI("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
) {
141 #ifndef BT_DEBUG_MODE
143 * Valgrind shows incomplete stack traces when
144 * dynamically loaded libraries are closed before it
145 * finishes. Use the LIBBABELTRACE2_NO_DLCLOSE in a debug
146 * build to avoid this.
148 const char *var
= getenv("LIBBABELTRACE2_NO_DLCLOSE");
150 if (!var
|| strcmp(var
, "1") != 0) {
152 BT_LOGI("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
;
160 #ifndef BT_DEBUG_MODE
162 BT_LOGI("Not closing GModule because `LIBBABELTRACE2_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 enum bt_plugin_status
bt_plugin_so_shared_lib_handle_create(
179 struct bt_plugin_so_shared_lib_handle
**shared_lib_handle
)
181 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
183 BT_ASSERT(shared_lib_handle
);
184 BT_LOGI("Creating shared library handle: path=\"%s\"", path
);
185 *shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
186 if (!*shared_lib_handle
) {
187 BT_LOGE_STR("Failed to allocate one shared library handle.");
188 status
= BT_PLUGIN_STATUS_NOMEM
;
192 bt_object_init_shared(&(*shared_lib_handle
)->base
,
193 bt_plugin_so_shared_lib_handle_destroy
);
199 (*shared_lib_handle
)->path
= g_string_new(path
);
200 if (!(*shared_lib_handle
)->path
) {
201 BT_LOGE_STR("Failed to allocate a GString.");
202 status
= BT_PLUGIN_STATUS_NOMEM
;
206 (*shared_lib_handle
)->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
207 if (!(*shared_lib_handle
)->module
) {
209 * INFO-level logging because we're only _trying_ to
210 * open this file as a Babeltrace plugin: if it's not,
211 * it's not an error. And because this can be tried
212 * during bt_plugin_find_all_from_dir(), it's not even a
215 BT_LOGI("Cannot open GModule: %s: path=\"%s\"",
216 g_module_error(), path
);
217 BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle
);
218 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
225 BT_ASSERT(*shared_lib_handle
|| status
!= BT_PLUGIN_STATUS_OK
);
226 if (*shared_lib_handle
) {
227 BT_LOGI("Created shared library handle: path=\"%s\", addr=%p",
228 path
, *shared_lib_handle
);
235 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
237 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
239 if (!plugin
->spec_data
) {
243 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
245 BT_OBJECT_PUT_REF_AND_RESET(spec
->shared_lib_handle
);
246 g_free(plugin
->spec_data
);
247 plugin
->spec_data
= NULL
;
251 * This function does the following:
253 * 1. Iterate on the plugin descriptor attributes section and set the
254 * plugin's attributes depending on the attribute types. This
255 * includes the name of the plugin, its description, and its
256 * initialization function, for example.
258 * 2. Iterate on the component class descriptors section and create one
259 * "full descriptor" (temporary structure) for each one that is found
260 * and attached to our plugin descriptor.
262 * 3. Iterate on the component class descriptor attributes section and
263 * set the corresponding full descriptor's attributes depending on
264 * the attribute types. This includes the description of the
265 * component class, as well as its initialization and destroy
268 * 4. Call the user's plugin initialization function, if any is
271 * 5. For each full component class descriptor, create a component class
272 * object, set its optional attributes, and add it to the plugin
275 * 6. Freeze the plugin object.
278 enum bt_plugin_status
bt_plugin_so_init(struct bt_plugin
*plugin
,
279 bool fail_on_load_error
,
280 const struct __bt_plugin_descriptor
*descriptor
,
281 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
282 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
283 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
284 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
285 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
286 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
289 * This structure's members point to the plugin's memory
292 struct comp_class_full_descriptor
{
293 const struct __bt_plugin_component_class_descriptor
*descriptor
;
294 const char *description
;
299 bt_component_class_source_init_method init
;
300 bt_component_class_source_finalize_method finalize
;
301 bt_component_class_source_query_method query
;
302 bt_component_class_source_output_port_connected_method output_port_connected
;
303 bt_component_class_source_message_iterator_init_method msg_iter_init
;
304 bt_component_class_source_message_iterator_finalize_method msg_iter_finalize
;
305 bt_component_class_source_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
306 bt_component_class_source_message_iterator_seek_beginning_method msg_iter_seek_beginning
;
307 bt_component_class_source_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
308 bt_component_class_source_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning
;
312 bt_component_class_filter_init_method init
;
313 bt_component_class_filter_finalize_method finalize
;
314 bt_component_class_filter_query_method query
;
315 bt_component_class_filter_input_port_connected_method input_port_connected
;
316 bt_component_class_filter_output_port_connected_method output_port_connected
;
317 bt_component_class_filter_message_iterator_init_method msg_iter_init
;
318 bt_component_class_filter_message_iterator_finalize_method msg_iter_finalize
;
319 bt_component_class_filter_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
320 bt_component_class_filter_message_iterator_seek_beginning_method msg_iter_seek_beginning
;
321 bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
322 bt_component_class_filter_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning
;
326 bt_component_class_sink_init_method init
;
327 bt_component_class_sink_finalize_method finalize
;
328 bt_component_class_sink_query_method query
;
329 bt_component_class_sink_input_port_connected_method input_port_connected
;
330 bt_component_class_sink_graph_is_configured_method graph_is_configured
;
335 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
336 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
337 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
338 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
339 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
340 GArray
*comp_class_full_descriptors
;
344 BT_LOGI("Initializing plugin object from descriptors found in sections: "
345 "plugin-addr=%p, plugin-path=\"%s\", "
346 "attrs-begin-addr=%p, attrs-end-addr=%p, "
347 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
348 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
350 spec
->shared_lib_handle
->path
?
351 spec
->shared_lib_handle
->path
->str
: NULL
,
352 attrs_begin
, attrs_end
,
353 cc_descriptors_begin
, cc_descriptors_end
,
354 cc_descr_attrs_begin
, cc_descr_attrs_end
);
355 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
356 sizeof(struct comp_class_full_descriptor
));
357 if (!comp_class_full_descriptors
) {
358 BT_LOGE_STR("Failed to allocate a GArray.");
359 status
= BT_PLUGIN_STATUS_NOMEM
;
363 /* Set mandatory attributes */
364 spec
->descriptor
= descriptor
;
365 bt_plugin_set_name(plugin
, descriptor
->name
);
368 * Find and set optional attributes attached to this plugin
371 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
372 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
375 if (cur_attr
== NULL
) {
379 if (cur_attr
->plugin_descriptor
!= descriptor
) {
383 switch (cur_attr
->type
) {
384 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
385 spec
->init
= cur_attr
->value
.init
;
387 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
388 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
390 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
391 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
393 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
394 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
396 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
397 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
399 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
400 bt_plugin_set_version(plugin
,
401 (unsigned int) cur_attr
->value
.version
.major
,
402 (unsigned int) cur_attr
->value
.version
.minor
,
403 (unsigned int) cur_attr
->value
.version
.patch
,
404 cur_attr
->value
.version
.extra
);
407 BT_LOG_WRITE(fail_on_load_error
?
408 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
409 "%s plugin descriptor attribute: "
410 "plugin-path=\"%s\", plugin-name=\"%s\", "
411 "attr-type-name=\"%s\", attr-type-id=%d",
412 fail_on_load_error
? "Unknown" :
414 spec
->shared_lib_handle
->path
?
415 spec
->shared_lib_handle
->path
->str
:
417 descriptor
->name
, cur_attr
->type_name
,
420 if (fail_on_load_error
) {
421 status
= BT_PLUGIN_STATUS_LOADING_ERROR
;
430 * Find component class descriptors attached to this plugin
431 * descriptor and initialize corresponding full component class
432 * descriptors in the array.
434 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
435 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
437 struct comp_class_full_descriptor full_descriptor
= {0};
439 if (cur_cc_descr
== NULL
) {
443 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
447 full_descriptor
.descriptor
= cur_cc_descr
;
448 g_array_append_val(comp_class_full_descriptors
,
453 * Find component class descriptor attributes attached to this
454 * plugin descriptor and update corresponding full component
455 * class descriptors in the array.
457 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
458 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
459 *cur_cc_descr_attr_ptr
;
460 enum bt_component_class_type cc_type
;
462 if (cur_cc_descr_attr
== NULL
) {
466 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
471 cc_type
= cur_cc_descr_attr
->comp_class_descriptor
->type
;
473 /* Find the corresponding component class descriptor entry */
474 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
475 struct comp_class_full_descriptor
*cc_full_descr
=
476 &g_array_index(comp_class_full_descriptors
,
477 struct comp_class_full_descriptor
, i
);
479 if (cur_cc_descr_attr
->comp_class_descriptor
!=
480 cc_full_descr
->descriptor
) {
484 switch (cur_cc_descr_attr
->type
) {
485 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
486 cc_full_descr
->description
=
487 cur_cc_descr_attr
->value
.description
;
489 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
490 cc_full_descr
->help
=
491 cur_cc_descr_attr
->value
.help
;
493 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
495 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
496 cc_full_descr
->methods
.source
.init
=
497 cur_cc_descr_attr
->value
.source_init_method
;
499 case BT_COMPONENT_CLASS_TYPE_FILTER
:
500 cc_full_descr
->methods
.filter
.init
=
501 cur_cc_descr_attr
->value
.filter_init_method
;
503 case BT_COMPONENT_CLASS_TYPE_SINK
:
504 cc_full_descr
->methods
.sink
.init
=
505 cur_cc_descr_attr
->value
.sink_init_method
;
511 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
513 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
514 cc_full_descr
->methods
.source
.finalize
=
515 cur_cc_descr_attr
->value
.source_finalize_method
;
517 case BT_COMPONENT_CLASS_TYPE_FILTER
:
518 cc_full_descr
->methods
.filter
.finalize
=
519 cur_cc_descr_attr
->value
.filter_finalize_method
;
521 case BT_COMPONENT_CLASS_TYPE_SINK
:
522 cc_full_descr
->methods
.sink
.finalize
=
523 cur_cc_descr_attr
->value
.sink_finalize_method
;
529 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
531 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
532 cc_full_descr
->methods
.source
.query
=
533 cur_cc_descr_attr
->value
.source_query_method
;
535 case BT_COMPONENT_CLASS_TYPE_FILTER
:
536 cc_full_descr
->methods
.filter
.query
=
537 cur_cc_descr_attr
->value
.filter_query_method
;
539 case BT_COMPONENT_CLASS_TYPE_SINK
:
540 cc_full_descr
->methods
.sink
.query
=
541 cur_cc_descr_attr
->value
.sink_query_method
;
547 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD
:
549 case BT_COMPONENT_CLASS_TYPE_FILTER
:
550 cc_full_descr
->methods
.filter
.input_port_connected
=
551 cur_cc_descr_attr
->value
.filter_input_port_connected_method
;
553 case BT_COMPONENT_CLASS_TYPE_SINK
:
554 cc_full_descr
->methods
.sink
.input_port_connected
=
555 cur_cc_descr_attr
->value
.sink_input_port_connected_method
;
561 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD
:
563 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
564 cc_full_descr
->methods
.source
.output_port_connected
=
565 cur_cc_descr_attr
->value
.source_output_port_connected_method
;
567 case BT_COMPONENT_CLASS_TYPE_FILTER
:
568 cc_full_descr
->methods
.filter
.output_port_connected
=
569 cur_cc_descr_attr
->value
.filter_output_port_connected_method
;
575 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD
:
577 case BT_COMPONENT_CLASS_TYPE_SINK
:
578 cc_full_descr
->methods
.sink
.graph_is_configured
=
579 cur_cc_descr_attr
->value
.sink_graph_is_configured_method
;
585 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INIT_METHOD
:
587 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
588 cc_full_descr
->methods
.source
.msg_iter_init
=
589 cur_cc_descr_attr
->value
.source_msg_iter_init_method
;
591 case BT_COMPONENT_CLASS_TYPE_FILTER
:
592 cc_full_descr
->methods
.filter
.msg_iter_init
=
593 cur_cc_descr_attr
->value
.filter_msg_iter_init_method
;
599 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD
:
601 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
602 cc_full_descr
->methods
.source
.msg_iter_finalize
=
603 cur_cc_descr_attr
->value
.source_msg_iter_finalize_method
;
605 case BT_COMPONENT_CLASS_TYPE_FILTER
:
606 cc_full_descr
->methods
.filter
.msg_iter_finalize
=
607 cur_cc_descr_attr
->value
.filter_msg_iter_finalize_method
;
613 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD
:
615 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
616 cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
=
617 cur_cc_descr_attr
->value
.source_msg_iter_seek_ns_from_origin_method
;
619 case BT_COMPONENT_CLASS_TYPE_FILTER
:
620 cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
=
621 cur_cc_descr_attr
->value
.filter_msg_iter_seek_ns_from_origin_method
;
627 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD
:
629 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
630 cc_full_descr
->methods
.source
.msg_iter_seek_beginning
=
631 cur_cc_descr_attr
->value
.source_msg_iter_seek_beginning_method
;
633 case BT_COMPONENT_CLASS_TYPE_FILTER
:
634 cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
=
635 cur_cc_descr_attr
->value
.filter_msg_iter_seek_beginning_method
;
641 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD
:
643 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
644 cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
=
645 cur_cc_descr_attr
->value
.source_msg_iter_can_seek_ns_from_origin_method
;
647 case BT_COMPONENT_CLASS_TYPE_FILTER
:
648 cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
=
649 cur_cc_descr_attr
->value
.filter_msg_iter_can_seek_ns_from_origin_method
;
655 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD
:
657 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
658 cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
=
659 cur_cc_descr_attr
->value
.source_msg_iter_can_seek_beginning_method
;
661 case BT_COMPONENT_CLASS_TYPE_FILTER
:
662 cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
=
663 cur_cc_descr_attr
->value
.filter_msg_iter_can_seek_beginning_method
;
670 BT_LOG_WRITE(fail_on_load_error
?
671 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
672 "%s component class descriptor attribute: "
673 "plugin-path=\"%s\", "
674 "plugin-name=\"%s\", "
675 "comp-class-name=\"%s\", "
676 "comp-class-type=%s, "
677 "attr-type-name=\"%s\", "
679 fail_on_load_error
? "Unknown" :
681 spec
->shared_lib_handle
->path
?
682 spec
->shared_lib_handle
->path
->str
:
685 cur_cc_descr_attr
->comp_class_descriptor
->name
,
686 bt_component_class_type_string(
687 cur_cc_descr_attr
->comp_class_descriptor
->type
),
688 cur_cc_descr_attr
->type_name
,
689 cur_cc_descr_attr
->type
);
691 if (fail_on_load_error
) {
692 status
= BT_PLUGIN_STATUS_LOADING_ERROR
;
701 /* Initialize plugin */
703 enum bt_self_plugin_status init_status
;
705 BT_LOGD_STR("Calling user's plugin initialization function.");
706 init_status
= spec
->init((void *) plugin
);
707 BT_LOGD("User function returned: status=%s",
708 bt_self_plugin_status_string(init_status
));
710 if (init_status
< 0) {
711 BT_LOG_WRITE(fail_on_load_error
?
712 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
713 "User's plugin initialization function failed: "
715 bt_self_plugin_status_string(init_status
));
717 if (fail_on_load_error
) {
718 status
= (int) init_status
;
724 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
726 /* Add described component classes to plugin */
727 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
728 struct comp_class_full_descriptor
*cc_full_descr
=
729 &g_array_index(comp_class_full_descriptors
,
730 struct comp_class_full_descriptor
, i
);
731 struct bt_component_class
*comp_class
= NULL
;
732 struct bt_component_class_source
*src_comp_class
= NULL
;
733 struct bt_component_class_filter
*flt_comp_class
= NULL
;
734 struct bt_component_class_sink
*sink_comp_class
= NULL
;
736 BT_LOGI("Creating and setting properties of plugin's component class: "
737 "plugin-path=\"%s\", plugin-name=\"%s\", "
738 "comp-class-name=\"%s\", comp-class-type=%s",
739 spec
->shared_lib_handle
->path
?
740 spec
->shared_lib_handle
->path
->str
:
743 cc_full_descr
->descriptor
->name
,
744 bt_component_class_type_string(
745 cc_full_descr
->descriptor
->type
));
747 switch (cc_full_descr
->descriptor
->type
) {
748 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
749 src_comp_class
= bt_component_class_source_create(
750 cc_full_descr
->descriptor
->name
,
751 cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
);
752 comp_class
= bt_component_class_source_as_component_class(
755 case BT_COMPONENT_CLASS_TYPE_FILTER
:
756 flt_comp_class
= bt_component_class_filter_create(
757 cc_full_descr
->descriptor
->name
,
758 cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
);
759 comp_class
= bt_component_class_filter_as_component_class(
762 case BT_COMPONENT_CLASS_TYPE_SINK
:
763 sink_comp_class
= bt_component_class_sink_create(
764 cc_full_descr
->descriptor
->name
,
765 cc_full_descr
->descriptor
->methods
.sink
.consume
);
766 comp_class
= bt_component_class_sink_as_component_class(
770 BT_LOG_WRITE(fail_on_load_error
?
771 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
772 "%s component class type: "
773 "plugin-path=\"%s\", plugin-name=\"%s\", "
774 "comp-class-name=\"%s\", comp-class-type=%d",
775 fail_on_load_error
? "Unknown" :
777 spec
->shared_lib_handle
->path
->str
?
778 spec
->shared_lib_handle
->path
->str
:
781 cc_full_descr
->descriptor
->name
,
782 cc_full_descr
->descriptor
->type
);
784 if (fail_on_load_error
) {
785 status
= BT_PLUGIN_STATUS_LOADING_ERROR
;
793 BT_LOGE_STR("Cannot create component class.");
794 status
= BT_PLUGIN_STATUS_NOMEM
;
798 if (cc_full_descr
->description
) {
799 ret
= bt_component_class_set_description(
800 comp_class
, cc_full_descr
->description
);
802 BT_LOGE_STR("Cannot set component class's description.");
803 status
= BT_PLUGIN_STATUS_NOMEM
;
804 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
809 if (cc_full_descr
->help
) {
810 ret
= bt_component_class_set_help(comp_class
,
811 cc_full_descr
->help
);
813 BT_LOGE_STR("Cannot set component class's help string.");
814 status
= BT_PLUGIN_STATUS_NOMEM
;
815 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
820 switch (cc_full_descr
->descriptor
->type
) {
821 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
822 if (cc_full_descr
->methods
.source
.init
) {
823 ret
= bt_component_class_source_set_init_method(
825 cc_full_descr
->methods
.source
.init
);
827 BT_LOGE_STR("Cannot set source component class's initialization method.");
828 status
= BT_PLUGIN_STATUS_NOMEM
;
829 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
834 if (cc_full_descr
->methods
.source
.finalize
) {
835 ret
= bt_component_class_source_set_finalize_method(
837 cc_full_descr
->methods
.source
.finalize
);
839 BT_LOGE_STR("Cannot set source component class's finalization method.");
840 status
= BT_PLUGIN_STATUS_NOMEM
;
841 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
846 if (cc_full_descr
->methods
.source
.query
) {
847 ret
= bt_component_class_source_set_query_method(
849 cc_full_descr
->methods
.source
.query
);
851 BT_LOGE_STR("Cannot set source component class's query method.");
852 status
= BT_PLUGIN_STATUS_NOMEM
;
853 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
858 if (cc_full_descr
->methods
.source
.output_port_connected
) {
859 ret
= bt_component_class_source_set_output_port_connected_method(
861 cc_full_descr
->methods
.source
.output_port_connected
);
863 BT_LOGE_STR("Cannot set source component class's \"output port connected\" method.");
864 status
= BT_PLUGIN_STATUS_NOMEM
;
865 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
870 if (cc_full_descr
->methods
.source
.msg_iter_init
) {
871 ret
= bt_component_class_source_set_message_iterator_init_method(
873 cc_full_descr
->methods
.source
.msg_iter_init
);
875 BT_LOGE_STR("Cannot set source component class's message iterator initialization method.");
876 status
= BT_PLUGIN_STATUS_NOMEM
;
877 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
882 if (cc_full_descr
->methods
.source
.msg_iter_finalize
) {
883 ret
= bt_component_class_source_set_message_iterator_finalize_method(
885 cc_full_descr
->methods
.source
.msg_iter_finalize
);
887 BT_LOGE_STR("Cannot set source component class's message iterator finalization method.");
888 status
= BT_PLUGIN_STATUS_NOMEM
;
889 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
894 if (cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
) {
895 ret
= bt_component_class_source_set_message_iterator_seek_ns_from_origin_method(
897 cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
);
899 BT_LOGE_STR("Cannot set source component class's message iterator \"seek nanoseconds from origin\" method.");
900 status
= BT_PLUGIN_STATUS_NOMEM
;
901 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
906 if (cc_full_descr
->methods
.source
.msg_iter_seek_beginning
) {
907 ret
= bt_component_class_source_set_message_iterator_seek_beginning_method(
909 cc_full_descr
->methods
.source
.msg_iter_seek_beginning
);
911 BT_LOGE_STR("Cannot set source component class's message iterator \"seek beginning\" method.");
912 status
= BT_PLUGIN_STATUS_NOMEM
;
913 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
918 if (cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
) {
919 ret
= bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method(
921 cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
);
923 BT_LOGE_STR("Cannot set source component class's message iterator \"can seek nanoseconds from origin\" method.");
924 status
= BT_PLUGIN_STATUS_NOMEM
;
925 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
930 if (cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
) {
931 ret
= bt_component_class_source_set_message_iterator_can_seek_beginning_method(
933 cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
);
935 BT_LOGE_STR("Cannot set source component class's message iterator \"can seek beginning\" method.");
936 status
= BT_PLUGIN_STATUS_NOMEM
;
937 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
943 case BT_COMPONENT_CLASS_TYPE_FILTER
:
944 if (cc_full_descr
->methods
.filter
.init
) {
945 ret
= bt_component_class_filter_set_init_method(
947 cc_full_descr
->methods
.filter
.init
);
949 BT_LOGE_STR("Cannot set filter component class's initialization method.");
950 status
= BT_PLUGIN_STATUS_NOMEM
;
951 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
956 if (cc_full_descr
->methods
.filter
.finalize
) {
957 ret
= bt_component_class_filter_set_finalize_method(
959 cc_full_descr
->methods
.filter
.finalize
);
961 BT_LOGE_STR("Cannot set filter component class's finalization method.");
962 status
= BT_PLUGIN_STATUS_NOMEM
;
963 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
968 if (cc_full_descr
->methods
.filter
.query
) {
969 ret
= bt_component_class_filter_set_query_method(
971 cc_full_descr
->methods
.filter
.query
);
973 BT_LOGE_STR("Cannot set filter component class's query method.");
974 status
= BT_PLUGIN_STATUS_NOMEM
;
975 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
980 if (cc_full_descr
->methods
.filter
.input_port_connected
) {
981 ret
= bt_component_class_filter_set_input_port_connected_method(
983 cc_full_descr
->methods
.filter
.input_port_connected
);
985 BT_LOGE_STR("Cannot set filter component class's \"input port connected\" method.");
986 status
= BT_PLUGIN_STATUS_NOMEM
;
987 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
992 if (cc_full_descr
->methods
.filter
.output_port_connected
) {
993 ret
= bt_component_class_filter_set_output_port_connected_method(
995 cc_full_descr
->methods
.filter
.output_port_connected
);
997 BT_LOGE_STR("Cannot set filter component class's \"output port connected\" method.");
998 status
= BT_PLUGIN_STATUS_NOMEM
;
999 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1004 if (cc_full_descr
->methods
.filter
.msg_iter_init
) {
1005 ret
= bt_component_class_filter_set_message_iterator_init_method(
1007 cc_full_descr
->methods
.filter
.msg_iter_init
);
1009 BT_LOGE_STR("Cannot set filter component class's message iterator initialization method.");
1010 status
= BT_PLUGIN_STATUS_NOMEM
;
1011 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1016 if (cc_full_descr
->methods
.filter
.msg_iter_finalize
) {
1017 ret
= bt_component_class_filter_set_message_iterator_finalize_method(
1019 cc_full_descr
->methods
.filter
.msg_iter_finalize
);
1021 BT_LOGE_STR("Cannot set filter component class's message iterator finalization method.");
1022 status
= BT_PLUGIN_STATUS_NOMEM
;
1023 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1028 if (cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
) {
1029 ret
= bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method(
1031 cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
);
1033 BT_LOGE_STR("Cannot set filter component class's message iterator \"seek nanoseconds from origin\" method.");
1034 status
= BT_PLUGIN_STATUS_NOMEM
;
1035 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1040 if (cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
) {
1041 ret
= bt_component_class_filter_set_message_iterator_seek_beginning_method(
1043 cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
);
1045 BT_LOGE_STR("Cannot set filter component class's message iterator \"seek beginning\" method.");
1046 status
= BT_PLUGIN_STATUS_NOMEM
;
1047 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1052 if (cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
) {
1053 ret
= bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method(
1055 cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
);
1057 BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek nanoseconds from origin\" method.");
1058 status
= BT_PLUGIN_STATUS_NOMEM
;
1059 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1064 if (cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
) {
1065 ret
= bt_component_class_filter_set_message_iterator_can_seek_beginning_method(
1067 cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
);
1069 BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek beginning\" method.");
1070 status
= BT_PLUGIN_STATUS_NOMEM
;
1071 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1077 case BT_COMPONENT_CLASS_TYPE_SINK
:
1078 if (cc_full_descr
->methods
.sink
.init
) {
1079 ret
= bt_component_class_sink_set_init_method(
1081 cc_full_descr
->methods
.sink
.init
);
1083 BT_LOGE_STR("Cannot set sink component class's initialization method.");
1084 status
= BT_PLUGIN_STATUS_NOMEM
;
1085 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1090 if (cc_full_descr
->methods
.sink
.finalize
) {
1091 ret
= bt_component_class_sink_set_finalize_method(
1093 cc_full_descr
->methods
.sink
.finalize
);
1095 BT_LOGE_STR("Cannot set sink component class's finalization method.");
1096 status
= BT_PLUGIN_STATUS_NOMEM
;
1097 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1102 if (cc_full_descr
->methods
.sink
.query
) {
1103 ret
= bt_component_class_sink_set_query_method(
1105 cc_full_descr
->methods
.sink
.query
);
1107 BT_LOGE_STR("Cannot set sink component class's query method.");
1108 status
= BT_PLUGIN_STATUS_NOMEM
;
1109 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1114 if (cc_full_descr
->methods
.sink
.input_port_connected
) {
1115 ret
= bt_component_class_sink_set_input_port_connected_method(
1117 cc_full_descr
->methods
.sink
.input_port_connected
);
1119 BT_LOGE_STR("Cannot set sink component class's \"input port connected\" method.");
1120 status
= BT_PLUGIN_STATUS_NOMEM
;
1121 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1126 if (cc_full_descr
->methods
.sink
.graph_is_configured
) {
1127 ret
= bt_component_class_sink_set_graph_is_configured_method(
1129 cc_full_descr
->methods
.sink
.graph_is_configured
);
1131 BT_LOGE_STR("Cannot set sink component class's \"graph is configured\" method.");
1132 status
= BT_PLUGIN_STATUS_NOMEM
;
1133 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1144 * Add component class to the plugin object.
1146 * This will call back
1147 * bt_plugin_so_on_add_component_class() so that we can
1148 * add a mapping in the component class list when we
1149 * know the component class is successfully added.
1151 status
= bt_plugin_add_component_class(plugin
,
1152 (void *) comp_class
);
1153 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
1155 BT_LOGE("Cannot add component class to plugin.");
1161 g_array_free(comp_class_full_descriptors
, TRUE
);
1166 struct bt_plugin
*bt_plugin_so_create_empty(
1167 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
1169 struct bt_plugin
*plugin
;
1170 struct bt_plugin_so_spec_data
*spec
;
1172 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
1177 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
1178 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
1179 if (!plugin
->spec_data
) {
1180 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
1184 spec
= plugin
->spec_data
;
1185 spec
->shared_lib_handle
= shared_lib_handle
;
1186 bt_object_get_no_null_check(spec
->shared_lib_handle
);
1190 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1197 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
1200 const int * const *begin_int
= (const int * const *) begin
;
1201 const int * const *end_int
= (const int * const *) end
;
1202 const int * const *iter
;
1204 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
1214 enum bt_plugin_status
bt_plugin_so_create_all_from_sections(
1215 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
1216 bool fail_on_load_error
,
1217 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
1218 struct __bt_plugin_descriptor
const * const *descriptors_end
,
1219 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
1220 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
1221 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
1222 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
1223 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
1224 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
,
1225 struct bt_plugin_set
**plugin_set_out
)
1227 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
1228 size_t descriptor_count
;
1230 size_t cc_descriptors_count
;
1231 size_t cc_descr_attrs_count
;
1234 BT_ASSERT(plugin_set_out
);
1235 *plugin_set_out
= NULL
;
1236 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
1237 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
1238 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
1239 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
1240 BT_LOGI("Creating all SO plugins from sections: "
1241 "plugin-path=\"%s\", "
1242 "descr-begin-addr=%p, descr-end-addr=%p, "
1243 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1244 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1245 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1246 "descr-count=%zu, attrs-count=%zu, "
1247 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1248 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
1249 descriptors_begin
, descriptors_end
,
1250 attrs_begin
, attrs_end
,
1251 cc_descriptors_begin
, cc_descriptors_end
,
1252 cc_descr_attrs_begin
, cc_descr_attrs_end
,
1253 descriptor_count
, attrs_count
,
1254 cc_descriptors_count
, cc_descr_attrs_count
);
1255 *plugin_set_out
= bt_plugin_set_create();
1256 if (!*plugin_set_out
) {
1257 BT_LOGE_STR("Cannot create empty plugin set.");
1258 status
= BT_PLUGIN_STATUS_NOMEM
;
1262 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
1263 enum bt_plugin_status status
;
1264 const struct __bt_plugin_descriptor
*descriptor
=
1265 descriptors_begin
[i
];
1266 struct bt_plugin
*plugin
;
1268 if (descriptor
== NULL
) {
1272 BT_LOGI("Creating plugin object for plugin: "
1273 "name=\"%s\", abi-major=%d, abi-minor=%d",
1274 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
1276 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
1277 BT_LOG_WRITE(fail_on_load_error
? BT_LOG_WARN
:
1278 BT_LOG_INFO
, BT_LOG_TAG
,
1279 "Unknown ABI major version: abi-major=%d",
1282 if (fail_on_load_error
) {
1283 status
= BT_PLUGIN_STATUS_LOADING_ERROR
;
1290 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
1292 BT_LOGE_STR("Cannot create empty shared library handle.");
1293 status
= BT_PLUGIN_STATUS_NOMEM
;
1297 if (shared_lib_handle
&& shared_lib_handle
->path
) {
1298 bt_plugin_set_path(plugin
,
1299 shared_lib_handle
->path
->str
);
1302 status
= bt_plugin_so_init(plugin
, fail_on_load_error
,
1303 descriptor
, attrs_begin
, attrs_end
,
1304 cc_descriptors_begin
, cc_descriptors_end
,
1305 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1306 if (status
== BT_PLUGIN_STATUS_OK
) {
1307 /* Add to plugin set */
1308 bt_plugin_set_add_plugin(*plugin_set_out
, plugin
);
1309 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1310 } else if (status
< 0) {
1312 * bt_plugin_so_init() handles
1313 * `fail_on_load_error`, so this is a "real"
1316 BT_LOGW_STR("Cannot initialize SO plugin object from sections.");
1317 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1324 BT_ASSERT(*plugin_set_out
);
1326 if ((*plugin_set_out
)->plugins
->len
== 0) {
1327 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1328 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
1334 BT_ASSERT(status
< 0);
1335 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1342 enum bt_plugin_status
bt_plugin_so_create_all_from_static(
1343 bool fail_on_load_error
,
1344 struct bt_plugin_set
**plugin_set_out
)
1346 enum bt_plugin_status status
;
1347 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1349 BT_ASSERT(plugin_set_out
);
1350 *plugin_set_out
= NULL
;
1351 status
= bt_plugin_so_shared_lib_handle_create(NULL
,
1352 &shared_lib_handle
);
1353 if (status
!= BT_PLUGIN_STATUS_OK
) {
1354 BT_ASSERT(!shared_lib_handle
);
1358 BT_ASSERT(shared_lib_handle
);
1359 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
1360 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1362 __bt_get_begin_section_plugin_descriptors(),
1363 __bt_get_end_section_plugin_descriptors(),
1364 __bt_get_begin_section_plugin_descriptor_attributes(),
1365 __bt_get_end_section_plugin_descriptor_attributes(),
1366 __bt_get_begin_section_component_class_descriptors(),
1367 __bt_get_end_section_component_class_descriptors(),
1368 __bt_get_begin_section_component_class_descriptor_attributes(),
1369 __bt_get_end_section_component_class_descriptor_attributes(),
1371 BT_ASSERT((status
== BT_PLUGIN_STATUS_OK
&& *plugin_set_out
&&
1372 (*plugin_set_out
)->plugins
->len
> 0) || !*plugin_set_out
);
1375 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1380 enum bt_plugin_status
bt_plugin_so_create_all_from_file(const char *path
,
1381 bool fail_on_load_error
, struct bt_plugin_set
**plugin_set_out
)
1384 enum bt_plugin_status status
;
1385 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
1386 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
1387 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
1388 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
1389 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
1390 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
1391 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
1392 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
1393 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
1394 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
1395 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
1396 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
1397 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
1398 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
1399 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
1400 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
1401 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
1402 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1405 BT_ASSERT(plugin_set_out
);
1406 *plugin_set_out
= NULL
;
1407 path_len
= strlen(path
);
1408 BT_ASSERT_PRE(path_len
> PLUGIN_SUFFIX_LEN
,
1409 "Path length is too short: path-length=%zu, min-length=%zu",
1410 path_len
, PLUGIN_SUFFIX_LEN
);
1411 BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path
);
1415 * Check if the file ends with a known plugin file type suffix
1416 * (i.e. .so or .la on Linux).
1418 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
1419 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
1420 LIBTOOL_PLUGIN_SUFFIX_LEN
);
1421 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
1422 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
1423 NATIVE_PLUGIN_SUFFIX_LEN
);
1424 if (!is_shared_object
&& !is_libtool_wrapper
) {
1425 /* Name indicates this is not a plugin file; not an error */
1426 BT_LOGI("File is not an SO plugin file: path=\"%s\"", path
);
1427 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
1431 status
= bt_plugin_so_shared_lib_handle_create(path
,
1432 &shared_lib_handle
);
1433 if (status
!= BT_PLUGIN_STATUS_OK
) {
1434 /* bt_plugin_so_shared_lib_handle_create() logs more details */
1435 BT_ASSERT(!shared_lib_handle
);
1436 BT_LOGE_STR("Cannot create shared library handle.");
1440 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1441 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1442 descriptors_begin
= get_begin_section_plugin_descriptors();
1445 * Use this first symbol to know whether or not this
1446 * shared object _looks like_ a Babeltrace plugin. Since
1447 * g_module_symbol() failed, assume that this is not a
1448 * Babeltrace plugin, so it's not an error.
1450 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1451 "symbol=\"%s\"", path
,
1452 "__bt_get_begin_section_plugin_descriptors");
1453 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
1458 * If g_module_symbol() fails for any of the other symbols, fail
1459 * if `fail_on_load_error` is true.
1461 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1462 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1463 descriptors_end
= get_end_section_plugin_descriptors();
1465 BT_LOG_WRITE(fail_on_load_error
? BT_LOG_WARN
: BT_LOG_INFO
,
1467 "Cannot resolve plugin symbol: path=\"%s\", "
1468 "symbol=\"%s\"", path
,
1469 "__bt_get_end_section_plugin_descriptors");
1470 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
1471 BT_PLUGIN_STATUS_NOT_FOUND
;
1475 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1476 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1477 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1479 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1480 "symbol=\"%s\"", path
,
1481 "__bt_get_begin_section_plugin_descriptor_attributes");
1484 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1485 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1486 attrs_end
= get_end_section_plugin_descriptor_attributes();
1488 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1489 "symbol=\"%s\"", path
,
1490 "__bt_get_end_section_plugin_descriptor_attributes");
1493 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1494 BT_LOG_WRITE(fail_on_load_error
? BT_LOG_WARN
: BT_LOG_INFO
,
1496 "Found section start or end symbol, but not both: "
1497 "path=\"%s\", symbol-start=\"%s\", "
1498 "symbol-end=\"%s\", symbol-start-addr=%p, "
1499 "symbol-end-addr=%p",
1500 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1501 "__bt_get_end_section_plugin_descriptor_attributes",
1502 attrs_begin
, attrs_end
);
1503 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
1504 BT_PLUGIN_STATUS_NOT_FOUND
;
1508 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1509 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1510 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1512 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1513 "symbol=\"%s\"", path
,
1514 "__bt_get_begin_section_component_class_descriptors");
1517 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1518 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1519 cc_descriptors_end
= get_end_section_component_class_descriptors();
1521 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1522 "symbol=\"%s\"", path
,
1523 "__bt_get_end_section_component_class_descriptors");
1526 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1527 BT_LOG_WRITE(fail_on_load_error
? BT_LOG_WARN
: BT_LOG_INFO
,
1529 "Found section start or end symbol, but not both: "
1530 "path=\"%s\", symbol-start=\"%s\", "
1531 "symbol-end=\"%s\", symbol-start-addr=%p, "
1532 "symbol-end-addr=%p",
1533 path
, "__bt_get_begin_section_component_class_descriptors",
1534 "__bt_get_end_section_component_class_descriptors",
1535 cc_descriptors_begin
, cc_descriptors_end
);
1536 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
1537 BT_PLUGIN_STATUS_NOT_FOUND
;
1541 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1542 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1543 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1545 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1546 "symbol=\"%s\"", path
,
1547 "__bt_get_begin_section_component_class_descriptor_attributes");
1550 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1551 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1552 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1554 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1555 "symbol=\"%s\"", path
,
1556 "__bt_get_end_section_component_class_descriptor_attributes");
1559 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1560 BT_LOG_WRITE(fail_on_load_error
? BT_LOG_WARN
: BT_LOG_INFO
,
1562 "Found section start or end symbol, but not both: "
1563 "path=\"%s\", symbol-start=\"%s\", "
1564 "symbol-end=\"%s\", symbol-start-addr=%p, "
1565 "symbol-end-addr=%p",
1566 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1567 "__bt_get_end_section_component_class_descriptor_attributes",
1568 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1569 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
1570 BT_PLUGIN_STATUS_NOT_FOUND
;
1574 /* Initialize plugin */
1575 BT_LOGD_STR("Initializing plugin object.");
1576 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1578 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1579 cc_descriptors_begin
, cc_descriptors_end
,
1580 cc_descr_attrs_begin
, cc_descr_attrs_end
, plugin_set_out
);
1583 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1588 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1591 bt_list_del(&comp_class
->node
);
1592 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
1593 BT_LOGD("Component class destroyed: removed entry from list: "
1594 "comp-cls-addr=%p", comp_class
);
1597 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1598 struct bt_component_class
*comp_class
)
1600 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1602 BT_ASSERT(plugin
->spec_data
);
1603 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1605 bt_list_add(&comp_class
->node
, &component_class_list
);
1606 comp_class
->so_handle
= spec
->shared_lib_handle
;
1607 bt_object_get_no_null_check(comp_class
->so_handle
);
1609 /* Add our custom destroy listener */
1610 bt_component_class_add_destroy_listener(comp_class
,
1611 plugin_comp_class_destroy_listener
, NULL
);