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/types.h>
36 #include "common/list.h"
44 #include "plugin-so.h"
45 #include "lib/func-status.h"
46 #include "common/common.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 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
108 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
111 shared_lib_handle
= container_of(obj
,
112 struct bt_plugin_so_shared_lib_handle
, base
);
113 const char *path
= shared_lib_handle
->path
?
114 shared_lib_handle
->path
->str
: NULL
;
116 BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"",
117 shared_lib_handle
, path
);
119 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
120 BT_LOGD_STR("Calling user's plugin exit function.");
121 shared_lib_handle
->exit();
122 BT_LOGD_STR("User function returned.");
125 if (shared_lib_handle
->module
) {
128 * Valgrind shows incomplete stack traces when
129 * dynamically loaded libraries are closed before it
130 * finishes. Use the LIBBABELTRACE2_NO_DLCLOSE in a debug
131 * build to avoid this.
133 const char *var
= getenv("LIBBABELTRACE2_NO_DLCLOSE");
135 if (!var
|| strcmp(var
, "1") != 0) {
137 BT_LOGI("Closing GModule: path=\"%s\"", path
);
139 if (!g_module_close(shared_lib_handle
->module
)) {
141 * Just log here: we're in a destructor,
142 * so we cannot append an error cause
143 * (there's no returned status).
145 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
146 g_module_error(), path
);
149 shared_lib_handle
->module
= NULL
;
152 BT_LOGI("Not closing GModule because `LIBBABELTRACE2_NO_DLCLOSE=1`: "
153 "path=\"%s\"", path
);
158 if (shared_lib_handle
->path
) {
159 g_string_free(shared_lib_handle
->path
, TRUE
);
160 shared_lib_handle
->path
= NULL
;
163 g_free(shared_lib_handle
);
167 int bt_plugin_so_shared_lib_handle_create(
169 struct bt_plugin_so_shared_lib_handle
**shared_lib_handle
)
171 int status
= BT_FUNC_STATUS_OK
;
173 BT_ASSERT(shared_lib_handle
);
174 BT_LOGI("Creating shared library handle: path=\"%s\"", path
? path
: "(null)");
175 *shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
176 if (!*shared_lib_handle
) {
177 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one shared library handle.");
178 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
182 bt_object_init_shared(&(*shared_lib_handle
)->base
,
183 bt_plugin_so_shared_lib_handle_destroy
);
189 (*shared_lib_handle
)->path
= g_string_new(path
);
190 if (!(*shared_lib_handle
)->path
) {
191 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
192 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
196 (*shared_lib_handle
)->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
197 if (!(*shared_lib_handle
)->module
) {
199 * INFO-level logging because we're only _trying_ to
200 * open this file as a Babeltrace plugin: if it's not,
201 * it's not an error. And because this can be tried
202 * during bt_plugin_find_all_from_dir(), it's not even a
205 BT_LOGI("Cannot open GModule: %s: path=\"%s\"",
206 g_module_error(), path
);
207 BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle
);
208 status
= BT_FUNC_STATUS_NOT_FOUND
;
215 BT_ASSERT(*shared_lib_handle
|| status
!= BT_FUNC_STATUS_OK
);
216 if (*shared_lib_handle
) {
217 BT_LOGI("Created shared library handle: path=\"%s\", addr=%p",
218 path
? path
: "(null)", *shared_lib_handle
);
225 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
227 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
229 if (!plugin
->spec_data
) {
233 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
235 BT_OBJECT_PUT_REF_AND_RESET(spec
->shared_lib_handle
);
236 g_free(plugin
->spec_data
);
237 plugin
->spec_data
= NULL
;
241 * This function does the following:
243 * 1. Iterate on the plugin descriptor attributes section and set the
244 * plugin's attributes depending on the attribute types. This
245 * includes the name of the plugin, its description, and its
246 * initialization function, for example.
248 * 2. Iterate on the component class descriptors section and create one
249 * "full descriptor" (temporary structure) for each one that is found
250 * and attached to our plugin descriptor.
252 * 3. Iterate on the component class descriptor attributes section and
253 * set the corresponding full descriptor's attributes depending on
254 * the attribute types. This includes the description of the
255 * component class, as well as its initialization and destroy
258 * 4. Call the user's plugin initialization function, if any is
261 * 5. For each full component class descriptor, create a component class
262 * object, set its optional attributes, and add it to the plugin
265 * 6. Freeze the plugin object.
268 int bt_plugin_so_init(struct bt_plugin
*plugin
,
269 bool fail_on_load_error
,
270 const struct __bt_plugin_descriptor
*descriptor
,
271 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
272 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
273 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
274 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
275 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
276 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
279 * This structure's members point to the plugin's memory
282 struct comp_class_full_descriptor
{
283 const struct __bt_plugin_component_class_descriptor
*descriptor
;
284 const char *description
;
289 bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions
;
290 bt_component_class_source_initialize_method init
;
291 bt_component_class_source_finalize_method finalize
;
292 bt_component_class_source_query_method query
;
293 bt_component_class_source_output_port_connected_method output_port_connected
;
294 bt_message_iterator_class_initialize_method msg_iter_initialize
;
295 bt_message_iterator_class_finalize_method msg_iter_finalize
;
296 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
297 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning
;
298 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
299 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning
;
303 bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions
;
304 bt_component_class_filter_initialize_method init
;
305 bt_component_class_filter_finalize_method finalize
;
306 bt_component_class_filter_query_method query
;
307 bt_component_class_filter_input_port_connected_method input_port_connected
;
308 bt_component_class_filter_output_port_connected_method output_port_connected
;
309 bt_message_iterator_class_initialize_method msg_iter_initialize
;
310 bt_message_iterator_class_finalize_method msg_iter_finalize
;
311 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
312 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning
;
313 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
314 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning
;
318 bt_component_class_sink_get_supported_mip_versions_method get_supported_mip_versions
;
319 bt_component_class_sink_initialize_method init
;
320 bt_component_class_sink_finalize_method finalize
;
321 bt_component_class_sink_query_method query
;
322 bt_component_class_sink_input_port_connected_method input_port_connected
;
323 bt_component_class_sink_graph_is_configured_method graph_is_configured
;
328 int status
= BT_FUNC_STATUS_OK
;
329 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
330 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
331 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
332 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
333 GArray
*comp_class_full_descriptors
;
336 struct bt_message_iterator_class
*msg_iter_class
= NULL
;
338 BT_LOGI("Initializing plugin object from descriptors found in sections: "
339 "plugin-addr=%p, plugin-path=\"%s\", "
340 "attrs-begin-addr=%p, attrs-end-addr=%p, "
341 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
342 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
344 spec
->shared_lib_handle
->path
?
345 spec
->shared_lib_handle
->path
->str
: NULL
,
346 attrs_begin
, attrs_end
,
347 cc_descriptors_begin
, cc_descriptors_end
,
348 cc_descr_attrs_begin
, cc_descr_attrs_end
);
349 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
350 sizeof(struct comp_class_full_descriptor
));
351 if (!comp_class_full_descriptors
) {
352 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
353 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
357 /* Set mandatory attributes */
358 spec
->descriptor
= descriptor
;
359 bt_plugin_set_name(plugin
, descriptor
->name
);
362 * Find and set optional attributes attached to this plugin
365 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
366 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
373 if (cur_attr
->plugin_descriptor
!= descriptor
) {
377 switch (cur_attr
->type
) {
378 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
379 spec
->init
= cur_attr
->value
.init
;
381 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
382 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
384 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
385 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
387 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
388 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
390 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
391 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
393 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
394 bt_plugin_set_version(plugin
,
395 (unsigned int) cur_attr
->value
.version
.major
,
396 (unsigned int) cur_attr
->value
.version
.minor
,
397 (unsigned int) cur_attr
->value
.version
.patch
,
398 cur_attr
->value
.version
.extra
);
401 if (fail_on_load_error
) {
402 BT_LIB_LOGW_APPEND_CAUSE(
403 "Unknown plugin descriptor attribute: "
404 "plugin-path=\"%s\", plugin-name=\"%s\", "
405 "attr-type-name=\"%s\", attr-type-id=%d",
406 spec
->shared_lib_handle
->path
?
407 spec
->shared_lib_handle
->path
->str
:
409 descriptor
->name
, cur_attr
->type_name
,
411 status
= BT_FUNC_STATUS_ERROR
;
415 "Ignoring unknown plugin descriptor attribute: "
416 "plugin-path=\"%s\", plugin-name=\"%s\", "
417 "attr-type-name=\"%s\", attr-type-id=%d",
418 spec
->shared_lib_handle
->path
?
419 spec
->shared_lib_handle
->path
->str
:
421 descriptor
->name
, cur_attr
->type_name
,
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};
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
) {
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_GET_SUPPORTED_MIP_VERSIONS_METHOD
:
495 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
496 cc_full_descr
->methods
.source
.get_supported_mip_versions
=
497 cur_cc_descr_attr
->value
.source_get_supported_mip_versions_method
;
499 case BT_COMPONENT_CLASS_TYPE_FILTER
:
500 cc_full_descr
->methods
.filter
.get_supported_mip_versions
=
501 cur_cc_descr_attr
->value
.filter_get_supported_mip_versions_method
;
503 case BT_COMPONENT_CLASS_TYPE_SINK
:
504 cc_full_descr
->methods
.sink
.get_supported_mip_versions
=
505 cur_cc_descr_attr
->value
.sink_get_supported_mip_versions_method
;
511 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INITIALIZE_METHOD
:
513 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
514 cc_full_descr
->methods
.source
.init
=
515 cur_cc_descr_attr
->value
.source_initialize_method
;
517 case BT_COMPONENT_CLASS_TYPE_FILTER
:
518 cc_full_descr
->methods
.filter
.init
=
519 cur_cc_descr_attr
->value
.filter_initialize_method
;
521 case BT_COMPONENT_CLASS_TYPE_SINK
:
522 cc_full_descr
->methods
.sink
.init
=
523 cur_cc_descr_attr
->value
.sink_initialize_method
;
529 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
531 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
532 cc_full_descr
->methods
.source
.finalize
=
533 cur_cc_descr_attr
->value
.source_finalize_method
;
535 case BT_COMPONENT_CLASS_TYPE_FILTER
:
536 cc_full_descr
->methods
.filter
.finalize
=
537 cur_cc_descr_attr
->value
.filter_finalize_method
;
539 case BT_COMPONENT_CLASS_TYPE_SINK
:
540 cc_full_descr
->methods
.sink
.finalize
=
541 cur_cc_descr_attr
->value
.sink_finalize_method
;
547 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
549 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
550 cc_full_descr
->methods
.source
.query
=
551 cur_cc_descr_attr
->value
.source_query_method
;
553 case BT_COMPONENT_CLASS_TYPE_FILTER
:
554 cc_full_descr
->methods
.filter
.query
=
555 cur_cc_descr_attr
->value
.filter_query_method
;
557 case BT_COMPONENT_CLASS_TYPE_SINK
:
558 cc_full_descr
->methods
.sink
.query
=
559 cur_cc_descr_attr
->value
.sink_query_method
;
565 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD
:
567 case BT_COMPONENT_CLASS_TYPE_FILTER
:
568 cc_full_descr
->methods
.filter
.input_port_connected
=
569 cur_cc_descr_attr
->value
.filter_input_port_connected_method
;
571 case BT_COMPONENT_CLASS_TYPE_SINK
:
572 cc_full_descr
->methods
.sink
.input_port_connected
=
573 cur_cc_descr_attr
->value
.sink_input_port_connected_method
;
579 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD
:
581 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
582 cc_full_descr
->methods
.source
.output_port_connected
=
583 cur_cc_descr_attr
->value
.source_output_port_connected_method
;
585 case BT_COMPONENT_CLASS_TYPE_FILTER
:
586 cc_full_descr
->methods
.filter
.output_port_connected
=
587 cur_cc_descr_attr
->value
.filter_output_port_connected_method
;
593 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD
:
595 case BT_COMPONENT_CLASS_TYPE_SINK
:
596 cc_full_descr
->methods
.sink
.graph_is_configured
=
597 cur_cc_descr_attr
->value
.sink_graph_is_configured_method
;
603 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INITIALIZE_METHOD
:
605 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
606 cc_full_descr
->methods
.source
.msg_iter_initialize
=
607 cur_cc_descr_attr
->value
.msg_iter_initialize_method
;
609 case BT_COMPONENT_CLASS_TYPE_FILTER
:
610 cc_full_descr
->methods
.filter
.msg_iter_initialize
=
611 cur_cc_descr_attr
->value
.msg_iter_initialize_method
;
617 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD
:
619 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
620 cc_full_descr
->methods
.source
.msg_iter_finalize
=
621 cur_cc_descr_attr
->value
.msg_iter_finalize_method
;
623 case BT_COMPONENT_CLASS_TYPE_FILTER
:
624 cc_full_descr
->methods
.filter
.msg_iter_finalize
=
625 cur_cc_descr_attr
->value
.msg_iter_finalize_method
;
631 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD
:
633 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
634 cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
=
635 cur_cc_descr_attr
->value
.msg_iter_seek_ns_from_origin_method
;
637 case BT_COMPONENT_CLASS_TYPE_FILTER
:
638 cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
=
639 cur_cc_descr_attr
->value
.msg_iter_seek_ns_from_origin_method
;
645 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD
:
647 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
648 cc_full_descr
->methods
.source
.msg_iter_seek_beginning
=
649 cur_cc_descr_attr
->value
.msg_iter_seek_beginning_method
;
651 case BT_COMPONENT_CLASS_TYPE_FILTER
:
652 cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
=
653 cur_cc_descr_attr
->value
.msg_iter_seek_beginning_method
;
659 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD
:
661 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
662 cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
=
663 cur_cc_descr_attr
->value
.msg_iter_can_seek_ns_from_origin_method
;
665 case BT_COMPONENT_CLASS_TYPE_FILTER
:
666 cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
=
667 cur_cc_descr_attr
->value
.msg_iter_can_seek_ns_from_origin_method
;
673 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD
:
675 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
676 cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
=
677 cur_cc_descr_attr
->value
.msg_iter_can_seek_beginning_method
;
679 case BT_COMPONENT_CLASS_TYPE_FILTER
:
680 cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
=
681 cur_cc_descr_attr
->value
.msg_iter_can_seek_beginning_method
;
688 if (fail_on_load_error
) {
689 BT_LIB_LOGW_APPEND_CAUSE(
690 "Unknown component class descriptor attribute: "
691 "plugin-path=\"%s\", "
692 "plugin-name=\"%s\", "
693 "comp-class-name=\"%s\", "
694 "comp-class-type=%s, "
695 "attr-type-name=\"%s\", "
697 spec
->shared_lib_handle
->path
?
698 spec
->shared_lib_handle
->path
->str
:
701 cur_cc_descr_attr
->comp_class_descriptor
->name
,
702 bt_component_class_type_string(
703 cur_cc_descr_attr
->comp_class_descriptor
->type
),
704 cur_cc_descr_attr
->type_name
,
705 cur_cc_descr_attr
->type
);
706 status
= BT_FUNC_STATUS_ERROR
;
710 "Ignoring unknown component class descriptor attribute: "
711 "plugin-path=\"%s\", "
712 "plugin-name=\"%s\", "
713 "comp-class-name=\"%s\", "
714 "comp-class-type=%s, "
715 "attr-type-name=\"%s\", "
717 spec
->shared_lib_handle
->path
?
718 spec
->shared_lib_handle
->path
->str
:
721 cur_cc_descr_attr
->comp_class_descriptor
->name
,
722 bt_component_class_type_string(
723 cur_cc_descr_attr
->comp_class_descriptor
->type
),
724 cur_cc_descr_attr
->type_name
,
725 cur_cc_descr_attr
->type
);
733 /* Initialize plugin */
735 enum bt_plugin_initialize_func_status init_status
;
737 BT_LOGD_STR("Calling user's plugin initialization function.");
738 init_status
= spec
->init((void *) plugin
);
739 BT_LOGD("User function returned: status=%s",
740 bt_common_func_status_string(init_status
));
742 if (init_status
< 0) {
743 if (fail_on_load_error
) {
744 BT_LIB_LOGW_APPEND_CAUSE(
745 "User's plugin initialization function failed: "
747 bt_common_func_status_string(init_status
));
748 status
= init_status
;
752 "User's plugin initialization function failed: "
754 bt_common_func_status_string(init_status
));
755 status
= BT_FUNC_STATUS_NOT_FOUND
;
762 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
764 /* Add described component classes to plugin */
765 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
766 struct comp_class_full_descriptor
*cc_full_descr
=
767 &g_array_index(comp_class_full_descriptors
,
768 struct comp_class_full_descriptor
, i
);
769 struct bt_component_class
*comp_class
= NULL
;
770 struct bt_component_class_source
*src_comp_class
= NULL
;
771 struct bt_component_class_filter
*flt_comp_class
= NULL
;
772 struct bt_component_class_sink
*sink_comp_class
= NULL
;
774 BT_LOGI("Creating and setting properties of plugin's component class: "
775 "plugin-path=\"%s\", plugin-name=\"%s\", "
776 "comp-class-name=\"%s\", comp-class-type=%s",
777 spec
->shared_lib_handle
->path
?
778 spec
->shared_lib_handle
->path
->str
:
781 cc_full_descr
->descriptor
->name
,
782 bt_component_class_type_string(
783 cc_full_descr
->descriptor
->type
));
785 if (cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
786 cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_FILTER
) {
787 bt_message_iterator_class_next_method next_method
;
788 bt_message_iterator_class_initialize_method init_method
;
789 bt_message_iterator_class_finalize_method fini_method
;
790 bt_message_iterator_class_seek_ns_from_origin_method seek_ns_from_origin_method
;
791 bt_message_iterator_class_seek_beginning_method seek_beginning_method
;
792 bt_message_iterator_class_can_seek_ns_from_origin_method can_seek_ns_from_origin_method
;
793 bt_message_iterator_class_can_seek_beginning_method can_seek_beginning_method
;
795 if (cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
796 next_method
= cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
;
797 init_method
= cc_full_descr
->methods
.source
.msg_iter_initialize
;
798 fini_method
= cc_full_descr
->methods
.source
.msg_iter_finalize
;
799 seek_ns_from_origin_method
= cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
;
800 can_seek_ns_from_origin_method
= cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
;
801 seek_beginning_method
= cc_full_descr
->methods
.source
.msg_iter_seek_beginning
;
802 can_seek_beginning_method
= cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
;
804 next_method
= cc_full_descr
->descriptor
->methods
.filter
.msg_iter_next
;
805 init_method
= cc_full_descr
->methods
.filter
.msg_iter_initialize
;
806 fini_method
= cc_full_descr
->methods
.filter
.msg_iter_finalize
;
807 seek_ns_from_origin_method
= cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
;
808 can_seek_ns_from_origin_method
= cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
;
809 seek_beginning_method
= cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
;
810 can_seek_beginning_method
= cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
;
813 msg_iter_class
= bt_message_iterator_class_create(next_method
);
814 if (!msg_iter_class
) {
815 BT_LIB_LOGE_APPEND_CAUSE(
816 "Cannot create message iterator class.");
817 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
822 ret
= bt_message_iterator_class_set_initialize_method(
823 msg_iter_class
, init_method
);
825 BT_LIB_LOGE_APPEND_CAUSE(
826 "Cannot set message iterator initialization method.");
827 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
833 ret
= bt_message_iterator_class_set_finalize_method(
834 msg_iter_class
, fini_method
);
836 BT_LIB_LOGE_APPEND_CAUSE(
837 "Cannot set message iterator finalization method.");
838 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
843 if (seek_ns_from_origin_method
) {
844 ret
= bt_message_iterator_class_set_seek_ns_from_origin_methods(
846 seek_ns_from_origin_method
,
847 can_seek_ns_from_origin_method
);
849 BT_LIB_LOGE_APPEND_CAUSE(
850 "Cannot set message iterator \"seek nanoseconds from origin\" methods.");
851 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
856 if (seek_beginning_method
) {
857 ret
= bt_message_iterator_class_set_seek_beginning_methods(
859 seek_beginning_method
,
860 can_seek_beginning_method
);
862 BT_LIB_LOGE_APPEND_CAUSE(
863 "Cannot set message iterator \"seek beginning\" methods.");
864 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
870 switch (cc_full_descr
->descriptor
->type
) {
871 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
872 BT_ASSERT(msg_iter_class
);
874 src_comp_class
= bt_component_class_source_create(
875 cc_full_descr
->descriptor
->name
, msg_iter_class
);
876 comp_class
= bt_component_class_source_as_component_class(
879 case BT_COMPONENT_CLASS_TYPE_FILTER
:
880 BT_ASSERT(msg_iter_class
);
882 flt_comp_class
= bt_component_class_filter_create(
883 cc_full_descr
->descriptor
->name
, msg_iter_class
);
884 comp_class
= bt_component_class_filter_as_component_class(
887 case BT_COMPONENT_CLASS_TYPE_SINK
:
888 BT_ASSERT(!msg_iter_class
);
890 sink_comp_class
= bt_component_class_sink_create(
891 cc_full_descr
->descriptor
->name
,
892 cc_full_descr
->descriptor
->methods
.sink
.consume
);
893 comp_class
= bt_component_class_sink_as_component_class(
897 if (fail_on_load_error
) {
898 BT_LIB_LOGW_APPEND_CAUSE(
899 "Unknown component class type: "
900 "plugin-path=\"%s\", plugin-name=\"%s\", "
901 "comp-class-name=\"%s\", comp-class-type=%d",
902 spec
->shared_lib_handle
->path
->str
?
903 spec
->shared_lib_handle
->path
->str
:
906 cc_full_descr
->descriptor
->name
,
907 cc_full_descr
->descriptor
->type
);
908 status
= BT_FUNC_STATUS_ERROR
;
912 "Ignoring unknown component class type: "
913 "plugin-path=\"%s\", plugin-name=\"%s\", "
914 "comp-class-name=\"%s\", comp-class-type=%d",
915 spec
->shared_lib_handle
->path
->str
?
916 spec
->shared_lib_handle
->path
->str
:
919 cc_full_descr
->descriptor
->name
,
920 cc_full_descr
->descriptor
->type
);
926 BT_LIB_LOGE_APPEND_CAUSE(
927 "Cannot create component class.");
928 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
933 * The component class has taken a reference on the message
934 * iterator class, so we can drop ours. The message iterator
935 * class will get destroyed at the same time as the component
938 bt_message_iterator_class_put_ref(msg_iter_class
);
939 msg_iter_class
= NULL
;
941 if (cc_full_descr
->description
) {
942 ret
= bt_component_class_set_description(
943 comp_class
, cc_full_descr
->description
);
945 BT_LIB_LOGE_APPEND_CAUSE(
946 "Cannot set component class's description.");
947 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
948 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
953 if (cc_full_descr
->help
) {
954 ret
= bt_component_class_set_help(comp_class
,
955 cc_full_descr
->help
);
957 BT_LIB_LOGE_APPEND_CAUSE(
958 "Cannot set component class's help string.");
959 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
960 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
965 switch (cc_full_descr
->descriptor
->type
) {
966 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
967 if (cc_full_descr
->methods
.source
.get_supported_mip_versions
) {
968 ret
= bt_component_class_source_set_get_supported_mip_versions_method(
970 cc_full_descr
->methods
.source
.get_supported_mip_versions
);
972 BT_LIB_LOGE_APPEND_CAUSE(
973 "Cannot set source component class's \"get supported MIP versions\" method.");
974 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
975 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
980 if (cc_full_descr
->methods
.source
.init
) {
981 ret
= bt_component_class_source_set_initialize_method(
983 cc_full_descr
->methods
.source
.init
);
985 BT_LIB_LOGE_APPEND_CAUSE(
986 "Cannot set source component class's initialization method.");
987 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
988 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
993 if (cc_full_descr
->methods
.source
.finalize
) {
994 ret
= bt_component_class_source_set_finalize_method(
996 cc_full_descr
->methods
.source
.finalize
);
998 BT_LIB_LOGE_APPEND_CAUSE(
999 "Cannot set source component class's finalization method.");
1000 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1001 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1006 if (cc_full_descr
->methods
.source
.query
) {
1007 ret
= bt_component_class_source_set_query_method(
1009 cc_full_descr
->methods
.source
.query
);
1011 BT_LIB_LOGE_APPEND_CAUSE(
1012 "Cannot set source component class's query method.");
1013 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1014 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1019 if (cc_full_descr
->methods
.source
.output_port_connected
) {
1020 ret
= bt_component_class_source_set_output_port_connected_method(
1022 cc_full_descr
->methods
.source
.output_port_connected
);
1024 BT_LIB_LOGE_APPEND_CAUSE(
1025 "Cannot set source component class's \"output port connected\" method.");
1026 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1027 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1033 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1034 if (cc_full_descr
->methods
.filter
.get_supported_mip_versions
) {
1035 ret
= bt_component_class_filter_set_get_supported_mip_versions_method(
1037 cc_full_descr
->methods
.filter
.get_supported_mip_versions
);
1039 BT_LIB_LOGE_APPEND_CAUSE(
1040 "Cannot set filter component class's \"get supported MIP versions\" method.");
1041 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1042 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1047 if (cc_full_descr
->methods
.filter
.init
) {
1048 ret
= bt_component_class_filter_set_initialize_method(
1050 cc_full_descr
->methods
.filter
.init
);
1052 BT_LIB_LOGE_APPEND_CAUSE(
1053 "Cannot set filter component class's initialization method.");
1054 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1055 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1060 if (cc_full_descr
->methods
.filter
.finalize
) {
1061 ret
= bt_component_class_filter_set_finalize_method(
1063 cc_full_descr
->methods
.filter
.finalize
);
1065 BT_LIB_LOGE_APPEND_CAUSE(
1066 "Cannot set filter component class's finalization method.");
1067 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1068 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1073 if (cc_full_descr
->methods
.filter
.query
) {
1074 ret
= bt_component_class_filter_set_query_method(
1076 cc_full_descr
->methods
.filter
.query
);
1078 BT_LIB_LOGE_APPEND_CAUSE(
1079 "Cannot set filter component class's query method.");
1080 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1081 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1086 if (cc_full_descr
->methods
.filter
.input_port_connected
) {
1087 ret
= bt_component_class_filter_set_input_port_connected_method(
1089 cc_full_descr
->methods
.filter
.input_port_connected
);
1091 BT_LIB_LOGE_APPEND_CAUSE(
1092 "Cannot set filter component class's \"input port connected\" method.");
1093 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1094 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1099 if (cc_full_descr
->methods
.filter
.output_port_connected
) {
1100 ret
= bt_component_class_filter_set_output_port_connected_method(
1102 cc_full_descr
->methods
.filter
.output_port_connected
);
1104 BT_LIB_LOGE_APPEND_CAUSE(
1105 "Cannot set filter component class's \"output port connected\" method.");
1106 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1107 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1113 case BT_COMPONENT_CLASS_TYPE_SINK
:
1114 if (cc_full_descr
->methods
.sink
.get_supported_mip_versions
) {
1115 ret
= bt_component_class_sink_set_get_supported_mip_versions_method(
1117 cc_full_descr
->methods
.sink
.get_supported_mip_versions
);
1119 BT_LIB_LOGE_APPEND_CAUSE(
1120 "Cannot set sink component class's \"get supported MIP versions\" method.");
1121 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1122 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1127 if (cc_full_descr
->methods
.sink
.init
) {
1128 ret
= bt_component_class_sink_set_initialize_method(
1130 cc_full_descr
->methods
.sink
.init
);
1132 BT_LIB_LOGE_APPEND_CAUSE(
1133 "Cannot set sink component class's initialization method.");
1134 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1135 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1140 if (cc_full_descr
->methods
.sink
.finalize
) {
1141 ret
= bt_component_class_sink_set_finalize_method(
1143 cc_full_descr
->methods
.sink
.finalize
);
1145 BT_LIB_LOGE_APPEND_CAUSE(
1146 "Cannot set sink component class's finalization method.");
1147 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1148 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1153 if (cc_full_descr
->methods
.sink
.query
) {
1154 ret
= bt_component_class_sink_set_query_method(
1156 cc_full_descr
->methods
.sink
.query
);
1158 BT_LIB_LOGE_APPEND_CAUSE(
1159 "Cannot set sink component class's query method.");
1160 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1161 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1166 if (cc_full_descr
->methods
.sink
.input_port_connected
) {
1167 ret
= bt_component_class_sink_set_input_port_connected_method(
1169 cc_full_descr
->methods
.sink
.input_port_connected
);
1171 BT_LIB_LOGE_APPEND_CAUSE(
1172 "Cannot set sink component class's \"input port connected\" method.");
1173 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1174 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1179 if (cc_full_descr
->methods
.sink
.graph_is_configured
) {
1180 ret
= bt_component_class_sink_set_graph_is_configured_method(
1182 cc_full_descr
->methods
.sink
.graph_is_configured
);
1184 BT_LIB_LOGE_APPEND_CAUSE(
1185 "Cannot set sink component class's \"graph is configured\" method.");
1186 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1187 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1198 * Add component class to the plugin object.
1200 * This will call back
1201 * bt_plugin_so_on_add_component_class() so that we can
1202 * add a mapping in the component class list when we
1203 * know the component class is successfully added.
1205 status
= bt_plugin_add_component_class(plugin
,
1206 (void *) comp_class
);
1207 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
1209 BT_LIB_LOGE_APPEND_CAUSE(
1210 "Cannot add component class to plugin.");
1216 bt_message_iterator_class_put_ref(msg_iter_class
);
1217 g_array_free(comp_class_full_descriptors
, TRUE
);
1222 struct bt_plugin
*bt_plugin_so_create_empty(
1223 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
1225 struct bt_plugin
*plugin
;
1226 struct bt_plugin_so_spec_data
*spec
;
1228 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
1233 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
1234 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
1235 if (!plugin
->spec_data
) {
1236 BT_LIB_LOGE_APPEND_CAUSE(
1237 "Failed to allocate one SO plugin specific data structure.");
1241 spec
= plugin
->spec_data
;
1242 spec
->shared_lib_handle
= shared_lib_handle
;
1243 bt_object_get_ref_no_null_check(spec
->shared_lib_handle
);
1247 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1254 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
1257 const int * const *begin_int
= (const int * const *) begin
;
1258 const int * const *end_int
= (const int * const *) end
;
1259 const int * const *iter
;
1261 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
1271 int bt_plugin_so_create_all_from_sections(
1272 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
1273 bool fail_on_load_error
,
1274 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
1275 struct __bt_plugin_descriptor
const * const *descriptors_end
,
1276 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
1277 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
1278 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
1279 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
1280 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
1281 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
,
1282 struct bt_plugin_set
**plugin_set_out
)
1284 int status
= BT_FUNC_STATUS_OK
;
1285 size_t descriptor_count
;
1287 size_t cc_descriptors_count
;
1288 size_t cc_descr_attrs_count
;
1291 BT_ASSERT(shared_lib_handle
);
1292 BT_ASSERT(plugin_set_out
);
1293 *plugin_set_out
= NULL
;
1294 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
1295 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
1296 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
1297 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
1298 BT_LOGI("Creating all SO plugins from sections: "
1299 "plugin-path=\"%s\", "
1300 "descr-begin-addr=%p, descr-end-addr=%p, "
1301 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1302 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1303 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1304 "descr-count=%zu, attrs-count=%zu, "
1305 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1306 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
1307 descriptors_begin
, descriptors_end
,
1308 attrs_begin
, attrs_end
,
1309 cc_descriptors_begin
, cc_descriptors_end
,
1310 cc_descr_attrs_begin
, cc_descr_attrs_end
,
1311 descriptor_count
, attrs_count
,
1312 cc_descriptors_count
, cc_descr_attrs_count
);
1313 *plugin_set_out
= bt_plugin_set_create();
1314 if (!*plugin_set_out
) {
1315 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
1316 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1320 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
1321 const struct __bt_plugin_descriptor
*descriptor
=
1322 descriptors_begin
[i
];
1323 struct bt_plugin
*plugin
;
1329 BT_LOGI("Creating plugin object for plugin: name=\"%s\"",
1331 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
1333 BT_LIB_LOGE_APPEND_CAUSE(
1334 "Cannot create empty shared library handle.");
1335 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1339 if (shared_lib_handle
->path
) {
1340 bt_plugin_set_path(plugin
,
1341 shared_lib_handle
->path
->str
);
1344 status
= bt_plugin_so_init(plugin
, fail_on_load_error
,
1345 descriptor
, attrs_begin
, attrs_end
,
1346 cc_descriptors_begin
, cc_descriptors_end
,
1347 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1348 if (status
== BT_FUNC_STATUS_OK
) {
1349 /* Add to plugin set */
1350 bt_plugin_set_add_plugin(*plugin_set_out
, plugin
);
1351 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1352 } else if (status
< 0) {
1354 * bt_plugin_so_init() handles
1355 * `fail_on_load_error`, so this is a "real"
1358 BT_LIB_LOGW_APPEND_CAUSE(
1359 "Cannot initialize SO plugin object from sections.");
1360 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1367 BT_ASSERT(*plugin_set_out
);
1369 if ((*plugin_set_out
)->plugins
->len
== 0) {
1370 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1371 status
= BT_FUNC_STATUS_NOT_FOUND
;
1377 BT_ASSERT(status
< 0);
1378 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1385 int bt_plugin_so_create_all_from_static(bool fail_on_load_error
,
1386 struct bt_plugin_set
**plugin_set_out
)
1389 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1391 BT_ASSERT(plugin_set_out
);
1392 *plugin_set_out
= NULL
;
1393 status
= bt_plugin_so_shared_lib_handle_create(NULL
,
1394 &shared_lib_handle
);
1395 if (status
!= BT_FUNC_STATUS_OK
) {
1396 BT_ASSERT(!shared_lib_handle
);
1400 BT_ASSERT(shared_lib_handle
);
1401 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
1402 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1404 __bt_get_begin_section_plugin_descriptors(),
1405 __bt_get_end_section_plugin_descriptors(),
1406 __bt_get_begin_section_plugin_descriptor_attributes(),
1407 __bt_get_end_section_plugin_descriptor_attributes(),
1408 __bt_get_begin_section_component_class_descriptors(),
1409 __bt_get_end_section_component_class_descriptors(),
1410 __bt_get_begin_section_component_class_descriptor_attributes(),
1411 __bt_get_end_section_component_class_descriptor_attributes(),
1413 BT_ASSERT((status
== BT_FUNC_STATUS_OK
&& *plugin_set_out
&&
1414 (*plugin_set_out
)->plugins
->len
> 0) || !*plugin_set_out
);
1417 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1422 int bt_plugin_so_create_all_from_file(const char *path
,
1423 bool fail_on_load_error
, struct bt_plugin_set
**plugin_set_out
)
1427 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
1428 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
1429 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
1430 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
1431 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
1432 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
1433 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
1434 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
1435 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
1436 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
1437 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
1438 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
1439 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
1440 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
1441 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
1442 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
1443 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
1444 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1447 BT_ASSERT(plugin_set_out
);
1448 *plugin_set_out
= NULL
;
1449 path_len
= strlen(path
);
1452 * An SO plugin file must have a known plugin file suffix. So the file
1453 * path must be longer than the suffix length.
1455 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
1456 BT_LOGI("Path is too short to be an `.so` or `.la` plugin file:"
1457 "path=%s, path-length=%zu, min-length=%zu",
1458 path
, path_len
, PLUGIN_SUFFIX_LEN
);
1459 status
= BT_FUNC_STATUS_NOT_FOUND
;
1463 BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path
);
1467 * Check if the file ends with a known plugin file type suffix
1468 * (i.e. .so or .la on Linux).
1470 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
1471 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
1472 LIBTOOL_PLUGIN_SUFFIX_LEN
);
1473 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
1474 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
1475 NATIVE_PLUGIN_SUFFIX_LEN
);
1476 if (!is_shared_object
&& !is_libtool_wrapper
) {
1477 /* Name indicates this is not a plugin file; not an error */
1478 BT_LOGI("File is not an SO plugin file: path=\"%s\"", path
);
1479 status
= BT_FUNC_STATUS_NOT_FOUND
;
1483 status
= bt_plugin_so_shared_lib_handle_create(path
,
1484 &shared_lib_handle
);
1485 if (status
!= BT_FUNC_STATUS_OK
) {
1486 /* bt_plugin_so_shared_lib_handle_create() logs more details */
1487 BT_ASSERT(!shared_lib_handle
);
1491 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1492 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1493 descriptors_begin
= get_begin_section_plugin_descriptors();
1496 * Use this first symbol to know whether or not this
1497 * shared object _looks like_ a Babeltrace plugin. Since
1498 * g_module_symbol() failed, assume that this is not a
1499 * Babeltrace plugin, so it's not an error.
1501 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1502 "symbol=\"%s\"", path
,
1503 "__bt_get_begin_section_plugin_descriptors");
1504 status
= BT_FUNC_STATUS_NOT_FOUND
;
1509 * If g_module_symbol() fails for any of the other symbols, fail
1510 * if `fail_on_load_error` is true.
1512 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1513 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1514 descriptors_end
= get_end_section_plugin_descriptors();
1516 if (fail_on_load_error
) {
1517 BT_LIB_LOGW_APPEND_CAUSE(
1518 "Cannot resolve plugin symbol: path=\"%s\", "
1519 "symbol=\"%s\"", path
,
1520 "__bt_get_end_section_plugin_descriptors");
1521 status
= BT_FUNC_STATUS_ERROR
;
1524 "Cannot resolve plugin symbol: path=\"%s\", "
1525 "symbol=\"%s\"", path
,
1526 "__bt_get_end_section_plugin_descriptors");
1527 status
= BT_FUNC_STATUS_NOT_FOUND
;
1533 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1534 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1535 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1537 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1538 "symbol=\"%s\"", path
,
1539 "__bt_get_begin_section_plugin_descriptor_attributes");
1542 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1543 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1544 attrs_end
= get_end_section_plugin_descriptor_attributes();
1546 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1547 "symbol=\"%s\"", path
,
1548 "__bt_get_end_section_plugin_descriptor_attributes");
1551 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1552 if (fail_on_load_error
) {
1553 BT_LIB_LOGW_APPEND_CAUSE(
1554 "Found section start or end symbol, but not both: "
1555 "path=\"%s\", symbol-start=\"%s\", "
1556 "symbol-end=\"%s\", symbol-start-addr=%p, "
1557 "symbol-end-addr=%p",
1558 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1559 "__bt_get_end_section_plugin_descriptor_attributes",
1560 attrs_begin
, attrs_end
);
1561 status
= BT_FUNC_STATUS_ERROR
;
1564 "Found section start or end symbol, but not both: "
1565 "path=\"%s\", symbol-start=\"%s\", "
1566 "symbol-end=\"%s\", symbol-start-addr=%p, "
1567 "symbol-end-addr=%p",
1568 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1569 "__bt_get_end_section_plugin_descriptor_attributes",
1570 attrs_begin
, attrs_end
);
1571 status
= BT_FUNC_STATUS_NOT_FOUND
;
1577 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1578 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1579 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1581 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1582 "symbol=\"%s\"", path
,
1583 "__bt_get_begin_section_component_class_descriptors");
1586 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1587 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1588 cc_descriptors_end
= get_end_section_component_class_descriptors();
1590 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1591 "symbol=\"%s\"", path
,
1592 "__bt_get_end_section_component_class_descriptors");
1595 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1596 if (fail_on_load_error
) {
1597 BT_LIB_LOGW_APPEND_CAUSE(
1598 "Found section start or end symbol, but not both: "
1599 "path=\"%s\", symbol-start=\"%s\", "
1600 "symbol-end=\"%s\", symbol-start-addr=%p, "
1601 "symbol-end-addr=%p",
1602 path
, "__bt_get_begin_section_component_class_descriptors",
1603 "__bt_get_end_section_component_class_descriptors",
1604 cc_descriptors_begin
, cc_descriptors_end
);
1605 status
= BT_FUNC_STATUS_ERROR
;
1608 "Found section start or end symbol, but not both: "
1609 "path=\"%s\", symbol-start=\"%s\", "
1610 "symbol-end=\"%s\", symbol-start-addr=%p, "
1611 "symbol-end-addr=%p",
1612 path
, "__bt_get_begin_section_component_class_descriptors",
1613 "__bt_get_end_section_component_class_descriptors",
1614 cc_descriptors_begin
, cc_descriptors_end
);
1615 status
= BT_FUNC_STATUS_NOT_FOUND
;
1621 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1622 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1623 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1625 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1626 "symbol=\"%s\"", path
,
1627 "__bt_get_begin_section_component_class_descriptor_attributes");
1630 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1631 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1632 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1634 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1635 "symbol=\"%s\"", path
,
1636 "__bt_get_end_section_component_class_descriptor_attributes");
1639 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1640 if (fail_on_load_error
) {
1641 BT_LIB_LOGW_APPEND_CAUSE(
1642 "Found section start or end symbol, but not both: "
1643 "path=\"%s\", symbol-start=\"%s\", "
1644 "symbol-end=\"%s\", symbol-start-addr=%p, "
1645 "symbol-end-addr=%p",
1646 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1647 "__bt_get_end_section_component_class_descriptor_attributes",
1648 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1649 status
= BT_FUNC_STATUS_ERROR
;
1652 "Found section start or end symbol, but not both: "
1653 "path=\"%s\", symbol-start=\"%s\", "
1654 "symbol-end=\"%s\", symbol-start-addr=%p, "
1655 "symbol-end-addr=%p",
1656 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1657 "__bt_get_end_section_component_class_descriptor_attributes",
1658 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1659 status
= BT_FUNC_STATUS_NOT_FOUND
;
1665 /* Initialize plugin */
1666 BT_LOGD_STR("Initializing plugin object.");
1667 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1669 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1670 cc_descriptors_begin
, cc_descriptors_end
,
1671 cc_descr_attrs_begin
, cc_descr_attrs_end
, plugin_set_out
);
1674 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1679 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1682 bt_list_del(&comp_class
->node
);
1683 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
1684 BT_LOGD("Component class destroyed: removed entry from list: "
1685 "comp-cls-addr=%p", comp_class
);
1688 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1689 struct bt_component_class
*comp_class
)
1691 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1693 BT_ASSERT(plugin
->spec_data
);
1694 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1696 bt_list_add(&comp_class
->node
, &component_class_list
);
1697 comp_class
->so_handle
= spec
->shared_lib_handle
;
1698 bt_object_get_ref_no_null_check(comp_class
->so_handle
);
1700 /* Add our custom destroy listener */
1701 bt_component_class_add_destroy_listener(comp_class
,
1702 plugin_comp_class_destroy_listener
, NULL
);