| 1 | /* |
| 2 | * python-plugin-provider.c |
| 3 | * |
| 4 | * Babeltrace Python plugin provider |
| 5 | * |
| 6 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 7 | * |
| 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 | #define BT_LOG_TAG "PLUGIN-PY" |
| 28 | |
| 29 | #include <babeltrace/babeltrace-internal.h> |
| 30 | #include <babeltrace/compiler-internal.h> |
| 31 | #include <babeltrace/plugin/plugin-const.h> |
| 32 | #include <babeltrace/plugin/plugin-internal.h> |
| 33 | #include <babeltrace/graph/component-class.h> |
| 34 | #include <babeltrace/graph/component-class-internal.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <signal.h> |
| 37 | #include <Python.h> |
| 38 | #include <glib.h> |
| 39 | #include <gmodule.h> |
| 40 | |
| 41 | #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_" |
| 42 | #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1) |
| 43 | #define PYTHON_PLUGIN_FILE_EXT ".py" |
| 44 | #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1) |
| 45 | |
| 46 | enum python_state { |
| 47 | /* init_python() not called yet */ |
| 48 | PYTHON_STATE_NOT_INITED, |
| 49 | |
| 50 | /* init_python() called once with success */ |
| 51 | PYTHON_STATE_FULLY_INITIALIZED, |
| 52 | |
| 53 | /* init_python() called once without success */ |
| 54 | PYTHON_STATE_CANNOT_INITIALIZE, |
| 55 | } python_state = PYTHON_STATE_NOT_INITED; |
| 56 | |
| 57 | static PyObject *py_try_load_plugin_module_func = NULL; |
| 58 | static bool python_was_initialized_by_us; |
| 59 | |
| 60 | static |
| 61 | void print_python_traceback_warn(void) |
| 62 | { |
| 63 | if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) { |
| 64 | BT_LOGW_STR("Exception occured: traceback: "); |
| 65 | PyErr_Print(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static |
| 70 | void pyerr_clear(void) |
| 71 | { |
| 72 | if (Py_IsInitialized()) { |
| 73 | PyErr_Clear(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static |
| 78 | void init_python(void) |
| 79 | { |
| 80 | PyObject *py_bt2_py_plugin_mod = NULL; |
| 81 | const char *dis_python_env; |
| 82 | #ifndef __MINGW32__ |
| 83 | sighandler_t old_sigint = signal(SIGINT, SIG_DFL); |
| 84 | #endif |
| 85 | |
| 86 | if (python_state != PYTHON_STATE_NOT_INITED) { |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * User can disable Python plugin support with the |
| 92 | * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to |
| 93 | * 1. |
| 94 | */ |
| 95 | dis_python_env = getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS"); |
| 96 | if (dis_python_env && strcmp(dis_python_env, "1") == 0) { |
| 97 | BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`."); |
| 98 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | if (!Py_IsInitialized()) { |
| 103 | BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter."); |
| 104 | Py_InitializeEx(0); |
| 105 | python_was_initialized_by_us = true; |
| 106 | BT_LOGI("Initialized Python interpreter: version=\"%s\"", |
| 107 | Py_GetVersion()); |
| 108 | } else { |
| 109 | BT_LOGI("Python interpreter is already initialized: version=\"%s\"", |
| 110 | Py_GetVersion()); |
| 111 | } |
| 112 | |
| 113 | py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin"); |
| 114 | if (!py_bt2_py_plugin_mod) { |
| 115 | BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled."); |
| 116 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | py_try_load_plugin_module_func = |
| 121 | PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module"); |
| 122 | if (!py_try_load_plugin_module_func) { |
| 123 | BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled."); |
| 124 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | python_state = PYTHON_STATE_FULLY_INITIALIZED; |
| 129 | |
| 130 | end: |
| 131 | #ifndef __MINGW32__ |
| 132 | if (old_sigint != SIG_ERR) { |
| 133 | (void) signal(SIGINT, old_sigint); |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | print_python_traceback_warn(); |
| 138 | pyerr_clear(); |
| 139 | Py_XDECREF(py_bt2_py_plugin_mod); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | __attribute__((destructor)) static |
| 144 | void fini_python(void) { |
| 145 | if (Py_IsInitialized() && python_was_initialized_by_us) { |
| 146 | if (py_try_load_plugin_module_func) { |
| 147 | Py_DECREF(py_try_load_plugin_module_func); |
| 148 | py_try_load_plugin_module_func = NULL; |
| 149 | } |
| 150 | |
| 151 | Py_Finalize(); |
| 152 | BT_LOGI_STR("Finalized Python interpreter."); |
| 153 | } |
| 154 | |
| 155 | python_state = PYTHON_STATE_NOT_INITED; |
| 156 | } |
| 157 | |
| 158 | static |
| 159 | bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info) |
| 160 | { |
| 161 | bt_plugin *plugin = NULL; |
| 162 | PyObject *py_name = NULL; |
| 163 | PyObject *py_author = NULL; |
| 164 | PyObject *py_description = NULL; |
| 165 | PyObject *py_license = NULL; |
| 166 | PyObject *py_version = NULL; |
| 167 | PyObject *py_comp_class_addrs = NULL; |
| 168 | const char *name = NULL; |
| 169 | const char *author = NULL; |
| 170 | const char *description = NULL; |
| 171 | const char *license = NULL; |
| 172 | unsigned int major = 0, minor = 0, patch = 0; |
| 173 | const char *version_extra = NULL; |
| 174 | int ret; |
| 175 | |
| 176 | BT_ASSERT(plugin_info); |
| 177 | BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED); |
| 178 | py_name = PyObject_GetAttrString(plugin_info, "name"); |
| 179 | if (!py_name) { |
| 180 | BT_LOGW("Cannot find `name` attribute in Python plugin info object: " |
| 181 | "py-plugin-info-addr=%p", plugin_info); |
| 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | py_author = PyObject_GetAttrString(plugin_info, "author"); |
| 186 | if (!py_author) { |
| 187 | BT_LOGW("Cannot find `author` attribute in Python plugin info object: " |
| 188 | "py-plugin-info-addr=%p", plugin_info); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | py_description = PyObject_GetAttrString(plugin_info, "description"); |
| 193 | if (!py_description) { |
| 194 | BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: " |
| 195 | "py-plugin-info-addr=%p", plugin_info); |
| 196 | goto error; |
| 197 | } |
| 198 | |
| 199 | py_license = PyObject_GetAttrString(plugin_info, "license"); |
| 200 | if (!py_license) { |
| 201 | BT_LOGW("Cannot find `license` attribute in Python plugin info object: " |
| 202 | "py-plugin-info-addr=%p", plugin_info); |
| 203 | goto error; |
| 204 | } |
| 205 | |
| 206 | py_version = PyObject_GetAttrString(plugin_info, "version"); |
| 207 | if (!py_version) { |
| 208 | BT_LOGW("Cannot find `version` attribute in Python plugin info object: " |
| 209 | "py-plugin-info-addr=%p", plugin_info); |
| 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | py_comp_class_addrs = PyObject_GetAttrString(plugin_info, |
| 214 | "comp_class_addrs"); |
| 215 | if (!py_comp_class_addrs) { |
| 216 | BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: " |
| 217 | "py-plugin-info-addr=%p", plugin_info); |
| 218 | goto error; |
| 219 | } |
| 220 | |
| 221 | if (PyUnicode_Check(py_name)) { |
| 222 | name = PyUnicode_AsUTF8(py_name); |
| 223 | if (!name) { |
| 224 | BT_LOGW("Cannot decode Python plugin name string: " |
| 225 | "py-plugin-info-addr=%p", plugin_info); |
| 226 | goto error; |
| 227 | } |
| 228 | } else { |
| 229 | /* Plugin name is mandatory */ |
| 230 | BT_LOGW("Plugin name is not a string: " |
| 231 | "py-plugin-info-addr=%p", plugin_info); |
| 232 | goto error; |
| 233 | } |
| 234 | |
| 235 | if (PyUnicode_Check(py_author)) { |
| 236 | author = PyUnicode_AsUTF8(py_author); |
| 237 | if (!author) { |
| 238 | BT_LOGW("Cannot decode Python plugin author string: " |
| 239 | "py-plugin-info-addr=%p", plugin_info); |
| 240 | goto error; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (PyUnicode_Check(py_description)) { |
| 245 | description = PyUnicode_AsUTF8(py_description); |
| 246 | if (!description) { |
| 247 | BT_LOGW("Cannot decode Python plugin description string: " |
| 248 | "py-plugin-info-addr=%p", plugin_info); |
| 249 | goto error; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (PyUnicode_Check(py_license)) { |
| 254 | license = PyUnicode_AsUTF8(py_license); |
| 255 | if (!license) { |
| 256 | BT_LOGW("Cannot decode Python plugin license string: " |
| 257 | "py-plugin-info-addr=%p", plugin_info); |
| 258 | goto error; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (PyTuple_Check(py_version)) { |
| 263 | if (PyTuple_Size(py_version) >= 3) { |
| 264 | PyObject *py_major = PyTuple_GetItem(py_version, 0); |
| 265 | PyObject *py_minor = PyTuple_GetItem(py_version, 1); |
| 266 | PyObject *py_patch = PyTuple_GetItem(py_version, 2); |
| 267 | |
| 268 | BT_ASSERT(py_major); |
| 269 | BT_ASSERT(py_minor); |
| 270 | BT_ASSERT(py_patch); |
| 271 | |
| 272 | if (PyLong_Check(py_major)) { |
| 273 | major = PyLong_AsUnsignedLong(py_major); |
| 274 | } |
| 275 | |
| 276 | if (PyLong_Check(py_minor)) { |
| 277 | minor = PyLong_AsUnsignedLong(py_minor); |
| 278 | } |
| 279 | |
| 280 | if (PyLong_Check(py_patch)) { |
| 281 | patch = PyLong_AsUnsignedLong(py_patch); |
| 282 | } |
| 283 | |
| 284 | if (PyErr_Occurred()) { |
| 285 | /* Overflow error, most probably */ |
| 286 | BT_LOGW("Invalid Python plugin version format: " |
| 287 | "py-plugin-info-addr=%p", plugin_info); |
| 288 | goto error; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (PyTuple_Size(py_version) >= 4) { |
| 293 | PyObject *py_extra = PyTuple_GetItem(py_version, 3); |
| 294 | |
| 295 | BT_ASSERT(py_extra); |
| 296 | |
| 297 | if (PyUnicode_Check(py_extra)) { |
| 298 | version_extra = PyUnicode_AsUTF8(py_extra); |
| 299 | if (!version_extra) { |
| 300 | BT_LOGW("Cannot decode Python plugin version's extra string: " |
| 301 | "py-plugin-info-addr=%p", plugin_info); |
| 302 | goto error; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON); |
| 309 | if (!plugin) { |
| 310 | BT_LOGE_STR("Cannot create empty plugin object."); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | bt_plugin_set_name(plugin, name); |
| 315 | |
| 316 | if (description) { |
| 317 | bt_plugin_set_description(plugin, description); |
| 318 | } |
| 319 | |
| 320 | if (author) { |
| 321 | bt_plugin_set_author(plugin, author); |
| 322 | } |
| 323 | |
| 324 | if (license) { |
| 325 | bt_plugin_set_license(plugin, license); |
| 326 | } |
| 327 | |
| 328 | bt_plugin_set_version(plugin, major, minor, patch, version_extra); |
| 329 | |
| 330 | if (PyList_Check(py_comp_class_addrs)) { |
| 331 | size_t i; |
| 332 | |
| 333 | for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) { |
| 334 | bt_component_class *comp_class; |
| 335 | PyObject *py_comp_class_addr; |
| 336 | |
| 337 | py_comp_class_addr = |
| 338 | PyList_GetItem(py_comp_class_addrs, i); |
| 339 | BT_ASSERT(py_comp_class_addr); |
| 340 | if (PyLong_Check(py_comp_class_addr)) { |
| 341 | comp_class = PyLong_AsVoidPtr(py_comp_class_addr); |
| 342 | } else { |
| 343 | BT_LOGW("Component class address is not an integer in Python plugin info object: " |
| 344 | "py-plugin-info-addr=%p, index=%zu", |
| 345 | plugin_info, i); |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | ret = bt_plugin_add_component_class(plugin, comp_class); |
| 350 | if (ret < 0) { |
| 351 | BT_LOGE("Cannot add component class to plugin: " |
| 352 | "py-plugin-info-addr=%p, " |
| 353 | "plugin-addr=%p, plugin-name=\"%s\", " |
| 354 | "comp-class-addr=%p, " |
| 355 | "comp-class-name=\"%s\", " |
| 356 | "comp-class-type=%s", |
| 357 | plugin_info, |
| 358 | plugin, bt_plugin_get_name(plugin), |
| 359 | comp_class, |
| 360 | bt_component_class_get_name(comp_class), |
| 361 | bt_component_class_type_string( |
| 362 | bt_component_class_get_type(comp_class))); |
| 363 | continue; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | goto end; |
| 369 | |
| 370 | error: |
| 371 | print_python_traceback_warn(); |
| 372 | pyerr_clear(); |
| 373 | BT_OBJECT_PUT_REF_AND_RESET(plugin); |
| 374 | |
| 375 | end: |
| 376 | Py_XDECREF(py_name); |
| 377 | Py_XDECREF(py_author); |
| 378 | Py_XDECREF(py_description); |
| 379 | Py_XDECREF(py_license); |
| 380 | Py_XDECREF(py_version); |
| 381 | Py_XDECREF(py_comp_class_addrs); |
| 382 | return plugin; |
| 383 | } |
| 384 | |
| 385 | G_MODULE_EXPORT |
| 386 | bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path) |
| 387 | { |
| 388 | bt_plugin_set *plugin_set = NULL; |
| 389 | bt_plugin *plugin = NULL; |
| 390 | PyObject *py_plugin_info = NULL; |
| 391 | gchar *basename = NULL; |
| 392 | size_t path_len; |
| 393 | |
| 394 | BT_ASSERT(path); |
| 395 | |
| 396 | if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) { |
| 397 | /* |
| 398 | * We do not even care about the rest of the function |
| 399 | * here because we already know Python cannot be fully |
| 400 | * initialized. |
| 401 | */ |
| 402 | goto error; |
| 403 | } |
| 404 | |
| 405 | BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path); |
| 406 | path_len = strlen(path); |
| 407 | |
| 408 | /* File name ends with `.py` */ |
| 409 | if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN, |
| 410 | PYTHON_PLUGIN_FILE_EXT, |
| 411 | PYTHON_PLUGIN_FILE_EXT_LEN) != 0) { |
| 412 | BT_LOGD("Skipping non-Python file: path=\"%s\"", path); |
| 413 | goto error; |
| 414 | } |
| 415 | |
| 416 | /* File name starts with `bt_plugin_` */ |
| 417 | basename = g_path_get_basename(path); |
| 418 | if (!basename) { |
| 419 | BT_LOGW("Cannot get path's basename: path=\"%s\"", path); |
| 420 | goto error; |
| 421 | } |
| 422 | |
| 423 | if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX, |
| 424 | PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) { |
| 425 | BT_LOGD("Skipping Python file not starting with `%s`: " |
| 426 | "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path); |
| 427 | goto error; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Initialize Python now. |
| 432 | * |
| 433 | * This is not done in the library contructor because the |
| 434 | * interpreter is somewhat slow to initialize. If you don't |
| 435 | * have any potential Python plugins, you don't need to endure |
| 436 | * this waiting time everytime you load the library. |
| 437 | */ |
| 438 | init_python(); |
| 439 | if (python_state != PYTHON_STATE_FULLY_INITIALIZED) { |
| 440 | /* |
| 441 | * For some reason we cannot initialize Python, |
| 442 | * import the required modules, and get the required |
| 443 | * attributes from them. |
| 444 | */ |
| 445 | BT_LOGI("Failed to initialize Python interpreter."); |
| 446 | goto error; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Call bt2.py_plugin._try_load_plugin_module() with this path |
| 451 | * to get plugin info if the plugin is loadable and complete. |
| 452 | * This function returns None when there's an error, but just in |
| 453 | * case we also manually clear the last Python error state. |
| 454 | */ |
| 455 | BT_LOGD_STR("Getting Python plugin info object from Python module."); |
| 456 | py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func, |
| 457 | "(s)", path); |
| 458 | if (!py_plugin_info || py_plugin_info == Py_None) { |
| 459 | BT_LOGW("Cannot load Python plugin: path=\"%s\"", path); |
| 460 | print_python_traceback_warn(); |
| 461 | PyErr_Clear(); |
| 462 | goto error; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Get bt_plugin from plugin info object. |
| 467 | */ |
| 468 | plugin = bt_plugin_from_python_plugin_info(py_plugin_info); |
| 469 | if (!plugin) { |
| 470 | BT_LOGW("Cannot create plugin object from Python plugin info object: " |
| 471 | "path=\"%s\", py-plugin-info-addr=%p", |
| 472 | path, py_plugin_info); |
| 473 | goto error; |
| 474 | } |
| 475 | |
| 476 | bt_plugin_set_path(plugin, path); |
| 477 | plugin_set = bt_plugin_set_create(); |
| 478 | if (!plugin_set) { |
| 479 | BT_LOGE_STR("Cannot create empty plugin set."); |
| 480 | goto error; |
| 481 | } |
| 482 | |
| 483 | bt_plugin_set_add_plugin(plugin_set, plugin); |
| 484 | BT_LOGD("Created all Python plugins from file: path=\"%s\", " |
| 485 | "plugin-addr=%p, plugin-name=\"%s\"", |
| 486 | path, plugin, bt_plugin_get_name(plugin)); |
| 487 | goto end; |
| 488 | |
| 489 | error: |
| 490 | BT_OBJECT_PUT_REF_AND_RESET(plugin_set); |
| 491 | |
| 492 | end: |
| 493 | bt_plugin_put_ref(plugin); |
| 494 | Py_XDECREF(py_plugin_info); |
| 495 | g_free(basename); |
| 496 | return plugin_set; |
| 497 | } |