Commit | Line | Data |
---|---|---|
33b34c43 PP |
1 | #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H |
2 | #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H | |
fb2dcc52 JG |
3 | |
4 | /* | |
fb2dcc52 JG |
5 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
6 | * | |
7 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
28 | #include <babeltrace/babeltrace-internal.h> | |
d94d92ac | 29 | #include <babeltrace/graph/component-class-internal.h> |
3fe0bf43 | 30 | #include <babeltrace/plugin/plugin.h> |
33b34c43 | 31 | #include <babeltrace/plugin/plugin-dev.h> |
d94d92ac | 32 | #include <babeltrace/plugin/plugin-so-internal.h> |
b8a06801 | 33 | #include <babeltrace/object-internal.h> |
d94d92ac | 34 | #include <babeltrace/object.h> |
c55a9f58 | 35 | #include <babeltrace/types.h> |
f6ccaed9 | 36 | #include <babeltrace/assert-internal.h> |
55bb57e0 | 37 | #include <glib.h> |
fb2dcc52 | 38 | |
55bb57e0 PP |
39 | enum bt_plugin_type { |
40 | BT_PLUGIN_TYPE_SO = 0, | |
41 | BT_PLUGIN_TYPE_PYTHON = 1, | |
fb2dcc52 JG |
42 | }; |
43 | ||
33b34c43 PP |
44 | struct bt_plugin { |
45 | struct bt_object base; | |
55bb57e0 | 46 | enum bt_plugin_type type; |
cba174d5 | 47 | |
d94d92ac PP |
48 | /* Arrays of `struct bt_component_class *` (owned by this) */ |
49 | GPtrArray *src_comp_classes; | |
50 | GPtrArray *flt_comp_classes; | |
51 | GPtrArray *sink_comp_classes; | |
6ba0b073 | 52 | |
55bb57e0 PP |
53 | /* Info (owned by this) */ |
54 | struct { | |
55 | GString *path; | |
56 | GString *name; | |
57 | GString *author; | |
58 | GString *license; | |
59 | GString *description; | |
60 | struct { | |
61 | unsigned int major; | |
62 | unsigned int minor; | |
63 | unsigned int patch; | |
64 | GString *extra; | |
65 | } version; | |
d94d92ac PP |
66 | bool path_set; |
67 | bool name_set; | |
68 | bool author_set; | |
69 | bool license_set; | |
70 | bool description_set; | |
71 | bool version_set; | |
55bb57e0 PP |
72 | } info; |
73 | ||
74 | /* Value depends on the specific plugin type */ | |
75 | void *spec_data; | |
6fbd4105 | 76 | void (*destroy_spec_data)(struct bt_plugin *); |
33b34c43 | 77 | }; |
fb2dcc52 | 78 | |
a8ff38ef PP |
79 | struct bt_plugin_set { |
80 | struct bt_object base; | |
81 | ||
82 | /* Array of struct bt_plugin * */ | |
83 | GPtrArray *plugins; | |
84 | }; | |
85 | ||
3fe0bf43 PP |
86 | static inline |
87 | const char *bt_plugin_status_string(enum bt_plugin_status status) | |
88 | { | |
89 | switch (status) { | |
90 | case BT_PLUGIN_STATUS_OK: | |
91 | return "BT_PLUGIN_STATUS_OK"; | |
92 | case BT_PLUGIN_STATUS_ERROR: | |
93 | return "BT_PLUGIN_STATUS_ERROR"; | |
94 | case BT_PLUGIN_STATUS_NOMEM: | |
95 | return "BT_PLUGIN_STATUS_NOMEM"; | |
96 | default: | |
97 | return "(unknown)"; | |
98 | } | |
99 | } | |
100 | ||
101 | static inline | |
102 | const char *bt_plugin_type_string(enum bt_plugin_type type) | |
103 | { | |
104 | switch (type) { | |
105 | case BT_PLUGIN_TYPE_SO: | |
106 | return "BT_PLUGIN_TYPE_SO"; | |
107 | case BT_PLUGIN_TYPE_PYTHON: | |
108 | return "BT_PLUGIN_TYPE_PYTHON"; | |
109 | default: | |
110 | return "(unknown)"; | |
111 | } | |
112 | } | |
113 | ||
6fbd4105 PP |
114 | static inline |
115 | void bt_plugin_destroy(struct bt_object *obj) | |
116 | { | |
117 | struct bt_plugin *plugin; | |
118 | ||
f6ccaed9 | 119 | BT_ASSERT(obj); |
6fbd4105 | 120 | plugin = container_of(obj, struct bt_plugin, base); |
d94d92ac | 121 | BT_LIB_LOGD("Destroying plugin object: %!+l", plugin); |
6fbd4105 PP |
122 | |
123 | if (plugin->destroy_spec_data) { | |
124 | plugin->destroy_spec_data(plugin); | |
125 | } | |
126 | ||
d94d92ac PP |
127 | if (plugin->src_comp_classes) { |
128 | BT_LOGD_STR("Putting source component classes."); | |
129 | g_ptr_array_free(plugin->src_comp_classes, TRUE); | |
db5504f9 | 130 | plugin->src_comp_classes = NULL; |
d94d92ac PP |
131 | } |
132 | ||
133 | if (plugin->flt_comp_classes) { | |
134 | BT_LOGD_STR("Putting filter component classes."); | |
135 | g_ptr_array_free(plugin->flt_comp_classes, TRUE); | |
db5504f9 | 136 | plugin->flt_comp_classes = NULL; |
d94d92ac PP |
137 | } |
138 | ||
139 | if (plugin->sink_comp_classes) { | |
140 | BT_LOGD_STR("Putting sink component classes."); | |
141 | g_ptr_array_free(plugin->sink_comp_classes, TRUE); | |
db5504f9 | 142 | plugin->sink_comp_classes = NULL; |
6fbd4105 PP |
143 | } |
144 | ||
145 | if (plugin->info.name) { | |
146 | g_string_free(plugin->info.name, TRUE); | |
db5504f9 | 147 | plugin->info.name = NULL; |
6fbd4105 PP |
148 | } |
149 | ||
150 | if (plugin->info.path) { | |
151 | g_string_free(plugin->info.path, TRUE); | |
db5504f9 | 152 | plugin->info.path = NULL; |
6fbd4105 PP |
153 | } |
154 | ||
155 | if (plugin->info.description) { | |
156 | g_string_free(plugin->info.description, TRUE); | |
db5504f9 | 157 | plugin->info.description = NULL; |
6fbd4105 PP |
158 | } |
159 | ||
160 | if (plugin->info.author) { | |
161 | g_string_free(plugin->info.author, TRUE); | |
db5504f9 | 162 | plugin->info.author = NULL; |
6fbd4105 PP |
163 | } |
164 | ||
165 | if (plugin->info.license) { | |
166 | g_string_free(plugin->info.license, TRUE); | |
db5504f9 | 167 | plugin->info.license = NULL; |
6fbd4105 PP |
168 | } |
169 | ||
170 | if (plugin->info.version.extra) { | |
171 | g_string_free(plugin->info.version.extra, TRUE); | |
db5504f9 | 172 | plugin->info.version.extra = NULL; |
6fbd4105 PP |
173 | } |
174 | ||
175 | g_free(plugin); | |
176 | } | |
177 | ||
178 | static inline | |
179 | struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type) | |
180 | { | |
181 | struct bt_plugin *plugin = NULL; | |
182 | ||
3fe0bf43 PP |
183 | BT_LOGD("Creating empty plugin object: type=%s", |
184 | bt_plugin_type_string(type)); | |
185 | ||
6fbd4105 PP |
186 | plugin = g_new0(struct bt_plugin, 1); |
187 | if (!plugin) { | |
3fe0bf43 | 188 | BT_LOGE_STR("Failed to allocate one plugin."); |
6fbd4105 PP |
189 | goto error; |
190 | } | |
191 | ||
3fea54f6 | 192 | bt_object_init_shared(&plugin->base, bt_plugin_destroy); |
6fbd4105 PP |
193 | plugin->type = type; |
194 | ||
d94d92ac PP |
195 | /* Create empty arrays of component classes */ |
196 | plugin->src_comp_classes = | |
197 | g_ptr_array_new_with_free_func( | |
198 | (GDestroyNotify) bt_object_put_ref); | |
199 | if (!plugin->src_comp_classes) { | |
200 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
201 | goto error; | |
202 | } | |
203 | ||
204 | plugin->flt_comp_classes = | |
205 | g_ptr_array_new_with_free_func( | |
206 | (GDestroyNotify) bt_object_put_ref); | |
207 | if (!plugin->flt_comp_classes) { | |
208 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
209 | goto error; | |
210 | } | |
211 | ||
212 | plugin->sink_comp_classes = | |
213 | g_ptr_array_new_with_free_func( | |
214 | (GDestroyNotify) bt_object_put_ref); | |
215 | if (!plugin->sink_comp_classes) { | |
3fe0bf43 | 216 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
6fbd4105 PP |
217 | goto error; |
218 | } | |
219 | ||
220 | /* Create empty info */ | |
221 | plugin->info.name = g_string_new(NULL); | |
222 | if (!plugin->info.name) { | |
3fe0bf43 | 223 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
224 | goto error; |
225 | } | |
226 | ||
227 | plugin->info.path = g_string_new(NULL); | |
228 | if (!plugin->info.path) { | |
3fe0bf43 | 229 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
230 | goto error; |
231 | } | |
232 | ||
233 | plugin->info.description = g_string_new(NULL); | |
234 | if (!plugin->info.description) { | |
3fe0bf43 | 235 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
236 | goto error; |
237 | } | |
238 | ||
239 | plugin->info.author = g_string_new(NULL); | |
240 | if (!plugin->info.author) { | |
3fe0bf43 | 241 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
242 | goto error; |
243 | } | |
244 | ||
245 | plugin->info.license = g_string_new(NULL); | |
246 | if (!plugin->info.license) { | |
3fe0bf43 | 247 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
248 | goto error; |
249 | } | |
250 | ||
251 | plugin->info.version.extra = g_string_new(NULL); | |
252 | if (!plugin->info.version.extra) { | |
3fe0bf43 | 253 | BT_LOGE_STR("Failed to allocate a GString."); |
6fbd4105 PP |
254 | goto error; |
255 | } | |
256 | ||
d94d92ac | 257 | BT_LIB_LOGD("Created empty plugin object: %!+l", plugin); |
6fbd4105 PP |
258 | goto end; |
259 | ||
260 | error: | |
65300d60 | 261 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
6fbd4105 PP |
262 | |
263 | end: | |
264 | return plugin; | |
265 | } | |
55bb57e0 PP |
266 | |
267 | static inline | |
268 | void bt_plugin_set_path(struct bt_plugin *plugin, const char *path) | |
269 | { | |
f6ccaed9 PP |
270 | BT_ASSERT(plugin); |
271 | BT_ASSERT(path); | |
55bb57e0 | 272 | g_string_assign(plugin->info.path, path); |
c55a9f58 | 273 | plugin->info.path_set = BT_TRUE; |
d94d92ac PP |
274 | BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"", |
275 | plugin, path); | |
55bb57e0 PP |
276 | } |
277 | ||
278 | static inline | |
279 | void bt_plugin_set_name(struct bt_plugin *plugin, const char *name) | |
280 | { | |
f6ccaed9 PP |
281 | BT_ASSERT(plugin); |
282 | BT_ASSERT(name); | |
55bb57e0 | 283 | g_string_assign(plugin->info.name, name); |
c55a9f58 | 284 | plugin->info.name_set = BT_TRUE; |
d94d92ac | 285 | BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"", |
3fe0bf43 | 286 | plugin, name); |
55bb57e0 PP |
287 | } |
288 | ||
289 | static inline | |
290 | void bt_plugin_set_description(struct bt_plugin *plugin, | |
291 | const char *description) | |
292 | { | |
f6ccaed9 PP |
293 | BT_ASSERT(plugin); |
294 | BT_ASSERT(description); | |
55bb57e0 | 295 | g_string_assign(plugin->info.description, description); |
c55a9f58 | 296 | plugin->info.description_set = BT_TRUE; |
d94d92ac | 297 | BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin); |
55bb57e0 PP |
298 | } |
299 | ||
300 | static inline | |
301 | void bt_plugin_set_author(struct bt_plugin *plugin, const char *author) | |
302 | { | |
f6ccaed9 PP |
303 | BT_ASSERT(plugin); |
304 | BT_ASSERT(author); | |
55bb57e0 | 305 | g_string_assign(plugin->info.author, author); |
c55a9f58 | 306 | plugin->info.author_set = BT_TRUE; |
d94d92ac PP |
307 | BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"", |
308 | plugin, author); | |
55bb57e0 PP |
309 | } |
310 | ||
311 | static inline | |
312 | void bt_plugin_set_license(struct bt_plugin *plugin, const char *license) | |
313 | { | |
f6ccaed9 PP |
314 | BT_ASSERT(plugin); |
315 | BT_ASSERT(license); | |
55bb57e0 | 316 | g_string_assign(plugin->info.license, license); |
c55a9f58 | 317 | plugin->info.license_set = BT_TRUE; |
d94d92ac PP |
318 | BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"", |
319 | plugin, license); | |
55bb57e0 PP |
320 | } |
321 | ||
322 | static inline | |
323 | void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major, | |
324 | unsigned int minor, unsigned int patch, const char *extra) | |
325 | { | |
f6ccaed9 | 326 | BT_ASSERT(plugin); |
55bb57e0 PP |
327 | plugin->info.version.major = major; |
328 | plugin->info.version.minor = minor; | |
329 | plugin->info.version.patch = patch; | |
330 | ||
331 | if (extra) { | |
332 | g_string_assign(plugin->info.version.extra, extra); | |
333 | } | |
334 | ||
c55a9f58 | 335 | plugin->info.version_set = BT_TRUE; |
d94d92ac | 336 | BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, " |
3fe0bf43 | 337 | "major=%u, minor=%u, patch=%u, extra=\"%s\"", |
d94d92ac | 338 | plugin, major, minor, patch, extra); |
55bb57e0 PP |
339 | } |
340 | ||
341 | static inline | |
d94d92ac PP |
342 | enum bt_plugin_status bt_plugin_add_component_class( |
343 | struct bt_plugin *plugin, struct bt_component_class *comp_class) | |
55bb57e0 | 344 | { |
d94d92ac PP |
345 | GPtrArray *comp_classes; |
346 | ||
f6ccaed9 | 347 | BT_ASSERT(plugin); |
d94d92ac PP |
348 | BT_ASSERT(comp_class); |
349 | ||
350 | switch (comp_class->type) { | |
351 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
352 | comp_classes = plugin->src_comp_classes; | |
353 | break; | |
354 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
355 | comp_classes = plugin->flt_comp_classes; | |
356 | break; | |
357 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
358 | comp_classes = plugin->sink_comp_classes; | |
359 | break; | |
360 | default: | |
361 | abort(); | |
362 | } | |
3fe0bf43 | 363 | |
d94d92ac | 364 | /* Add new component class */ |
398454ed PP |
365 | bt_object_get_ref(comp_class); |
366 | g_ptr_array_add(comp_classes, comp_class); | |
d94d92ac PP |
367 | |
368 | /* Special case for a shared object plugin */ | |
369 | if (plugin->type == BT_PLUGIN_TYPE_SO) { | |
370 | bt_plugin_so_on_add_component_class(plugin, comp_class); | |
3fe0bf43 PP |
371 | } |
372 | ||
d94d92ac PP |
373 | BT_LIB_LOGD("Added component class to plugin: " |
374 | "%![plugin-]+l, %![cc-]+C", plugin, comp_class); | |
375 | return BT_PLUGIN_STATUS_OK; | |
55bb57e0 PP |
376 | } |
377 | ||
a8ff38ef PP |
378 | static |
379 | void bt_plugin_set_destroy(struct bt_object *obj) | |
380 | { | |
381 | struct bt_plugin_set *plugin_set = | |
382 | container_of(obj, struct bt_plugin_set, base); | |
383 | ||
384 | if (!plugin_set) { | |
385 | return; | |
386 | } | |
387 | ||
3fe0bf43 PP |
388 | BT_LOGD("Destroying plugin set: addr=%p", plugin_set); |
389 | ||
a8ff38ef | 390 | if (plugin_set->plugins) { |
3fe0bf43 | 391 | BT_LOGD_STR("Putting plugins."); |
a8ff38ef PP |
392 | g_ptr_array_free(plugin_set->plugins, TRUE); |
393 | } | |
394 | ||
395 | g_free(plugin_set); | |
396 | } | |
397 | ||
398 | static inline | |
399 | struct bt_plugin_set *bt_plugin_set_create(void) | |
400 | { | |
401 | struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1); | |
402 | ||
403 | if (!plugin_set) { | |
404 | goto end; | |
405 | } | |
406 | ||
3fe0bf43 | 407 | BT_LOGD_STR("Creating empty plugin set."); |
3fea54f6 | 408 | bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy); |
a8ff38ef PP |
409 | |
410 | plugin_set->plugins = g_ptr_array_new_with_free_func( | |
65300d60 | 411 | (GDestroyNotify) bt_object_put_ref); |
a8ff38ef | 412 | if (!plugin_set->plugins) { |
3fe0bf43 | 413 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
65300d60 | 414 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
a8ff38ef PP |
415 | goto end; |
416 | } | |
417 | ||
3fe0bf43 PP |
418 | BT_LOGD("Created empty plugin set: addr=%p", plugin_set); |
419 | ||
a8ff38ef PP |
420 | end: |
421 | return plugin_set; | |
422 | } | |
423 | ||
424 | static inline | |
425 | void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set, | |
426 | struct bt_plugin *plugin) | |
427 | { | |
f6ccaed9 PP |
428 | BT_ASSERT(plugin_set); |
429 | BT_ASSERT(plugin); | |
398454ed PP |
430 | bt_object_get_ref(plugin); |
431 | g_ptr_array_add(plugin_set->plugins, plugin); | |
d94d92ac PP |
432 | BT_LIB_LOGV("Added plugin to plugin set: " |
433 | "plugin-set-addr=%p, %![plugin-]+l", | |
434 | plugin_set, plugin); | |
8c1a3187 PP |
435 | } |
436 | ||
33b34c43 | 437 | #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */ |