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; | |
279b3f15 | 252 | const char *help; |
55bb57e0 PP |
253 | bt_component_class_init_method init_method; |
254 | bt_component_class_destroy_method destroy_method; | |
8463eac2 | 255 | bt_component_class_query_info_method query_info_method; |
55bb57e0 PP |
256 | bt_component_class_filter_add_iterator_method filter_add_iterator_method; |
257 | bt_component_class_sink_add_iterator_method sink_add_iterator_method; | |
258 | struct bt_component_class_iterator_methods iterator_methods; | |
259 | }; | |
260 | ||
261 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
262 | struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr; | |
263 | struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr; | |
264 | struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr; | |
265 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
266 | GArray *comp_class_full_descriptors; | |
267 | size_t i; | |
268 | int ret; | |
269 | ||
270 | comp_class_full_descriptors = g_array_new(FALSE, TRUE, | |
271 | sizeof(struct comp_class_full_descriptor)); | |
272 | if (!comp_class_full_descriptors) { | |
273 | status = BT_PLUGIN_STATUS_ERROR; | |
274 | goto end; | |
275 | } | |
276 | ||
277 | /* Set mandatory attributes */ | |
278 | spec->descriptor = descriptor; | |
279 | bt_plugin_set_name(plugin, descriptor->name); | |
280 | ||
281 | /* | |
282 | * Find and set optional attributes attached to this plugin | |
283 | * descriptor. | |
284 | */ | |
285 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { | |
286 | const struct __bt_plugin_descriptor_attribute *cur_attr = | |
287 | *cur_attr_ptr; | |
288 | ||
289 | if (cur_attr->plugin_descriptor != descriptor) { | |
290 | continue; | |
291 | } | |
292 | ||
293 | switch (cur_attr->type) { | |
294 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: | |
295 | spec->init = cur_attr->value.init; | |
296 | break; | |
297 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: | |
298 | spec->shared_lib_handle->exit = cur_attr->value.exit; | |
299 | break; | |
300 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: | |
301 | bt_plugin_set_author(plugin, cur_attr->value.author); | |
302 | break; | |
303 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: | |
304 | bt_plugin_set_license(plugin, cur_attr->value.license); | |
305 | break; | |
306 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
307 | bt_plugin_set_description(plugin, cur_attr->value.description); | |
308 | break; | |
309 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION: | |
310 | bt_plugin_set_version(plugin, | |
311 | (unsigned int) cur_attr->value.version.major, | |
312 | (unsigned int) cur_attr->value.version.minor, | |
313 | (unsigned int) cur_attr->value.version.patch, | |
314 | cur_attr->value.version.extra); | |
315 | break; | |
316 | default: | |
317 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for plugin %s\n", | |
318 | cur_attr->type_name, cur_attr->type, | |
319 | descriptor->name); | |
320 | break; | |
321 | } | |
322 | } | |
323 | ||
324 | /* | |
325 | * Find component class descriptors attached to this plugin | |
326 | * descriptor and initialize corresponding full component class | |
327 | * descriptors in the array. | |
328 | */ | |
329 | for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) { | |
330 | const struct __bt_plugin_component_class_descriptor *cur_cc_descr = | |
331 | *cur_cc_descr_ptr; | |
332 | struct comp_class_full_descriptor full_descriptor = {0}; | |
333 | ||
334 | if (cur_cc_descr->plugin_descriptor != descriptor) { | |
335 | continue; | |
336 | } | |
337 | ||
338 | full_descriptor.descriptor = cur_cc_descr; | |
339 | g_array_append_val(comp_class_full_descriptors, | |
340 | full_descriptor); | |
341 | } | |
342 | ||
343 | /* | |
344 | * Find component class descriptor attributes attached to this | |
345 | * plugin descriptor and update corresponding full component | |
346 | * class descriptors in the array. | |
347 | */ | |
348 | for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) { | |
349 | const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr = | |
350 | *cur_cc_descr_attr_ptr; | |
351 | ||
352 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != | |
353 | descriptor) { | |
354 | continue; | |
355 | } | |
356 | ||
357 | /* Find the corresponding component class descriptor entry */ | |
358 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
359 | struct comp_class_full_descriptor *cc_full_descr = | |
360 | &g_array_index(comp_class_full_descriptors, | |
361 | struct comp_class_full_descriptor, i); | |
362 | ||
363 | if (cur_cc_descr_attr->comp_class_descriptor == | |
364 | cc_full_descr->descriptor) { | |
365 | switch (cur_cc_descr_attr->type) { | |
366 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
367 | cc_full_descr->description = | |
368 | cur_cc_descr_attr->value.description; | |
369 | break; | |
279b3f15 PP |
370 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP: |
371 | cc_full_descr->help = | |
372 | cur_cc_descr_attr->value.help; | |
373 | break; | |
55bb57e0 PP |
374 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD: |
375 | cc_full_descr->init_method = | |
376 | cur_cc_descr_attr->value.init_method; | |
377 | break; | |
378 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESTROY_METHOD: | |
379 | cc_full_descr->destroy_method = | |
380 | cur_cc_descr_attr->value.destroy_method; | |
381 | break; | |
8463eac2 PP |
382 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_INFO_METHOD: |
383 | cc_full_descr->query_info_method = | |
384 | cur_cc_descr_attr->value.query_info_method; | |
385 | break; | |
55bb57e0 PP |
386 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FILTER_ADD_ITERATOR_METHOD: |
387 | cc_full_descr->filter_add_iterator_method = | |
388 | cur_cc_descr_attr->value.filter_add_iterator_method; | |
389 | break; | |
390 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_SINK_ADD_ITERATOR_METHOD: | |
391 | cc_full_descr->sink_add_iterator_method = | |
392 | cur_cc_descr_attr->value.sink_add_iterator_method; | |
393 | break; | |
394 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_INIT_METHOD: | |
395 | cc_full_descr->iterator_methods.init = | |
396 | cur_cc_descr_attr->value.notif_iter_init_method; | |
397 | break; | |
398 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_DESTROY_METHOD: | |
399 | cc_full_descr->iterator_methods.destroy = | |
400 | cur_cc_descr_attr->value.notif_iter_destroy_method; | |
401 | break; | |
402 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_NOTIF_ITER_SEEK_TIME_METHOD: | |
403 | cc_full_descr->iterator_methods.seek_time = | |
404 | cur_cc_descr_attr->value.notif_iter_seek_time_method; | |
405 | break; | |
406 | default: | |
407 | printf_verbose("WARNING: Unknown attribute \"%s\" (type %d) for component class %s (type %d) in plugin %s\n", | |
408 | cur_cc_descr_attr->type_name, | |
409 | cur_cc_descr_attr->type, | |
410 | cur_cc_descr_attr->comp_class_descriptor->name, | |
411 | cur_cc_descr_attr->comp_class_descriptor->type, | |
412 | descriptor->name); | |
413 | break; | |
414 | } | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | /* Initialize plugin */ | |
420 | if (spec->init) { | |
421 | status = spec->init(plugin); | |
422 | if (status < 0) { | |
423 | printf_verbose("Plugin `%s` initialization error: %d\n", | |
424 | bt_plugin_get_name(plugin), status); | |
425 | goto end; | |
426 | } | |
427 | } | |
428 | ||
429 | spec->shared_lib_handle->init_called = true; | |
430 | ||
431 | /* Add described component classes to plugin */ | |
432 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
433 | struct comp_class_full_descriptor *cc_full_descr = | |
434 | &g_array_index(comp_class_full_descriptors, | |
435 | struct comp_class_full_descriptor, i); | |
436 | struct bt_component_class *comp_class; | |
437 | ||
438 | switch (cc_full_descr->descriptor->type) { | |
439 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
440 | comp_class = bt_component_class_source_create( | |
441 | cc_full_descr->descriptor->name, | |
442 | cc_full_descr->descriptor->methods.source.notif_iter_get, | |
443 | cc_full_descr->descriptor->methods.source.notif_iter_next); | |
444 | break; | |
445 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
446 | comp_class = bt_component_class_filter_create( | |
447 | cc_full_descr->descriptor->name, | |
448 | cc_full_descr->descriptor->methods.source.notif_iter_get, | |
449 | cc_full_descr->descriptor->methods.source.notif_iter_next); | |
450 | break; | |
451 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
452 | comp_class = bt_component_class_sink_create( | |
453 | cc_full_descr->descriptor->name, | |
454 | cc_full_descr->descriptor->methods.sink.consume); | |
455 | break; | |
456 | default: | |
457 | printf_verbose("WARNING: Unknown component class type %d for component class %s in plugin %s\n", | |
458 | cc_full_descr->descriptor->type, | |
459 | cc_full_descr->descriptor->name, | |
460 | descriptor->name); | |
461 | continue; | |
462 | } | |
463 | ||
464 | if (!comp_class) { | |
465 | status = BT_PLUGIN_STATUS_ERROR; | |
466 | goto end; | |
467 | } | |
468 | ||
469 | if (cc_full_descr->description) { | |
470 | ret = bt_component_class_set_description(comp_class, | |
471 | cc_full_descr->description); | |
472 | if (ret) { | |
473 | status = BT_PLUGIN_STATUS_ERROR; | |
474 | BT_PUT(comp_class); | |
475 | goto end; | |
476 | } | |
477 | } | |
478 | ||
279b3f15 PP |
479 | if (cc_full_descr->help) { |
480 | ret = bt_component_class_set_help(comp_class, | |
481 | cc_full_descr->help); | |
482 | if (ret) { | |
483 | status = BT_PLUGIN_STATUS_ERROR; | |
484 | BT_PUT(comp_class); | |
485 | goto end; | |
486 | } | |
487 | } | |
488 | ||
55bb57e0 PP |
489 | if (cc_full_descr->init_method) { |
490 | ret = bt_component_class_set_init_method(comp_class, | |
491 | cc_full_descr->init_method); | |
492 | if (ret) { | |
493 | status = BT_PLUGIN_STATUS_ERROR; | |
494 | BT_PUT(comp_class); | |
495 | goto end; | |
496 | } | |
497 | } | |
498 | ||
499 | if (cc_full_descr->destroy_method) { | |
500 | ret = bt_component_class_set_destroy_method(comp_class, | |
501 | cc_full_descr->destroy_method); | |
502 | if (ret) { | |
503 | status = BT_PLUGIN_STATUS_ERROR; | |
504 | BT_PUT(comp_class); | |
505 | goto end; | |
506 | } | |
507 | } | |
508 | ||
8463eac2 PP |
509 | if (cc_full_descr->query_info_method) { |
510 | ret = bt_component_class_set_query_info_method( | |
511 | comp_class, cc_full_descr->query_info_method); | |
512 | if (ret) { | |
513 | status = BT_PLUGIN_STATUS_ERROR; | |
514 | BT_PUT(comp_class); | |
515 | goto end; | |
516 | } | |
517 | } | |
518 | ||
55bb57e0 PP |
519 | switch (cc_full_descr->descriptor->type) { |
520 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
521 | if (cc_full_descr->iterator_methods.init) { | |
522 | ret = bt_component_class_source_set_notification_iterator_init_method( | |
523 | comp_class, | |
524 | cc_full_descr->iterator_methods.init); | |
525 | if (ret) { | |
526 | status = BT_PLUGIN_STATUS_ERROR; | |
527 | BT_PUT(comp_class); | |
528 | goto end; | |
529 | } | |
530 | } | |
531 | ||
532 | if (cc_full_descr->iterator_methods.destroy) { | |
533 | ret = bt_component_class_source_set_notification_iterator_destroy_method( | |
534 | comp_class, | |
535 | cc_full_descr->iterator_methods.destroy); | |
536 | if (ret) { | |
537 | status = BT_PLUGIN_STATUS_ERROR; | |
538 | BT_PUT(comp_class); | |
539 | goto end; | |
540 | } | |
541 | } | |
542 | ||
543 | if (cc_full_descr->iterator_methods.seek_time) { | |
544 | ret = bt_component_class_source_set_notification_iterator_seek_time_method( | |
545 | comp_class, | |
546 | cc_full_descr->iterator_methods.seek_time); | |
547 | if (ret) { | |
548 | status = BT_PLUGIN_STATUS_ERROR; | |
549 | BT_PUT(comp_class); | |
550 | goto end; | |
551 | } | |
552 | } | |
553 | break; | |
554 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
555 | if (cc_full_descr->filter_add_iterator_method) { | |
556 | ret = bt_component_class_filter_set_add_iterator_method( | |
557 | comp_class, | |
558 | cc_full_descr->filter_add_iterator_method); | |
559 | if (ret) { | |
560 | status = BT_PLUGIN_STATUS_ERROR; | |
561 | BT_PUT(comp_class); | |
562 | goto end; | |
563 | } | |
564 | } | |
565 | ||
566 | if (cc_full_descr->iterator_methods.init) { | |
567 | ret = bt_component_class_filter_set_notification_iterator_init_method( | |
568 | comp_class, | |
569 | cc_full_descr->iterator_methods.init); | |
570 | if (ret) { | |
571 | status = BT_PLUGIN_STATUS_ERROR; | |
572 | BT_PUT(comp_class); | |
573 | goto end; | |
574 | } | |
575 | } | |
576 | ||
577 | if (cc_full_descr->iterator_methods.destroy) { | |
578 | ret = bt_component_class_filter_set_notification_iterator_destroy_method( | |
579 | comp_class, | |
580 | cc_full_descr->iterator_methods.destroy); | |
581 | if (ret) { | |
582 | status = BT_PLUGIN_STATUS_ERROR; | |
583 | BT_PUT(comp_class); | |
584 | goto end; | |
585 | } | |
586 | } | |
587 | ||
588 | if (cc_full_descr->iterator_methods.seek_time) { | |
589 | ret = bt_component_class_filter_set_notification_iterator_seek_time_method( | |
590 | comp_class, | |
591 | cc_full_descr->iterator_methods.seek_time); | |
592 | if (ret) { | |
593 | status = BT_PLUGIN_STATUS_ERROR; | |
594 | BT_PUT(comp_class); | |
595 | goto end; | |
596 | } | |
597 | } | |
598 | break; | |
599 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
600 | if (cc_full_descr->sink_add_iterator_method) { | |
601 | ret = bt_component_class_sink_set_add_iterator_method( | |
602 | comp_class, | |
603 | cc_full_descr->sink_add_iterator_method); | |
604 | if (ret) { | |
605 | status = BT_PLUGIN_STATUS_ERROR; | |
606 | BT_PUT(comp_class); | |
607 | goto end; | |
608 | } | |
609 | } | |
610 | break; | |
611 | default: | |
612 | assert(false); | |
613 | break; | |
614 | } | |
615 | ||
616 | /* | |
617 | * Add component class to the plugin object. | |
618 | * | |
619 | * This will call back | |
620 | * bt_plugin_so_on_add_component_class() so that we can | |
621 | * add a mapping in comp_classes_to_shlib_handles when | |
622 | * we know the component class is successfully added. | |
623 | */ | |
624 | status = bt_plugin_add_component_class(plugin, | |
625 | comp_class); | |
626 | BT_PUT(comp_class); | |
627 | if (status < 0) { | |
628 | printf_verbose("Cannot add component class %s (type %d) to plugin `%s`: status = %d\n", | |
629 | cc_full_descr->descriptor->name, | |
630 | cc_full_descr->descriptor->type, | |
631 | bt_plugin_get_name(plugin), status); | |
632 | goto end; | |
633 | } | |
634 | } | |
635 | ||
636 | /* | |
637 | * All the plugin's component classes should be added at this | |
638 | * point. We freeze the plugin so that it's not possible to add | |
639 | * component classes to this plugin object after this stage | |
640 | * (plugin object becomes immutable). | |
641 | */ | |
642 | bt_plugin_freeze(plugin); | |
643 | ||
644 | end: | |
645 | g_array_free(comp_class_full_descriptors, TRUE); | |
646 | return status; | |
647 | } | |
648 | ||
649 | static | |
650 | struct bt_plugin *bt_plugin_so_create_empty( | |
651 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle) | |
652 | { | |
653 | struct bt_plugin *plugin; | |
654 | struct bt_plugin_so_spec_data *spec; | |
655 | ||
656 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO); | |
657 | if (!plugin) { | |
658 | goto error; | |
659 | } | |
660 | ||
661 | plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); | |
662 | if (!plugin->spec_data) { | |
663 | goto error; | |
664 | } | |
665 | ||
666 | spec = plugin->spec_data; | |
667 | spec->shared_lib_handle = bt_get(shared_lib_handle); | |
668 | goto end; | |
669 | ||
670 | error: | |
671 | BT_PUT(plugin); | |
672 | ||
673 | end: | |
674 | return plugin; | |
675 | } | |
676 | ||
677 | static | |
678 | struct bt_plugin **bt_plugin_so_create_all_from_sections( | |
679 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle, | |
680 | struct __bt_plugin_descriptor const * const *descriptors_begin, | |
681 | struct __bt_plugin_descriptor const * const *descriptors_end, | |
682 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
683 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
684 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
685 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
686 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
687 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) | |
688 | { | |
689 | size_t descriptor_count; | |
690 | size_t attrs_count; | |
691 | size_t cc_descriptors_count; | |
692 | size_t cc_descr_attrs_count; | |
693 | size_t i; | |
694 | struct bt_plugin **plugins = NULL; | |
695 | ||
696 | descriptor_count = descriptors_end - descriptors_begin; | |
697 | attrs_count = attrs_end - attrs_begin; | |
698 | cc_descriptors_count = cc_descriptors_end - cc_descriptors_begin; | |
699 | cc_descr_attrs_count = cc_descr_attrs_end - cc_descr_attrs_begin; | |
700 | printf_verbose("Section: Plugin descriptors: [%p - %p], (%zu elements)\n", | |
701 | descriptors_begin, descriptors_end, descriptor_count); | |
702 | printf_verbose("Section: Plugin descriptor attributes: [%p - %p], (%zu elements)\n", | |
703 | attrs_begin, attrs_end, attrs_count); | |
704 | printf_verbose("Section: Plugin component class descriptors: [%p - %p], (%zu elements)\n", | |
705 | cc_descriptors_begin, cc_descriptors_end, cc_descriptors_count); | |
706 | printf_verbose("Section: Plugin component class descriptor attributes: [%p - %p], (%zu elements)\n", | |
707 | cc_descr_attrs_begin, cc_descr_attrs_end, cc_descr_attrs_count); | |
708 | plugins = calloc(descriptor_count + 1, sizeof(*plugins)); | |
709 | if (!plugins) { | |
710 | goto error; | |
711 | } | |
712 | ||
713 | for (i = 0; i < descriptor_count; i++) { | |
714 | enum bt_plugin_status status; | |
715 | const struct __bt_plugin_descriptor *descriptor = | |
716 | descriptors_begin[i]; | |
717 | struct bt_plugin *plugin; | |
718 | ||
719 | printf_verbose("Loading plugin %s (ABI %d.%d)\n", descriptor->name, | |
720 | descriptor->major, descriptor->minor); | |
721 | ||
722 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { | |
723 | printf_error("Unknown plugin's major version: %d\n", | |
724 | descriptor->major); | |
725 | goto error; | |
726 | } | |
727 | ||
728 | plugin = bt_plugin_so_create_empty(shared_lib_handle); | |
729 | if (!plugin) { | |
730 | printf_error("Cannot allocate plugin object for plugin %s\n", | |
731 | descriptor->name); | |
732 | goto error; | |
733 | } | |
734 | ||
735 | if (shared_lib_handle && shared_lib_handle->path) { | |
736 | bt_plugin_set_path(plugin, shared_lib_handle->path->str); | |
737 | } | |
738 | ||
739 | status = bt_plugin_so_init(plugin, descriptor, attrs_begin, | |
740 | attrs_end, cc_descriptors_begin, cc_descriptors_end, | |
741 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
742 | if (status < 0) { | |
743 | printf_error("Cannot initialize plugin object %s\n", | |
744 | descriptor->name); | |
745 | BT_PUT(plugin); | |
746 | goto error; | |
747 | } | |
748 | ||
749 | /* Transfer ownership to the array */ | |
750 | plugins[i] = plugin; | |
751 | } | |
752 | ||
753 | goto end; | |
754 | ||
755 | error: | |
756 | g_free(plugins); | |
757 | plugins = NULL; | |
758 | ||
759 | end: | |
760 | return plugins; | |
761 | } | |
762 | ||
763 | BT_HIDDEN | |
764 | struct bt_plugin **bt_plugin_so_create_all_from_static(void) | |
765 | { | |
766 | struct bt_plugin **plugins = NULL; | |
767 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = | |
768 | bt_plugin_so_shared_lib_handle_create(NULL); | |
769 | ||
770 | if (!shared_lib_handle) { | |
771 | goto end; | |
772 | } | |
773 | ||
774 | plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, | |
775 | SECTION_BEGIN(__bt_plugin_descriptors), | |
776 | SECTION_END(__bt_plugin_descriptors), | |
777 | SECTION_BEGIN(__bt_plugin_descriptor_attributes), | |
778 | SECTION_END(__bt_plugin_descriptor_attributes), | |
779 | SECTION_BEGIN(__bt_plugin_component_class_descriptors), | |
780 | SECTION_END(__bt_plugin_component_class_descriptors), | |
781 | SECTION_BEGIN(__bt_plugin_component_class_descriptor_attributes), | |
782 | SECTION_END(__bt_plugin_component_class_descriptor_attributes)); | |
783 | ||
784 | end: | |
785 | BT_PUT(shared_lib_handle); | |
786 | ||
787 | return plugins; | |
788 | } | |
789 | ||
790 | BT_HIDDEN | |
791 | struct bt_plugin **bt_plugin_so_create_all_from_file(const char *path) | |
792 | { | |
793 | size_t path_len; | |
794 | struct bt_plugin **plugins = NULL; | |
795 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; | |
796 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; | |
797 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; | |
798 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; | |
799 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; | |
800 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; | |
801 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; | |
802 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; | |
803 | bool is_libtool_wrapper = false, is_shared_object = false; | |
804 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; | |
805 | ||
806 | if (!path) { | |
807 | goto end; | |
808 | } | |
809 | ||
810 | path_len = strlen(path); | |
811 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
812 | goto end; | |
813 | } | |
814 | ||
815 | path_len++; | |
816 | /* | |
817 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
818 | * or .la on Linux). | |
819 | */ | |
820 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, | |
821 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, | |
822 | LIBTOOL_PLUGIN_SUFFIX_LEN); | |
823 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, | |
824 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, | |
825 | NATIVE_PLUGIN_SUFFIX_LEN); | |
826 | if (!is_shared_object && !is_libtool_wrapper) { | |
827 | /* Name indicates that this is not a plugin file. */ | |
828 | goto end; | |
829 | } | |
830 | ||
831 | shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path); | |
832 | if (!shared_lib_handle) { | |
833 | goto end; | |
834 | } | |
835 | ||
836 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptors", | |
837 | (gpointer *) &descriptors_begin)) { | |
838 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
839 | "__start___bt_plugin_descriptors", | |
840 | g_module_name(shared_lib_handle->module)); | |
841 | goto end; | |
842 | } | |
843 | ||
844 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptors", | |
845 | (gpointer *) &descriptors_end)) { | |
846 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
847 | "__stop___bt_plugin_descriptors", | |
848 | g_module_name(shared_lib_handle->module)); | |
849 | goto end; | |
850 | } | |
851 | ||
852 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_descriptor_attributes", | |
853 | (gpointer *) &attrs_begin)) { | |
854 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
855 | "__start___bt_plugin_descriptor_attributes", | |
856 | g_module_name(shared_lib_handle->module)); | |
857 | } | |
858 | ||
859 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_descriptor_attributes", | |
860 | (gpointer *) &attrs_end)) { | |
861 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
862 | "__stop___bt_plugin_descriptor_attributes", | |
863 | g_module_name(shared_lib_handle->module)); | |
864 | } | |
865 | ||
866 | if ((!!attrs_begin - !!attrs_end) != 0) { | |
867 | printf_verbose("Found __start___bt_plugin_descriptor_attributes or __stop___bt_plugin_descriptor_attributes symbol, but not both in %s\n", | |
868 | g_module_name(shared_lib_handle->module)); | |
869 | goto end; | |
870 | } | |
871 | ||
872 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptors", | |
873 | (gpointer *) &cc_descriptors_begin)) { | |
874 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
875 | "__start___bt_plugin_component_class_descriptors", | |
876 | g_module_name(shared_lib_handle->module)); | |
877 | } | |
878 | ||
879 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptors", | |
880 | (gpointer *) &cc_descriptors_end)) { | |
881 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
882 | "__stop___bt_plugin_component_class_descriptors", | |
883 | g_module_name(shared_lib_handle->module)); | |
884 | } | |
885 | ||
886 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { | |
887 | printf_verbose("Found __start___bt_plugin_component_class_descriptors or __stop___bt_plugin_component_class_descriptors symbol, but not both in %s\n", | |
888 | g_module_name(shared_lib_handle->module)); | |
889 | goto end; | |
890 | } | |
891 | ||
892 | if (!g_module_symbol(shared_lib_handle->module, "__start___bt_plugin_component_class_descriptor_attributes", | |
893 | (gpointer *) &cc_descr_attrs_begin)) { | |
894 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
895 | "__start___bt_plugin_component_class_descriptor_attributes", | |
896 | g_module_name(shared_lib_handle->module)); | |
897 | } | |
898 | ||
899 | if (!g_module_symbol(shared_lib_handle->module, "__stop___bt_plugin_component_class_descriptor_attributes", | |
900 | (gpointer *) &cc_descr_attrs_end)) { | |
901 | printf_verbose("Unable to resolve plugin symbol %s from %s\n", | |
902 | "__stop___bt_plugin_component_class_descriptor_attributes", | |
903 | g_module_name(shared_lib_handle->module)); | |
904 | } | |
905 | ||
906 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { | |
907 | 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", | |
908 | g_module_name(shared_lib_handle->module)); | |
909 | goto end; | |
910 | } | |
911 | ||
912 | /* Initialize plugin */ | |
913 | plugins = bt_plugin_so_create_all_from_sections(shared_lib_handle, | |
914 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, | |
915 | cc_descriptors_begin, cc_descriptors_end, | |
916 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
917 | ||
918 | end: | |
919 | BT_PUT(shared_lib_handle); | |
920 | return plugins; | |
921 | } | |
922 | ||
923 | static | |
924 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, | |
925 | void *data) | |
926 | { | |
927 | gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles, | |
928 | comp_class); | |
929 | assert(exists); | |
930 | } | |
931 | ||
932 | BT_HIDDEN | |
933 | int bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, | |
934 | struct bt_component_class *comp_class) | |
935 | { | |
936 | int ret; | |
937 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
938 | ||
939 | assert(plugin->spec_data); | |
940 | assert(plugin->type == BT_PLUGIN_TYPE_SO); | |
941 | ||
942 | /* Map component class pointer to shared lib handle in global HT */ | |
943 | g_hash_table_insert(comp_classes_to_shlib_handles, comp_class, | |
944 | bt_get(spec->shared_lib_handle)); | |
945 | ||
946 | /* Add our custom destroy listener */ | |
947 | ret = bt_component_class_add_destroy_listener(comp_class, | |
948 | plugin_comp_class_destroy_listener, NULL); | |
949 | if (ret) { | |
950 | goto error; | |
951 | } | |
952 | goto end; | |
953 | ||
954 | error: | |
955 | /* Remove entry from global hash table (if exists) */ | |
956 | g_hash_table_remove(comp_classes_to_shlib_handles, | |
957 | comp_class); | |
958 | ||
959 | end: | |
960 | return ret; | |
961 | } |