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