Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H |
2 | #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H | |
fb2dcc52 JG |
3 | |
4 | /* | |
f2b0325d | 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
fb2dcc52 JG |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7 | * | |
fb2dcc52 JG |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
85e7137b | 27 | #include "common/macros.h" |
24847fc7 | 28 | #include "common/common.h" |
57952005 | 29 | #include "lib/graph/component-class.h" |
7704a0af | 30 | #include <babeltrace2/plugin/plugin-loading.h> |
71c5da58 | 31 | #include <babeltrace2/plugin/plugin-dev.h> |
57952005 | 32 | #include "lib/object.h" |
71c5da58 | 33 | #include <babeltrace2/types.h> |
57952005 | 34 | #include "common/assert.h" |
55bb57e0 | 35 | #include <glib.h> |
969c1d8a | 36 | #include <stdbool.h> |
fb2dcc52 | 37 | |
57952005 | 38 | #include "plugin-so.h" |
fb25b9e3 | 39 | #include "lib/func-status.h" |
57952005 | 40 | |
517689f3 | 41 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
ebb4875d | 42 | #ifndef BT_LIB_LOG_SUPPORTED |
1633ef46 | 43 | # error Please include "lib/logging.h" before including this file. |
517689f3 PP |
44 | #endif |
45 | ||
55bb57e0 PP |
46 | enum bt_plugin_type { |
47 | BT_PLUGIN_TYPE_SO = 0, | |
48 | BT_PLUGIN_TYPE_PYTHON = 1, | |
fb2dcc52 JG |
49 | }; |
50 | ||
33b34c43 PP |
51 | struct bt_plugin { |
52 | struct bt_object base; | |
55bb57e0 | 53 | enum bt_plugin_type type; |
cba174d5 | 54 | |
834e9996 PP |
55 | /* Arrays of `struct bt_component_class *` (owned by this) */ |
56 | GPtrArray *src_comp_classes; | |
57 | GPtrArray *flt_comp_classes; | |
58 | GPtrArray *sink_comp_classes; | |
6ba0b073 | 59 | |
55bb57e0 PP |
60 | /* Info (owned by this) */ |
61 | struct { | |
62 | GString *path; | |
63 | GString *name; | |
64 | GString *author; | |
65 | GString *license; | |
66 | GString *description; | |
67 | struct { | |
68 | unsigned int major; | |
69 | unsigned int minor; | |
70 | unsigned int patch; | |
71 | GString *extra; | |
72 | } version; | |
834e9996 PP |
73 | bool path_set; |
74 | bool name_set; | |
75 | bool author_set; | |
76 | bool license_set; | |
77 | bool description_set; | |
78 | bool version_set; | |
55bb57e0 PP |
79 | } info; |
80 | ||
81 | /* Value depends on the specific plugin type */ | |
82 | void *spec_data; | |
6fbd4105 | 83 | void (*destroy_spec_data)(struct bt_plugin *); |
33b34c43 | 84 | }; |
fb2dcc52 | 85 | |
a8ff38ef PP |
86 | struct bt_plugin_set { |
87 | struct bt_object base; | |
88 | ||
89 | /* Array of struct bt_plugin * */ | |
90 | GPtrArray *plugins; | |
91 | }; | |
92 | ||
3fe0bf43 PP |
93 | static inline |
94 | const char *bt_plugin_type_string(enum bt_plugin_type type) | |
95 | { | |
96 | switch (type) { | |
97 | case BT_PLUGIN_TYPE_SO: | |
e05f8464 | 98 | return "SO"; |
3fe0bf43 | 99 | case BT_PLUGIN_TYPE_PYTHON: |
e05f8464 | 100 | return "PYTHON"; |
3fe0bf43 PP |
101 | default: |
102 | return "(unknown)"; | |
103 | } | |
104 | } | |
105 | ||
6fbd4105 PP |
106 | static inline |
107 | void bt_plugin_destroy(struct bt_object *obj) | |
108 | { | |
109 | struct bt_plugin *plugin; | |
110 | ||
8b45963b | 111 | BT_ASSERT(obj); |
6fbd4105 | 112 | plugin = container_of(obj, struct bt_plugin, base); |
a684a357 | 113 | BT_LIB_LOGI("Destroying plugin object: %!+l", plugin); |
6fbd4105 PP |
114 | |
115 | if (plugin->destroy_spec_data) { | |
116 | plugin->destroy_spec_data(plugin); | |
117 | } | |
118 | ||
834e9996 PP |
119 | if (plugin->src_comp_classes) { |
120 | BT_LOGD_STR("Putting source component classes."); | |
121 | g_ptr_array_free(plugin->src_comp_classes, TRUE); | |
c6578b03 | 122 | plugin->src_comp_classes = NULL; |
834e9996 PP |
123 | } |
124 | ||
125 | if (plugin->flt_comp_classes) { | |
126 | BT_LOGD_STR("Putting filter component classes."); | |
127 | g_ptr_array_free(plugin->flt_comp_classes, TRUE); | |
c6578b03 | 128 | plugin->flt_comp_classes = NULL; |
834e9996 PP |
129 | } |
130 | ||
131 | if (plugin->sink_comp_classes) { | |
132 | BT_LOGD_STR("Putting sink component classes."); | |
133 | g_ptr_array_free(plugin->sink_comp_classes, TRUE); | |
c6578b03 | 134 | plugin->sink_comp_classes = NULL; |
6fbd4105 PP |
135 | } |
136 | ||
137 | if (plugin->info.name) { | |
138 | g_string_free(plugin->info.name, TRUE); | |
c6578b03 | 139 | plugin->info.name = NULL; |
6fbd4105 PP |
140 | } |
141 | ||
142 | if (plugin->info.path) { | |
143 | g_string_free(plugin->info.path, TRUE); | |
c6578b03 | 144 | plugin->info.path = NULL; |
6fbd4105 PP |
145 | } |
146 | ||
147 | if (plugin->info.description) { | |
148 | g_string_free(plugin->info.description, TRUE); | |
c6578b03 | 149 | plugin->info.description = NULL; |
6fbd4105 PP |
150 | } |
151 | ||
152 | if (plugin->info.author) { | |
153 | g_string_free(plugin->info.author, TRUE); | |
c6578b03 | 154 | plugin->info.author = NULL; |
6fbd4105 PP |
155 | } |
156 | ||
157 | if (plugin->info.license) { | |
158 | g_string_free(plugin->info.license, TRUE); | |
c6578b03 | 159 | plugin->info.license = NULL; |
6fbd4105 PP |
160 | } |
161 | ||
162 | if (plugin->info.version.extra) { | |
163 | g_string_free(plugin->info.version.extra, TRUE); | |
c6578b03 | 164 | plugin->info.version.extra = NULL; |
6fbd4105 PP |
165 | } |
166 | ||
167 | g_free(plugin); | |
168 | } | |
169 | ||
170 | static inline | |
171 | struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type) | |
172 | { | |
173 | struct bt_plugin *plugin = NULL; | |
174 | ||
3fe0bf43 PP |
175 | BT_LOGD("Creating empty plugin object: type=%s", |
176 | bt_plugin_type_string(type)); | |
177 | ||
6fbd4105 PP |
178 | plugin = g_new0(struct bt_plugin, 1); |
179 | if (!plugin) { | |
a8f90e5d | 180 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one plugin."); |
6fbd4105 PP |
181 | goto error; |
182 | } | |
183 | ||
1d7bf349 | 184 | bt_object_init_shared(&plugin->base, bt_plugin_destroy); |
6fbd4105 PP |
185 | plugin->type = type; |
186 | ||
834e9996 PP |
187 | /* Create empty arrays of component classes */ |
188 | plugin->src_comp_classes = | |
189 | g_ptr_array_new_with_free_func( | |
190 | (GDestroyNotify) bt_object_put_ref); | |
191 | if (!plugin->src_comp_classes) { | |
a8f90e5d | 192 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
834e9996 PP |
193 | goto error; |
194 | } | |
195 | ||
196 | plugin->flt_comp_classes = | |
197 | g_ptr_array_new_with_free_func( | |
198 | (GDestroyNotify) bt_object_put_ref); | |
199 | if (!plugin->flt_comp_classes) { | |
a8f90e5d | 200 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
834e9996 PP |
201 | goto error; |
202 | } | |
203 | ||
204 | plugin->sink_comp_classes = | |
205 | g_ptr_array_new_with_free_func( | |
206 | (GDestroyNotify) bt_object_put_ref); | |
207 | if (!plugin->sink_comp_classes) { | |
a8f90e5d | 208 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
6fbd4105 PP |
209 | goto error; |
210 | } | |
211 | ||
212 | /* Create empty info */ | |
213 | plugin->info.name = g_string_new(NULL); | |
214 | if (!plugin->info.name) { | |
a8f90e5d | 215 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
216 | goto error; |
217 | } | |
218 | ||
219 | plugin->info.path = g_string_new(NULL); | |
220 | if (!plugin->info.path) { | |
a8f90e5d | 221 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
222 | goto error; |
223 | } | |
224 | ||
225 | plugin->info.description = g_string_new(NULL); | |
226 | if (!plugin->info.description) { | |
a8f90e5d | 227 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
228 | goto error; |
229 | } | |
230 | ||
231 | plugin->info.author = g_string_new(NULL); | |
232 | if (!plugin->info.author) { | |
a8f90e5d | 233 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
234 | goto error; |
235 | } | |
236 | ||
237 | plugin->info.license = g_string_new(NULL); | |
238 | if (!plugin->info.license) { | |
a8f90e5d | 239 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
240 | goto error; |
241 | } | |
242 | ||
243 | plugin->info.version.extra = g_string_new(NULL); | |
244 | if (!plugin->info.version.extra) { | |
a8f90e5d | 245 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
6fbd4105 PP |
246 | goto error; |
247 | } | |
248 | ||
834e9996 | 249 | BT_LIB_LOGD("Created empty plugin object: %!+l", plugin); |
6fbd4105 PP |
250 | goto end; |
251 | ||
252 | error: | |
8138bfe1 | 253 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
6fbd4105 PP |
254 | |
255 | end: | |
256 | return plugin; | |
257 | } | |
55bb57e0 PP |
258 | |
259 | static inline | |
260 | void bt_plugin_set_path(struct bt_plugin *plugin, const char *path) | |
261 | { | |
8b45963b PP |
262 | BT_ASSERT(plugin); |
263 | BT_ASSERT(path); | |
55bb57e0 | 264 | g_string_assign(plugin->info.path, path); |
c55a9f58 | 265 | plugin->info.path_set = BT_TRUE; |
a684a357 | 266 | BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, path=\"%s\"", |
834e9996 | 267 | plugin, path); |
55bb57e0 PP |
268 | } |
269 | ||
270 | static inline | |
271 | void bt_plugin_set_name(struct bt_plugin *plugin, const char *name) | |
272 | { | |
8b45963b PP |
273 | BT_ASSERT(plugin); |
274 | BT_ASSERT(name); | |
55bb57e0 | 275 | g_string_assign(plugin->info.name, name); |
c55a9f58 | 276 | plugin->info.name_set = BT_TRUE; |
a684a357 | 277 | BT_LIB_LOGD("Set plugin's name: %![plugin-]+l, name=\"%s\"", |
3fe0bf43 | 278 | plugin, name); |
55bb57e0 PP |
279 | } |
280 | ||
281 | static inline | |
282 | void bt_plugin_set_description(struct bt_plugin *plugin, | |
283 | const char *description) | |
284 | { | |
8b45963b PP |
285 | BT_ASSERT(plugin); |
286 | BT_ASSERT(description); | |
55bb57e0 | 287 | g_string_assign(plugin->info.description, description); |
c55a9f58 | 288 | plugin->info.description_set = BT_TRUE; |
a684a357 | 289 | BT_LIB_LOGD("Set plugin's description: %![plugin-]+l", plugin); |
55bb57e0 PP |
290 | } |
291 | ||
292 | static inline | |
293 | void bt_plugin_set_author(struct bt_plugin *plugin, const char *author) | |
294 | { | |
8b45963b PP |
295 | BT_ASSERT(plugin); |
296 | BT_ASSERT(author); | |
55bb57e0 | 297 | g_string_assign(plugin->info.author, author); |
c55a9f58 | 298 | plugin->info.author_set = BT_TRUE; |
a684a357 | 299 | BT_LIB_LOGD("Set plugin's author: %![plugin-]+l, author=\"%s\"", |
834e9996 | 300 | plugin, author); |
55bb57e0 PP |
301 | } |
302 | ||
303 | static inline | |
304 | void bt_plugin_set_license(struct bt_plugin *plugin, const char *license) | |
305 | { | |
8b45963b PP |
306 | BT_ASSERT(plugin); |
307 | BT_ASSERT(license); | |
55bb57e0 | 308 | g_string_assign(plugin->info.license, license); |
c55a9f58 | 309 | plugin->info.license_set = BT_TRUE; |
a684a357 | 310 | BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, license=\"%s\"", |
834e9996 | 311 | plugin, license); |
55bb57e0 PP |
312 | } |
313 | ||
314 | static inline | |
315 | void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major, | |
316 | unsigned int minor, unsigned int patch, const char *extra) | |
317 | { | |
8b45963b | 318 | BT_ASSERT(plugin); |
55bb57e0 PP |
319 | plugin->info.version.major = major; |
320 | plugin->info.version.minor = minor; | |
321 | plugin->info.version.patch = patch; | |
322 | ||
323 | if (extra) { | |
324 | g_string_assign(plugin->info.version.extra, extra); | |
325 | } | |
326 | ||
c55a9f58 | 327 | plugin->info.version_set = BT_TRUE; |
a684a357 | 328 | BT_LIB_LOGD("Set plugin's version: %![plugin-]+l, " |
3fe0bf43 | 329 | "major=%u, minor=%u, patch=%u, extra=\"%s\"", |
834e9996 | 330 | plugin, major, minor, patch, extra); |
55bb57e0 PP |
331 | } |
332 | ||
333 | static inline | |
fb25b9e3 | 334 | int bt_plugin_add_component_class( |
834e9996 | 335 | struct bt_plugin *plugin, struct bt_component_class *comp_class) |
55bb57e0 | 336 | { |
834e9996 PP |
337 | GPtrArray *comp_classes; |
338 | ||
8b45963b | 339 | BT_ASSERT(plugin); |
834e9996 PP |
340 | BT_ASSERT(comp_class); |
341 | ||
342 | switch (comp_class->type) { | |
343 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
344 | comp_classes = plugin->src_comp_classes; | |
345 | break; | |
346 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
347 | comp_classes = plugin->flt_comp_classes; | |
348 | break; | |
349 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
350 | comp_classes = plugin->sink_comp_classes; | |
351 | break; | |
352 | default: | |
24847fc7 | 353 | bt_common_abort(); |
834e9996 | 354 | } |
3fe0bf43 | 355 | |
0c99efa4 PP |
356 | /* Set component class's original plugin name */ |
357 | BT_ASSERT(comp_class->plugin_name); | |
358 | BT_ASSERT(plugin->info.name); | |
359 | g_string_assign(comp_class->plugin_name, plugin->info.name->str); | |
360 | ||
834e9996 | 361 | /* Add new component class */ |
4b70020d PP |
362 | bt_object_get_ref(comp_class); |
363 | g_ptr_array_add(comp_classes, comp_class); | |
834e9996 PP |
364 | |
365 | /* Special case for a shared object plugin */ | |
366 | if (plugin->type == BT_PLUGIN_TYPE_SO) { | |
367 | bt_plugin_so_on_add_component_class(plugin, comp_class); | |
3fe0bf43 PP |
368 | } |
369 | ||
834e9996 PP |
370 | BT_LIB_LOGD("Added component class to plugin: " |
371 | "%![plugin-]+l, %![cc-]+C", plugin, comp_class); | |
fb25b9e3 | 372 | return BT_FUNC_STATUS_OK; |
55bb57e0 PP |
373 | } |
374 | ||
a8ff38ef PP |
375 | static |
376 | void bt_plugin_set_destroy(struct bt_object *obj) | |
377 | { | |
378 | struct bt_plugin_set *plugin_set = | |
379 | container_of(obj, struct bt_plugin_set, base); | |
380 | ||
381 | if (!plugin_set) { | |
382 | return; | |
383 | } | |
384 | ||
3fe0bf43 PP |
385 | BT_LOGD("Destroying plugin set: addr=%p", plugin_set); |
386 | ||
a8ff38ef | 387 | if (plugin_set->plugins) { |
3fe0bf43 | 388 | BT_LOGD_STR("Putting plugins."); |
a8ff38ef PP |
389 | g_ptr_array_free(plugin_set->plugins, TRUE); |
390 | } | |
391 | ||
392 | g_free(plugin_set); | |
393 | } | |
394 | ||
395 | static inline | |
396 | struct bt_plugin_set *bt_plugin_set_create(void) | |
397 | { | |
398 | struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1); | |
399 | ||
400 | if (!plugin_set) { | |
401 | goto end; | |
402 | } | |
403 | ||
3fe0bf43 | 404 | BT_LOGD_STR("Creating empty plugin set."); |
1d7bf349 | 405 | bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy); |
a8ff38ef PP |
406 | |
407 | plugin_set->plugins = g_ptr_array_new_with_free_func( | |
8138bfe1 | 408 | (GDestroyNotify) bt_object_put_ref); |
a8ff38ef | 409 | if (!plugin_set->plugins) { |
a8f90e5d | 410 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
8138bfe1 | 411 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
a8ff38ef PP |
412 | goto end; |
413 | } | |
414 | ||
3fe0bf43 PP |
415 | BT_LOGD("Created empty plugin set: addr=%p", plugin_set); |
416 | ||
a8ff38ef PP |
417 | end: |
418 | return plugin_set; | |
419 | } | |
420 | ||
7d8f15e6 PP |
421 | static inline |
422 | bool bt_plugin_set_contains_plugin(struct bt_plugin_set *plugin_set, | |
423 | const char *name) | |
424 | { | |
425 | uint64_t i; | |
426 | bool contains = false; | |
427 | ||
428 | BT_ASSERT(plugin_set); | |
429 | BT_ASSERT(name); | |
430 | ||
431 | for (i = 0; i < plugin_set->plugins->len; i++) { | |
432 | const struct bt_plugin *plugin = plugin_set->plugins->pdata[i]; | |
433 | ||
434 | if (strcmp(plugin->info.name->str, name) == 0) { | |
435 | contains = true; | |
436 | goto end; | |
437 | } | |
438 | } | |
439 | ||
440 | end: | |
441 | return contains; | |
442 | } | |
443 | ||
a8ff38ef PP |
444 | static inline |
445 | void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set, | |
446 | struct bt_plugin *plugin) | |
447 | { | |
8b45963b PP |
448 | BT_ASSERT(plugin_set); |
449 | BT_ASSERT(plugin); | |
7d8f15e6 PP |
450 | |
451 | if (bt_plugin_set_contains_plugin(plugin_set, | |
452 | plugin->info.name->str)) { | |
453 | goto end; | |
454 | } | |
455 | ||
4b70020d PP |
456 | bt_object_get_ref(plugin); |
457 | g_ptr_array_add(plugin_set->plugins, plugin); | |
a684a357 | 458 | BT_LIB_LOGD("Added plugin to plugin set: " |
834e9996 PP |
459 | "plugin-set-addr=%p, %![plugin-]+l", |
460 | plugin_set, plugin); | |
7d8f15e6 PP |
461 | |
462 | end: | |
463 | return; | |
8c1a3187 PP |
464 | } |
465 | ||
33b34c43 | 466 | #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */ |