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