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