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))
55 #define SECTION_BEGIN(_name) (&(__start_##_name))
56 #define SECTION_END(_name) (&(__stop_##_name))
57 #define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name))
59 #define DECLARE_SECTION(_type, _name) \
60 extern _type __start_##_name __attribute((weak)); \
61 extern _type __stop_##_name __attribute((weak))
63 DECLARE_SECTION(struct __bt_plugin_descriptor
const *, __bt_plugin_descriptors
);
64 DECLARE_SECTION(struct __bt_plugin_descriptor_attribute
const *, __bt_plugin_descriptor_attributes
);
65 DECLARE_SECTION(struct __bt_plugin_component_class_descriptor
const *, __bt_plugin_component_class_descriptors
);
66 DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute
const *, __bt_plugin_component_class_descriptor_attributes
);
69 * This list, global to the library, keeps all component classes that
70 * have a reference to their shared library handles. It allows iteration
71 * on all component classes still present when the destructor executes
72 * to release the shared library handle references they might still have.
74 * The list items are the component classes created with
75 * bt_plugin_add_component_class(). They keep the shared library handle
76 * object created by their plugin alive so that the plugin's code is
77 * not discarded when it could still be in use by living components
78 * created from those component classes:
80 * [component] --ref-> [component class]-> [shlib handle]
82 * It allows this use-case:
84 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
85 * // instantiate components from a plugin's component classes
86 * // put plugins and free my_plugins here
87 * // user code of instantiated components still exists
89 * An entry is removed from this list when a component class is
90 * destroyed thanks to a custom destroy listener. When the entry is
91 * removed, the entry is removed from the list, and we release the
92 * reference on the shlib handle. Assuming the original plugin object
93 * which contained some component classes is put first, when the last
94 * component class is removed from this list, the shared library handle
95 * object's reference count falls to zero and the shared library is
100 BT_LIST_HEAD(component_class_list
);
102 __attribute__((destructor
)) static
103 void fini_comp_class_list(void)
105 struct bt_component_class
*comp_class
, *tmp
;
107 bt_list_for_each_entry_safe(comp_class
, tmp
, &component_class_list
, node
) {
108 bt_list_del(&comp_class
->node
);
109 BT_PUT(comp_class
->so_handle
);
111 BT_LOGD_STR("Released references from all component classes to shared library handles.");
115 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
117 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
120 shared_lib_handle
= container_of(obj
,
121 struct bt_plugin_so_shared_lib_handle
, base
);
122 const char *path
= shared_lib_handle
->path
?
123 shared_lib_handle
->path
->str
: NULL
;
125 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
126 shared_lib_handle
, path
);
128 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
129 enum bt_plugin_status status
;
131 BT_LOGD_STR("Calling user's plugin exit function.");
132 status
= shared_lib_handle
->exit();
133 BT_LOGD("User function returned: %s",
134 bt_plugin_status_string(status
));
137 BT_LOGW("User's plugin exit function failed: "
138 "path=\"%s\"", path
);
142 if (shared_lib_handle
->module
) {
145 * Valgrind shows incomplete stack traces when
146 * dynamically loaded libraries are closed before it
147 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
148 * build to avoid this.
150 const char *var
= getenv("BABELTRACE_NO_DLCLOSE");
152 if (!var
|| strcmp(var
, "1") != 0) {
154 BT_LOGD("Closing GModule: path=\"%s\"", path
);
156 if (!g_module_close(shared_lib_handle
->module
)) {
157 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
158 g_module_error(), path
);
162 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
163 "path=\"%s\"", path
);
168 if (shared_lib_handle
->path
) {
169 g_string_free(shared_lib_handle
->path
, TRUE
);
172 g_free(shared_lib_handle
);
176 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
179 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
181 BT_LOGD("Creating shared library handle: path=\"%s\"", path
);
182 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
183 if (!shared_lib_handle
) {
184 BT_LOGE_STR("Failed to allocate one shared library handle.");
188 bt_object_init(shared_lib_handle
, bt_plugin_so_shared_lib_handle_destroy
);
194 shared_lib_handle
->path
= g_string_new(path
);
195 if (!shared_lib_handle
->path
) {
196 BT_LOGE_STR("Failed to allocate a GString.");
200 shared_lib_handle
->module
= g_module_open(path
, G_MODULE_BIND_LOCAL
);
201 if (!shared_lib_handle
->module
) {
203 * DEBUG-level logging because we're only _trying_ to
204 * open this file as a Babeltrace plugin: if it's not,
205 * it's not an error. And because this can be tried
206 * during bt_plugin_create_all_from_dir(), it's not even
209 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
210 g_module_error(), path
);
217 BT_PUT(shared_lib_handle
);
220 if (shared_lib_handle
) {
221 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
222 path
, shared_lib_handle
);
225 return shared_lib_handle
;
229 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
231 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
233 if (!plugin
->spec_data
) {
237 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
239 BT_PUT(spec
->shared_lib_handle
);
240 g_free(plugin
->spec_data
);
241 plugin
->spec_data
= NULL
;
245 * This function does the following:
247 * 1. Iterate on the plugin descriptor attributes section and set the
248 * plugin's attributes depending on the attribute types. This
249 * includes the name of the plugin, its description, and its
250 * initialization function, for example.
252 * 2. Iterate on the component class descriptors section and create one
253 * "full descriptor" (temporary structure) for each one that is found
254 * and attached to our plugin descriptor.
256 * 3. Iterate on the component class descriptor attributes section and
257 * set the corresponding full descriptor's attributes depending on
258 * the attribute types. This includes the description of the
259 * component class, as well as its initialization and destroy
262 * 4. Call the user's plugin initialization function, if any is
265 * 5. For each full component class descriptor, create a component class
266 * object, set its optional attributes, and add it to the plugin
269 * 6. Freeze the plugin object.
272 enum bt_plugin_status
bt_plugin_so_init(
273 struct bt_plugin
*plugin
,
274 const struct __bt_plugin_descriptor
*descriptor
,
275 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
276 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
277 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
278 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
279 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
280 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
283 * This structure's members point to the plugin's memory
286 struct comp_class_full_descriptor
{
287 const struct __bt_plugin_component_class_descriptor
*descriptor
;
288 const char *description
;
290 bt_component_class_init_method init_method
;
291 bt_component_class_finalize_method finalize_method
;
292 bt_component_class_query_method query_method
;
293 bt_component_class_accept_port_connection_method accept_port_connection_method
;
294 bt_component_class_port_connected_method port_connected_method
;
295 bt_component_class_port_disconnected_method port_disconnected_method
;
296 struct bt_component_class_iterator_methods iterator_methods
;
299 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
300 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
301 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
302 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
303 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
304 GArray
*comp_class_full_descriptors
;
308 BT_LOGD("Initializing plugin object from descriptors found in sections: "
309 "plugin-addr=%p, plugin-path=\"%s\", "
310 "attrs-begin-addr=%p, attrs-end-addr=%p, "
311 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
312 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
314 spec
->shared_lib_handle
->path
?
315 spec
->shared_lib_handle
->path
->str
: NULL
,
316 attrs_begin
, attrs_end
,
317 cc_descriptors_begin
, cc_descriptors_end
,
318 cc_descr_attrs_begin
, cc_descr_attrs_end
);
319 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
320 sizeof(struct comp_class_full_descriptor
));
321 if (!comp_class_full_descriptors
) {
322 BT_LOGE_STR("Failed to allocate a GArray.");
323 status
= BT_PLUGIN_STATUS_ERROR
;
327 /* Set mandatory attributes */
328 spec
->descriptor
= descriptor
;
329 bt_plugin_set_name(plugin
, descriptor
->name
);
332 * Find and set optional attributes attached to this plugin
335 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
336 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
339 if (cur_attr
->plugin_descriptor
!= descriptor
) {
343 switch (cur_attr
->type
) {
344 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
345 spec
->init
= cur_attr
->value
.init
;
347 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
348 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
350 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
351 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
353 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
354 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
356 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
357 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
359 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
360 bt_plugin_set_version(plugin
,
361 (unsigned int) cur_attr
->value
.version
.major
,
362 (unsigned int) cur_attr
->value
.version
.minor
,
363 (unsigned int) cur_attr
->value
.version
.patch
,
364 cur_attr
->value
.version
.extra
);
368 * WARN-level logging because this should not
369 * happen with the appropriate ABI version. If
370 * we're here, we know that for the reported
371 * version of the ABI, this attribute is
374 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
375 "plugin-path=\"%s\", plugin-name=\"%s\", "
376 "attr-type-name=\"%s\", attr-type-id=%d",
377 spec
->shared_lib_handle
->path
?
378 spec
->shared_lib_handle
->path
->str
:
380 descriptor
->name
, cur_attr
->type_name
,
387 * Find component class descriptors attached to this plugin
388 * descriptor and initialize corresponding full component class
389 * descriptors in the array.
391 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
392 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
394 struct comp_class_full_descriptor full_descriptor
= {0};
396 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
400 full_descriptor
.descriptor
= cur_cc_descr
;
401 g_array_append_val(comp_class_full_descriptors
,
406 * Find component class descriptor attributes attached to this
407 * plugin descriptor and update corresponding full component
408 * class descriptors in the array.
410 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
411 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
412 *cur_cc_descr_attr_ptr
;
414 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
419 /* Find the corresponding component class descriptor entry */
420 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
421 struct comp_class_full_descriptor
*cc_full_descr
=
422 &g_array_index(comp_class_full_descriptors
,
423 struct comp_class_full_descriptor
, i
);
425 if (cur_cc_descr_attr
->comp_class_descriptor
==
426 cc_full_descr
->descriptor
) {
427 switch (cur_cc_descr_attr
->type
) {
428 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
429 cc_full_descr
->description
=
430 cur_cc_descr_attr
->value
.description
;
432 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
433 cc_full_descr
->help
=
434 cur_cc_descr_attr
->value
.help
;
436 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
437 cc_full_descr
->init_method
=
438 cur_cc_descr_attr
->value
.init_method
;
440 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
441 cc_full_descr
->finalize_method
=
442 cur_cc_descr_attr
->value
.finalize_method
;
444 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
445 cc_full_descr
->query_method
=
446 cur_cc_descr_attr
->value
.query_method
;
448 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
449 cc_full_descr
->accept_port_connection_method
=
450 cur_cc_descr_attr
->value
.accept_port_connection_method
;
452 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD
:
453 cc_full_descr
->port_connected_method
=
454 cur_cc_descr_attr
->value
.port_connected_method
;
456 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
457 cc_full_descr
->port_disconnected_method
=
458 cur_cc_descr_attr
->value
.port_disconnected_method
;
460 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
461 cc_full_descr
->iterator_methods
.init
=
462 cur_cc_descr_attr
->value
.notif_iter_init_method
;
464 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
465 cc_full_descr
->iterator_methods
.finalize
=
466 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
468 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD
:
469 cc_full_descr
->iterator_methods
.seek_time
=
470 cur_cc_descr_attr
->value
.notif_iter_seek_time_method
;
474 * WARN-level logging because
475 * this should not happen with
476 * the appropriate ABI version.
477 * If we're here, we know that
478 * for the reported version of
479 * the ABI, this attribute is
482 BT_LOGW("Ignoring unknown component class descriptor attribute: "
483 "plugin-path=\"%s\", "
484 "plugin-name=\"%s\", "
485 "comp-class-name=\"%s\", "
486 "comp-class-type=%s, "
487 "attr-type-name=\"%s\", "
489 spec
->shared_lib_handle
->path
?
490 spec
->shared_lib_handle
->path
->str
:
493 cur_cc_descr_attr
->comp_class_descriptor
->name
,
494 bt_component_class_type_string(
495 cur_cc_descr_attr
->comp_class_descriptor
->type
),
496 cur_cc_descr_attr
->type_name
,
497 cur_cc_descr_attr
->type
);
504 /* Initialize plugin */
506 BT_LOGD_STR("Calling user's plugin initialization function.");
507 status
= spec
->init(plugin
);
508 BT_LOGD("User function returned: %s",
509 bt_plugin_status_string(status
));
512 BT_LOGW_STR("User's plugin initialization function failed.");
517 spec
->shared_lib_handle
->init_called
= BT_TRUE
;
519 /* Add described component classes to plugin */
520 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
521 struct comp_class_full_descriptor
*cc_full_descr
=
522 &g_array_index(comp_class_full_descriptors
,
523 struct comp_class_full_descriptor
, i
);
524 struct bt_component_class
*comp_class
;
526 BT_LOGD("Creating and setting properties of plugin's component class: "
527 "plugin-path=\"%s\", plugin-name=\"%s\", "
528 "comp-class-name=\"%s\", comp-class-type=%s",
529 spec
->shared_lib_handle
->path
?
530 spec
->shared_lib_handle
->path
->str
:
533 cc_full_descr
->descriptor
->name
,
534 bt_component_class_type_string(
535 cc_full_descr
->descriptor
->type
));
537 switch (cc_full_descr
->descriptor
->type
) {
538 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
539 comp_class
= bt_component_class_source_create(
540 cc_full_descr
->descriptor
->name
,
541 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
543 case BT_COMPONENT_CLASS_TYPE_FILTER
:
544 comp_class
= bt_component_class_filter_create(
545 cc_full_descr
->descriptor
->name
,
546 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
548 case BT_COMPONENT_CLASS_TYPE_SINK
:
549 comp_class
= bt_component_class_sink_create(
550 cc_full_descr
->descriptor
->name
,
551 cc_full_descr
->descriptor
->methods
.sink
.consume
);
555 * WARN-level logging because this should not
556 * happen with the appropriate ABI version. If
557 * we're here, we know that for the reported
558 * version of the ABI, this component class type
561 BT_LOGW("Ignoring unknown component class type: "
562 "plugin-path=\"%s\", plugin-name=\"%s\", "
563 "comp-class-name=\"%s\", comp-class-type=%d",
564 spec
->shared_lib_handle
->path
->str
?
565 spec
->shared_lib_handle
->path
->str
:
568 cc_full_descr
->descriptor
->name
,
569 cc_full_descr
->descriptor
->type
);
574 BT_LOGE_STR("Cannot create component class.");
575 status
= BT_PLUGIN_STATUS_ERROR
;
579 if (cc_full_descr
->description
) {
580 ret
= bt_component_class_set_description(comp_class
,
581 cc_full_descr
->description
);
583 BT_LOGE_STR("Cannot set component class's description.");
584 status
= BT_PLUGIN_STATUS_ERROR
;
590 if (cc_full_descr
->help
) {
591 ret
= bt_component_class_set_help(comp_class
,
592 cc_full_descr
->help
);
594 BT_LOGE_STR("Cannot set component class's help string.");
595 status
= BT_PLUGIN_STATUS_ERROR
;
601 if (cc_full_descr
->init_method
) {
602 ret
= bt_component_class_set_init_method(comp_class
,
603 cc_full_descr
->init_method
);
605 BT_LOGE_STR("Cannot set component class's initialization method.");
606 status
= BT_PLUGIN_STATUS_ERROR
;
612 if (cc_full_descr
->finalize_method
) {
613 ret
= bt_component_class_set_finalize_method(comp_class
,
614 cc_full_descr
->finalize_method
);
616 BT_LOGE_STR("Cannot set component class's finalization method.");
617 status
= BT_PLUGIN_STATUS_ERROR
;
623 if (cc_full_descr
->query_method
) {
624 ret
= bt_component_class_set_query_method(
625 comp_class
, cc_full_descr
->query_method
);
627 BT_LOGE_STR("Cannot set component class's query method.");
628 status
= BT_PLUGIN_STATUS_ERROR
;
634 if (cc_full_descr
->accept_port_connection_method
) {
635 ret
= bt_component_class_set_accept_port_connection_method(
636 comp_class
, cc_full_descr
->accept_port_connection_method
);
638 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
639 status
= BT_PLUGIN_STATUS_ERROR
;
645 if (cc_full_descr
->port_connected_method
) {
646 ret
= bt_component_class_set_port_connected_method(
647 comp_class
, cc_full_descr
->port_connected_method
);
649 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
650 status
= BT_PLUGIN_STATUS_ERROR
;
656 if (cc_full_descr
->port_disconnected_method
) {
657 ret
= bt_component_class_set_port_disconnected_method(
658 comp_class
, cc_full_descr
->port_disconnected_method
);
660 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
661 status
= BT_PLUGIN_STATUS_ERROR
;
667 switch (cc_full_descr
->descriptor
->type
) {
668 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
669 if (cc_full_descr
->iterator_methods
.init
) {
670 ret
= bt_component_class_source_set_notification_iterator_init_method(
672 cc_full_descr
->iterator_methods
.init
);
674 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
675 status
= BT_PLUGIN_STATUS_ERROR
;
681 if (cc_full_descr
->iterator_methods
.finalize
) {
682 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
684 cc_full_descr
->iterator_methods
.finalize
);
686 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
687 status
= BT_PLUGIN_STATUS_ERROR
;
693 if (cc_full_descr
->iterator_methods
.seek_time
) {
694 ret
= bt_component_class_source_set_notification_iterator_seek_time_method(
696 cc_full_descr
->iterator_methods
.seek_time
);
698 BT_LOGE_STR("Cannot set source component class's notification iterator seek to time method.");
699 status
= BT_PLUGIN_STATUS_ERROR
;
705 case BT_COMPONENT_CLASS_TYPE_FILTER
:
706 if (cc_full_descr
->iterator_methods
.init
) {
707 ret
= bt_component_class_filter_set_notification_iterator_init_method(
709 cc_full_descr
->iterator_methods
.init
);
711 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
712 status
= BT_PLUGIN_STATUS_ERROR
;
718 if (cc_full_descr
->iterator_methods
.finalize
) {
719 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
721 cc_full_descr
->iterator_methods
.finalize
);
723 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
724 status
= BT_PLUGIN_STATUS_ERROR
;
730 if (cc_full_descr
->iterator_methods
.seek_time
) {
731 ret
= bt_component_class_filter_set_notification_iterator_seek_time_method(
733 cc_full_descr
->iterator_methods
.seek_time
);
735 BT_LOGE_STR("Cannot set filter component class's notification iterator seek to time method.");
736 status
= BT_PLUGIN_STATUS_ERROR
;
742 case BT_COMPONENT_CLASS_TYPE_SINK
:
749 * Add component class to the plugin object.
751 * This will call back
752 * bt_plugin_so_on_add_component_class() so that we can
753 * add a mapping in the component class list when
754 * we know the component class is successfully added.
756 status
= bt_plugin_add_component_class(plugin
,
760 BT_LOGE("Cannot add component class to plugin.");
766 * All the plugin's component classes should be added at this
767 * point. We freeze the plugin so that it's not possible to add
768 * component classes to this plugin object after this stage
769 * (plugin object becomes immutable).
771 bt_plugin_freeze(plugin
);
774 g_array_free(comp_class_full_descriptors
, TRUE
);
779 struct bt_plugin
*bt_plugin_so_create_empty(
780 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
782 struct bt_plugin
*plugin
;
783 struct bt_plugin_so_spec_data
*spec
;
785 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
790 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
791 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
792 if (!plugin
->spec_data
) {
793 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
797 spec
= plugin
->spec_data
;
798 spec
->shared_lib_handle
= bt_get(shared_lib_handle
);
809 struct bt_plugin_set
*bt_plugin_so_create_all_from_sections(
810 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
811 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
812 struct __bt_plugin_descriptor
const * const *descriptors_end
,
813 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
814 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
815 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
816 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
817 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
818 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
820 size_t descriptor_count
;
822 size_t cc_descriptors_count
;
823 size_t cc_descr_attrs_count
;
825 struct bt_plugin_set
*plugin_set
= NULL
;
827 descriptor_count
= descriptors_end
- descriptors_begin
;
828 attrs_count
= attrs_end
- attrs_begin
;
829 cc_descriptors_count
= cc_descriptors_end
- cc_descriptors_begin
;
830 cc_descr_attrs_count
= cc_descr_attrs_end
- cc_descr_attrs_begin
;
832 BT_LOGD("Creating all SO plugins from sections: "
833 "plugin-path=\"%s\", "
834 "descr-begin-addr=%p, descr-end-addr=%p, "
835 "attrs-begin-addr=%p, attrs-end-addr=%p, "
836 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
837 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
838 "descr-count=%zu, attrs-count=%zu, "
839 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
840 shared_lib_handle
->path
? shared_lib_handle
->path
->str
: NULL
,
841 descriptors_begin
, descriptors_end
,
842 attrs_begin
, attrs_end
,
843 cc_descriptors_begin
, cc_descriptors_end
,
844 cc_descr_attrs_begin
, cc_descr_attrs_end
,
845 descriptor_count
, attrs_count
,
846 cc_descriptors_count
, cc_descr_attrs_count
);
847 plugin_set
= bt_plugin_set_create();
849 BT_LOGE_STR("Cannot create empty plugin set.");
853 for (i
= 0; i
< descriptor_count
; i
++) {
854 enum bt_plugin_status status
;
855 const struct __bt_plugin_descriptor
*descriptor
=
856 descriptors_begin
[i
];
857 struct bt_plugin
*plugin
;
859 BT_LOGD("Creating plugin object for plugin: "
860 "name=\"%s\", abi-major=%d, abi-minor=%d",
861 descriptor
->name
, descriptor
->major
, descriptor
->minor
);
863 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
865 * DEBUG-level logging because we're only
866 * _trying_ to open this file as a compatible
867 * Babeltrace plugin: if it's not, it's not an
868 * error. And because this can be tried during
869 * bt_plugin_create_all_from_dir(), it's not
872 BT_LOGD("Unknown ABI major version: abi-major=%d",
877 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
879 BT_LOGE_STR("Cannot create empty shared library handle.");
883 if (shared_lib_handle
&& shared_lib_handle
->path
) {
884 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
887 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
888 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
889 cc_descr_attrs_begin
, cc_descr_attrs_end
);
892 * DEBUG-level logging because we're only
893 * _trying_ to open this file as a compatible
894 * Babeltrace plugin: if it's not, it's not an
895 * error. And because this can be tried during
896 * bt_plugin_create_all_from_dir(), it's not
899 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
904 /* Add to plugin set */
905 bt_plugin_set_add_plugin(plugin_set
, plugin
);
919 struct bt_plugin_set
*bt_plugin_so_create_all_from_static(void)
921 struct bt_plugin_set
*plugin_set
= NULL
;
922 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
923 bt_plugin_so_shared_lib_handle_create(NULL
);
925 if (!shared_lib_handle
) {
929 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
930 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
931 SECTION_BEGIN(__bt_plugin_descriptors
),
932 SECTION_END(__bt_plugin_descriptors
),
933 SECTION_BEGIN(__bt_plugin_descriptor_attributes
),
934 SECTION_END(__bt_plugin_descriptor_attributes
),
935 SECTION_BEGIN(__bt_plugin_component_class_descriptors
),
936 SECTION_END(__bt_plugin_component_class_descriptors
),
937 SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes
),
938 SECTION_END(__bt_plugin_component_class_descriptor_attributes
));
941 BT_PUT(shared_lib_handle
);
947 struct bt_plugin_set
*bt_plugin_so_create_all_from_file(const char *path
)
950 struct bt_plugin_set
*plugin_set
= NULL
;
951 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
952 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
953 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
954 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
955 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
956 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
957 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
958 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
959 bt_bool is_libtool_wrapper
= BT_FALSE
, is_shared_object
= BT_FALSE
;
960 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
963 BT_LOGW_STR("Invalid parameter: path is NULL.");
967 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path
);
968 path_len
= strlen(path
);
969 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
970 BT_LOGW("Invalid parameter: path length is too short: "
971 "path-length=%zu", path_len
);
977 * Check if the file ends with a known plugin file type suffix (i.e. .so
980 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
981 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
982 LIBTOOL_PLUGIN_SUFFIX_LEN
);
983 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
984 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
985 NATIVE_PLUGIN_SUFFIX_LEN
);
986 if (!is_shared_object
&& !is_libtool_wrapper
) {
987 /* Name indicates this is not a plugin file; not an error */
988 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path
);
992 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
993 if (!shared_lib_handle
) {
994 BT_LOGD_STR("Cannot create shared library handle.");
998 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptors",
999 (gpointer
*) &descriptors_begin
)) {
1000 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1001 "symbol=\"%s\"", path
,
1002 "__start___bt_plugin_descriptors");
1006 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptors",
1007 (gpointer
*) &descriptors_end
)) {
1008 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1009 "symbol=\"%s\"", path
,
1010 "__stop___bt_plugin_descriptors");
1014 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptor_attributes",
1015 (gpointer
*) &attrs_begin
)) {
1016 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1017 "symbol=\"%s\"", path
,
1018 "__start___bt_plugin_descriptor_attributes");
1021 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptor_attributes",
1022 (gpointer
*) &attrs_end
)) {
1023 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1024 "symbol=\"%s\"", path
,
1025 "__stop___bt_plugin_descriptor_attributes");
1028 if ((!!attrs_begin
- !!attrs_end
) != 0) {
1029 BT_LOGD("Found section start or end symbol, but not both: "
1030 "path=\"%s\", symbol-start=\"%s\", "
1031 "symbol-end=\"%s\", symbol-start-addr=%p, "
1032 "symbol-end-addr=%p",
1033 path
, "__start___bt_plugin_descriptor_attributes",
1034 "__stop___bt_plugin_descriptor_attributes",
1035 attrs_begin
, attrs_end
);
1039 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptors",
1040 (gpointer
*) &cc_descriptors_begin
)) {
1041 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1042 "symbol=\"%s\"", path
,
1043 "__start___bt_plugin_component_class_descriptors");
1046 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptors",
1047 (gpointer
*) &cc_descriptors_end
)) {
1048 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1049 "symbol=\"%s\"", path
,
1050 "__stop___bt_plugin_component_class_descriptors");
1053 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
1054 BT_LOGD("Found section start or end symbol, but not both: "
1055 "path=\"%s\", symbol-start=\"%s\", "
1056 "symbol-end=\"%s\", symbol-start-addr=%p, "
1057 "symbol-end-addr=%p",
1058 path
, "__start___bt_plugin_component_class_descriptors",
1059 "__stop___bt_plugin_component_class_descriptors",
1060 cc_descriptors_begin
, cc_descriptors_end
);
1064 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptor_attributes",
1065 (gpointer
*) &cc_descr_attrs_begin
)) {
1066 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1067 "symbol=\"%s\"", path
,
1068 "__start___bt_plugin_component_class_descriptor_attributes");
1071 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptor_attributes",
1072 (gpointer
*) &cc_descr_attrs_end
)) {
1073 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1074 "symbol=\"%s\"", path
,
1075 "__stop___bt_plugin_component_class_descriptor_attributes");
1078 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
1079 BT_LOGD("Found section start or end symbol, but not both: "
1080 "path=\"%s\", symbol-start=\"%s\", "
1081 "symbol-end=\"%s\", symbol-start-addr=%p, "
1082 "symbol-end-addr=%p",
1083 path
, "__start___bt_plugin_component_class_descriptor_attributes",
1084 "__stop___bt_plugin_component_class_descriptor_attributes",
1085 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1089 /* Initialize plugin */
1090 BT_LOGD_STR("Initializing plugin object.");
1091 plugin_set
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
1092 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
1093 cc_descriptors_begin
, cc_descriptors_end
,
1094 cc_descr_attrs_begin
, cc_descr_attrs_end
);
1097 BT_PUT(shared_lib_handle
);
1102 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
1105 bt_list_del(&comp_class
->node
);
1106 BT_PUT(comp_class
->so_handle
);
1107 BT_LOGV("Component class destroyed: removed entry from list: "
1108 "comp-cls-addr=%p", comp_class
);
1112 void bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
1113 struct bt_component_class
*comp_class
)
1115 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
1117 assert(plugin
->spec_data
);
1118 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
1120 bt_list_add(&comp_class
->node
, &component_class_list
);
1121 comp_class
->so_handle
= bt_get(spec
->shared_lib_handle
);
1123 /* Add our custom destroy listener */
1124 bt_component_class_add_destroy_listener(comp_class
,
1125 plugin_comp_class_destroy_listener
, NULL
);