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