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