Commit | Line | Data |
---|---|---|
55bb57e0 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
55bb57e0 | 3 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
55bb57e0 PP |
4 | * |
5 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
350ad6c1 | 26 | #define BT_LOG_TAG "LIB/PLUGIN-SO" |
c2d9d9cf | 27 | #include "lib/logging.h" |
3fe0bf43 | 28 | |
578e048b MJ |
29 | #include "common/assert.h" |
30 | #include "lib/assert-pre.h" | |
31 | #include "compat/compiler.h" | |
3fadfbc0 | 32 | #include <babeltrace2/plugin/plugin-dev.h> |
578e048b | 33 | #include "lib/graph/component-class.h" |
3fadfbc0 MJ |
34 | #include <babeltrace2/graph/component-class.h> |
35 | #include <babeltrace2/graph/component-class-source.h> | |
36 | #include <babeltrace2/graph/component-class-filter.h> | |
37 | #include <babeltrace2/graph/component-class-sink.h> | |
38 | #include <babeltrace2/types.h> | |
578e048b | 39 | #include "common/list.h" |
55bb57e0 | 40 | #include <string.h> |
0fbb9a9f | 41 | #include <stdlib.h> |
55bb57e0 PP |
42 | #include <glib.h> |
43 | #include <gmodule.h> | |
44 | ||
578e048b MJ |
45 | #include "plugin.h" |
46 | #include "plugin-so.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 | ||
91d81473 | 53 | #define PLUGIN_SUFFIX_LEN bt_max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ |
55bb57e0 PP |
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 | 73 | * |
c8db3219 | 74 | * my_plugins = bt_plugin_find_all_from_file("/path/to/my-plugin.so"); |
55bb57e0 PP |
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); | |
65300d60 | 99 | BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle); |
55bb57e0 | 100 | } |
d94d92ac | 101 | |
bfa9a4be | 102 | BT_LOGD_STR("Released references from all component classes to shared library handles."); |
55bb57e0 PP |
103 | } |
104 | ||
9724cce9 | 105 | static inline |
a21d1cb8 | 106 | const char *bt_self_plugin_status_string(enum bt_self_plugin_status status) |
9724cce9 PP |
107 | { |
108 | switch (status) { | |
a21d1cb8 PP |
109 | case BT_SELF_PLUGIN_STATUS_OK: |
110 | return "BT_SELF_PLUGIN_STATUS_OK"; | |
111 | case BT_SELF_PLUGIN_STATUS_ERROR: | |
112 | return "BT_SELF_PLUGIN_STATUS_ERROR"; | |
113 | case BT_SELF_PLUGIN_STATUS_NOMEM: | |
114 | return "BT_SELF_PLUGIN_STATUS_NOMEM"; | |
9724cce9 PP |
115 | default: |
116 | return "(unknown)"; | |
117 | } | |
118 | } | |
119 | ||
55bb57e0 PP |
120 | static |
121 | void bt_plugin_so_shared_lib_handle_destroy(struct bt_object *obj) | |
122 | { | |
123 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle; | |
124 | ||
f6ccaed9 | 125 | BT_ASSERT(obj); |
55bb57e0 PP |
126 | shared_lib_handle = container_of(obj, |
127 | struct bt_plugin_so_shared_lib_handle, base); | |
3fe0bf43 PP |
128 | const char *path = shared_lib_handle->path ? |
129 | shared_lib_handle->path->str : NULL; | |
130 | ||
3f7d4d90 | 131 | BT_LOGI("Destroying shared library handle: addr=%p, path=\"%s\"", |
3fe0bf43 | 132 | shared_lib_handle, path); |
55bb57e0 PP |
133 | |
134 | if (shared_lib_handle->init_called && shared_lib_handle->exit) { | |
3fe0bf43 | 135 | BT_LOGD_STR("Calling user's plugin exit function."); |
9724cce9 PP |
136 | shared_lib_handle->exit(); |
137 | BT_LOGD_STR("User function returned."); | |
55bb57e0 PP |
138 | } |
139 | ||
140 | if (shared_lib_handle->module) { | |
3f3b1761 | 141 | #ifndef BT_DEBUG_MODE |
f1447220 PP |
142 | /* |
143 | * Valgrind shows incomplete stack traces when | |
144 | * dynamically loaded libraries are closed before it | |
145 | * finishes. Use the BABELTRACE_NO_DLCLOSE in a debug | |
146 | * build to avoid this. | |
147 | */ | |
148 | const char *var = getenv("BABELTRACE_NO_DLCLOSE"); | |
149 | ||
150 | if (!var || strcmp(var, "1") != 0) { | |
151 | #endif | |
3f7d4d90 | 152 | BT_LOGI("Closing GModule: path=\"%s\"", path); |
3fe0bf43 | 153 | |
f1447220 | 154 | if (!g_module_close(shared_lib_handle->module)) { |
3fe0bf43 PP |
155 | BT_LOGE("Cannot close GModule: %s: path=\"%s\"", |
156 | g_module_error(), path); | |
f1447220 | 157 | } |
db5504f9 PP |
158 | |
159 | shared_lib_handle->module = NULL; | |
3f3b1761 | 160 | #ifndef BT_DEBUG_MODE |
3fe0bf43 | 161 | } else { |
3f7d4d90 | 162 | BT_LOGI("Not closing GModule because `BABELTRACE_NO_DLCLOSE=1`: " |
3fe0bf43 | 163 | "path=\"%s\"", path); |
55bb57e0 | 164 | } |
f1447220 | 165 | #endif |
55bb57e0 PP |
166 | } |
167 | ||
168 | if (shared_lib_handle->path) { | |
169 | g_string_free(shared_lib_handle->path, TRUE); | |
db5504f9 | 170 | shared_lib_handle->path = NULL; |
55bb57e0 PP |
171 | } |
172 | ||
173 | g_free(shared_lib_handle); | |
174 | } | |
175 | ||
176 | static | |
9736d991 PP |
177 | enum bt_plugin_status bt_plugin_so_shared_lib_handle_create( |
178 | const char *path, | |
179 | struct bt_plugin_so_shared_lib_handle **shared_lib_handle) | |
55bb57e0 | 180 | { |
9736d991 | 181 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
55bb57e0 | 182 | |
9736d991 | 183 | BT_ASSERT(shared_lib_handle); |
3f7d4d90 | 184 | BT_LOGI("Creating shared library handle: path=\"%s\"", path); |
9736d991 PP |
185 | *shared_lib_handle = g_new0(struct bt_plugin_so_shared_lib_handle, 1); |
186 | if (!*shared_lib_handle) { | |
3fe0bf43 | 187 | BT_LOGE_STR("Failed to allocate one shared library handle."); |
9736d991 PP |
188 | status = BT_PLUGIN_STATUS_NOMEM; |
189 | goto end; | |
55bb57e0 PP |
190 | } |
191 | ||
9736d991 | 192 | bt_object_init_shared(&(*shared_lib_handle)->base, |
3fea54f6 | 193 | bt_plugin_so_shared_lib_handle_destroy); |
55bb57e0 PP |
194 | |
195 | if (!path) { | |
196 | goto end; | |
197 | } | |
198 | ||
9736d991 PP |
199 | (*shared_lib_handle)->path = g_string_new(path); |
200 | if (!(*shared_lib_handle)->path) { | |
3fe0bf43 | 201 | BT_LOGE_STR("Failed to allocate a GString."); |
9736d991 PP |
202 | status = BT_PLUGIN_STATUS_NOMEM; |
203 | goto end; | |
55bb57e0 PP |
204 | } |
205 | ||
9736d991 PP |
206 | (*shared_lib_handle)->module = g_module_open(path, G_MODULE_BIND_LOCAL); |
207 | if (!(*shared_lib_handle)->module) { | |
50ad9320 | 208 | /* |
3f7d4d90 | 209 | * INFO-level logging because we're only _trying_ to |
50ad9320 PP |
210 | * open this file as a Babeltrace plugin: if it's not, |
211 | * it's not an error. And because this can be tried | |
3f7d4d90 PP |
212 | * during bt_plugin_find_all_from_dir(), it's not even a |
213 | * warning. | |
50ad9320 | 214 | */ |
3f7d4d90 | 215 | BT_LOGI("Cannot open GModule: %s: path=\"%s\"", |
3fe0bf43 | 216 | g_module_error(), path); |
9736d991 PP |
217 | BT_OBJECT_PUT_REF_AND_RESET(*shared_lib_handle); |
218 | status = BT_PLUGIN_STATUS_NOT_FOUND; | |
219 | goto end; | |
55bb57e0 PP |
220 | } |
221 | ||
222 | goto end; | |
223 | ||
55bb57e0 | 224 | end: |
9736d991 PP |
225 | BT_ASSERT(*shared_lib_handle || status != BT_PLUGIN_STATUS_OK); |
226 | if (*shared_lib_handle) { | |
3f7d4d90 | 227 | BT_LOGI("Created shared library handle: path=\"%s\", addr=%p", |
9736d991 | 228 | path, *shared_lib_handle); |
3fe0bf43 PP |
229 | } |
230 | ||
9736d991 | 231 | return status; |
55bb57e0 PP |
232 | } |
233 | ||
6fbd4105 | 234 | static |
55bb57e0 PP |
235 | void bt_plugin_so_destroy_spec_data(struct bt_plugin *plugin) |
236 | { | |
237 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
238 | ||
239 | if (!plugin->spec_data) { | |
240 | return; | |
241 | } | |
242 | ||
f6ccaed9 PP |
243 | BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO); |
244 | BT_ASSERT(spec); | |
65300d60 | 245 | BT_OBJECT_PUT_REF_AND_RESET(spec->shared_lib_handle); |
55bb57e0 PP |
246 | g_free(plugin->spec_data); |
247 | plugin->spec_data = NULL; | |
248 | } | |
249 | ||
250 | /* | |
251 | * This function does the following: | |
252 | * | |
253 | * 1. Iterate on the plugin descriptor attributes section and set the | |
254 | * plugin's attributes depending on the attribute types. This | |
255 | * includes the name of the plugin, its description, and its | |
256 | * initialization function, for example. | |
257 | * | |
258 | * 2. Iterate on the component class descriptors section and create one | |
259 | * "full descriptor" (temporary structure) for each one that is found | |
260 | * and attached to our plugin descriptor. | |
261 | * | |
262 | * 3. Iterate on the component class descriptor attributes section and | |
263 | * set the corresponding full descriptor's attributes depending on | |
264 | * the attribute types. This includes the description of the | |
265 | * component class, as well as its initialization and destroy | |
266 | * methods. | |
267 | * | |
268 | * 4. Call the user's plugin initialization function, if any is | |
269 | * defined. | |
270 | * | |
271 | * 5. For each full component class descriptor, create a component class | |
272 | * object, set its optional attributes, and add it to the plugin | |
273 | * object. | |
274 | * | |
275 | * 6. Freeze the plugin object. | |
276 | */ | |
277 | static | |
9736d991 PP |
278 | enum bt_plugin_status bt_plugin_so_init(struct bt_plugin *plugin, |
279 | bool fail_on_load_error, | |
55bb57e0 PP |
280 | const struct __bt_plugin_descriptor *descriptor, |
281 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
282 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
283 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
284 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
285 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
286 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end) | |
287 | { | |
288 | /* | |
289 | * This structure's members point to the plugin's memory | |
290 | * (do NOT free). | |
291 | */ | |
292 | struct comp_class_full_descriptor { | |
293 | const struct __bt_plugin_component_class_descriptor *descriptor; | |
294 | const char *description; | |
279b3f15 | 295 | const char *help; |
d94d92ac PP |
296 | |
297 | union { | |
298 | struct { | |
0d72b8c3 PP |
299 | bt_component_class_source_init_method init; |
300 | bt_component_class_source_finalize_method finalize; | |
301 | bt_component_class_source_query_method query; | |
0d72b8c3 | 302 | bt_component_class_source_output_port_connected_method output_port_connected; |
d6e69534 PP |
303 | bt_component_class_source_message_iterator_init_method msg_iter_init; |
304 | bt_component_class_source_message_iterator_finalize_method msg_iter_finalize; | |
7474e7d3 PP |
305 | bt_component_class_source_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin; |
306 | bt_component_class_source_message_iterator_seek_beginning_method msg_iter_seek_beginning; | |
307 | bt_component_class_source_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin; | |
308 | bt_component_class_source_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning; | |
d94d92ac PP |
309 | } source; |
310 | ||
311 | struct { | |
0d72b8c3 PP |
312 | bt_component_class_filter_init_method init; |
313 | bt_component_class_filter_finalize_method finalize; | |
314 | bt_component_class_filter_query_method query; | |
0d72b8c3 PP |
315 | bt_component_class_filter_input_port_connected_method input_port_connected; |
316 | bt_component_class_filter_output_port_connected_method output_port_connected; | |
d6e69534 PP |
317 | bt_component_class_filter_message_iterator_init_method msg_iter_init; |
318 | bt_component_class_filter_message_iterator_finalize_method msg_iter_finalize; | |
7474e7d3 PP |
319 | bt_component_class_filter_message_iterator_seek_ns_from_origin_method msg_iter_seek_ns_from_origin; |
320 | bt_component_class_filter_message_iterator_seek_beginning_method msg_iter_seek_beginning; | |
321 | bt_component_class_filter_message_iterator_can_seek_ns_from_origin_method msg_iter_can_seek_ns_from_origin; | |
322 | bt_component_class_filter_message_iterator_can_seek_beginning_method msg_iter_can_seek_beginning; | |
d94d92ac PP |
323 | } filter; |
324 | ||
325 | struct { | |
0d72b8c3 PP |
326 | bt_component_class_sink_init_method init; |
327 | bt_component_class_sink_finalize_method finalize; | |
328 | bt_component_class_sink_query_method query; | |
0d72b8c3 | 329 | bt_component_class_sink_input_port_connected_method input_port_connected; |
5badd463 | 330 | bt_component_class_sink_graph_is_configured_method graph_is_configured; |
d94d92ac PP |
331 | } sink; |
332 | } methods; | |
55bb57e0 PP |
333 | }; |
334 | ||
335 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; | |
336 | struct __bt_plugin_descriptor_attribute const * const *cur_attr_ptr; | |
337 | struct __bt_plugin_component_class_descriptor const * const *cur_cc_descr_ptr; | |
338 | struct __bt_plugin_component_class_descriptor_attribute const * const *cur_cc_descr_attr_ptr; | |
339 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; | |
340 | GArray *comp_class_full_descriptors; | |
341 | size_t i; | |
342 | int ret; | |
343 | ||
3f7d4d90 | 344 | BT_LOGI("Initializing plugin object from descriptors found in sections: " |
3fe0bf43 PP |
345 | "plugin-addr=%p, plugin-path=\"%s\", " |
346 | "attrs-begin-addr=%p, attrs-end-addr=%p, " | |
347 | "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " | |
348 | "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p", | |
349 | plugin, | |
350 | spec->shared_lib_handle->path ? | |
351 | spec->shared_lib_handle->path->str : NULL, | |
352 | attrs_begin, attrs_end, | |
353 | cc_descriptors_begin, cc_descriptors_end, | |
354 | cc_descr_attrs_begin, cc_descr_attrs_end); | |
55bb57e0 PP |
355 | comp_class_full_descriptors = g_array_new(FALSE, TRUE, |
356 | sizeof(struct comp_class_full_descriptor)); | |
357 | if (!comp_class_full_descriptors) { | |
3fe0bf43 | 358 | BT_LOGE_STR("Failed to allocate a GArray."); |
9736d991 | 359 | status = BT_PLUGIN_STATUS_NOMEM; |
55bb57e0 PP |
360 | goto end; |
361 | } | |
362 | ||
363 | /* Set mandatory attributes */ | |
364 | spec->descriptor = descriptor; | |
365 | bt_plugin_set_name(plugin, descriptor->name); | |
366 | ||
367 | /* | |
368 | * Find and set optional attributes attached to this plugin | |
369 | * descriptor. | |
370 | */ | |
371 | for (cur_attr_ptr = attrs_begin; cur_attr_ptr != attrs_end; cur_attr_ptr++) { | |
372 | const struct __bt_plugin_descriptor_attribute *cur_attr = | |
373 | *cur_attr_ptr; | |
374 | ||
52238017 MJ |
375 | if (cur_attr == NULL) { |
376 | continue; | |
377 | } | |
378 | ||
55bb57e0 PP |
379 | if (cur_attr->plugin_descriptor != descriptor) { |
380 | continue; | |
381 | } | |
382 | ||
383 | switch (cur_attr->type) { | |
384 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_INIT: | |
385 | spec->init = cur_attr->value.init; | |
386 | break; | |
387 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_EXIT: | |
388 | spec->shared_lib_handle->exit = cur_attr->value.exit; | |
389 | break; | |
390 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_AUTHOR: | |
391 | bt_plugin_set_author(plugin, cur_attr->value.author); | |
392 | break; | |
393 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_LICENSE: | |
394 | bt_plugin_set_license(plugin, cur_attr->value.license); | |
395 | break; | |
396 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
397 | bt_plugin_set_description(plugin, cur_attr->value.description); | |
398 | break; | |
399 | case BT_PLUGIN_DESCRIPTOR_ATTRIBUTE_TYPE_VERSION: | |
400 | bt_plugin_set_version(plugin, | |
401 | (unsigned int) cur_attr->value.version.major, | |
402 | (unsigned int) cur_attr->value.version.minor, | |
403 | (unsigned int) cur_attr->value.version.patch, | |
404 | cur_attr->value.version.extra); | |
405 | break; | |
406 | default: | |
9736d991 PP |
407 | BT_LOG_WRITE(fail_on_load_error ? |
408 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
409 | "%s plugin descriptor attribute: " | |
3fe0bf43 PP |
410 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
411 | "attr-type-name=\"%s\", attr-type-id=%d", | |
9736d991 PP |
412 | fail_on_load_error ? "Unknown" : |
413 | "Ignoring unknown", | |
3fe0bf43 PP |
414 | spec->shared_lib_handle->path ? |
415 | spec->shared_lib_handle->path->str : | |
416 | NULL, | |
417 | descriptor->name, cur_attr->type_name, | |
418 | cur_attr->type); | |
9736d991 PP |
419 | |
420 | if (fail_on_load_error) { | |
421 | status = BT_PLUGIN_STATUS_LOADING_ERROR; | |
422 | goto end; | |
423 | } | |
424 | ||
55bb57e0 PP |
425 | break; |
426 | } | |
427 | } | |
428 | ||
429 | /* | |
430 | * Find component class descriptors attached to this plugin | |
431 | * descriptor and initialize corresponding full component class | |
432 | * descriptors in the array. | |
433 | */ | |
434 | for (cur_cc_descr_ptr = cc_descriptors_begin; cur_cc_descr_ptr != cc_descriptors_end; cur_cc_descr_ptr++) { | |
435 | const struct __bt_plugin_component_class_descriptor *cur_cc_descr = | |
436 | *cur_cc_descr_ptr; | |
437 | struct comp_class_full_descriptor full_descriptor = {0}; | |
438 | ||
52238017 MJ |
439 | if (cur_cc_descr == NULL) { |
440 | continue; | |
441 | } | |
442 | ||
55bb57e0 PP |
443 | if (cur_cc_descr->plugin_descriptor != descriptor) { |
444 | continue; | |
445 | } | |
446 | ||
447 | full_descriptor.descriptor = cur_cc_descr; | |
448 | g_array_append_val(comp_class_full_descriptors, | |
449 | full_descriptor); | |
450 | } | |
451 | ||
452 | /* | |
453 | * Find component class descriptor attributes attached to this | |
454 | * plugin descriptor and update corresponding full component | |
455 | * class descriptors in the array. | |
456 | */ | |
457 | for (cur_cc_descr_attr_ptr = cc_descr_attrs_begin; cur_cc_descr_attr_ptr != cc_descr_attrs_end; cur_cc_descr_attr_ptr++) { | |
458 | const struct __bt_plugin_component_class_descriptor_attribute *cur_cc_descr_attr = | |
459 | *cur_cc_descr_attr_ptr; | |
d94d92ac | 460 | enum bt_component_class_type cc_type; |
55bb57e0 | 461 | |
52238017 MJ |
462 | if (cur_cc_descr_attr == NULL) { |
463 | continue; | |
464 | } | |
465 | ||
55bb57e0 PP |
466 | if (cur_cc_descr_attr->comp_class_descriptor->plugin_descriptor != |
467 | descriptor) { | |
468 | continue; | |
469 | } | |
470 | ||
d94d92ac PP |
471 | cc_type = cur_cc_descr_attr->comp_class_descriptor->type; |
472 | ||
55bb57e0 PP |
473 | /* Find the corresponding component class descriptor entry */ |
474 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
475 | struct comp_class_full_descriptor *cc_full_descr = | |
476 | &g_array_index(comp_class_full_descriptors, | |
477 | struct comp_class_full_descriptor, i); | |
478 | ||
d94d92ac | 479 | if (cur_cc_descr_attr->comp_class_descriptor != |
55bb57e0 | 480 | cc_full_descr->descriptor) { |
d94d92ac PP |
481 | continue; |
482 | } | |
483 | ||
484 | switch (cur_cc_descr_attr->type) { | |
485 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_DESCRIPTION: | |
486 | cc_full_descr->description = | |
487 | cur_cc_descr_attr->value.description; | |
488 | break; | |
489 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_HELP: | |
490 | cc_full_descr->help = | |
491 | cur_cc_descr_attr->value.help; | |
492 | break; | |
493 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INIT_METHOD: | |
494 | switch (cc_type) { | |
495 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
496 | cc_full_descr->methods.source.init = | |
497 | cur_cc_descr_attr->value.source_init_method; | |
55bb57e0 | 498 | break; |
d94d92ac PP |
499 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
500 | cc_full_descr->methods.filter.init = | |
501 | cur_cc_descr_attr->value.filter_init_method; | |
279b3f15 | 502 | break; |
d94d92ac PP |
503 | case BT_COMPONENT_CLASS_TYPE_SINK: |
504 | cc_full_descr->methods.sink.init = | |
505 | cur_cc_descr_attr->value.sink_init_method; | |
55bb57e0 | 506 | break; |
d94d92ac PP |
507 | default: |
508 | abort(); | |
509 | } | |
510 | break; | |
511 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_FINALIZE_METHOD: | |
512 | switch (cc_type) { | |
513 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
514 | cc_full_descr->methods.source.finalize = | |
515 | cur_cc_descr_attr->value.source_finalize_method; | |
55bb57e0 | 516 | break; |
d94d92ac PP |
517 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
518 | cc_full_descr->methods.filter.finalize = | |
519 | cur_cc_descr_attr->value.filter_finalize_method; | |
8463eac2 | 520 | break; |
d94d92ac PP |
521 | case BT_COMPONENT_CLASS_TYPE_SINK: |
522 | cc_full_descr->methods.sink.finalize = | |
523 | cur_cc_descr_attr->value.sink_finalize_method; | |
72b913fb | 524 | break; |
d94d92ac PP |
525 | default: |
526 | abort(); | |
527 | } | |
528 | break; | |
529 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_QUERY_METHOD: | |
530 | switch (cc_type) { | |
531 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
532 | cc_full_descr->methods.source.query = | |
533 | cur_cc_descr_attr->value.source_query_method; | |
0d8b4d8e | 534 | break; |
d94d92ac PP |
535 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
536 | cc_full_descr->methods.filter.query = | |
537 | cur_cc_descr_attr->value.filter_query_method; | |
55bb57e0 | 538 | break; |
d94d92ac PP |
539 | case BT_COMPONENT_CLASS_TYPE_SINK: |
540 | cc_full_descr->methods.sink.query = | |
541 | cur_cc_descr_attr->value.sink_query_method; | |
55bb57e0 | 542 | break; |
d94d92ac PP |
543 | default: |
544 | abort(); | |
545 | } | |
546 | break; | |
d94d92ac PP |
547 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_INPUT_PORT_CONNECTED_METHOD: |
548 | switch (cc_type) { | |
549 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
550 | cc_full_descr->methods.filter.input_port_connected = | |
551 | cur_cc_descr_attr->value.filter_input_port_connected_method; | |
552 | break; | |
553 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
554 | cc_full_descr->methods.sink.input_port_connected = | |
555 | cur_cc_descr_attr->value.sink_input_port_connected_method; | |
55bb57e0 | 556 | break; |
55bb57e0 | 557 | default: |
d94d92ac PP |
558 | abort(); |
559 | } | |
560 | break; | |
561 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_OUTPUT_PORT_CONNECTED_METHOD: | |
562 | switch (cc_type) { | |
563 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
564 | cc_full_descr->methods.source.output_port_connected = | |
565 | cur_cc_descr_attr->value.source_output_port_connected_method; | |
566 | break; | |
567 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
568 | cc_full_descr->methods.filter.output_port_connected = | |
569 | cur_cc_descr_attr->value.filter_output_port_connected_method; | |
55bb57e0 | 570 | break; |
d94d92ac PP |
571 | default: |
572 | abort(); | |
55bb57e0 | 573 | } |
d94d92ac | 574 | break; |
5badd463 PP |
575 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_GRAPH_IS_CONFIGURED_METHOD: |
576 | switch (cc_type) { | |
577 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
578 | cc_full_descr->methods.sink.graph_is_configured = | |
579 | cur_cc_descr_attr->value.sink_graph_is_configured_method; | |
580 | break; | |
581 | default: | |
582 | abort(); | |
583 | } | |
584 | break; | |
d6e69534 | 585 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_INIT_METHOD: |
d94d92ac PP |
586 | switch (cc_type) { |
587 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
d6e69534 PP |
588 | cc_full_descr->methods.source.msg_iter_init = |
589 | cur_cc_descr_attr->value.source_msg_iter_init_method; | |
d94d92ac PP |
590 | break; |
591 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
d6e69534 PP |
592 | cc_full_descr->methods.filter.msg_iter_init = |
593 | cur_cc_descr_attr->value.filter_msg_iter_init_method; | |
d94d92ac PP |
594 | break; |
595 | default: | |
596 | abort(); | |
597 | } | |
598 | break; | |
d6e69534 | 599 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_FINALIZE_METHOD: |
d94d92ac PP |
600 | switch (cc_type) { |
601 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
d6e69534 PP |
602 | cc_full_descr->methods.source.msg_iter_finalize = |
603 | cur_cc_descr_attr->value.source_msg_iter_finalize_method; | |
d94d92ac PP |
604 | break; |
605 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
d6e69534 PP |
606 | cc_full_descr->methods.filter.msg_iter_finalize = |
607 | cur_cc_descr_attr->value.filter_msg_iter_finalize_method; | |
d94d92ac PP |
608 | break; |
609 | default: | |
610 | abort(); | |
611 | } | |
612 | break; | |
7474e7d3 PP |
613 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_NS_FROM_ORIGIN_METHOD: |
614 | switch (cc_type) { | |
615 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
616 | cc_full_descr->methods.source.msg_iter_seek_ns_from_origin = | |
617 | cur_cc_descr_attr->value.source_msg_iter_seek_ns_from_origin_method; | |
618 | break; | |
619 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
620 | cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin = | |
621 | cur_cc_descr_attr->value.filter_msg_iter_seek_ns_from_origin_method; | |
622 | break; | |
623 | default: | |
624 | abort(); | |
625 | } | |
626 | break; | |
627 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_SEEK_BEGINNING_METHOD: | |
628 | switch (cc_type) { | |
629 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
630 | cc_full_descr->methods.source.msg_iter_seek_beginning = | |
631 | cur_cc_descr_attr->value.source_msg_iter_seek_beginning_method; | |
632 | break; | |
633 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
634 | cc_full_descr->methods.filter.msg_iter_seek_beginning = | |
635 | cur_cc_descr_attr->value.filter_msg_iter_seek_beginning_method; | |
636 | break; | |
637 | default: | |
638 | abort(); | |
639 | } | |
640 | break; | |
641 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_NS_FROM_ORIGIN_METHOD: | |
642 | switch (cc_type) { | |
643 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
644 | cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin = | |
645 | cur_cc_descr_attr->value.source_msg_iter_can_seek_ns_from_origin_method; | |
646 | break; | |
647 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
648 | cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin = | |
649 | cur_cc_descr_attr->value.filter_msg_iter_can_seek_ns_from_origin_method; | |
650 | break; | |
651 | default: | |
652 | abort(); | |
653 | } | |
654 | break; | |
655 | case BT_PLUGIN_COMPONENT_CLASS_DESCRIPTOR_ATTRIBUTE_TYPE_MSG_ITER_CAN_SEEK_BEGINNING_METHOD: | |
656 | switch (cc_type) { | |
657 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
658 | cc_full_descr->methods.source.msg_iter_can_seek_beginning = | |
659 | cur_cc_descr_attr->value.source_msg_iter_can_seek_beginning_method; | |
660 | break; | |
661 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
662 | cc_full_descr->methods.filter.msg_iter_can_seek_beginning = | |
663 | cur_cc_descr_attr->value.filter_msg_iter_can_seek_beginning_method; | |
664 | break; | |
665 | default: | |
666 | abort(); | |
667 | } | |
668 | break; | |
d94d92ac | 669 | default: |
9736d991 PP |
670 | BT_LOG_WRITE(fail_on_load_error ? |
671 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
672 | "%s component class descriptor attribute: " | |
d94d92ac PP |
673 | "plugin-path=\"%s\", " |
674 | "plugin-name=\"%s\", " | |
675 | "comp-class-name=\"%s\", " | |
676 | "comp-class-type=%s, " | |
677 | "attr-type-name=\"%s\", " | |
678 | "attr-type-id=%d", | |
9736d991 PP |
679 | fail_on_load_error ? "Unknown" : |
680 | "Ignoring unknown", | |
d94d92ac PP |
681 | spec->shared_lib_handle->path ? |
682 | spec->shared_lib_handle->path->str : | |
683 | NULL, | |
684 | descriptor->name, | |
685 | cur_cc_descr_attr->comp_class_descriptor->name, | |
686 | bt_component_class_type_string( | |
687 | cur_cc_descr_attr->comp_class_descriptor->type), | |
688 | cur_cc_descr_attr->type_name, | |
689 | cur_cc_descr_attr->type); | |
9736d991 PP |
690 | |
691 | if (fail_on_load_error) { | |
692 | status = BT_PLUGIN_STATUS_LOADING_ERROR; | |
693 | goto end; | |
694 | } | |
695 | ||
d94d92ac | 696 | break; |
55bb57e0 PP |
697 | } |
698 | } | |
699 | } | |
700 | ||
701 | /* Initialize plugin */ | |
702 | if (spec->init) { | |
a21d1cb8 PP |
703 | enum bt_self_plugin_status init_status; |
704 | ||
3fe0bf43 | 705 | BT_LOGD_STR("Calling user's plugin initialization function."); |
a21d1cb8 | 706 | init_status = spec->init((void *) plugin); |
9736d991 | 707 | BT_LOGD("User function returned: status=%s", |
a21d1cb8 | 708 | bt_self_plugin_status_string(init_status)); |
3fe0bf43 | 709 | |
9724cce9 | 710 | if (init_status < 0) { |
9736d991 PP |
711 | BT_LOG_WRITE(fail_on_load_error ? |
712 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
713 | "User's plugin initialization function failed: " | |
714 | "status=%s", | |
715 | bt_self_plugin_status_string(init_status)); | |
716 | ||
717 | if (fail_on_load_error) { | |
718 | status = (int) init_status; | |
719 | goto end; | |
720 | } | |
55bb57e0 PP |
721 | } |
722 | } | |
723 | ||
c55a9f58 | 724 | spec->shared_lib_handle->init_called = BT_TRUE; |
55bb57e0 PP |
725 | |
726 | /* Add described component classes to plugin */ | |
727 | for (i = 0; i < comp_class_full_descriptors->len; i++) { | |
728 | struct comp_class_full_descriptor *cc_full_descr = | |
729 | &g_array_index(comp_class_full_descriptors, | |
730 | struct comp_class_full_descriptor, i); | |
0d72b8c3 PP |
731 | struct bt_component_class *comp_class = NULL; |
732 | struct bt_component_class_source *src_comp_class = NULL; | |
733 | struct bt_component_class_filter *flt_comp_class = NULL; | |
734 | struct bt_component_class_sink *sink_comp_class = NULL; | |
55bb57e0 | 735 | |
3f7d4d90 | 736 | BT_LOGI("Creating and setting properties of plugin's component class: " |
3fe0bf43 PP |
737 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
738 | "comp-class-name=\"%s\", comp-class-type=%s", | |
739 | spec->shared_lib_handle->path ? | |
740 | spec->shared_lib_handle->path->str : | |
741 | NULL, | |
742 | descriptor->name, | |
743 | cc_full_descr->descriptor->name, | |
744 | bt_component_class_type_string( | |
745 | cc_full_descr->descriptor->type)); | |
746 | ||
55bb57e0 PP |
747 | switch (cc_full_descr->descriptor->type) { |
748 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
0d72b8c3 | 749 | src_comp_class = bt_component_class_source_create( |
55bb57e0 | 750 | cc_full_descr->descriptor->name, |
d6e69534 | 751 | cc_full_descr->descriptor->methods.source.msg_iter_next); |
0d72b8c3 | 752 | comp_class = bt_component_class_source_as_component_class( |
d94d92ac | 753 | src_comp_class); |
55bb57e0 PP |
754 | break; |
755 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
0d72b8c3 | 756 | flt_comp_class = bt_component_class_filter_create( |
55bb57e0 | 757 | cc_full_descr->descriptor->name, |
d6e69534 | 758 | cc_full_descr->descriptor->methods.source.msg_iter_next); |
0d72b8c3 | 759 | comp_class = bt_component_class_filter_as_component_class( |
d94d92ac | 760 | flt_comp_class); |
55bb57e0 PP |
761 | break; |
762 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
0d72b8c3 | 763 | sink_comp_class = bt_component_class_sink_create( |
55bb57e0 PP |
764 | cc_full_descr->descriptor->name, |
765 | cc_full_descr->descriptor->methods.sink.consume); | |
0d72b8c3 | 766 | comp_class = bt_component_class_sink_as_component_class( |
d94d92ac | 767 | sink_comp_class); |
55bb57e0 PP |
768 | break; |
769 | default: | |
9736d991 PP |
770 | BT_LOG_WRITE(fail_on_load_error ? |
771 | BT_LOG_WARN : BT_LOG_INFO, BT_LOG_TAG, | |
772 | "%s component class type: " | |
3fe0bf43 PP |
773 | "plugin-path=\"%s\", plugin-name=\"%s\", " |
774 | "comp-class-name=\"%s\", comp-class-type=%d", | |
9736d991 PP |
775 | fail_on_load_error ? "Unknown" : |
776 | "Ignoring unknown", | |
3fe0bf43 PP |
777 | spec->shared_lib_handle->path->str ? |
778 | spec->shared_lib_handle->path->str : | |
779 | NULL, | |
780 | descriptor->name, | |
55bb57e0 | 781 | cc_full_descr->descriptor->name, |
3fe0bf43 | 782 | cc_full_descr->descriptor->type); |
9736d991 PP |
783 | |
784 | if (fail_on_load_error) { | |
785 | status = BT_PLUGIN_STATUS_LOADING_ERROR; | |
786 | goto end; | |
787 | } | |
788 | ||
55bb57e0 PP |
789 | continue; |
790 | } | |
791 | ||
792 | if (!comp_class) { | |
3fe0bf43 | 793 | BT_LOGE_STR("Cannot create component class."); |
9736d991 | 794 | status = BT_PLUGIN_STATUS_NOMEM; |
55bb57e0 PP |
795 | goto end; |
796 | } | |
797 | ||
798 | if (cc_full_descr->description) { | |
0d72b8c3 | 799 | ret = bt_component_class_set_description( |
d94d92ac | 800 | comp_class, cc_full_descr->description); |
55bb57e0 | 801 | if (ret) { |
3fe0bf43 | 802 | BT_LOGE_STR("Cannot set component class's description."); |
9736d991 | 803 | status = BT_PLUGIN_STATUS_NOMEM; |
65300d60 | 804 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
55bb57e0 PP |
805 | goto end; |
806 | } | |
807 | } | |
808 | ||
279b3f15 | 809 | if (cc_full_descr->help) { |
0d72b8c3 | 810 | ret = bt_component_class_set_help(comp_class, |
279b3f15 PP |
811 | cc_full_descr->help); |
812 | if (ret) { | |
3fe0bf43 | 813 | BT_LOGE_STR("Cannot set component class's help string."); |
9736d991 | 814 | status = BT_PLUGIN_STATUS_NOMEM; |
65300d60 | 815 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
279b3f15 PP |
816 | goto end; |
817 | } | |
818 | } | |
819 | ||
d94d92ac PP |
820 | switch (cc_full_descr->descriptor->type) { |
821 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
822 | if (cc_full_descr->methods.source.init) { | |
0d72b8c3 | 823 | ret = bt_component_class_source_set_init_method( |
d94d92ac PP |
824 | src_comp_class, |
825 | cc_full_descr->methods.source.init); | |
826 | if (ret) { | |
827 | BT_LOGE_STR("Cannot set source component class's initialization method."); | |
9736d991 | 828 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
829 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
830 | goto end; | |
831 | } | |
55bb57e0 | 832 | } |
55bb57e0 | 833 | |
d94d92ac | 834 | if (cc_full_descr->methods.source.finalize) { |
0d72b8c3 | 835 | ret = bt_component_class_source_set_finalize_method( |
d94d92ac PP |
836 | src_comp_class, |
837 | cc_full_descr->methods.source.finalize); | |
838 | if (ret) { | |
839 | BT_LOGE_STR("Cannot set source component class's finalization method."); | |
9736d991 | 840 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
841 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
842 | goto end; | |
843 | } | |
55bb57e0 | 844 | } |
55bb57e0 | 845 | |
d94d92ac | 846 | if (cc_full_descr->methods.source.query) { |
0d72b8c3 | 847 | ret = bt_component_class_source_set_query_method( |
d94d92ac PP |
848 | src_comp_class, |
849 | cc_full_descr->methods.source.query); | |
850 | if (ret) { | |
851 | BT_LOGE_STR("Cannot set source component class's query method."); | |
9736d991 | 852 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
853 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
854 | goto end; | |
855 | } | |
8463eac2 | 856 | } |
8463eac2 | 857 | |
d94d92ac | 858 | if (cc_full_descr->methods.source.output_port_connected) { |
0d72b8c3 | 859 | ret = bt_component_class_source_set_output_port_connected_method( |
d94d92ac PP |
860 | src_comp_class, |
861 | cc_full_descr->methods.source.output_port_connected); | |
862 | if (ret) { | |
863 | BT_LOGE_STR("Cannot set source component class's \"output port connected\" method."); | |
9736d991 | 864 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
865 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
866 | goto end; | |
867 | } | |
0d8b4d8e | 868 | } |
0d8b4d8e | 869 | |
d6e69534 PP |
870 | if (cc_full_descr->methods.source.msg_iter_init) { |
871 | ret = bt_component_class_source_set_message_iterator_init_method( | |
d94d92ac | 872 | src_comp_class, |
d6e69534 | 873 | cc_full_descr->methods.source.msg_iter_init); |
55bb57e0 | 874 | if (ret) { |
d6e69534 | 875 | BT_LOGE_STR("Cannot set source component class's message iterator initialization method."); |
9736d991 | 876 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac | 877 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
55bb57e0 PP |
878 | goto end; |
879 | } | |
880 | } | |
881 | ||
d6e69534 PP |
882 | if (cc_full_descr->methods.source.msg_iter_finalize) { |
883 | ret = bt_component_class_source_set_message_iterator_finalize_method( | |
d94d92ac | 884 | src_comp_class, |
d6e69534 | 885 | cc_full_descr->methods.source.msg_iter_finalize); |
55bb57e0 | 886 | if (ret) { |
d6e69534 | 887 | BT_LOGE_STR("Cannot set source component class's message iterator finalization method."); |
9736d991 | 888 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac | 889 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
55bb57e0 PP |
890 | goto end; |
891 | } | |
892 | } | |
d94d92ac | 893 | |
7474e7d3 PP |
894 | if (cc_full_descr->methods.source.msg_iter_seek_ns_from_origin) { |
895 | ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_method( | |
896 | src_comp_class, | |
897 | cc_full_descr->methods.source.msg_iter_seek_ns_from_origin); | |
898 | if (ret) { | |
899 | BT_LOGE_STR("Cannot set source component class's message iterator \"seek nanoseconds from origin\" method."); | |
9736d991 | 900 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
901 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
902 | goto end; | |
903 | } | |
904 | } | |
905 | ||
906 | if (cc_full_descr->methods.source.msg_iter_seek_beginning) { | |
907 | ret = bt_component_class_source_set_message_iterator_seek_beginning_method( | |
908 | src_comp_class, | |
909 | cc_full_descr->methods.source.msg_iter_seek_beginning); | |
910 | if (ret) { | |
911 | BT_LOGE_STR("Cannot set source component class's message iterator \"seek beginning\" method."); | |
9736d991 | 912 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
913 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
914 | goto end; | |
915 | } | |
916 | } | |
917 | ||
918 | if (cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin) { | |
919 | ret = bt_component_class_source_set_message_iterator_can_seek_ns_from_origin_method( | |
920 | src_comp_class, | |
921 | cc_full_descr->methods.source.msg_iter_can_seek_ns_from_origin); | |
922 | if (ret) { | |
923 | BT_LOGE_STR("Cannot set source component class's message iterator \"can seek nanoseconds from origin\" method."); | |
9736d991 | 924 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
925 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
926 | goto end; | |
927 | } | |
928 | } | |
929 | ||
930 | if (cc_full_descr->methods.source.msg_iter_can_seek_beginning) { | |
931 | ret = bt_component_class_source_set_message_iterator_can_seek_beginning_method( | |
932 | src_comp_class, | |
933 | cc_full_descr->methods.source.msg_iter_can_seek_beginning); | |
934 | if (ret) { | |
935 | BT_LOGE_STR("Cannot set source component class's message iterator \"can seek beginning\" method."); | |
9736d991 | 936 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
937 | BT_OBJECT_PUT_REF_AND_RESET(src_comp_class); |
938 | goto end; | |
939 | } | |
940 | } | |
941 | ||
55bb57e0 PP |
942 | break; |
943 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
d94d92ac | 944 | if (cc_full_descr->methods.filter.init) { |
0d72b8c3 | 945 | ret = bt_component_class_filter_set_init_method( |
d94d92ac PP |
946 | flt_comp_class, |
947 | cc_full_descr->methods.filter.init); | |
948 | if (ret) { | |
949 | BT_LOGE_STR("Cannot set filter component class's initialization method."); | |
9736d991 | 950 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
951 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
952 | goto end; | |
953 | } | |
954 | } | |
955 | ||
956 | if (cc_full_descr->methods.filter.finalize) { | |
0d72b8c3 | 957 | ret = bt_component_class_filter_set_finalize_method( |
d94d92ac PP |
958 | flt_comp_class, |
959 | cc_full_descr->methods.filter.finalize); | |
960 | if (ret) { | |
961 | BT_LOGE_STR("Cannot set filter component class's finalization method."); | |
9736d991 | 962 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
963 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
964 | goto end; | |
965 | } | |
966 | } | |
967 | ||
968 | if (cc_full_descr->methods.filter.query) { | |
0d72b8c3 | 969 | ret = bt_component_class_filter_set_query_method( |
d94d92ac PP |
970 | flt_comp_class, |
971 | cc_full_descr->methods.filter.query); | |
972 | if (ret) { | |
973 | BT_LOGE_STR("Cannot set filter component class's query method."); | |
9736d991 | 974 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
975 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
976 | goto end; | |
977 | } | |
978 | } | |
979 | ||
d94d92ac | 980 | if (cc_full_descr->methods.filter.input_port_connected) { |
0d72b8c3 | 981 | ret = bt_component_class_filter_set_input_port_connected_method( |
d94d92ac PP |
982 | flt_comp_class, |
983 | cc_full_descr->methods.filter.input_port_connected); | |
984 | if (ret) { | |
985 | BT_LOGE_STR("Cannot set filter component class's \"input port connected\" method."); | |
9736d991 | 986 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
987 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
988 | goto end; | |
989 | } | |
990 | } | |
991 | ||
992 | if (cc_full_descr->methods.filter.output_port_connected) { | |
0d72b8c3 | 993 | ret = bt_component_class_filter_set_output_port_connected_method( |
d94d92ac PP |
994 | flt_comp_class, |
995 | cc_full_descr->methods.filter.output_port_connected); | |
996 | if (ret) { | |
997 | BT_LOGE_STR("Cannot set filter component class's \"output port connected\" method."); | |
9736d991 | 998 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
999 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
1000 | goto end; | |
1001 | } | |
1002 | } | |
1003 | ||
d6e69534 PP |
1004 | if (cc_full_descr->methods.filter.msg_iter_init) { |
1005 | ret = bt_component_class_filter_set_message_iterator_init_method( | |
d94d92ac | 1006 | flt_comp_class, |
d6e69534 | 1007 | cc_full_descr->methods.filter.msg_iter_init); |
55bb57e0 | 1008 | if (ret) { |
d6e69534 | 1009 | BT_LOGE_STR("Cannot set filter component class's message iterator initialization method."); |
9736d991 | 1010 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac | 1011 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
55bb57e0 PP |
1012 | goto end; |
1013 | } | |
1014 | } | |
1015 | ||
d6e69534 PP |
1016 | if (cc_full_descr->methods.filter.msg_iter_finalize) { |
1017 | ret = bt_component_class_filter_set_message_iterator_finalize_method( | |
d94d92ac | 1018 | flt_comp_class, |
d6e69534 | 1019 | cc_full_descr->methods.filter.msg_iter_finalize); |
55bb57e0 | 1020 | if (ret) { |
d6e69534 | 1021 | BT_LOGE_STR("Cannot set filter component class's message iterator finalization method."); |
9736d991 | 1022 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac | 1023 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
55bb57e0 PP |
1024 | goto end; |
1025 | } | |
1026 | } | |
d94d92ac | 1027 | |
7474e7d3 PP |
1028 | if (cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin) { |
1029 | ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_method( | |
1030 | flt_comp_class, | |
1031 | cc_full_descr->methods.filter.msg_iter_seek_ns_from_origin); | |
1032 | if (ret) { | |
1033 | BT_LOGE_STR("Cannot set filter component class's message iterator \"seek nanoseconds from origin\" method."); | |
9736d991 | 1034 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
1035 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
1036 | goto end; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | if (cc_full_descr->methods.filter.msg_iter_seek_beginning) { | |
1041 | ret = bt_component_class_filter_set_message_iterator_seek_beginning_method( | |
1042 | flt_comp_class, | |
1043 | cc_full_descr->methods.filter.msg_iter_seek_beginning); | |
1044 | if (ret) { | |
1045 | BT_LOGE_STR("Cannot set filter component class's message iterator \"seek beginning\" method."); | |
9736d991 | 1046 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
1047 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
1048 | goto end; | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | if (cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin) { | |
1053 | ret = bt_component_class_filter_set_message_iterator_can_seek_ns_from_origin_method( | |
1054 | flt_comp_class, | |
1055 | cc_full_descr->methods.filter.msg_iter_can_seek_ns_from_origin); | |
1056 | if (ret) { | |
1057 | BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek nanoseconds from origin\" method."); | |
9736d991 | 1058 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
1059 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
1060 | goto end; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | if (cc_full_descr->methods.filter.msg_iter_can_seek_beginning) { | |
1065 | ret = bt_component_class_filter_set_message_iterator_can_seek_beginning_method( | |
1066 | flt_comp_class, | |
1067 | cc_full_descr->methods.filter.msg_iter_can_seek_beginning); | |
1068 | if (ret) { | |
1069 | BT_LOGE_STR("Cannot set filter component class's message iterator \"can seek beginning\" method."); | |
9736d991 | 1070 | status = BT_PLUGIN_STATUS_NOMEM; |
7474e7d3 PP |
1071 | BT_OBJECT_PUT_REF_AND_RESET(flt_comp_class); |
1072 | goto end; | |
1073 | } | |
1074 | } | |
1075 | ||
55bb57e0 PP |
1076 | break; |
1077 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
d94d92ac | 1078 | if (cc_full_descr->methods.sink.init) { |
0d72b8c3 | 1079 | ret = bt_component_class_sink_set_init_method( |
d94d92ac PP |
1080 | sink_comp_class, |
1081 | cc_full_descr->methods.sink.init); | |
1082 | if (ret) { | |
1083 | BT_LOGE_STR("Cannot set sink component class's initialization method."); | |
9736d991 | 1084 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
1085 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
1086 | goto end; | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | if (cc_full_descr->methods.sink.finalize) { | |
0d72b8c3 | 1091 | ret = bt_component_class_sink_set_finalize_method( |
d94d92ac PP |
1092 | sink_comp_class, |
1093 | cc_full_descr->methods.sink.finalize); | |
1094 | if (ret) { | |
1095 | BT_LOGE_STR("Cannot set sink component class's finalization method."); | |
9736d991 | 1096 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
1097 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
1098 | goto end; | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | if (cc_full_descr->methods.sink.query) { | |
0d72b8c3 | 1103 | ret = bt_component_class_sink_set_query_method( |
d94d92ac PP |
1104 | sink_comp_class, |
1105 | cc_full_descr->methods.sink.query); | |
1106 | if (ret) { | |
1107 | BT_LOGE_STR("Cannot set sink component class's query method."); | |
9736d991 | 1108 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
1109 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
1110 | goto end; | |
1111 | } | |
1112 | } | |
1113 | ||
d94d92ac | 1114 | if (cc_full_descr->methods.sink.input_port_connected) { |
0d72b8c3 | 1115 | ret = bt_component_class_sink_set_input_port_connected_method( |
d94d92ac PP |
1116 | sink_comp_class, |
1117 | cc_full_descr->methods.sink.input_port_connected); | |
1118 | if (ret) { | |
1119 | BT_LOGE_STR("Cannot set sink component class's \"input port connected\" method."); | |
9736d991 | 1120 | status = BT_PLUGIN_STATUS_NOMEM; |
d94d92ac PP |
1121 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
1122 | goto end; | |
1123 | } | |
1124 | } | |
1125 | ||
5badd463 PP |
1126 | if (cc_full_descr->methods.sink.graph_is_configured) { |
1127 | ret = bt_component_class_sink_set_graph_is_configured_method( | |
1128 | sink_comp_class, | |
1129 | cc_full_descr->methods.sink.graph_is_configured); | |
1130 | if (ret) { | |
1131 | BT_LOGE_STR("Cannot set sink component class's \"graph is configured\" method."); | |
9736d991 | 1132 | status = BT_PLUGIN_STATUS_NOMEM; |
5badd463 PP |
1133 | BT_OBJECT_PUT_REF_AND_RESET(sink_comp_class); |
1134 | goto end; | |
1135 | } | |
1136 | } | |
1137 | ||
55bb57e0 PP |
1138 | break; |
1139 | default: | |
0fbb9a9f | 1140 | abort(); |
55bb57e0 PP |
1141 | } |
1142 | ||
1143 | /* | |
1144 | * Add component class to the plugin object. | |
1145 | * | |
1146 | * This will call back | |
1147 | * bt_plugin_so_on_add_component_class() so that we can | |
d94d92ac PP |
1148 | * add a mapping in the component class list when we |
1149 | * know the component class is successfully added. | |
55bb57e0 PP |
1150 | */ |
1151 | status = bt_plugin_add_component_class(plugin, | |
d94d92ac | 1152 | (void *) comp_class); |
65300d60 | 1153 | BT_OBJECT_PUT_REF_AND_RESET(comp_class); |
55bb57e0 | 1154 | if (status < 0) { |
3fe0bf43 | 1155 | BT_LOGE("Cannot add component class to plugin."); |
55bb57e0 PP |
1156 | goto end; |
1157 | } | |
1158 | } | |
1159 | ||
55bb57e0 PP |
1160 | end: |
1161 | g_array_free(comp_class_full_descriptors, TRUE); | |
1162 | return status; | |
1163 | } | |
1164 | ||
1165 | static | |
1166 | struct bt_plugin *bt_plugin_so_create_empty( | |
1167 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle) | |
1168 | { | |
1169 | struct bt_plugin *plugin; | |
1170 | struct bt_plugin_so_spec_data *spec; | |
1171 | ||
1172 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_SO); | |
1173 | if (!plugin) { | |
1174 | goto error; | |
1175 | } | |
1176 | ||
6fbd4105 | 1177 | plugin->destroy_spec_data = bt_plugin_so_destroy_spec_data; |
55bb57e0 PP |
1178 | plugin->spec_data = g_new0(struct bt_plugin_so_spec_data, 1); |
1179 | if (!plugin->spec_data) { | |
3fe0bf43 | 1180 | BT_LOGE_STR("Failed to allocate one SO plugin specific data structure."); |
55bb57e0 PP |
1181 | goto error; |
1182 | } | |
1183 | ||
1184 | spec = plugin->spec_data; | |
398454ed PP |
1185 | spec->shared_lib_handle = shared_lib_handle; |
1186 | bt_object_get_no_null_check(spec->shared_lib_handle); | |
55bb57e0 PP |
1187 | goto end; |
1188 | ||
1189 | error: | |
65300d60 | 1190 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
55bb57e0 PP |
1191 | |
1192 | end: | |
1193 | return plugin; | |
1194 | } | |
1195 | ||
52238017 MJ |
1196 | static |
1197 | size_t count_non_null_items_in_section(const void *begin, const void *end) | |
1198 | { | |
1199 | size_t count = 0; | |
1200 | const int * const *begin_int = (const int * const *) begin; | |
1201 | const int * const *end_int = (const int * const *) end; | |
1202 | const int * const *iter; | |
1203 | ||
1204 | for (iter = begin_int; iter != end_int; iter++) { | |
1205 | if (*iter) { | |
1206 | count++; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | return count; | |
1211 | } | |
1212 | ||
55bb57e0 | 1213 | static |
9736d991 | 1214 | enum bt_plugin_status bt_plugin_so_create_all_from_sections( |
55bb57e0 | 1215 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle, |
9736d991 | 1216 | bool fail_on_load_error, |
55bb57e0 PP |
1217 | struct __bt_plugin_descriptor const * const *descriptors_begin, |
1218 | struct __bt_plugin_descriptor const * const *descriptors_end, | |
1219 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin, | |
1220 | struct __bt_plugin_descriptor_attribute const * const *attrs_end, | |
1221 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin, | |
1222 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end, | |
1223 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin, | |
9736d991 PP |
1224 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end, |
1225 | struct bt_plugin_set **plugin_set_out) | |
55bb57e0 | 1226 | { |
9736d991 | 1227 | enum bt_plugin_status status = BT_PLUGIN_STATUS_OK; |
55bb57e0 PP |
1228 | size_t descriptor_count; |
1229 | size_t attrs_count; | |
1230 | size_t cc_descriptors_count; | |
1231 | size_t cc_descr_attrs_count; | |
1232 | size_t i; | |
55bb57e0 | 1233 | |
9736d991 PP |
1234 | BT_ASSERT(plugin_set_out); |
1235 | *plugin_set_out = NULL; | |
52238017 MJ |
1236 | descriptor_count = count_non_null_items_in_section(descriptors_begin, descriptors_end); |
1237 | attrs_count = count_non_null_items_in_section(attrs_begin, attrs_end); | |
1238 | cc_descriptors_count = count_non_null_items_in_section(cc_descriptors_begin, cc_descriptors_end); | |
1239 | cc_descr_attrs_count = count_non_null_items_in_section(cc_descr_attrs_begin, cc_descr_attrs_end); | |
3f7d4d90 | 1240 | BT_LOGI("Creating all SO plugins from sections: " |
3fe0bf43 PP |
1241 | "plugin-path=\"%s\", " |
1242 | "descr-begin-addr=%p, descr-end-addr=%p, " | |
1243 | "attrs-begin-addr=%p, attrs-end-addr=%p, " | |
1244 | "cc-descr-begin-addr=%p, cc-descr-end-addr=%p, " | |
1245 | "cc-descr-attrs-begin-addr=%p, cc-descr-attrs-end-addr=%p, " | |
1246 | "descr-count=%zu, attrs-count=%zu, " | |
1247 | "cc-descr-count=%zu, cc-descr-attrs-count=%zu", | |
1248 | shared_lib_handle->path ? shared_lib_handle->path->str : NULL, | |
1249 | descriptors_begin, descriptors_end, | |
1250 | attrs_begin, attrs_end, | |
1251 | cc_descriptors_begin, cc_descriptors_end, | |
1252 | cc_descr_attrs_begin, cc_descr_attrs_end, | |
1253 | descriptor_count, attrs_count, | |
1254 | cc_descriptors_count, cc_descr_attrs_count); | |
9736d991 PP |
1255 | *plugin_set_out = bt_plugin_set_create(); |
1256 | if (!*plugin_set_out) { | |
3fe0bf43 | 1257 | BT_LOGE_STR("Cannot create empty plugin set."); |
9736d991 | 1258 | status = BT_PLUGIN_STATUS_NOMEM; |
55bb57e0 PP |
1259 | goto error; |
1260 | } | |
1261 | ||
52238017 | 1262 | for (i = 0; i < descriptors_end - descriptors_begin; i++) { |
55bb57e0 PP |
1263 | enum bt_plugin_status status; |
1264 | const struct __bt_plugin_descriptor *descriptor = | |
1265 | descriptors_begin[i]; | |
1266 | struct bt_plugin *plugin; | |
1267 | ||
52238017 MJ |
1268 | if (descriptor == NULL) { |
1269 | continue; | |
1270 | } | |
1271 | ||
3f7d4d90 | 1272 | BT_LOGI("Creating plugin object for plugin: " |
3fe0bf43 PP |
1273 | "name=\"%s\", abi-major=%d, abi-minor=%d", |
1274 | descriptor->name, descriptor->major, descriptor->minor); | |
55bb57e0 PP |
1275 | |
1276 | if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) { | |
9736d991 PP |
1277 | BT_LOG_WRITE(fail_on_load_error ? BT_LOG_WARN : |
1278 | BT_LOG_INFO, BT_LOG_TAG, | |
1279 | "Unknown ABI major version: abi-major=%d", | |
1280 | descriptor->major); | |
1281 | ||
1282 | if (fail_on_load_error) { | |
1283 | status = BT_PLUGIN_STATUS_LOADING_ERROR; | |
1284 | goto error; | |
1285 | } else { | |
1286 | continue; | |
1287 | } | |
55bb57e0 PP |
1288 | } |
1289 | ||
1290 | plugin = bt_plugin_so_create_empty(shared_lib_handle); | |
1291 | if (!plugin) { | |
3fe0bf43 | 1292 | BT_LOGE_STR("Cannot create empty shared library handle."); |
9736d991 | 1293 | status = BT_PLUGIN_STATUS_NOMEM; |
55bb57e0 PP |
1294 | goto error; |
1295 | } | |
1296 | ||
1297 | if (shared_lib_handle && shared_lib_handle->path) { | |
9736d991 PP |
1298 | bt_plugin_set_path(plugin, |
1299 | shared_lib_handle->path->str); | |
55bb57e0 PP |
1300 | } |
1301 | ||
9736d991 PP |
1302 | status = bt_plugin_so_init(plugin, fail_on_load_error, |
1303 | descriptor, attrs_begin, attrs_end, | |
1304 | cc_descriptors_begin, cc_descriptors_end, | |
55bb57e0 | 1305 | cc_descr_attrs_begin, cc_descr_attrs_end); |
9736d991 PP |
1306 | if (status == BT_PLUGIN_STATUS_OK) { |
1307 | /* Add to plugin set */ | |
1308 | bt_plugin_set_add_plugin(*plugin_set_out, plugin); | |
1309 | BT_OBJECT_PUT_REF_AND_RESET(plugin); | |
1310 | } else if (status < 0) { | |
50ad9320 | 1311 | /* |
9736d991 PP |
1312 | * bt_plugin_so_init() handles |
1313 | * `fail_on_load_error`, so this is a "real" | |
1314 | * error. | |
50ad9320 | 1315 | */ |
9736d991 | 1316 | BT_LOGW_STR("Cannot initialize SO plugin object from sections."); |
65300d60 | 1317 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
55bb57e0 PP |
1318 | goto error; |
1319 | } | |
1320 | ||
9736d991 PP |
1321 | BT_ASSERT(!plugin); |
1322 | } | |
1323 | ||
1324 | BT_ASSERT(*plugin_set_out); | |
1325 | ||
1326 | if ((*plugin_set_out)->plugins->len == 0) { | |
1327 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); | |
1328 | status = BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1329 | } |
1330 | ||
1331 | goto end; | |
1332 | ||
1333 | error: | |
9736d991 PP |
1334 | BT_ASSERT(status < 0); |
1335 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); | |
55bb57e0 PP |
1336 | |
1337 | end: | |
9736d991 | 1338 | return status; |
55bb57e0 PP |
1339 | } |
1340 | ||
1341 | BT_HIDDEN | |
9736d991 PP |
1342 | enum bt_plugin_status bt_plugin_so_create_all_from_static( |
1343 | bool fail_on_load_error, | |
1344 | struct bt_plugin_set **plugin_set_out) | |
55bb57e0 | 1345 | { |
9736d991 PP |
1346 | enum bt_plugin_status status; |
1347 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; | |
55bb57e0 | 1348 | |
9736d991 PP |
1349 | BT_ASSERT(plugin_set_out); |
1350 | *plugin_set_out = NULL; | |
1351 | status = bt_plugin_so_shared_lib_handle_create(NULL, | |
1352 | &shared_lib_handle); | |
1353 | if (status != BT_PLUGIN_STATUS_OK) { | |
1354 | BT_ASSERT(!shared_lib_handle); | |
55bb57e0 PP |
1355 | goto end; |
1356 | } | |
1357 | ||
9736d991 | 1358 | BT_ASSERT(shared_lib_handle); |
3fe0bf43 | 1359 | BT_LOGD_STR("Creating all SO plugins from built-in plugins."); |
9736d991 PP |
1360 | status = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
1361 | fail_on_load_error, | |
52238017 MJ |
1362 | __bt_get_begin_section_plugin_descriptors(), |
1363 | __bt_get_end_section_plugin_descriptors(), | |
1364 | __bt_get_begin_section_plugin_descriptor_attributes(), | |
1365 | __bt_get_end_section_plugin_descriptor_attributes(), | |
1366 | __bt_get_begin_section_component_class_descriptors(), | |
1367 | __bt_get_end_section_component_class_descriptors(), | |
1368 | __bt_get_begin_section_component_class_descriptor_attributes(), | |
9736d991 PP |
1369 | __bt_get_end_section_component_class_descriptor_attributes(), |
1370 | plugin_set_out); | |
1371 | BT_ASSERT((status == BT_PLUGIN_STATUS_OK && *plugin_set_out && | |
1372 | (*plugin_set_out)->plugins->len > 0) || !*plugin_set_out); | |
55bb57e0 PP |
1373 | |
1374 | end: | |
65300d60 | 1375 | BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle); |
9736d991 | 1376 | return status; |
55bb57e0 PP |
1377 | } |
1378 | ||
1379 | BT_HIDDEN | |
9736d991 PP |
1380 | enum bt_plugin_status bt_plugin_so_create_all_from_file(const char *path, |
1381 | bool fail_on_load_error, struct bt_plugin_set **plugin_set_out) | |
55bb57e0 PP |
1382 | { |
1383 | size_t path_len; | |
9736d991 | 1384 | enum bt_plugin_status status; |
55bb57e0 PP |
1385 | struct __bt_plugin_descriptor const * const *descriptors_begin = NULL; |
1386 | struct __bt_plugin_descriptor const * const *descriptors_end = NULL; | |
1387 | struct __bt_plugin_descriptor_attribute const * const *attrs_begin = NULL; | |
1388 | struct __bt_plugin_descriptor_attribute const * const *attrs_end = NULL; | |
1389 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_begin = NULL; | |
1390 | struct __bt_plugin_component_class_descriptor const * const *cc_descriptors_end = NULL; | |
1391 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_begin = NULL; | |
1392 | struct __bt_plugin_component_class_descriptor_attribute const * const *cc_descr_attrs_end = NULL; | |
52238017 MJ |
1393 | struct __bt_plugin_descriptor const * const *(*get_begin_section_plugin_descriptors)(void); |
1394 | struct __bt_plugin_descriptor const * const *(*get_end_section_plugin_descriptors)(void); | |
1395 | struct __bt_plugin_descriptor_attribute const * const *(*get_begin_section_plugin_descriptor_attributes)(void); | |
1396 | struct __bt_plugin_descriptor_attribute const * const *(*get_end_section_plugin_descriptor_attributes)(void); | |
1397 | struct __bt_plugin_component_class_descriptor const * const *(*get_begin_section_component_class_descriptors)(void); | |
1398 | struct __bt_plugin_component_class_descriptor const * const *(*get_end_section_component_class_descriptors)(void); | |
1399 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_begin_section_component_class_descriptor_attributes)(void); | |
1400 | struct __bt_plugin_component_class_descriptor_attribute const * const *(*get_end_section_component_class_descriptor_attributes)(void); | |
c55a9f58 | 1401 | bt_bool is_libtool_wrapper = BT_FALSE, is_shared_object = BT_FALSE; |
55bb57e0 PP |
1402 | struct bt_plugin_so_shared_lib_handle *shared_lib_handle = NULL; |
1403 | ||
3f7d4d90 | 1404 | BT_ASSERT(path); |
9736d991 PP |
1405 | BT_ASSERT(plugin_set_out); |
1406 | *plugin_set_out = NULL; | |
55bb57e0 | 1407 | path_len = strlen(path); |
3f7d4d90 PP |
1408 | BT_ASSERT_PRE(path_len > PLUGIN_SUFFIX_LEN, |
1409 | "Path length is too short: path-length=%zu, min-length=%zu", | |
1410 | path_len, PLUGIN_SUFFIX_LEN); | |
1411 | BT_LOGI("Trying to create all SO plugins from file: path=\"%s\"", path); | |
55bb57e0 | 1412 | path_len++; |
3f7d4d90 | 1413 | |
55bb57e0 | 1414 | /* |
3f7d4d90 PP |
1415 | * Check if the file ends with a known plugin file type suffix |
1416 | * (i.e. .so or .la on Linux). | |
55bb57e0 PP |
1417 | */ |
1418 | is_libtool_wrapper = !strncmp(LIBTOOL_PLUGIN_SUFFIX, | |
1419 | path + path_len - LIBTOOL_PLUGIN_SUFFIX_LEN, | |
1420 | LIBTOOL_PLUGIN_SUFFIX_LEN); | |
1421 | is_shared_object = !strncmp(NATIVE_PLUGIN_SUFFIX, | |
1422 | path + path_len - NATIVE_PLUGIN_SUFFIX_LEN, | |
1423 | NATIVE_PLUGIN_SUFFIX_LEN); | |
1424 | if (!is_shared_object && !is_libtool_wrapper) { | |
3fe0bf43 | 1425 | /* Name indicates this is not a plugin file; not an error */ |
9736d991 PP |
1426 | BT_LOGI("File is not an SO plugin file: path=\"%s\"", path); |
1427 | status = BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1428 | goto end; |
1429 | } | |
1430 | ||
9736d991 PP |
1431 | status = bt_plugin_so_shared_lib_handle_create(path, |
1432 | &shared_lib_handle); | |
1433 | if (status != BT_PLUGIN_STATUS_OK) { | |
3f7d4d90 | 1434 | /* bt_plugin_so_shared_lib_handle_create() logs more details */ |
9736d991 PP |
1435 | BT_ASSERT(!shared_lib_handle); |
1436 | BT_LOGE_STR("Cannot create shared library handle."); | |
55bb57e0 PP |
1437 | goto end; |
1438 | } | |
1439 | ||
52238017 MJ |
1440 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptors", |
1441 | (gpointer *) &get_begin_section_plugin_descriptors)) { | |
1442 | descriptors_begin = get_begin_section_plugin_descriptors(); | |
1443 | } else { | |
9736d991 PP |
1444 | /* |
1445 | * Use this first symbol to know whether or not this | |
1446 | * shared object _looks like_ a Babeltrace plugin. Since | |
1447 | * g_module_symbol() failed, assume that this is not a | |
1448 | * Babeltrace plugin, so it's not an error. | |
1449 | */ | |
3f7d4d90 | 1450 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1451 | "symbol=\"%s\"", path, |
52238017 | 1452 | "__bt_get_begin_section_plugin_descriptors"); |
9736d991 | 1453 | status = BT_PLUGIN_STATUS_NOT_FOUND; |
55bb57e0 PP |
1454 | goto end; |
1455 | } | |
1456 | ||
9736d991 PP |
1457 | /* |
1458 | * If g_module_symbol() fails for any of the other symbols, fail | |
1459 | * if `fail_on_load_error` is true. | |
1460 | */ | |
52238017 MJ |
1461 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptors", |
1462 | (gpointer *) &get_end_section_plugin_descriptors)) { | |
1463 | descriptors_end = get_end_section_plugin_descriptors(); | |
1464 | } else { | |
9736d991 PP |
1465 | BT_LOG_WRITE(fail_on_load_error ? BT_LOG_WARN : BT_LOG_INFO, |
1466 | BT_LOG_TAG, | |
1467 | "Cannot resolve plugin symbol: path=\"%s\", " | |
3fe0bf43 | 1468 | "symbol=\"%s\"", path, |
52238017 | 1469 | "__bt_get_end_section_plugin_descriptors"); |
9736d991 PP |
1470 | status = fail_on_load_error ? BT_PLUGIN_STATUS_LOADING_ERROR : |
1471 | BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1472 | goto end; |
1473 | } | |
1474 | ||
52238017 MJ |
1475 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_plugin_descriptor_attributes", |
1476 | (gpointer *) &get_begin_section_plugin_descriptor_attributes)) { | |
1477 | attrs_begin = get_begin_section_plugin_descriptor_attributes(); | |
1478 | } else { | |
3f7d4d90 | 1479 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1480 | "symbol=\"%s\"", path, |
52238017 | 1481 | "__bt_get_begin_section_plugin_descriptor_attributes"); |
55bb57e0 PP |
1482 | } |
1483 | ||
52238017 MJ |
1484 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_plugin_descriptor_attributes", |
1485 | (gpointer *) &get_end_section_plugin_descriptor_attributes)) { | |
1486 | attrs_end = get_end_section_plugin_descriptor_attributes(); | |
1487 | } else { | |
3f7d4d90 | 1488 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1489 | "symbol=\"%s\"", path, |
52238017 | 1490 | "__bt_get_end_section_plugin_descriptor_attributes"); |
55bb57e0 PP |
1491 | } |
1492 | ||
1493 | if ((!!attrs_begin - !!attrs_end) != 0) { | |
9736d991 PP |
1494 | BT_LOG_WRITE(fail_on_load_error ? BT_LOG_WARN : BT_LOG_INFO, |
1495 | BT_LOG_TAG, | |
1496 | "Found section start or end symbol, but not both: " | |
3fe0bf43 PP |
1497 | "path=\"%s\", symbol-start=\"%s\", " |
1498 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1499 | "symbol-end-addr=%p", | |
52238017 MJ |
1500 | path, "__bt_get_begin_section_plugin_descriptor_attributes", |
1501 | "__bt_get_end_section_plugin_descriptor_attributes", | |
3fe0bf43 | 1502 | attrs_begin, attrs_end); |
9736d991 PP |
1503 | status = fail_on_load_error ? BT_PLUGIN_STATUS_LOADING_ERROR : |
1504 | BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1505 | goto end; |
1506 | } | |
1507 | ||
52238017 MJ |
1508 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptors", |
1509 | (gpointer *) &get_begin_section_component_class_descriptors)) { | |
1510 | cc_descriptors_begin = get_begin_section_component_class_descriptors(); | |
1511 | } else { | |
3f7d4d90 | 1512 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1513 | "symbol=\"%s\"", path, |
52238017 | 1514 | "__bt_get_begin_section_component_class_descriptors"); |
55bb57e0 PP |
1515 | } |
1516 | ||
52238017 MJ |
1517 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptors", |
1518 | (gpointer *) &get_end_section_component_class_descriptors)) { | |
1519 | cc_descriptors_end = get_end_section_component_class_descriptors(); | |
1520 | } else { | |
3f7d4d90 | 1521 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1522 | "symbol=\"%s\"", path, |
52238017 | 1523 | "__bt_get_end_section_component_class_descriptors"); |
55bb57e0 PP |
1524 | } |
1525 | ||
1526 | if ((!!cc_descriptors_begin - !!cc_descriptors_end) != 0) { | |
9736d991 PP |
1527 | BT_LOG_WRITE(fail_on_load_error ? BT_LOG_WARN : BT_LOG_INFO, |
1528 | BT_LOG_TAG, | |
1529 | "Found section start or end symbol, but not both: " | |
3fe0bf43 PP |
1530 | "path=\"%s\", symbol-start=\"%s\", " |
1531 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1532 | "symbol-end-addr=%p", | |
52238017 MJ |
1533 | path, "__bt_get_begin_section_component_class_descriptors", |
1534 | "__bt_get_end_section_component_class_descriptors", | |
3fe0bf43 | 1535 | cc_descriptors_begin, cc_descriptors_end); |
9736d991 PP |
1536 | status = fail_on_load_error ? BT_PLUGIN_STATUS_LOADING_ERROR : |
1537 | BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1538 | goto end; |
1539 | } | |
1540 | ||
52238017 MJ |
1541 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_begin_section_component_class_descriptor_attributes", |
1542 | (gpointer *) &get_begin_section_component_class_descriptor_attributes)) { | |
1543 | cc_descr_attrs_begin = get_begin_section_component_class_descriptor_attributes(); | |
1544 | } else { | |
3f7d4d90 | 1545 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1546 | "symbol=\"%s\"", path, |
52238017 | 1547 | "__bt_get_begin_section_component_class_descriptor_attributes"); |
55bb57e0 PP |
1548 | } |
1549 | ||
52238017 MJ |
1550 | if (g_module_symbol(shared_lib_handle->module, "__bt_get_end_section_component_class_descriptor_attributes", |
1551 | (gpointer *) &get_end_section_component_class_descriptor_attributes)) { | |
1552 | cc_descr_attrs_end = get_end_section_component_class_descriptor_attributes(); | |
1553 | } else { | |
3f7d4d90 | 1554 | BT_LOGI("Cannot resolve plugin symbol: path=\"%s\", " |
3fe0bf43 | 1555 | "symbol=\"%s\"", path, |
52238017 | 1556 | "__bt_get_end_section_component_class_descriptor_attributes"); |
55bb57e0 PP |
1557 | } |
1558 | ||
1559 | if ((!!cc_descr_attrs_begin - !!cc_descr_attrs_end) != 0) { | |
9736d991 PP |
1560 | BT_LOG_WRITE(fail_on_load_error ? BT_LOG_WARN : BT_LOG_INFO, |
1561 | BT_LOG_TAG, | |
1562 | "Found section start or end symbol, but not both: " | |
3fe0bf43 PP |
1563 | "path=\"%s\", symbol-start=\"%s\", " |
1564 | "symbol-end=\"%s\", symbol-start-addr=%p, " | |
1565 | "symbol-end-addr=%p", | |
52238017 MJ |
1566 | path, "__bt_get_begin_section_component_class_descriptor_attributes", |
1567 | "__bt_get_end_section_component_class_descriptor_attributes", | |
3fe0bf43 | 1568 | cc_descr_attrs_begin, cc_descr_attrs_end); |
9736d991 PP |
1569 | status = fail_on_load_error ? BT_PLUGIN_STATUS_LOADING_ERROR : |
1570 | BT_PLUGIN_STATUS_NOT_FOUND; | |
55bb57e0 PP |
1571 | goto end; |
1572 | } | |
1573 | ||
1574 | /* Initialize plugin */ | |
3fe0bf43 | 1575 | BT_LOGD_STR("Initializing plugin object."); |
9736d991 PP |
1576 | status = bt_plugin_so_create_all_from_sections(shared_lib_handle, |
1577 | fail_on_load_error, | |
55bb57e0 PP |
1578 | descriptors_begin, descriptors_end, attrs_begin, attrs_end, |
1579 | cc_descriptors_begin, cc_descriptors_end, | |
9736d991 | 1580 | cc_descr_attrs_begin, cc_descr_attrs_end, plugin_set_out); |
55bb57e0 PP |
1581 | |
1582 | end: | |
65300d60 | 1583 | BT_OBJECT_PUT_REF_AND_RESET(shared_lib_handle); |
9736d991 | 1584 | return status; |
55bb57e0 PP |
1585 | } |
1586 | ||
1587 | static | |
1588 | void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class, | |
1589 | void *data) | |
1590 | { | |
bfa9a4be | 1591 | bt_list_del(&comp_class->node); |
65300d60 | 1592 | BT_OBJECT_PUT_REF_AND_RESET(comp_class->so_handle); |
3f7d4d90 | 1593 | BT_LOGD("Component class destroyed: removed entry from list: " |
3fe0bf43 | 1594 | "comp-cls-addr=%p", comp_class); |
55bb57e0 PP |
1595 | } |
1596 | ||
3230ee6b | 1597 | void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, |
55bb57e0 PP |
1598 | struct bt_component_class *comp_class) |
1599 | { | |
55bb57e0 PP |
1600 | struct bt_plugin_so_spec_data *spec = plugin->spec_data; |
1601 | ||
f6ccaed9 PP |
1602 | BT_ASSERT(plugin->spec_data); |
1603 | BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO); | |
55bb57e0 | 1604 | |
bfa9a4be | 1605 | bt_list_add(&comp_class->node, &component_class_list); |
398454ed PP |
1606 | comp_class->so_handle = spec->shared_lib_handle; |
1607 | bt_object_get_no_null_check(comp_class->so_handle); | |
55bb57e0 PP |
1608 | |
1609 | /* Add our custom destroy listener */ | |
3230ee6b | 1610 | bt_component_class_add_destroy_listener(comp_class, |
55bb57e0 | 1611 | plugin_comp_class_destroy_listener, NULL); |
55bb57e0 | 1612 | } |