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