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