4 * Babeltrace Plugin (shared object)
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define BT_LOG_TAG "PLUGIN-SO"
31 #include <babeltrace/lib-logging-internal.h>
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/plugin/plugin-internal.h>
36 #include <babeltrace/plugin/plugin-so-internal.h>
37 #include <babeltrace/plugin/plugin-dev.h>
38 #include <babeltrace/plugin/plugin-internal.h>
39 #include <babeltrace/graph/component-class-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/list-internal.h>
47 #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
48 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
49 #define LIBTOOL_PLUGIN_SUFFIX ".la"
50 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
52 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
53 sizeof(LIBTOOL_PLUGIN_SUFFIX))
58 * This list, global to the library, keeps all component classes that
59 * have a reference to their shared library handles. It allows iteration
60 * on all component classes still present when the destructor executes
61 * to release the shared library handle references they might still have.
63 * The list items are the component classes created with
64 * bt_plugin_add_component_class(). They keep the shared library handle
65 * object created by their plugin alive so that the plugin's code is
66 * not discarded when it could still be in use by living components
67 * created from those component classes:
69 * [component] --ref-> [component class]-> [shlib handle]
71 * It allows this use-case:
73 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
74 * // instantiate components from a plugin's component classes
75 * // put plugins and free my_plugins here
76 * // user code of instantiated components still exists
78 * An entry is removed from this list when a component class is
79 * destroyed thanks to a custom destroy listener. When the entry is
80 * removed, the entry is removed from the list, and we release the
81 * reference on the shlib handle. Assuming the original plugin object
82 * which contained some component classes is put first, when the last
83 * component class is removed from this list, the shared library handle
84 * object's reference count falls to zero and the shared library is
89 BT_LIST_HEAD(component_class_list
);
91 __attribute__((destructor
)) static
92 void fini_comp_class_list(void)
94 struct bt_component_class
*comp_class
, *tmp
;
96 bt_list_for_each_entry_safe(comp_class
, tmp
, &component_class_list
, node
) {
97 bt_list_del(&comp_class
->node
);
98 BT_PUT(comp_class
->so_handle
);
100 BT_LOGD_STR("Released references from all component classes to shared library handles.");
104 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
106 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
109 shared_lib_handle
= container_of(obj
,
110 struct bt_plugin_so_shared_lib_handle
, base
);
111 const char *path
= shared_lib_handle
->path
?
112 shared_lib_handle
->path
->str
: NULL
;
114 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
115 shared_lib_handle
, path
);
117 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
118 enum bt_plugin_status status
;
120 BT_LOGD_STR("Calling user's plugin exit function.");
121 status
= shared_lib_handle
->exit();
122 BT_LOGD("User function returned: %s",
123 bt_plugin_status_string(status
));
126 BT_LOGW("User's plugin exit function failed: "
127 "path=\"%s\"", path
);
131 if (shared_lib_handle
->module
) {
134 * Valgrind shows incomplete stack traces when
135 * dynamically loaded libraries are closed before it
136 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
137 * build to avoid this.
139 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
141 if (!var
|| strcmp(var
, "1") != 0) {
143 BT_LOGD("Closing GModule: path=\"%s\"", path
);
145 if (!g_module_close(shared_lib_handle
->module
)) {
146 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
147 g_module_error(), path
);
151 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
152 "path=\"%s\"", path
);
157 if (shared_lib_handle
->path
) {
158 g_string_free(shared_lib_handle
->path
, TRUE
);
161 g_free(shared_lib_handle
);
165 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
168 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
170 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
171 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
172 if (!shared_lib_handle
) {
173 BT_LOGE_STR("Failed to allocate one shared library handle.");
177 bt_object_init(shared_lib_handle
, bt_plugin_so_shared_lib_handle_destroy
);
183 shared_lib_handle
->path
= g_string_new(path
);
184 if (!shared_lib_handle
->path
) {
185 BT_LOGE_STR("Failed to allocate a GString.");
189 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
190 if (!shared_lib_handle
->module
) {
192 * DEBUG-level logging because we're only _trying_ to
193 * open this file as a Babeltrace plugin: if it's not,
194 * it's not an error. And because this can be tried
195 * during bt_plugin_create_all_from_dir(), it's not even
198 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
199 g_module_error(), path
);
206 BT_PUT(shared_lib_handle
);
209 if (shared_lib_handle
) {
210 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
211 path
, shared_lib_handle
);
214 return shared_lib_handle
;
218 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
220 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
222 if (!plugin
->spec_data
) {
226 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
228 BT_PUT(spec
->shared_lib_handle
);
229 g_free(plugin
->spec_data
);
230 plugin
->spec_data
= NULL
;
234 * This function does the following:
236 * 1. Iterate on the plugin descriptor attributes section and set the
237 * plugin's attributes depending on the attribute types. This
238 * includes the name of the plugin, its description, and its
239 * initialization function, for example.
241 * 2. Iterate on the component class descriptors section and create one
242 * "full descriptor" (temporary structure) for each one that is found
243 * and attached to our plugin descriptor.
245 * 3. Iterate on the component class descriptor attributes section and
246 * set the corresponding full descriptor's attributes depending on
247 * the attribute types. This includes the description of the
248 * component class, as well as its initialization and destroy
251 * 4. Call the user's plugin initialization function, if any is
254 * 5. For each full component class descriptor, create a component class
255 * object, set its optional attributes, and add it to the plugin
258 * 6. Freeze the plugin object.
261 enum bt_plugin_status
bt_plugin_so_init(
262 struct bt_plugin
*plugin
,
263 const struct __bt_plugin_descriptor
*descriptor
,
264 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
265 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
266 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
267 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
268 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
269 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
272 * This structure's members point to the plugin's memory
275 struct comp_class_full_descriptor
{
276 const struct __bt_plugin_component_class_descriptor
*descriptor
;
277 const char *description
;
279 bt_component_class_init_method init_method
;
280 bt_component_class_finalize_method finalize_method
;
281 bt_component_class_query_method query_method
;
282 bt_component_class_accept_port_connection_method accept_port_connection_method
;
283 bt_component_class_port_connected_method port_connected_method
;
284 bt_component_class_port_disconnected_method port_disconnected_method
;
285 struct bt_component_class_notification_iterator_methods iterator_methods
;
288 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
289 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
290 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
291 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
292 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
293 GArray
*comp_class_full_descriptors
;
297 BT_LOGD("Initializing plugin object from descriptors found in sections: "
298 "plugin-addr=%p, plugin-path=\"%s\", "
299 "attrs-begin-addr=%p, attrs-end-addr=%p, "
300 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
301 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
303 spec
->shared_lib_handle
->path
?
304 spec
->shared_lib_handle
->path
->str
: NULL
,
305 attrs_begin
, attrs_end
,
306 cc_descriptors_begin
, cc_descriptors_end
,
307 cc_descr_attrs_begin
, cc_descr_attrs_end
);
308 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
309 sizeof(struct comp_class_full_descriptor
));
310 if (!comp_class_full_descriptors
) {
311 BT_LOGE_STR("Failed to allocate a GArray.");
312 status
= BT_PLUGIN_STATUS_ERROR
;
316 /* Set mandatory attributes */
317 spec
->descriptor
= descriptor
;
318 bt_plugin_set_name(plugin
, descriptor
->name
);
321 * Find and set optional attributes attached to this plugin
324 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
325 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
328 if (cur_attr
== NULL
) {
332 if (cur_attr
->plugin_descriptor
!= descriptor
) {
336 switch (cur_attr
->type
) {
337 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
338 spec
->init
= cur_attr
->value
.init
;
340 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
341 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
343 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
344 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
346 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
347 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
349 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
350 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
352 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
353 bt_plugin_set_version(plugin
,
354 (unsigned int) cur_attr
->value
.version
.major
,
355 (unsigned int) cur_attr
->value
.version
.minor
,
356 (unsigned int) cur_attr
->value
.version
.patch
,
357 cur_attr
->value
.version
.extra
);
361 * WARN-level logging because this should not
362 * happen with the appropriate ABI version. If
363 * we're here, we know that for the reported
364 * version of the ABI, this attribute is
367 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
368 "plugin-path=\"%s\", plugin-name=\"%s\", "
369 "attr-type-name=\"%s\", attr-type-id=%d",
370 spec
->shared_lib_handle
->path
?
371 spec
->shared_lib_handle
->path
->str
:
373 descriptor
->name
, cur_attr
->type_name
,
380 * Find component class descriptors attached to this plugin
381 * descriptor and initialize corresponding full component class
382 * descriptors in the array.
384 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
385 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
387 struct comp_class_full_descriptor full_descriptor
= {0};
389 if (cur_cc_descr
== NULL
) {
393 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
397 full_descriptor
.descriptor
= cur_cc_descr
;
398 g_array_append_val(comp_class_full_descriptors
,
403 * Find component class descriptor attributes attached to this
404 * plugin descriptor and update corresponding full component
405 * class descriptors in the array.
407 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
408 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
409 *cur_cc_descr_attr_ptr
;
411 if (cur_cc_descr_attr
== NULL
) {
415 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
420 /* Find the corresponding component class descriptor entry */
421 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
422 struct comp_class_full_descriptor
*cc_full_descr
=
423 &g_array_index(comp_class_full_descriptors
,
424 struct comp_class_full_descriptor
, i
);
426 if (cur_cc_descr_attr
->comp_class_descriptor
==
427 cc_full_descr
->descriptor
) {
428 switch (cur_cc_descr_attr
->type
) {
429 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
430 cc_full_descr
->description
=
431 cur_cc_descr_attr
->value
.description
;
433 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
434 cc_full_descr
->help
=
435 cur_cc_descr_attr
->value
.help
;
437 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
438 cc_full_descr
->init_method
=
439 cur_cc_descr_attr
->value
.init_method
;
441 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
442 cc_full_descr
->finalize_method
=
443 cur_cc_descr_attr
->value
.finalize_method
;
445 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
446 cc_full_descr
->query_method
=
447 cur_cc_descr_attr
->value
.query_method
;
449 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
450 cc_full_descr
->accept_port_connection_method
=
451 cur_cc_descr_attr
->value
.accept_port_connection_method
;
453 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD
:
454 cc_full_descr
->port_connected_method
=
455 cur_cc_descr_attr
->value
.port_connected_method
;
457 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
458 cc_full_descr
->port_disconnected_method
=
459 cur_cc_descr_attr
->value
.port_disconnected_method
;
461 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
462 cc_full_descr
->iterator_methods
.init
=
463 cur_cc_descr_attr
->value
.notif_iter_init_method
;
465 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
466 cc_full_descr
->iterator_methods
.finalize
=
467 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
471 * WARN-level logging because
472 * this should not happen with
473 * the appropriate ABI version.
474 * If we're here, we know that
475 * for the reported version of
476 * the ABI, this attribute is
479 BT_LOGW("Ignoring unknown component class descriptor attribute: "
480 "plugin-path=\"%s\", "
481 "plugin-name=\"%s\", "
482 "comp-class-name=\"%s\", "
483 "comp-class-type=%s, "
484 "attr-type-name=\"%s\", "
486 spec
->shared_lib_handle
->path
?
487 spec
->shared_lib_handle
->path
->str
:
490 cur_cc_descr_attr
->comp_class_descriptor
->name
,
491 bt_component_class_type_string(
492 cur_cc_descr_attr
->comp_class_descriptor
->type
),
493 cur_cc_descr_attr
->type_name
,
494 cur_cc_descr_attr
->type
);
501 /* Initialize plugin */
503 BT_LOGD_STR("Calling user's plugin initialization function.");
504 status
= spec
->init(plugin
);
505 BT_LOGD("User function returned: %s",
506 bt_plugin_status_string(status
));
509 BT_LOGW_STR("User's plugin initialization function failed.");
514 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
516 /* Add described component classes to plugin */
517 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
518 struct comp_class_full_descriptor
*cc_full_descr
=
519 &g_array_index(comp_class_full_descriptors
,
520 struct comp_class_full_descriptor
, i
);
521 struct bt_component_class
*comp_class
;
523 BT_LOGD("Creating and setting properties of plugin's component class: "
524 "plugin-path=\"%s\", plugin-name=\"%s\", "
525 "comp-class-name=\"%s\", comp-class-type=%s",
526 spec
->shared_lib_handle
->path
?
527 spec
->shared_lib_handle
->path
->str
:
530 cc_full_descr
->descriptor
->name
,
531 bt_component_class_type_string(
532 cc_full_descr
->descriptor
->type
));
534 switch (cc_full_descr
->descriptor
->type
) {
535 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
536 comp_class
= bt_component_class_source_create(
537 cc_full_descr
->descriptor
->name
,
538 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
540 case BT_COMPONENT_CLASS_TYPE_FILTER
:
541 comp_class
= bt_component_class_filter_create(
542 cc_full_descr
->descriptor
->name
,
543 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
545 case BT_COMPONENT_CLASS_TYPE_SINK
:
546 comp_class
= bt_component_class_sink_create(
547 cc_full_descr
->descriptor
->name
,
548 cc_full_descr
->descriptor
->methods
.sink
.consume
);
552 * WARN-level logging because this should not
553 * happen with the appropriate ABI version. If
554 * we're here, we know that for the reported
555 * version of the ABI, this component class type
558 BT_LOGW("Ignoring unknown component class type: "
559 "plugin-path=\"%s\", plugin-name=\"%s\", "
560 "comp-class-name=\"%s\", comp-class-type=%d",
561 spec
->shared_lib_handle
->path
->str
?
562 spec
->shared_lib_handle
->path
->str
:
565 cc_full_descr
->descriptor
->name
,
566 cc_full_descr
->descriptor
->type
);
571 BT_LOGE_STR("Cannot create component class.");
572 status
= BT_PLUGIN_STATUS_ERROR
;
576 if (cc_full_descr
->description
) {
577 ret
= bt_component_class_set_description(comp_class
,
578 cc_full_descr
->description
);
580 BT_LOGE_STR("Cannot set component class's description.");
581 status
= BT_PLUGIN_STATUS_ERROR
;
587 if (cc_full_descr
->help
) {
588 ret
= bt_component_class_set_help(comp_class
,
589 cc_full_descr
->help
);
591 BT_LOGE_STR("Cannot set component class's help string.");
592 status
= BT_PLUGIN_STATUS_ERROR
;
598 if (cc_full_descr
->init_method
) {
599 ret
= bt_component_class_set_init_method(comp_class
,
600 cc_full_descr
->init_method
);
602 BT_LOGE_STR("Cannot set component class's initialization method.");
603 status
= BT_PLUGIN_STATUS_ERROR
;
609 if (cc_full_descr
->finalize_method
) {
610 ret
= bt_component_class_set_finalize_method(comp_class
,
611 cc_full_descr
->finalize_method
);
613 BT_LOGE_STR("Cannot set component class's finalization method.");
614 status
= BT_PLUGIN_STATUS_ERROR
;
620 if (cc_full_descr
->query_method
) {
621 ret
= bt_component_class_set_query_method(
622 comp_class
, cc_full_descr
->query_method
);
624 BT_LOGE_STR("Cannot set component class's query method.");
625 status
= BT_PLUGIN_STATUS_ERROR
;
631 if (cc_full_descr
->accept_port_connection_method
) {
632 ret
= bt_component_class_set_accept_port_connection_method(
633 comp_class
, cc_full_descr
->accept_port_connection_method
);
635 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
636 status
= BT_PLUGIN_STATUS_ERROR
;
642 if (cc_full_descr
->port_connected_method
) {
643 ret
= bt_component_class_set_port_connected_method(
644 comp_class
, cc_full_descr
->port_connected_method
);
646 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
647 status
= BT_PLUGIN_STATUS_ERROR
;
653 if (cc_full_descr
->port_disconnected_method
) {
654 ret
= bt_component_class_set_port_disconnected_method(
655 comp_class
, cc_full_descr
->port_disconnected_method
);
657 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
658 status
= BT_PLUGIN_STATUS_ERROR
;
664 switch (cc_full_descr
->descriptor
->type
) {
665 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
666 if (cc_full_descr
->iterator_methods
.init
) {
667 ret
= bt_component_class_source_set_notification_iterator_init_method(
669 cc_full_descr
->iterator_methods
.init
);
671 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
672 status
= BT_PLUGIN_STATUS_ERROR
;
678 if (cc_full_descr
->iterator_methods
.finalize
) {
679 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
681 cc_full_descr
->iterator_methods
.finalize
);
683 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
684 status
= BT_PLUGIN_STATUS_ERROR
;
690 case BT_COMPONENT_CLASS_TYPE_FILTER
:
691 if (cc_full_descr
->iterator_methods
.init
) {
692 ret
= bt_component_class_filter_set_notification_iterator_init_method(
694 cc_full_descr
->iterator_methods
.init
);
696 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
697 status
= BT_PLUGIN_STATUS_ERROR
;
703 if (cc_full_descr
->iterator_methods
.finalize
) {
704 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
706 cc_full_descr
->iterator_methods
.finalize
);
708 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
709 status
= BT_PLUGIN_STATUS_ERROR
;
715 case BT_COMPONENT_CLASS_TYPE_SINK
:
722 * Add component class to the plugin object.
724 * This will call back
725 * bt_plugin_so_on_add_component_class() so that we can
726 * add a mapping in the component class list when
727 * we know the component class is successfully added.
729 status
= bt_plugin_add_component_class(plugin
,
733 BT_LOGE("Cannot add component class to plugin.");
739 * All the plugin's component classes should be added at this
740 * point. We freeze the plugin so that it's not possible to add
741 * component classes to this plugin object after this stage
742 * (plugin object becomes immutable).
744 bt_plugin_freeze(plugin
);
747 g_array_free(comp_class_full_descriptors
, TRUE
);
752 struct bt_plugin
*bt_plugin_so_create_empty(
753 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
755 struct bt_plugin
*plugin
;
756 struct bt_plugin_so_spec_data
*spec
;
758 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
763 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
764 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
765 if (!plugin
->spec_data
) {
766 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
770 spec
= plugin
->spec_data
;
771 spec
->shared_lib_handle
= bt_get(shared_lib_handle
);
782 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
785 const int * const *begin_int
= (const int * const *) begin
;
786 const int * const *end_int
= (const int * const *) end
;
787 const int * const *iter
;
789 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
799 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
800 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
801 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
802 struct __bt_plugin_descriptor
const * const *descriptors_end
,
803 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
804 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
805 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
806 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
807 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
808 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
810 size_t descriptor_count
;
812 size_t cc_descriptors_count
;
813 size_t cc_descr_attrs_count
;
815 struct bt_plugin_set
*plugin_set
= NULL
;
817 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
818 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
819 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
820 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
822 BT_LOGD("Creating all SO plugins from sections: "
823 "plugin-path=\"%s\", "
824 "descr-begin-addr=%p, descr-end-addr=%p, "
825 "attrs-begin-addr=%p, attrs-end-addr=%p, "
826 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
827 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
828 "descr-count=%zu, attrs-count=%zu, "
829 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
830 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
831 descriptors_begin
, descriptors_end
,
832 attrs_begin
, attrs_end
,
833 cc_descriptors_begin
, cc_descriptors_end
,
834 cc_descr_attrs_begin
, cc_descr_attrs_end
,
835 descriptor_count
, attrs_count
,
836 cc_descriptors_count
, cc_descr_attrs_count
);
837 plugin_set
= bt_plugin_set_create();
839 BT_LOGE_STR("Cannot create empty plugin set.");
843 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
844 enum bt_plugin_status status
;
845 const struct __bt_plugin_descriptor
*descriptor
=
846 descriptors_begin
[i
];
847 struct bt_plugin
*plugin
;
849 if (descriptor
== NULL
) {
853 BT_LOGD("Creating plugin object for plugin: "
854 "name=\"%s\", abi-major=%d, abi-minor=%d",
855 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
857 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
859 * DEBUG-level logging because we're only
860 * _trying_ to open this file as a compatible
861 * Babeltrace plugin: if it's not, it's not an
862 * error. And because this can be tried during
863 * bt_plugin_create_all_from_dir(), it's not
866 BT_LOGD("Unknown ABI major version: abi-major=%d",
871 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
873 BT_LOGE_STR("Cannot create empty shared library handle.");
877 if (shared_lib_handle
&& shared_lib_handle
->path
) {
878 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
881 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
882 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
883 cc_descr_attrs_begin
, cc_descr_attrs_end
);
886 * DEBUG-level logging because we're only
887 * _trying_ to open this file as a compatible
888 * Babeltrace plugin: if it's not, it's not an
889 * error. And because this can be tried during
890 * bt_plugin_create_all_from_dir(), it's not
893 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
898 /* Add to plugin set */
899 bt_plugin_set_add_plugin(plugin_set
, plugin
);
913 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
915 struct bt_plugin_set
*plugin_set
= NULL
;
916 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
917 bt_plugin_so_shared_lib_handle_create(NULL
);
919 if (!shared_lib_handle
) {
923 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
924 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
925 __bt_get_begin_section_plugin_descriptors(),
926 __bt_get_end_section_plugin_descriptors(),
927 __bt_get_begin_section_plugin_descriptor_attributes(),
928 __bt_get_end_section_plugin_descriptor_attributes(),
929 __bt_get_begin_section_component_class_descriptors(),
930 __bt_get_end_section_component_class_descriptors(),
931 __bt_get_begin_section_component_class_descriptor_attributes(),
932 __bt_get_end_section_component_class_descriptor_attributes());
935 BT_PUT(shared_lib_handle
);
941 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
944 struct bt_plugin_set
*plugin_set
= NULL
;
945 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
946 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
947 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
948 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
949 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
950 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
951 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
952 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
953 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
954 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
955 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
956 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
957 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
958 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
959 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
960 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
961 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
962 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
965 BT_LOGW_STR("Invalid parameter: path is NULL.");
969 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
970 path_len
= strlen(path
);
971 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
972 BT_LOGW("Invalid parameter: path length is too short: "
973 "path-length=%zu", path_len
);
979 * Check if the file ends with a known plugin file type suffix (i.e. .so
982 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
983 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
984 LIBTOOL_PLUGIN_SUFFIX_LEN
);
985 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
986 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
987 NATIVE_PLUGIN_SUFFIX_LEN
);
988 if (!is_shared_object
&& !is_libtool_wrapper
) {
989 /* Name indicates this is not a plugin file; not an error */
990 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
994 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
995 if (!shared_lib_handle
) {
996 BT_LOGD_STR("Cannot create shared library handle.");
1000 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1001 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1002 descriptors_begin
= get_begin_section_plugin_descriptors();
1004 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1005 "symbol=\"%s\"", path
,
1006 "__bt_get_begin_section_plugin_descriptors");
1010 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1011 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1012 descriptors_end
= get_end_section_plugin_descriptors();
1014 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1015 "symbol=\"%s\"", path
,
1016 "__bt_get_end_section_plugin_descriptors");
1020 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1021 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1022 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1024 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1025 "symbol=\"%s\"", path
,
1026 "__bt_get_begin_section_plugin_descriptor_attributes");
1029 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1030 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1031 attrs_end
= get_end_section_plugin_descriptor_attributes();
1033 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1034 "symbol=\"%s\"", path
,
1035 "__bt_get_end_section_plugin_descriptor_attributes");
1038 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1039 BT_LOGD("Found section start or end symbol, but not both: "
1040 "path=\"%s\", symbol-start=\"%s\", "
1041 "symbol-end=\"%s\", symbol-start-addr=%p, "
1042 "symbol-end-addr=%p",
1043 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1044 "__bt_get_end_section_plugin_descriptor_attributes",
1045 attrs_begin
, attrs_end
);
1049 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1050 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1051 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1053 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1054 "symbol=\"%s\"", path
,
1055 "__bt_get_begin_section_component_class_descriptors");
1058 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1059 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1060 cc_descriptors_end
= get_end_section_component_class_descriptors();
1062 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1063 "symbol=\"%s\"", path
,
1064 "__bt_get_end_section_component_class_descriptors");
1067 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1068 BT_LOGD("Found section start or end symbol, but not both: "
1069 "path=\"%s\", symbol-start=\"%s\", "
1070 "symbol-end=\"%s\", symbol-start-addr=%p, "
1071 "symbol-end-addr=%p",
1072 path
, "__bt_get_begin_section_component_class_descriptors",
1073 "__bt_get_end_section_component_class_descriptors",
1074 cc_descriptors_begin
, cc_descriptors_end
);
1078 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1079 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1080 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1082 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1083 "symbol=\"%s\"", path
,
1084 "__bt_get_begin_section_component_class_descriptor_attributes");
1087 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1088 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1089 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1091 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1092 "symbol=\"%s\"", path
,
1093 "__bt_get_end_section_component_class_descriptor_attributes");
1096 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1097 BT_LOGD("Found section start or end symbol, but not both: "
1098 "path=\"%s\", symbol-start=\"%s\", "
1099 "symbol-end=\"%s\", symbol-start-addr=%p, "
1100 "symbol-end-addr=%p",
1101 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1102 "__bt_get_end_section_component_class_descriptor_attributes",
1103 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1107 /* Initialize plugin */
1108 BT_LOGD_STR("Initializing plugin object.");
1109 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1110 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1111 cc_descriptors_begin
, cc_descriptors_end
,
1112 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1115 BT_PUT(shared_lib_handle
);
1120 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1123 bt_list_del(&comp_class
->node
);
1124 BT_PUT(comp_class
->so_handle
);
1125 BT_LOGV("Component class destroyed: removed entry from list: "
1126 "comp-cls-addr=%p", comp_class
);
1130 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1131 struct bt_component_class
*comp_class
)
1133 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1135 assert(plugin
->spec_data
);
1136 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1138 bt_list_add(&comp_class
->node
, &component_class_list
);
1139 comp_class
->so_handle
= bt_get(spec
->shared_lib_handle
);
1141 /* Add our custom destroy listener */
1142 bt_component_class_add_destroy_listener(comp_class
,
1143 plugin_comp_class_destroy_listener
, NULL
);