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