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