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 | ||
52238017 | 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 | ||
424b8c43 | 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; |
90157d89 | 285 | struct bt_component_class_notification_iterator_methods iterator_methods; |
55bb57e0 PP |
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 | ||
52238017 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 | ||
52238017 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 | ||
52238017 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 | 468 | break; |
55bb57e0 | 469 | default: |
50ad9320 PP |
470 | /* |
471 | * WARN-level logging because | |
472 | * this should not happen with | |
473 | * the appropriate ABI version. | |
474 | * If we're here, we know that | |
475 | * for the reported version of | |
476 | * the ABI, this attribute is | |
477 | * unknown. | |
478 | */ | |
3fe0bf43 PP |
479 | BT_LOGW("Ignoring unknown component class descriptor attribute: " |
480 | "plugin-path=\"%s\", " | |
481 | "plugin-name=\"%s\", " | |
482 | "comp-class-name=\"%s\", " | |
483 | "comp-class-type=%s, " | |
484 | "attr-type-name=\"%s\", " | |
485 | "attr-type-id=%d", | |
486 | spec->shared_lib_handle->path ? | |
487 | spec->shared_lib_handle->path->str : | |
488 | NULL, | |
489 | descriptor->name, | |
55bb57e0 | 490 | cur_cc_descr_attr->comp_class_descriptor->name, |
3fe0bf43 PP |
491 | bt_component_class_type_string( |
492 | cur_cc_descr_attr->comp_class_descriptor->type), | |
493 | cur_cc_descr_attr->type_name, | |
494 | cur_cc_descr_attr->type); | |
55bb57e0 PP |
495 | break; |
496 | } | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | /* Initialize plugin */ | |
502 | if (spec->init) { | |
3fe0bf43 | 503 | BT_LOGD_STR("Calling user's plugin initialization function."); |
55bb57e0 | 504 | status = spec->init(plugin); |
3fe0bf43 PP |
505 | BT_LOGD("User function returned: %s", |
506 | bt_plugin_status_string(status)); | |
507 | ||
55bb57e0 | 508 | if (status < 0) { |
3fe0bf43 | 509 | BT_LOGW_STR("User's plugin initialization function failed."); |
55bb57e0 PP |
510 | goto end; |
511 | } | |
512 | } | |
513 | ||
c55a9f58 | 514 | spec->shared_lib_handle->init_called = BT_TRUE; |
55bb57e0 PP |
515 | |
516 | /* Add described component classes to plugin */ | |
517 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
518 | struct comp_class_full_descriptor *cc_full_descr = | |
519 | &g_array_index(comp_class_full_descriptors, | |
520 | struct comp_class_full_descriptor, i); | |
521 | struct bt_component_class *comp_class; | |
522 | ||
3fe0bf43 PP |
523 | BT_LOGD("Creating and setting properties of plugin's component class: " |
524 | "plugin-path=\"%s\", plugin-name=\"%s\", " | |
525 | "comp-class-name=\"%s\", comp-class-type=%s", | |
526 | spec->shared_lib_handle->path ? | |
527 | spec->shared_lib_handle->path->str : | |
528 | NULL, | |
529 | descriptor->name, | |
530 | cc_full_descr->descriptor->name, | |
531 | bt_component_class_type_string( | |
532 | cc_full_descr->descriptor->type)); | |
533 | ||
55bb57e0 PP |
534 | switch (cc_full_descr->descriptor->type) { |
535 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
536 | comp_class = bt_component_class_source_create( | |
537 | cc_full_descr->descriptor->name, | |
55bb57e0 PP |
538 | cc_full_descr->descriptor->methods.source.notif_iter_next); |
539 | break; | |
540 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
541 | comp_class = bt_component_class_filter_create( | |
542 | cc_full_descr->descriptor->name, | |
55bb57e0 PP |
543 | cc_full_descr->descriptor->methods.source.notif_iter_next); |
544 | break; | |
545 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
546 | comp_class = bt_component_class_sink_create( | |
547 | cc_full_descr->descriptor->name, | |
548 | cc_full_descr->descriptor->methods.sink.consume); | |
549 | break; | |
550 | default: | |
50ad9320 PP |
551 | /* |
552 | * WARN-level logging because this should not | |
553 | * happen with the appropriate ABI version. If | |
554 | * we're here, we know that for the reported | |
555 | * version of the ABI, this component class type | |
556 | * is unknown. | |
557 | */ | |
3fe0bf43 PP |
558 | BT_LOGW("Ignoring unknown component class type: " |
559 | "plugin-path=\"%s\", plugin-name=\"%s\", " | |
560 | "comp-class-name=\"%s\", comp-class-type=%d", | |
561 | spec->shared_lib_handle->path->str ? | |
562 | spec->shared_lib_handle->path->str : | |
563 | NULL, | |
564 | descriptor->name, | |
55bb57e0 | 565 | cc_full_descr->descriptor->name, |
3fe0bf43 | 566 | cc_full_descr->descriptor->type); |
55bb57e0 PP |
567 | continue; |
568 | } | |
569 | ||
570 | if (!comp_class) { | |
3fe0bf43 | 571 | BT_LOGE_STR("Cannot create component class."); |
55bb57e0 PP |
572 | status = BT_PLUGIN_STATUS_ERROR; |
573 | goto end; | |
574 | } | |
575 | ||
576 | if (cc_full_descr->description) { | |
577 | ret = bt_component_class_set_description(comp_class, | |
578 | cc_full_descr->description); | |
579 | if (ret) { | |
3fe0bf43 | 580 | BT_LOGE_STR("Cannot set component class's description."); |
55bb57e0 PP |
581 | status = BT_PLUGIN_STATUS_ERROR; |
582 | BT_PUT(comp_class); | |
583 | goto end; | |
584 | } | |
585 | } | |
586 | ||
279b3f15 PP |
587 | if (cc_full_descr->help) { |
588 | ret = bt_component_class_set_help(comp_class, | |
589 | cc_full_descr->help); | |
590 | if (ret) { | |
3fe0bf43 | 591 | BT_LOGE_STR("Cannot set component class's help string."); |
279b3f15 PP |
592 | status = BT_PLUGIN_STATUS_ERROR; |
593 | BT_PUT(comp_class); | |
594 | goto end; | |
595 | } | |
596 | } | |
597 | ||
55bb57e0 PP |
598 | if (cc_full_descr->init_method) { |
599 | ret = bt_component_class_set_init_method(comp_class, | |
600 | cc_full_descr->init_method); | |
601 | if (ret) { | |
3fe0bf43 | 602 | BT_LOGE_STR("Cannot set component class's initialization method."); |
55bb57e0 PP |
603 | status = BT_PLUGIN_STATUS_ERROR; |
604 | BT_PUT(comp_class); | |
605 | goto end; | |
606 | } | |
607 | } | |
608 | ||
64cadc66 PP |
609 | if (cc_full_descr->finalize_method) { |
610 | ret = bt_component_class_set_finalize_method(comp_class, | |
611 | cc_full_descr->finalize_method); | |
55bb57e0 | 612 | if (ret) { |
3fe0bf43 | 613 | BT_LOGE_STR("Cannot set component class's finalization method."); |
55bb57e0 PP |
614 | status = BT_PLUGIN_STATUS_ERROR; |
615 | BT_PUT(comp_class); | |
616 | goto end; | |
617 | } | |
618 | } | |
619 | ||
a67681c1 PP |
620 | if (cc_full_descr->query_method) { |
621 | ret = bt_component_class_set_query_method( | |
622 | comp_class, cc_full_descr->query_method); | |
8463eac2 | 623 | if (ret) { |
3fe0bf43 | 624 | BT_LOGE_STR("Cannot set component class's query method."); |
8463eac2 PP |
625 | status = BT_PLUGIN_STATUS_ERROR; |
626 | BT_PUT(comp_class); | |
627 | goto end; | |
628 | } | |
629 | } | |
630 | ||
72b913fb PP |
631 | if (cc_full_descr->accept_port_connection_method) { |
632 | ret = bt_component_class_set_accept_port_connection_method( | |
633 | comp_class, cc_full_descr->accept_port_connection_method); | |
634 | if (ret) { | |
3fe0bf43 | 635 | BT_LOGE_STR("Cannot set component class's \"accept port connection\" method."); |
72b913fb PP |
636 | status = BT_PLUGIN_STATUS_ERROR; |
637 | BT_PUT(comp_class); | |
638 | goto end; | |
639 | } | |
640 | } | |
641 | ||
0d8b4d8e PP |
642 | if (cc_full_descr->port_connected_method) { |
643 | ret = bt_component_class_set_port_connected_method( | |
644 | comp_class, cc_full_descr->port_connected_method); | |
645 | if (ret) { | |
3fe0bf43 | 646 | BT_LOGE_STR("Cannot set component class's \"port connected\" method."); |
0d8b4d8e PP |
647 | status = BT_PLUGIN_STATUS_ERROR; |
648 | BT_PUT(comp_class); | |
649 | goto end; | |
650 | } | |
651 | } | |
652 | ||
72b913fb PP |
653 | if (cc_full_descr->port_disconnected_method) { |
654 | ret = bt_component_class_set_port_disconnected_method( | |
655 | comp_class, cc_full_descr->port_disconnected_method); | |
2d41b99e | 656 | if (ret) { |
3fe0bf43 | 657 | BT_LOGE_STR("Cannot set component class's \"port disconnected\" method."); |
2d41b99e JG |
658 | status = BT_PLUGIN_STATUS_ERROR; |
659 | BT_PUT(comp_class); | |
660 | goto end; | |
661 | } | |
662 | } | |
663 | ||
55bb57e0 PP |
664 | switch (cc_full_descr->descriptor->type) { |
665 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
666 | if (cc_full_descr->iterator_methods.init) { | |
667 | ret = bt_component_class_source_set_notification_iterator_init_method( | |
668 | comp_class, | |
669 | cc_full_descr->iterator_methods.init); | |
670 | if (ret) { | |
3fe0bf43 | 671 | BT_LOGE_STR("Cannot set component class's notification iterator initialization method."); |
55bb57e0 PP |
672 | status = BT_PLUGIN_STATUS_ERROR; |
673 | BT_PUT(comp_class); | |
674 | goto end; | |
675 | } | |
676 | } | |
677 | ||
64cadc66 PP |
678 | if (cc_full_descr->iterator_methods.finalize) { |
679 | ret = bt_component_class_source_set_notification_iterator_finalize_method( | |
55bb57e0 | 680 | comp_class, |
64cadc66 | 681 | cc_full_descr->iterator_methods.finalize); |
55bb57e0 | 682 | if (ret) { |
3fe0bf43 | 683 | BT_LOGE_STR("Cannot set source component class's notification iterator finalization method."); |
55bb57e0 PP |
684 | status = BT_PLUGIN_STATUS_ERROR; |
685 | BT_PUT(comp_class); | |
686 | goto end; | |
687 | } | |
688 | } | |
55bb57e0 PP |
689 | break; |
690 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
55bb57e0 PP |
691 | if (cc_full_descr->iterator_methods.init) { |
692 | ret = bt_component_class_filter_set_notification_iterator_init_method( | |
693 | comp_class, | |
694 | cc_full_descr->iterator_methods.init); | |
695 | if (ret) { | |
3fe0bf43 | 696 | BT_LOGE_STR("Cannot set filter component class's notification iterator initialization method."); |
55bb57e0 PP |
697 | status = BT_PLUGIN_STATUS_ERROR; |
698 | BT_PUT(comp_class); | |
699 | goto end; | |
700 | } | |
701 | } | |
702 | ||
64cadc66 PP |
703 | if (cc_full_descr->iterator_methods.finalize) { |
704 | ret = bt_component_class_filter_set_notification_iterator_finalize_method( | |
55bb57e0 | 705 | comp_class, |
64cadc66 | 706 | cc_full_descr->iterator_methods.finalize); |
55bb57e0 | 707 | if (ret) { |
3fe0bf43 | 708 | BT_LOGE_STR("Cannot set filter component class's notification iterator finalization method."); |
55bb57e0 PP |
709 | status = BT_PLUGIN_STATUS_ERROR; |
710 | BT_PUT(comp_class); | |
711 | goto end; | |
712 | } | |
713 | } | |
55bb57e0 PP |
714 | break; |
715 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
55bb57e0 PP |
716 | break; |
717 | default: | |
0fbb9a9f | 718 | abort(); |
55bb57e0 PP |
719 | } |
720 | ||
721 | /* | |
722 | * Add component class to the plugin object. | |
723 | * | |
724 | * This will call back | |
725 | * bt_plugin_so_on_add_component_class() so that we can | |
bfa9a4be | 726 | * add a mapping in the component class list when |
55bb57e0 PP |
727 | * we know the component class is successfully added. |
728 | */ | |
729 | status = bt_plugin_add_component_class(plugin, | |
730 | comp_class); | |
731 | BT_PUT(comp_class); | |
732 | if (status < 0) { | |
3fe0bf43 | 733 | BT_LOGE("Cannot add component class to plugin."); |
55bb57e0 PP |
734 | goto end; |
735 | } | |
736 | } | |
737 | ||
738 | /* | |
739 | * All the plugin's component classes should be added at this | |
740 | * point. We freeze the plugin so that it's not possible to add | |
741 | * component classes to this plugin object after this stage | |
742 | * (plugin object becomes immutable). | |
743 | */ | |
744 | bt_plugin_freeze(plugin); | |
745 | ||
746 | end: | |
747 | g_array_free(comp_class_full_descriptors, TRUE); | |
748 | return status; | |
749 | } | |
750 | ||
751 | static | |
752 | struct bt_plugin *bt_plugin_so_create_empty( | |
753 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle) | |
754 | { | |
755 | struct bt_plugin *plugin; | |
756 | struct bt_plugin_so_spec_data *spec; | |
757 | ||
758 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO); | |
759 | if (!plugin) { | |
760 | goto error; | |
761 | } | |
762 | ||
6fbd4105 | 763 | plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data; |
55bb57e0 PP |
764 | plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); |
765 | if (!plugin->spec_data) { | |
3fe0bf43 | 766 | BT_LOGE_STR("Failed to allocate one SO plugin specific data structure."); |
55bb57e0 PP |
767 | goto error; |
768 | } | |
769 | ||
770 | spec = plugin->spec_data; | |
771 | spec->shared_lib_handle = bt_get(shared_lib_handle); | |
772 | goto end; | |
773 | ||
774 | error: | |
775 | BT_PUT(plugin); | |
776 | ||
777 | end: | |
778 | return plugin; | |
779 | } | |
780 | ||
52238017 MJ |
781 | static |
782 | size_t count_non_null_items_in_section(const void *begin, const void *end) | |
783 | { | |
784 | size_t count = 0; | |
785 | const int * const *begin_int = (const int * const *) begin; | |
786 | const int * const *end_int = (const int * const *) end; | |
787 | const int * const *iter; | |
788 | ||
789 | for (iter = begin_int; iter != end_int; iter++) { | |
790 | if (*iter) { | |
791 | count++; | |
792 | } | |
793 | } | |
794 | ||
795 | return count; | |
796 | } | |
797 | ||
55bb57e0 | 798 | static |
a8ff38ef | 799 | struct bt_plugin_set *bt_plugin_so_create_all_from_sections( |
55bb57e0 PP |
800 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle, |
801 | struct __bt_plugin_descriptor const * const *descriptors_begin, | |
802 | struct __bt_plugin_descriptor const * const *descriptors_end, | |
803 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
804 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
805 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
806 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
807 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
808 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) | |
809 | { | |
810 | size_t descriptor_count; | |
811 | size_t attrs_count; | |
812 | size_t cc_descriptors_count; | |
813 | size_t cc_descr_attrs_count; | |
814 | size_t i; | |
a8ff38ef | 815 | struct bt_plugin_set *plugin_set = NULL; |
55bb57e0 | 816 | |
52238017 MJ |
817 | descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end); |
818 | attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end); | |
819 | cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end); | |
820 | cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end); | |
3fe0bf43 PP |
821 | |
822 | BT_LOGD("Creating all SO plugins from sections: " | |
823 | "plugin-path=\"%s\", " | |
824 | "descr-begin-addr=%p, descr-end-addr=%p, " | |
825 | "attrs-begin-addr=%p, attrs-end-addr=%p, " | |
826 | "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " | |
827 | "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, " | |
828 | "descr-count=%zu, attrs-count=%zu, " | |
829 | "cc-descr-count=%zu, cc-descr-attrs-count=%zu", | |
830 | shared_lib_handle->path ? shared_lib_handle->path->str : NULL, | |
831 | descriptors_begin, descriptors_end, | |
832 | attrs_begin, attrs_end, | |
833 | cc_descriptors_begin, cc_descriptors_end, | |
834 | cc_descr_attrs_begin, cc_descr_attrs_end, | |
835 | descriptor_count, attrs_count, | |
836 | cc_descriptors_count, cc_descr_attrs_count); | |
a8ff38ef PP |
837 | plugin_set = bt_plugin_set_create(); |
838 | if (!plugin_set) { | |
3fe0bf43 | 839 | BT_LOGE_STR("Cannot create empty plugin set."); |
55bb57e0 PP |
840 | goto error; |
841 | } | |
842 | ||
52238017 | 843 | for (i = 0; i < descriptors_end - descriptors_begin; i++) { |
55bb57e0 PP |
844 | enum bt_plugin_status status; |
845 | const struct __bt_plugin_descriptor *descriptor = | |
846 | descriptors_begin[i]; | |
847 | struct bt_plugin *plugin; | |
848 | ||
52238017 MJ |
849 | if (descriptor == NULL) { |
850 | continue; | |
851 | } | |
852 | ||
3fe0bf43 PP |
853 | BT_LOGD("Creating plugin object for plugin: " |
854 | "name=\"%s\", abi-major=%d, abi-minor=%d", | |
855 | descriptor->name, descriptor->major, descriptor->minor); | |
55bb57e0 PP |
856 | |
857 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { | |
50ad9320 PP |
858 | /* |
859 | * DEBUG-level logging because we're only | |
860 | * _trying_ to open this file as a compatible | |
861 | * Babeltrace plugin: if it's not, it's not an | |
862 | * error. And because this can be tried during | |
863 | * bt_plugin_create_all_from_dir(), it's not | |
864 | * even a warning. | |
865 | */ | |
866 | BT_LOGD("Unknown ABI major version: abi-major=%d", | |
55bb57e0 PP |
867 | descriptor->major); |
868 | goto error; | |
869 | } | |
870 | ||
871 | plugin = bt_plugin_so_create_empty(shared_lib_handle); | |
872 | if (!plugin) { | |
3fe0bf43 | 873 | BT_LOGE_STR("Cannot create empty shared library handle."); |
55bb57e0 PP |
874 | goto error; |
875 | } | |
876 | ||
877 | if (shared_lib_handle && shared_lib_handle->path) { | |
878 | bt_plugin_set_path(plugin, shared_lib_handle->path->str); | |
879 | } | |
880 | ||
881 | status = bt_plugin_so_init(plugin, descriptor, attrs_begin, | |
882 | attrs_end, cc_descriptors_begin, cc_descriptors_end, | |
883 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
884 | if (status < 0) { | |
50ad9320 PP |
885 | /* |
886 | * DEBUG-level logging because we're only | |
887 | * _trying_ to open this file as a compatible | |
888 | * Babeltrace plugin: if it's not, it's not an | |
889 | * error. And because this can be tried during | |
890 | * bt_plugin_create_all_from_dir(), it's not | |
891 | * even a warning. | |
892 | */ | |
893 | BT_LOGD_STR("Cannot initialize SO plugin object from sections."); | |
55bb57e0 PP |
894 | BT_PUT(plugin); |
895 | goto error; | |
896 | } | |
897 | ||
a8ff38ef PP |
898 | /* Add to plugin set */ |
899 | bt_plugin_set_add_plugin(plugin_set, plugin); | |
900 | bt_put(plugin); | |
55bb57e0 PP |
901 | } |
902 | ||
903 | goto end; | |
904 | ||
905 | error: | |
a8ff38ef | 906 | BT_PUT(plugin_set); |
55bb57e0 PP |
907 | |
908 | end: | |
a8ff38ef | 909 | return plugin_set; |
55bb57e0 PP |
910 | } |
911 | ||
912 | BT_HIDDEN | |
a8ff38ef | 913 | struct bt_plugin_set *bt_plugin_so_create_all_from_static(void) |
55bb57e0 | 914 | { |
a8ff38ef | 915 | struct bt_plugin_set *plugin_set = NULL; |
55bb57e0 PP |
916 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = |
917 | bt_plugin_so_shared_lib_handle_create(NULL); | |
918 | ||
919 | if (!shared_lib_handle) { | |
920 | goto end; | |
921 | } | |
922 | ||
3fe0bf43 | 923 | BT_LOGD_STR("Creating all SO plugins from built-in plugins."); |
a8ff38ef | 924 | plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
52238017 MJ |
925 | __bt_get_begin_section_plugin_descriptors(), |
926 | __bt_get_end_section_plugin_descriptors(), | |
927 | __bt_get_begin_section_plugin_descriptor_attributes(), | |
928 | __bt_get_end_section_plugin_descriptor_attributes(), | |
929 | __bt_get_begin_section_component_class_descriptors(), | |
930 | __bt_get_end_section_component_class_descriptors(), | |
931 | __bt_get_begin_section_component_class_descriptor_attributes(), | |
932 | __bt_get_end_section_component_class_descriptor_attributes()); | |
55bb57e0 PP |
933 | |
934 | end: | |
935 | BT_PUT(shared_lib_handle); | |
936 | ||
a8ff38ef | 937 | return plugin_set; |
55bb57e0 PP |
938 | } |
939 | ||
940 | BT_HIDDEN | |
a8ff38ef | 941 | struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path) |
55bb57e0 PP |
942 | { |
943 | size_t path_len; | |
a8ff38ef | 944 | struct bt_plugin_set *plugin_set = NULL; |
55bb57e0 PP |
945 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; |
946 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; | |
947 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; | |
948 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; | |
949 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; | |
950 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; | |
951 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; | |
952 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; | |
52238017 MJ |
953 | struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void); |
954 | struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void); | |
955 | struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void); | |
956 | struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void); | |
957 | struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void); | |
958 | struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void); | |
959 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void); | |
960 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void); | |
c55a9f58 | 961 | bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE; |
55bb57e0 PP |
962 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; |
963 | ||
964 | if (!path) { | |
3fe0bf43 | 965 | BT_LOGW_STR("Invalid parameter: path is NULL."); |
55bb57e0 PP |
966 | goto end; |
967 | } | |
968 | ||
3fe0bf43 | 969 | BT_LOGD("Creating all SO plugins from file: path=\"%s\"", path); |
55bb57e0 PP |
970 | path_len = strlen(path); |
971 | if (path_len <= PLUGIN_SUFFIX_LEN) { | |
3fe0bf43 PP |
972 | BT_LOGW("Invalid parameter: path length is too short: " |
973 | "path-length=%zu", path_len); | |
55bb57e0 PP |
974 | goto end; |
975 | } | |
976 | ||
977 | path_len++; | |
978 | /* | |
979 | * Check if the file ends with a known plugin file type suffix (i.e. .so | |
980 | * or .la on Linux). | |
981 | */ | |
982 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, | |
983 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, | |
984 | LIBTOOL_PLUGIN_SUFFIX_LEN); | |
985 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, | |
986 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, | |
987 | NATIVE_PLUGIN_SUFFIX_LEN); | |
988 | if (!is_shared_object && !is_libtool_wrapper) { | |
3fe0bf43 PP |
989 | /* Name indicates this is not a plugin file; not an error */ |
990 | BT_LOGV("File is not a SO plugin file: path=\"%s\"", path); | |
55bb57e0 PP |
991 | goto end; |
992 | } | |
993 | ||
994 | shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path); | |
995 | if (!shared_lib_handle) { | |
50ad9320 | 996 | BT_LOGD_STR("Cannot create shared library handle."); |
55bb57e0 PP |
997 | goto end; |
998 | } | |
999 | ||
52238017 MJ |
1000 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors", |
1001 | (gpointer *) &get_begin_section_plugin_descriptors)) { | |
1002 | descriptors_begin = get_begin_section_plugin_descriptors(); | |
1003 | } else { | |
3fe0bf43 PP |
1004 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1005 | "symbol=\"%s\"", path, | |
52238017 | 1006 | "__bt_get_begin_section_plugin_descriptors"); |
55bb57e0 PP |
1007 | goto end; |
1008 | } | |
1009 | ||
52238017 MJ |
1010 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors", |
1011 | (gpointer *) &get_end_section_plugin_descriptors)) { | |
1012 | descriptors_end = get_end_section_plugin_descriptors(); | |
1013 | } else { | |
3fe0bf43 PP |
1014 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1015 | "symbol=\"%s\"", path, | |
52238017 | 1016 | "__bt_get_end_section_plugin_descriptors"); |
55bb57e0 PP |
1017 | goto end; |
1018 | } | |
1019 | ||
52238017 MJ |
1020 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes", |
1021 | (gpointer *) &get_begin_section_plugin_descriptor_attributes)) { | |
1022 | attrs_begin = get_begin_section_plugin_descriptor_attributes(); | |
1023 | } else { | |
3fe0bf43 PP |
1024 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1025 | "symbol=\"%s\"", path, | |
52238017 | 1026 | "__bt_get_begin_section_plugin_descriptor_attributes"); |
55bb57e0 PP |
1027 | } |
1028 | ||
52238017 MJ |
1029 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes", |
1030 | (gpointer *) &get_end_section_plugin_descriptor_attributes)) { | |
1031 | attrs_end = get_end_section_plugin_descriptor_attributes(); | |
1032 | } else { | |
3fe0bf43 PP |
1033 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1034 | "symbol=\"%s\"", path, | |
52238017 | 1035 | "__bt_get_end_section_plugin_descriptor_attributes"); |
55bb57e0 PP |
1036 | } |
1037 | ||
1038 | if ((!!attrs_begin - !!attrs_end) != 0) { | |
3fe0bf43 PP |
1039 | BT_LOGD("Found section start or end symbol, but not both: " |
1040 | "path=\"%s\", symbol-start=\"%s\", " | |
1041 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1042 | "symbol-end-addr=%p", | |
52238017 MJ |
1043 | path, "__bt_get_begin_section_plugin_descriptor_attributes", |
1044 | "__bt_get_end_section_plugin_descriptor_attributes", | |
3fe0bf43 | 1045 | attrs_begin, attrs_end); |
55bb57e0 PP |
1046 | goto end; |
1047 | } | |
1048 | ||
52238017 MJ |
1049 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors", |
1050 | (gpointer *) &get_begin_section_component_class_descriptors)) { | |
1051 | cc_descriptors_begin = get_begin_section_component_class_descriptors(); | |
1052 | } else { | |
3fe0bf43 PP |
1053 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1054 | "symbol=\"%s\"", path, | |
52238017 | 1055 | "__bt_get_begin_section_component_class_descriptors"); |
55bb57e0 PP |
1056 | } |
1057 | ||
52238017 MJ |
1058 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors", |
1059 | (gpointer *) &get_end_section_component_class_descriptors)) { | |
1060 | cc_descriptors_end = get_end_section_component_class_descriptors(); | |
1061 | } else { | |
3fe0bf43 PP |
1062 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1063 | "symbol=\"%s\"", path, | |
52238017 | 1064 | "__bt_get_end_section_component_class_descriptors"); |
55bb57e0 PP |
1065 | } |
1066 | ||
1067 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { | |
3fe0bf43 PP |
1068 | BT_LOGD("Found section start or end symbol, but not both: " |
1069 | "path=\"%s\", symbol-start=\"%s\", " | |
1070 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1071 | "symbol-end-addr=%p", | |
52238017 MJ |
1072 | path, "__bt_get_begin_section_component_class_descriptors", |
1073 | "__bt_get_end_section_component_class_descriptors", | |
3fe0bf43 | 1074 | cc_descriptors_begin, cc_descriptors_end); |
55bb57e0 PP |
1075 | goto end; |
1076 | } | |
1077 | ||
52238017 MJ |
1078 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes", |
1079 | (gpointer *) &get_begin_section_component_class_descriptor_attributes)) { | |
1080 | cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes(); | |
1081 | } else { | |
3fe0bf43 PP |
1082 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1083 | "symbol=\"%s\"", path, | |
52238017 | 1084 | "__bt_get_begin_section_component_class_descriptor_attributes"); |
55bb57e0 PP |
1085 | } |
1086 | ||
52238017 MJ |
1087 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes", |
1088 | (gpointer *) &get_end_section_component_class_descriptor_attributes)) { | |
1089 | cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes(); | |
1090 | } else { | |
3fe0bf43 PP |
1091 | BT_LOGD("Cannot resolve plugin symbol: path=\"%s\", " |
1092 | "symbol=\"%s\"", path, | |
52238017 | 1093 | "__bt_get_end_section_component_class_descriptor_attributes"); |
55bb57e0 PP |
1094 | } |
1095 | ||
1096 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { | |
3fe0bf43 PP |
1097 | BT_LOGD("Found section start or end symbol, but not both: " |
1098 | "path=\"%s\", symbol-start=\"%s\", " | |
1099 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1100 | "symbol-end-addr=%p", | |
52238017 MJ |
1101 | path, "__bt_get_begin_section_component_class_descriptor_attributes", |
1102 | "__bt_get_end_section_component_class_descriptor_attributes", | |
3fe0bf43 | 1103 | cc_descr_attrs_begin, cc_descr_attrs_end); |
55bb57e0 PP |
1104 | goto end; |
1105 | } | |
1106 | ||
1107 | /* Initialize plugin */ | |
3fe0bf43 | 1108 | BT_LOGD_STR("Initializing plugin object."); |
a8ff38ef | 1109 | plugin_set = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
55bb57e0 PP |
1110 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, |
1111 | cc_descriptors_begin, cc_descriptors_end, | |
1112 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
1113 | ||
1114 | end: | |
1115 | BT_PUT(shared_lib_handle); | |
a8ff38ef | 1116 | return plugin_set; |
55bb57e0 PP |
1117 | } |
1118 | ||
1119 | static | |
1120 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, | |
1121 | void *data) | |
1122 | { | |
bfa9a4be MD |
1123 | bt_list_del(&comp_class->node); |
1124 | BT_PUT(comp_class->so_handle); | |
1125 | BT_LOGV("Component class destroyed: removed entry from list: " | |
3fe0bf43 | 1126 | "comp-cls-addr=%p", comp_class); |
55bb57e0 PP |
1127 | } |
1128 | ||
1129 | BT_HIDDEN | |
3230ee6b | 1130 | void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, |
55bb57e0 PP |
1131 | struct bt_component_class *comp_class) |
1132 | { | |
55bb57e0 PP |
1133 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; |
1134 | ||
1135 | assert(plugin->spec_data); | |
1136 | assert(plugin->type == BT_PLUGIN_TYPE_SO); | |
1137 | ||
bfa9a4be MD |
1138 | bt_list_add(&comp_class->node, &component_class_list); |
1139 | comp_class->so_handle = bt_get(spec->shared_lib_handle); | |
55bb57e0 PP |
1140 | |
1141 | /* Add our custom destroy listener */ | |
3230ee6b | 1142 | bt_component_class_add_destroy_listener(comp_class, |
55bb57e0 | 1143 | plugin_comp_class_destroy_listener, NULL); |
55bb57e0 | 1144 | } |