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