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