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