Commit | Line | Data |
---|---|---|
55bb57e0 PP |
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 | #include <babeltrace/compiler.h> | |
31 | #include <babeltrace/ref.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/component/component-class-internal.h> | |
37 | #include <string.h> | |
38 | #include <stdbool.h> | |
39 | #include <glib.h> | |
40 | #include <gmodule.h> | |
41 | ||
42 | #define NATIVE_PLUGIN_SUFFIX ".so" | |
43 | #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) | |
44 | #define LIBTOOL_PLUGIN_SUFFIX ".la" | |
45 | #define LIBTOOL_PLUGIN_SUFFIX_LEN sizeof(LIBTOOL_PLUGIN_SUFFIX) | |
46 | ||
47 | #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ | |
48 | sizeof(LIBTOOL_PLUGIN_SUFFIX)) | |
49 | ||
50 | #define SECTION_BEGIN(_name) (&(__start_##_name)) | |
51 | #define SECTION_END(_name) (&(__stop_##_name)) | |
52 | #define SECTION_ELEMENT_COUNT(_name) (SECTION_END(_name) - SECTION_BEGIN(_name)) | |
53 | ||
54 | #define DECLARE_SECTION(_type, _name) \ | |
55 | extern _type __start_##_name __attribute((weak)); \ | |
56 | extern _type __stop_##_name __attribute((weak)) | |
57 | ||
58 | DECLARE_SECTION(struct __bt_plugin_descriptor const *, __bt_plugin_descriptors); | |
59 | DECLARE_SECTION(struct __bt_plugin_descriptor_attribute const *, __bt_plugin_descriptor_attributes); | |
60 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor const *, __bt_plugin_component_class_descriptors); | |
61 | DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute const *, __bt_plugin_component_class_descriptor_attributes); | |
62 | ||
63 | /* | |
64 | * This hash table, global to the library, maps component class pointers | |
65 | * to shared library handles. | |
66 | * | |
67 | * The keys (component classes) are NOT owned by this hash table, whereas | |
68 | * the values (shared library handles) are owned by this hash table. | |
69 | * | |
70 | * The keys are the component classes created with | |
71 | * bt_plugin_add_component_class(). They keep the shared library handle | |
72 | * object created by their plugin alive so that the plugin's code is | |
73 | * not discarded when it could still be in use by living components | |
74 | * created from those component classes: | |
75 | * | |
76 | * [component] --ref-> [component class] --through this HT-> [shlib handle] | |
77 | * | |
78 | * This hash table exists for two reasons: | |
79 | * | |
80 | * 1. To allow this application: | |
81 | * | |
82 | * my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so"); | |
83 | * // instantiate components from a plugin's component classes | |
84 | * // put plugins and free my_plugins here | |
85 | * // user code of instantiated components still exists | |
86 | * | |
87 | * 2. To decouple the plugin subsystem from the component subsystem: | |
88 | * while plugins objects need to know component class objects, the | |
89 | * opposite is not necessary, thus it makes no sense for a component | |
90 | * class to keep a reference to the plugin object from which it was | |
91 | * created. | |
92 | * | |
93 | * An entry is removed from this HT when a component class is destroyed | |
94 | * thanks to a custom destroy listener. When the entry is removed, the | |
95 | * GLib function calls the value destroy notifier of the HT, which is | |
96 | * bt_put(). This decreases the reference count of the mapped shared | |
97 | * library handle. Assuming the original plugin object which contained | |
98 | * some component classes is put first, when the last component class is | |
99 | * removed from this HT, the shared library handle object's reference | |
100 | * count falls to zero and the shared library is finally closed. | |
101 | */ | |
102 | static | |
103 | GHashTable *comp_classes_to_shlib_handles; | |
104 | ||
105 | __attribute__((constructor)) static | |
106 | void init_comp_classes_to_shlib_handles(void) { | |
107 | comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash, | |
108 | g_direct_equal, NULL, bt_put); | |
109 | assert(comp_classes_to_shlib_handles); | |
110 | } | |
111 | ||
112 | __attribute__((destructor)) static | |
113 | void fini_comp_classes_to_shlib_handles(void) { | |
114 | if (comp_classes_to_shlib_handles) { | |
115 | g_hash_table_destroy(comp_classes_to_shlib_handles); | |
116 | } | |
117 | } | |
118 | ||
119 | static | |
120 | void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj) | |
121 | { | |
122 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle; | |
123 | ||
124 | assert(obj); | |
125 | shared_lib_handle = container_of(obj, | |
126 | struct bt_plugin_so_shared_lib_handle, base); | |
127 | ||
128 | if (shared_lib_handle->init_called && shared_lib_handle->exit) { | |
129 | enum bt_plugin_status status = shared_lib_handle->exit(); | |
130 | ||
131 | if (status < 0) { | |
132 | const char *path = shared_lib_handle->path ? | |
133 | shared_lib_handle->path->str : "[built-in]"; | |
134 | ||
135 | printf_verbose("Plugin in module `%s` exited with error %d\n", | |
136 | path, status); | |
137 | } | |
138 | } | |
139 | ||
140 | if (shared_lib_handle->module) { | |
141 | if (!g_module_close(shared_lib_handle->module)) { | |
142 | printf_error("Module close error: %s\n", | |
143 | g_module_error()); | |
144 | } | |
145 | } | |
146 | ||
147 | if (shared_lib_handle->path) { | |
148 | g_string_free(shared_lib_handle->path, TRUE); | |
149 | } | |
150 | ||
151 | g_free(shared_lib_handle); | |
152 | } | |
153 | ||
154 | static | |
155 | struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create( | |
156 | const char *path) | |
157 | { | |
158 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; | |
159 | ||
160 | shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1); | |
161 | if (!shared_lib_handle) { | |
162 | goto error; | |
163 | } | |
164 | ||
165 | bt_object_init(shared_lib_handle, bt_plugin_so_shared_lib_handle_destroy); | |
166 | ||
167 | if (!path) { | |
168 | goto end; | |
169 | } | |
170 | ||
171 | shared_lib_handle->path = g_string_new(path); | |
172 | if (!shared_lib_handle->path) { | |
173 | goto error; | |
174 | } | |
175 | ||
176 | shared_lib_handle->module = g_module_open(path, 0); | |
177 | if (!shared_lib_handle->module) { | |
178 | printf_verbose("Module open error: %s\n", g_module_error()); | |
179 | goto error; | |
180 | } | |
181 | ||
182 | goto end; | |
183 | ||
184 | error: | |
185 | BT_PUT(shared_lib_handle); | |
186 | ||
187 | end: | |
188 | return shared_lib_handle; | |
189 | } | |
190 | ||
191 | BT_HIDDEN | |
192 | void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin) | |
193 | { | |
194 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
195 | ||
196 | if (!plugin->spec_data) { | |
197 | return; | |
198 | } | |
199 | ||
200 | assert(plugin->type == BT_PLUGIN_TYPE_SO); | |
201 | assert(spec); | |
202 | BT_PUT(spec->shared_lib_handle); | |
203 | g_free(plugin->spec_data); | |
204 | plugin->spec_data = NULL; | |
205 | } | |
206 | ||
207 | /* | |
208 | * This function does the following: | |
209 | * | |
210 | * 1. Iterate on the plugin descriptor attributes section and set the | |
211 | * plugin's attributes depending on the attribute types. This | |
212 | * includes the name of the plugin, its description, and its | |
213 | * initialization function, for example. | |
214 | * | |
215 | * 2. Iterate on the component class descriptors section and create one | |
216 | * "full descriptor" (temporary structure) for each one that is found | |
217 | * and attached to our plugin descriptor. | |
218 | * | |
219 | * 3. Iterate on the component class descriptor attributes section and | |
220 | * set the corresponding full descriptor's attributes depending on | |
221 | * the attribute types. This includes the description of the | |
222 | * component class, as well as its initialization and destroy | |
223 | * methods. | |
224 | * | |
225 | * 4. Call the user's plugin initialization function, if any is | |
226 | * defined. | |
227 | * | |
228 | * 5. For each full component class descriptor, create a component class | |
229 | * object, set its optional attributes, and add it to the plugin | |
230 | * object. | |
231 | * | |
232 | * 6. Freeze the plugin object. | |
233 | */ | |
234 | static | |
235 | enum bt_plugin_status bt_plugin_so_init( | |
236 | struct bt_plugin *plugin, | |
237 | const struct __bt_plugin_descriptor *descriptor, | |
238 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
239 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
240 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
241 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
242 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
243 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) | |
244 | { | |
245 | /* | |
246 | * This structure's members point to the plugin's memory | |
247 | * (do NOT free). | |
248 | */ | |
249 | struct comp_class_full_descriptor { | |
250 | const struct __bt_plugin_component_class_descriptor *descriptor; | |
251 | const char *description; | |
252 | bt_component_class_init_method init_method; | |
253 | bt_component_class_destroy_method destroy_method; | |
254 | bt_component_class_filter_add_iterator_method filter_add_iterator_method; | |
255 | bt_component_class_sink_add_iterator_method sink_add_iterator_method; | |
256 | struct bt_component_class_iterator_methods iterator_methods; | |
257 | }; | |
258 | ||
259 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
260 | struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr; | |
261 | struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr; | |
262 | struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr; | |
263 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
264 | GArray *comp_class_full_descriptors; | |
265 | size_t i; | |
266 | int ret; | |
267 | ||
268 | comp_class_full_descriptors = g_array_new(FALSE, TRUE, | |
269 | sizeof(struct comp_class_full_descriptor)); | |
270 | if (!comp_class_full_descriptors) { | |
271 | status = BT_PLUGIN_STATUS_ERROR; | |
272 | goto end; | |
273 | } | |
274 | ||
275 | /* Set mandatory attributes */ | |
276 | spec->descriptor = descriptor; | |
277 | bt_plugin_set_name(plugin, descriptor->name); | |
278 | ||
279 | /* | |
280 | * Find and set optional attributes attached to this plugin | |
281 | * descriptor. | |
282 | */ | |
283 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { | |
284 | const struct __bt_plugin_descriptor_attribute *cur_attr = | |
285 | *cur_attr_ptr; | |
286 | ||
287 | if (cur_attr->plugin_descriptor != descriptor) { | |
288 | continue; | |
289 | } | |
290 | ||
291 | switch (cur_attr->type) { | |
292 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: | |
293 | spec->init = cur_attr->value.init; | |
294 | break; | |
295 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: | |
296 | spec->shared_lib_handle->exit = cur_attr->value.exit; | |
297 | break; | |
298 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: | |
299 | bt_plugin_set_author(plugin, cur_attr->value.author); | |
300 | break; | |
301 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: | |
302 | bt_plugin_set_license(plugin, cur_attr->value.license); | |
303 | break; | |
304 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
305 | bt_plugin_set_description(plugin, cur_attr->value.description); | |
306 | break; | |
307 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION: | |
308 | bt_plugin_set_version(plugin, | |
309 | (unsigned int) cur_attr->value.version.major, | |
310 | (unsigned int) cur_attr->value.version.minor, | |
311 | (unsigned int) cur_attr->value.version.patch, | |
312 | cur_attr->value.version.extra); | |
313 | break; | |
314 | default: | |
315 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n", | |
316 | cur_attr->type_name, cur_attr->type, | |
317 | descriptor->name); | |
318 | break; | |
319 | } | |
320 | } | |
321 | ||
322 | /* | |
323 | * Find component class descriptors attached to this plugin | |
324 | * descriptor and initialize corresponding full component class | |
325 | * descriptors in the array. | |
326 | */ | |
327 | for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) { | |
328 | const struct __bt_plugin_component_class_descriptor *cur_cc_descr = | |
329 | *cur_cc_descr_ptr; | |
330 | struct comp_class_full_descriptor full_descriptor = {0}; | |
331 | ||
332 | if (cur_cc_descr->plugin_descriptor != descriptor) { | |
333 | continue; | |
334 | } | |
335 | ||
336 | full_descriptor.descriptor = cur_cc_descr; | |
337 | g_array_append_val(comp_class_full_descriptors, | |
338 | full_descriptor); | |
339 | } | |
340 | ||
341 | /* | |
342 | * Find component class descriptor attributes attached to this | |
343 | * plugin descriptor and update corresponding full component | |
344 | * class descriptors in the array. | |
345 | */ | |
346 | for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) { | |
347 | const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr = | |
348 | *cur_cc_descr_attr_ptr; | |
349 | ||
350 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != | |
351 | descriptor) { | |
352 | continue; | |
353 | } | |
354 | ||
355 | /* Find the corresponding component class descriptor entry */ | |
356 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
357 | struct comp_class_full_descriptor *cc_full_descr = | |
358 | &g_array_index(comp_class_full_descriptors, | |
359 | struct comp_class_full_descriptor, i); | |
360 | ||
361 | if (cur_cc_descr_attr->comp_class_descriptor == | |
362 | cc_full_descr->descriptor) { | |
363 | switch (cur_cc_descr_attr->type) { | |
364 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
365 | cc_full_descr->description = | |
366 | cur_cc_descr_attr->value.description; | |
367 | break; | |
368 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD: | |
369 | cc_full_descr->init_method = | |
370 | cur_cc_descr_attr->value.init_method; | |
371 | break; | |
372 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESTROY_METHOD: | |
373 | cc_full_descr->destroy_method = | |
374 | cur_cc_descr_attr->value.destroy_method; | |
375 | break; | |
376 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FILTER_ADD_ITERATOR_METHOD: | |
377 | cc_full_descr->filter_add_iterator_method = | |
378 | cur_cc_descr_attr->value.filter_add_iterator_method; | |
379 | break; | |
380 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_SINK_ADD_ITERATOR_METHOD: | |
381 | cc_full_descr->sink_add_iterator_method = | |
382 | cur_cc_descr_attr->value.sink_add_iterator_method; | |
383 | break; | |
384 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD: | |
385 | cc_full_descr->iterator_methods.init = | |
386 | cur_cc_descr_attr->value.notif_iter_init_method; | |
387 | break; | |
388 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_DESTROY_METHOD: | |
389 | cc_full_descr->iterator_methods.destroy = | |
390 | cur_cc_descr_attr->value.notif_iter_destroy_method; | |
391 | break; | |
392 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD: | |
393 | cc_full_descr->iterator_methods.seek_time = | |
394 | cur_cc_descr_attr->value.notif_iter_seek_time_method; | |
395 | break; | |
396 | default: | |
397 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n", | |
398 | cur_cc_descr_attr->type_name, | |
399 | cur_cc_descr_attr->type, | |
400 | cur_cc_descr_attr->comp_class_descriptor->name, | |
401 | cur_cc_descr_attr->comp_class_descriptor->type, | |
402 | descriptor->name); | |
403 | break; | |
404 | } | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
409 | /* Initialize plugin */ | |
410 | if (spec->init) { | |
411 | status = spec->init(plugin); | |
412 | if (status < 0) { | |
413 | printf_verbose("Plugin `%s` initialization error: %d\n", | |
414 | bt_plugin_get_name(plugin), status); | |
415 | goto end; | |
416 | } | |
417 | } | |
418 | ||
419 | spec->shared_lib_handle->init_called = true; | |
420 | ||
421 | /* Add described component classes to plugin */ | |
422 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
423 | struct comp_class_full_descriptor *cc_full_descr = | |
424 | &g_array_index(comp_class_full_descriptors, | |
425 | struct comp_class_full_descriptor, i); | |
426 | struct bt_component_class *comp_class; | |
427 | ||
428 | switch (cc_full_descr->descriptor->type) { | |
429 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
430 | comp_class = bt_component_class_source_create( | |
431 | cc_full_descr->descriptor->name, | |
432 | cc_full_descr->descriptor->methods.source.notif_iter_get, | |
433 | cc_full_descr->descriptor->methods.source.notif_iter_next); | |
434 | break; | |
435 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
436 | comp_class = bt_component_class_filter_create( | |
437 | cc_full_descr->descriptor->name, | |
438 | cc_full_descr->descriptor->methods.source.notif_iter_get, | |
439 | cc_full_descr->descriptor->methods.source.notif_iter_next); | |
440 | break; | |
441 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
442 | comp_class = bt_component_class_sink_create( | |
443 | cc_full_descr->descriptor->name, | |
444 | cc_full_descr->descriptor->methods.sink.consume); | |
445 | break; | |
446 | default: | |
447 | printf_verbose("WARNING: Unknown component class type %d for component class %s in plugin %s\n", | |
448 | cc_full_descr->descriptor->type, | |
449 | cc_full_descr->descriptor->name, | |
450 | descriptor->name); | |
451 | continue; | |
452 | } | |
453 | ||
454 | if (!comp_class) { | |
455 | status = BT_PLUGIN_STATUS_ERROR; | |
456 | goto end; | |
457 | } | |
458 | ||
459 | if (cc_full_descr->description) { | |
460 | ret = bt_component_class_set_description(comp_class, | |
461 | cc_full_descr->description); | |
462 | if (ret) { | |
463 | status = BT_PLUGIN_STATUS_ERROR; | |
464 | BT_PUT(comp_class); | |
465 | goto end; | |
466 | } | |
467 | } | |
468 | ||
469 | if (cc_full_descr->init_method) { | |
470 | ret = bt_component_class_set_init_method(comp_class, | |
471 | cc_full_descr->init_method); | |
472 | if (ret) { | |
473 | status = BT_PLUGIN_STATUS_ERROR; | |
474 | BT_PUT(comp_class); | |
475 | goto end; | |
476 | } | |
477 | } | |
478 | ||
479 | if (cc_full_descr->destroy_method) { | |
480 | ret = bt_component_class_set_destroy_method(comp_class, | |
481 | cc_full_descr->destroy_method); | |
482 | if (ret) { | |
483 | status = BT_PLUGIN_STATUS_ERROR; | |
484 | BT_PUT(comp_class); | |
485 | goto end; | |
486 | } | |
487 | } | |
488 | ||
489 | switch (cc_full_descr->descriptor->type) { | |
490 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
491 | if (cc_full_descr->iterator_methods.init) { | |
492 | ret = bt_component_class_source_set_notification_iterator_init_method( | |
493 | comp_class, | |
494 | cc_full_descr->iterator_methods.init); | |
495 | if (ret) { | |
496 | status = BT_PLUGIN_STATUS_ERROR; | |
497 | BT_PUT(comp_class); | |
498 | goto end; | |
499 | } | |
500 | } | |
501 | ||
502 | if (cc_full_descr->iterator_methods.destroy) { | |
503 | ret = bt_component_class_source_set_notification_iterator_destroy_method( | |
504 | comp_class, | |
505 | cc_full_descr->iterator_methods.destroy); | |
506 | if (ret) { | |
507 | status = BT_PLUGIN_STATUS_ERROR; | |
508 | BT_PUT(comp_class); | |
509 | goto end; | |
510 | } | |
511 | } | |
512 | ||
513 | if (cc_full_descr->iterator_methods.seek_time) { | |
514 | ret = bt_component_class_source_set_notification_iterator_seek_time_method( | |
515 | comp_class, | |
516 | cc_full_descr->iterator_methods.seek_time); | |
517 | if (ret) { | |
518 | status = BT_PLUGIN_STATUS_ERROR; | |
519 | BT_PUT(comp_class); | |
520 | goto end; | |
521 | } | |
522 | } | |
523 | break; | |
524 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
525 | if (cc_full_descr->filter_add_iterator_method) { | |
526 | ret = bt_component_class_filter_set_add_iterator_method( | |
527 | comp_class, | |
528 | cc_full_descr->filter_add_iterator_method); | |
529 | if (ret) { | |
530 | status = BT_PLUGIN_STATUS_ERROR; | |
531 | BT_PUT(comp_class); | |
532 | goto end; | |
533 | } | |
534 | } | |
535 | ||
536 | if (cc_full_descr->iterator_methods.init) { | |
537 | ret = bt_component_class_filter_set_notification_iterator_init_method( | |
538 | comp_class, | |
539 | cc_full_descr->iterator_methods.init); | |
540 | if (ret) { | |
541 | status = BT_PLUGIN_STATUS_ERROR; | |
542 | BT_PUT(comp_class); | |
543 | goto end; | |
544 | } | |
545 | } | |
546 | ||
547 | if (cc_full_descr->iterator_methods.destroy) { | |
548 | ret = bt_component_class_filter_set_notification_iterator_destroy_method( | |
549 | comp_class, | |
550 | cc_full_descr->iterator_methods.destroy); | |
551 | if (ret) { | |
552 | status = BT_PLUGIN_STATUS_ERROR; | |
553 | BT_PUT(comp_class); | |
554 | goto end; | |
555 | } | |
556 | } | |
557 | ||
558 | if (cc_full_descr->iterator_methods.seek_time) { | |
559 | ret = bt_component_class_filter_set_notification_iterator_seek_time_method( | |
560 | comp_class, | |
561 | cc_full_descr->iterator_methods.seek_time); | |
562 | if (ret) { | |
563 | status = BT_PLUGIN_STATUS_ERROR; | |
564 | BT_PUT(comp_class); | |
565 | goto end; | |
566 | } | |
567 | } | |
568 | break; | |
569 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
570 | if (cc_full_descr->sink_add_iterator_method) { | |
571 | ret = bt_component_class_sink_set_add_iterator_method( | |
572 | comp_class, | |
573 | cc_full_descr->sink_add_iterator_method); | |
574 | if (ret) { | |
575 | status = BT_PLUGIN_STATUS_ERROR; | |
576 | BT_PUT(comp_class); | |
577 | goto end; | |
578 | } | |
579 | } | |
580 | break; | |
581 | default: | |
582 | assert(false); | |
583 | break; | |
584 | } | |
585 | ||
586 | /* | |
587 | * Add component class to the plugin object. | |
588 | * | |
589 | * This will call back | |
590 | * bt_plugin_so_on_add_component_class() so that we can | |
591 | * add a mapping in comp_classes_to_shlib_handles when | |
592 | * we know the component class is successfully added. | |
593 | */ | |
594 | status = bt_plugin_add_component_class(plugin, | |
595 | comp_class); | |
596 | BT_PUT(comp_class); | |
597 | if (status < 0) { | |
598 | printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n", | |
599 | cc_full_descr->descriptor->name, | |
600 | cc_full_descr->descriptor->type, | |
601 | bt_plugin_get_name(plugin), status); | |
602 | goto end; | |
603 | } | |
604 | } | |
605 | ||
606 | /* | |
607 | * All the plugin's component classes should be added at this | |
608 | * point. We freeze the plugin so that it's not possible to add | |
609 | * component classes to this plugin object after this stage | |
610 | * (plugin object becomes immutable). | |
611 | */ | |
612 | bt_plugin_freeze(plugin); | |
613 | ||
614 | end: | |
615 | g_array_free(comp_class_full_descriptors, TRUE); | |
616 | return status; | |
617 | } | |
618 | ||
619 | static | |
620 | struct bt_plugin *bt_plugin_so_create_empty( | |
621 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle) | |
622 | { | |
623 | struct bt_plugin *plugin; | |
624 | struct bt_plugin_so_spec_data *spec; | |
625 | ||
626 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO); | |
627 | if (!plugin) { | |
628 | goto error; | |
629 | } | |
630 | ||
631 | plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); | |
632 | if (!plugin->spec_data) { | |
633 | goto error; | |
634 | } | |
635 | ||
636 | spec = plugin->spec_data; | |
637 | spec->shared_lib_handle = bt_get(shared_lib_handle); | |
638 | goto end; | |
639 | ||
640 | error: | |
641 | BT_PUT(plugin); | |
642 | ||
643 | end: | |
644 | return plugin; | |
645 | } | |
646 | ||
647 | static | |
648 | struct bt_plugin **bt_plugin_so_create_all_from_sections( | |
649 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle, | |
650 | struct __bt_plugin_descriptor const * const *descriptors_begin, | |
651 | struct __bt_plugin_descriptor const * const *descriptors_end, | |
652 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
653 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
654 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
655 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
656 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
657 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) | |
658 | { | |
659 | size_t descriptor_count; | |
660 | size_t attrs_count; | |
661 | size_t cc_descriptors_count; | |
662 | size_t cc_descr_attrs_count; | |
663 | size_t i; | |
664 | struct bt_plugin **plugins = NULL; | |
665 | ||
666 | descriptor_count = descriptors_end - descriptors_begin; | |
667 | attrs_count = attrs_end - attrs_begin; | |
668 | cc_descriptors_count = cc_descriptors_end - cc_descriptors_begin; | |
669 | cc_descr_attrs_count = cc_descr_attrs_end - cc_descr_attrs_begin; | |
670 | printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n", | |
671 | descriptors_begin, descriptors_end, descriptor_count); | |
672 | printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n", | |
673 | attrs_begin, attrs_end, attrs_count); | |
674 | printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n", | |
675 | cc_descriptors_begin, cc_descriptors_end, cc_descriptors_count); | |
676 | printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n", | |
677 | cc_descr_attrs_begin, cc_descr_attrs_end, cc_descr_attrs_count); | |
678 | plugins = calloc(descriptor_count + 1, sizeof(*plugins)); | |
679 | if (!plugins) { | |
680 | goto error; | |
681 | } | |
682 | ||
683 | for (i = 0; i < descriptor_count; i++) { | |
684 | enum bt_plugin_status status; | |
685 | const struct __bt_plugin_descriptor *descriptor = | |
686 | descriptors_begin[i]; | |
687 | struct bt_plugin *plugin; | |
688 | ||
689 | printf_verbose("Loading plugin %s (ABI %d.%d)\n", descriptor->name, | |
690 | descriptor->major, descriptor->minor); | |
691 | ||
692 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { | |
693 | printf_error("Unknown plugin's major version: %d\n", | |
694 | descriptor->major); | |
695 | goto error; | |
696 | } | |
697 | ||
698 | plugin = bt_plugin_so_create_empty(shared_lib_handle); | |
699 | if (!plugin) { | |
700 | printf_error("Cannot allocate plugin object for plugin %s\n", | |
701 | descriptor->name); | |
702 | goto error; | |
703 | } | |
704 | ||
705 | if (shared_lib_handle && shared_lib_handle->path) { | |
706 | bt_plugin_set_path(plugin, shared_lib_handle->path->str); | |
707 | } | |
708 | ||
709 | status = bt_plugin_so_init(plugin, descriptor, attrs_begin, | |
710 | attrs_end, cc_descriptors_begin, cc_descriptors_end, | |
711 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
712 | if (status < 0) { | |
713 | printf_error("Cannot initialize plugin object %s\n", | |
714 | descriptor->name); | |
715 | BT_PUT(plugin); | |
716 | goto error; | |
717 | } | |
718 | ||
719 | /* Transfer ownership to the array */ | |
720 | plugins[i] = plugin; | |
721 | } | |
722 | ||
723 | goto end; | |
724 | ||
725 | error: | |
726 | g_free(plugins); | |
727 | plugins = NULL; | |
728 | ||
729 | end: | |
730 | return plugins; | |
731 | } | |
732 | ||
733 | BT_HIDDEN | |
734 | struct bt_plugin **bt_plugin_so_create_all_from_static(void) | |
735 | { | |
736 | struct bt_plugin **plugins = NULL; | |
737 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = | |
738 | bt_plugin_so_shared_lib_handle_create(NULL); | |
739 | ||
740 | if (!shared_lib_handle) { | |
741 | goto end; | |
742 | } | |
743 | ||
744 | plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, | |
745 | SECTION_BEGIN(__bt_plugin_descriptors), | |
746 | SECTION_END(__bt_plugin_descriptors), | |
747 | SECTION_BEGIN(__bt_plugin_descriptor_attributes), | |
748 | SECTION_END(__bt_plugin_descriptor_attributes), | |
749 | SECTION_BEGIN(__bt_plugin_component_class_descriptors), | |
750 | SECTION_END(__bt_plugin_component_class_descriptors), | |
751 | SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes), | |
752 | SECTION_END(__bt_plugin_component_class_descriptor_attributes)); | |
753 | ||
754 | end: | |
755 | BT_PUT(shared_lib_handle); | |
756 | ||
757 | return plugins; | |
758 | } | |
759 | ||
760 | BT_HIDDEN | |
761 | struct bt_plugin **bt_plugin_so_create_all_from_file(const char *path) | |
762 | { | |
763 | size_t path_len; | |
764 | struct bt_plugin **plugins = NULL; | |
765 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; | |
766 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; | |
767 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; | |
768 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; | |
769 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; | |
770 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; | |
771 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; | |
772 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; | |
773 | bool is_libtool_wrapper = false, is_shared_object = false; | |
774 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; | |
775 | ||
776 | if (!path) { | |
777 | goto end; | |
778 | } | |
779 | ||
780 | path_len = strlen(path); | |
781 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
782 | goto end; | |
783 | } | |
784 | ||
785 | path_len++; | |
786 | /* | |
787 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
788 | * or .la on Linux). | |
789 | */ | |
790 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, | |
791 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, | |
792 | LIBTOOL_PLUGIN_SUFFIX_LEN); | |
793 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, | |
794 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, | |
795 | NATIVE_PLUGIN_SUFFIX_LEN); | |
796 | if (!is_shared_object && !is_libtool_wrapper) { | |
797 | /* Name indicates that this is not a plugin file. */ | |
798 | goto end; | |
799 | } | |
800 | ||
801 | shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path); | |
802 | if (!shared_lib_handle) { | |
803 | goto end; | |
804 | } | |
805 | ||
806 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptors", | |
807 | (gpointer *) &descriptors_begin)) { | |
808 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
809 | "__start___bt_plugin_descriptors", | |
810 | g_module_name(shared_lib_handle->module)); | |
811 | goto end; | |
812 | } | |
813 | ||
814 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptors", | |
815 | (gpointer *) &descriptors_end)) { | |
816 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
817 | "__stop___bt_plugin_descriptors", | |
818 | g_module_name(shared_lib_handle->module)); | |
819 | goto end; | |
820 | } | |
821 | ||
822 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptor_attributes", | |
823 | (gpointer *) &attrs_begin)) { | |
824 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
825 | "__start___bt_plugin_descriptor_attributes", | |
826 | g_module_name(shared_lib_handle->module)); | |
827 | } | |
828 | ||
829 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptor_attributes", | |
830 | (gpointer *) &attrs_end)) { | |
831 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
832 | "__stop___bt_plugin_descriptor_attributes", | |
833 | g_module_name(shared_lib_handle->module)); | |
834 | } | |
835 | ||
836 | if ((!!attrs_begin - !!attrs_end) != 0) { | |
837 | printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n", | |
838 | g_module_name(shared_lib_handle->module)); | |
839 | goto end; | |
840 | } | |
841 | ||
842 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptors", | |
843 | (gpointer *) &cc_descriptors_begin)) { | |
844 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
845 | "__start___bt_plugin_component_class_descriptors", | |
846 | g_module_name(shared_lib_handle->module)); | |
847 | } | |
848 | ||
849 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptors", | |
850 | (gpointer *) &cc_descriptors_end)) { | |
851 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
852 | "__stop___bt_plugin_component_class_descriptors", | |
853 | g_module_name(shared_lib_handle->module)); | |
854 | } | |
855 | ||
856 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { | |
857 | printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n", | |
858 | g_module_name(shared_lib_handle->module)); | |
859 | goto end; | |
860 | } | |
861 | ||
862 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptor_attributes", | |
863 | (gpointer *) &cc_descr_attrs_begin)) { | |
864 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
865 | "__start___bt_plugin_component_class_descriptor_attributes", | |
866 | g_module_name(shared_lib_handle->module)); | |
867 | } | |
868 | ||
869 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptor_attributes", | |
870 | (gpointer *) &cc_descr_attrs_end)) { | |
871 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
872 | "__stop___bt_plugin_component_class_descriptor_attributes", | |
873 | g_module_name(shared_lib_handle->module)); | |
874 | } | |
875 | ||
876 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { | |
877 | printf_verbose("Found __start___bt_plugin_component_class_descriptor_attributes or __stop___bt_plugin_component_class_descriptor_attributes symbol, but not both in %s\n", | |
878 | g_module_name(shared_lib_handle->module)); | |
879 | goto end; | |
880 | } | |
881 | ||
882 | /* Initialize plugin */ | |
883 | plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, | |
884 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, | |
885 | cc_descriptors_begin, cc_descriptors_end, | |
886 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
887 | ||
888 | end: | |
889 | BT_PUT(shared_lib_handle); | |
890 | return plugins; | |
891 | } | |
892 | ||
893 | static | |
894 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, | |
895 | void *data) | |
896 | { | |
897 | gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles, | |
898 | comp_class); | |
899 | assert(exists); | |
900 | } | |
901 | ||
902 | BT_HIDDEN | |
903 | int bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, | |
904 | struct bt_component_class *comp_class) | |
905 | { | |
906 | int ret; | |
907 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
908 | ||
909 | assert(plugin->spec_data); | |
910 | assert(plugin->type == BT_PLUGIN_TYPE_SO); | |
911 | ||
912 | /* Map component class pointer to shared lib handle in global HT */ | |
913 | g_hash_table_insert(comp_classes_to_shlib_handles, comp_class, | |
914 | bt_get(spec->shared_lib_handle)); | |
915 | ||
916 | /* Add our custom destroy listener */ | |
917 | ret = bt_component_class_add_destroy_listener(comp_class, | |
918 | plugin_comp_class_destroy_listener, NULL); | |
919 | if (ret) { | |
920 | goto error; | |
921 | } | |
922 | goto end; | |
923 | ||
924 | error: | |
925 | /* Remove entry from global hash table (if exists) */ | |
926 | g_hash_table_remove(comp_classes_to_shlib_handles, | |
927 | comp_class); | |
928 | ||
929 | end: | |
930 | return ret; | |
931 | } |