CTF writer: use own `bt_ctf_object` and `bt_ctf_value` internal APIs
[babeltrace.git] / lib / plugin / plugin-so.c
CommitLineData
55bb57e0 1/*
55bb57e0
PP
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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
3fe0bf43
PP
26#define BT_LOG_TAG "PLUGIN-SO"
27#include <babeltrace/lib-logging-internal.h>
28
3d9990ac 29#include <babeltrace/compiler-internal.h>
65300d60 30#include <babeltrace/object.h>
55bb57e0
PP
31#include <babeltrace/plugin/plugin-internal.h>
32#include <babeltrace/plugin/plugin-so-internal.h>
33#include <babeltrace/plugin/plugin-dev.h>
34#include <babeltrace/plugin/plugin-internal.h>
b2e0c907 35#include <babeltrace/graph/component-class-internal.h>
d94d92ac
PP
36#include <babeltrace/graph/private-component-class.h>
37#include <babeltrace/graph/private-component-class-source.h>
38#include <babeltrace/graph/private-component-class-filter.h>
39#include <babeltrace/graph/private-component-class-sink.h>
c55a9f58 40#include <babeltrace/types.h>
bfa9a4be 41#include <babeltrace/list-internal.h>
f6ccaed9 42#include <babeltrace/assert-internal.h>
55bb57e0 43#include <string.h>
0fbb9a9f 44#include <stdlib.h>
55bb57e0
PP
45#include <glib.h>
46#include <gmodule.h>
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
53#define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
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
PP
73 *
74 * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
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
116 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
117 shared_lib_handle, path);
55bb57e0
PP
118
119 if (shared_lib_handle->init_called && shared_lib_handle->exit) {
3fe0bf43 120 enum bt_plugin_status status;
55bb57e0 121
3fe0bf43
PP
122 BT_LOGD_STR("Calling user's plugin exit function.");
123 status = shared_lib_handle->exit();
124 BT_LOGD("User function returned: %s",
125 bt_plugin_status_string(status));
55bb57e0 126
3fe0bf43
PP
127 if (status < 0) {
128 BT_LOGW("User's plugin exit function failed: "
129 "path=\"%s\"", path);
55bb57e0
PP
130 }
131 }
132
133 if (shared_lib_handle->module) {
f1447220
PP
134#ifndef NDEBUG
135 /*
136 * Valgrind shows incomplete stack traces when
137 * dynamically loaded libraries are closed before it
138 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
139 * build to avoid this.
140 */
141 const char *var = getenv("BABELTRACE_NO_DLCLOSE");
142
143 if (!var || strcmp(var, "1") != 0) {
144#endif
3fe0bf43
PP
145 BT_LOGD("Closing GModule: path=\"%s\"", path);
146
f1447220 147 if (!g_module_close(shared_lib_handle->module)) {
3fe0bf43
PP
148 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
149 g_module_error(), path);
f1447220 150 }
db5504f9
PP
151
152 shared_lib_handle->module = NULL;
f1447220 153#ifndef NDEBUG
3fe0bf43
PP
154 } else {
155 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
156 "path=\"%s\"", path);
55bb57e0 157 }
f1447220 158#endif
55bb57e0
PP
159 }
160
161 if (shared_lib_handle->path) {
162 g_string_free(shared_lib_handle->path, TRUE);
db5504f9 163 shared_lib_handle->path = NULL;
55bb57e0
PP
164 }
165
166 g_free(shared_lib_handle);
167}
168
169static
170struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create(
171 const char *path)
172{
173 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
174
3fe0bf43 175 BT_LOGD("Creating shared library handle: path=\"%s\"", path);
55bb57e0
PP
176 shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1);
177 if (!shared_lib_handle) {
3fe0bf43 178 BT_LOGE_STR("Failed to allocate one shared library handle.");
55bb57e0
PP
179 goto error;
180 }
181
3fea54f6
PP
182 bt_object_init_shared(&shared_lib_handle->base,
183 bt_plugin_so_shared_lib_handle_destroy);
55bb57e0
PP
184
185 if (!path) {
186 goto end;
187 }
188
189 shared_lib_handle->path = g_string_new(path);
190 if (!shared_lib_handle->path) {
3fe0bf43 191 BT_LOGE_STR("Failed to allocate a GString.");
55bb57e0
PP
192 goto error;
193 }
194
424b8c43 195 shared_lib_handle->module = g_module_open(path, G_MODULE_BIND_LOCAL);
55bb57e0 196 if (!shared_lib_handle->module) {
50ad9320
PP
197 /*
198 * DEBUG-level logging because we're only _trying_ to
199 * open this file as a Babeltrace plugin: if it's not,
200 * it's not an error. And because this can be tried
201 * during bt_plugin_create_all_from_dir(), it's not even
202 * a warning.
203 */
204 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
3fe0bf43 205 g_module_error(), path);
55bb57e0
PP
206 goto error;
207 }
208
209 goto end;
210
211error:
65300d60 212 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
55bb57e0
PP
213
214end:
3fe0bf43
PP
215 if (shared_lib_handle) {
216 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
217 path, shared_lib_handle);
218 }
219
55bb57e0
PP
220 return shared_lib_handle;
221}
222
6fbd4105 223static
55bb57e0
PP
224void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin)
225{
226 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
227
228 if (!plugin->spec_data) {
229 return;
230 }
231
f6ccaed9
PP
232 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
233 BT_ASSERT(spec);
65300d60 234 BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle);
55bb57e0
PP
235 g_free(plugin->spec_data);
236 plugin->spec_data = NULL;
237}
238
239/*
240 * This function does the following:
241 *
242 * 1. Iterate on the plugin descriptor attributes section and set the
243 * plugin's attributes depending on the attribute types. This
244 * includes the name of the plugin, its description, and its
245 * initialization function, for example.
246 *
247 * 2. Iterate on the component class descriptors section and create one
248 * "full descriptor" (temporary structure) for each one that is found
249 * and attached to our plugin descriptor.
250 *
251 * 3. Iterate on the component class descriptor attributes section and
252 * set the corresponding full descriptor's attributes depending on
253 * the attribute types. This includes the description of the
254 * component class, as well as its initialization and destroy
255 * methods.
256 *
257 * 4. Call the user's plugin initialization function, if any is
258 * defined.
259 *
260 * 5. For each full component class descriptor, create a component class
261 * object, set its optional attributes, and add it to the plugin
262 * object.
263 *
264 * 6. Freeze the plugin object.
265 */
266static
267enum bt_plugin_status bt_plugin_so_init(
268 struct bt_plugin *plugin,
269 const struct __bt_plugin_descriptor *descriptor,
270 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
271 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
272 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
273 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
274 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
275 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
276{
277 /*
278 * This structure's members point to the plugin's memory
279 * (do NOT free).
280 */
281 struct comp_class_full_descriptor {
282 const struct __bt_plugin_component_class_descriptor *descriptor;
283 const char *description;
279b3f15 284 const char *help;
d94d92ac
PP
285
286 union {
287 struct {
288 bt_private_component_class_source_init_method init;
289 bt_private_component_class_source_finalize_method finalize;
290 bt_private_component_class_source_query_method query;
291 bt_private_component_class_source_accept_output_port_connection_method accept_output_port_connection;
292 bt_private_component_class_source_output_port_connected_method output_port_connected;
293 bt_private_component_class_source_output_port_disconnected_method output_port_disconnected;
294 bt_private_component_class_source_notification_iterator_init_method notif_iter_init;
295 bt_private_component_class_source_notification_iterator_finalize_method notif_iter_finalize;
296 } source;
297
298 struct {
299 bt_private_component_class_filter_init_method init;
300 bt_private_component_class_filter_finalize_method finalize;
301 bt_private_component_class_filter_query_method query;
302 bt_private_component_class_filter_accept_input_port_connection_method accept_input_port_connection;
303 bt_private_component_class_filter_accept_output_port_connection_method accept_output_port_connection;
304 bt_private_component_class_filter_input_port_connected_method input_port_connected;
305 bt_private_component_class_filter_output_port_connected_method output_port_connected;
306 bt_private_component_class_filter_input_port_disconnected_method input_port_disconnected;
307 bt_private_component_class_filter_output_port_disconnected_method output_port_disconnected;
308 bt_private_component_class_filter_notification_iterator_init_method notif_iter_init;
309 bt_private_component_class_filter_notification_iterator_finalize_method notif_iter_finalize;
310 } filter;
311
312 struct {
313 bt_private_component_class_sink_init_method init;
314 bt_private_component_class_sink_finalize_method finalize;
315 bt_private_component_class_sink_query_method query;
316 bt_private_component_class_sink_accept_input_port_connection_method accept_input_port_connection;
317 bt_private_component_class_sink_input_port_connected_method input_port_connected;
318 bt_private_component_class_sink_input_port_disconnected_method input_port_disconnected;
319 } sink;
320 } methods;
55bb57e0
PP
321 };
322
323 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
324 struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr;
325 struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr;
326 struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr;
327 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
328 GArray *comp_class_full_descriptors;
329 size_t i;
330 int ret;
331
3fe0bf43
PP
332 BT_LOGD("Initializing plugin object from descriptors found in sections: "
333 "plugin-addr=%p, plugin-path=\"%s\", "
334 "attrs-begin-addr=%p, attrs-end-addr=%p, "
335 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
336 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
337 plugin,
338 spec->shared_lib_handle->path ?
339 spec->shared_lib_handle->path->str : NULL,
340 attrs_begin, attrs_end,
341 cc_descriptors_begin, cc_descriptors_end,
342 cc_descr_attrs_begin, cc_descr_attrs_end);
55bb57e0
PP
343 comp_class_full_descriptors = g_array_new(FALSE, TRUE,
344 sizeof(struct comp_class_full_descriptor));
345 if (!comp_class_full_descriptors) {
3fe0bf43 346 BT_LOGE_STR("Failed to allocate a GArray.");
55bb57e0
PP
347 status = BT_PLUGIN_STATUS_ERROR;
348 goto end;
349 }
350
351 /* Set mandatory attributes */
352 spec->descriptor = descriptor;
353 bt_plugin_set_name(plugin, descriptor->name);
354
355 /*
356 * Find and set optional attributes attached to this plugin
357 * descriptor.
358 */
359 for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) {
360 const struct __bt_plugin_descriptor_attribute *cur_attr =
361 *cur_attr_ptr;
362
52238017
MJ
363 if (cur_attr == NULL) {
364 continue;
365 }
366
55bb57e0
PP
367 if (cur_attr->plugin_descriptor != descriptor) {
368 continue;
369 }
370
371 switch (cur_attr->type) {
372 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT:
373 spec->init = cur_attr->value.init;
374 break;
375 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT:
376 spec->shared_lib_handle->exit = cur_attr->value.exit;
377 break;
378 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR:
379 bt_plugin_set_author(plugin, cur_attr->value.author);
380 break;
381 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE:
382 bt_plugin_set_license(plugin, cur_attr->value.license);
383 break;
384 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
385 bt_plugin_set_description(plugin, cur_attr->value.description);
386 break;
387 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION:
388 bt_plugin_set_version(plugin,
389 (unsigned int) cur_attr->value.version.major,
390 (unsigned int) cur_attr->value.version.minor,
391 (unsigned int) cur_attr->value.version.patch,
392 cur_attr->value.version.extra);
393 break;
394 default:
50ad9320
PP
395 /*
396 * WARN-level logging because this should not
397 * happen with the appropriate ABI version. If
398 * we're here, we know that for the reported
399 * version of the ABI, this attribute is
400 * unknown.
401 */
3fe0bf43
PP
402 BT_LOGW("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);
55bb57e0
PP
410 break;
411 }
412 }
413
414 /*
415 * Find component class descriptors attached to this plugin
416 * descriptor and initialize corresponding full component class
417 * descriptors in the array.
418 */
419 for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) {
420 const struct __bt_plugin_component_class_descriptor *cur_cc_descr =
421 *cur_cc_descr_ptr;
422 struct comp_class_full_descriptor full_descriptor = {0};
423
52238017
MJ
424 if (cur_cc_descr == NULL) {
425 continue;
426 }
427
55bb57e0
PP
428 if (cur_cc_descr->plugin_descriptor != descriptor) {
429 continue;
430 }
431
432 full_descriptor.descriptor = cur_cc_descr;
433 g_array_append_val(comp_class_full_descriptors,
434 full_descriptor);
435 }
436
437 /*
438 * Find component class descriptor attributes attached to this
439 * plugin descriptor and update corresponding full component
440 * class descriptors in the array.
441 */
442 for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) {
443 const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr =
444 *cur_cc_descr_attr_ptr;
d94d92ac 445 enum bt_component_class_type cc_type;
55bb57e0 446
52238017
MJ
447 if (cur_cc_descr_attr == NULL) {
448 continue;
449 }
450
55bb57e0
PP
451 if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor !=
452 descriptor) {
453 continue;
454 }
455
d94d92ac
PP
456 cc_type = cur_cc_descr_attr->comp_class_descriptor->type;
457
55bb57e0
PP
458 /* Find the corresponding component class descriptor entry */
459 for (i = 0; i < comp_class_full_descriptors->len; i++) {
460 struct comp_class_full_descriptor *cc_full_descr =
461 &g_array_index(comp_class_full_descriptors,
462 struct comp_class_full_descriptor, i);
463
d94d92ac 464 if (cur_cc_descr_attr->comp_class_descriptor !=
55bb57e0 465 cc_full_descr->descriptor) {
d94d92ac
PP
466 continue;
467 }
468
469 switch (cur_cc_descr_attr->type) {
470 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
471 cc_full_descr->description =
472 cur_cc_descr_attr->value.description;
473 break;
474 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP:
475 cc_full_descr->help =
476 cur_cc_descr_attr->value.help;
477 break;
478 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD:
479 switch (cc_type) {
480 case BT_COMPONENT_CLASS_TYPE_SOURCE:
481 cc_full_descr->methods.source.init =
482 cur_cc_descr_attr->value.source_init_method;
55bb57e0 483 break;
d94d92ac
PP
484 case BT_COMPONENT_CLASS_TYPE_FILTER:
485 cc_full_descr->methods.filter.init =
486 cur_cc_descr_attr->value.filter_init_method;
279b3f15 487 break;
d94d92ac
PP
488 case BT_COMPONENT_CLASS_TYPE_SINK:
489 cc_full_descr->methods.sink.init =
490 cur_cc_descr_attr->value.sink_init_method;
55bb57e0 491 break;
d94d92ac
PP
492 default:
493 abort();
494 }
495 break;
496 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD:
497 switch (cc_type) {
498 case BT_COMPONENT_CLASS_TYPE_SOURCE:
499 cc_full_descr->methods.source.finalize =
500 cur_cc_descr_attr->value.source_finalize_method;
55bb57e0 501 break;
d94d92ac
PP
502 case BT_COMPONENT_CLASS_TYPE_FILTER:
503 cc_full_descr->methods.filter.finalize =
504 cur_cc_descr_attr->value.filter_finalize_method;
8463eac2 505 break;
d94d92ac
PP
506 case BT_COMPONENT_CLASS_TYPE_SINK:
507 cc_full_descr->methods.sink.finalize =
508 cur_cc_descr_attr->value.sink_finalize_method;
72b913fb 509 break;
d94d92ac
PP
510 default:
511 abort();
512 }
513 break;
514 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD:
515 switch (cc_type) {
516 case BT_COMPONENT_CLASS_TYPE_SOURCE:
517 cc_full_descr->methods.source.query =
518 cur_cc_descr_attr->value.source_query_method;
0d8b4d8e 519 break;
d94d92ac
PP
520 case BT_COMPONENT_CLASS_TYPE_FILTER:
521 cc_full_descr->methods.filter.query =
522 cur_cc_descr_attr->value.filter_query_method;
55bb57e0 523 break;
d94d92ac
PP
524 case BT_COMPONENT_CLASS_TYPE_SINK:
525 cc_full_descr->methods.sink.query =
526 cur_cc_descr_attr->value.sink_query_method;
55bb57e0 527 break;
d94d92ac
PP
528 default:
529 abort();
530 }
531 break;
532 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_INPUT_PORT_CONNECTION_METHOD:
533 switch (cc_type) {
534 case BT_COMPONENT_CLASS_TYPE_FILTER:
535 cc_full_descr->methods.filter.accept_input_port_connection =
536 cur_cc_descr_attr->value.filter_accept_input_port_connection_method;
537 break;
538 case BT_COMPONENT_CLASS_TYPE_SINK:
539 cc_full_descr->methods.sink.accept_input_port_connection =
540 cur_cc_descr_attr->value.sink_accept_input_port_connection_method;
541 break;
542 default:
543 abort();
544 }
545 break;
546 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_OUTPUT_PORT_CONNECTION_METHOD:
547 switch (cc_type) {
548 case BT_COMPONENT_CLASS_TYPE_SOURCE:
549 cc_full_descr->methods.source.accept_output_port_connection =
550 cur_cc_descr_attr->value.source_accept_output_port_connection_method;
551 break;
552 case BT_COMPONENT_CLASS_TYPE_FILTER:
553 cc_full_descr->methods.filter.accept_output_port_connection =
554 cur_cc_descr_attr->value.filter_accept_output_port_connection_method;
555 break;
556 default:
557 abort();
558 }
559 break;
560 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD:
561 switch (cc_type) {
562 case BT_COMPONENT_CLASS_TYPE_FILTER:
563 cc_full_descr->methods.filter.input_port_connected =
564 cur_cc_descr_attr->value.filter_input_port_connected_method;
565 break;
566 case BT_COMPONENT_CLASS_TYPE_SINK:
567 cc_full_descr->methods.sink.input_port_connected =
568 cur_cc_descr_attr->value.sink_input_port_connected_method;
55bb57e0 569 break;
55bb57e0 570 default:
d94d92ac
PP
571 abort();
572 }
573 break;
574 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD:
575 switch (cc_type) {
576 case BT_COMPONENT_CLASS_TYPE_SOURCE:
577 cc_full_descr->methods.source.output_port_connected =
578 cur_cc_descr_attr->value.source_output_port_connected_method;
579 break;
580 case BT_COMPONENT_CLASS_TYPE_FILTER:
581 cc_full_descr->methods.filter.output_port_connected =
582 cur_cc_descr_attr->value.filter_output_port_connected_method;
55bb57e0 583 break;
d94d92ac
PP
584 default:
585 abort();
55bb57e0 586 }
d94d92ac
PP
587 break;
588 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_DISCONNECTED_METHOD:
589 switch (cc_type) {
590 case BT_COMPONENT_CLASS_TYPE_FILTER:
591 cc_full_descr->methods.filter.input_port_disconnected =
592 cur_cc_descr_attr->value.filter_input_port_disconnected_method;
593 break;
594 case BT_COMPONENT_CLASS_TYPE_SINK:
595 cc_full_descr->methods.sink.input_port_disconnected =
596 cur_cc_descr_attr->value.sink_input_port_disconnected_method;
597 break;
598 default:
599 abort();
600 }
601 break;
602 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_DISCONNECTED_METHOD:
603 switch (cc_type) {
604 case BT_COMPONENT_CLASS_TYPE_SOURCE:
605 cc_full_descr->methods.source.output_port_disconnected =
606 cur_cc_descr_attr->value.source_output_port_disconnected_method;
607 break;
608 case BT_COMPONENT_CLASS_TYPE_FILTER:
609 cc_full_descr->methods.filter.output_port_disconnected =
610 cur_cc_descr_attr->value.filter_output_port_disconnected_method;
611 break;
612 default:
613 abort();
614 }
615 break;
616 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD:
617 switch (cc_type) {
618 case BT_COMPONENT_CLASS_TYPE_SOURCE:
619 cc_full_descr->methods.source.notif_iter_init =
620 cur_cc_descr_attr->value.source_notif_iter_init_method;
621 break;
622 case BT_COMPONENT_CLASS_TYPE_FILTER:
623 cc_full_descr->methods.filter.notif_iter_init =
624 cur_cc_descr_attr->value.filter_notif_iter_init_method;
625 break;
626 default:
627 abort();
628 }
629 break;
630 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD:
631 switch (cc_type) {
632 case BT_COMPONENT_CLASS_TYPE_SOURCE:
633 cc_full_descr->methods.source.notif_iter_finalize =
634 cur_cc_descr_attr->value.source_notif_iter_finalize_method;
635 break;
636 case BT_COMPONENT_CLASS_TYPE_FILTER:
637 cc_full_descr->methods.filter.notif_iter_finalize =
638 cur_cc_descr_attr->value.filter_notif_iter_finalize_method;
639 break;
640 default:
641 abort();
642 }
643 break;
644 default:
645 /*
646 * WARN-level logging because this
647 * should not happen with the
648 * appropriate ABI version. If we're
649 * here, we know that for the reported
650 * version of the ABI, this attribute is
651 * unknown.
652 */
653 BT_LOGW("Ignoring unknown component class descriptor attribute: "
654 "plugin-path=\"%s\", "
655 "plugin-name=\"%s\", "
656 "comp-class-name=\"%s\", "
657 "comp-class-type=%s, "
658 "attr-type-name=\"%s\", "
659 "attr-type-id=%d",
660 spec->shared_lib_handle->path ?
661 spec->shared_lib_handle->path->str :
662 NULL,
663 descriptor->name,
664 cur_cc_descr_attr->comp_class_descriptor->name,
665 bt_component_class_type_string(
666 cur_cc_descr_attr->comp_class_descriptor->type),
667 cur_cc_descr_attr->type_name,
668 cur_cc_descr_attr->type);
669 break;
55bb57e0
PP
670 }
671 }
672 }
673
674 /* Initialize plugin */
675 if (spec->init) {
3fe0bf43 676 BT_LOGD_STR("Calling user's plugin initialization function.");
55bb57e0 677 status = spec->init(plugin);
3fe0bf43
PP
678 BT_LOGD("User function returned: %s",
679 bt_plugin_status_string(status));
680
55bb57e0 681 if (status < 0) {
3fe0bf43 682 BT_LOGW_STR("User's plugin initialization function failed.");
55bb57e0
PP
683 goto end;
684 }
685 }
686
c55a9f58 687 spec->shared_lib_handle->init_called = BT_TRUE;
55bb57e0
PP
688
689 /* Add described component classes to plugin */
690 for (i = 0; i < comp_class_full_descriptors->len; i++) {
691 struct comp_class_full_descriptor *cc_full_descr =
692 &g_array_index(comp_class_full_descriptors,
693 struct comp_class_full_descriptor, i);
d94d92ac
PP
694 struct bt_private_component_class *comp_class = NULL;
695 struct bt_private_component_class_source *src_comp_class = NULL;
696 struct bt_private_component_class_filter *flt_comp_class = NULL;
697 struct bt_private_component_class_sink *sink_comp_class = NULL;
55bb57e0 698
3fe0bf43
PP
699 BT_LOGD("Creating and setting properties of plugin's component class: "
700 "plugin-path=\"%s\", plugin-name=\"%s\", "
701 "comp-class-name=\"%s\", comp-class-type=%s",
702 spec->shared_lib_handle->path ?
703 spec->shared_lib_handle->path->str :
704 NULL,
705 descriptor->name,
706 cc_full_descr->descriptor->name,
707 bt_component_class_type_string(
708 cc_full_descr->descriptor->type));
709
55bb57e0
PP
710 switch (cc_full_descr->descriptor->type) {
711 case BT_COMPONENT_CLASS_TYPE_SOURCE:
d94d92ac 712 src_comp_class = bt_private_component_class_source_create(
55bb57e0 713 cc_full_descr->descriptor->name,
55bb57e0 714 cc_full_descr->descriptor->methods.source.notif_iter_next);
707b7d35 715 comp_class = bt_private_component_class_source_as_private_component_class(
d94d92ac 716 src_comp_class);
55bb57e0
PP
717 break;
718 case BT_COMPONENT_CLASS_TYPE_FILTER:
d94d92ac 719 flt_comp_class = bt_private_component_class_filter_create(
55bb57e0 720 cc_full_descr->descriptor->name,
55bb57e0 721 cc_full_descr->descriptor->methods.source.notif_iter_next);
707b7d35 722 comp_class = bt_private_component_class_filter_as_private_component_class(
d94d92ac 723 flt_comp_class);
55bb57e0
PP
724 break;
725 case BT_COMPONENT_CLASS_TYPE_SINK:
d94d92ac 726 sink_comp_class = bt_private_component_class_sink_create(
55bb57e0
PP
727 cc_full_descr->descriptor->name,
728 cc_full_descr->descriptor->methods.sink.consume);
707b7d35 729 comp_class = bt_private_component_class_sink_as_private_component_class(
d94d92ac 730 sink_comp_class);
55bb57e0
PP
731 break;
732 default:
50ad9320
PP
733 /*
734 * WARN-level logging because this should not
735 * happen with the appropriate ABI version. If
736 * we're here, we know that for the reported
737 * version of the ABI, this component class type
738 * is unknown.
739 */
3fe0bf43
PP
740 BT_LOGW("Ignoring unknown component class type: "
741 "plugin-path=\"%s\", plugin-name=\"%s\", "
742 "comp-class-name=\"%s\", comp-class-type=%d",
743 spec->shared_lib_handle->path->str ?
744 spec->shared_lib_handle->path->str :
745 NULL,
746 descriptor->name,
55bb57e0 747 cc_full_descr->descriptor->name,
3fe0bf43 748 cc_full_descr->descriptor->type);
55bb57e0
PP
749 continue;
750 }
751
752 if (!comp_class) {
3fe0bf43 753 BT_LOGE_STR("Cannot create component class.");
55bb57e0
PP
754 status = BT_PLUGIN_STATUS_ERROR;
755 goto end;
756 }
757
758 if (cc_full_descr->description) {
d94d92ac
PP
759 ret = bt_private_component_class_set_description(
760 comp_class, cc_full_descr->description);
55bb57e0 761 if (ret) {
3fe0bf43 762 BT_LOGE_STR("Cannot set component class's description.");
55bb57e0 763 status = BT_PLUGIN_STATUS_ERROR;
65300d60 764 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
55bb57e0
PP
765 goto end;
766 }
767 }
768
279b3f15 769 if (cc_full_descr->help) {
d94d92ac 770 ret = bt_private_component_class_set_help(comp_class,
279b3f15
PP
771 cc_full_descr->help);
772 if (ret) {
3fe0bf43 773 BT_LOGE_STR("Cannot set component class's help string.");
279b3f15 774 status = BT_PLUGIN_STATUS_ERROR;
65300d60 775 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
279b3f15
PP
776 goto end;
777 }
778 }
779
d94d92ac
PP
780 switch (cc_full_descr->descriptor->type) {
781 case BT_COMPONENT_CLASS_TYPE_SOURCE:
782 if (cc_full_descr->methods.source.init) {
783 ret = bt_private_component_class_source_set_init_method(
784 src_comp_class,
785 cc_full_descr->methods.source.init);
786 if (ret) {
787 BT_LOGE_STR("Cannot set source component class's initialization method.");
788 status = BT_PLUGIN_STATUS_ERROR;
789 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
790 goto end;
791 }
55bb57e0 792 }
55bb57e0 793
d94d92ac
PP
794 if (cc_full_descr->methods.source.finalize) {
795 ret = bt_private_component_class_source_set_finalize_method(
796 src_comp_class,
797 cc_full_descr->methods.source.finalize);
798 if (ret) {
799 BT_LOGE_STR("Cannot set source component class's finalization method.");
800 status = BT_PLUGIN_STATUS_ERROR;
801 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
802 goto end;
803 }
55bb57e0 804 }
55bb57e0 805
d94d92ac
PP
806 if (cc_full_descr->methods.source.query) {
807 ret = bt_private_component_class_source_set_query_method(
808 src_comp_class,
809 cc_full_descr->methods.source.query);
810 if (ret) {
811 BT_LOGE_STR("Cannot set source component class's query method.");
812 status = BT_PLUGIN_STATUS_ERROR;
813 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
814 goto end;
815 }
8463eac2 816 }
8463eac2 817
d94d92ac
PP
818 if (cc_full_descr->methods.source.accept_output_port_connection) {
819 ret = bt_private_component_class_source_set_accept_output_port_connection_method(
820 src_comp_class,
821 cc_full_descr->methods.source.accept_output_port_connection);
822 if (ret) {
823 BT_LOGE_STR("Cannot set source component class's \"accept input output connection\" method.");
824 status = BT_PLUGIN_STATUS_ERROR;
825 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
826 goto end;
827 }
72b913fb 828 }
72b913fb 829
d94d92ac
PP
830 if (cc_full_descr->methods.source.output_port_connected) {
831 ret = bt_private_component_class_source_set_output_port_connected_method(
832 src_comp_class,
833 cc_full_descr->methods.source.output_port_connected);
834 if (ret) {
835 BT_LOGE_STR("Cannot set source component class's \"output port connected\" method.");
836 status = BT_PLUGIN_STATUS_ERROR;
837 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
838 goto end;
839 }
0d8b4d8e 840 }
0d8b4d8e 841
d94d92ac
PP
842 if (cc_full_descr->methods.source.output_port_disconnected) {
843 ret = bt_private_component_class_source_set_output_port_disconnected_method(
844 src_comp_class,
845 cc_full_descr->methods.source.output_port_disconnected);
846 if (ret) {
847 BT_LOGE_STR("Cannot set source component class's \"output port disconnected\" method.");
848 status = BT_PLUGIN_STATUS_ERROR;
849 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
850 goto end;
851 }
2d41b99e 852 }
2d41b99e 853
d94d92ac
PP
854 if (cc_full_descr->methods.source.notif_iter_init) {
855 ret = bt_private_component_class_source_set_notification_iterator_init_method(
856 src_comp_class,
857 cc_full_descr->methods.source.notif_iter_init);
55bb57e0 858 if (ret) {
d94d92ac 859 BT_LOGE_STR("Cannot set source component class's notification iterator initialization method.");
55bb57e0 860 status = BT_PLUGIN_STATUS_ERROR;
d94d92ac 861 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
55bb57e0
PP
862 goto end;
863 }
864 }
865
d94d92ac
PP
866 if (cc_full_descr->methods.source.notif_iter_finalize) {
867 ret = bt_private_component_class_source_set_notification_iterator_finalize_method(
868 src_comp_class,
869 cc_full_descr->methods.source.notif_iter_finalize);
55bb57e0 870 if (ret) {
3fe0bf43 871 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
55bb57e0 872 status = BT_PLUGIN_STATUS_ERROR;
d94d92ac 873 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
55bb57e0
PP
874 goto end;
875 }
876 }
d94d92ac 877
55bb57e0
PP
878 break;
879 case BT_COMPONENT_CLASS_TYPE_FILTER:
d94d92ac
PP
880 if (cc_full_descr->methods.filter.init) {
881 ret = bt_private_component_class_filter_set_init_method(
882 flt_comp_class,
883 cc_full_descr->methods.filter.init);
884 if (ret) {
885 BT_LOGE_STR("Cannot set filter component class's initialization method.");
886 status = BT_PLUGIN_STATUS_ERROR;
887 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
888 goto end;
889 }
890 }
891
892 if (cc_full_descr->methods.filter.finalize) {
893 ret = bt_private_component_class_filter_set_finalize_method(
894 flt_comp_class,
895 cc_full_descr->methods.filter.finalize);
896 if (ret) {
897 BT_LOGE_STR("Cannot set filter component class's finalization method.");
898 status = BT_PLUGIN_STATUS_ERROR;
899 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
900 goto end;
901 }
902 }
903
904 if (cc_full_descr->methods.filter.query) {
905 ret = bt_private_component_class_filter_set_query_method(
906 flt_comp_class,
907 cc_full_descr->methods.filter.query);
908 if (ret) {
909 BT_LOGE_STR("Cannot set filter component class's query method.");
910 status = BT_PLUGIN_STATUS_ERROR;
911 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
912 goto end;
913 }
914 }
915
916 if (cc_full_descr->methods.filter.accept_input_port_connection) {
917 ret = bt_private_component_class_filter_set_accept_input_port_connection_method(
918 flt_comp_class,
919 cc_full_descr->methods.filter.accept_input_port_connection);
920 if (ret) {
921 BT_LOGE_STR("Cannot set filter component class's \"accept input port connection\" method.");
922 status = BT_PLUGIN_STATUS_ERROR;
923 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
924 goto end;
925 }
926 }
927
928 if (cc_full_descr->methods.filter.accept_output_port_connection) {
929 ret = bt_private_component_class_filter_set_accept_output_port_connection_method(
930 flt_comp_class,
931 cc_full_descr->methods.filter.accept_output_port_connection);
932 if (ret) {
933 BT_LOGE_STR("Cannot set filter component class's \"accept input output connection\" method.");
934 status = BT_PLUGIN_STATUS_ERROR;
935 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
936 goto end;
937 }
938 }
939
940 if (cc_full_descr->methods.filter.input_port_connected) {
941 ret = bt_private_component_class_filter_set_input_port_connected_method(
942 flt_comp_class,
943 cc_full_descr->methods.filter.input_port_connected);
944 if (ret) {
945 BT_LOGE_STR("Cannot set filter component class's \"input port connected\" method.");
946 status = BT_PLUGIN_STATUS_ERROR;
947 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
948 goto end;
949 }
950 }
951
952 if (cc_full_descr->methods.filter.output_port_connected) {
953 ret = bt_private_component_class_filter_set_output_port_connected_method(
954 flt_comp_class,
955 cc_full_descr->methods.filter.output_port_connected);
956 if (ret) {
957 BT_LOGE_STR("Cannot set filter component class's \"output port connected\" method.");
958 status = BT_PLUGIN_STATUS_ERROR;
959 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
960 goto end;
961 }
962 }
963
964 if (cc_full_descr->methods.filter.input_port_disconnected) {
965 ret = bt_private_component_class_filter_set_input_port_disconnected_method(
966 flt_comp_class,
967 cc_full_descr->methods.filter.input_port_disconnected);
968 if (ret) {
969 BT_LOGE_STR("Cannot set filter component class's \"input port disconnected\" method.");
970 status = BT_PLUGIN_STATUS_ERROR;
971 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
972 goto end;
973 }
974 }
975
976 if (cc_full_descr->methods.filter.output_port_disconnected) {
977 ret = bt_private_component_class_filter_set_output_port_disconnected_method(
978 flt_comp_class,
979 cc_full_descr->methods.filter.output_port_disconnected);
980 if (ret) {
981 BT_LOGE_STR("Cannot set filter component class's \"output port disconnected\" method.");
982 status = BT_PLUGIN_STATUS_ERROR;
983 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
984 goto end;
985 }
986 }
987
988 if (cc_full_descr->methods.filter.notif_iter_init) {
989 ret = bt_private_component_class_filter_set_notification_iterator_init_method(
990 flt_comp_class,
991 cc_full_descr->methods.filter.notif_iter_init);
55bb57e0 992 if (ret) {
3fe0bf43 993 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
55bb57e0 994 status = BT_PLUGIN_STATUS_ERROR;
d94d92ac 995 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
55bb57e0
PP
996 goto end;
997 }
998 }
999
d94d92ac
PP
1000 if (cc_full_descr->methods.filter.notif_iter_finalize) {
1001 ret = bt_private_component_class_filter_set_notification_iterator_finalize_method(
1002 flt_comp_class,
1003 cc_full_descr->methods.filter.notif_iter_finalize);
55bb57e0 1004 if (ret) {
3fe0bf43 1005 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
55bb57e0 1006 status = BT_PLUGIN_STATUS_ERROR;
d94d92ac 1007 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
55bb57e0
PP
1008 goto end;
1009 }
1010 }
d94d92ac 1011
55bb57e0
PP
1012 break;
1013 case BT_COMPONENT_CLASS_TYPE_SINK:
d94d92ac
PP
1014 if (cc_full_descr->methods.sink.init) {
1015 ret = bt_private_component_class_sink_set_init_method(
1016 sink_comp_class,
1017 cc_full_descr->methods.sink.init);
1018 if (ret) {
1019 BT_LOGE_STR("Cannot set sink component class's initialization method.");
1020 status = BT_PLUGIN_STATUS_ERROR;
1021 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1022 goto end;
1023 }
1024 }
1025
1026 if (cc_full_descr->methods.sink.finalize) {
1027 ret = bt_private_component_class_sink_set_finalize_method(
1028 sink_comp_class,
1029 cc_full_descr->methods.sink.finalize);
1030 if (ret) {
1031 BT_LOGE_STR("Cannot set sink component class's finalization method.");
1032 status = BT_PLUGIN_STATUS_ERROR;
1033 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1034 goto end;
1035 }
1036 }
1037
1038 if (cc_full_descr->methods.sink.query) {
1039 ret = bt_private_component_class_sink_set_query_method(
1040 sink_comp_class,
1041 cc_full_descr->methods.sink.query);
1042 if (ret) {
1043 BT_LOGE_STR("Cannot set sink component class's query method.");
1044 status = BT_PLUGIN_STATUS_ERROR;
1045 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1046 goto end;
1047 }
1048 }
1049
1050 if (cc_full_descr->methods.sink.accept_input_port_connection) {
1051 ret = bt_private_component_class_sink_set_accept_input_port_connection_method(
1052 sink_comp_class,
1053 cc_full_descr->methods.sink.accept_input_port_connection);
1054 if (ret) {
1055 BT_LOGE_STR("Cannot set sink component class's \"accept input port connection\" method.");
1056 status = BT_PLUGIN_STATUS_ERROR;
1057 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1058 goto end;
1059 }
1060 }
1061
1062 if (cc_full_descr->methods.sink.input_port_connected) {
1063 ret = bt_private_component_class_sink_set_input_port_connected_method(
1064 sink_comp_class,
1065 cc_full_descr->methods.sink.input_port_connected);
1066 if (ret) {
1067 BT_LOGE_STR("Cannot set sink component class's \"input port connected\" method.");
1068 status = BT_PLUGIN_STATUS_ERROR;
1069 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1070 goto end;
1071 }
1072 }
1073
1074 if (cc_full_descr->methods.sink.input_port_disconnected) {
1075 ret = bt_private_component_class_sink_set_input_port_disconnected_method(
1076 sink_comp_class,
1077 cc_full_descr->methods.sink.input_port_disconnected);
1078 if (ret) {
1079 BT_LOGE_STR("Cannot set sink component class's \"input port disconnected\" method.");
1080 status = BT_PLUGIN_STATUS_ERROR;
1081 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1082 goto end;
1083 }
1084 }
1085
55bb57e0
PP
1086 break;
1087 default:
0fbb9a9f 1088 abort();
55bb57e0
PP
1089 }
1090
1091 /*
1092 * Add component class to the plugin object.
1093 *
1094 * This will call back
1095 * bt_plugin_so_on_add_component_class() so that we can
d94d92ac
PP
1096 * add a mapping in the component class list when we
1097 * know the component class is successfully added.
55bb57e0
PP
1098 */
1099 status = bt_plugin_add_component_class(plugin,
d94d92ac 1100 (void *) comp_class);
65300d60 1101 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
55bb57e0 1102 if (status < 0) {
3fe0bf43 1103 BT_LOGE("Cannot add component class to plugin.");
55bb57e0
PP
1104 goto end;
1105 }
1106 }
1107
55bb57e0
PP
1108end:
1109 g_array_free(comp_class_full_descriptors, TRUE);
1110 return status;
1111}
1112
1113static
1114struct bt_plugin *bt_plugin_so_create_empty(
1115 struct bt_plugin_so_shared_lib_handle *shared_lib_handle)
1116{
1117 struct bt_plugin *plugin;
1118 struct bt_plugin_so_spec_data *spec;
1119
1120 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO);
1121 if (!plugin) {
1122 goto error;
1123 }
1124
6fbd4105 1125 plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data;
55bb57e0
PP
1126 plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1);
1127 if (!plugin->spec_data) {
3fe0bf43 1128 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
55bb57e0
PP
1129 goto error;
1130 }
1131
1132 spec = plugin->spec_data;
65300d60 1133 spec->shared_lib_handle = bt_object_get_ref(shared_lib_handle);
55bb57e0
PP
1134 goto end;
1135
1136error:
65300d60 1137 BT_OBJECT_PUT_REF_AND_RESET(plugin);
55bb57e0
PP
1138
1139end:
1140 return plugin;
1141}
1142
52238017
MJ
1143static
1144size_t count_non_null_items_in_section(const void *begin, const void *end)
1145{
1146 size_t count = 0;
1147 const int * const *begin_int = (const int * const *) begin;
1148 const int * const *end_int = (const int * const *) end;
1149 const int * const *iter;
1150
1151 for (iter = begin_int; iter != end_int; iter++) {
1152 if (*iter) {
1153 count++;
1154 }
1155 }
1156
1157 return count;
1158}
1159
55bb57e0 1160static
a8ff38ef 1161struct bt_plugin_set *bt_plugin_so_create_all_from_sections(
55bb57e0
PP
1162 struct bt_plugin_so_shared_lib_handle *shared_lib_handle,
1163 struct __bt_plugin_descriptor const * const *descriptors_begin,
1164 struct __bt_plugin_descriptor const * const *descriptors_end,
1165 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
1166 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
1167 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
1168 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
1169 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
1170 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
1171{
1172 size_t descriptor_count;
1173 size_t attrs_count;
1174 size_t cc_descriptors_count;
1175 size_t cc_descr_attrs_count;
1176 size_t i;
a8ff38ef 1177 struct bt_plugin_set *plugin_set = NULL;
55bb57e0 1178
52238017
MJ
1179 descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end);
1180 attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end);
1181 cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end);
1182 cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end);
3fe0bf43
PP
1183
1184 BT_LOGD("Creating all SO plugins from sections: "
1185 "plugin-path=\"%s\", "
1186 "descr-begin-addr=%p, descr-end-addr=%p, "
1187 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1188 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1189 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1190 "descr-count=%zu, attrs-count=%zu, "
1191 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1192 shared_lib_handle->path ? shared_lib_handle->path->str : NULL,
1193 descriptors_begin, descriptors_end,
1194 attrs_begin, attrs_end,
1195 cc_descriptors_begin, cc_descriptors_end,
1196 cc_descr_attrs_begin, cc_descr_attrs_end,
1197 descriptor_count, attrs_count,
1198 cc_descriptors_count, cc_descr_attrs_count);
a8ff38ef
PP
1199 plugin_set = bt_plugin_set_create();
1200 if (!plugin_set) {
3fe0bf43 1201 BT_LOGE_STR("Cannot create empty plugin set.");
55bb57e0
PP
1202 goto error;
1203 }
1204
52238017 1205 for (i = 0; i < descriptors_end - descriptors_begin; i++) {
55bb57e0
PP
1206 enum bt_plugin_status status;
1207 const struct __bt_plugin_descriptor *descriptor =
1208 descriptors_begin[i];
1209 struct bt_plugin *plugin;
1210
52238017
MJ
1211 if (descriptor == NULL) {
1212 continue;
1213 }
1214
3fe0bf43
PP
1215 BT_LOGD("Creating plugin object for plugin: "
1216 "name=\"%s\", abi-major=%d, abi-minor=%d",
1217 descriptor->name, descriptor->major, descriptor->minor);
55bb57e0
PP
1218
1219 if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) {
50ad9320
PP
1220 /*
1221 * DEBUG-level logging because we're only
1222 * _trying_ to open this file as a compatible
1223 * Babeltrace plugin: if it's not, it's not an
1224 * error. And because this can be tried during
1225 * bt_plugin_create_all_from_dir(), it's not
1226 * even a warning.
1227 */
1228 BT_LOGD("Unknown ABI major version: abi-major=%d",
55bb57e0
PP
1229 descriptor->major);
1230 goto error;
1231 }
1232
1233 plugin = bt_plugin_so_create_empty(shared_lib_handle);
1234 if (!plugin) {
3fe0bf43 1235 BT_LOGE_STR("Cannot create empty shared library handle.");
55bb57e0
PP
1236 goto error;
1237 }
1238
1239 if (shared_lib_handle && shared_lib_handle->path) {
1240 bt_plugin_set_path(plugin, shared_lib_handle->path->str);
1241 }
1242
1243 status = bt_plugin_so_init(plugin, descriptor, attrs_begin,
1244 attrs_end, cc_descriptors_begin, cc_descriptors_end,
1245 cc_descr_attrs_begin, cc_descr_attrs_end);
1246 if (status < 0) {
50ad9320
PP
1247 /*
1248 * DEBUG-level logging because we're only
1249 * _trying_ to open this file as a compatible
1250 * Babeltrace plugin: if it's not, it's not an
1251 * error. And because this can be tried during
1252 * bt_plugin_create_all_from_dir(), it's not
1253 * even a warning.
1254 */
1255 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
65300d60 1256 BT_OBJECT_PUT_REF_AND_RESET(plugin);
55bb57e0
PP
1257 goto error;
1258 }
1259
a8ff38ef
PP
1260 /* Add to plugin set */
1261 bt_plugin_set_add_plugin(plugin_set, plugin);
65300d60 1262 bt_object_put_ref(plugin);
55bb57e0
PP
1263 }
1264
1265 goto end;
1266
1267error:
65300d60 1268 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
55bb57e0
PP
1269
1270end:
a8ff38ef 1271 return plugin_set;
55bb57e0
PP
1272}
1273
1274BT_HIDDEN
a8ff38ef 1275struct bt_plugin_set *bt_plugin_so_create_all_from_static(void)
55bb57e0 1276{
a8ff38ef 1277 struct bt_plugin_set *plugin_set = NULL;
55bb57e0
PP
1278 struct bt_plugin_so_shared_lib_handle *shared_lib_handle =
1279 bt_plugin_so_shared_lib_handle_create(NULL);
1280
1281 if (!shared_lib_handle) {
1282 goto end;
1283 }
1284
3fe0bf43 1285 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
a8ff38ef 1286 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
52238017
MJ
1287 __bt_get_begin_section_plugin_descriptors(),
1288 __bt_get_end_section_plugin_descriptors(),
1289 __bt_get_begin_section_plugin_descriptor_attributes(),
1290 __bt_get_end_section_plugin_descriptor_attributes(),
1291 __bt_get_begin_section_component_class_descriptors(),
1292 __bt_get_end_section_component_class_descriptors(),
1293 __bt_get_begin_section_component_class_descriptor_attributes(),
1294 __bt_get_end_section_component_class_descriptor_attributes());
55bb57e0
PP
1295
1296end:
65300d60 1297 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
55bb57e0 1298
a8ff38ef 1299 return plugin_set;
55bb57e0
PP
1300}
1301
1302BT_HIDDEN
a8ff38ef 1303struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path)
55bb57e0
PP
1304{
1305 size_t path_len;
a8ff38ef 1306 struct bt_plugin_set *plugin_set = NULL;
55bb57e0
PP
1307 struct __bt_plugin_descriptor const * const *descriptors_begin = NULL;
1308 struct __bt_plugin_descriptor const * const *descriptors_end = NULL;
1309 struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL;
1310 struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL;
1311 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL;
1312 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL;
1313 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL;
1314 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL;
52238017
MJ
1315 struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void);
1316 struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void);
1317 struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void);
1318 struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void);
1319 struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void);
1320 struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void);
1321 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void);
1322 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void);
c55a9f58 1323 bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE;
55bb57e0
PP
1324 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
1325
1326 if (!path) {
3fe0bf43 1327 BT_LOGW_STR("Invalid parameter: path is NULL.");
55bb57e0
PP
1328 goto end;
1329 }
1330
3fe0bf43 1331 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path);
55bb57e0
PP
1332 path_len = strlen(path);
1333 if (path_len <= PLUGIN_SUFFIX_LEN) {
3fe0bf43
PP
1334 BT_LOGW("Invalid parameter: path length is too short: "
1335 "path-length=%zu", path_len);
55bb57e0
PP
1336 goto end;
1337 }
1338
1339 path_len++;
1340 /*
1341 * Check if the file ends with a known plugin file type suffix (i.e. .so
1342 * or .la on Linux).
1343 */
1344 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
1345 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
1346 LIBTOOL_PLUGIN_SUFFIX_LEN);
1347 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
1348 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
1349 NATIVE_PLUGIN_SUFFIX_LEN);
1350 if (!is_shared_object && !is_libtool_wrapper) {
3fe0bf43
PP
1351 /* Name indicates this is not a plugin file; not an error */
1352 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path);
55bb57e0
PP
1353 goto end;
1354 }
1355
1356 shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path);
1357 if (!shared_lib_handle) {
50ad9320 1358 BT_LOGD_STR("Cannot create shared library handle.");
55bb57e0
PP
1359 goto end;
1360 }
1361
52238017
MJ
1362 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors",
1363 (gpointer *) &get_begin_section_plugin_descriptors)) {
1364 descriptors_begin = get_begin_section_plugin_descriptors();
1365 } else {
3fe0bf43
PP
1366 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1367 "symbol=\"%s\"", path,
52238017 1368 "__bt_get_begin_section_plugin_descriptors");
55bb57e0
PP
1369 goto end;
1370 }
1371
52238017
MJ
1372 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors",
1373 (gpointer *) &get_end_section_plugin_descriptors)) {
1374 descriptors_end = get_end_section_plugin_descriptors();
1375 } else {
3fe0bf43
PP
1376 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1377 "symbol=\"%s\"", path,
52238017 1378 "__bt_get_end_section_plugin_descriptors");
55bb57e0
PP
1379 goto end;
1380 }
1381
52238017
MJ
1382 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes",
1383 (gpointer *) &get_begin_section_plugin_descriptor_attributes)) {
1384 attrs_begin = get_begin_section_plugin_descriptor_attributes();
1385 } else {
3fe0bf43
PP
1386 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1387 "symbol=\"%s\"", path,
52238017 1388 "__bt_get_begin_section_plugin_descriptor_attributes");
55bb57e0
PP
1389 }
1390
52238017
MJ
1391 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes",
1392 (gpointer *) &get_end_section_plugin_descriptor_attributes)) {
1393 attrs_end = get_end_section_plugin_descriptor_attributes();
1394 } else {
3fe0bf43
PP
1395 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1396 "symbol=\"%s\"", path,
52238017 1397 "__bt_get_end_section_plugin_descriptor_attributes");
55bb57e0
PP
1398 }
1399
1400 if ((!!attrs_begin - !!attrs_end) != 0) {
3fe0bf43
PP
1401 BT_LOGD("Found section start or end symbol, but not both: "
1402 "path=\"%s\", symbol-start=\"%s\", "
1403 "symbol-end=\"%s\", symbol-start-addr=%p, "
1404 "symbol-end-addr=%p",
52238017
MJ
1405 path, "__bt_get_begin_section_plugin_descriptor_attributes",
1406 "__bt_get_end_section_plugin_descriptor_attributes",
3fe0bf43 1407 attrs_begin, attrs_end);
55bb57e0
PP
1408 goto end;
1409 }
1410
52238017
MJ
1411 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors",
1412 (gpointer *) &get_begin_section_component_class_descriptors)) {
1413 cc_descriptors_begin = get_begin_section_component_class_descriptors();
1414 } else {
3fe0bf43
PP
1415 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1416 "symbol=\"%s\"", path,
52238017 1417 "__bt_get_begin_section_component_class_descriptors");
55bb57e0
PP
1418 }
1419
52238017
MJ
1420 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors",
1421 (gpointer *) &get_end_section_component_class_descriptors)) {
1422 cc_descriptors_end = get_end_section_component_class_descriptors();
1423 } else {
3fe0bf43
PP
1424 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1425 "symbol=\"%s\"", path,
52238017 1426 "__bt_get_end_section_component_class_descriptors");
55bb57e0
PP
1427 }
1428
1429 if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) {
3fe0bf43
PP
1430 BT_LOGD("Found section start or end symbol, but not both: "
1431 "path=\"%s\", symbol-start=\"%s\", "
1432 "symbol-end=\"%s\", symbol-start-addr=%p, "
1433 "symbol-end-addr=%p",
52238017
MJ
1434 path, "__bt_get_begin_section_component_class_descriptors",
1435 "__bt_get_end_section_component_class_descriptors",
3fe0bf43 1436 cc_descriptors_begin, cc_descriptors_end);
55bb57e0
PP
1437 goto end;
1438 }
1439
52238017
MJ
1440 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes",
1441 (gpointer *) &get_begin_section_component_class_descriptor_attributes)) {
1442 cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes();
1443 } else {
3fe0bf43
PP
1444 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1445 "symbol=\"%s\"", path,
52238017 1446 "__bt_get_begin_section_component_class_descriptor_attributes");
55bb57e0
PP
1447 }
1448
52238017
MJ
1449 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes",
1450 (gpointer *) &get_end_section_component_class_descriptor_attributes)) {
1451 cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes();
1452 } else {
3fe0bf43
PP
1453 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1454 "symbol=\"%s\"", path,
52238017 1455 "__bt_get_end_section_component_class_descriptor_attributes");
55bb57e0
PP
1456 }
1457
1458 if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) {
3fe0bf43
PP
1459 BT_LOGD("Found section start or end symbol, but not both: "
1460 "path=\"%s\", symbol-start=\"%s\", "
1461 "symbol-end=\"%s\", symbol-start-addr=%p, "
1462 "symbol-end-addr=%p",
52238017
MJ
1463 path, "__bt_get_begin_section_component_class_descriptor_attributes",
1464 "__bt_get_end_section_component_class_descriptor_attributes",
3fe0bf43 1465 cc_descr_attrs_begin, cc_descr_attrs_end);
55bb57e0
PP
1466 goto end;
1467 }
1468
1469 /* Initialize plugin */
3fe0bf43 1470 BT_LOGD_STR("Initializing plugin object.");
a8ff38ef 1471 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
55bb57e0
PP
1472 descriptors_begin, descriptors_end, attrs_begin, attrs_end,
1473 cc_descriptors_begin, cc_descriptors_end,
1474 cc_descr_attrs_begin, cc_descr_attrs_end);
1475
1476end:
65300d60 1477 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
a8ff38ef 1478 return plugin_set;
55bb57e0
PP
1479}
1480
1481static
1482void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
1483 void *data)
1484{
bfa9a4be 1485 bt_list_del(&comp_class->node);
65300d60 1486 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
bfa9a4be 1487 BT_LOGV("Component class destroyed: removed entry from list: "
3fe0bf43 1488 "comp-cls-addr=%p", comp_class);
55bb57e0
PP
1489}
1490
1491BT_HIDDEN
3230ee6b 1492void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin,
55bb57e0
PP
1493 struct bt_component_class *comp_class)
1494{
55bb57e0
PP
1495 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
1496
f6ccaed9
PP
1497 BT_ASSERT(plugin->spec_data);
1498 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
55bb57e0 1499
bfa9a4be 1500 bt_list_add(&comp_class->node, &component_class_list);
65300d60 1501 comp_class->so_handle = bt_object_get_ref(spec->shared_lib_handle);
55bb57e0
PP
1502
1503 /* Add our custom destroy listener */
3230ee6b 1504 bt_component_class_add_destroy_listener(comp_class,
55bb57e0 1505 plugin_comp_class_destroy_listener, NULL);
55bb57e0 1506}
This page took 0.099378 seconds and 4 git commands to generate.