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