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