cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / plugin / plugin-so.c
CommitLineData
55bb57e0 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
55bb57e0 5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
55bb57e0
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/PLUGIN-SO"
c2d9d9cf 9#include "lib/logging.h"
3fe0bf43 10
578e048b 11#include "common/assert.h"
578e048b 12#include "compat/compiler.h"
3fadfbc0 13#include <babeltrace2/plugin/plugin-dev.h>
578e048b 14#include "lib/graph/component-class.h"
3fadfbc0 15#include <babeltrace2/graph/component-class.h>
3fadfbc0 16#include <babeltrace2/types.h>
578e048b 17#include "common/list.h"
55bb57e0 18#include <string.h>
c4f23e30 19#include <stdbool.h>
0fbb9a9f 20#include <stdlib.h>
55bb57e0
PP
21#include <glib.h>
22#include <gmodule.h>
23
578e048b
MJ
24#include "plugin.h"
25#include "plugin-so.h"
d24d5663
PP
26#include "lib/func-status.h"
27#include "common/common.h"
578e048b 28
44a3451a 29#define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
55bb57e0
PP
30#define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
31#define LIBTOOL_PLUGIN_SUFFIX ".la"
32#define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX)
33
91d81473 34#define PLUGIN_SUFFIX_LEN bt_max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
55bb57e0
PP
35 sizeof(LIBTOOL_PLUGIN_SUFFIX))
36
52238017 37BT_PLUGIN_MODULE();
55bb57e0
PP
38
39/*
bfa9a4be
MD
40 * This list, global to the library, keeps all component classes that
41 * have a reference to their shared library handles. It allows iteration
42 * on all component classes still present when the destructor executes
43 * to release the shared library handle references they might still have.
55bb57e0 44 *
bfa9a4be 45 * The list items are the component classes created with
55bb57e0
PP
46 * bt_plugin_add_component_class(). They keep the shared library handle
47 * object created by their plugin alive so that the plugin's code is
48 * not discarded when it could still be in use by living components
49 * created from those component classes:
50 *
bfa9a4be 51 * [component] --ref-> [component class]-> [shlib handle]
55bb57e0 52 *
bfa9a4be 53 * It allows this use-case:
55bb57e0 54 *
c8db3219 55 * my_plugins = bt_plugin_find_all_from_file("/path/to/my-plugin.so");
55bb57e0
PP
56 * // instantiate components from a plugin's component classes
57 * // put plugins and free my_plugins here
58 * // user code of instantiated components still exists
59 *
bfa9a4be
MD
60 * An entry is removed from this list when a component class is
61 * destroyed thanks to a custom destroy listener. When the entry is
62 * removed, the entry is removed from the list, and we release the
63 * reference on the shlib handle. Assuming the original plugin object
64 * which contained some component classes is put first, when the last
65 * component class is removed from this list, the shared library handle
66 * object's reference count falls to zero and the shared library is
67 * finally closed.
5cea295b
PP
68 *
69 * We're not using a GLib linked list here because this destructor is
70 * called after GLib's thread-specific data is destroyed, which contains
71 * the allocated memory for GLib data structures (what's used by
72 * g_slice_alloc()).
55bb57e0 73 */
bfa9a4be 74
55bb57e0 75static
bfa9a4be 76BT_LIST_HEAD(component_class_list);
55bb57e0
PP
77
78__attribute__((destructor)) static
bfa9a4be
MD
79void fini_comp_class_list(void)
80{
81 struct bt_component_class *comp_class, *tmp;
82
83 bt_list_for_each_entry_safe(comp_class, tmp, &component_class_list, node) {
84 bt_list_del(&comp_class->node);
65300d60 85 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
55bb57e0 86 }
d94d92ac 87
bfa9a4be 88 BT_LOGD_STR("Released references from all component classes to shared library handles.");
55bb57e0
PP
89}
90
91static
92void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj)
93{
94 struct bt_plugin_so_shared_lib_handle *shared_lib_handle;
95
f6ccaed9 96 BT_ASSERT(obj);
55bb57e0
PP
97 shared_lib_handle = container_of(obj,
98 struct bt_plugin_so_shared_lib_handle, base);
3fe0bf43
PP
99 const char *path = shared_lib_handle->path ?
100 shared_lib_handle->path->str : NULL;
101
3f7d4d90 102 BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"",
3fe0bf43 103 shared_lib_handle, path);
55bb57e0
PP
104
105 if (shared_lib_handle->init_called && shared_lib_handle->exit) {
3fe0bf43 106 BT_LOGD_STR("Calling user's plugin exit function.");
9724cce9
PP
107 shared_lib_handle->exit();
108 BT_LOGD_STR("User function returned.");
55bb57e0
PP
109 }
110
111 if (shared_lib_handle->module) {
21e6ffa5 112#ifdef BT_DEBUG_MODE
f1447220
PP
113 /*
114 * Valgrind shows incomplete stack traces when
115 * dynamically loaded libraries are closed before it
16b7b5e7 116 * finishes. Use the LIBBABELTRACE2_NO_DLCLOSE in a debug
f1447220
PP
117 * build to avoid this.
118 */
16b7b5e7 119 const char *var = getenv("LIBBABELTRACE2_NO_DLCLOSE");
f1447220
PP
120
121 if (!var || strcmp(var, "1") != 0) {
122#endif
3f7d4d90 123 BT_LOGI("Closing GModule: path=\"%s\"", path);
3fe0bf43 124
f1447220 125 if (!g_module_close(shared_lib_handle->module)) {
870631a2
PP
126 /*
127 * Just log here: we're in a destructor,
128 * so we cannot append an error cause
129 * (there's no returned status).
130 */
3fe0bf43
PP
131 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
132 g_module_error(), path);
f1447220 133 }
db5504f9
PP
134
135 shared_lib_handle->module = NULL;
21e6ffa5 136#ifdef BT_DEBUG_MODE
3fe0bf43 137 } else {
16b7b5e7 138 BT_LOGI("Not closing GModule because `LIBBABELTRACE2_NO_DLCLOSE=1`: "
3fe0bf43 139 "path=\"%s\"", path);
55bb57e0 140 }
f1447220 141#endif
55bb57e0
PP
142 }
143
144 if (shared_lib_handle->path) {
145 g_string_free(shared_lib_handle->path, TRUE);
db5504f9 146 shared_lib_handle->path = NULL;
55bb57e0
PP
147 }
148
149 g_free(shared_lib_handle);
150}
151
152static
d24d5663 153int bt_plugin_so_shared_lib_handle_create(
9736d991
PP
154 const char *path,
155 struct bt_plugin_so_shared_lib_handle **shared_lib_handle)
55bb57e0 156{
d24d5663 157 int status = BT_FUNC_STATUS_OK;
55bb57e0 158
9736d991 159 BT_ASSERT(shared_lib_handle);
9ba4e1ec 160 BT_LOGI("Creating shared library handle: path=\"%s\"", path ? path : "(null)");
9736d991
PP
161 *shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1);
162 if (!*shared_lib_handle) {
870631a2 163 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one shared library handle.");
d24d5663 164 status = BT_FUNC_STATUS_MEMORY_ERROR;
9736d991 165 goto end;
55bb57e0
PP
166 }
167
9736d991 168 bt_object_init_shared(&(*shared_lib_handle)->base,
3fea54f6 169 bt_plugin_so_shared_lib_handle_destroy);
55bb57e0
PP
170
171 if (!path) {
172 goto end;
173 }
174
9736d991
PP
175 (*shared_lib_handle)->path = g_string_new(path);
176 if (!(*shared_lib_handle)->path) {
870631a2 177 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
d24d5663 178 status = BT_FUNC_STATUS_MEMORY_ERROR;
9736d991 179 goto end;
55bb57e0
PP
180 }
181
9736d991
PP
182 (*shared_lib_handle)->module = g_module_open(path, G_MODULE_BIND_LOCAL);
183 if (!(*shared_lib_handle)->module) {
50ad9320 184 /*
3f7d4d90 185 * INFO-level logging because we're only _trying_ to
50ad9320
PP
186 * open this file as a Babeltrace plugin: if it's not,
187 * it's not an error. And because this can be tried
3f7d4d90
PP
188 * during bt_plugin_find_all_from_dir(), it's not even a
189 * warning.
50ad9320 190 */
3f7d4d90 191 BT_LOGI("Cannot open GModule: %s: path=\"%s\"",
3fe0bf43 192 g_module_error(), path);
9736d991 193 BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle);
d24d5663 194 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991 195 goto end;
55bb57e0
PP
196 }
197
198 goto end;
199
55bb57e0 200end:
d24d5663 201 BT_ASSERT(*shared_lib_handle || status != BT_FUNC_STATUS_OK);
9736d991 202 if (*shared_lib_handle) {
3f7d4d90 203 BT_LOGI("Created shared library handle: path=\"%s\", addr=%p",
9ba4e1ec 204 path ? path : "(null)", *shared_lib_handle);
3fe0bf43
PP
205 }
206
9736d991 207 return status;
55bb57e0
PP
208}
209
6fbd4105 210static
55bb57e0
PP
211void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin)
212{
213 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
214
215 if (!plugin->spec_data) {
216 return;
217 }
218
f6ccaed9
PP
219 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
220 BT_ASSERT(spec);
65300d60 221 BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle);
55bb57e0
PP
222 g_free(plugin->spec_data);
223 plugin->spec_data = NULL;
224}
225
226/*
227 * This function does the following:
228 *
229 * 1. Iterate on the plugin descriptor attributes section and set the
230 * plugin's attributes depending on the attribute types. This
231 * includes the name of the plugin, its description, and its
232 * initialization function, for example.
233 *
234 * 2. Iterate on the component class descriptors section and create one
235 * "full descriptor" (temporary structure) for each one that is found
236 * and attached to our plugin descriptor.
237 *
238 * 3. Iterate on the component class descriptor attributes section and
239 * set the corresponding full descriptor's attributes depending on
240 * the attribute types. This includes the description of the
241 * component class, as well as its initialization and destroy
242 * methods.
243 *
244 * 4. Call the user's plugin initialization function, if any is
245 * defined.
246 *
247 * 5. For each full component class descriptor, create a component class
248 * object, set its optional attributes, and add it to the plugin
249 * object.
250 *
251 * 6. Freeze the plugin object.
252 */
253static
d24d5663 254int bt_plugin_so_init(struct bt_plugin *plugin,
9736d991 255 bool fail_on_load_error,
55bb57e0
PP
256 const struct __bt_plugin_descriptor *descriptor,
257 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
258 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
259 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
260 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
261 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
262 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
263{
264 /*
265 * This structure's members point to the plugin's memory
266 * (do NOT free).
267 */
268 struct comp_class_full_descriptor {
269 const struct __bt_plugin_component_class_descriptor *descriptor;
270 const char *description;
279b3f15 271 const char *help;
d94d92ac
PP
272
273 union {
274 struct {
2b55df78 275 bt_component_class_source_get_supported_mip_versions_method get_supported_mip_versions;
21a9f056 276 bt_component_class_source_initialize_method init;
0d72b8c3
PP
277 bt_component_class_source_finalize_method finalize;
278 bt_component_class_source_query_method query;
0d72b8c3 279 bt_component_class_source_output_port_connected_method output_port_connected;
a3f0c7db
SM
280 bt_message_iterator_class_initialize_method msg_iter_initialize;
281 bt_message_iterator_class_finalize_method msg_iter_finalize;
282 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin;
283 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning;
284 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin;
285 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning;
d94d92ac
PP
286 } source;
287
288 struct {
2b55df78 289 bt_component_class_filter_get_supported_mip_versions_method get_supported_mip_versions;
21a9f056 290 bt_component_class_filter_initialize_method init;
0d72b8c3
PP
291 bt_component_class_filter_finalize_method finalize;
292 bt_component_class_filter_query_method query;
0d72b8c3
PP
293 bt_component_class_filter_input_port_connected_method input_port_connected;
294 bt_component_class_filter_output_port_connected_method output_port_connected;
a3f0c7db
SM
295 bt_message_iterator_class_initialize_method msg_iter_initialize;
296 bt_message_iterator_class_finalize_method msg_iter_finalize;
297 bt_message_iterator_class_seek_ns_from_origin_method msg_iter_seek_ns_from_origin;
298 bt_message_iterator_class_seek_beginning_method msg_iter_seek_beginning;
299 bt_message_iterator_class_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin;
300 bt_message_iterator_class_can_seek_beginning_method msg_iter_can_seek_beginning;
d94d92ac
PP
301 } filter;
302
303 struct {
2b55df78 304 bt_component_class_sink_get_supported_mip_versions_method get_supported_mip_versions;
21a9f056 305 bt_component_class_sink_initialize_method init;
0d72b8c3
PP
306 bt_component_class_sink_finalize_method finalize;
307 bt_component_class_sink_query_method query;
0d72b8c3 308 bt_component_class_sink_input_port_connected_method input_port_connected;
5badd463 309 bt_component_class_sink_graph_is_configured_method graph_is_configured;
d94d92ac
PP
310 } sink;
311 } methods;
55bb57e0
PP
312 };
313
d24d5663 314 int status = BT_FUNC_STATUS_OK;
55bb57e0
PP
315 struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr;
316 struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr;
317 struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr;
318 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
319 GArray *comp_class_full_descriptors;
320 size_t i;
321 int ret;
a3f0c7db 322 struct bt_message_iterator_class *msg_iter_class = NULL;
55bb57e0 323
3f7d4d90 324 BT_LOGI("Initializing plugin object from descriptors found in sections: "
3fe0bf43
PP
325 "plugin-addr=%p, plugin-path=\"%s\", "
326 "attrs-begin-addr=%p, attrs-end-addr=%p, "
327 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
328 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
329 plugin,
330 spec->shared_lib_handle->path ?
331 spec->shared_lib_handle->path->str : NULL,
332 attrs_begin, attrs_end,
333 cc_descriptors_begin, cc_descriptors_end,
334 cc_descr_attrs_begin, cc_descr_attrs_end);
55bb57e0
PP
335 comp_class_full_descriptors = g_array_new(FALSE, TRUE,
336 sizeof(struct comp_class_full_descriptor));
337 if (!comp_class_full_descriptors) {
870631a2 338 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
d24d5663 339 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
340 goto end;
341 }
342
343 /* Set mandatory attributes */
344 spec->descriptor = descriptor;
345 bt_plugin_set_name(plugin, descriptor->name);
346
347 /*
348 * Find and set optional attributes attached to this plugin
349 * descriptor.
350 */
351 for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) {
352 const struct __bt_plugin_descriptor_attribute *cur_attr =
353 *cur_attr_ptr;
354
5084732e 355 if (!cur_attr) {
52238017
MJ
356 continue;
357 }
358
55bb57e0
PP
359 if (cur_attr->plugin_descriptor != descriptor) {
360 continue;
361 }
362
363 switch (cur_attr->type) {
364 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT:
365 spec->init = cur_attr->value.init;
366 break;
367 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT:
368 spec->shared_lib_handle->exit = cur_attr->value.exit;
369 break;
370 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR:
371 bt_plugin_set_author(plugin, cur_attr->value.author);
372 break;
373 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE:
374 bt_plugin_set_license(plugin, cur_attr->value.license);
375 break;
376 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
377 bt_plugin_set_description(plugin, cur_attr->value.description);
378 break;
379 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION:
380 bt_plugin_set_version(plugin,
381 (unsigned int) cur_attr->value.version.major,
382 (unsigned int) cur_attr->value.version.minor,
383 (unsigned int) cur_attr->value.version.patch,
384 cur_attr->value.version.extra);
385 break;
386 default:
9736d991 387 if (fail_on_load_error) {
870631a2
PP
388 BT_LIB_LOGW_APPEND_CAUSE(
389 "Unknown plugin descriptor attribute: "
390 "plugin-path=\"%s\", plugin-name=\"%s\", "
391 "attr-type-name=\"%s\", attr-type-id=%d",
392 spec->shared_lib_handle->path ?
393 spec->shared_lib_handle->path->str :
394 NULL,
395 descriptor->name, cur_attr->type_name,
396 cur_attr->type);
b189a968 397 status = BT_FUNC_STATUS_ERROR;
9736d991 398 goto end;
870631a2 399 } else {
a0cdfce8 400 BT_LIB_LOGW(
870631a2
PP
401 "Ignoring unknown plugin descriptor attribute: "
402 "plugin-path=\"%s\", plugin-name=\"%s\", "
403 "attr-type-name=\"%s\", attr-type-id=%d",
404 spec->shared_lib_handle->path ?
405 spec->shared_lib_handle->path->str :
406 NULL,
407 descriptor->name, cur_attr->type_name,
408 cur_attr->type);
9736d991
PP
409 }
410
55bb57e0
PP
411 break;
412 }
413 }
414
415 /*
416 * Find component class descriptors attached to this plugin
417 * descriptor and initialize corresponding full component class
418 * descriptors in the array.
419 */
420 for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) {
421 const struct __bt_plugin_component_class_descriptor *cur_cc_descr =
422 *cur_cc_descr_ptr;
423 struct comp_class_full_descriptor full_descriptor = {0};
424
5084732e 425 if (!cur_cc_descr) {
52238017
MJ
426 continue;
427 }
428
55bb57e0
PP
429 if (cur_cc_descr->plugin_descriptor != descriptor) {
430 continue;
431 }
432
433 full_descriptor.descriptor = cur_cc_descr;
434 g_array_append_val(comp_class_full_descriptors,
435 full_descriptor);
436 }
437
438 /*
439 * Find component class descriptor attributes attached to this
440 * plugin descriptor and update corresponding full component
441 * class descriptors in the array.
442 */
443 for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) {
444 const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr =
445 *cur_cc_descr_attr_ptr;
d94d92ac 446 enum bt_component_class_type cc_type;
55bb57e0 447
5084732e 448 if (!cur_cc_descr_attr) {
52238017
MJ
449 continue;
450 }
451
55bb57e0
PP
452 if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor !=
453 descriptor) {
454 continue;
455 }
456
d94d92ac
PP
457 cc_type = cur_cc_descr_attr->comp_class_descriptor->type;
458
55bb57e0
PP
459 /* Find the corresponding component class descriptor entry */
460 for (i = 0; i < comp_class_full_descriptors->len; i++) {
461 struct comp_class_full_descriptor *cc_full_descr =
d50d46f3 462 &bt_g_array_index(comp_class_full_descriptors,
55bb57e0
PP
463 struct comp_class_full_descriptor, i);
464
d94d92ac 465 if (cur_cc_descr_attr->comp_class_descriptor !=
55bb57e0 466 cc_full_descr->descriptor) {
d94d92ac
PP
467 continue;
468 }
469
470 switch (cur_cc_descr_attr->type) {
471 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
472 cc_full_descr->description =
473 cur_cc_descr_attr->value.description;
474 break;
475 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP:
476 cc_full_descr->help =
477 cur_cc_descr_attr->value.help;
478 break;
2b55df78
PP
479 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GET_SUPPORTED_MIP_VERSIONS_METHOD:
480 switch (cc_type) {
481 case BT_COMPONENT_CLASS_TYPE_SOURCE:
482 cc_full_descr->methods.source.get_supported_mip_versions =
483 cur_cc_descr_attr->value.source_get_supported_mip_versions_method;
484 break;
485 case BT_COMPONENT_CLASS_TYPE_FILTER:
486 cc_full_descr->methods.filter.get_supported_mip_versions =
487 cur_cc_descr_attr->value.filter_get_supported_mip_versions_method;
488 break;
489 case BT_COMPONENT_CLASS_TYPE_SINK:
490 cc_full_descr->methods.sink.get_supported_mip_versions =
491 cur_cc_descr_attr->value.sink_get_supported_mip_versions_method;
492 break;
493 default:
498e7994 494 bt_common_abort();
2b55df78
PP
495 }
496 break;
21a9f056 497 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INITIALIZE_METHOD:
d94d92ac
PP
498 switch (cc_type) {
499 case BT_COMPONENT_CLASS_TYPE_SOURCE:
500 cc_full_descr->methods.source.init =
21a9f056 501 cur_cc_descr_attr->value.source_initialize_method;
55bb57e0 502 break;
d94d92ac
PP
503 case BT_COMPONENT_CLASS_TYPE_FILTER:
504 cc_full_descr->methods.filter.init =
21a9f056 505 cur_cc_descr_attr->value.filter_initialize_method;
279b3f15 506 break;
d94d92ac
PP
507 case BT_COMPONENT_CLASS_TYPE_SINK:
508 cc_full_descr->methods.sink.init =
21a9f056 509 cur_cc_descr_attr->value.sink_initialize_method;
55bb57e0 510 break;
d94d92ac 511 default:
498e7994 512 bt_common_abort();
d94d92ac
PP
513 }
514 break;
515 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD:
516 switch (cc_type) {
517 case BT_COMPONENT_CLASS_TYPE_SOURCE:
518 cc_full_descr->methods.source.finalize =
519 cur_cc_descr_attr->value.source_finalize_method;
55bb57e0 520 break;
d94d92ac
PP
521 case BT_COMPONENT_CLASS_TYPE_FILTER:
522 cc_full_descr->methods.filter.finalize =
523 cur_cc_descr_attr->value.filter_finalize_method;
8463eac2 524 break;
d94d92ac
PP
525 case BT_COMPONENT_CLASS_TYPE_SINK:
526 cc_full_descr->methods.sink.finalize =
527 cur_cc_descr_attr->value.sink_finalize_method;
72b913fb 528 break;
d94d92ac 529 default:
498e7994 530 bt_common_abort();
d94d92ac
PP
531 }
532 break;
533 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD:
534 switch (cc_type) {
535 case BT_COMPONENT_CLASS_TYPE_SOURCE:
536 cc_full_descr->methods.source.query =
537 cur_cc_descr_attr->value.source_query_method;
0d8b4d8e 538 break;
d94d92ac
PP
539 case BT_COMPONENT_CLASS_TYPE_FILTER:
540 cc_full_descr->methods.filter.query =
541 cur_cc_descr_attr->value.filter_query_method;
55bb57e0 542 break;
d94d92ac
PP
543 case BT_COMPONENT_CLASS_TYPE_SINK:
544 cc_full_descr->methods.sink.query =
545 cur_cc_descr_attr->value.sink_query_method;
55bb57e0 546 break;
d94d92ac 547 default:
498e7994 548 bt_common_abort();
d94d92ac
PP
549 }
550 break;
d94d92ac
PP
551 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD:
552 switch (cc_type) {
553 case BT_COMPONENT_CLASS_TYPE_FILTER:
554 cc_full_descr->methods.filter.input_port_connected =
555 cur_cc_descr_attr->value.filter_input_port_connected_method;
556 break;
557 case BT_COMPONENT_CLASS_TYPE_SINK:
558 cc_full_descr->methods.sink.input_port_connected =
559 cur_cc_descr_attr->value.sink_input_port_connected_method;
55bb57e0 560 break;
55bb57e0 561 default:
498e7994 562 bt_common_abort();
d94d92ac
PP
563 }
564 break;
565 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD:
566 switch (cc_type) {
567 case BT_COMPONENT_CLASS_TYPE_SOURCE:
568 cc_full_descr->methods.source.output_port_connected =
569 cur_cc_descr_attr->value.source_output_port_connected_method;
570 break;
571 case BT_COMPONENT_CLASS_TYPE_FILTER:
572 cc_full_descr->methods.filter.output_port_connected =
573 cur_cc_descr_attr->value.filter_output_port_connected_method;
55bb57e0 574 break;
d94d92ac 575 default:
498e7994 576 bt_common_abort();
55bb57e0 577 }
d94d92ac 578 break;
5badd463
PP
579 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD:
580 switch (cc_type) {
581 case BT_COMPONENT_CLASS_TYPE_SINK:
582 cc_full_descr->methods.sink.graph_is_configured =
583 cur_cc_descr_attr->value.sink_graph_is_configured_method;
584 break;
585 default:
498e7994 586 bt_common_abort();
5badd463
PP
587 }
588 break;
21a9f056 589 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INITIALIZE_METHOD:
d94d92ac
PP
590 switch (cc_type) {
591 case BT_COMPONENT_CLASS_TYPE_SOURCE:
21a9f056 592 cc_full_descr->methods.source.msg_iter_initialize =
a3f0c7db 593 cur_cc_descr_attr->value.msg_iter_initialize_method;
d94d92ac
PP
594 break;
595 case BT_COMPONENT_CLASS_TYPE_FILTER:
21a9f056 596 cc_full_descr->methods.filter.msg_iter_initialize =
a3f0c7db 597 cur_cc_descr_attr->value.msg_iter_initialize_method;
d94d92ac
PP
598 break;
599 default:
498e7994 600 bt_common_abort();
d94d92ac
PP
601 }
602 break;
d6e69534 603 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD:
d94d92ac
PP
604 switch (cc_type) {
605 case BT_COMPONENT_CLASS_TYPE_SOURCE:
d6e69534 606 cc_full_descr->methods.source.msg_iter_finalize =
a3f0c7db 607 cur_cc_descr_attr->value.msg_iter_finalize_method;
d94d92ac
PP
608 break;
609 case BT_COMPONENT_CLASS_TYPE_FILTER:
d6e69534 610 cc_full_descr->methods.filter.msg_iter_finalize =
a3f0c7db 611 cur_cc_descr_attr->value.msg_iter_finalize_method;
d94d92ac
PP
612 break;
613 default:
498e7994 614 bt_common_abort();
d94d92ac
PP
615 }
616 break;
7474e7d3
PP
617 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD:
618 switch (cc_type) {
619 case BT_COMPONENT_CLASS_TYPE_SOURCE:
620 cc_full_descr->methods.source.msg_iter_seek_ns_from_origin =
a3f0c7db 621 cur_cc_descr_attr->value.msg_iter_seek_ns_from_origin_method;
7474e7d3
PP
622 break;
623 case BT_COMPONENT_CLASS_TYPE_FILTER:
624 cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin =
a3f0c7db 625 cur_cc_descr_attr->value.msg_iter_seek_ns_from_origin_method;
7474e7d3
PP
626 break;
627 default:
498e7994 628 bt_common_abort();
7474e7d3
PP
629 }
630 break;
631 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD:
632 switch (cc_type) {
633 case BT_COMPONENT_CLASS_TYPE_SOURCE:
634 cc_full_descr->methods.source.msg_iter_seek_beginning =
a3f0c7db 635 cur_cc_descr_attr->value.msg_iter_seek_beginning_method;
7474e7d3
PP
636 break;
637 case BT_COMPONENT_CLASS_TYPE_FILTER:
638 cc_full_descr->methods.filter.msg_iter_seek_beginning =
a3f0c7db 639 cur_cc_descr_attr->value.msg_iter_seek_beginning_method;
7474e7d3
PP
640 break;
641 default:
498e7994 642 bt_common_abort();
7474e7d3
PP
643 }
644 break;
645 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD:
646 switch (cc_type) {
647 case BT_COMPONENT_CLASS_TYPE_SOURCE:
648 cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin =
a3f0c7db 649 cur_cc_descr_attr->value.msg_iter_can_seek_ns_from_origin_method;
7474e7d3
PP
650 break;
651 case BT_COMPONENT_CLASS_TYPE_FILTER:
652 cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin =
a3f0c7db 653 cur_cc_descr_attr->value.msg_iter_can_seek_ns_from_origin_method;
7474e7d3
PP
654 break;
655 default:
498e7994 656 bt_common_abort();
7474e7d3
PP
657 }
658 break;
659 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD:
660 switch (cc_type) {
661 case BT_COMPONENT_CLASS_TYPE_SOURCE:
662 cc_full_descr->methods.source.msg_iter_can_seek_beginning =
a3f0c7db 663 cur_cc_descr_attr->value.msg_iter_can_seek_beginning_method;
7474e7d3
PP
664 break;
665 case BT_COMPONENT_CLASS_TYPE_FILTER:
666 cc_full_descr->methods.filter.msg_iter_can_seek_beginning =
a3f0c7db 667 cur_cc_descr_attr->value.msg_iter_can_seek_beginning_method;
7474e7d3
PP
668 break;
669 default:
498e7994 670 bt_common_abort();
7474e7d3
PP
671 }
672 break;
d94d92ac 673 default:
9736d991 674 if (fail_on_load_error) {
870631a2
PP
675 BT_LIB_LOGW_APPEND_CAUSE(
676 "Unknown component class descriptor attribute: "
677 "plugin-path=\"%s\", "
678 "plugin-name=\"%s\", "
679 "comp-class-name=\"%s\", "
680 "comp-class-type=%s, "
681 "attr-type-name=\"%s\", "
682 "attr-type-id=%d",
683 spec->shared_lib_handle->path ?
684 spec->shared_lib_handle->path->str :
685 NULL,
686 descriptor->name,
687 cur_cc_descr_attr->comp_class_descriptor->name,
6375b942 688 bt_common_component_class_type_string(
870631a2
PP
689 cur_cc_descr_attr->comp_class_descriptor->type),
690 cur_cc_descr_attr->type_name,
691 cur_cc_descr_attr->type);
b189a968 692 status = BT_FUNC_STATUS_ERROR;
9736d991 693 goto end;
870631a2 694 } else {
a0cdfce8 695 BT_LIB_LOGW(
870631a2
PP
696 "Ignoring unknown component class descriptor attribute: "
697 "plugin-path=\"%s\", "
698 "plugin-name=\"%s\", "
699 "comp-class-name=\"%s\", "
700 "comp-class-type=%s, "
701 "attr-type-name=\"%s\", "
702 "attr-type-id=%d",
703 spec->shared_lib_handle->path ?
704 spec->shared_lib_handle->path->str :
705 NULL,
706 descriptor->name,
707 cur_cc_descr_attr->comp_class_descriptor->name,
6375b942 708 bt_common_component_class_type_string(
870631a2
PP
709 cur_cc_descr_attr->comp_class_descriptor->type),
710 cur_cc_descr_attr->type_name,
711 cur_cc_descr_attr->type);
9736d991
PP
712 }
713
d94d92ac 714 break;
55bb57e0
PP
715 }
716 }
717 }
718
719 /* Initialize plugin */
720 if (spec->init) {
21a9f056 721 enum bt_plugin_initialize_func_status init_status;
a21d1cb8 722
3fe0bf43 723 BT_LOGD_STR("Calling user's plugin initialization function.");
a21d1cb8 724 init_status = spec->init((void *) plugin);
9736d991 725 BT_LOGD("User function returned: status=%s",
d24d5663 726 bt_common_func_status_string(init_status));
3fe0bf43 727
9724cce9 728 if (init_status < 0) {
9736d991 729 if (fail_on_load_error) {
870631a2
PP
730 BT_LIB_LOGW_APPEND_CAUSE(
731 "User's plugin initialization function failed: "
732 "status=%s",
733 bt_common_func_status_string(init_status));
d24d5663 734 status = init_status;
9736d991 735 goto end;
870631a2 736 } else {
1a9956b9
SM
737 /*
738 * Since we don't return an error,
739 * there's no way to communicate this
740 * error to the caller.
741 */
742 bt_current_thread_clear_error();
a0cdfce8 743 BT_LIB_LOGW(
870631a2
PP
744 "User's plugin initialization function failed: "
745 "status=%s",
746 bt_common_func_status_string(init_status));
747 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991 748 }
870631a2
PP
749
750 goto end;
55bb57e0
PP
751 }
752 }
753
c55a9f58 754 spec->shared_lib_handle->init_called = BT_TRUE;
55bb57e0
PP
755
756 /* Add described component classes to plugin */
757 for (i = 0; i < comp_class_full_descriptors->len; i++) {
758 struct comp_class_full_descriptor *cc_full_descr =
d50d46f3 759 &bt_g_array_index(comp_class_full_descriptors,
55bb57e0 760 struct comp_class_full_descriptor, i);
0d72b8c3
PP
761 struct bt_component_class *comp_class = NULL;
762 struct bt_component_class_source *src_comp_class = NULL;
763 struct bt_component_class_filter *flt_comp_class = NULL;
764 struct bt_component_class_sink *sink_comp_class = NULL;
55bb57e0 765
3f7d4d90 766 BT_LOGI("Creating and setting properties of plugin's component class: "
3fe0bf43
PP
767 "plugin-path=\"%s\", plugin-name=\"%s\", "
768 "comp-class-name=\"%s\", comp-class-type=%s",
769 spec->shared_lib_handle->path ?
770 spec->shared_lib_handle->path->str :
771 NULL,
772 descriptor->name,
773 cc_full_descr->descriptor->name,
6375b942 774 bt_common_component_class_type_string(
3fe0bf43
PP
775 cc_full_descr->descriptor->type));
776
a3f0c7db
SM
777 if (cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
778 cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_FILTER) {
779 bt_message_iterator_class_next_method next_method;
780 bt_message_iterator_class_initialize_method init_method;
781 bt_message_iterator_class_finalize_method fini_method;
782 bt_message_iterator_class_seek_ns_from_origin_method seek_ns_from_origin_method;
783 bt_message_iterator_class_seek_beginning_method seek_beginning_method;
784 bt_message_iterator_class_can_seek_ns_from_origin_method can_seek_ns_from_origin_method;
785 bt_message_iterator_class_can_seek_beginning_method can_seek_beginning_method;
786
787 if (cc_full_descr->descriptor->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
788 next_method = cc_full_descr->descriptor->methods.source.msg_iter_next;
789 init_method = cc_full_descr->methods.source.msg_iter_initialize;
790 fini_method = cc_full_descr->methods.source.msg_iter_finalize;
791 seek_ns_from_origin_method = cc_full_descr->methods.source.msg_iter_seek_ns_from_origin;
792 can_seek_ns_from_origin_method = cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin;
793 seek_beginning_method = cc_full_descr->methods.source.msg_iter_seek_beginning;
794 can_seek_beginning_method = cc_full_descr->methods.source.msg_iter_can_seek_beginning;
795 } else {
796 next_method = cc_full_descr->descriptor->methods.filter.msg_iter_next;
797 init_method = cc_full_descr->methods.filter.msg_iter_initialize;
798 fini_method = cc_full_descr->methods.filter.msg_iter_finalize;
799 seek_ns_from_origin_method = cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin;
800 can_seek_ns_from_origin_method = cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin;
801 seek_beginning_method = cc_full_descr->methods.filter.msg_iter_seek_beginning;
802 can_seek_beginning_method = cc_full_descr->methods.filter.msg_iter_can_seek_beginning;
803 }
804
805 msg_iter_class = bt_message_iterator_class_create(next_method);
806 if (!msg_iter_class) {
807 BT_LIB_LOGE_APPEND_CAUSE(
808 "Cannot create message iterator class.");
809 status = BT_FUNC_STATUS_MEMORY_ERROR;
810 goto end;
811 }
812
813 if (init_method) {
814 ret = bt_message_iterator_class_set_initialize_method(
815 msg_iter_class, init_method);
816 if (ret) {
817 BT_LIB_LOGE_APPEND_CAUSE(
818 "Cannot set message iterator initialization method.");
819 status = BT_FUNC_STATUS_MEMORY_ERROR;
820 goto end;
821 }
822 }
823
824 if (fini_method) {
825 ret = bt_message_iterator_class_set_finalize_method(
826 msg_iter_class, fini_method);
827 if (ret) {
828 BT_LIB_LOGE_APPEND_CAUSE(
829 "Cannot set message iterator finalization method.");
830 status = BT_FUNC_STATUS_MEMORY_ERROR;
831 goto end;
832 }
833 }
834
835 if (seek_ns_from_origin_method) {
836 ret = bt_message_iterator_class_set_seek_ns_from_origin_methods(
837 msg_iter_class,
838 seek_ns_from_origin_method,
839 can_seek_ns_from_origin_method);
840 if (ret) {
841 BT_LIB_LOGE_APPEND_CAUSE(
842 "Cannot set message iterator \"seek nanoseconds from origin\" methods.");
843 status = BT_FUNC_STATUS_MEMORY_ERROR;
844 goto end;
845 }
846 }
847
848 if (seek_beginning_method) {
849 ret = bt_message_iterator_class_set_seek_beginning_methods(
850 msg_iter_class,
851 seek_beginning_method,
852 can_seek_beginning_method);
853 if (ret) {
854 BT_LIB_LOGE_APPEND_CAUSE(
855 "Cannot set message iterator \"seek beginning\" methods.");
856 status = BT_FUNC_STATUS_MEMORY_ERROR;
857 goto end;
858 }
859 }
860 }
861
55bb57e0
PP
862 switch (cc_full_descr->descriptor->type) {
863 case BT_COMPONENT_CLASS_TYPE_SOURCE:
a3f0c7db
SM
864 BT_ASSERT(msg_iter_class);
865
0d72b8c3 866 src_comp_class = bt_component_class_source_create(
a3f0c7db 867 cc_full_descr->descriptor->name, msg_iter_class);
0d72b8c3 868 comp_class = bt_component_class_source_as_component_class(
d94d92ac 869 src_comp_class);
55bb57e0
PP
870 break;
871 case BT_COMPONENT_CLASS_TYPE_FILTER:
a3f0c7db
SM
872 BT_ASSERT(msg_iter_class);
873
0d72b8c3 874 flt_comp_class = bt_component_class_filter_create(
a3f0c7db 875 cc_full_descr->descriptor->name, msg_iter_class);
0d72b8c3 876 comp_class = bt_component_class_filter_as_component_class(
d94d92ac 877 flt_comp_class);
55bb57e0
PP
878 break;
879 case BT_COMPONENT_CLASS_TYPE_SINK:
a3f0c7db
SM
880 BT_ASSERT(!msg_iter_class);
881
0d72b8c3 882 sink_comp_class = bt_component_class_sink_create(
55bb57e0
PP
883 cc_full_descr->descriptor->name,
884 cc_full_descr->descriptor->methods.sink.consume);
0d72b8c3 885 comp_class = bt_component_class_sink_as_component_class(
d94d92ac 886 sink_comp_class);
55bb57e0
PP
887 break;
888 default:
9736d991 889 if (fail_on_load_error) {
870631a2
PP
890 BT_LIB_LOGW_APPEND_CAUSE(
891 "Unknown component class type: "
892 "plugin-path=\"%s\", plugin-name=\"%s\", "
893 "comp-class-name=\"%s\", comp-class-type=%d",
894 spec->shared_lib_handle->path->str ?
895 spec->shared_lib_handle->path->str :
896 NULL,
897 descriptor->name,
898 cc_full_descr->descriptor->name,
899 cc_full_descr->descriptor->type);
b189a968 900 status = BT_FUNC_STATUS_ERROR;
9736d991 901 goto end;
870631a2 902 } else {
a0cdfce8 903 BT_LIB_LOGW(
870631a2
PP
904 "Ignoring unknown component class type: "
905 "plugin-path=\"%s\", plugin-name=\"%s\", "
906 "comp-class-name=\"%s\", comp-class-type=%d",
907 spec->shared_lib_handle->path->str ?
908 spec->shared_lib_handle->path->str :
909 NULL,
910 descriptor->name,
911 cc_full_descr->descriptor->name,
912 cc_full_descr->descriptor->type);
913 continue;
9736d991 914 }
55bb57e0
PP
915 }
916
917 if (!comp_class) {
870631a2
PP
918 BT_LIB_LOGE_APPEND_CAUSE(
919 "Cannot create component class.");
d24d5663 920 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
921 goto end;
922 }
923
a3f0c7db
SM
924 /*
925 * The component class has taken a reference on the message
926 * iterator class, so we can drop ours. The message iterator
927 * class will get destroyed at the same time as the component
928 * class.
929 */
930 bt_message_iterator_class_put_ref(msg_iter_class);
931 msg_iter_class = NULL;
932
55bb57e0 933 if (cc_full_descr->description) {
0d72b8c3 934 ret = bt_component_class_set_description(
d94d92ac 935 comp_class, cc_full_descr->description);
55bb57e0 936 if (ret) {
870631a2
PP
937 BT_LIB_LOGE_APPEND_CAUSE(
938 "Cannot set component class's description.");
d24d5663 939 status = BT_FUNC_STATUS_MEMORY_ERROR;
65300d60 940 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
55bb57e0
PP
941 goto end;
942 }
943 }
944
279b3f15 945 if (cc_full_descr->help) {
0d72b8c3 946 ret = bt_component_class_set_help(comp_class,
279b3f15
PP
947 cc_full_descr->help);
948 if (ret) {
870631a2
PP
949 BT_LIB_LOGE_APPEND_CAUSE(
950 "Cannot set component class's help string.");
d24d5663 951 status = BT_FUNC_STATUS_MEMORY_ERROR;
65300d60 952 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
279b3f15
PP
953 goto end;
954 }
955 }
956
d94d92ac
PP
957 switch (cc_full_descr->descriptor->type) {
958 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2b55df78
PP
959 if (cc_full_descr->methods.source.get_supported_mip_versions) {
960 ret = bt_component_class_source_set_get_supported_mip_versions_method(
961 src_comp_class,
962 cc_full_descr->methods.source.get_supported_mip_versions);
963 if (ret) {
964 BT_LIB_LOGE_APPEND_CAUSE(
965 "Cannot set source component class's \"get supported MIP versions\" method.");
966 status = BT_FUNC_STATUS_MEMORY_ERROR;
967 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
968 goto end;
969 }
970 }
971
d94d92ac 972 if (cc_full_descr->methods.source.init) {
21a9f056 973 ret = bt_component_class_source_set_initialize_method(
d94d92ac
PP
974 src_comp_class,
975 cc_full_descr->methods.source.init);
976 if (ret) {
870631a2
PP
977 BT_LIB_LOGE_APPEND_CAUSE(
978 "Cannot set source component class's initialization method.");
d24d5663 979 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
980 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
981 goto end;
982 }
55bb57e0 983 }
55bb57e0 984
d94d92ac 985 if (cc_full_descr->methods.source.finalize) {
0d72b8c3 986 ret = bt_component_class_source_set_finalize_method(
d94d92ac
PP
987 src_comp_class,
988 cc_full_descr->methods.source.finalize);
989 if (ret) {
870631a2
PP
990 BT_LIB_LOGE_APPEND_CAUSE(
991 "Cannot set source component class's finalization method.");
d24d5663 992 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
993 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
994 goto end;
995 }
55bb57e0 996 }
55bb57e0 997
d94d92ac 998 if (cc_full_descr->methods.source.query) {
0d72b8c3 999 ret = bt_component_class_source_set_query_method(
d94d92ac
PP
1000 src_comp_class,
1001 cc_full_descr->methods.source.query);
1002 if (ret) {
870631a2
PP
1003 BT_LIB_LOGE_APPEND_CAUSE(
1004 "Cannot set source component class's query method.");
d24d5663 1005 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1006 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
1007 goto end;
1008 }
8463eac2 1009 }
8463eac2 1010
d94d92ac 1011 if (cc_full_descr->methods.source.output_port_connected) {
0d72b8c3 1012 ret = bt_component_class_source_set_output_port_connected_method(
d94d92ac
PP
1013 src_comp_class,
1014 cc_full_descr->methods.source.output_port_connected);
1015 if (ret) {
870631a2
PP
1016 BT_LIB_LOGE_APPEND_CAUSE(
1017 "Cannot set source component class's \"output port connected\" method.");
d24d5663 1018 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1019 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
1020 goto end;
1021 }
0d8b4d8e 1022 }
0d8b4d8e 1023
55bb57e0
PP
1024 break;
1025 case BT_COMPONENT_CLASS_TYPE_FILTER:
2b55df78
PP
1026 if (cc_full_descr->methods.filter.get_supported_mip_versions) {
1027 ret = bt_component_class_filter_set_get_supported_mip_versions_method(
1028 flt_comp_class,
1029 cc_full_descr->methods.filter.get_supported_mip_versions);
1030 if (ret) {
1031 BT_LIB_LOGE_APPEND_CAUSE(
1032 "Cannot set filter component class's \"get supported MIP versions\" method.");
1033 status = BT_FUNC_STATUS_MEMORY_ERROR;
1034 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1035 goto end;
1036 }
1037 }
1038
d94d92ac 1039 if (cc_full_descr->methods.filter.init) {
21a9f056 1040 ret = bt_component_class_filter_set_initialize_method(
d94d92ac
PP
1041 flt_comp_class,
1042 cc_full_descr->methods.filter.init);
1043 if (ret) {
870631a2
PP
1044 BT_LIB_LOGE_APPEND_CAUSE(
1045 "Cannot set filter component class's initialization method.");
d24d5663 1046 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1047 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1048 goto end;
1049 }
1050 }
1051
1052 if (cc_full_descr->methods.filter.finalize) {
0d72b8c3 1053 ret = bt_component_class_filter_set_finalize_method(
d94d92ac
PP
1054 flt_comp_class,
1055 cc_full_descr->methods.filter.finalize);
1056 if (ret) {
870631a2
PP
1057 BT_LIB_LOGE_APPEND_CAUSE(
1058 "Cannot set filter component class's finalization method.");
d24d5663 1059 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1060 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1061 goto end;
1062 }
1063 }
1064
1065 if (cc_full_descr->methods.filter.query) {
0d72b8c3 1066 ret = bt_component_class_filter_set_query_method(
d94d92ac
PP
1067 flt_comp_class,
1068 cc_full_descr->methods.filter.query);
1069 if (ret) {
870631a2
PP
1070 BT_LIB_LOGE_APPEND_CAUSE(
1071 "Cannot set filter component class's query method.");
d24d5663 1072 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1073 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1074 goto end;
1075 }
1076 }
1077
d94d92ac 1078 if (cc_full_descr->methods.filter.input_port_connected) {
0d72b8c3 1079 ret = bt_component_class_filter_set_input_port_connected_method(
d94d92ac
PP
1080 flt_comp_class,
1081 cc_full_descr->methods.filter.input_port_connected);
1082 if (ret) {
870631a2
PP
1083 BT_LIB_LOGE_APPEND_CAUSE(
1084 "Cannot set filter component class's \"input port connected\" method.");
d24d5663 1085 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1086 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1087 goto end;
1088 }
1089 }
1090
1091 if (cc_full_descr->methods.filter.output_port_connected) {
0d72b8c3 1092 ret = bt_component_class_filter_set_output_port_connected_method(
d94d92ac
PP
1093 flt_comp_class,
1094 cc_full_descr->methods.filter.output_port_connected);
1095 if (ret) {
870631a2
PP
1096 BT_LIB_LOGE_APPEND_CAUSE(
1097 "Cannot set filter component class's \"output port connected\" method.");
d24d5663 1098 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1099 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1100 goto end;
1101 }
1102 }
1103
55bb57e0
PP
1104 break;
1105 case BT_COMPONENT_CLASS_TYPE_SINK:
2b55df78
PP
1106 if (cc_full_descr->methods.sink.get_supported_mip_versions) {
1107 ret = bt_component_class_sink_set_get_supported_mip_versions_method(
1108 sink_comp_class,
1109 cc_full_descr->methods.sink.get_supported_mip_versions);
1110 if (ret) {
1111 BT_LIB_LOGE_APPEND_CAUSE(
1112 "Cannot set sink component class's \"get supported MIP versions\" method.");
1113 status = BT_FUNC_STATUS_MEMORY_ERROR;
1114 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1115 goto end;
1116 }
1117 }
1118
d94d92ac 1119 if (cc_full_descr->methods.sink.init) {
21a9f056 1120 ret = bt_component_class_sink_set_initialize_method(
d94d92ac
PP
1121 sink_comp_class,
1122 cc_full_descr->methods.sink.init);
1123 if (ret) {
870631a2
PP
1124 BT_LIB_LOGE_APPEND_CAUSE(
1125 "Cannot set sink component class's initialization method.");
d24d5663 1126 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1127 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1128 goto end;
1129 }
1130 }
1131
1132 if (cc_full_descr->methods.sink.finalize) {
0d72b8c3 1133 ret = bt_component_class_sink_set_finalize_method(
d94d92ac
PP
1134 sink_comp_class,
1135 cc_full_descr->methods.sink.finalize);
1136 if (ret) {
870631a2
PP
1137 BT_LIB_LOGE_APPEND_CAUSE(
1138 "Cannot set sink component class's finalization method.");
d24d5663 1139 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1140 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1141 goto end;
1142 }
1143 }
1144
1145 if (cc_full_descr->methods.sink.query) {
0d72b8c3 1146 ret = bt_component_class_sink_set_query_method(
d94d92ac
PP
1147 sink_comp_class,
1148 cc_full_descr->methods.sink.query);
1149 if (ret) {
870631a2
PP
1150 BT_LIB_LOGE_APPEND_CAUSE(
1151 "Cannot set sink component class's query method.");
d24d5663 1152 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1153 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1154 goto end;
1155 }
1156 }
1157
d94d92ac 1158 if (cc_full_descr->methods.sink.input_port_connected) {
0d72b8c3 1159 ret = bt_component_class_sink_set_input_port_connected_method(
d94d92ac
PP
1160 sink_comp_class,
1161 cc_full_descr->methods.sink.input_port_connected);
1162 if (ret) {
870631a2
PP
1163 BT_LIB_LOGE_APPEND_CAUSE(
1164 "Cannot set sink component class's \"input port connected\" method.");
d24d5663 1165 status = BT_FUNC_STATUS_MEMORY_ERROR;
d94d92ac
PP
1166 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1167 goto end;
1168 }
1169 }
1170
5badd463
PP
1171 if (cc_full_descr->methods.sink.graph_is_configured) {
1172 ret = bt_component_class_sink_set_graph_is_configured_method(
1173 sink_comp_class,
1174 cc_full_descr->methods.sink.graph_is_configured);
1175 if (ret) {
870631a2
PP
1176 BT_LIB_LOGE_APPEND_CAUSE(
1177 "Cannot set sink component class's \"graph is configured\" method.");
d24d5663 1178 status = BT_FUNC_STATUS_MEMORY_ERROR;
5badd463
PP
1179 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1180 goto end;
1181 }
1182 }
1183
55bb57e0
PP
1184 break;
1185 default:
498e7994 1186 bt_common_abort();
55bb57e0
PP
1187 }
1188
1189 /*
1190 * Add component class to the plugin object.
1191 *
1192 * This will call back
1193 * bt_plugin_so_on_add_component_class() so that we can
d94d92ac
PP
1194 * add a mapping in the component class list when we
1195 * know the component class is successfully added.
55bb57e0
PP
1196 */
1197 status = bt_plugin_add_component_class(plugin,
d94d92ac 1198 (void *) comp_class);
65300d60 1199 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
55bb57e0 1200 if (status < 0) {
870631a2
PP
1201 BT_LIB_LOGE_APPEND_CAUSE(
1202 "Cannot add component class to plugin.");
55bb57e0
PP
1203 goto end;
1204 }
1205 }
1206
55bb57e0 1207end:
a3f0c7db 1208 bt_message_iterator_class_put_ref(msg_iter_class);
55bb57e0
PP
1209 g_array_free(comp_class_full_descriptors, TRUE);
1210 return status;
1211}
1212
1213static
1214struct bt_plugin *bt_plugin_so_create_empty(
1215 struct bt_plugin_so_shared_lib_handle *shared_lib_handle)
1216{
1217 struct bt_plugin *plugin;
1218 struct bt_plugin_so_spec_data *spec;
1219
1220 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO);
1221 if (!plugin) {
1222 goto error;
1223 }
1224
6fbd4105 1225 plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data;
55bb57e0
PP
1226 plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1);
1227 if (!plugin->spec_data) {
870631a2
PP
1228 BT_LIB_LOGE_APPEND_CAUSE(
1229 "Failed to allocate one SO plugin specific data structure.");
55bb57e0
PP
1230 goto error;
1231 }
1232
1233 spec = plugin->spec_data;
398454ed 1234 spec->shared_lib_handle = shared_lib_handle;
6871026b 1235 bt_object_get_ref_no_null_check(spec->shared_lib_handle);
55bb57e0
PP
1236 goto end;
1237
1238error:
65300d60 1239 BT_OBJECT_PUT_REF_AND_RESET(plugin);
55bb57e0
PP
1240
1241end:
1242 return plugin;
1243}
1244
52238017
MJ
1245static
1246size_t count_non_null_items_in_section(const void *begin, const void *end)
1247{
1248 size_t count = 0;
1249 const int * const *begin_int = (const int * const *) begin;
1250 const int * const *end_int = (const int * const *) end;
1251 const int * const *iter;
1252
1253 for (iter = begin_int; iter != end_int; iter++) {
1254 if (*iter) {
1255 count++;
1256 }
1257 }
1258
1259 return count;
1260}
1261
55bb57e0 1262static
d24d5663 1263int bt_plugin_so_create_all_from_sections(
55bb57e0 1264 struct bt_plugin_so_shared_lib_handle *shared_lib_handle,
9736d991 1265 bool fail_on_load_error,
55bb57e0
PP
1266 struct __bt_plugin_descriptor const * const *descriptors_begin,
1267 struct __bt_plugin_descriptor const * const *descriptors_end,
1268 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
1269 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
1270 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
1271 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
1272 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
9736d991
PP
1273 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end,
1274 struct bt_plugin_set **plugin_set_out)
55bb57e0 1275{
d24d5663 1276 int status = BT_FUNC_STATUS_OK;
55bb57e0
PP
1277 size_t descriptor_count;
1278 size_t attrs_count;
1279 size_t cc_descriptors_count;
1280 size_t cc_descr_attrs_count;
1281 size_t i;
55bb57e0 1282
829fbd63 1283 BT_ASSERT(shared_lib_handle);
9736d991
PP
1284 BT_ASSERT(plugin_set_out);
1285 *plugin_set_out = NULL;
52238017
MJ
1286 descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end);
1287 attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end);
1288 cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end);
1289 cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end);
3f7d4d90 1290 BT_LOGI("Creating all SO plugins from sections: "
3fe0bf43
PP
1291 "plugin-path=\"%s\", "
1292 "descr-begin-addr=%p, descr-end-addr=%p, "
1293 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1294 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1295 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1296 "descr-count=%zu, attrs-count=%zu, "
1297 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1298 shared_lib_handle->path ? shared_lib_handle->path->str : NULL,
1299 descriptors_begin, descriptors_end,
1300 attrs_begin, attrs_end,
1301 cc_descriptors_begin, cc_descriptors_end,
1302 cc_descr_attrs_begin, cc_descr_attrs_end,
1303 descriptor_count, attrs_count,
1304 cc_descriptors_count, cc_descr_attrs_count);
9736d991
PP
1305 *plugin_set_out = bt_plugin_set_create();
1306 if (!*plugin_set_out) {
870631a2 1307 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
d24d5663 1308 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
1309 goto error;
1310 }
1311
52238017 1312 for (i = 0; i < descriptors_end - descriptors_begin; i++) {
55bb57e0
PP
1313 const struct __bt_plugin_descriptor *descriptor =
1314 descriptors_begin[i];
1315 struct bt_plugin *plugin;
1316
5084732e 1317 if (!descriptor) {
52238017
MJ
1318 continue;
1319 }
1320
7c0244d6
PP
1321 BT_LOGI("Creating plugin object for plugin: name=\"%s\"",
1322 descriptor->name);
55bb57e0
PP
1323 plugin = bt_plugin_so_create_empty(shared_lib_handle);
1324 if (!plugin) {
870631a2
PP
1325 BT_LIB_LOGE_APPEND_CAUSE(
1326 "Cannot create empty shared library handle.");
d24d5663 1327 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
1328 goto error;
1329 }
1330
ea251e8b 1331 if (shared_lib_handle->path) {
9736d991
PP
1332 bt_plugin_set_path(plugin,
1333 shared_lib_handle->path->str);
55bb57e0
PP
1334 }
1335
9736d991
PP
1336 status = bt_plugin_so_init(plugin, fail_on_load_error,
1337 descriptor, attrs_begin, attrs_end,
1338 cc_descriptors_begin, cc_descriptors_end,
55bb57e0 1339 cc_descr_attrs_begin, cc_descr_attrs_end);
d24d5663 1340 if (status == BT_FUNC_STATUS_OK) {
9736d991
PP
1341 /* Add to plugin set */
1342 bt_plugin_set_add_plugin(*plugin_set_out, plugin);
1343 BT_OBJECT_PUT_REF_AND_RESET(plugin);
1a9956b9
SM
1344 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
1345 /*
1346 * There was an error initializing the plugin,
1347 * but `fail_on_load_error` is false.
1348 */
1349 BT_OBJECT_PUT_REF_AND_RESET(plugin);
9736d991 1350 } else if (status < 0) {
50ad9320 1351 /*
9736d991
PP
1352 * bt_plugin_so_init() handles
1353 * `fail_on_load_error`, so this is a "real"
1354 * error.
50ad9320 1355 */
870631a2
PP
1356 BT_LIB_LOGW_APPEND_CAUSE(
1357 "Cannot initialize SO plugin object from sections.");
65300d60 1358 BT_OBJECT_PUT_REF_AND_RESET(plugin);
55bb57e0
PP
1359 goto error;
1360 }
1361
9736d991
PP
1362 BT_ASSERT(!plugin);
1363 }
1364
1365 BT_ASSERT(*plugin_set_out);
1366
1367 if ((*plugin_set_out)->plugins->len == 0) {
1368 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
d24d5663 1369 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
1370 }
1371
1372 goto end;
1373
1374error:
9736d991
PP
1375 BT_ASSERT(status < 0);
1376 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
55bb57e0
PP
1377
1378end:
9736d991 1379 return status;
55bb57e0
PP
1380}
1381
d24d5663 1382int bt_plugin_so_create_all_from_static(bool fail_on_load_error,
9736d991 1383 struct bt_plugin_set **plugin_set_out)
55bb57e0 1384{
d24d5663 1385 int status;
9736d991 1386 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
55bb57e0 1387
9736d991
PP
1388 BT_ASSERT(plugin_set_out);
1389 *plugin_set_out = NULL;
1390 status = bt_plugin_so_shared_lib_handle_create(NULL,
1391 &shared_lib_handle);
d24d5663 1392 if (status != BT_FUNC_STATUS_OK) {
9736d991 1393 BT_ASSERT(!shared_lib_handle);
55bb57e0
PP
1394 goto end;
1395 }
1396
9736d991 1397 BT_ASSERT(shared_lib_handle);
3fe0bf43 1398 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
9736d991
PP
1399 status = bt_plugin_so_create_all_from_sections(shared_lib_handle,
1400 fail_on_load_error,
52238017
MJ
1401 __bt_get_begin_section_plugin_descriptors(),
1402 __bt_get_end_section_plugin_descriptors(),
1403 __bt_get_begin_section_plugin_descriptor_attributes(),
1404 __bt_get_end_section_plugin_descriptor_attributes(),
1405 __bt_get_begin_section_component_class_descriptors(),
1406 __bt_get_end_section_component_class_descriptors(),
1407 __bt_get_begin_section_component_class_descriptor_attributes(),
9736d991
PP
1408 __bt_get_end_section_component_class_descriptor_attributes(),
1409 plugin_set_out);
d24d5663 1410 BT_ASSERT((status == BT_FUNC_STATUS_OK && *plugin_set_out &&
9736d991 1411 (*plugin_set_out)->plugins->len > 0) || !*plugin_set_out);
55bb57e0
PP
1412
1413end:
65300d60 1414 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
9736d991 1415 return status;
55bb57e0
PP
1416}
1417
d24d5663 1418int bt_plugin_so_create_all_from_file(const char *path,
9736d991 1419 bool fail_on_load_error, struct bt_plugin_set **plugin_set_out)
55bb57e0
PP
1420{
1421 size_t path_len;
d24d5663 1422 int status;
55bb57e0
PP
1423 struct __bt_plugin_descriptor const * const *descriptors_begin = NULL;
1424 struct __bt_plugin_descriptor const * const *descriptors_end = NULL;
1425 struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL;
1426 struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL;
1427 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL;
1428 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL;
1429 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL;
1430 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL;
52238017
MJ
1431 struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void);
1432 struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void);
1433 struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void);
1434 struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void);
1435 struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void);
1436 struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void);
1437 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void);
1438 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void);
c55a9f58 1439 bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE;
55bb57e0
PP
1440 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
1441
3f7d4d90 1442 BT_ASSERT(path);
9736d991
PP
1443 BT_ASSERT(plugin_set_out);
1444 *plugin_set_out = NULL;
55bb57e0 1445 path_len = strlen(path);
4830a4bc
FD
1446
1447 /*
1448 * An SO plugin file must have a known plugin file suffix. So the file
1449 * path must be longer than the suffix length.
1450 */
1451 if (path_len <= PLUGIN_SUFFIX_LEN) {
1452 BT_LOGI("Path is too short to be an `.so` or `.la` plugin file:"
1453 "path=%s, path-length=%zu, min-length=%zu",
1454 path, path_len, PLUGIN_SUFFIX_LEN);
1455 status = BT_FUNC_STATUS_NOT_FOUND;
1456 goto end;
1457 }
1458
3f7d4d90 1459 BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path);
55bb57e0 1460 path_len++;
3f7d4d90 1461
55bb57e0 1462 /*
3f7d4d90
PP
1463 * Check if the file ends with a known plugin file type suffix
1464 * (i.e. .so or .la on Linux).
55bb57e0
PP
1465 */
1466 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
1467 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
1468 LIBTOOL_PLUGIN_SUFFIX_LEN);
1469 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
1470 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
1471 NATIVE_PLUGIN_SUFFIX_LEN);
1472 if (!is_shared_object && !is_libtool_wrapper) {
3fe0bf43 1473 /* Name indicates this is not a plugin file; not an error */
9736d991 1474 BT_LOGI("File is not an SO plugin file: path=\"%s\"", path);
d24d5663 1475 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
1476 goto end;
1477 }
1478
9736d991
PP
1479 status = bt_plugin_so_shared_lib_handle_create(path,
1480 &shared_lib_handle);
d24d5663 1481 if (status != BT_FUNC_STATUS_OK) {
3f7d4d90 1482 /* bt_plugin_so_shared_lib_handle_create() logs more details */
9736d991 1483 BT_ASSERT(!shared_lib_handle);
55bb57e0
PP
1484 goto end;
1485 }
1486
52238017
MJ
1487 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors",
1488 (gpointer *) &get_begin_section_plugin_descriptors)) {
1489 descriptors_begin = get_begin_section_plugin_descriptors();
1490 } else {
9736d991
PP
1491 /*
1492 * Use this first symbol to know whether or not this
1493 * shared object _looks like_ a Babeltrace plugin. Since
1494 * g_module_symbol() failed, assume that this is not a
1495 * Babeltrace plugin, so it's not an error.
1496 */
3f7d4d90 1497 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1498 "symbol=\"%s\"", path,
52238017 1499 "__bt_get_begin_section_plugin_descriptors");
d24d5663 1500 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
1501 goto end;
1502 }
1503
9736d991
PP
1504 /*
1505 * If g_module_symbol() fails for any of the other symbols, fail
1506 * if `fail_on_load_error` is true.
1507 */
52238017
MJ
1508 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors",
1509 (gpointer *) &get_end_section_plugin_descriptors)) {
1510 descriptors_end = get_end_section_plugin_descriptors();
1511 } else {
870631a2
PP
1512 if (fail_on_load_error) {
1513 BT_LIB_LOGW_APPEND_CAUSE(
1514 "Cannot resolve plugin symbol: path=\"%s\", "
1515 "symbol=\"%s\"", path,
1516 "__bt_get_end_section_plugin_descriptors");
b189a968 1517 status = BT_FUNC_STATUS_ERROR;
870631a2 1518 } else {
a0cdfce8 1519 BT_LIB_LOGW(
870631a2
PP
1520 "Cannot resolve plugin symbol: path=\"%s\", "
1521 "symbol=\"%s\"", path,
1522 "__bt_get_end_section_plugin_descriptors");
1523 status = BT_FUNC_STATUS_NOT_FOUND;
1524 }
1525
55bb57e0
PP
1526 goto end;
1527 }
1528
52238017
MJ
1529 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes",
1530 (gpointer *) &get_begin_section_plugin_descriptor_attributes)) {
1531 attrs_begin = get_begin_section_plugin_descriptor_attributes();
1532 } else {
3f7d4d90 1533 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1534 "symbol=\"%s\"", path,
52238017 1535 "__bt_get_begin_section_plugin_descriptor_attributes");
55bb57e0
PP
1536 }
1537
52238017
MJ
1538 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes",
1539 (gpointer *) &get_end_section_plugin_descriptor_attributes)) {
1540 attrs_end = get_end_section_plugin_descriptor_attributes();
1541 } else {
3f7d4d90 1542 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1543 "symbol=\"%s\"", path,
52238017 1544 "__bt_get_end_section_plugin_descriptor_attributes");
55bb57e0
PP
1545 }
1546
1547 if ((!!attrs_begin - !!attrs_end) != 0) {
870631a2
PP
1548 if (fail_on_load_error) {
1549 BT_LIB_LOGW_APPEND_CAUSE(
1550 "Found section start or end symbol, but not both: "
1551 "path=\"%s\", symbol-start=\"%s\", "
1552 "symbol-end=\"%s\", symbol-start-addr=%p, "
1553 "symbol-end-addr=%p",
1554 path, "__bt_get_begin_section_plugin_descriptor_attributes",
1555 "__bt_get_end_section_plugin_descriptor_attributes",
1556 attrs_begin, attrs_end);
b189a968 1557 status = BT_FUNC_STATUS_ERROR;
870631a2 1558 } else {
a0cdfce8 1559 BT_LIB_LOGW(
870631a2
PP
1560 "Found section start or end symbol, but not both: "
1561 "path=\"%s\", symbol-start=\"%s\", "
1562 "symbol-end=\"%s\", symbol-start-addr=%p, "
1563 "symbol-end-addr=%p",
1564 path, "__bt_get_begin_section_plugin_descriptor_attributes",
1565 "__bt_get_end_section_plugin_descriptor_attributes",
1566 attrs_begin, attrs_end);
1567 status = BT_FUNC_STATUS_NOT_FOUND;
1568 }
1569
55bb57e0
PP
1570 goto end;
1571 }
1572
52238017
MJ
1573 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors",
1574 (gpointer *) &get_begin_section_component_class_descriptors)) {
1575 cc_descriptors_begin = get_begin_section_component_class_descriptors();
1576 } else {
3f7d4d90 1577 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1578 "symbol=\"%s\"", path,
52238017 1579 "__bt_get_begin_section_component_class_descriptors");
55bb57e0
PP
1580 }
1581
52238017
MJ
1582 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors",
1583 (gpointer *) &get_end_section_component_class_descriptors)) {
1584 cc_descriptors_end = get_end_section_component_class_descriptors();
1585 } else {
3f7d4d90 1586 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1587 "symbol=\"%s\"", path,
52238017 1588 "__bt_get_end_section_component_class_descriptors");
55bb57e0
PP
1589 }
1590
1591 if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) {
870631a2
PP
1592 if (fail_on_load_error) {
1593 BT_LIB_LOGW_APPEND_CAUSE(
1594 "Found section start or end symbol, but not both: "
1595 "path=\"%s\", symbol-start=\"%s\", "
1596 "symbol-end=\"%s\", symbol-start-addr=%p, "
1597 "symbol-end-addr=%p",
1598 path, "__bt_get_begin_section_component_class_descriptors",
1599 "__bt_get_end_section_component_class_descriptors",
1600 cc_descriptors_begin, cc_descriptors_end);
b189a968 1601 status = BT_FUNC_STATUS_ERROR;
870631a2 1602 } else {
a0cdfce8 1603 BT_LIB_LOGW(
870631a2
PP
1604 "Found section start or end symbol, but not both: "
1605 "path=\"%s\", symbol-start=\"%s\", "
1606 "symbol-end=\"%s\", symbol-start-addr=%p, "
1607 "symbol-end-addr=%p",
1608 path, "__bt_get_begin_section_component_class_descriptors",
1609 "__bt_get_end_section_component_class_descriptors",
1610 cc_descriptors_begin, cc_descriptors_end);
1611 status = BT_FUNC_STATUS_NOT_FOUND;
1612 }
1613
55bb57e0
PP
1614 goto end;
1615 }
1616
52238017
MJ
1617 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes",
1618 (gpointer *) &get_begin_section_component_class_descriptor_attributes)) {
1619 cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes();
1620 } else {
3f7d4d90 1621 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1622 "symbol=\"%s\"", path,
52238017 1623 "__bt_get_begin_section_component_class_descriptor_attributes");
55bb57e0
PP
1624 }
1625
52238017
MJ
1626 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes",
1627 (gpointer *) &get_end_section_component_class_descriptor_attributes)) {
1628 cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes();
1629 } else {
3f7d4d90 1630 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
3fe0bf43 1631 "symbol=\"%s\"", path,
52238017 1632 "__bt_get_end_section_component_class_descriptor_attributes");
55bb57e0
PP
1633 }
1634
1635 if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) {
870631a2
PP
1636 if (fail_on_load_error) {
1637 BT_LIB_LOGW_APPEND_CAUSE(
1638 "Found section start or end symbol, but not both: "
1639 "path=\"%s\", symbol-start=\"%s\", "
1640 "symbol-end=\"%s\", symbol-start-addr=%p, "
1641 "symbol-end-addr=%p",
1642 path, "__bt_get_begin_section_component_class_descriptor_attributes",
1643 "__bt_get_end_section_component_class_descriptor_attributes",
1644 cc_descr_attrs_begin, cc_descr_attrs_end);
b189a968 1645 status = BT_FUNC_STATUS_ERROR;
870631a2 1646 } else {
a0cdfce8 1647 BT_LIB_LOGW(
870631a2
PP
1648 "Found section start or end symbol, but not both: "
1649 "path=\"%s\", symbol-start=\"%s\", "
1650 "symbol-end=\"%s\", symbol-start-addr=%p, "
1651 "symbol-end-addr=%p",
1652 path, "__bt_get_begin_section_component_class_descriptor_attributes",
1653 "__bt_get_end_section_component_class_descriptor_attributes",
1654 cc_descr_attrs_begin, cc_descr_attrs_end);
1655 status = BT_FUNC_STATUS_NOT_FOUND;
1656 }
1657
55bb57e0
PP
1658 goto end;
1659 }
1660
1661 /* Initialize plugin */
3fe0bf43 1662 BT_LOGD_STR("Initializing plugin object.");
9736d991
PP
1663 status = bt_plugin_so_create_all_from_sections(shared_lib_handle,
1664 fail_on_load_error,
55bb57e0
PP
1665 descriptors_begin, descriptors_end, attrs_begin, attrs_end,
1666 cc_descriptors_begin, cc_descriptors_end,
9736d991 1667 cc_descr_attrs_begin, cc_descr_attrs_end, plugin_set_out);
55bb57e0
PP
1668
1669end:
65300d60 1670 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
9736d991 1671 return status;
55bb57e0
PP
1672}
1673
1674static
1675void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
ecd7492f 1676 void *data __attribute__((unused)))
55bb57e0 1677{
bfa9a4be 1678 bt_list_del(&comp_class->node);
65300d60 1679 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
3f7d4d90 1680 BT_LOGD("Component class destroyed: removed entry from list: "
3fe0bf43 1681 "comp-cls-addr=%p", comp_class);
55bb57e0
PP
1682}
1683
1353b066
SM
1684/*
1685 * This function would normally not be BT_EXPORTed, but it is used by the
1686 * Python plugin provider, which is conceptually part of libbabeltrace2, but
1687 * implemented as a separate shared object, for modularity. It is therefore
1688 * exposed, but not part of the public ABI.
1689 */
1690BT_EXPORT
3230ee6b 1691void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin,
55bb57e0
PP
1692 struct bt_component_class *comp_class)
1693{
55bb57e0
PP
1694 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
1695
f6ccaed9
PP
1696 BT_ASSERT(plugin->spec_data);
1697 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
55bb57e0 1698
bfa9a4be 1699 bt_list_add(&comp_class->node, &component_class_list);
398454ed 1700 comp_class->so_handle = spec->shared_lib_handle;
6871026b 1701 bt_object_get_ref_no_null_check(comp_class->so_handle);
55bb57e0
PP
1702
1703 /* Add our custom destroy listener */
3230ee6b 1704 bt_component_class_add_destroy_listener(comp_class,
55bb57e0 1705 plugin_comp_class_destroy_listener, NULL);
55bb57e0 1706}
This page took 0.183472 seconds and 4 git commands to generate.