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>
42 #include <babeltrace/assert-internal.h>
48 #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
49 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
50 #define LIBTOOL_PLUGIN_SUFFIX ".la"
51 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
53 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
54 sizeof(LIBTOOL_PLUGIN_SUFFIX))
59 * This list, global to the library, keeps all component classes that
60 * have a reference to their shared library handles. It allows iteration
61 * on all component classes still present when the destructor executes
62 * to release the shared library handle references they might still have.
64 * The list items are the component classes created with
65 * bt_plugin_add_component_class(). They keep the shared library handle
66 * object created by their plugin alive so that the plugin's code is
67 * not discarded when it could still be in use by living components
68 * created from those component classes:
70 * [component] --ref-> [component class]-> [shlib handle]
72 * It allows this use-case:
74 * my_plugins = bt_plugin_create_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_PUT(comp_class
->so_handle
);
101 BT_LOGD_STR("Released references from all component classes to shared library handles.");
105 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
107 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
110 shared_lib_handle
= container_of(obj
,
111 struct bt_plugin_so_shared_lib_handle
, base
);
112 const char *path
= shared_lib_handle
->path
?
113 shared_lib_handle
->path
->str
: NULL
;
115 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
116 shared_lib_handle
, path
);
118 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
119 enum bt_plugin_status status
;
121 BT_LOGD_STR("Calling user's plugin exit function.");
122 status
= shared_lib_handle
->exit();
123 BT_LOGD("User function returned: %s",
124 bt_plugin_status_string(status
));
127 BT_LOGW("User's plugin exit function failed: "
128 "path=\"%s\"", path
);
132 if (shared_lib_handle
->module
) {
135 * Valgrind shows incomplete stack traces when
136 * dynamically loaded libraries are closed before it
137 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
138 * build to avoid this.
140 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
142 if (!var
|| strcmp(var
, "1") != 0) {
144 BT_LOGD("Closing GModule: path=\"%s\"", path
);
146 if (!g_module_close(shared_lib_handle
->module
)) {
147 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
148 g_module_error(), path
);
152 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
153 "path=\"%s\"", path
);
158 if (shared_lib_handle
->path
) {
159 g_string_free(shared_lib_handle
->path
, TRUE
);
162 g_free(shared_lib_handle
);
166 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
169 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
171 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
172 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
173 if (!shared_lib_handle
) {
174 BT_LOGE_STR("Failed to allocate one shared library handle.");
178 bt_object_init(shared_lib_handle
, bt_plugin_so_shared_lib_handle_destroy
);
184 shared_lib_handle
->path
= g_string_new(path
);
185 if (!shared_lib_handle
->path
) {
186 BT_LOGE_STR("Failed to allocate a GString.");
190 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
191 if (!shared_lib_handle
->module
) {
193 * DEBUG-level logging because we're only _trying_ to
194 * open this file as a Babeltrace plugin: if it's not,
195 * it's not an error. And because this can be tried
196 * during bt_plugin_create_all_from_dir(), it's not even
199 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
200 g_module_error(), path
);
207 BT_PUT(shared_lib_handle
);
210 if (shared_lib_handle
) {
211 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
212 path
, shared_lib_handle
);
215 return shared_lib_handle
;
219 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
221 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
223 if (!plugin
->spec_data
) {
227 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
229 BT_PUT(spec
->shared_lib_handle
);
230 g_free(plugin
->spec_data
);
231 plugin
->spec_data
= NULL
;
235 * This function does the following:
237 * 1. Iterate on the plugin descriptor attributes section and set the
238 * plugin's attributes depending on the attribute types. This
239 * includes the name of the plugin, its description, and its
240 * initialization function, for example.
242 * 2. Iterate on the component class descriptors section and create one
243 * "full descriptor" (temporary structure) for each one that is found
244 * and attached to our plugin descriptor.
246 * 3. Iterate on the component class descriptor attributes section and
247 * set the corresponding full descriptor's attributes depending on
248 * the attribute types. This includes the description of the
249 * component class, as well as its initialization and destroy
252 * 4. Call the user's plugin initialization function, if any is
255 * 5. For each full component class descriptor, create a component class
256 * object, set its optional attributes, and add it to the plugin
259 * 6. Freeze the plugin object.
262 enum bt_plugin_status
bt_plugin_so_init(
263 struct bt_plugin
*plugin
,
264 const struct __bt_plugin_descriptor
*descriptor
,
265 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
266 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
267 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
268 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
269 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
270 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
273 * This structure's members point to the plugin's memory
276 struct comp_class_full_descriptor
{
277 const struct __bt_plugin_component_class_descriptor
*descriptor
;
278 const char *description
;
280 bt_component_class_init_method init_method
;
281 bt_component_class_finalize_method finalize_method
;
282 bt_component_class_query_method query_method
;
283 bt_component_class_accept_port_connection_method accept_port_connection_method
;
284 bt_component_class_port_connected_method port_connected_method
;
285 bt_component_class_port_disconnected_method port_disconnected_method
;
286 struct bt_component_class_notification_iterator_methods iterator_methods
;
289 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
290 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
291 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
292 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
293 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
294 GArray
*comp_class_full_descriptors
;
298 BT_LOGD("Initializing plugin object from descriptors found in sections: "
299 "plugin-addr=%p, plugin-path=\"%s\", "
300 "attrs-begin-addr=%p, attrs-end-addr=%p, "
301 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
302 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
304 spec
->shared_lib_handle
->path
?
305 spec
->shared_lib_handle
->path
->str
: NULL
,
306 attrs_begin
, attrs_end
,
307 cc_descriptors_begin
, cc_descriptors_end
,
308 cc_descr_attrs_begin
, cc_descr_attrs_end
);
309 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
310 sizeof(struct comp_class_full_descriptor
));
311 if (!comp_class_full_descriptors
) {
312 BT_LOGE_STR("Failed to allocate a GArray.");
313 status
= BT_PLUGIN_STATUS_ERROR
;
317 /* Set mandatory attributes */
318 spec
->descriptor
= descriptor
;
319 bt_plugin_set_name(plugin
, descriptor
->name
);
322 * Find and set optional attributes attached to this plugin
325 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
326 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
329 if (cur_attr
== NULL
) {
333 if (cur_attr
->plugin_descriptor
!= descriptor
) {
337 switch (cur_attr
->type
) {
338 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
339 spec
->init
= cur_attr
->value
.init
;
341 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
342 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
344 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
345 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
347 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
348 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
350 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
351 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
353 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
354 bt_plugin_set_version(plugin
,
355 (unsigned int) cur_attr
->value
.version
.major
,
356 (unsigned int) cur_attr
->value
.version
.minor
,
357 (unsigned int) cur_attr
->value
.version
.patch
,
358 cur_attr
->value
.version
.extra
);
362 * WARN-level logging because this should not
363 * happen with the appropriate ABI version. If
364 * we're here, we know that for the reported
365 * version of the ABI, this attribute is
368 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
369 "plugin-path=\"%s\", plugin-name=\"%s\", "
370 "attr-type-name=\"%s\", attr-type-id=%d",
371 spec
->shared_lib_handle
->path
?
372 spec
->shared_lib_handle
->path
->str
:
374 descriptor
->name
, cur_attr
->type_name
,
381 * Find component class descriptors attached to this plugin
382 * descriptor and initialize corresponding full component class
383 * descriptors in the array.
385 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
386 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
388 struct comp_class_full_descriptor full_descriptor
= {0};
390 if (cur_cc_descr
== NULL
) {
394 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
398 full_descriptor
.descriptor
= cur_cc_descr
;
399 g_array_append_val(comp_class_full_descriptors
,
404 * Find component class descriptor attributes attached to this
405 * plugin descriptor and update corresponding full component
406 * class descriptors in the array.
408 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
409 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
410 *cur_cc_descr_attr_ptr
;
412 if (cur_cc_descr_attr
== NULL
) {
416 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
421 /* Find the corresponding component class descriptor entry */
422 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
423 struct comp_class_full_descriptor
*cc_full_descr
=
424 &g_array_index(comp_class_full_descriptors
,
425 struct comp_class_full_descriptor
, i
);
427 if (cur_cc_descr_attr
->comp_class_descriptor
==
428 cc_full_descr
->descriptor
) {
429 switch (cur_cc_descr_attr
->type
) {
430 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
431 cc_full_descr
->description
=
432 cur_cc_descr_attr
->value
.description
;
434 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
435 cc_full_descr
->help
=
436 cur_cc_descr_attr
->value
.help
;
438 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
439 cc_full_descr
->init_method
=
440 cur_cc_descr_attr
->value
.init_method
;
442 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
443 cc_full_descr
->finalize_method
=
444 cur_cc_descr_attr
->value
.finalize_method
;
446 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
447 cc_full_descr
->query_method
=
448 cur_cc_descr_attr
->value
.query_method
;
450 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
451 cc_full_descr
->accept_port_connection_method
=
452 cur_cc_descr_attr
->value
.accept_port_connection_method
;
454 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD
:
455 cc_full_descr
->port_connected_method
=
456 cur_cc_descr_attr
->value
.port_connected_method
;
458 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
459 cc_full_descr
->port_disconnected_method
=
460 cur_cc_descr_attr
->value
.port_disconnected_method
;
462 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
463 cc_full_descr
->iterator_methods
.init
=
464 cur_cc_descr_attr
->value
.notif_iter_init_method
;
466 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
467 cc_full_descr
->iterator_methods
.finalize
=
468 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
472 * WARN-level logging because
473 * this should not happen with
474 * the appropriate ABI version.
475 * If we're here, we know that
476 * for the reported version of
477 * the ABI, this attribute is
480 BT_LOGW("Ignoring unknown component class descriptor attribute: "
481 "plugin-path=\"%s\", "
482 "plugin-name=\"%s\", "
483 "comp-class-name=\"%s\", "
484 "comp-class-type=%s, "
485 "attr-type-name=\"%s\", "
487 spec
->shared_lib_handle
->path
?
488 spec
->shared_lib_handle
->path
->str
:
491 cur_cc_descr_attr
->comp_class_descriptor
->name
,
492 bt_component_class_type_string(
493 cur_cc_descr_attr
->comp_class_descriptor
->type
),
494 cur_cc_descr_attr
->type_name
,
495 cur_cc_descr_attr
->type
);
502 /* Initialize plugin */
504 BT_LOGD_STR("Calling user's plugin initialization function.");
505 status
= spec
->init(plugin
);
506 BT_LOGD("User function returned: %s",
507 bt_plugin_status_string(status
));
510 BT_LOGW_STR("User's plugin initialization function failed.");
515 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
517 /* Add described component classes to plugin */
518 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
519 struct comp_class_full_descriptor
*cc_full_descr
=
520 &g_array_index(comp_class_full_descriptors
,
521 struct comp_class_full_descriptor
, i
);
522 struct bt_component_class
*comp_class
;
524 BT_LOGD("Creating and setting properties of plugin's component class: "
525 "plugin-path=\"%s\", plugin-name=\"%s\", "
526 "comp-class-name=\"%s\", comp-class-type=%s",
527 spec
->shared_lib_handle
->path
?
528 spec
->shared_lib_handle
->path
->str
:
531 cc_full_descr
->descriptor
->name
,
532 bt_component_class_type_string(
533 cc_full_descr
->descriptor
->type
));
535 switch (cc_full_descr
->descriptor
->type
) {
536 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
537 comp_class
= bt_component_class_source_create(
538 cc_full_descr
->descriptor
->name
,
539 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
541 case BT_COMPONENT_CLASS_TYPE_FILTER
:
542 comp_class
= bt_component_class_filter_create(
543 cc_full_descr
->descriptor
->name
,
544 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
546 case BT_COMPONENT_CLASS_TYPE_SINK
:
547 comp_class
= bt_component_class_sink_create(
548 cc_full_descr
->descriptor
->name
,
549 cc_full_descr
->descriptor
->methods
.sink
.consume
);
553 * WARN-level logging because this should not
554 * happen with the appropriate ABI version. If
555 * we're here, we know that for the reported
556 * version of the ABI, this component class type
559 BT_LOGW("Ignoring unknown component class type: "
560 "plugin-path=\"%s\", plugin-name=\"%s\", "
561 "comp-class-name=\"%s\", comp-class-type=%d",
562 spec
->shared_lib_handle
->path
->str
?
563 spec
->shared_lib_handle
->path
->str
:
566 cc_full_descr
->descriptor
->name
,
567 cc_full_descr
->descriptor
->type
);
572 BT_LOGE_STR("Cannot create component class.");
573 status
= BT_PLUGIN_STATUS_ERROR
;
577 if (cc_full_descr
->description
) {
578 ret
= bt_component_class_set_description(comp_class
,
579 cc_full_descr
->description
);
581 BT_LOGE_STR("Cannot set component class's description.");
582 status
= BT_PLUGIN_STATUS_ERROR
;
588 if (cc_full_descr
->help
) {
589 ret
= bt_component_class_set_help(comp_class
,
590 cc_full_descr
->help
);
592 BT_LOGE_STR("Cannot set component class's help string.");
593 status
= BT_PLUGIN_STATUS_ERROR
;
599 if (cc_full_descr
->init_method
) {
600 ret
= bt_component_class_set_init_method(comp_class
,
601 cc_full_descr
->init_method
);
603 BT_LOGE_STR("Cannot set component class's initialization method.");
604 status
= BT_PLUGIN_STATUS_ERROR
;
610 if (cc_full_descr
->finalize_method
) {
611 ret
= bt_component_class_set_finalize_method(comp_class
,
612 cc_full_descr
->finalize_method
);
614 BT_LOGE_STR("Cannot set component class's finalization method.");
615 status
= BT_PLUGIN_STATUS_ERROR
;
621 if (cc_full_descr
->query_method
) {
622 ret
= bt_component_class_set_query_method(
623 comp_class
, cc_full_descr
->query_method
);
625 BT_LOGE_STR("Cannot set component class's query method.");
626 status
= BT_PLUGIN_STATUS_ERROR
;
632 if (cc_full_descr
->accept_port_connection_method
) {
633 ret
= bt_component_class_set_accept_port_connection_method(
634 comp_class
, cc_full_descr
->accept_port_connection_method
);
636 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
637 status
= BT_PLUGIN_STATUS_ERROR
;
643 if (cc_full_descr
->port_connected_method
) {
644 ret
= bt_component_class_set_port_connected_method(
645 comp_class
, cc_full_descr
->port_connected_method
);
647 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
648 status
= BT_PLUGIN_STATUS_ERROR
;
654 if (cc_full_descr
->port_disconnected_method
) {
655 ret
= bt_component_class_set_port_disconnected_method(
656 comp_class
, cc_full_descr
->port_disconnected_method
);
658 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
659 status
= BT_PLUGIN_STATUS_ERROR
;
665 switch (cc_full_descr
->descriptor
->type
) {
666 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
667 if (cc_full_descr
->iterator_methods
.init
) {
668 ret
= bt_component_class_source_set_notification_iterator_init_method(
670 cc_full_descr
->iterator_methods
.init
);
672 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
673 status
= BT_PLUGIN_STATUS_ERROR
;
679 if (cc_full_descr
->iterator_methods
.finalize
) {
680 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
682 cc_full_descr
->iterator_methods
.finalize
);
684 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
685 status
= BT_PLUGIN_STATUS_ERROR
;
691 case BT_COMPONENT_CLASS_TYPE_FILTER
:
692 if (cc_full_descr
->iterator_methods
.init
) {
693 ret
= bt_component_class_filter_set_notification_iterator_init_method(
695 cc_full_descr
->iterator_methods
.init
);
697 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
698 status
= BT_PLUGIN_STATUS_ERROR
;
704 if (cc_full_descr
->iterator_methods
.finalize
) {
705 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
707 cc_full_descr
->iterator_methods
.finalize
);
709 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
710 status
= BT_PLUGIN_STATUS_ERROR
;
716 case BT_COMPONENT_CLASS_TYPE_SINK
:
723 * Add component class to the plugin object.
725 * This will call back
726 * bt_plugin_so_on_add_component_class() so that we can
727 * add a mapping in the component class list when
728 * we know the component class is successfully added.
730 status
= bt_plugin_add_component_class(plugin
,
734 BT_LOGE("Cannot add component class to plugin.");
740 * All the plugin's component classes should be added at this
741 * point. We freeze the plugin so that it's not possible to add
742 * component classes to this plugin object after this stage
743 * (plugin object becomes immutable).
745 bt_plugin_freeze(plugin
);
748 g_array_free(comp_class_full_descriptors
, TRUE
);
753 struct bt_plugin
*bt_plugin_so_create_empty(
754 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
756 struct bt_plugin
*plugin
;
757 struct bt_plugin_so_spec_data
*spec
;
759 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
764 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
765 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
766 if (!plugin
->spec_data
) {
767 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
771 spec
= plugin
->spec_data
;
772 spec
->shared_lib_handle
= bt_get(shared_lib_handle
);
783 size_t count_non_null_items_in_section(const void *begin
, const void *end
)
786 const int * const *begin_int
= (const int * const *) begin
;
787 const int * const *end_int
= (const int * const *) end
;
788 const int * const *iter
;
790 for (iter
= begin_int
; iter
!= end_int
; iter
++) {
800 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
801 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
802 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
803 struct __bt_plugin_descriptor
const * const *descriptors_end
,
804 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
805 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
806 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
807 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
808 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
809 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
811 size_t descriptor_count
;
813 size_t cc_descriptors_count
;
814 size_t cc_descr_attrs_count
;
816 struct bt_plugin_set
*plugin_set
= NULL
;
818 descriptor_count
= count_non_null_items_in_section(descriptors_begin
, descriptors_end
);
819 attrs_count
= count_non_null_items_in_section(attrs_begin
, attrs_end
);
820 cc_descriptors_count
= count_non_null_items_in_section(cc_descriptors_begin
, cc_descriptors_end
);
821 cc_descr_attrs_count
= count_non_null_items_in_section(cc_descr_attrs_begin
, cc_descr_attrs_end
);
823 BT_LOGD("Creating all SO plugins from sections: "
824 "plugin-path=\"%s\", "
825 "descr-begin-addr=%p, descr-end-addr=%p, "
826 "attrs-begin-addr=%p, attrs-end-addr=%p, "
827 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
828 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
829 "descr-count=%zu, attrs-count=%zu, "
830 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
831 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
832 descriptors_begin
, descriptors_end
,
833 attrs_begin
, attrs_end
,
834 cc_descriptors_begin
, cc_descriptors_end
,
835 cc_descr_attrs_begin
, cc_descr_attrs_end
,
836 descriptor_count
, attrs_count
,
837 cc_descriptors_count
, cc_descr_attrs_count
);
838 plugin_set
= bt_plugin_set_create();
840 BT_LOGE_STR("Cannot create empty plugin set.");
844 for (i
= 0; i
< descriptors_end
- descriptors_begin
; i
++) {
845 enum bt_plugin_status status
;
846 const struct __bt_plugin_descriptor
*descriptor
=
847 descriptors_begin
[i
];
848 struct bt_plugin
*plugin
;
850 if (descriptor
== NULL
) {
854 BT_LOGD("Creating plugin object for plugin: "
855 "name=\"%s\", abi-major=%d, abi-minor=%d",
856 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
858 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
860 * DEBUG-level logging because we're only
861 * _trying_ to open this file as a compatible
862 * Babeltrace plugin: if it's not, it's not an
863 * error. And because this can be tried during
864 * bt_plugin_create_all_from_dir(), it's not
867 BT_LOGD("Unknown ABI major version: abi-major=%d",
872 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
874 BT_LOGE_STR("Cannot create empty shared library handle.");
878 if (shared_lib_handle
&& shared_lib_handle
->path
) {
879 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
882 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
883 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
884 cc_descr_attrs_begin
, cc_descr_attrs_end
);
887 * DEBUG-level logging because we're only
888 * _trying_ to open this file as a compatible
889 * Babeltrace plugin: if it's not, it's not an
890 * error. And because this can be tried during
891 * bt_plugin_create_all_from_dir(), it's not
894 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
899 /* Add to plugin set */
900 bt_plugin_set_add_plugin(plugin_set
, plugin
);
914 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
916 struct bt_plugin_set
*plugin_set
= NULL
;
917 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
918 bt_plugin_so_shared_lib_handle_create(NULL
);
920 if (!shared_lib_handle
) {
924 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
925 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
926 __bt_get_begin_section_plugin_descriptors(),
927 __bt_get_end_section_plugin_descriptors(),
928 __bt_get_begin_section_plugin_descriptor_attributes(),
929 __bt_get_end_section_plugin_descriptor_attributes(),
930 __bt_get_begin_section_component_class_descriptors(),
931 __bt_get_end_section_component_class_descriptors(),
932 __bt_get_begin_section_component_class_descriptor_attributes(),
933 __bt_get_end_section_component_class_descriptor_attributes());
936 BT_PUT(shared_lib_handle
);
942 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
945 struct bt_plugin_set
*plugin_set
= NULL
;
946 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
947 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
948 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
949 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
950 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
951 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
952 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
953 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
954 struct __bt_plugin_descriptor
const * const *(*get_begin_section_plugin_descriptors
)(void);
955 struct __bt_plugin_descriptor
const * const *(*get_end_section_plugin_descriptors
)(void);
956 struct __bt_plugin_descriptor_attribute
const * const *(*get_begin_section_plugin_descriptor_attributes
)(void);
957 struct __bt_plugin_descriptor_attribute
const * const *(*get_end_section_plugin_descriptor_attributes
)(void);
958 struct __bt_plugin_component_class_descriptor
const * const *(*get_begin_section_component_class_descriptors
)(void);
959 struct __bt_plugin_component_class_descriptor
const * const *(*get_end_section_component_class_descriptors
)(void);
960 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_begin_section_component_class_descriptor_attributes
)(void);
961 struct __bt_plugin_component_class_descriptor_attribute
const * const *(*get_end_section_component_class_descriptor_attributes
)(void);
962 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
963 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
966 BT_LOGW_STR("Invalid parameter: path is NULL.");
970 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
971 path_len
= strlen(path
);
972 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
973 BT_LOGW("Invalid parameter: path length is too short: "
974 "path-length=%zu", path_len
);
980 * Check if the file ends with a known plugin file type suffix (i.e. .so
983 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
984 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
985 LIBTOOL_PLUGIN_SUFFIX_LEN
);
986 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
987 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
988 NATIVE_PLUGIN_SUFFIX_LEN
);
989 if (!is_shared_object
&& !is_libtool_wrapper
) {
990 /* Name indicates this is not a plugin file; not an error */
991 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
995 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
996 if (!shared_lib_handle
) {
997 BT_LOGD_STR("Cannot create shared library handle.");
1001 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptors",
1002 (gpointer
*) &get_begin_section_plugin_descriptors
)) {
1003 descriptors_begin
= get_begin_section_plugin_descriptors();
1005 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1006 "symbol=\"%s\"", path
,
1007 "__bt_get_begin_section_plugin_descriptors");
1011 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptors",
1012 (gpointer
*) &get_end_section_plugin_descriptors
)) {
1013 descriptors_end
= get_end_section_plugin_descriptors();
1015 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1016 "symbol=\"%s\"", path
,
1017 "__bt_get_end_section_plugin_descriptors");
1021 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_plugin_descriptor_attributes",
1022 (gpointer
*) &get_begin_section_plugin_descriptor_attributes
)) {
1023 attrs_begin
= get_begin_section_plugin_descriptor_attributes();
1025 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1026 "symbol=\"%s\"", path
,
1027 "__bt_get_begin_section_plugin_descriptor_attributes");
1030 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_plugin_descriptor_attributes",
1031 (gpointer
*) &get_end_section_plugin_descriptor_attributes
)) {
1032 attrs_end
= get_end_section_plugin_descriptor_attributes();
1034 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1035 "symbol=\"%s\"", path
,
1036 "__bt_get_end_section_plugin_descriptor_attributes");
1039 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1040 BT_LOGD("Found section start or end symbol, but not both: "
1041 "path=\"%s\", symbol-start=\"%s\", "
1042 "symbol-end=\"%s\", symbol-start-addr=%p, "
1043 "symbol-end-addr=%p",
1044 path
, "__bt_get_begin_section_plugin_descriptor_attributes",
1045 "__bt_get_end_section_plugin_descriptor_attributes",
1046 attrs_begin
, attrs_end
);
1050 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptors",
1051 (gpointer
*) &get_begin_section_component_class_descriptors
)) {
1052 cc_descriptors_begin
= get_begin_section_component_class_descriptors();
1054 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1055 "symbol=\"%s\"", path
,
1056 "__bt_get_begin_section_component_class_descriptors");
1059 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptors",
1060 (gpointer
*) &get_end_section_component_class_descriptors
)) {
1061 cc_descriptors_end
= get_end_section_component_class_descriptors();
1063 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1064 "symbol=\"%s\"", path
,
1065 "__bt_get_end_section_component_class_descriptors");
1068 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1069 BT_LOGD("Found section start or end symbol, but not both: "
1070 "path=\"%s\", symbol-start=\"%s\", "
1071 "symbol-end=\"%s\", symbol-start-addr=%p, "
1072 "symbol-end-addr=%p",
1073 path
, "__bt_get_begin_section_component_class_descriptors",
1074 "__bt_get_end_section_component_class_descriptors",
1075 cc_descriptors_begin
, cc_descriptors_end
);
1079 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_begin_section_component_class_descriptor_attributes",
1080 (gpointer
*) &get_begin_section_component_class_descriptor_attributes
)) {
1081 cc_descr_attrs_begin
= get_begin_section_component_class_descriptor_attributes();
1083 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1084 "symbol=\"%s\"", path
,
1085 "__bt_get_begin_section_component_class_descriptor_attributes");
1088 if (g_module_symbol(shared_lib_handle
->module
, "__bt_get_end_section_component_class_descriptor_attributes",
1089 (gpointer
*) &get_end_section_component_class_descriptor_attributes
)) {
1090 cc_descr_attrs_end
= get_end_section_component_class_descriptor_attributes();
1092 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1093 "symbol=\"%s\"", path
,
1094 "__bt_get_end_section_component_class_descriptor_attributes");
1097 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1098 BT_LOGD("Found section start or end symbol, but not both: "
1099 "path=\"%s\", symbol-start=\"%s\", "
1100 "symbol-end=\"%s\", symbol-start-addr=%p, "
1101 "symbol-end-addr=%p",
1102 path
, "__bt_get_begin_section_component_class_descriptor_attributes",
1103 "__bt_get_end_section_component_class_descriptor_attributes",
1104 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1108 /* Initialize plugin */
1109 BT_LOGD_STR("Initializing plugin object.");
1110 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1111 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1112 cc_descriptors_begin
, cc_descriptors_end
,
1113 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1116 BT_PUT(shared_lib_handle
);
1121 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1124 bt_list_del(&comp_class
->node
);
1125 BT_PUT(comp_class
->so_handle
);
1126 BT_LOGV("Component class destroyed: removed entry from list: "
1127 "comp-cls-addr=%p", comp_class
);
1131 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1132 struct bt_component_class
*comp_class
)
1134 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1136 BT_ASSERT(plugin
->spec_data
);
1137 BT_ASSERT(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1139 bt_list_add(&comp_class
->node
, &component_class_list
);
1140 comp_class
->so_handle
= bt_get(spec
->shared_lib_handle
);
1142 /* Add our custom destroy listener */
1143 bt_component_class_add_destroy_listener(comp_class
,
1144 plugin_comp_class_destroy_listener
, NULL
);