Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / lib / plugin / plugin-so.c
1 /*
2 * plugin-so.c
3 *
4 * Babeltrace Plugin (shared object)
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #define BT_LOG_TAG "PLUGIN-SO"
31 #include <babeltrace/lib-logging-internal.h>
32
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/object.h>
35 #include <babeltrace/plugin/plugin-internal.h>
36 #include <babeltrace/plugin/plugin-so-internal.h>
37 #include <babeltrace/plugin/plugin-dev.h>
38 #include <babeltrace/plugin/plugin-internal.h>
39 #include <babeltrace/graph/component-class-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/list-internal.h>
42 #include <babeltrace/assert-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_create_all_from_file("/path/to/my-plugin.so");
75 * // instantiate components from a plugin's component classes
76 * // put plugins and free my_plugins here
77 * // user code of instantiated components still exists
78 *
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 BT_LOGD_STR("Released references from all component classes to shared library handles.");
102 }
103
104 static
105 void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj)
106 {
107 struct bt_plugin_so_shared_lib_handle *shared_lib_handle;
108
109 BT_ASSERT(obj);
110 shared_lib_handle = container_of(obj,
111 struct bt_plugin_so_shared_lib_handle, base);
112 const char *path = shared_lib_handle->path ?
113 shared_lib_handle->path->str : NULL;
114
115 BT_LOGD("Destroying shared library handle: addr=%p, path=\"%s\"",
116 shared_lib_handle, path);
117
118 if (shared_lib_handle->init_called && shared_lib_handle->exit) {
119 enum bt_plugin_status status;
120
121 BT_LOGD_STR("Calling user's plugin exit function.");
122 status = shared_lib_handle->exit();
123 BT_LOGD("User function returned: %s",
124 bt_plugin_status_string(status));
125
126 if (status < 0) {
127 BT_LOGW("User's plugin exit function failed: "
128 "path=\"%s\"", path);
129 }
130 }
131
132 if (shared_lib_handle->module) {
133 #ifndef NDEBUG
134 /*
135 * Valgrind shows incomplete stack traces when
136 * dynamically loaded libraries are closed before it
137 * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug
138 * build to avoid this.
139 */
140 const char *var = getenv("BABELTRACE_NO_DLCLOSE");
141
142 if (!var || strcmp(var, "1") != 0) {
143 #endif
144 BT_LOGD("Closing GModule: path=\"%s\"", path);
145
146 if (!g_module_close(shared_lib_handle->module)) {
147 BT_LOGE("Cannot close GModule: %s: path=\"%s\"",
148 g_module_error(), path);
149 }
150 #ifndef NDEBUG
151 } else {
152 BT_LOGD("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: "
153 "path=\"%s\"", path);
154 }
155 #endif
156 }
157
158 if (shared_lib_handle->path) {
159 g_string_free(shared_lib_handle->path, TRUE);
160 }
161
162 g_free(shared_lib_handle);
163 }
164
165 static
166 struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create(
167 const char *path)
168 {
169 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
170
171 BT_LOGD("Creating shared library handle: path=\"%s\"", path);
172 shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1);
173 if (!shared_lib_handle) {
174 BT_LOGE_STR("Failed to allocate one shared library handle.");
175 goto error;
176 }
177
178 bt_object_init_shared(&shared_lib_handle->base,
179 bt_plugin_so_shared_lib_handle_destroy);
180
181 if (!path) {
182 goto end;
183 }
184
185 shared_lib_handle->path = g_string_new(path);
186 if (!shared_lib_handle->path) {
187 BT_LOGE_STR("Failed to allocate a GString.");
188 goto error;
189 }
190
191 shared_lib_handle->module = g_module_open(path, G_MODULE_BIND_LOCAL);
192 if (!shared_lib_handle->module) {
193 /*
194 * DEBUG-level logging because we're only _trying_ to
195 * open this file as a Babeltrace plugin: if it's not,
196 * it's not an error. And because this can be tried
197 * during bt_plugin_create_all_from_dir(), it's not even
198 * a warning.
199 */
200 BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
201 g_module_error(), path);
202 goto error;
203 }
204
205 goto end;
206
207 error:
208 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
209
210 end:
211 if (shared_lib_handle) {
212 BT_LOGD("Created shared library handle: path=\"%s\", addr=%p",
213 path, shared_lib_handle);
214 }
215
216 return shared_lib_handle;
217 }
218
219 static
220 void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin)
221 {
222 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
223
224 if (!plugin->spec_data) {
225 return;
226 }
227
228 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
229 BT_ASSERT(spec);
230 BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle);
231 g_free(plugin->spec_data);
232 plugin->spec_data = NULL;
233 }
234
235 /*
236 * This function does the following:
237 *
238 * 1. Iterate on the plugin descriptor attributes section and set the
239 * plugin's attributes depending on the attribute types. This
240 * includes the name of the plugin, its description, and its
241 * initialization function, for example.
242 *
243 * 2. Iterate on the component class descriptors section and create one
244 * "full descriptor" (temporary structure) for each one that is found
245 * and attached to our plugin descriptor.
246 *
247 * 3. Iterate on the component class descriptor attributes section and
248 * set the corresponding full descriptor's attributes depending on
249 * the attribute types. This includes the description of the
250 * component class, as well as its initialization and destroy
251 * methods.
252 *
253 * 4. Call the user's plugin initialization function, if any is
254 * defined.
255 *
256 * 5. For each full component class descriptor, create a component class
257 * object, set its optional attributes, and add it to the plugin
258 * object.
259 *
260 * 6. Freeze the plugin object.
261 */
262 static
263 enum bt_plugin_status bt_plugin_so_init(
264 struct bt_plugin *plugin,
265 const struct __bt_plugin_descriptor *descriptor,
266 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
267 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
268 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
269 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
270 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
271 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
272 {
273 /*
274 * This structure's members point to the plugin's memory
275 * (do NOT free).
276 */
277 struct comp_class_full_descriptor {
278 const struct __bt_plugin_component_class_descriptor *descriptor;
279 const char *description;
280 const char *help;
281 bt_component_class_init_method init_method;
282 bt_component_class_finalize_method finalize_method;
283 bt_component_class_query_method query_method;
284 bt_component_class_accept_port_connection_method accept_port_connection_method;
285 bt_component_class_port_connected_method port_connected_method;
286 bt_component_class_port_disconnected_method port_disconnected_method;
287 struct bt_component_class_notification_iterator_methods iterator_methods;
288 };
289
290 enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
291 struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr;
292 struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr;
293 struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr;
294 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
295 GArray *comp_class_full_descriptors;
296 size_t i;
297 int ret;
298
299 BT_LOGD("Initializing plugin object from descriptors found in sections: "
300 "plugin-addr=%p, plugin-path=\"%s\", "
301 "attrs-begin-addr=%p, attrs-end-addr=%p, "
302 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
303 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p",
304 plugin,
305 spec->shared_lib_handle->path ?
306 spec->shared_lib_handle->path->str : NULL,
307 attrs_begin, attrs_end,
308 cc_descriptors_begin, cc_descriptors_end,
309 cc_descr_attrs_begin, cc_descr_attrs_end);
310 comp_class_full_descriptors = g_array_new(FALSE, TRUE,
311 sizeof(struct comp_class_full_descriptor));
312 if (!comp_class_full_descriptors) {
313 BT_LOGE_STR("Failed to allocate a GArray.");
314 status = BT_PLUGIN_STATUS_ERROR;
315 goto end;
316 }
317
318 /* Set mandatory attributes */
319 spec->descriptor = descriptor;
320 bt_plugin_set_name(plugin, descriptor->name);
321
322 /*
323 * Find and set optional attributes attached to this plugin
324 * descriptor.
325 */
326 for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) {
327 const struct __bt_plugin_descriptor_attribute *cur_attr =
328 *cur_attr_ptr;
329
330 if (cur_attr == NULL) {
331 continue;
332 }
333
334 if (cur_attr->plugin_descriptor != descriptor) {
335 continue;
336 }
337
338 switch (cur_attr->type) {
339 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT:
340 spec->init = cur_attr->value.init;
341 break;
342 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT:
343 spec->shared_lib_handle->exit = cur_attr->value.exit;
344 break;
345 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR:
346 bt_plugin_set_author(plugin, cur_attr->value.author);
347 break;
348 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE:
349 bt_plugin_set_license(plugin, cur_attr->value.license);
350 break;
351 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
352 bt_plugin_set_description(plugin, cur_attr->value.description);
353 break;
354 case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION:
355 bt_plugin_set_version(plugin,
356 (unsigned int) cur_attr->value.version.major,
357 (unsigned int) cur_attr->value.version.minor,
358 (unsigned int) cur_attr->value.version.patch,
359 cur_attr->value.version.extra);
360 break;
361 default:
362 /*
363 * WARN-level logging because this should not
364 * happen with the appropriate ABI version. If
365 * we're here, we know that for the reported
366 * version of the ABI, this attribute is
367 * unknown.
368 */
369 BT_LOGW("Ignoring unknown plugin descriptor attribute: "
370 "plugin-path=\"%s\", plugin-name=\"%s\", "
371 "attr-type-name=\"%s\", attr-type-id=%d",
372 spec->shared_lib_handle->path ?
373 spec->shared_lib_handle->path->str :
374 NULL,
375 descriptor->name, cur_attr->type_name,
376 cur_attr->type);
377 break;
378 }
379 }
380
381 /*
382 * Find component class descriptors attached to this plugin
383 * descriptor and initialize corresponding full component class
384 * descriptors in the array.
385 */
386 for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) {
387 const struct __bt_plugin_component_class_descriptor *cur_cc_descr =
388 *cur_cc_descr_ptr;
389 struct comp_class_full_descriptor full_descriptor = {0};
390
391 if (cur_cc_descr == NULL) {
392 continue;
393 }
394
395 if (cur_cc_descr->plugin_descriptor != descriptor) {
396 continue;
397 }
398
399 full_descriptor.descriptor = cur_cc_descr;
400 g_array_append_val(comp_class_full_descriptors,
401 full_descriptor);
402 }
403
404 /*
405 * Find component class descriptor attributes attached to this
406 * plugin descriptor and update corresponding full component
407 * class descriptors in the array.
408 */
409 for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) {
410 const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr =
411 *cur_cc_descr_attr_ptr;
412
413 if (cur_cc_descr_attr == NULL) {
414 continue;
415 }
416
417 if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor !=
418 descriptor) {
419 continue;
420 }
421
422 /* Find the corresponding component class descriptor entry */
423 for (i = 0; i < comp_class_full_descriptors->len; i++) {
424 struct comp_class_full_descriptor *cc_full_descr =
425 &g_array_index(comp_class_full_descriptors,
426 struct comp_class_full_descriptor, i);
427
428 if (cur_cc_descr_attr->comp_class_descriptor ==
429 cc_full_descr->descriptor) {
430 switch (cur_cc_descr_attr->type) {
431 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION:
432 cc_full_descr->description =
433 cur_cc_descr_attr->value.description;
434 break;
435 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP:
436 cc_full_descr->help =
437 cur_cc_descr_attr->value.help;
438 break;
439 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD:
440 cc_full_descr->init_method =
441 cur_cc_descr_attr->value.init_method;
442 break;
443 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD:
444 cc_full_descr->finalize_method =
445 cur_cc_descr_attr->value.finalize_method;
446 break;
447 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD:
448 cc_full_descr->query_method =
449 cur_cc_descr_attr->value.query_method;
450 break;
451 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_ACCEPT_PORT_CONNECTION_METHOD:
452 cc_full_descr->accept_port_connection_method =
453 cur_cc_descr_attr->value.accept_port_connection_method;
454 break;
455 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_CONNECTED_METHOD:
456 cc_full_descr->port_connected_method =
457 cur_cc_descr_attr->value.port_connected_method;
458 break;
459 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_PORT_DISCONNECTED_METHOD:
460 cc_full_descr->port_disconnected_method =
461 cur_cc_descr_attr->value.port_disconnected_method;
462 break;
463 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD:
464 cc_full_descr->iterator_methods.init =
465 cur_cc_descr_attr->value.notif_iter_init_method;
466 break;
467 case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_FINALIZE_METHOD:
468 cc_full_descr->iterator_methods.finalize =
469 cur_cc_descr_attr->value.notif_iter_finalize_method;
470 break;
471 default:
472 /*
473 * WARN-level logging because
474 * this should not happen with
475 * the appropriate ABI version.
476 * If we're here, we know that
477 * for the reported version of
478 * the ABI, this attribute is
479 * unknown.
480 */
481 BT_LOGW("Ignoring unknown component class descriptor attribute: "
482 "plugin-path=\"%s\", "
483 "plugin-name=\"%s\", "
484 "comp-class-name=\"%s\", "
485 "comp-class-type=%s, "
486 "attr-type-name=\"%s\", "
487 "attr-type-id=%d",
488 spec->shared_lib_handle->path ?
489 spec->shared_lib_handle->path->str :
490 NULL,
491 descriptor->name,
492 cur_cc_descr_attr->comp_class_descriptor->name,
493 bt_component_class_type_string(
494 cur_cc_descr_attr->comp_class_descriptor->type),
495 cur_cc_descr_attr->type_name,
496 cur_cc_descr_attr->type);
497 break;
498 }
499 }
500 }
501 }
502
503 /* Initialize plugin */
504 if (spec->init) {
505 BT_LOGD_STR("Calling user's plugin initialization function.");
506 status = spec->init(plugin);
507 BT_LOGD("User function returned: %s",
508 bt_plugin_status_string(status));
509
510 if (status < 0) {
511 BT_LOGW_STR("User's plugin initialization function failed.");
512 goto end;
513 }
514 }
515
516 spec->shared_lib_handle->init_called = BT_TRUE;
517
518 /* Add described component classes to plugin */
519 for (i = 0; i < comp_class_full_descriptors->len; i++) {
520 struct comp_class_full_descriptor *cc_full_descr =
521 &g_array_index(comp_class_full_descriptors,
522 struct comp_class_full_descriptor, i);
523 struct bt_component_class *comp_class;
524
525 BT_LOGD("Creating and setting properties of plugin's component class: "
526 "plugin-path=\"%s\", plugin-name=\"%s\", "
527 "comp-class-name=\"%s\", comp-class-type=%s",
528 spec->shared_lib_handle->path ?
529 spec->shared_lib_handle->path->str :
530 NULL,
531 descriptor->name,
532 cc_full_descr->descriptor->name,
533 bt_component_class_type_string(
534 cc_full_descr->descriptor->type));
535
536 switch (cc_full_descr->descriptor->type) {
537 case BT_COMPONENT_CLASS_TYPE_SOURCE:
538 comp_class = bt_component_class_source_create(
539 cc_full_descr->descriptor->name,
540 cc_full_descr->descriptor->methods.source.notif_iter_next);
541 break;
542 case BT_COMPONENT_CLASS_TYPE_FILTER:
543 comp_class = bt_component_class_filter_create(
544 cc_full_descr->descriptor->name,
545 cc_full_descr->descriptor->methods.source.notif_iter_next);
546 break;
547 case BT_COMPONENT_CLASS_TYPE_SINK:
548 comp_class = bt_component_class_sink_create(
549 cc_full_descr->descriptor->name,
550 cc_full_descr->descriptor->methods.sink.consume);
551 break;
552 default:
553 /*
554 * WARN-level logging because this should not
555 * happen with the appropriate ABI version. If
556 * we're here, we know that for the reported
557 * version of the ABI, this component class type
558 * is unknown.
559 */
560 BT_LOGW("Ignoring unknown component class type: "
561 "plugin-path=\"%s\", plugin-name=\"%s\", "
562 "comp-class-name=\"%s\", comp-class-type=%d",
563 spec->shared_lib_handle->path->str ?
564 spec->shared_lib_handle->path->str :
565 NULL,
566 descriptor->name,
567 cc_full_descr->descriptor->name,
568 cc_full_descr->descriptor->type);
569 continue;
570 }
571
572 if (!comp_class) {
573 BT_LOGE_STR("Cannot create component class.");
574 status = BT_PLUGIN_STATUS_ERROR;
575 goto end;
576 }
577
578 if (cc_full_descr->description) {
579 ret = bt_component_class_set_description(comp_class,
580 cc_full_descr->description);
581 if (ret) {
582 BT_LOGE_STR("Cannot set component class's description.");
583 status = BT_PLUGIN_STATUS_ERROR;
584 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
585 goto end;
586 }
587 }
588
589 if (cc_full_descr->help) {
590 ret = bt_component_class_set_help(comp_class,
591 cc_full_descr->help);
592 if (ret) {
593 BT_LOGE_STR("Cannot set component class's help string.");
594 status = BT_PLUGIN_STATUS_ERROR;
595 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
596 goto end;
597 }
598 }
599
600 if (cc_full_descr->init_method) {
601 ret = bt_component_class_set_init_method(comp_class,
602 cc_full_descr->init_method);
603 if (ret) {
604 BT_LOGE_STR("Cannot set component class's initialization method.");
605 status = BT_PLUGIN_STATUS_ERROR;
606 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
607 goto end;
608 }
609 }
610
611 if (cc_full_descr->finalize_method) {
612 ret = bt_component_class_set_finalize_method(comp_class,
613 cc_full_descr->finalize_method);
614 if (ret) {
615 BT_LOGE_STR("Cannot set component class's finalization method.");
616 status = BT_PLUGIN_STATUS_ERROR;
617 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
618 goto end;
619 }
620 }
621
622 if (cc_full_descr->query_method) {
623 ret = bt_component_class_set_query_method(
624 comp_class, cc_full_descr->query_method);
625 if (ret) {
626 BT_LOGE_STR("Cannot set component class's query method.");
627 status = BT_PLUGIN_STATUS_ERROR;
628 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
629 goto end;
630 }
631 }
632
633 if (cc_full_descr->accept_port_connection_method) {
634 ret = bt_component_class_set_accept_port_connection_method(
635 comp_class, cc_full_descr->accept_port_connection_method);
636 if (ret) {
637 BT_LOGE_STR("Cannot set component class's \"accept port connection\" method.");
638 status = BT_PLUGIN_STATUS_ERROR;
639 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
640 goto end;
641 }
642 }
643
644 if (cc_full_descr->port_connected_method) {
645 ret = bt_component_class_set_port_connected_method(
646 comp_class, cc_full_descr->port_connected_method);
647 if (ret) {
648 BT_LOGE_STR("Cannot set component class's \"port connected\" method.");
649 status = BT_PLUGIN_STATUS_ERROR;
650 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
651 goto end;
652 }
653 }
654
655 if (cc_full_descr->port_disconnected_method) {
656 ret = bt_component_class_set_port_disconnected_method(
657 comp_class, cc_full_descr->port_disconnected_method);
658 if (ret) {
659 BT_LOGE_STR("Cannot set component class's \"port disconnected\" method.");
660 status = BT_PLUGIN_STATUS_ERROR;
661 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
662 goto end;
663 }
664 }
665
666 switch (cc_full_descr->descriptor->type) {
667 case BT_COMPONENT_CLASS_TYPE_SOURCE:
668 if (cc_full_descr->iterator_methods.init) {
669 ret = bt_component_class_source_set_notification_iterator_init_method(
670 comp_class,
671 cc_full_descr->iterator_methods.init);
672 if (ret) {
673 BT_LOGE_STR("Cannot set component class's notification iterator initialization method.");
674 status = BT_PLUGIN_STATUS_ERROR;
675 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
676 goto end;
677 }
678 }
679
680 if (cc_full_descr->iterator_methods.finalize) {
681 ret = bt_component_class_source_set_notification_iterator_finalize_method(
682 comp_class,
683 cc_full_descr->iterator_methods.finalize);
684 if (ret) {
685 BT_LOGE_STR("Cannot set source component class's notification iterator finalization method.");
686 status = BT_PLUGIN_STATUS_ERROR;
687 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
688 goto end;
689 }
690 }
691 break;
692 case BT_COMPONENT_CLASS_TYPE_FILTER:
693 if (cc_full_descr->iterator_methods.init) {
694 ret = bt_component_class_filter_set_notification_iterator_init_method(
695 comp_class,
696 cc_full_descr->iterator_methods.init);
697 if (ret) {
698 BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method.");
699 status = BT_PLUGIN_STATUS_ERROR;
700 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
701 goto end;
702 }
703 }
704
705 if (cc_full_descr->iterator_methods.finalize) {
706 ret = bt_component_class_filter_set_notification_iterator_finalize_method(
707 comp_class,
708 cc_full_descr->iterator_methods.finalize);
709 if (ret) {
710 BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method.");
711 status = BT_PLUGIN_STATUS_ERROR;
712 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
713 goto end;
714 }
715 }
716 break;
717 case BT_COMPONENT_CLASS_TYPE_SINK:
718 break;
719 default:
720 abort();
721 }
722
723 /*
724 * Add component class to the plugin object.
725 *
726 * This will call back
727 * bt_plugin_so_on_add_component_class() so that we can
728 * add a mapping in the component class list when
729 * we know the component class is successfully added.
730 */
731 status = bt_plugin_add_component_class(plugin,
732 comp_class);
733 BT_OBJECT_PUT_REF_AND_RESET(comp_class);
734 if (status < 0) {
735 BT_LOGE("Cannot add component class to plugin.");
736 goto end;
737 }
738 }
739
740 /*
741 * All the plugin's component classes should be added at this
742 * point. We freeze the plugin so that it's not possible to add
743 * component classes to this plugin object after this stage
744 * (plugin object becomes immutable).
745 */
746 bt_plugin_freeze(plugin);
747
748 end:
749 g_array_free(comp_class_full_descriptors, TRUE);
750 return status;
751 }
752
753 static
754 struct bt_plugin *bt_plugin_so_create_empty(
755 struct bt_plugin_so_shared_lib_handle *shared_lib_handle)
756 {
757 struct bt_plugin *plugin;
758 struct bt_plugin_so_spec_data *spec;
759
760 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO);
761 if (!plugin) {
762 goto error;
763 }
764
765 plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data;
766 plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1);
767 if (!plugin->spec_data) {
768 BT_LOGE_STR("Failed to allocate one SO plugin specific data structure.");
769 goto error;
770 }
771
772 spec = plugin->spec_data;
773 spec->shared_lib_handle = bt_object_get_ref(shared_lib_handle);
774 goto end;
775
776 error:
777 BT_OBJECT_PUT_REF_AND_RESET(plugin);
778
779 end:
780 return plugin;
781 }
782
783 static
784 size_t count_non_null_items_in_section(const void *begin, const void *end)
785 {
786 size_t count = 0;
787 const int * const *begin_int = (const int * const *) begin;
788 const int * const *end_int = (const int * const *) end;
789 const int * const *iter;
790
791 for (iter = begin_int; iter != end_int; iter++) {
792 if (*iter) {
793 count++;
794 }
795 }
796
797 return count;
798 }
799
800 static
801 struct bt_plugin_set *bt_plugin_so_create_all_from_sections(
802 struct bt_plugin_so_shared_lib_handle *shared_lib_handle,
803 struct __bt_plugin_descriptor const * const *descriptors_begin,
804 struct __bt_plugin_descriptor const * const *descriptors_end,
805 struct __bt_plugin_descriptor_attribute const * const *attrs_begin,
806 struct __bt_plugin_descriptor_attribute const * const *attrs_end,
807 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin,
808 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end,
809 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin,
810 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end)
811 {
812 size_t descriptor_count;
813 size_t attrs_count;
814 size_t cc_descriptors_count;
815 size_t cc_descr_attrs_count;
816 size_t i;
817 struct bt_plugin_set *plugin_set = NULL;
818
819 descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end);
820 attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end);
821 cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end);
822 cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end);
823
824 BT_LOGD("Creating all SO plugins from sections: "
825 "plugin-path=\"%s\", "
826 "descr-begin-addr=%p, descr-end-addr=%p, "
827 "attrs-begin-addr=%p, attrs-end-addr=%p, "
828 "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, "
829 "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, "
830 "descr-count=%zu, attrs-count=%zu, "
831 "cc-descr-count=%zu, cc-descr-attrs-count=%zu",
832 shared_lib_handle->path ? shared_lib_handle->path->str : NULL,
833 descriptors_begin, descriptors_end,
834 attrs_begin, attrs_end,
835 cc_descriptors_begin, cc_descriptors_end,
836 cc_descr_attrs_begin, cc_descr_attrs_end,
837 descriptor_count, attrs_count,
838 cc_descriptors_count, cc_descr_attrs_count);
839 plugin_set = bt_plugin_set_create();
840 if (!plugin_set) {
841 BT_LOGE_STR("Cannot create empty plugin set.");
842 goto error;
843 }
844
845 for (i = 0; i < descriptors_end - descriptors_begin; i++) {
846 enum bt_plugin_status status;
847 const struct __bt_plugin_descriptor *descriptor =
848 descriptors_begin[i];
849 struct bt_plugin *plugin;
850
851 if (descriptor == NULL) {
852 continue;
853 }
854
855 BT_LOGD("Creating plugin object for plugin: "
856 "name=\"%s\", abi-major=%d, abi-minor=%d",
857 descriptor->name, descriptor->major, descriptor->minor);
858
859 if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) {
860 /*
861 * DEBUG-level logging because we're only
862 * _trying_ to open this file as a compatible
863 * Babeltrace plugin: if it's not, it's not an
864 * error. And because this can be tried during
865 * bt_plugin_create_all_from_dir(), it's not
866 * even a warning.
867 */
868 BT_LOGD("Unknown ABI major version: abi-major=%d",
869 descriptor->major);
870 goto error;
871 }
872
873 plugin = bt_plugin_so_create_empty(shared_lib_handle);
874 if (!plugin) {
875 BT_LOGE_STR("Cannot create empty shared library handle.");
876 goto error;
877 }
878
879 if (shared_lib_handle && shared_lib_handle->path) {
880 bt_plugin_set_path(plugin, shared_lib_handle->path->str);
881 }
882
883 status = bt_plugin_so_init(plugin, descriptor, attrs_begin,
884 attrs_end, cc_descriptors_begin, cc_descriptors_end,
885 cc_descr_attrs_begin, cc_descr_attrs_end);
886 if (status < 0) {
887 /*
888 * DEBUG-level logging because we're only
889 * _trying_ to open this file as a compatible
890 * Babeltrace plugin: if it's not, it's not an
891 * error. And because this can be tried during
892 * bt_plugin_create_all_from_dir(), it's not
893 * even a warning.
894 */
895 BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
896 BT_OBJECT_PUT_REF_AND_RESET(plugin);
897 goto error;
898 }
899
900 /* Add to plugin set */
901 bt_plugin_set_add_plugin(plugin_set, plugin);
902 bt_object_put_ref(plugin);
903 }
904
905 goto end;
906
907 error:
908 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
909
910 end:
911 return plugin_set;
912 }
913
914 BT_HIDDEN
915 struct bt_plugin_set *bt_plugin_so_create_all_from_static(void)
916 {
917 struct bt_plugin_set *plugin_set = NULL;
918 struct bt_plugin_so_shared_lib_handle *shared_lib_handle =
919 bt_plugin_so_shared_lib_handle_create(NULL);
920
921 if (!shared_lib_handle) {
922 goto end;
923 }
924
925 BT_LOGD_STR("Creating all SO plugins from built-in plugins.");
926 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
927 __bt_get_begin_section_plugin_descriptors(),
928 __bt_get_end_section_plugin_descriptors(),
929 __bt_get_begin_section_plugin_descriptor_attributes(),
930 __bt_get_end_section_plugin_descriptor_attributes(),
931 __bt_get_begin_section_component_class_descriptors(),
932 __bt_get_end_section_component_class_descriptors(),
933 __bt_get_begin_section_component_class_descriptor_attributes(),
934 __bt_get_end_section_component_class_descriptor_attributes());
935
936 end:
937 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
938
939 return plugin_set;
940 }
941
942 BT_HIDDEN
943 struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path)
944 {
945 size_t path_len;
946 struct bt_plugin_set *plugin_set = NULL;
947 struct __bt_plugin_descriptor const * const *descriptors_begin = NULL;
948 struct __bt_plugin_descriptor const * const *descriptors_end = NULL;
949 struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL;
950 struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL;
951 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL;
952 struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL;
953 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL;
954 struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL;
955 struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void);
956 struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void);
957 struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void);
958 struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void);
959 struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void);
960 struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void);
961 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void);
962 struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void);
963 bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE;
964 struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL;
965
966 if (!path) {
967 BT_LOGW_STR("Invalid parameter: path is NULL.");
968 goto end;
969 }
970
971 BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path);
972 path_len = strlen(path);
973 if (path_len <= PLUGIN_SUFFIX_LEN) {
974 BT_LOGW("Invalid parameter: path length is too short: "
975 "path-length=%zu", path_len);
976 goto end;
977 }
978
979 path_len++;
980 /*
981 * Check if the file ends with a known plugin file type suffix (i.e. .so
982 * or .la on Linux).
983 */
984 is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX,
985 path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN,
986 LIBTOOL_PLUGIN_SUFFIX_LEN);
987 is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX,
988 path + path_len - NATIVE_PLUGIN_SUFFIX_LEN,
989 NATIVE_PLUGIN_SUFFIX_LEN);
990 if (!is_shared_object && !is_libtool_wrapper) {
991 /* Name indicates this is not a plugin file; not an error */
992 BT_LOGV("File is not a SO plugin file: path=\"%s\"", path);
993 goto end;
994 }
995
996 shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path);
997 if (!shared_lib_handle) {
998 BT_LOGD_STR("Cannot create shared library handle.");
999 goto end;
1000 }
1001
1002 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors",
1003 (gpointer *) &get_begin_section_plugin_descriptors)) {
1004 descriptors_begin = get_begin_section_plugin_descriptors();
1005 } else {
1006 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1007 "symbol=\"%s\"", path,
1008 "__bt_get_begin_section_plugin_descriptors");
1009 goto end;
1010 }
1011
1012 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors",
1013 (gpointer *) &get_end_section_plugin_descriptors)) {
1014 descriptors_end = get_end_section_plugin_descriptors();
1015 } else {
1016 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1017 "symbol=\"%s\"", path,
1018 "__bt_get_end_section_plugin_descriptors");
1019 goto end;
1020 }
1021
1022 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes",
1023 (gpointer *) &get_begin_section_plugin_descriptor_attributes)) {
1024 attrs_begin = get_begin_section_plugin_descriptor_attributes();
1025 } else {
1026 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1027 "symbol=\"%s\"", path,
1028 "__bt_get_begin_section_plugin_descriptor_attributes");
1029 }
1030
1031 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes",
1032 (gpointer *) &get_end_section_plugin_descriptor_attributes)) {
1033 attrs_end = get_end_section_plugin_descriptor_attributes();
1034 } else {
1035 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1036 "symbol=\"%s\"", path,
1037 "__bt_get_end_section_plugin_descriptor_attributes");
1038 }
1039
1040 if ((!!attrs_begin - !!attrs_end) != 0) {
1041 BT_LOGD("Found section start or end symbol, but not both: "
1042 "path=\"%s\", symbol-start=\"%s\", "
1043 "symbol-end=\"%s\", symbol-start-addr=%p, "
1044 "symbol-end-addr=%p",
1045 path, "__bt_get_begin_section_plugin_descriptor_attributes",
1046 "__bt_get_end_section_plugin_descriptor_attributes",
1047 attrs_begin, attrs_end);
1048 goto end;
1049 }
1050
1051 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors",
1052 (gpointer *) &get_begin_section_component_class_descriptors)) {
1053 cc_descriptors_begin = get_begin_section_component_class_descriptors();
1054 } else {
1055 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1056 "symbol=\"%s\"", path,
1057 "__bt_get_begin_section_component_class_descriptors");
1058 }
1059
1060 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors",
1061 (gpointer *) &get_end_section_component_class_descriptors)) {
1062 cc_descriptors_end = get_end_section_component_class_descriptors();
1063 } else {
1064 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1065 "symbol=\"%s\"", path,
1066 "__bt_get_end_section_component_class_descriptors");
1067 }
1068
1069 if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) {
1070 BT_LOGD("Found section start or end symbol, but not both: "
1071 "path=\"%s\", symbol-start=\"%s\", "
1072 "symbol-end=\"%s\", symbol-start-addr=%p, "
1073 "symbol-end-addr=%p",
1074 path, "__bt_get_begin_section_component_class_descriptors",
1075 "__bt_get_end_section_component_class_descriptors",
1076 cc_descriptors_begin, cc_descriptors_end);
1077 goto end;
1078 }
1079
1080 if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes",
1081 (gpointer *) &get_begin_section_component_class_descriptor_attributes)) {
1082 cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes();
1083 } else {
1084 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1085 "symbol=\"%s\"", path,
1086 "__bt_get_begin_section_component_class_descriptor_attributes");
1087 }
1088
1089 if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes",
1090 (gpointer *) &get_end_section_component_class_descriptor_attributes)) {
1091 cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes();
1092 } else {
1093 BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", "
1094 "symbol=\"%s\"", path,
1095 "__bt_get_end_section_component_class_descriptor_attributes");
1096 }
1097
1098 if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) {
1099 BT_LOGD("Found section start or end symbol, but not both: "
1100 "path=\"%s\", symbol-start=\"%s\", "
1101 "symbol-end=\"%s\", symbol-start-addr=%p, "
1102 "symbol-end-addr=%p",
1103 path, "__bt_get_begin_section_component_class_descriptor_attributes",
1104 "__bt_get_end_section_component_class_descriptor_attributes",
1105 cc_descr_attrs_begin, cc_descr_attrs_end);
1106 goto end;
1107 }
1108
1109 /* Initialize plugin */
1110 BT_LOGD_STR("Initializing plugin object.");
1111 plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle,
1112 descriptors_begin, descriptors_end, attrs_begin, attrs_end,
1113 cc_descriptors_begin, cc_descriptors_end,
1114 cc_descr_attrs_begin, cc_descr_attrs_end);
1115
1116 end:
1117 BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle);
1118 return plugin_set;
1119 }
1120
1121 static
1122 void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
1123 void *data)
1124 {
1125 bt_list_del(&comp_class->node);
1126 BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle);
1127 BT_LOGV("Component class destroyed: removed entry from list: "
1128 "comp-cls-addr=%p", comp_class);
1129 }
1130
1131 BT_HIDDEN
1132 void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin,
1133 struct bt_component_class *comp_class)
1134 {
1135 struct bt_plugin_so_spec_data *spec = plugin->spec_data;
1136
1137 BT_ASSERT(plugin->spec_data);
1138 BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO);
1139
1140 bt_list_add(&comp_class->node, &component_class_list);
1141 comp_class->so_handle = bt_object_get_ref(spec->shared_lib_handle);
1142
1143 /* Add our custom destroy listener */
1144 bt_component_class_add_destroy_listener(comp_class,
1145 plugin_comp_class_destroy_listener, NULL);
1146 }
This page took 0.063902 seconds and 4 git commands to generate.