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 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/plugin/plugin-internal.h>
33 #include <babeltrace/plugin/plugin-so-internal.h>
34 #include <babeltrace/plugin/plugin-dev.h>
35 #include <babeltrace/plugin/plugin-internal.h>
36 #include <babeltrace/graph/component-class-internal.h>
42 #define NATIVE_PLUGIN_SUFFIX ".so"
43 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
44 #define LIBTOOL_PLUGIN_SUFFIX ".la"
45 #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
47 #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
48 sizeof(LIBTOOL_PLUGIN_SUFFIX))
50 #define SECTION_BEGIN(_name) (&(__start_##_name))
51 #define SECTION_END(_name) (&(__stop_##_name))
52 #define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name))
54 #define DECLARE_SECTION(_type, _name) \
55 extern _type __start_##_name __attribute((weak)); \
56 extern _type __stop_##_name __attribute((weak))
58 DECLARE_SECTION(struct __bt_plugin_descriptor
const *, __bt_plugin_descriptors
);
59 DECLARE_SECTION(struct __bt_plugin_descriptor_attribute
const *, __bt_plugin_descriptor_attributes
);
60 DECLARE_SECTION(struct __bt_plugin_component_class_descriptor
const *, __bt_plugin_component_class_descriptors
);
61 DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute
const *, __bt_plugin_component_class_descriptor_attributes
);
64 * This hash table, global to the library, maps component class pointers
65 * to shared library handles.
67 * The keys (component classes) are NOT owned by this hash table, whereas
68 * the values (shared library handles) are owned by this hash table.
70 * The keys are the component classes created with
71 * bt_plugin_add_component_class(). They keep the shared library handle
72 * object created by their plugin alive so that the plugin's code is
73 * not discarded when it could still be in use by living components
74 * created from those component classes:
76 * [component] --ref-> [component class] --through this HT-> [shlib handle]
78 * This hash table exists for two reasons:
80 * 1. To allow this application:
82 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
83 * // instantiate components from a plugin's component classes
84 * // put plugins and free my_plugins here
85 * // user code of instantiated components still exists
87 * 2. To decouple the plugin subsystem from the component subsystem:
88 * while plugins objects need to know component class objects, the
89 * opposite is not necessary, thus it makes no sense for a component
90 * class to keep a reference to the plugin object from which it was
93 * An entry is removed from this HT when a component class is destroyed
94 * thanks to a custom destroy listener. When the entry is removed, the
95 * GLib function calls the value destroy notifier of the HT, which is
96 * bt_put(). This decreases the reference count of the mapped shared
97 * library handle. Assuming the original plugin object which contained
98 * some component classes is put first, when the last component class is
99 * removed from this HT, the shared library handle object's reference
100 * count falls to zero and the shared library is finally closed.
103 GHashTable
*comp_classes_to_shlib_handles
;
105 __attribute__((constructor
)) static
106 void init_comp_classes_to_shlib_handles(void) {
107 comp_classes_to_shlib_handles
= g_hash_table_new_full(g_direct_hash
,
108 g_direct_equal
, NULL
, bt_put
);
109 assert(comp_classes_to_shlib_handles
);
112 __attribute__((destructor
)) static
113 void fini_comp_classes_to_shlib_handles(void) {
114 if (comp_classes_to_shlib_handles
) {
115 g_hash_table_destroy(comp_classes_to_shlib_handles
);
120 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object
*obj
)
122 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
;
125 shared_lib_handle
= container_of(obj
,
126 struct bt_plugin_so_shared_lib_handle
, base
);
128 if (shared_lib_handle
->init_called
&& shared_lib_handle
->exit
) {
129 enum bt_plugin_status status
= shared_lib_handle
->exit();
132 const char *path
= shared_lib_handle
->path
?
133 shared_lib_handle
->path
->str
: "[built-in]";
135 printf_verbose("Plugin in module `%s` exited with error %d\n",
140 if (shared_lib_handle
->module
) {
141 if (!g_module_close(shared_lib_handle
->module
)) {
142 printf_error("Module close error: %s\n",
147 if (shared_lib_handle
->path
) {
148 g_string_free(shared_lib_handle
->path
, TRUE
);
151 g_free(shared_lib_handle
);
155 struct bt_plugin_so_shared_lib_handle
*bt_plugin_so_shared_lib_handle_create(
158 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
160 shared_lib_handle
= g_new0(struct bt_plugin_so_shared_lib_handle
, 1);
161 if (!shared_lib_handle
) {
165 bt_object_init(shared_lib_handle
, bt_plugin_so_shared_lib_handle_destroy
);
171 shared_lib_handle
->path
= g_string_new(path
);
172 if (!shared_lib_handle
->path
) {
176 shared_lib_handle
->module
= g_module_open(path
, 0);
177 if (!shared_lib_handle
->module
) {
178 printf_verbose("Module open error: %s\n", g_module_error());
185 BT_PUT(shared_lib_handle
);
188 return shared_lib_handle
;
192 void bt_plugin_so_destroy_spec_data(struct bt_plugin
*plugin
)
194 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
196 if (!plugin
->spec_data
) {
200 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
202 BT_PUT(spec
->shared_lib_handle
);
203 g_free(plugin
->spec_data
);
204 plugin
->spec_data
= NULL
;
208 * This function does the following:
210 * 1. Iterate on the plugin descriptor attributes section and set the
211 * plugin's attributes depending on the attribute types. This
212 * includes the name of the plugin, its description, and its
213 * initialization function, for example.
215 * 2. Iterate on the component class descriptors section and create one
216 * "full descriptor" (temporary structure) for each one that is found
217 * and attached to our plugin descriptor.
219 * 3. Iterate on the component class descriptor attributes section and
220 * set the corresponding full descriptor's attributes depending on
221 * the attribute types. This includes the description of the
222 * component class, as well as its initialization and destroy
225 * 4. Call the user's plugin initialization function, if any is
228 * 5. For each full component class descriptor, create a component class
229 * object, set its optional attributes, and add it to the plugin
232 * 6. Freeze the plugin object.
235 enum bt_plugin_status
bt_plugin_so_init(
236 struct bt_plugin
*plugin
,
237 const struct __bt_plugin_descriptor
*descriptor
,
238 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
239 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
240 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
241 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
242 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
243 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
246 * This structure's members point to the plugin's memory
249 struct comp_class_full_descriptor
{
250 const struct __bt_plugin_component_class_descriptor
*descriptor
;
251 const char *description
;
253 bt_component_class_init_method init_method
;
254 bt_component_class_finalize_method finalize_method
;
255 bt_component_class_query_method query_method
;
256 bt_component_class_accept_port_connection_method accept_port_connection_method
;
257 bt_component_class_port_disconnected_method port_disconnected_method
;
258 struct bt_component_class_iterator_methods iterator_methods
;
261 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
262 struct __bt_plugin_descriptor_attribute
const * const *cur_attr_ptr
;
263 struct __bt_plugin_component_class_descriptor
const * const *cur_cc_descr_ptr
;
264 struct __bt_plugin_component_class_descriptor_attribute
const * const *cur_cc_descr_attr_ptr
;
265 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
266 GArray
*comp_class_full_descriptors
;
270 comp_class_full_descriptors
= g_array_new(FALSE
, TRUE
,
271 sizeof(struct comp_class_full_descriptor
));
272 if (!comp_class_full_descriptors
) {
273 status
= BT_PLUGIN_STATUS_ERROR
;
277 /* Set mandatory attributes */
278 spec
->descriptor
= descriptor
;
279 bt_plugin_set_name(plugin
, descriptor
->name
);
282 * Find and set optional attributes attached to this plugin
285 for (cur_attr_ptr
= attrs_begin
; cur_attr_ptr
!= attrs_end
; cur_attr_ptr
++) {
286 const struct __bt_plugin_descriptor_attribute
*cur_attr
=
289 if (cur_attr
->plugin_descriptor
!= descriptor
) {
293 switch (cur_attr
->type
) {
294 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT
:
295 spec
->init
= cur_attr
->value
.init
;
297 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT
:
298 spec
->shared_lib_handle
->exit
= cur_attr
->value
.exit
;
300 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR
:
301 bt_plugin_set_author(plugin
, cur_attr
->value
.author
);
303 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE
:
304 bt_plugin_set_license(plugin
, cur_attr
->value
.license
);
306 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
307 bt_plugin_set_description(plugin
, cur_attr
->value
.description
);
309 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION
:
310 bt_plugin_set_version(plugin
,
311 (unsigned int) cur_attr
->value
.version
.major
,
312 (unsigned int) cur_attr
->value
.version
.minor
,
313 (unsigned int) cur_attr
->value
.version
.patch
,
314 cur_attr
->value
.version
.extra
);
317 printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n",
318 cur_attr
->type_name
, cur_attr
->type
,
325 * Find component class descriptors attached to this plugin
326 * descriptor and initialize corresponding full component class
327 * descriptors in the array.
329 for (cur_cc_descr_ptr
= cc_descriptors_begin
; cur_cc_descr_ptr
!= cc_descriptors_end
; cur_cc_descr_ptr
++) {
330 const struct __bt_plugin_component_class_descriptor
*cur_cc_descr
=
332 struct comp_class_full_descriptor full_descriptor
= {0};
334 if (cur_cc_descr
->plugin_descriptor
!= descriptor
) {
338 full_descriptor
.descriptor
= cur_cc_descr
;
339 g_array_append_val(comp_class_full_descriptors
,
344 * Find component class descriptor attributes attached to this
345 * plugin descriptor and update corresponding full component
346 * class descriptors in the array.
348 for (cur_cc_descr_attr_ptr
= cc_descr_attrs_begin
; cur_cc_descr_attr_ptr
!= cc_descr_attrs_end
; cur_cc_descr_attr_ptr
++) {
349 const struct __bt_plugin_component_class_descriptor_attribute
*cur_cc_descr_attr
=
350 *cur_cc_descr_attr_ptr
;
352 if (cur_cc_descr_attr
->comp_class_descriptor
->plugin_descriptor
!=
357 /* Find the corresponding component class descriptor entry */
358 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
359 struct comp_class_full_descriptor
*cc_full_descr
=
360 &g_array_index(comp_class_full_descriptors
,
361 struct comp_class_full_descriptor
, i
);
363 if (cur_cc_descr_attr
->comp_class_descriptor
==
364 cc_full_descr
->descriptor
) {
365 switch (cur_cc_descr_attr
->type
) {
366 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION
:
367 cc_full_descr
->description
=
368 cur_cc_descr_attr
->value
.description
;
370 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP
:
371 cc_full_descr
->help
=
372 cur_cc_descr_attr
->value
.help
;
374 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD
:
375 cc_full_descr
->init_method
=
376 cur_cc_descr_attr
->value
.init_method
;
378 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD
:
379 cc_full_descr
->finalize_method
=
380 cur_cc_descr_attr
->value
.finalize_method
;
382 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD
:
383 cc_full_descr
->query_method
=
384 cur_cc_descr_attr
->value
.query_method
;
386 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD
:
387 cc_full_descr
->accept_port_connection_method
=
388 cur_cc_descr_attr
->value
.accept_port_connection_method
;
390 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD
:
391 cc_full_descr
->port_disconnected_method
=
392 cur_cc_descr_attr
->value
.port_disconnected_method
;
394 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD
:
395 cc_full_descr
->iterator_methods
.init
=
396 cur_cc_descr_attr
->value
.notif_iter_init_method
;
398 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD
:
399 cc_full_descr
->iterator_methods
.finalize
=
400 cur_cc_descr_attr
->value
.notif_iter_finalize_method
;
402 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD
:
403 cc_full_descr
->iterator_methods
.seek_time
=
404 cur_cc_descr_attr
->value
.notif_iter_seek_time_method
;
407 printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n",
408 cur_cc_descr_attr
->type_name
,
409 cur_cc_descr_attr
->type
,
410 cur_cc_descr_attr
->comp_class_descriptor
->name
,
411 cur_cc_descr_attr
->comp_class_descriptor
->type
,
419 /* Initialize plugin */
421 status
= spec
->init(plugin
);
423 printf_verbose("Plugin `%s` initialization error: %d\n",
424 bt_plugin_get_name(plugin
), status
);
429 spec
->shared_lib_handle
->init_called
= true;
431 /* Add described component classes to plugin */
432 for (i
= 0; i
< comp_class_full_descriptors
->len
; i
++) {
433 struct comp_class_full_descriptor
*cc_full_descr
=
434 &g_array_index(comp_class_full_descriptors
,
435 struct comp_class_full_descriptor
, i
);
436 struct bt_component_class
*comp_class
;
438 switch (cc_full_descr
->descriptor
->type
) {
439 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
440 comp_class
= bt_component_class_source_create(
441 cc_full_descr
->descriptor
->name
,
442 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
444 case BT_COMPONENT_CLASS_TYPE_FILTER
:
445 comp_class
= bt_component_class_filter_create(
446 cc_full_descr
->descriptor
->name
,
447 cc_full_descr
->descriptor
->methods
.source
.notif_iter_next
);
449 case BT_COMPONENT_CLASS_TYPE_SINK
:
450 comp_class
= bt_component_class_sink_create(
451 cc_full_descr
->descriptor
->name
,
452 cc_full_descr
->descriptor
->methods
.sink
.consume
);
455 printf_verbose("WARNING: Unknown component class type %d for component class %s in plugin %s\n",
456 cc_full_descr
->descriptor
->type
,
457 cc_full_descr
->descriptor
->name
,
463 status
= BT_PLUGIN_STATUS_ERROR
;
467 if (cc_full_descr
->description
) {
468 ret
= bt_component_class_set_description(comp_class
,
469 cc_full_descr
->description
);
471 status
= BT_PLUGIN_STATUS_ERROR
;
477 if (cc_full_descr
->help
) {
478 ret
= bt_component_class_set_help(comp_class
,
479 cc_full_descr
->help
);
481 status
= BT_PLUGIN_STATUS_ERROR
;
487 if (cc_full_descr
->init_method
) {
488 ret
= bt_component_class_set_init_method(comp_class
,
489 cc_full_descr
->init_method
);
491 status
= BT_PLUGIN_STATUS_ERROR
;
497 if (cc_full_descr
->finalize_method
) {
498 ret
= bt_component_class_set_finalize_method(comp_class
,
499 cc_full_descr
->finalize_method
);
501 status
= BT_PLUGIN_STATUS_ERROR
;
507 if (cc_full_descr
->query_method
) {
508 ret
= bt_component_class_set_query_method(
509 comp_class
, cc_full_descr
->query_method
);
511 status
= BT_PLUGIN_STATUS_ERROR
;
517 if (cc_full_descr
->accept_port_connection_method
) {
518 ret
= bt_component_class_set_accept_port_connection_method(
519 comp_class
, cc_full_descr
->accept_port_connection_method
);
521 status
= BT_PLUGIN_STATUS_ERROR
;
527 if (cc_full_descr
->port_disconnected_method
) {
528 ret
= bt_component_class_set_port_disconnected_method(
529 comp_class
, cc_full_descr
->port_disconnected_method
);
531 status
= BT_PLUGIN_STATUS_ERROR
;
537 switch (cc_full_descr
->descriptor
->type
) {
538 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
539 if (cc_full_descr
->iterator_methods
.init
) {
540 ret
= bt_component_class_source_set_notification_iterator_init_method(
542 cc_full_descr
->iterator_methods
.init
);
544 status
= BT_PLUGIN_STATUS_ERROR
;
550 if (cc_full_descr
->iterator_methods
.finalize
) {
551 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
553 cc_full_descr
->iterator_methods
.finalize
);
555 status
= BT_PLUGIN_STATUS_ERROR
;
561 if (cc_full_descr
->iterator_methods
.seek_time
) {
562 ret
= bt_component_class_source_set_notification_iterator_seek_time_method(
564 cc_full_descr
->iterator_methods
.seek_time
);
566 status
= BT_PLUGIN_STATUS_ERROR
;
572 case BT_COMPONENT_CLASS_TYPE_FILTER
:
573 if (cc_full_descr
->iterator_methods
.init
) {
574 ret
= bt_component_class_filter_set_notification_iterator_init_method(
576 cc_full_descr
->iterator_methods
.init
);
578 status
= BT_PLUGIN_STATUS_ERROR
;
584 if (cc_full_descr
->iterator_methods
.finalize
) {
585 ret
= bt_component_class_filter_set_notification_iterator_finalize_method(
587 cc_full_descr
->iterator_methods
.finalize
);
589 status
= BT_PLUGIN_STATUS_ERROR
;
595 if (cc_full_descr
->iterator_methods
.seek_time
) {
596 ret
= bt_component_class_filter_set_notification_iterator_seek_time_method(
598 cc_full_descr
->iterator_methods
.seek_time
);
600 status
= BT_PLUGIN_STATUS_ERROR
;
606 case BT_COMPONENT_CLASS_TYPE_SINK
:
614 * Add component class to the plugin object.
616 * This will call back
617 * bt_plugin_so_on_add_component_class() so that we can
618 * add a mapping in comp_classes_to_shlib_handles when
619 * we know the component class is successfully added.
621 status
= bt_plugin_add_component_class(plugin
,
625 printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n",
626 cc_full_descr
->descriptor
->name
,
627 cc_full_descr
->descriptor
->type
,
628 bt_plugin_get_name(plugin
), status
);
634 * All the plugin's component classes should be added at this
635 * point. We freeze the plugin so that it's not possible to add
636 * component classes to this plugin object after this stage
637 * (plugin object becomes immutable).
639 bt_plugin_freeze(plugin
);
642 g_array_free(comp_class_full_descriptors
, TRUE
);
647 struct bt_plugin
*bt_plugin_so_create_empty(
648 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
)
650 struct bt_plugin
*plugin
;
651 struct bt_plugin_so_spec_data
*spec
;
653 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_SO
);
658 plugin
->destroy_spec_data
= bt_plugin_so_destroy_spec_data
;
659 plugin
->spec_data
= g_new0(struct bt_plugin_so_spec_data
, 1);
660 if (!plugin
->spec_data
) {
664 spec
= plugin
->spec_data
;
665 spec
->shared_lib_handle
= bt_get(shared_lib_handle
);
676 struct bt_plugin
**bt_plugin_so_create_all_from_sections(
677 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
,
678 struct __bt_plugin_descriptor
const * const *descriptors_begin
,
679 struct __bt_plugin_descriptor
const * const *descriptors_end
,
680 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
,
681 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
,
682 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
,
683 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
,
684 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
,
685 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
)
687 size_t descriptor_count
;
689 size_t cc_descriptors_count
;
690 size_t cc_descr_attrs_count
;
692 struct bt_plugin
**plugins
= NULL
;
694 descriptor_count
= descriptors_end
- descriptors_begin
;
695 attrs_count
= attrs_end
- attrs_begin
;
696 cc_descriptors_count
= cc_descriptors_end
- cc_descriptors_begin
;
697 cc_descr_attrs_count
= cc_descr_attrs_end
- cc_descr_attrs_begin
;
698 printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n",
699 descriptors_begin
, descriptors_end
, descriptor_count
);
700 printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n",
701 attrs_begin
, attrs_end
, attrs_count
);
702 printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n",
703 cc_descriptors_begin
, cc_descriptors_end
, cc_descriptors_count
);
704 printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n",
705 cc_descr_attrs_begin
, cc_descr_attrs_end
, cc_descr_attrs_count
);
706 plugins
= calloc(descriptor_count
+ 1, sizeof(*plugins
));
711 for (i
= 0; i
< descriptor_count
; i
++) {
712 enum bt_plugin_status status
;
713 const struct __bt_plugin_descriptor
*descriptor
=
714 descriptors_begin
[i
];
715 struct bt_plugin
*plugin
;
717 printf_verbose("Loading plugin %s (ABI %d.%d)\n", descriptor
->name
,
718 descriptor
->major
, descriptor
->minor
);
720 if (descriptor
->major
> __BT_PLUGIN_VERSION_MAJOR
) {
721 printf_error("Unknown plugin's major version: %d\n",
726 plugin
= bt_plugin_so_create_empty(shared_lib_handle
);
728 printf_error("Cannot allocate plugin object for plugin %s\n",
733 if (shared_lib_handle
&& shared_lib_handle
->path
) {
734 bt_plugin_set_path(plugin
, shared_lib_handle
->path
->str
);
737 status
= bt_plugin_so_init(plugin
, descriptor
, attrs_begin
,
738 attrs_end
, cc_descriptors_begin
, cc_descriptors_end
,
739 cc_descr_attrs_begin
, cc_descr_attrs_end
);
741 printf_error("Cannot initialize plugin object %s\n",
747 /* Transfer ownership to the array */
762 struct bt_plugin
**bt_plugin_so_create_all_from_static(void)
764 struct bt_plugin
**plugins
= NULL
;
765 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
=
766 bt_plugin_so_shared_lib_handle_create(NULL
);
768 if (!shared_lib_handle
) {
772 plugins
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
773 SECTION_BEGIN(__bt_plugin_descriptors
),
774 SECTION_END(__bt_plugin_descriptors
),
775 SECTION_BEGIN(__bt_plugin_descriptor_attributes
),
776 SECTION_END(__bt_plugin_descriptor_attributes
),
777 SECTION_BEGIN(__bt_plugin_component_class_descriptors
),
778 SECTION_END(__bt_plugin_component_class_descriptors
),
779 SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes
),
780 SECTION_END(__bt_plugin_component_class_descriptor_attributes
));
783 BT_PUT(shared_lib_handle
);
789 struct bt_plugin
**bt_plugin_so_create_all_from_file(const char *path
)
792 struct bt_plugin
**plugins
= NULL
;
793 struct __bt_plugin_descriptor
const * const *descriptors_begin
= NULL
;
794 struct __bt_plugin_descriptor
const * const *descriptors_end
= NULL
;
795 struct __bt_plugin_descriptor_attribute
const * const *attrs_begin
= NULL
;
796 struct __bt_plugin_descriptor_attribute
const * const *attrs_end
= NULL
;
797 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_begin
= NULL
;
798 struct __bt_plugin_component_class_descriptor
const * const *cc_descriptors_end
= NULL
;
799 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_begin
= NULL
;
800 struct __bt_plugin_component_class_descriptor_attribute
const * const *cc_descr_attrs_end
= NULL
;
801 bool is_libtool_wrapper
= false, is_shared_object
= false;
802 struct bt_plugin_so_shared_lib_handle
*shared_lib_handle
= NULL
;
808 path_len
= strlen(path
);
809 if (path_len
<= PLUGIN_SUFFIX_LEN
) {
815 * Check if the file ends with a known plugin file type suffix (i.e. .so
818 is_libtool_wrapper
= !strncmp(LIBTOOL_PLUGIN_SUFFIX
,
819 path
+ path_len
- LIBTOOL_PLUGIN_SUFFIX_LEN
,
820 LIBTOOL_PLUGIN_SUFFIX_LEN
);
821 is_shared_object
= !strncmp(NATIVE_PLUGIN_SUFFIX
,
822 path
+ path_len
- NATIVE_PLUGIN_SUFFIX_LEN
,
823 NATIVE_PLUGIN_SUFFIX_LEN
);
824 if (!is_shared_object
&& !is_libtool_wrapper
) {
825 /* Name indicates that this is not a plugin file. */
829 shared_lib_handle
= bt_plugin_so_shared_lib_handle_create(path
);
830 if (!shared_lib_handle
) {
834 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptors",
835 (gpointer
*) &descriptors_begin
)) {
836 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
837 "__start___bt_plugin_descriptors",
838 g_module_name(shared_lib_handle
->module
));
842 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptors",
843 (gpointer
*) &descriptors_end
)) {
844 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
845 "__stop___bt_plugin_descriptors",
846 g_module_name(shared_lib_handle
->module
));
850 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_descriptor_attributes",
851 (gpointer
*) &attrs_begin
)) {
852 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
853 "__start___bt_plugin_descriptor_attributes",
854 g_module_name(shared_lib_handle
->module
));
857 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_descriptor_attributes",
858 (gpointer
*) &attrs_end
)) {
859 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
860 "__stop___bt_plugin_descriptor_attributes",
861 g_module_name(shared_lib_handle
->module
));
864 if ((!!attrs_begin
- !!attrs_end
) != 0) {
865 printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n",
866 g_module_name(shared_lib_handle
->module
));
870 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptors",
871 (gpointer
*) &cc_descriptors_begin
)) {
872 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
873 "__start___bt_plugin_component_class_descriptors",
874 g_module_name(shared_lib_handle
->module
));
877 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptors",
878 (gpointer
*) &cc_descriptors_end
)) {
879 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
880 "__stop___bt_plugin_component_class_descriptors",
881 g_module_name(shared_lib_handle
->module
));
884 if ((!!cc_descriptors_begin
- !!cc_descriptors_end
) != 0) {
885 printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n",
886 g_module_name(shared_lib_handle
->module
));
890 if (!g_module_symbol(shared_lib_handle
->module
, "__start___bt_plugin_component_class_descriptor_attributes",
891 (gpointer
*) &cc_descr_attrs_begin
)) {
892 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
893 "__start___bt_plugin_component_class_descriptor_attributes",
894 g_module_name(shared_lib_handle
->module
));
897 if (!g_module_symbol(shared_lib_handle
->module
, "__stop___bt_plugin_component_class_descriptor_attributes",
898 (gpointer
*) &cc_descr_attrs_end
)) {
899 printf_verbose("Unable to resolve plugin symbol %s from %s\n",
900 "__stop___bt_plugin_component_class_descriptor_attributes",
901 g_module_name(shared_lib_handle
->module
));
904 if ((!!cc_descr_attrs_begin
- !!cc_descr_attrs_end
) != 0) {
905 printf_verbose("Found __start___bt_plugin_component_class_descriptor_attributes or __stop___bt_plugin_component_class_descriptor_attributes symbol, but not both in %s\n",
906 g_module_name(shared_lib_handle
->module
));
910 /* Initialize plugin */
911 plugins
= bt_plugin_so_create_all_from_sections(shared_lib_handle
,
912 descriptors_begin
, descriptors_end
, attrs_begin
, attrs_end
,
913 cc_descriptors_begin
, cc_descriptors_end
,
914 cc_descr_attrs_begin
, cc_descr_attrs_end
);
917 BT_PUT(shared_lib_handle
);
922 void plugin_comp_class_destroy_listener(struct bt_component_class
*comp_class
,
925 gboolean exists
= g_hash_table_remove(comp_classes_to_shlib_handles
,
931 int bt_plugin_so_on_add_component_class(struct bt_plugin
*plugin
,
932 struct bt_component_class
*comp_class
)
935 struct bt_plugin_so_spec_data
*spec
= plugin
->spec_data
;
937 assert(plugin
->spec_data
);
938 assert(plugin
->type
== BT_PLUGIN_TYPE_SO
);
940 /* Map component class pointer to shared lib handle in global HT */
941 g_hash_table_insert(comp_classes_to_shlib_handles
, comp_class
,
942 bt_get(spec
->shared_lib_handle
));
944 /* Add our custom destroy listener */
945 ret
= bt_component_class_add_destroy_listener(comp_class
,
946 plugin_comp_class_destroy_listener
, NULL
);
953 /* Remove entry from global hash table (if exists) */
954 g_hash_table_remove(comp_classes_to_shlib_handles
,