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