lib: remove "accept port" concept
[babeltrace.git] / src / lib / plugin / plugin-so.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@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
26 #define BT_LOG_TAG "LIB/PLUGIN-SO"
27 #include "lib/logging.h"
28
29 #include "common/assert.h"
30 #include "lib/assert-pre.h"
31 #include "compat/compiler.h"
32 #include <babeltrace2/plugin/plugin-dev.h>
33 #include "lib/graph/component-class.h"
34 #include <babeltrace2/graph/component-class.h>
35 #include <babeltrace2/graph/component-class-source.h>
36 #include <babeltrace2/graph/component-class-filter.h>
37 #include <babeltrace2/graph/component-class-sink.h>
38 #include <babeltrace2/types.h>
39 #include "common/list.h"
40 #include <string.h>
41 #include <stdlib.h>
42 #include <glib.h>
43 #include <gmodule.h>
44
45 #include "plugin.h"
46 #include "plugin-so.h"
47
48 #define NATIVE_PLUGIN_SUFFIX "." G_MODULE_SUFFIX
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 bt_max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \
54 sizeof(LIBTOOL_PLUGIN_SUFFIX))
55
56 BT_PLUGIN_MODULE();
57
58 /*
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.
63 *
64 * The list items are the component classes created with
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 *
70 * [component] --ref-> [component class]-> [shlib handle]
71 *
72 * It allows this use-case:
73 *
74 * my_plugins = bt_plugin_find_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 *
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.
87 */
88
89 static
90 BT_LIST_HEAD(component_class_list);
91
92 __attribute__((destructor)) static
93 void 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);
99 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
100 }
101
102 BT_LOGD_STR("Released references from all component classes to shared library handles.");
103 }
104
105 static inline
106 const char *bt_self_plugin_status_string(enum bt_self_plugin_status status)
107 {
108 switch (status) {
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";
115 default:
116 return "(unknown)";
117 }
118 }
119
120 static
121 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj)
122 {
123 struct bt_plugin_so_shared_lib_handle *shared_lib_handle;
124
125 BT_ASSERT(obj);
126 shared_lib_handle = container_of(obj,
127 struct bt_plugin_so_shared_lib_handle, base);
128 const char *path = shared_lib_handle->path ?
129 shared_lib_handle->path->str : NULL;
130
131 BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"",
132 shared_lib_handle, path);
133
134 if (shared_lib_handle->init_called && shared_lib_handle->exit) {
135 BT_LOGD_STR("Calling user's plugin exit function.");
136 shared_lib_handle->exit();
137 BT_LOGD_STR("User function returned.");
138 }
139
140 if (shared_lib_handle->module) {
141 #ifndef BT_DEBUG_MODE
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
152 BT_LOGI("Closing GModule: path=\"%s\"", path);
153
154 if (!g_module_close(shared_lib_handle->module)) {
155 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
156 g_module_error(), path);
157 }
158
159 shared_lib_handle->module = NULL;
160 #ifndef BT_DEBUG_MODE
161 } else {
162 BT_LOGI("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
163 "path=\"%s\"", path);
164 }
165 #endif
166 }
167
168 if (shared_lib_handle->path) {
169 g_string_free(shared_lib_handle->path, TRUE);
170 shared_lib_handle->path = NULL;
171 }
172
173 g_free(shared_lib_handle);
174 }
175
176 static
177 struct 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
182 BT_LOGI("Creating shared library handle: path=\"%s\"", path);
183 shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1);
184 if (!shared_lib_handle) {
185 BT_LOGE_STR("Failed to allocate one shared library handle.");
186 goto error;
187 }
188
189 bt_object_init_shared(&shared_lib_handle->base,
190 bt_plugin_so_shared_lib_handle_destroy);
191
192 if (!path) {
193 goto end;
194 }
195
196 shared_lib_handle->path = g_string_new(path);
197 if (!shared_lib_handle->path) {
198 BT_LOGE_STR("Failed to allocate a GString.");
199 goto error;
200 }
201
202 shared_lib_handle->module = g_module_open(path, G_MODULE_BIND_LOCAL);
203 if (!shared_lib_handle->module) {
204 /*
205 * INFO-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
208 * during bt_plugin_find_all_from_dir(), it's not even a
209 * warning.
210 */
211 BT_LOGI("Cannot open GModule: %s: path=\"%s\"",
212 g_module_error(), path);
213 goto error;
214 }
215
216 goto end;
217
218 error:
219 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
220
221 end:
222 if (shared_lib_handle) {
223 BT_LOGI("Created shared library handle: path=\"%s\", addr=%p",
224 path, shared_lib_handle);
225 }
226
227 return shared_lib_handle;
228 }
229
230 static
231 void 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
239 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
240 BT_ASSERT(spec);
241 BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle);
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 */
273 static
274 enum 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;
291 const char *help;
292
293 union {
294 struct {
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_output_port_connected_method output_port_connected;
299 bt_component_class_source_message_iterator_init_method msg_iter_init;
300 bt_component_class_source_message_iterator_finalize_method msg_iter_finalize;
301 bt_component_class_source_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin;
302 bt_component_class_source_message_iterator_seek_beginning_method msg_iter_seek_beginning;
303 bt_component_class_source_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin;
304 bt_component_class_source_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning;
305 } source;
306
307 struct {
308 bt_component_class_filter_init_method init;
309 bt_component_class_filter_finalize_method finalize;
310 bt_component_class_filter_query_method query;
311 bt_component_class_filter_input_port_connected_method input_port_connected;
312 bt_component_class_filter_output_port_connected_method output_port_connected;
313 bt_component_class_filter_message_iterator_init_method msg_iter_init;
314 bt_component_class_filter_message_iterator_finalize_method msg_iter_finalize;
315 bt_component_class_filter_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin;
316 bt_component_class_filter_message_iterator_seek_beginning_method msg_iter_seek_beginning;
317 bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin;
318 bt_component_class_filter_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning;
319 } filter;
320
321 struct {
322 bt_component_class_sink_init_method init;
323 bt_component_class_sink_finalize_method finalize;
324 bt_component_class_sink_query_method query;
325 bt_component_class_sink_input_port_connected_method input_port_connected;
326 bt_component_class_sink_graph_is_configured_method graph_is_configured;
327 } sink;
328 } methods;
329 };
330
331 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
332 struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr;
333 struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr;
334 struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr;
335 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
336 GArray *comp_class_full_descriptors;
337 size_t i;
338 int ret;
339
340 BT_LOGI("Initializing plugin object from descriptors found in sections: "
341 "plugin-addr=%p, plugin-path=\"%s\", "
342 "attrs-begin-addr=%p, attrs-end-addr=%p, "
343 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
344 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
345 plugin,
346 spec->shared_lib_handle->path ?
347 spec->shared_lib_handle->path->str : NULL,
348 attrs_begin, attrs_end,
349 cc_descriptors_begin, cc_descriptors_end,
350 cc_descr_attrs_begin, cc_descr_attrs_end);
351 comp_class_full_descriptors = g_array_new(FALSE, TRUE,
352 sizeof(struct comp_class_full_descriptor));
353 if (!comp_class_full_descriptors) {
354 BT_LOGE_STR("Failed to allocate a GArray.");
355 status = BT_PLUGIN_STATUS_ERROR;
356 goto end;
357 }
358
359 /* Set mandatory attributes */
360 spec->descriptor = descriptor;
361 bt_plugin_set_name(plugin, descriptor->name);
362
363 /*
364 * Find and set optional attributes attached to this plugin
365 * descriptor.
366 */
367 for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) {
368 const struct __bt_plugin_descriptor_attribute *cur_attr =
369 *cur_attr_ptr;
370
371 if (cur_attr == NULL) {
372 continue;
373 }
374
375 if (cur_attr->plugin_descriptor != descriptor) {
376 continue;
377 }
378
379 switch (cur_attr->type) {
380 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT:
381 spec->init = cur_attr->value.init;
382 break;
383 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT:
384 spec->shared_lib_handle->exit = cur_attr->value.exit;
385 break;
386 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR:
387 bt_plugin_set_author(plugin, cur_attr->value.author);
388 break;
389 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE:
390 bt_plugin_set_license(plugin, cur_attr->value.license);
391 break;
392 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
393 bt_plugin_set_description(plugin, cur_attr->value.description);
394 break;
395 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION:
396 bt_plugin_set_version(plugin,
397 (unsigned int) cur_attr->value.version.major,
398 (unsigned int) cur_attr->value.version.minor,
399 (unsigned int) cur_attr->value.version.patch,
400 cur_attr->value.version.extra);
401 break;
402 default:
403 /*
404 * WARN-level logging because this should not
405 * happen with the appropriate ABI version. If
406 * we're here, we know that for the reported
407 * version of the ABI, this attribute is
408 * unknown.
409 */
410 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
411 "plugin-path=\"%s\", plugin-name=\"%s\", "
412 "attr-type-name=\"%s\", attr-type-id=%d",
413 spec->shared_lib_handle->path ?
414 spec->shared_lib_handle->path->str :
415 NULL,
416 descriptor->name, cur_attr->type_name,
417 cur_attr->type);
418 break;
419 }
420 }
421
422 /*
423 * Find component class descriptors attached to this plugin
424 * descriptor and initialize corresponding full component class
425 * descriptors in the array.
426 */
427 for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) {
428 const struct __bt_plugin_component_class_descriptor *cur_cc_descr =
429 *cur_cc_descr_ptr;
430 struct comp_class_full_descriptor full_descriptor = {0};
431
432 if (cur_cc_descr == NULL) {
433 continue;
434 }
435
436 if (cur_cc_descr->plugin_descriptor != descriptor) {
437 continue;
438 }
439
440 full_descriptor.descriptor = cur_cc_descr;
441 g_array_append_val(comp_class_full_descriptors,
442 full_descriptor);
443 }
444
445 /*
446 * Find component class descriptor attributes attached to this
447 * plugin descriptor and update corresponding full component
448 * class descriptors in the array.
449 */
450 for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) {
451 const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr =
452 *cur_cc_descr_attr_ptr;
453 enum bt_component_class_type cc_type;
454
455 if (cur_cc_descr_attr == NULL) {
456 continue;
457 }
458
459 if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor !=
460 descriptor) {
461 continue;
462 }
463
464 cc_type = cur_cc_descr_attr->comp_class_descriptor->type;
465
466 /* Find the corresponding component class descriptor entry */
467 for (i = 0; i < comp_class_full_descriptors->len; i++) {
468 struct comp_class_full_descriptor *cc_full_descr =
469 &g_array_index(comp_class_full_descriptors,
470 struct comp_class_full_descriptor, i);
471
472 if (cur_cc_descr_attr->comp_class_descriptor !=
473 cc_full_descr->descriptor) {
474 continue;
475 }
476
477 switch (cur_cc_descr_attr->type) {
478 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
479 cc_full_descr->description =
480 cur_cc_descr_attr->value.description;
481 break;
482 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP:
483 cc_full_descr->help =
484 cur_cc_descr_attr->value.help;
485 break;
486 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD:
487 switch (cc_type) {
488 case BT_COMPONENT_CLASS_TYPE_SOURCE:
489 cc_full_descr->methods.source.init =
490 cur_cc_descr_attr->value.source_init_method;
491 break;
492 case BT_COMPONENT_CLASS_TYPE_FILTER:
493 cc_full_descr->methods.filter.init =
494 cur_cc_descr_attr->value.filter_init_method;
495 break;
496 case BT_COMPONENT_CLASS_TYPE_SINK:
497 cc_full_descr->methods.sink.init =
498 cur_cc_descr_attr->value.sink_init_method;
499 break;
500 default:
501 abort();
502 }
503 break;
504 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD:
505 switch (cc_type) {
506 case BT_COMPONENT_CLASS_TYPE_SOURCE:
507 cc_full_descr->methods.source.finalize =
508 cur_cc_descr_attr->value.source_finalize_method;
509 break;
510 case BT_COMPONENT_CLASS_TYPE_FILTER:
511 cc_full_descr->methods.filter.finalize =
512 cur_cc_descr_attr->value.filter_finalize_method;
513 break;
514 case BT_COMPONENT_CLASS_TYPE_SINK:
515 cc_full_descr->methods.sink.finalize =
516 cur_cc_descr_attr->value.sink_finalize_method;
517 break;
518 default:
519 abort();
520 }
521 break;
522 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD:
523 switch (cc_type) {
524 case BT_COMPONENT_CLASS_TYPE_SOURCE:
525 cc_full_descr->methods.source.query =
526 cur_cc_descr_attr->value.source_query_method;
527 break;
528 case BT_COMPONENT_CLASS_TYPE_FILTER:
529 cc_full_descr->methods.filter.query =
530 cur_cc_descr_attr->value.filter_query_method;
531 break;
532 case BT_COMPONENT_CLASS_TYPE_SINK:
533 cc_full_descr->methods.sink.query =
534 cur_cc_descr_attr->value.sink_query_method;
535 break;
536 default:
537 abort();
538 }
539 break;
540 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD:
541 switch (cc_type) {
542 case BT_COMPONENT_CLASS_TYPE_FILTER:
543 cc_full_descr->methods.filter.input_port_connected =
544 cur_cc_descr_attr->value.filter_input_port_connected_method;
545 break;
546 case BT_COMPONENT_CLASS_TYPE_SINK:
547 cc_full_descr->methods.sink.input_port_connected =
548 cur_cc_descr_attr->value.sink_input_port_connected_method;
549 break;
550 default:
551 abort();
552 }
553 break;
554 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD:
555 switch (cc_type) {
556 case BT_COMPONENT_CLASS_TYPE_SOURCE:
557 cc_full_descr->methods.source.output_port_connected =
558 cur_cc_descr_attr->value.source_output_port_connected_method;
559 break;
560 case BT_COMPONENT_CLASS_TYPE_FILTER:
561 cc_full_descr->methods.filter.output_port_connected =
562 cur_cc_descr_attr->value.filter_output_port_connected_method;
563 break;
564 default:
565 abort();
566 }
567 break;
568 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD:
569 switch (cc_type) {
570 case BT_COMPONENT_CLASS_TYPE_SINK:
571 cc_full_descr->methods.sink.graph_is_configured =
572 cur_cc_descr_attr->value.sink_graph_is_configured_method;
573 break;
574 default:
575 abort();
576 }
577 break;
578 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INIT_METHOD:
579 switch (cc_type) {
580 case BT_COMPONENT_CLASS_TYPE_SOURCE:
581 cc_full_descr->methods.source.msg_iter_init =
582 cur_cc_descr_attr->value.source_msg_iter_init_method;
583 break;
584 case BT_COMPONENT_CLASS_TYPE_FILTER:
585 cc_full_descr->methods.filter.msg_iter_init =
586 cur_cc_descr_attr->value.filter_msg_iter_init_method;
587 break;
588 default:
589 abort();
590 }
591 break;
592 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD:
593 switch (cc_type) {
594 case BT_COMPONENT_CLASS_TYPE_SOURCE:
595 cc_full_descr->methods.source.msg_iter_finalize =
596 cur_cc_descr_attr->value.source_msg_iter_finalize_method;
597 break;
598 case BT_COMPONENT_CLASS_TYPE_FILTER:
599 cc_full_descr->methods.filter.msg_iter_finalize =
600 cur_cc_descr_attr->value.filter_msg_iter_finalize_method;
601 break;
602 default:
603 abort();
604 }
605 break;
606 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD:
607 switch (cc_type) {
608 case BT_COMPONENT_CLASS_TYPE_SOURCE:
609 cc_full_descr->methods.source.msg_iter_seek_ns_from_origin =
610 cur_cc_descr_attr->value.source_msg_iter_seek_ns_from_origin_method;
611 break;
612 case BT_COMPONENT_CLASS_TYPE_FILTER:
613 cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin =
614 cur_cc_descr_attr->value.filter_msg_iter_seek_ns_from_origin_method;
615 break;
616 default:
617 abort();
618 }
619 break;
620 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD:
621 switch (cc_type) {
622 case BT_COMPONENT_CLASS_TYPE_SOURCE:
623 cc_full_descr->methods.source.msg_iter_seek_beginning =
624 cur_cc_descr_attr->value.source_msg_iter_seek_beginning_method;
625 break;
626 case BT_COMPONENT_CLASS_TYPE_FILTER:
627 cc_full_descr->methods.filter.msg_iter_seek_beginning =
628 cur_cc_descr_attr->value.filter_msg_iter_seek_beginning_method;
629 break;
630 default:
631 abort();
632 }
633 break;
634 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD:
635 switch (cc_type) {
636 case BT_COMPONENT_CLASS_TYPE_SOURCE:
637 cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin =
638 cur_cc_descr_attr->value.source_msg_iter_can_seek_ns_from_origin_method;
639 break;
640 case BT_COMPONENT_CLASS_TYPE_FILTER:
641 cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin =
642 cur_cc_descr_attr->value.filter_msg_iter_can_seek_ns_from_origin_method;
643 break;
644 default:
645 abort();
646 }
647 break;
648 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD:
649 switch (cc_type) {
650 case BT_COMPONENT_CLASS_TYPE_SOURCE:
651 cc_full_descr->methods.source.msg_iter_can_seek_beginning =
652 cur_cc_descr_attr->value.source_msg_iter_can_seek_beginning_method;
653 break;
654 case BT_COMPONENT_CLASS_TYPE_FILTER:
655 cc_full_descr->methods.filter.msg_iter_can_seek_beginning =
656 cur_cc_descr_attr->value.filter_msg_iter_can_seek_beginning_method;
657 break;
658 default:
659 abort();
660 }
661 break;
662 default:
663 /*
664 * WARN-level logging because this
665 * should not happen with the
666 * appropriate ABI version. If we're
667 * here, we know that for the reported
668 * version of the ABI, this attribute is
669 * unknown.
670 */
671 BT_LOGW("Ignoring unknown component class descriptor attribute: "
672 "plugin-path=\"%s\", "
673 "plugin-name=\"%s\", "
674 "comp-class-name=\"%s\", "
675 "comp-class-type=%s, "
676 "attr-type-name=\"%s\", "
677 "attr-type-id=%d",
678 spec->shared_lib_handle->path ?
679 spec->shared_lib_handle->path->str :
680 NULL,
681 descriptor->name,
682 cur_cc_descr_attr->comp_class_descriptor->name,
683 bt_component_class_type_string(
684 cur_cc_descr_attr->comp_class_descriptor->type),
685 cur_cc_descr_attr->type_name,
686 cur_cc_descr_attr->type);
687 break;
688 }
689 }
690 }
691
692 /* Initialize plugin */
693 if (spec->init) {
694 enum bt_self_plugin_status init_status;
695
696 BT_LOGD_STR("Calling user's plugin initialization function.");
697 init_status = spec->init((void *) plugin);
698 BT_LOGD("User function returned: %s",
699 bt_self_plugin_status_string(init_status));
700
701 if (init_status < 0) {
702 BT_LOGW_STR("User's plugin initialization function failed.");
703 status = BT_PLUGIN_STATUS_ERROR;
704 goto end;
705 }
706 }
707
708 spec->shared_lib_handle->init_called = BT_TRUE;
709
710 /* Add described component classes to plugin */
711 for (i = 0; i < comp_class_full_descriptors->len; i++) {
712 struct comp_class_full_descriptor *cc_full_descr =
713 &g_array_index(comp_class_full_descriptors,
714 struct comp_class_full_descriptor, i);
715 struct bt_component_class *comp_class = NULL;
716 struct bt_component_class_source *src_comp_class = NULL;
717 struct bt_component_class_filter *flt_comp_class = NULL;
718 struct bt_component_class_sink *sink_comp_class = NULL;
719
720 BT_LOGI("Creating and setting properties of plugin's component class: "
721 "plugin-path=\"%s\", plugin-name=\"%s\", "
722 "comp-class-name=\"%s\", comp-class-type=%s",
723 spec->shared_lib_handle->path ?
724 spec->shared_lib_handle->path->str :
725 NULL,
726 descriptor->name,
727 cc_full_descr->descriptor->name,
728 bt_component_class_type_string(
729 cc_full_descr->descriptor->type));
730
731 switch (cc_full_descr->descriptor->type) {
732 case BT_COMPONENT_CLASS_TYPE_SOURCE:
733 src_comp_class = bt_component_class_source_create(
734 cc_full_descr->descriptor->name,
735 cc_full_descr->descriptor->methods.source.msg_iter_next);
736 comp_class = bt_component_class_source_as_component_class(
737 src_comp_class);
738 break;
739 case BT_COMPONENT_CLASS_TYPE_FILTER:
740 flt_comp_class = bt_component_class_filter_create(
741 cc_full_descr->descriptor->name,
742 cc_full_descr->descriptor->methods.source.msg_iter_next);
743 comp_class = bt_component_class_filter_as_component_class(
744 flt_comp_class);
745 break;
746 case BT_COMPONENT_CLASS_TYPE_SINK:
747 sink_comp_class = bt_component_class_sink_create(
748 cc_full_descr->descriptor->name,
749 cc_full_descr->descriptor->methods.sink.consume);
750 comp_class = bt_component_class_sink_as_component_class(
751 sink_comp_class);
752 break;
753 default:
754 /*
755 * WARN-level logging because this should not
756 * happen with the appropriate ABI version. If
757 * we're here, we know that for the reported
758 * version of the ABI, this component class type
759 * is unknown.
760 */
761 BT_LOGW("Ignoring unknown component class type: "
762 "plugin-path=\"%s\", plugin-name=\"%s\", "
763 "comp-class-name=\"%s\", comp-class-type=%d",
764 spec->shared_lib_handle->path->str ?
765 spec->shared_lib_handle->path->str :
766 NULL,
767 descriptor->name,
768 cc_full_descr->descriptor->name,
769 cc_full_descr->descriptor->type);
770 continue;
771 }
772
773 if (!comp_class) {
774 BT_LOGE_STR("Cannot create component class.");
775 status = BT_PLUGIN_STATUS_ERROR;
776 goto end;
777 }
778
779 if (cc_full_descr->description) {
780 ret = bt_component_class_set_description(
781 comp_class, cc_full_descr->description);
782 if (ret) {
783 BT_LOGE_STR("Cannot set component class's description.");
784 status = BT_PLUGIN_STATUS_ERROR;
785 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
786 goto end;
787 }
788 }
789
790 if (cc_full_descr->help) {
791 ret = bt_component_class_set_help(comp_class,
792 cc_full_descr->help);
793 if (ret) {
794 BT_LOGE_STR("Cannot set component class's help string.");
795 status = BT_PLUGIN_STATUS_ERROR;
796 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
797 goto end;
798 }
799 }
800
801 switch (cc_full_descr->descriptor->type) {
802 case BT_COMPONENT_CLASS_TYPE_SOURCE:
803 if (cc_full_descr->methods.source.init) {
804 ret = bt_component_class_source_set_init_method(
805 src_comp_class,
806 cc_full_descr->methods.source.init);
807 if (ret) {
808 BT_LOGE_STR("Cannot set source component class's initialization method.");
809 status = BT_PLUGIN_STATUS_ERROR;
810 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
811 goto end;
812 }
813 }
814
815 if (cc_full_descr->methods.source.finalize) {
816 ret = bt_component_class_source_set_finalize_method(
817 src_comp_class,
818 cc_full_descr->methods.source.finalize);
819 if (ret) {
820 BT_LOGE_STR("Cannot set source component class's finalization method.");
821 status = BT_PLUGIN_STATUS_ERROR;
822 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
823 goto end;
824 }
825 }
826
827 if (cc_full_descr->methods.source.query) {
828 ret = bt_component_class_source_set_query_method(
829 src_comp_class,
830 cc_full_descr->methods.source.query);
831 if (ret) {
832 BT_LOGE_STR("Cannot set source component class's query method.");
833 status = BT_PLUGIN_STATUS_ERROR;
834 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
835 goto end;
836 }
837 }
838
839 if (cc_full_descr->methods.source.output_port_connected) {
840 ret = bt_component_class_source_set_output_port_connected_method(
841 src_comp_class,
842 cc_full_descr->methods.source.output_port_connected);
843 if (ret) {
844 BT_LOGE_STR("Cannot set source component class's \"output port connected\" method.");
845 status = BT_PLUGIN_STATUS_ERROR;
846 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
847 goto end;
848 }
849 }
850
851 if (cc_full_descr->methods.source.msg_iter_init) {
852 ret = bt_component_class_source_set_message_iterator_init_method(
853 src_comp_class,
854 cc_full_descr->methods.source.msg_iter_init);
855 if (ret) {
856 BT_LOGE_STR("Cannot set source component class's message iterator initialization method.");
857 status = BT_PLUGIN_STATUS_ERROR;
858 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
859 goto end;
860 }
861 }
862
863 if (cc_full_descr->methods.source.msg_iter_finalize) {
864 ret = bt_component_class_source_set_message_iterator_finalize_method(
865 src_comp_class,
866 cc_full_descr->methods.source.msg_iter_finalize);
867 if (ret) {
868 BT_LOGE_STR("Cannot set source component class's message iterator finalization method.");
869 status = BT_PLUGIN_STATUS_ERROR;
870 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
871 goto end;
872 }
873 }
874
875 if (cc_full_descr->methods.source.msg_iter_seek_ns_from_origin) {
876 ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_method(
877 src_comp_class,
878 cc_full_descr->methods.source.msg_iter_seek_ns_from_origin);
879 if (ret) {
880 BT_LOGE_STR("Cannot set source component class's message iterator \"seek nanoseconds from origin\" method.");
881 status = BT_PLUGIN_STATUS_ERROR;
882 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
883 goto end;
884 }
885 }
886
887 if (cc_full_descr->methods.source.msg_iter_seek_beginning) {
888 ret = bt_component_class_source_set_message_iterator_seek_beginning_method(
889 src_comp_class,
890 cc_full_descr->methods.source.msg_iter_seek_beginning);
891 if (ret) {
892 BT_LOGE_STR("Cannot set source component class's message iterator \"seek beginning\" method.");
893 status = BT_PLUGIN_STATUS_ERROR;
894 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
895 goto end;
896 }
897 }
898
899 if (cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin) {
900 ret = bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method(
901 src_comp_class,
902 cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin);
903 if (ret) {
904 BT_LOGE_STR("Cannot set source component class's message iterator \"can seek nanoseconds from origin\" method.");
905 status = BT_PLUGIN_STATUS_ERROR;
906 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
907 goto end;
908 }
909 }
910
911 if (cc_full_descr->methods.source.msg_iter_can_seek_beginning) {
912 ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method(
913 src_comp_class,
914 cc_full_descr->methods.source.msg_iter_can_seek_beginning);
915 if (ret) {
916 BT_LOGE_STR("Cannot set source component class's message iterator \"can seek beginning\" method.");
917 status = BT_PLUGIN_STATUS_ERROR;
918 BT_OBJECT_PUT_REF_AND_RESET(src_comp_class);
919 goto end;
920 }
921 }
922
923 break;
924 case BT_COMPONENT_CLASS_TYPE_FILTER:
925 if (cc_full_descr->methods.filter.init) {
926 ret = bt_component_class_filter_set_init_method(
927 flt_comp_class,
928 cc_full_descr->methods.filter.init);
929 if (ret) {
930 BT_LOGE_STR("Cannot set filter component class's initialization 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.finalize) {
938 ret = bt_component_class_filter_set_finalize_method(
939 flt_comp_class,
940 cc_full_descr->methods.filter.finalize);
941 if (ret) {
942 BT_LOGE_STR("Cannot set filter component class's finalization 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.query) {
950 ret = bt_component_class_filter_set_query_method(
951 flt_comp_class,
952 cc_full_descr->methods.filter.query);
953 if (ret) {
954 BT_LOGE_STR("Cannot set filter component class's query 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_connected) {
962 ret = bt_component_class_filter_set_input_port_connected_method(
963 flt_comp_class,
964 cc_full_descr->methods.filter.input_port_connected);
965 if (ret) {
966 BT_LOGE_STR("Cannot set filter component class's \"input port connected\" 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_connected) {
974 ret = bt_component_class_filter_set_output_port_connected_method(
975 flt_comp_class,
976 cc_full_descr->methods.filter.output_port_connected);
977 if (ret) {
978 BT_LOGE_STR("Cannot set filter component class's \"output port connected\" 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.msg_iter_init) {
986 ret = bt_component_class_filter_set_message_iterator_init_method(
987 flt_comp_class,
988 cc_full_descr->methods.filter.msg_iter_init);
989 if (ret) {
990 BT_LOGE_STR("Cannot set filter component class's message iterator initialization method.");
991 status = BT_PLUGIN_STATUS_ERROR;
992 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
993 goto end;
994 }
995 }
996
997 if (cc_full_descr->methods.filter.msg_iter_finalize) {
998 ret = bt_component_class_filter_set_message_iterator_finalize_method(
999 flt_comp_class,
1000 cc_full_descr->methods.filter.msg_iter_finalize);
1001 if (ret) {
1002 BT_LOGE_STR("Cannot set filter component class's message iterator finalization method.");
1003 status = BT_PLUGIN_STATUS_ERROR;
1004 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1005 goto end;
1006 }
1007 }
1008
1009 if (cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin) {
1010 ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method(
1011 flt_comp_class,
1012 cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin);
1013 if (ret) {
1014 BT_LOGE_STR("Cannot set filter component class's message iterator \"seek nanoseconds from origin\" method.");
1015 status = BT_PLUGIN_STATUS_ERROR;
1016 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1017 goto end;
1018 }
1019 }
1020
1021 if (cc_full_descr->methods.filter.msg_iter_seek_beginning) {
1022 ret = bt_component_class_filter_set_message_iterator_seek_beginning_method(
1023 flt_comp_class,
1024 cc_full_descr->methods.filter.msg_iter_seek_beginning);
1025 if (ret) {
1026 BT_LOGE_STR("Cannot set filter component class's message iterator \"seek beginning\" method.");
1027 status = BT_PLUGIN_STATUS_ERROR;
1028 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1029 goto end;
1030 }
1031 }
1032
1033 if (cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin) {
1034 ret = bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method(
1035 flt_comp_class,
1036 cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin);
1037 if (ret) {
1038 BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek nanoseconds from origin\" method.");
1039 status = BT_PLUGIN_STATUS_ERROR;
1040 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1041 goto end;
1042 }
1043 }
1044
1045 if (cc_full_descr->methods.filter.msg_iter_can_seek_beginning) {
1046 ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method(
1047 flt_comp_class,
1048 cc_full_descr->methods.filter.msg_iter_can_seek_beginning);
1049 if (ret) {
1050 BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek beginning\" method.");
1051 status = BT_PLUGIN_STATUS_ERROR;
1052 BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class);
1053 goto end;
1054 }
1055 }
1056
1057 break;
1058 case BT_COMPONENT_CLASS_TYPE_SINK:
1059 if (cc_full_descr->methods.sink.init) {
1060 ret = bt_component_class_sink_set_init_method(
1061 sink_comp_class,
1062 cc_full_descr->methods.sink.init);
1063 if (ret) {
1064 BT_LOGE_STR("Cannot set sink component class's initialization 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.finalize) {
1072 ret = bt_component_class_sink_set_finalize_method(
1073 sink_comp_class,
1074 cc_full_descr->methods.sink.finalize);
1075 if (ret) {
1076 BT_LOGE_STR("Cannot set sink component class's finalization method.");
1077 status = BT_PLUGIN_STATUS_ERROR;
1078 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1079 goto end;
1080 }
1081 }
1082
1083 if (cc_full_descr->methods.sink.query) {
1084 ret = bt_component_class_sink_set_query_method(
1085 sink_comp_class,
1086 cc_full_descr->methods.sink.query);
1087 if (ret) {
1088 BT_LOGE_STR("Cannot set sink component class's query method.");
1089 status = BT_PLUGIN_STATUS_ERROR;
1090 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1091 goto end;
1092 }
1093 }
1094
1095 if (cc_full_descr->methods.sink.input_port_connected) {
1096 ret = bt_component_class_sink_set_input_port_connected_method(
1097 sink_comp_class,
1098 cc_full_descr->methods.sink.input_port_connected);
1099 if (ret) {
1100 BT_LOGE_STR("Cannot set sink component class's \"input port connected\" method.");
1101 status = BT_PLUGIN_STATUS_ERROR;
1102 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1103 goto end;
1104 }
1105 }
1106
1107 if (cc_full_descr->methods.sink.graph_is_configured) {
1108 ret = bt_component_class_sink_set_graph_is_configured_method(
1109 sink_comp_class,
1110 cc_full_descr->methods.sink.graph_is_configured);
1111 if (ret) {
1112 BT_LOGE_STR("Cannot set sink component class's \"graph is configured\" method.");
1113 status = BT_PLUGIN_STATUS_ERROR;
1114 BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class);
1115 goto end;
1116 }
1117 }
1118
1119 break;
1120 default:
1121 abort();
1122 }
1123
1124 /*
1125 * Add component class to the plugin object.
1126 *
1127 * This will call back
1128 * bt_plugin_so_on_add_component_class() so that we can
1129 * add a mapping in the component class list when we
1130 * know the component class is successfully added.
1131 */
1132 status = bt_plugin_add_component_class(plugin,
1133 (void *) comp_class);
1134 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
1135 if (status < 0) {
1136 BT_LOGE("Cannot add component class to plugin.");
1137 goto end;
1138 }
1139 }
1140
1141 end:
1142 g_array_free(comp_class_full_descriptors, TRUE);
1143 return status;
1144 }
1145
1146 static
1147 struct bt_plugin *bt_plugin_so_create_empty(
1148 struct bt_plugin_so_shared_lib_handle *shared_lib_handle)
1149 {
1150 struct bt_plugin *plugin;
1151 struct bt_plugin_so_spec_data *spec;
1152
1153 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO);
1154 if (!plugin) {
1155 goto error;
1156 }
1157
1158 plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data;
1159 plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1);
1160 if (!plugin->spec_data) {
1161 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
1162 goto error;
1163 }
1164
1165 spec = plugin->spec_data;
1166 spec->shared_lib_handle = shared_lib_handle;
1167 bt_object_get_no_null_check(spec->shared_lib_handle);
1168 goto end;
1169
1170 error:
1171 BT_OBJECT_PUT_REF_AND_RESET(plugin);
1172
1173 end:
1174 return plugin;
1175 }
1176
1177 static
1178 size_t count_non_null_items_in_section(const void *begin, const void *end)
1179 {
1180 size_t count = 0;
1181 const int * const *begin_int = (const int * const *) begin;
1182 const int * const *end_int = (const int * const *) end;
1183 const int * const *iter;
1184
1185 for (iter = begin_int; iter != end_int; iter++) {
1186 if (*iter) {
1187 count++;
1188 }
1189 }
1190
1191 return count;
1192 }
1193
1194 static
1195 struct bt_plugin_set *bt_plugin_so_create_all_from_sections(
1196 struct bt_plugin_so_shared_lib_handle *shared_lib_handle,
1197 struct __bt_plugin_descriptor const * const *descriptors_begin,
1198 struct __bt_plugin_descriptor const * const *descriptors_end,
1199 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
1200 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
1201 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
1202 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
1203 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
1204 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
1205 {
1206 size_t descriptor_count;
1207 size_t attrs_count;
1208 size_t cc_descriptors_count;
1209 size_t cc_descr_attrs_count;
1210 size_t i;
1211 struct bt_plugin_set *plugin_set = NULL;
1212
1213 descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end);
1214 attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end);
1215 cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end);
1216 cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end);
1217
1218 BT_LOGI("Creating all SO plugins from sections: "
1219 "plugin-path=\"%s\", "
1220 "descr-begin-addr=%p, descr-end-addr=%p, "
1221 "attrs-begin-addr=%p, attrs-end-addr=%p, "
1222 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
1223 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
1224 "descr-count=%zu, attrs-count=%zu, "
1225 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
1226 shared_lib_handle->path ? shared_lib_handle->path->str : NULL,
1227 descriptors_begin, descriptors_end,
1228 attrs_begin, attrs_end,
1229 cc_descriptors_begin, cc_descriptors_end,
1230 cc_descr_attrs_begin, cc_descr_attrs_end,
1231 descriptor_count, attrs_count,
1232 cc_descriptors_count, cc_descr_attrs_count);
1233 plugin_set = bt_plugin_set_create();
1234 if (!plugin_set) {
1235 BT_LOGE_STR("Cannot create empty plugin set.");
1236 goto error;
1237 }
1238
1239 for (i = 0; i < descriptors_end - descriptors_begin; i++) {
1240 enum bt_plugin_status status;
1241 const struct __bt_plugin_descriptor *descriptor =
1242 descriptors_begin[i];
1243 struct bt_plugin *plugin;
1244
1245 if (descriptor == NULL) {
1246 continue;
1247 }
1248
1249 BT_LOGI("Creating plugin object for plugin: "
1250 "name=\"%s\", abi-major=%d, abi-minor=%d",
1251 descriptor->name, descriptor->major, descriptor->minor);
1252
1253 if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) {
1254 /*
1255 * INFO-level logging because we're only
1256 * _trying_ to open this file as a compatible
1257 * Babeltrace plugin: if it's not, it's not an
1258 * error. And because this can be tried during
1259 * bt_plugin_find_all_from_dir(), it's not even
1260 * a warning.
1261 */
1262 BT_LOGI("Unknown ABI major version: abi-major=%d",
1263 descriptor->major);
1264 goto error;
1265 }
1266
1267 plugin = bt_plugin_so_create_empty(shared_lib_handle);
1268 if (!plugin) {
1269 BT_LOGE_STR("Cannot create empty shared library handle.");
1270 goto error;
1271 }
1272
1273 if (shared_lib_handle && shared_lib_handle->path) {
1274 bt_plugin_set_path(plugin, shared_lib_handle->path->str);
1275 }
1276
1277 status = bt_plugin_so_init(plugin, descriptor, attrs_begin,
1278 attrs_end, cc_descriptors_begin, cc_descriptors_end,
1279 cc_descr_attrs_begin, cc_descr_attrs_end);
1280 if (status < 0) {
1281 /*
1282 * DEBUG-level logging because we're only
1283 * _trying_ to open this file as a compatible
1284 * Babeltrace plugin: if it's not, it's not an
1285 * error. And because this can be tried during
1286 * bt_plugin_find_all_from_dir(), it's not
1287 * even a warning.
1288 */
1289 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
1290 BT_OBJECT_PUT_REF_AND_RESET(plugin);
1291 goto error;
1292 }
1293
1294 /* Add to plugin set */
1295 bt_plugin_set_add_plugin(plugin_set, plugin);
1296 bt_object_put_ref(plugin);
1297 }
1298
1299 goto end;
1300
1301 error:
1302 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
1303
1304 end:
1305 return plugin_set;
1306 }
1307
1308 BT_HIDDEN
1309 struct bt_plugin_set *bt_plugin_so_create_all_from_static(void)
1310 {
1311 struct bt_plugin_set *plugin_set = NULL;
1312 struct bt_plugin_so_shared_lib_handle *shared_lib_handle =
1313 bt_plugin_so_shared_lib_handle_create(NULL);
1314
1315 if (!shared_lib_handle) {
1316 goto end;
1317 }
1318
1319 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
1320 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
1321 __bt_get_begin_section_plugin_descriptors(),
1322 __bt_get_end_section_plugin_descriptors(),
1323 __bt_get_begin_section_plugin_descriptor_attributes(),
1324 __bt_get_end_section_plugin_descriptor_attributes(),
1325 __bt_get_begin_section_component_class_descriptors(),
1326 __bt_get_end_section_component_class_descriptors(),
1327 __bt_get_begin_section_component_class_descriptor_attributes(),
1328 __bt_get_end_section_component_class_descriptor_attributes());
1329
1330 end:
1331 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
1332
1333 return plugin_set;
1334 }
1335
1336 BT_HIDDEN
1337 struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path)
1338 {
1339 size_t path_len;
1340 struct bt_plugin_set *plugin_set = NULL;
1341 struct __bt_plugin_descriptor const * const *descriptors_begin = NULL;
1342 struct __bt_plugin_descriptor const * const *descriptors_end = NULL;
1343 struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL;
1344 struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL;
1345 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL;
1346 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL;
1347 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL;
1348 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL;
1349 struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void);
1350 struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void);
1351 struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void);
1352 struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void);
1353 struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void);
1354 struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void);
1355 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void);
1356 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void);
1357 bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE;
1358 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
1359
1360 BT_ASSERT(path);
1361 path_len = strlen(path);
1362 BT_ASSERT_PRE(path_len > PLUGIN_SUFFIX_LEN,
1363 "Path length is too short: path-length=%zu, min-length=%zu",
1364 path_len, PLUGIN_SUFFIX_LEN);
1365 BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path);
1366 path_len++;
1367
1368 /*
1369 * Check if the file ends with a known plugin file type suffix
1370 * (i.e. .so or .la on Linux).
1371 */
1372 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
1373 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
1374 LIBTOOL_PLUGIN_SUFFIX_LEN);
1375 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
1376 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
1377 NATIVE_PLUGIN_SUFFIX_LEN);
1378 if (!is_shared_object && !is_libtool_wrapper) {
1379 /* Name indicates this is not a plugin file; not an error */
1380 BT_LOGI("File is not a SO plugin file: path=\"%s\"", path);
1381 goto end;
1382 }
1383
1384 shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path);
1385 if (!shared_lib_handle) {
1386 /* bt_plugin_so_shared_lib_handle_create() logs more details */
1387 BT_LOGD_STR("Cannot create shared library handle.");
1388 goto end;
1389 }
1390
1391 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors",
1392 (gpointer *) &get_begin_section_plugin_descriptors)) {
1393 descriptors_begin = get_begin_section_plugin_descriptors();
1394 } else {
1395 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1396 "symbol=\"%s\"", path,
1397 "__bt_get_begin_section_plugin_descriptors");
1398 goto end;
1399 }
1400
1401 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors",
1402 (gpointer *) &get_end_section_plugin_descriptors)) {
1403 descriptors_end = get_end_section_plugin_descriptors();
1404 } else {
1405 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1406 "symbol=\"%s\"", path,
1407 "__bt_get_end_section_plugin_descriptors");
1408 goto end;
1409 }
1410
1411 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes",
1412 (gpointer *) &get_begin_section_plugin_descriptor_attributes)) {
1413 attrs_begin = get_begin_section_plugin_descriptor_attributes();
1414 } else {
1415 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1416 "symbol=\"%s\"", path,
1417 "__bt_get_begin_section_plugin_descriptor_attributes");
1418 }
1419
1420 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes",
1421 (gpointer *) &get_end_section_plugin_descriptor_attributes)) {
1422 attrs_end = get_end_section_plugin_descriptor_attributes();
1423 } else {
1424 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1425 "symbol=\"%s\"", path,
1426 "__bt_get_end_section_plugin_descriptor_attributes");
1427 }
1428
1429 if ((!!attrs_begin - !!attrs_end) != 0) {
1430 BT_LOGI("Found section start or end symbol, but not both: "
1431 "path=\"%s\", symbol-start=\"%s\", "
1432 "symbol-end=\"%s\", symbol-start-addr=%p, "
1433 "symbol-end-addr=%p",
1434 path, "__bt_get_begin_section_plugin_descriptor_attributes",
1435 "__bt_get_end_section_plugin_descriptor_attributes",
1436 attrs_begin, attrs_end);
1437 goto end;
1438 }
1439
1440 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors",
1441 (gpointer *) &get_begin_section_component_class_descriptors)) {
1442 cc_descriptors_begin = get_begin_section_component_class_descriptors();
1443 } else {
1444 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1445 "symbol=\"%s\"", path,
1446 "__bt_get_begin_section_component_class_descriptors");
1447 }
1448
1449 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors",
1450 (gpointer *) &get_end_section_component_class_descriptors)) {
1451 cc_descriptors_end = get_end_section_component_class_descriptors();
1452 } else {
1453 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1454 "symbol=\"%s\"", path,
1455 "__bt_get_end_section_component_class_descriptors");
1456 }
1457
1458 if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) {
1459 BT_LOGI("Found section start or end symbol, but not both: "
1460 "path=\"%s\", symbol-start=\"%s\", "
1461 "symbol-end=\"%s\", symbol-start-addr=%p, "
1462 "symbol-end-addr=%p",
1463 path, "__bt_get_begin_section_component_class_descriptors",
1464 "__bt_get_end_section_component_class_descriptors",
1465 cc_descriptors_begin, cc_descriptors_end);
1466 goto end;
1467 }
1468
1469 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes",
1470 (gpointer *) &get_begin_section_component_class_descriptor_attributes)) {
1471 cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes();
1472 } else {
1473 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1474 "symbol=\"%s\"", path,
1475 "__bt_get_begin_section_component_class_descriptor_attributes");
1476 }
1477
1478 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes",
1479 (gpointer *) &get_end_section_component_class_descriptor_attributes)) {
1480 cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes();
1481 } else {
1482 BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", "
1483 "symbol=\"%s\"", path,
1484 "__bt_get_end_section_component_class_descriptor_attributes");
1485 }
1486
1487 if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) {
1488 BT_LOGI("Found section start or end symbol, but not both: "
1489 "path=\"%s\", symbol-start=\"%s\", "
1490 "symbol-end=\"%s\", symbol-start-addr=%p, "
1491 "symbol-end-addr=%p",
1492 path, "__bt_get_begin_section_component_class_descriptor_attributes",
1493 "__bt_get_end_section_component_class_descriptor_attributes",
1494 cc_descr_attrs_begin, cc_descr_attrs_end);
1495 goto end;
1496 }
1497
1498 /* Initialize plugin */
1499 BT_LOGD_STR("Initializing plugin object.");
1500 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
1501 descriptors_begin, descriptors_end, attrs_begin, attrs_end,
1502 cc_descriptors_begin, cc_descriptors_end,
1503 cc_descr_attrs_begin, cc_descr_attrs_end);
1504
1505 end:
1506 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
1507 return plugin_set;
1508 }
1509
1510 static
1511 void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
1512 void *data)
1513 {
1514 bt_list_del(&comp_class->node);
1515 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
1516 BT_LOGD("Component class destroyed: removed entry from list: "
1517 "comp-cls-addr=%p", comp_class);
1518 }
1519
1520 void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin,
1521 struct bt_component_class *comp_class)
1522 {
1523 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
1524
1525 BT_ASSERT(plugin->spec_data);
1526 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
1527
1528 bt_list_add(&comp_class->node, &component_class_list);
1529 comp_class->so_handle = spec->shared_lib_handle;
1530 bt_object_get_no_null_check(comp_class->so_handle);
1531
1532 /* Add our custom destroy listener */
1533 bt_component_class_add_destroy_listener(comp_class,
1534 plugin_comp_class_destroy_listener, NULL);
1535 }
This page took 0.06577 seconds and 5 git commands to generate.