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