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
88 * We're not using a GLib linked list here because this destructor is
89 * called after GLib's thread-specific data is destroyed, which contains
90 * the allocated memory for GLib data structures (what's used by
95 BT_LIST_HEAD(component_class_list
);
97 __attribute__((destructor
)) static
98 void fini_comp_class_list(void)
100 struct bt_component_class
*comp_class
, *tmp
;
102 bt_list_for_each_entry_safe(comp_class
, tmp
, &component_class_list
, node
) {
103 bt_list_del(&comp_class
->node
);
104 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
107 BT_LOGD_STR("Released references from all component classes to shared library handles.");
111 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
113 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
116 shared_lib_handle
= container_of(obj
,
117 struct bt_plugin_so_shared_lib_handle
, base
);
118 const char *path
= shared_lib_handle
->path
?
119 shared_lib_handle
->path
->str
: NULL
;
121 BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"",
122 shared_lib_handle
, path
);
124 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
125 BT_LOGD_STR("Calling user's plugin exit function.");
126 shared_lib_handle
->exit();
127 BT_LOGD_STR("User function returned.");
130 if (shared_lib_handle
->module
) {
133 * Valgrind shows incomplete stack traces when
134 * dynamically loaded libraries are closed before it
135 * finishes. Use the LIBBABELTRACE2_NO_DLCLOSE in a debug
136 * build to avoid this.
138 const char *var
= getenv("LIBBABELTRACE2_NO_DLCLOSE");
140 if (!var
|| strcmp(var
, "1") != 0) {
142 BT_LOGI("Closing GModule: path=\"%s\"", path
);
144 if (!g_module_close(shared_lib_handle
->module
)) {
146 * Just log here: we're in a destructor,
147 * so we cannot append an error cause
148 * (there's no returned status).
150 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
151 g_module_error(), path
);
154 shared_lib_handle
->module
= NULL
;
157 BT_LOGI("Not closing GModule because `LIBBABELTRACE2_NO_DLCLOSE=1`: "
158 "path=\"%s\"", path
);
163 if (shared_lib_handle
->path
) {
164 g_string_free(shared_lib_handle
->path
, TRUE
);
165 shared_lib_handle
->path
= NULL
;
168 g_free(shared_lib_handle
);
172 int bt_plugin_so_shared_lib_handle_create(
174 struct bt_plugin_so_shared_lib_handle
**shared_lib_handle
)
176 int status
= BT_FUNC_STATUS_OK
;
178 BT_ASSERT(shared_lib_handle
);
179 BT_LOGI("Creating shared library handle: path=\"%s\"", path
);
180 *shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
181 if (!*shared_lib_handle
) {
182 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one shared library handle.");
183 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
187 bt_object_init_shared(&(*shared_lib_handle
)->base
,
188 bt_plugin_so_shared_lib_handle_destroy
);
194 (*shared_lib_handle
)->path
= g_string_new(path
);
195 if (!(*shared_lib_handle
)->path
) {
196 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
197 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
201 (*shared_lib_handle
)->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
202 if (!(*shared_lib_handle
)->module
) {
204 * INFO-level logging because we're only _trying_ to
205 * open this file as a Babeltrace plugin: if it's not,
206 * it's not an error. And because this can be tried
207 * during bt_plugin_find_all_from_dir(), it's not even a
210 BT_LOGI("Cannot open GModule: %s: path=\"%s\"",
211 g_module_error(), path
);
212 BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle
);
213 status
= BT_FUNC_STATUS_NOT_FOUND
;
220 BT_ASSERT(*shared_lib_handle
|| status
!= BT_FUNC_STATUS_OK
);
221 if (*shared_lib_handle
) {
222 BT_LOGI("Created shared library handle: path=\"%s\", addr=%p",
223 path
, *shared_lib_handle
);
230 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
232 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
234 if (!plugin
->spec_data
) {
238 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
240 BT_OBJECT_PUT_REF_AND_RESET(spec
->shared_lib_handle
);
241 g_free(plugin
->spec_data
);
242 plugin
->spec_data
= NULL
;
246 * This function does the following:
248 * 1. Iterate on the plugin descriptor attributes section and set the
249 * plugin's attributes depending on the attribute types. This
250 * includes the name of the plugin, its description, and its
251 * initialization function, for example.
253 * 2. Iterate on the component class descriptors section and create one
254 * "full descriptor" (temporary structure) for each one that is found
255 * and attached to our plugin descriptor.
257 * 3. Iterate on the component class descriptor attributes section and
258 * set the corresponding full descriptor's attributes depending on
259 * the attribute types. This includes the description of the
260 * component class, as well as its initialization and destroy
263 * 4. Call the user's plugin initialization function, if any is
266 * 5. For each full component class descriptor, create a component class
267 * object, set its optional attributes, and add it to the plugin
270 * 6. Freeze the plugin object.
273 int bt_plugin_so_init(struct bt_plugin
*plugin
,
274 bool fail_on_load_error
,
275 const struct __bt_plugin_descriptor
*descriptor
,
276 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
277 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
278 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
279 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
280 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
281 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
284 * This structure's members point to the plugin's memory
287 struct comp_class_full_descriptor
{
288 const struct __bt_plugin_component_class_descriptor
*descriptor
;
289 const char *description
;
294 bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions
;
295 bt_component_class_source_initialize_method init
;
296 bt_component_class_source_finalize_method finalize
;
297 bt_component_class_source_query_method query
;
298 bt_component_class_source_output_port_connected_method output_port_connected
;
299 bt_message_iterator_class_initialize_method msg_iter_initialize
;
300 bt_message_iterator_class_finalize_method msg_iter_finalize
;
301 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
302 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning
;
303 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
304 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning
;
308 bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions
;
309 bt_component_class_filter_initialize_method init
;
310 bt_component_class_filter_finalize_method finalize
;
311 bt_component_class_filter_query_method query
;
312 bt_component_class_filter_input_port_connected_method input_port_connected
;
313 bt_component_class_filter_output_port_connected_method output_port_connected
;
314 bt_message_iterator_class_initialize_method msg_iter_initialize
;
315 bt_message_iterator_class_finalize_method msg_iter_finalize
;
316 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin
;
317 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning
;
318 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin
;
319 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning
;
323 bt_component_class_sink_get_supported_mip_versions_method get_supported_mip_versions
;
324 bt_component_class_sink_initialize_method init
;
325 bt_component_class_sink_finalize_method finalize
;
326 bt_component_class_sink_query_method query
;
327 bt_component_class_sink_input_port_connected_method input_port_connected
;
328 bt_component_class_sink_graph_is_configured_method graph_is_configured
;
333 int status
= BT_FUNC_STATUS_OK
;
334 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
335 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
336 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
337 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
338 GArray
*comp_class_full_descriptors
;
341 struct bt_message_iterator_class
*msg_iter_class
= NULL
;
343 BT_LOGI("Initializing plugin object from descriptors found in sections: "
344 "plugin-addr=%p, plugin-path=\"%s\", "
345 "attrs-begin-addr=%p, attrs-end-addr=%p, "
346 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
347 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
349 spec
->shared_lib_handle
->path
?
350 spec
->shared_lib_handle
->path
->str
: NULL
,
351 attrs_begin
, attrs_end
,
352 cc_descriptors_begin
, cc_descriptors_end
,
353 cc_descr_attrs_begin
, cc_descr_attrs_end
);
354 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
355 sizeof(struct comp_class_full_descriptor
));
356 if (!comp_class_full_descriptors
) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
358 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
362 /* Set mandatory attributes */
363 spec
->descriptor
= descriptor
;
364 bt_plugin_set_name(plugin
, descriptor
->name
);
367 * Find and set optional attributes attached to this plugin
370 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
371 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
378 if (cur_attr
->plugin_descriptor
!= descriptor
) {
382 switch (cur_attr
->type
) {
383 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
384 spec
->init
= cur_attr
->value
.init
;
386 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
387 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
389 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
390 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
392 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
393 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
395 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
396 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
398 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
399 bt_plugin_set_version(plugin
,
400 (unsigned int) cur_attr
->value
.version
.major
,
401 (unsigned int) cur_attr
->value
.version
.minor
,
402 (unsigned int) cur_attr
->value
.version
.patch
,
403 cur_attr
->value
.version
.extra
);
406 if (fail_on_load_error
) {
407 BT_LIB_LOGW_APPEND_CAUSE(
408 "Unknown plugin descriptor attribute: "
409 "plugin-path=\"%s\", plugin-name=\"%s\", "
410 "attr-type-name=\"%s\", attr-type-id=%d",
411 spec
->shared_lib_handle
->path
?
412 spec
->shared_lib_handle
->path
->str
:
414 descriptor
->name
, cur_attr
->type_name
,
416 status
= BT_FUNC_STATUS_ERROR
;
420 "Ignoring unknown plugin descriptor attribute: "
421 "plugin-path=\"%s\", plugin-name=\"%s\", "
422 "attr-type-name=\"%s\", attr-type-id=%d",
423 spec
->shared_lib_handle
->path
?
424 spec
->shared_lib_handle
->path
->str
:
426 descriptor
->name
, cur_attr
->type_name
,
435 * Find component class descriptors attached to this plugin
436 * descriptor and initialize corresponding full component class
437 * descriptors in the array.
439 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
440 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
442 struct comp_class_full_descriptor full_descriptor
= {0};
448 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
452 full_descriptor
.descriptor
= cur_cc_descr
;
453 g_array_append_val(comp_class_full_descriptors
,
458 * Find component class descriptor attributes attached to this
459 * plugin descriptor and update corresponding full component
460 * class descriptors in the array.
462 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
463 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
464 *cur_cc_descr_attr_ptr
;
465 enum bt_component_class_type cc_type
;
467 if (!cur_cc_descr_attr
) {
471 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
476 cc_type
= cur_cc_descr_attr
->comp_class_descriptor
->type
;
478 /* Find the corresponding component class descriptor entry */
479 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
480 struct comp_class_full_descriptor
*cc_full_descr
=
481 &g_array_index(comp_class_full_descriptors
,
482 struct comp_class_full_descriptor
, i
);
484 if (cur_cc_descr_attr
->comp_class_descriptor
!=
485 cc_full_descr
->descriptor
) {
489 switch (cur_cc_descr_attr
->type
) {
490 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
491 cc_full_descr
->description
=
492 cur_cc_descr_attr
->value
.description
;
494 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
495 cc_full_descr
->help
=
496 cur_cc_descr_attr
->value
.help
;
498 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GET_SUPPORTED_MIP_VERSIONS_METHOD
:
500 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
501 cc_full_descr
->methods
.source
.get_supported_mip_versions
=
502 cur_cc_descr_attr
->value
.source_get_supported_mip_versions_method
;
504 case BT_COMPONENT_CLASS_TYPE_FILTER
:
505 cc_full_descr
->methods
.filter
.get_supported_mip_versions
=
506 cur_cc_descr_attr
->value
.filter_get_supported_mip_versions_method
;
508 case BT_COMPONENT_CLASS_TYPE_SINK
:
509 cc_full_descr
->methods
.sink
.get_supported_mip_versions
=
510 cur_cc_descr_attr
->value
.sink_get_supported_mip_versions_method
;
516 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INITIALIZE_METHOD
:
518 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
519 cc_full_descr
->methods
.source
.init
=
520 cur_cc_descr_attr
->value
.source_initialize_method
;
522 case BT_COMPONENT_CLASS_TYPE_FILTER
:
523 cc_full_descr
->methods
.filter
.init
=
524 cur_cc_descr_attr
->value
.filter_initialize_method
;
526 case BT_COMPONENT_CLASS_TYPE_SINK
:
527 cc_full_descr
->methods
.sink
.init
=
528 cur_cc_descr_attr
->value
.sink_initialize_method
;
534 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
536 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
537 cc_full_descr
->methods
.source
.finalize
=
538 cur_cc_descr_attr
->value
.source_finalize_method
;
540 case BT_COMPONENT_CLASS_TYPE_FILTER
:
541 cc_full_descr
->methods
.filter
.finalize
=
542 cur_cc_descr_attr
->value
.filter_finalize_method
;
544 case BT_COMPONENT_CLASS_TYPE_SINK
:
545 cc_full_descr
->methods
.sink
.finalize
=
546 cur_cc_descr_attr
->value
.sink_finalize_method
;
552 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
554 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
555 cc_full_descr
->methods
.source
.query
=
556 cur_cc_descr_attr
->value
.source_query_method
;
558 case BT_COMPONENT_CLASS_TYPE_FILTER
:
559 cc_full_descr
->methods
.filter
.query
=
560 cur_cc_descr_attr
->value
.filter_query_method
;
562 case BT_COMPONENT_CLASS_TYPE_SINK
:
563 cc_full_descr
->methods
.sink
.query
=
564 cur_cc_descr_attr
->value
.sink_query_method
;
570 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD
:
572 case BT_COMPONENT_CLASS_TYPE_FILTER
:
573 cc_full_descr
->methods
.filter
.input_port_connected
=
574 cur_cc_descr_attr
->value
.filter_input_port_connected_method
;
576 case BT_COMPONENT_CLASS_TYPE_SINK
:
577 cc_full_descr
->methods
.sink
.input_port_connected
=
578 cur_cc_descr_attr
->value
.sink_input_port_connected_method
;
584 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD
:
586 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
587 cc_full_descr
->methods
.source
.output_port_connected
=
588 cur_cc_descr_attr
->value
.source_output_port_connected_method
;
590 case BT_COMPONENT_CLASS_TYPE_FILTER
:
591 cc_full_descr
->methods
.filter
.output_port_connected
=
592 cur_cc_descr_attr
->value
.filter_output_port_connected_method
;
598 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD
:
600 case BT_COMPONENT_CLASS_TYPE_SINK
:
601 cc_full_descr
->methods
.sink
.graph_is_configured
=
602 cur_cc_descr_attr
->value
.sink_graph_is_configured_method
;
608 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INITIALIZE_METHOD
:
610 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
611 cc_full_descr
->methods
.source
.msg_iter_initialize
=
612 cur_cc_descr_attr
->value
.msg_iter_initialize_method
;
614 case BT_COMPONENT_CLASS_TYPE_FILTER
:
615 cc_full_descr
->methods
.filter
.msg_iter_initialize
=
616 cur_cc_descr_attr
->value
.msg_iter_initialize_method
;
622 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD
:
624 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
625 cc_full_descr
->methods
.source
.msg_iter_finalize
=
626 cur_cc_descr_attr
->value
.msg_iter_finalize_method
;
628 case BT_COMPONENT_CLASS_TYPE_FILTER
:
629 cc_full_descr
->methods
.filter
.msg_iter_finalize
=
630 cur_cc_descr_attr
->value
.msg_iter_finalize_method
;
636 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD
:
638 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
639 cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
=
640 cur_cc_descr_attr
->value
.msg_iter_seek_ns_from_origin_method
;
642 case BT_COMPONENT_CLASS_TYPE_FILTER
:
643 cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
=
644 cur_cc_descr_attr
->value
.msg_iter_seek_ns_from_origin_method
;
650 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD
:
652 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
653 cc_full_descr
->methods
.source
.msg_iter_seek_beginning
=
654 cur_cc_descr_attr
->value
.msg_iter_seek_beginning_method
;
656 case BT_COMPONENT_CLASS_TYPE_FILTER
:
657 cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
=
658 cur_cc_descr_attr
->value
.msg_iter_seek_beginning_method
;
664 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD
:
666 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
667 cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
=
668 cur_cc_descr_attr
->value
.msg_iter_can_seek_ns_from_origin_method
;
670 case BT_COMPONENT_CLASS_TYPE_FILTER
:
671 cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
=
672 cur_cc_descr_attr
->value
.msg_iter_can_seek_ns_from_origin_method
;
678 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD
:
680 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
681 cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
=
682 cur_cc_descr_attr
->value
.msg_iter_can_seek_beginning_method
;
684 case BT_COMPONENT_CLASS_TYPE_FILTER
:
685 cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
=
686 cur_cc_descr_attr
->value
.msg_iter_can_seek_beginning_method
;
693 if (fail_on_load_error
) {
694 BT_LIB_LOGW_APPEND_CAUSE(
695 "Unknown component class descriptor attribute: "
696 "plugin-path=\"%s\", "
697 "plugin-name=\"%s\", "
698 "comp-class-name=\"%s\", "
699 "comp-class-type=%s, "
700 "attr-type-name=\"%s\", "
702 spec
->shared_lib_handle
->path
?
703 spec
->shared_lib_handle
->path
->str
:
706 cur_cc_descr_attr
->comp_class_descriptor
->name
,
707 bt_component_class_type_string(
708 cur_cc_descr_attr
->comp_class_descriptor
->type
),
709 cur_cc_descr_attr
->type_name
,
710 cur_cc_descr_attr
->type
);
711 status
= BT_FUNC_STATUS_ERROR
;
715 "Ignoring unknown component class descriptor attribute: "
716 "plugin-path=\"%s\", "
717 "plugin-name=\"%s\", "
718 "comp-class-name=\"%s\", "
719 "comp-class-type=%s, "
720 "attr-type-name=\"%s\", "
722 spec
->shared_lib_handle
->path
?
723 spec
->shared_lib_handle
->path
->str
:
726 cur_cc_descr_attr
->comp_class_descriptor
->name
,
727 bt_component_class_type_string(
728 cur_cc_descr_attr
->comp_class_descriptor
->type
),
729 cur_cc_descr_attr
->type_name
,
730 cur_cc_descr_attr
->type
);
738 /* Initialize plugin */
740 enum bt_plugin_initialize_func_status init_status
;
742 BT_LOGD_STR("Calling user's plugin initialization function.");
743 init_status
= spec
->init((void *) plugin
);
744 BT_LOGD("User function returned: status=%s",
745 bt_common_func_status_string(init_status
));
747 if (init_status
< 0) {
748 if (fail_on_load_error
) {
749 BT_LIB_LOGW_APPEND_CAUSE(
750 "User's plugin initialization function failed: "
752 bt_common_func_status_string(init_status
));
753 status
= init_status
;
757 "User's plugin initialization function failed: "
759 bt_common_func_status_string(init_status
));
760 status
= BT_FUNC_STATUS_NOT_FOUND
;
767 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
769 /* Add described component classes to plugin */
770 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
771 struct comp_class_full_descriptor
*cc_full_descr
=
772 &g_array_index(comp_class_full_descriptors
,
773 struct comp_class_full_descriptor
, i
);
774 struct bt_component_class
*comp_class
= NULL
;
775 struct bt_component_class_source
*src_comp_class
= NULL
;
776 struct bt_component_class_filter
*flt_comp_class
= NULL
;
777 struct bt_component_class_sink
*sink_comp_class
= NULL
;
779 BT_LOGI("Creating and setting properties of plugin's component class: "
780 "plugin-path=\"%s\", plugin-name=\"%s\", "
781 "comp-class-name=\"%s\", comp-class-type=%s",
782 spec
->shared_lib_handle
->path
?
783 spec
->shared_lib_handle
->path
->str
:
786 cc_full_descr
->descriptor
->name
,
787 bt_component_class_type_string(
788 cc_full_descr
->descriptor
->type
));
790 if (cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
791 cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_FILTER
) {
792 bt_message_iterator_class_next_method next_method
;
793 bt_message_iterator_class_initialize_method init_method
;
794 bt_message_iterator_class_finalize_method fini_method
;
795 bt_message_iterator_class_seek_ns_from_origin_method seek_ns_from_origin_method
;
796 bt_message_iterator_class_seek_beginning_method seek_beginning_method
;
797 bt_message_iterator_class_can_seek_ns_from_origin_method can_seek_ns_from_origin_method
;
798 bt_message_iterator_class_can_seek_beginning_method can_seek_beginning_method
;
800 if (cc_full_descr
->descriptor
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
801 next_method
= cc_full_descr
->descriptor
->methods
.source
.msg_iter_next
;
802 init_method
= cc_full_descr
->methods
.source
.msg_iter_initialize
;
803 fini_method
= cc_full_descr
->methods
.source
.msg_iter_finalize
;
804 seek_ns_from_origin_method
= cc_full_descr
->methods
.source
.msg_iter_seek_ns_from_origin
;
805 can_seek_ns_from_origin_method
= cc_full_descr
->methods
.source
.msg_iter_can_seek_ns_from_origin
;
806 seek_beginning_method
= cc_full_descr
->methods
.source
.msg_iter_seek_beginning
;
807 can_seek_beginning_method
= cc_full_descr
->methods
.source
.msg_iter_can_seek_beginning
;
809 next_method
= cc_full_descr
->descriptor
->methods
.filter
.msg_iter_next
;
810 init_method
= cc_full_descr
->methods
.filter
.msg_iter_initialize
;
811 fini_method
= cc_full_descr
->methods
.filter
.msg_iter_finalize
;
812 seek_ns_from_origin_method
= cc_full_descr
->methods
.filter
.msg_iter_seek_ns_from_origin
;
813 can_seek_ns_from_origin_method
= cc_full_descr
->methods
.filter
.msg_iter_can_seek_ns_from_origin
;
814 seek_beginning_method
= cc_full_descr
->methods
.filter
.msg_iter_seek_beginning
;
815 can_seek_beginning_method
= cc_full_descr
->methods
.filter
.msg_iter_can_seek_beginning
;
818 msg_iter_class
= bt_message_iterator_class_create(next_method
);
819 if (!msg_iter_class
) {
820 BT_LIB_LOGE_APPEND_CAUSE(
821 "Cannot create message iterator class.");
822 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
827 ret
= bt_message_iterator_class_set_initialize_method(
828 msg_iter_class
, init_method
);
830 BT_LIB_LOGE_APPEND_CAUSE(
831 "Cannot set message iterator initialization method.");
832 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
838 ret
= bt_message_iterator_class_set_finalize_method(
839 msg_iter_class
, fini_method
);
841 BT_LIB_LOGE_APPEND_CAUSE(
842 "Cannot set message iterator finalization method.");
843 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
848 if (seek_ns_from_origin_method
) {
849 ret
= bt_message_iterator_class_set_seek_ns_from_origin_methods(
851 seek_ns_from_origin_method
,
852 can_seek_ns_from_origin_method
);
854 BT_LIB_LOGE_APPEND_CAUSE(
855 "Cannot set message iterator \"seek nanoseconds from origin\" methods.");
856 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
861 if (seek_beginning_method
) {
862 ret
= bt_message_iterator_class_set_seek_beginning_methods(
864 seek_beginning_method
,
865 can_seek_beginning_method
);
867 BT_LIB_LOGE_APPEND_CAUSE(
868 "Cannot set message iterator \"seek beginning\" methods.");
869 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
875 switch (cc_full_descr
->descriptor
->type
) {
876 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
877 BT_ASSERT(msg_iter_class
);
879 src_comp_class
= bt_component_class_source_create(
880 cc_full_descr
->descriptor
->name
, msg_iter_class
);
881 comp_class
= bt_component_class_source_as_component_class(
884 case BT_COMPONENT_CLASS_TYPE_FILTER
:
885 BT_ASSERT(msg_iter_class
);
887 flt_comp_class
= bt_component_class_filter_create(
888 cc_full_descr
->descriptor
->name
, msg_iter_class
);
889 comp_class
= bt_component_class_filter_as_component_class(
892 case BT_COMPONENT_CLASS_TYPE_SINK
:
893 BT_ASSERT(!msg_iter_class
);
895 sink_comp_class
= bt_component_class_sink_create(
896 cc_full_descr
->descriptor
->name
,
897 cc_full_descr
->descriptor
->methods
.sink
.consume
);
898 comp_class
= bt_component_class_sink_as_component_class(
902 if (fail_on_load_error
) {
903 BT_LIB_LOGW_APPEND_CAUSE(
904 "Unknown component class type: "
905 "plugin-path=\"%s\", plugin-name=\"%s\", "
906 "comp-class-name=\"%s\", comp-class-type=%d",
907 spec
->shared_lib_handle
->path
->str
?
908 spec
->shared_lib_handle
->path
->str
:
911 cc_full_descr
->descriptor
->name
,
912 cc_full_descr
->descriptor
->type
);
913 status
= BT_FUNC_STATUS_ERROR
;
917 "Ignoring unknown component class type: "
918 "plugin-path=\"%s\", plugin-name=\"%s\", "
919 "comp-class-name=\"%s\", comp-class-type=%d",
920 spec
->shared_lib_handle
->path
->str
?
921 spec
->shared_lib_handle
->path
->str
:
924 cc_full_descr
->descriptor
->name
,
925 cc_full_descr
->descriptor
->type
);
931 BT_LIB_LOGE_APPEND_CAUSE(
932 "Cannot create component class.");
933 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
938 * The component class has taken a reference on the message
939 * iterator class, so we can drop ours. The message iterator
940 * class will get destroyed at the same time as the component
943 bt_message_iterator_class_put_ref(msg_iter_class
);
944 msg_iter_class
= NULL
;
946 if (cc_full_descr
->description
) {
947 ret
= bt_component_class_set_description(
948 comp_class
, cc_full_descr
->description
);
950 BT_LIB_LOGE_APPEND_CAUSE(
951 "Cannot set component class's description.");
952 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
953 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
958 if (cc_full_descr
->help
) {
959 ret
= bt_component_class_set_help(comp_class
,
960 cc_full_descr
->help
);
962 BT_LIB_LOGE_APPEND_CAUSE(
963 "Cannot set component class's help string.");
964 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
965 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
970 switch (cc_full_descr
->descriptor
->type
) {
971 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
972 if (cc_full_descr
->methods
.source
.get_supported_mip_versions
) {
973 ret
= bt_component_class_source_set_get_supported_mip_versions_method(
975 cc_full_descr
->methods
.source
.get_supported_mip_versions
);
977 BT_LIB_LOGE_APPEND_CAUSE(
978 "Cannot set source component class's \"get supported MIP versions\" method.");
979 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
980 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
985 if (cc_full_descr
->methods
.source
.init
) {
986 ret
= bt_component_class_source_set_initialize_method(
988 cc_full_descr
->methods
.source
.init
);
990 BT_LIB_LOGE_APPEND_CAUSE(
991 "Cannot set source component class's initialization method.");
992 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
993 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
998 if (cc_full_descr
->methods
.source
.finalize
) {
999 ret
= bt_component_class_source_set_finalize_method(
1001 cc_full_descr
->methods
.source
.finalize
);
1003 BT_LIB_LOGE_APPEND_CAUSE(
1004 "Cannot set source component class's finalization method.");
1005 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1006 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1011 if (cc_full_descr
->methods
.source
.query
) {
1012 ret
= bt_component_class_source_set_query_method(
1014 cc_full_descr
->methods
.source
.query
);
1016 BT_LIB_LOGE_APPEND_CAUSE(
1017 "Cannot set source component class's query method.");
1018 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1019 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1024 if (cc_full_descr
->methods
.source
.output_port_connected
) {
1025 ret
= bt_component_class_source_set_output_port_connected_method(
1027 cc_full_descr
->methods
.source
.output_port_connected
);
1029 BT_LIB_LOGE_APPEND_CAUSE(
1030 "Cannot set source component class's \"output port connected\" method.");
1031 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1032 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class
);
1038 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1039 if (cc_full_descr
->methods
.filter
.get_supported_mip_versions
) {
1040 ret
= bt_component_class_filter_set_get_supported_mip_versions_method(
1042 cc_full_descr
->methods
.filter
.get_supported_mip_versions
);
1044 BT_LIB_LOGE_APPEND_CAUSE(
1045 "Cannot set filter component class's \"get supported MIP versions\" method.");
1046 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1047 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1052 if (cc_full_descr
->methods
.filter
.init
) {
1053 ret
= bt_component_class_filter_set_initialize_method(
1055 cc_full_descr
->methods
.filter
.init
);
1057 BT_LIB_LOGE_APPEND_CAUSE(
1058 "Cannot set filter component class's initialization method.");
1059 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1060 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1065 if (cc_full_descr
->methods
.filter
.finalize
) {
1066 ret
= bt_component_class_filter_set_finalize_method(
1068 cc_full_descr
->methods
.filter
.finalize
);
1070 BT_LIB_LOGE_APPEND_CAUSE(
1071 "Cannot set filter component class's finalization method.");
1072 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1073 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1078 if (cc_full_descr
->methods
.filter
.query
) {
1079 ret
= bt_component_class_filter_set_query_method(
1081 cc_full_descr
->methods
.filter
.query
);
1083 BT_LIB_LOGE_APPEND_CAUSE(
1084 "Cannot set filter component class's query method.");
1085 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1086 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1091 if (cc_full_descr
->methods
.filter
.input_port_connected
) {
1092 ret
= bt_component_class_filter_set_input_port_connected_method(
1094 cc_full_descr
->methods
.filter
.input_port_connected
);
1096 BT_LIB_LOGE_APPEND_CAUSE(
1097 "Cannot set filter component class's \"input port connected\" method.");
1098 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1099 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1104 if (cc_full_descr
->methods
.filter
.output_port_connected
) {
1105 ret
= bt_component_class_filter_set_output_port_connected_method(
1107 cc_full_descr
->methods
.filter
.output_port_connected
);
1109 BT_LIB_LOGE_APPEND_CAUSE(
1110 "Cannot set filter component class's \"output port connected\" method.");
1111 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1112 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class
);
1118 case BT_COMPONENT_CLASS_TYPE_SINK
:
1119 if (cc_full_descr
->methods
.sink
.get_supported_mip_versions
) {
1120 ret
= bt_component_class_sink_set_get_supported_mip_versions_method(
1122 cc_full_descr
->methods
.sink
.get_supported_mip_versions
);
1124 BT_LIB_LOGE_APPEND_CAUSE(
1125 "Cannot set sink component class's \"get supported MIP versions\" method.");
1126 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1127 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1132 if (cc_full_descr
->methods
.sink
.init
) {
1133 ret
= bt_component_class_sink_set_initialize_method(
1135 cc_full_descr
->methods
.sink
.init
);
1137 BT_LIB_LOGE_APPEND_CAUSE(
1138 "Cannot set sink component class's initialization method.");
1139 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1140 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1145 if (cc_full_descr
->methods
.sink
.finalize
) {
1146 ret
= bt_component_class_sink_set_finalize_method(
1148 cc_full_descr
->methods
.sink
.finalize
);
1150 BT_LIB_LOGE_APPEND_CAUSE(
1151 "Cannot set sink component class's finalization method.");
1152 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1153 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1158 if (cc_full_descr
->methods
.sink
.query
) {
1159 ret
= bt_component_class_sink_set_query_method(
1161 cc_full_descr
->methods
.sink
.query
);
1163 BT_LIB_LOGE_APPEND_CAUSE(
1164 "Cannot set sink component class's query method.");
1165 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1166 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1171 if (cc_full_descr
->methods
.sink
.input_port_connected
) {
1172 ret
= bt_component_class_sink_set_input_port_connected_method(
1174 cc_full_descr
->methods
.sink
.input_port_connected
);
1176 BT_LIB_LOGE_APPEND_CAUSE(
1177 "Cannot set sink component class's \"input port connected\" method.");
1178 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1179 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1184 if (cc_full_descr
->methods
.sink
.graph_is_configured
) {
1185 ret
= bt_component_class_sink_set_graph_is_configured_method(
1187 cc_full_descr
->methods
.sink
.graph_is_configured
);
1189 BT_LIB_LOGE_APPEND_CAUSE(
1190 "Cannot set sink component class's \"graph is configured\" method.");
1191 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1192 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class
);
1203 * Add component class to the plugin object.
1205 * This will call back
1206 * bt_plugin_so_on_add_component_class() so that we can
1207 * add a mapping in the component class list when we
1208 * know the component class is successfully added.
1210 status
= bt_plugin_add_component_class(plugin
,
1211 (void *) comp_class
);
1212 BT_OBJECT_PUT_REF_AND_RESET(comp_class
);
1214 BT_LIB_LOGE_APPEND_CAUSE(
1215 "Cannot add component class to plugin.");
1221 bt_message_iterator_class_put_ref(msg_iter_class
);
1222 g_array_free(comp_class_full_descriptors
, TRUE
);
1227 struct bt_plugin
*bt_plugin_so_create_empty(
1228 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
1230 struct bt_plugin
*plugin
;
1231 struct bt_plugin_so_spec_data
*spec
;
1233 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
1238 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
1239 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
1240 if (!plugin
->spec_data
) {
1241 BT_LIB_LOGE_APPEND_CAUSE(
1242 "Failed to allocate one SO plugin specific data structure.");
1246 spec
= plugin
->spec_data
;
1247 spec
->shared_lib_handle
= shared_lib_handle
;
1248 bt_object_get_ref_no_null_check(spec
->shared_lib_handle
);
1252 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1259 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
1262 const int * const *begin_int
= (const int * const *) begin
;
1263 const int * const *end_int
= (const int * const *) end
;
1264 const int * const *iter
;
1266 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
1276 int bt_plugin_so_create_all_from_sections(
1277 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
1278 bool fail_on_load_error
,
1279 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
1280 struct __bt_plugin_descriptor
const * const *descriptors_end
,
1281 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
1282 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
1283 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
1284 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
1285 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
1286 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
,
1287 struct bt_plugin_set
**plugin_set_out
)
1289 int status
= BT_FUNC_STATUS_OK
;
1290 size_t descriptor_count
;
1292 size_t cc_descriptors_count
;
1293 size_t cc_descr_attrs_count
;
1296 BT_ASSERT(shared_lib_handle
);
1297 BT_ASSERT(plugin_set_out
);
1298 *plugin_set_out
= NULL
;
1299 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
1300 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
1301 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
1302 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
1303 BT_LOGI("Creating all SO plugins from sections: "
1304 "plugin-path=\"%s\", "
1305 "descr-begin-addr=%p, descr-end-addr=%p, "
1306 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1307 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1308 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1309 "descr-count=%zu, attrs-count=%zu, "
1310 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1311 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
1312 descriptors_begin
, descriptors_end
,
1313 attrs_begin
, attrs_end
,
1314 cc_descriptors_begin
, cc_descriptors_end
,
1315 cc_descr_attrs_begin
, cc_descr_attrs_end
,
1316 descriptor_count
, attrs_count
,
1317 cc_descriptors_count
, cc_descr_attrs_count
);
1318 *plugin_set_out
= bt_plugin_set_create();
1319 if (!*plugin_set_out
) {
1320 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
1321 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1325 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
1326 const struct __bt_plugin_descriptor
*descriptor
=
1327 descriptors_begin
[i
];
1328 struct bt_plugin
*plugin
;
1334 BT_LOGI("Creating plugin object for plugin: name=\"%s\"",
1336 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
1338 BT_LIB_LOGE_APPEND_CAUSE(
1339 "Cannot create empty shared library handle.");
1340 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1344 if (shared_lib_handle
->path
) {
1345 bt_plugin_set_path(plugin
,
1346 shared_lib_handle
->path
->str
);
1349 status
= bt_plugin_so_init(plugin
, fail_on_load_error
,
1350 descriptor
, attrs_begin
, attrs_end
,
1351 cc_descriptors_begin
, cc_descriptors_end
,
1352 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1353 if (status
== BT_FUNC_STATUS_OK
) {
1354 /* Add to plugin set */
1355 bt_plugin_set_add_plugin(*plugin_set_out
, plugin
);
1356 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1357 } else if (status
< 0) {
1359 * bt_plugin_so_init() handles
1360 * `fail_on_load_error`, so this is a "real"
1363 BT_LIB_LOGW_APPEND_CAUSE(
1364 "Cannot initialize SO plugin object from sections.");
1365 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
1372 BT_ASSERT(*plugin_set_out
);
1374 if ((*plugin_set_out
)->plugins
->len
== 0) {
1375 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1376 status
= BT_FUNC_STATUS_NOT_FOUND
;
1382 BT_ASSERT(status
< 0);
1383 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
1390 int bt_plugin_so_create_all_from_static(bool fail_on_load_error
,
1391 struct bt_plugin_set
**plugin_set_out
)
1394 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1396 BT_ASSERT(plugin_set_out
);
1397 *plugin_set_out
= NULL
;
1398 status
= bt_plugin_so_shared_lib_handle_create(NULL
,
1399 &shared_lib_handle
);
1400 if (status
!= BT_FUNC_STATUS_OK
) {
1401 BT_ASSERT(!shared_lib_handle
);
1405 BT_ASSERT(shared_lib_handle
);
1406 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
1407 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1409 __bt_get_begin_section_plugin_descriptors(),
1410 __bt_get_end_section_plugin_descriptors(),
1411 __bt_get_begin_section_plugin_descriptor_attributes(),
1412 __bt_get_end_section_plugin_descriptor_attributes(),
1413 __bt_get_begin_section_component_class_descriptors(),
1414 __bt_get_end_section_component_class_descriptors(),
1415 __bt_get_begin_section_component_class_descriptor_attributes(),
1416 __bt_get_end_section_component_class_descriptor_attributes(),
1418 BT_ASSERT((status
== BT_FUNC_STATUS_OK
&& *plugin_set_out
&&
1419 (*plugin_set_out
)->plugins
->len
> 0) || !*plugin_set_out
);
1422 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1427 int bt_plugin_so_create_all_from_file(const char *path
,
1428 bool fail_on_load_error
, struct bt_plugin_set
**plugin_set_out
)
1432 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
1433 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
1434 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
1435 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
1436 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
1437 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
1438 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
1439 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
1440 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
1441 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
1442 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
1443 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
1444 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
1445 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
1446 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
1447 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
1448 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
1449 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
1452 BT_ASSERT(plugin_set_out
);
1453 *plugin_set_out
= NULL
;
1454 path_len
= strlen(path
);
1457 * An SO plugin file must have a known plugin file suffix. So the file
1458 * path must be longer than the suffix length.
1460 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
1461 BT_LOGI("Path is too short to be an `.so` or `.la` plugin file:"
1462 "path=%s, path-length=%zu, min-length=%zu",
1463 path
, path_len
, PLUGIN_SUFFIX_LEN
);
1464 status
= BT_FUNC_STATUS_NOT_FOUND
;
1468 BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path
);
1472 * Check if the file ends with a known plugin file type suffix
1473 * (i.e. .so or .la on Linux).
1475 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
1476 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
1477 LIBTOOL_PLUGIN_SUFFIX_LEN
);
1478 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
1479 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
1480 NATIVE_PLUGIN_SUFFIX_LEN
);
1481 if (!is_shared_object
&& !is_libtool_wrapper
) {
1482 /* Name indicates this is not a plugin file; not an error */
1483 BT_LOGI("File is not an SO plugin file: path=\"%s\"", path
);
1484 status
= BT_FUNC_STATUS_NOT_FOUND
;
1488 status
= bt_plugin_so_shared_lib_handle_create(path
,
1489 &shared_lib_handle
);
1490 if (status
!= BT_FUNC_STATUS_OK
) {
1491 /* bt_plugin_so_shared_lib_handle_create() logs more details */
1492 BT_ASSERT(!shared_lib_handle
);
1496 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1497 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1498 descriptors_begin
= get_begin_section_plugin_descriptors();
1501 * Use this first symbol to know whether or not this
1502 * shared object _looks like_ a Babeltrace plugin. Since
1503 * g_module_symbol() failed, assume that this is not a
1504 * Babeltrace plugin, so it's not an error.
1506 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1507 "symbol=\"%s\"", path
,
1508 "__bt_get_begin_section_plugin_descriptors");
1509 status
= BT_FUNC_STATUS_NOT_FOUND
;
1514 * If g_module_symbol() fails for any of the other symbols, fail
1515 * if `fail_on_load_error` is true.
1517 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1518 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1519 descriptors_end
= get_end_section_plugin_descriptors();
1521 if (fail_on_load_error
) {
1522 BT_LIB_LOGW_APPEND_CAUSE(
1523 "Cannot resolve plugin symbol: path=\"%s\", "
1524 "symbol=\"%s\"", path
,
1525 "__bt_get_end_section_plugin_descriptors");
1526 status
= BT_FUNC_STATUS_ERROR
;
1529 "Cannot resolve plugin symbol: path=\"%s\", "
1530 "symbol=\"%s\"", path
,
1531 "__bt_get_end_section_plugin_descriptors");
1532 status
= BT_FUNC_STATUS_NOT_FOUND
;
1538 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1539 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1540 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1542 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1543 "symbol=\"%s\"", path
,
1544 "__bt_get_begin_section_plugin_descriptor_attributes");
1547 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1548 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1549 attrs_end
= get_end_section_plugin_descriptor_attributes();
1551 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1552 "symbol=\"%s\"", path
,
1553 "__bt_get_end_section_plugin_descriptor_attributes");
1556 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1557 if (fail_on_load_error
) {
1558 BT_LIB_LOGW_APPEND_CAUSE(
1559 "Found section start or end symbol, but not both: "
1560 "path=\"%s\", symbol-start=\"%s\", "
1561 "symbol-end=\"%s\", symbol-start-addr=%p, "
1562 "symbol-end-addr=%p",
1563 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1564 "__bt_get_end_section_plugin_descriptor_attributes",
1565 attrs_begin
, attrs_end
);
1566 status
= BT_FUNC_STATUS_ERROR
;
1569 "Found section start or end symbol, but not both: "
1570 "path=\"%s\", symbol-start=\"%s\", "
1571 "symbol-end=\"%s\", symbol-start-addr=%p, "
1572 "symbol-end-addr=%p",
1573 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1574 "__bt_get_end_section_plugin_descriptor_attributes",
1575 attrs_begin
, attrs_end
);
1576 status
= BT_FUNC_STATUS_NOT_FOUND
;
1582 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1583 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1584 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1586 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1587 "symbol=\"%s\"", path
,
1588 "__bt_get_begin_section_component_class_descriptors");
1591 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1592 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1593 cc_descriptors_end
= get_end_section_component_class_descriptors();
1595 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1596 "symbol=\"%s\"", path
,
1597 "__bt_get_end_section_component_class_descriptors");
1600 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1601 if (fail_on_load_error
) {
1602 BT_LIB_LOGW_APPEND_CAUSE(
1603 "Found section start or end symbol, but not both: "
1604 "path=\"%s\", symbol-start=\"%s\", "
1605 "symbol-end=\"%s\", symbol-start-addr=%p, "
1606 "symbol-end-addr=%p",
1607 path
, "__bt_get_begin_section_component_class_descriptors",
1608 "__bt_get_end_section_component_class_descriptors",
1609 cc_descriptors_begin
, cc_descriptors_end
);
1610 status
= BT_FUNC_STATUS_ERROR
;
1613 "Found section start or end symbol, but not both: "
1614 "path=\"%s\", symbol-start=\"%s\", "
1615 "symbol-end=\"%s\", symbol-start-addr=%p, "
1616 "symbol-end-addr=%p",
1617 path
, "__bt_get_begin_section_component_class_descriptors",
1618 "__bt_get_end_section_component_class_descriptors",
1619 cc_descriptors_begin
, cc_descriptors_end
);
1620 status
= BT_FUNC_STATUS_NOT_FOUND
;
1626 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1627 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1628 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1630 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1631 "symbol=\"%s\"", path
,
1632 "__bt_get_begin_section_component_class_descriptor_attributes");
1635 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1636 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1637 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1639 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1640 "symbol=\"%s\"", path
,
1641 "__bt_get_end_section_component_class_descriptor_attributes");
1644 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1645 if (fail_on_load_error
) {
1646 BT_LIB_LOGW_APPEND_CAUSE(
1647 "Found section start or end symbol, but not both: "
1648 "path=\"%s\", symbol-start=\"%s\", "
1649 "symbol-end=\"%s\", symbol-start-addr=%p, "
1650 "symbol-end-addr=%p",
1651 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1652 "__bt_get_end_section_component_class_descriptor_attributes",
1653 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1654 status
= BT_FUNC_STATUS_ERROR
;
1657 "Found section start or end symbol, but not both: "
1658 "path=\"%s\", symbol-start=\"%s\", "
1659 "symbol-end=\"%s\", symbol-start-addr=%p, "
1660 "symbol-end-addr=%p",
1661 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1662 "__bt_get_end_section_component_class_descriptor_attributes",
1663 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1664 status
= BT_FUNC_STATUS_NOT_FOUND
;
1670 /* Initialize plugin */
1671 BT_LOGD_STR("Initializing plugin object.");
1672 status
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1674 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1675 cc_descriptors_begin
, cc_descriptors_end
,
1676 cc_descr_attrs_begin
, cc_descr_attrs_end
, plugin_set_out
);
1679 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle
);
1684 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1687 bt_list_del(&comp_class
->node
);
1688 BT_OBJECT_PUT_REF_AND_RESET(comp_class
->so_handle
);
1689 BT_LOGD("Component class destroyed: removed entry from list: "
1690 "comp-cls-addr=%p", comp_class
);
1693 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1694 struct bt_component_class
*comp_class
)
1696 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1698 BT_ASSERT(plugin
->spec_data
);
1699 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1701 bt_list_add(&comp_class
->node
, &component_class_list
);
1702 comp_class
->so_handle
= spec
->shared_lib_handle
;
1703 bt_object_get_ref_no_null_check(comp_class
->so_handle
);
1705 /* Add our custom destroy listener */
1706 bt_component_class_add_destroy_listener(comp_class
,
1707 plugin_comp_class_destroy_listener
, NULL
);