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