| 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 "LIB/PLUGIN-PY" |
| 28 | #include "lib/logging.h" |
| 29 | |
| 30 | #include "python-plugin-provider.h" |
| 31 | |
| 32 | #include "common/macros.h" |
| 33 | #include "compat/compiler.h" |
| 34 | #include <babeltrace2/plugin/plugin-const.h> |
| 35 | #include "lib/plugin/plugin.h" |
| 36 | #include <babeltrace2/graph/component-class.h> |
| 37 | #include <babeltrace2/current-thread.h> |
| 38 | #include "lib/graph/component-class.h" |
| 39 | #include "py-common/py-common.h" |
| 40 | #include <stdbool.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <signal.h> |
| 43 | #include <Python.h> |
| 44 | #include <glib.h> |
| 45 | #include <gmodule.h> |
| 46 | |
| 47 | #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_" |
| 48 | #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1) |
| 49 | #define PYTHON_PLUGIN_FILE_EXT ".py" |
| 50 | #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1) |
| 51 | |
| 52 | static enum python_state { |
| 53 | /* init_python() not called yet */ |
| 54 | PYTHON_STATE_NOT_INITED, |
| 55 | |
| 56 | /* init_python() called once with success */ |
| 57 | PYTHON_STATE_FULLY_INITIALIZED, |
| 58 | |
| 59 | /* init_python() called once without success */ |
| 60 | PYTHON_STATE_CANNOT_INITIALIZE, |
| 61 | |
| 62 | /* |
| 63 | * init_python() called, but environment variable asks the |
| 64 | * Python interpreter not to be loaded. |
| 65 | */ |
| 66 | PYTHON_STATE_WONT_INITIALIZE, |
| 67 | } python_state = PYTHON_STATE_NOT_INITED; |
| 68 | |
| 69 | static PyObject *py_try_load_plugin_module_func = NULL; |
| 70 | static bool python_was_initialized_by_us; |
| 71 | |
| 72 | static |
| 73 | void append_python_traceback_error_cause(void) |
| 74 | { |
| 75 | GString *exc = NULL; |
| 76 | |
| 77 | if (Py_IsInitialized() && PyErr_Occurred()) { |
| 78 | exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL); |
| 79 | if (!exc) { |
| 80 | BT_LOGE_STR("Failed to format Python exception."); |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( |
| 85 | BT_LIB_LOG_LIBBABELTRACE2_NAME, "%s", exc->str); |
| 86 | } |
| 87 | |
| 88 | end: |
| 89 | if (exc) { |
| 90 | g_string_free(exc, TRUE); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static |
| 95 | void log_python_traceback(int log_level) |
| 96 | { |
| 97 | GString *exc = NULL; |
| 98 | |
| 99 | if (Py_IsInitialized() && PyErr_Occurred()) { |
| 100 | exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL); |
| 101 | if (!exc) { |
| 102 | BT_LOGE_STR("Failed to format Python exception."); |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | BT_LOG_WRITE(log_level, BT_LOG_TAG, |
| 107 | "Exception occured: Python traceback:\n%s", exc->str); |
| 108 | } |
| 109 | |
| 110 | end: |
| 111 | if (exc) { |
| 112 | g_string_free(exc, TRUE); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | void pyerr_clear(void) |
| 118 | { |
| 119 | if (Py_IsInitialized()) { |
| 120 | PyErr_Clear(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static |
| 125 | int init_python(void) |
| 126 | { |
| 127 | int ret = BT_FUNC_STATUS_OK; |
| 128 | PyObject *py_bt2_py_plugin_mod = NULL; |
| 129 | const char *dis_python_env; |
| 130 | #ifndef __MINGW32__ |
| 131 | sig_t old_sigint = signal(SIGINT, SIG_DFL); |
| 132 | #endif |
| 133 | |
| 134 | switch (python_state) { |
| 135 | case PYTHON_STATE_NOT_INITED: |
| 136 | break; |
| 137 | case PYTHON_STATE_FULLY_INITIALIZED: |
| 138 | goto end; |
| 139 | case PYTHON_STATE_WONT_INITIALIZE: |
| 140 | ret = BT_FUNC_STATUS_NOT_FOUND; |
| 141 | goto end; |
| 142 | case PYTHON_STATE_CANNOT_INITIALIZE: |
| 143 | ret = BT_FUNC_STATUS_ERROR; |
| 144 | goto end; |
| 145 | default: |
| 146 | bt_common_abort(); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * User can disable Python plugin support with the |
| 151 | * `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment variable |
| 152 | * set to 1. |
| 153 | */ |
| 154 | dis_python_env = getenv("LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS"); |
| 155 | if (dis_python_env && strcmp(dis_python_env, "1") == 0) { |
| 156 | BT_LOGI_STR("Python plugin support is disabled because the " |
| 157 | "`LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment " |
| 158 | "variable is set to `1`."); |
| 159 | python_state = PYTHON_STATE_WONT_INITIALIZE; |
| 160 | ret = BT_FUNC_STATUS_NOT_FOUND; |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | if (!Py_IsInitialized()) { |
| 165 | BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter."); |
| 166 | Py_InitializeEx(0); |
| 167 | python_was_initialized_by_us = true; |
| 168 | BT_LOGI("Initialized Python interpreter: version=\"%s\"", |
| 169 | Py_GetVersion()); |
| 170 | } else { |
| 171 | BT_LOGI("Python interpreter is already initialized: version=\"%s\"", |
| 172 | Py_GetVersion()); |
| 173 | } |
| 174 | |
| 175 | py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin"); |
| 176 | if (!py_bt2_py_plugin_mod) { |
| 177 | append_python_traceback_error_cause(); |
| 178 | BT_LIB_LOGW_APPEND_CAUSE( |
| 179 | "Cannot import `bt2.py_plugin` Python module: " |
| 180 | "Python plugin support is disabled."); |
| 181 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
| 182 | ret = BT_FUNC_STATUS_ERROR; |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | py_try_load_plugin_module_func = |
| 187 | PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module"); |
| 188 | if (!py_try_load_plugin_module_func) { |
| 189 | append_python_traceback_error_cause(); |
| 190 | BT_LIB_LOGW_APPEND_CAUSE( |
| 191 | "Cannot get `_try_load_plugin_module` attribute from `bt2.py_plugin` Python module: " |
| 192 | "Python plugin support is disabled."); |
| 193 | python_state = PYTHON_STATE_CANNOT_INITIALIZE; |
| 194 | ret = BT_FUNC_STATUS_ERROR; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | python_state = PYTHON_STATE_FULLY_INITIALIZED; |
| 199 | |
| 200 | end: |
| 201 | #ifndef __MINGW32__ |
| 202 | if (old_sigint != SIG_ERR) { |
| 203 | (void) signal(SIGINT, old_sigint); |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | log_python_traceback(ret == BT_FUNC_STATUS_ERROR ? |
| 208 | BT_LOG_WARNING : BT_LOG_INFO); |
| 209 | pyerr_clear(); |
| 210 | Py_XDECREF(py_bt2_py_plugin_mod); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | __attribute__((destructor)) static |
| 215 | void fini_python(void) { |
| 216 | if (Py_IsInitialized() && python_was_initialized_by_us) { |
| 217 | if (py_try_load_plugin_module_func) { |
| 218 | Py_DECREF(py_try_load_plugin_module_func); |
| 219 | py_try_load_plugin_module_func = NULL; |
| 220 | } |
| 221 | |
| 222 | Py_Finalize(); |
| 223 | BT_LOGI_STR("Finalized Python interpreter."); |
| 224 | } |
| 225 | |
| 226 | python_state = PYTHON_STATE_NOT_INITED; |
| 227 | } |
| 228 | |
| 229 | static |
| 230 | int bt_plugin_from_python_plugin_info(PyObject *plugin_info, |
| 231 | bool fail_on_load_error, bt_plugin **plugin_out) |
| 232 | { |
| 233 | int status = BT_FUNC_STATUS_OK; |
| 234 | PyObject *py_name = NULL; |
| 235 | PyObject *py_author = NULL; |
| 236 | PyObject *py_description = NULL; |
| 237 | PyObject *py_license = NULL; |
| 238 | PyObject *py_version = NULL; |
| 239 | PyObject *py_comp_class_addrs = NULL; |
| 240 | const char *name = NULL; |
| 241 | const char *author = NULL; |
| 242 | const char *description = NULL; |
| 243 | const char *license = NULL; |
| 244 | unsigned int major = 0, minor = 0, patch = 0; |
| 245 | const char *version_extra = NULL; |
| 246 | |
| 247 | BT_ASSERT(plugin_out); |
| 248 | *plugin_out = NULL; |
| 249 | BT_ASSERT(plugin_info); |
| 250 | BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED); |
| 251 | py_name = PyObject_GetAttrString(plugin_info, "name"); |
| 252 | if (!py_name) { |
| 253 | if (fail_on_load_error) { |
| 254 | append_python_traceback_error_cause(); |
| 255 | BT_LIB_LOGW_APPEND_CAUSE( |
| 256 | "Cannot find `name` attribute in Python plugin info object: " |
| 257 | "py-plugin-info-addr=%p", plugin_info); |
| 258 | status = BT_FUNC_STATUS_ERROR; |
| 259 | } else { |
| 260 | BT_LIB_LOGW( |
| 261 | "Cannot find `name` attribute in Python plugin info object: " |
| 262 | "py-plugin-info-addr=%p", plugin_info); |
| 263 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 264 | } |
| 265 | |
| 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | py_author = PyObject_GetAttrString(plugin_info, "author"); |
| 270 | if (!py_author) { |
| 271 | if (fail_on_load_error) { |
| 272 | append_python_traceback_error_cause(); |
| 273 | BT_LIB_LOGW_APPEND_CAUSE( |
| 274 | "Cannot find `author` attribute in Python plugin info object: " |
| 275 | "py-plugin-info-addr=%p", plugin_info); |
| 276 | status = BT_FUNC_STATUS_ERROR; |
| 277 | } else { |
| 278 | BT_LIB_LOGW( |
| 279 | "Cannot find `author` attribute in Python plugin info object: " |
| 280 | "py-plugin-info-addr=%p", plugin_info); |
| 281 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 282 | } |
| 283 | |
| 284 | goto error; |
| 285 | } |
| 286 | |
| 287 | py_description = PyObject_GetAttrString(plugin_info, "description"); |
| 288 | if (!py_description) { |
| 289 | if (fail_on_load_error) { |
| 290 | append_python_traceback_error_cause(); |
| 291 | BT_LIB_LOGW_APPEND_CAUSE( |
| 292 | "Cannot find `description` attribute in Python plugin info object: " |
| 293 | "py-plugin-info-addr=%p", plugin_info); |
| 294 | status = BT_FUNC_STATUS_ERROR; |
| 295 | } else { |
| 296 | BT_LIB_LOGW( |
| 297 | "Cannot find `description` attribute in Python plugin info object: " |
| 298 | "py-plugin-info-addr=%p", plugin_info); |
| 299 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 300 | } |
| 301 | |
| 302 | goto error; |
| 303 | } |
| 304 | |
| 305 | py_license = PyObject_GetAttrString(plugin_info, "license"); |
| 306 | if (!py_license) { |
| 307 | if (fail_on_load_error) { |
| 308 | append_python_traceback_error_cause(); |
| 309 | BT_LIB_LOGW_APPEND_CAUSE( |
| 310 | "Cannot find `license` attribute in Python plugin info object: " |
| 311 | "py-plugin-info-addr=%p", plugin_info); |
| 312 | status = BT_FUNC_STATUS_ERROR; |
| 313 | } else { |
| 314 | BT_LIB_LOGW( |
| 315 | "Cannot find `license` attribute in Python plugin info object: " |
| 316 | "py-plugin-info-addr=%p", plugin_info); |
| 317 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 318 | } |
| 319 | |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | py_version = PyObject_GetAttrString(plugin_info, "version"); |
| 324 | if (!py_version) { |
| 325 | if (fail_on_load_error) { |
| 326 | append_python_traceback_error_cause(); |
| 327 | BT_LIB_LOGW_APPEND_CAUSE( |
| 328 | "Cannot find `version` attribute in Python plugin info object: " |
| 329 | "py-plugin-info-addr=%p", plugin_info); |
| 330 | status = BT_FUNC_STATUS_ERROR; |
| 331 | } else { |
| 332 | BT_LIB_LOGW( |
| 333 | "Cannot find `version` attribute in Python plugin info object: " |
| 334 | "py-plugin-info-addr=%p", plugin_info); |
| 335 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 336 | } |
| 337 | |
| 338 | goto error; |
| 339 | } |
| 340 | |
| 341 | py_comp_class_addrs = PyObject_GetAttrString(plugin_info, |
| 342 | "comp_class_addrs"); |
| 343 | if (!py_comp_class_addrs) { |
| 344 | if (fail_on_load_error) { |
| 345 | append_python_traceback_error_cause(); |
| 346 | BT_LIB_LOGW_APPEND_CAUSE( |
| 347 | "Cannot find `comp_class_addrs` attribute in Python plugin info object: " |
| 348 | "py-plugin-info-addr=%p", plugin_info); |
| 349 | status = BT_FUNC_STATUS_ERROR; |
| 350 | } else { |
| 351 | BT_LIB_LOGW( |
| 352 | "Cannot find `comp_class_addrs` attribute in Python plugin info object: " |
| 353 | "py-plugin-info-addr=%p", plugin_info); |
| 354 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 355 | } |
| 356 | |
| 357 | goto error; |
| 358 | } |
| 359 | |
| 360 | if (PyUnicode_Check(py_name)) { |
| 361 | name = PyUnicode_AsUTF8(py_name); |
| 362 | if (!name) { |
| 363 | if (fail_on_load_error) { |
| 364 | append_python_traceback_error_cause(); |
| 365 | BT_LIB_LOGW_APPEND_CAUSE( |
| 366 | "Cannot decode Python plugin name string: " |
| 367 | "py-plugin-info-addr=%p", plugin_info); |
| 368 | status = BT_FUNC_STATUS_ERROR; |
| 369 | } else { |
| 370 | BT_LIB_LOGW( |
| 371 | "Cannot decode Python plugin name string: " |
| 372 | "py-plugin-info-addr=%p", plugin_info); |
| 373 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 374 | } |
| 375 | |
| 376 | goto error; |
| 377 | } |
| 378 | } else { |
| 379 | /* Plugin name is mandatory */ |
| 380 | if (fail_on_load_error) { |
| 381 | append_python_traceback_error_cause(); |
| 382 | BT_LIB_LOGW_APPEND_CAUSE( |
| 383 | "Plugin name is not a string: " |
| 384 | "py-plugin-info-addr=%p", plugin_info); |
| 385 | status = BT_FUNC_STATUS_ERROR; |
| 386 | } else { |
| 387 | BT_LIB_LOGW( |
| 388 | "Plugin name is not a string: " |
| 389 | "py-plugin-info-addr=%p", plugin_info); |
| 390 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 391 | } |
| 392 | |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | if (PyUnicode_Check(py_author)) { |
| 397 | author = PyUnicode_AsUTF8(py_author); |
| 398 | if (!author) { |
| 399 | if (fail_on_load_error) { |
| 400 | append_python_traceback_error_cause(); |
| 401 | BT_LIB_LOGW_APPEND_CAUSE( |
| 402 | "Cannot decode Python plugin author string: " |
| 403 | "py-plugin-info-addr=%p", plugin_info); |
| 404 | status = BT_FUNC_STATUS_ERROR; |
| 405 | } else { |
| 406 | BT_LIB_LOGW( |
| 407 | "Cannot decode Python plugin author string: " |
| 408 | "py-plugin-info-addr=%p", plugin_info); |
| 409 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 410 | } |
| 411 | |
| 412 | goto error; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if (PyUnicode_Check(py_description)) { |
| 417 | description = PyUnicode_AsUTF8(py_description); |
| 418 | if (!description) { |
| 419 | if (fail_on_load_error) { |
| 420 | append_python_traceback_error_cause(); |
| 421 | BT_LIB_LOGW_APPEND_CAUSE( |
| 422 | "Cannot decode Python plugin description string: " |
| 423 | "py-plugin-info-addr=%p", plugin_info); |
| 424 | status = BT_FUNC_STATUS_ERROR; |
| 425 | } else { |
| 426 | BT_LIB_LOGW( |
| 427 | "Cannot decode Python plugin description string: " |
| 428 | "py-plugin-info-addr=%p", plugin_info); |
| 429 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 430 | } |
| 431 | |
| 432 | goto error; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (PyUnicode_Check(py_license)) { |
| 437 | license = PyUnicode_AsUTF8(py_license); |
| 438 | if (!license) { |
| 439 | if (fail_on_load_error) { |
| 440 | append_python_traceback_error_cause(); |
| 441 | BT_LIB_LOGW_APPEND_CAUSE( |
| 442 | "Cannot decode Python plugin license string: " |
| 443 | "py-plugin-info-addr=%p", plugin_info); |
| 444 | status = BT_FUNC_STATUS_ERROR; |
| 445 | } else { |
| 446 | BT_LIB_LOGW( |
| 447 | "Cannot decode Python plugin license string: " |
| 448 | "py-plugin-info-addr=%p", plugin_info); |
| 449 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 450 | } |
| 451 | |
| 452 | goto error; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (PyTuple_Check(py_version)) { |
| 457 | if (PyTuple_Size(py_version) >= 3) { |
| 458 | PyObject *py_major = PyTuple_GetItem(py_version, 0); |
| 459 | PyObject *py_minor = PyTuple_GetItem(py_version, 1); |
| 460 | PyObject *py_patch = PyTuple_GetItem(py_version, 2); |
| 461 | |
| 462 | BT_ASSERT(py_major); |
| 463 | BT_ASSERT(py_minor); |
| 464 | BT_ASSERT(py_patch); |
| 465 | |
| 466 | if (PyLong_Check(py_major)) { |
| 467 | major = PyLong_AsUnsignedLong(py_major); |
| 468 | } |
| 469 | |
| 470 | if (PyLong_Check(py_minor)) { |
| 471 | minor = PyLong_AsUnsignedLong(py_minor); |
| 472 | } |
| 473 | |
| 474 | if (PyLong_Check(py_patch)) { |
| 475 | patch = PyLong_AsUnsignedLong(py_patch); |
| 476 | } |
| 477 | |
| 478 | if (PyErr_Occurred()) { |
| 479 | /* Overflow error, most probably */ |
| 480 | if (fail_on_load_error) { |
| 481 | append_python_traceback_error_cause(); |
| 482 | BT_LIB_LOGW_APPEND_CAUSE( |
| 483 | "Invalid Python plugin version format: " |
| 484 | "py-plugin-info-addr=%p", plugin_info); |
| 485 | status = BT_FUNC_STATUS_ERROR; |
| 486 | } else { |
| 487 | BT_LIB_LOGW( |
| 488 | "Invalid Python plugin version format: " |
| 489 | "py-plugin-info-addr=%p", plugin_info); |
| 490 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 491 | } |
| 492 | |
| 493 | goto error; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (PyTuple_Size(py_version) >= 4) { |
| 498 | PyObject *py_extra = PyTuple_GetItem(py_version, 3); |
| 499 | |
| 500 | BT_ASSERT(py_extra); |
| 501 | |
| 502 | if (PyUnicode_Check(py_extra)) { |
| 503 | version_extra = PyUnicode_AsUTF8(py_extra); |
| 504 | if (!version_extra) { |
| 505 | if (fail_on_load_error) { |
| 506 | append_python_traceback_error_cause(); |
| 507 | BT_LIB_LOGW_APPEND_CAUSE( |
| 508 | "Cannot decode Python plugin version's extra string: " |
| 509 | "py-plugin-info-addr=%p", plugin_info); |
| 510 | status = BT_FUNC_STATUS_ERROR; |
| 511 | } else { |
| 512 | BT_LIB_LOGW( |
| 513 | "Cannot decode Python plugin version's extra string: " |
| 514 | "py-plugin-info-addr=%p", plugin_info); |
| 515 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 516 | } |
| 517 | |
| 518 | goto error; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | *plugin_out = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON); |
| 525 | if (!*plugin_out) { |
| 526 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin object."); |
| 527 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 528 | goto error; |
| 529 | } |
| 530 | |
| 531 | bt_plugin_set_name(*plugin_out, name); |
| 532 | |
| 533 | if (description) { |
| 534 | bt_plugin_set_description(*plugin_out, description); |
| 535 | } |
| 536 | |
| 537 | if (author) { |
| 538 | bt_plugin_set_author(*plugin_out, author); |
| 539 | } |
| 540 | |
| 541 | if (license) { |
| 542 | bt_plugin_set_license(*plugin_out, license); |
| 543 | } |
| 544 | |
| 545 | bt_plugin_set_version(*plugin_out, major, minor, patch, version_extra); |
| 546 | |
| 547 | if (PyList_Check(py_comp_class_addrs)) { |
| 548 | size_t i; |
| 549 | |
| 550 | for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) { |
| 551 | bt_component_class *comp_class; |
| 552 | PyObject *py_comp_class_addr; |
| 553 | |
| 554 | py_comp_class_addr = |
| 555 | PyList_GetItem(py_comp_class_addrs, i); |
| 556 | BT_ASSERT(py_comp_class_addr); |
| 557 | if (PyLong_Check(py_comp_class_addr)) { |
| 558 | comp_class = PyLong_AsVoidPtr(py_comp_class_addr); |
| 559 | } else { |
| 560 | if (fail_on_load_error) { |
| 561 | append_python_traceback_error_cause(); |
| 562 | BT_LIB_LOGW_APPEND_CAUSE( |
| 563 | "Component class address is not an integer in Python plugin info object: " |
| 564 | "py-plugin-info-addr=%p, index=%zu", |
| 565 | plugin_info, i); |
| 566 | status = BT_FUNC_STATUS_ERROR; |
| 567 | } else { |
| 568 | BT_LIB_LOGW( |
| 569 | "Component class address is not an integer in Python plugin info object: " |
| 570 | "py-plugin-info-addr=%p, index=%zu", |
| 571 | plugin_info, i); |
| 572 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 573 | } |
| 574 | |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | status = bt_plugin_add_component_class(*plugin_out, |
| 579 | comp_class); |
| 580 | if (status < 0) { |
| 581 | BT_LIB_LOGE_APPEND_CAUSE( |
| 582 | "Cannot add component class to plugin: " |
| 583 | "py-plugin-info-addr=%p, " |
| 584 | "plugin-addr=%p, plugin-name=\"%s\", " |
| 585 | "comp-class-addr=%p, " |
| 586 | "comp-class-name=\"%s\", " |
| 587 | "comp-class-type=%s", |
| 588 | plugin_info, *plugin_out, |
| 589 | bt_plugin_get_name(*plugin_out), |
| 590 | comp_class, |
| 591 | bt_component_class_get_name(comp_class), |
| 592 | bt_component_class_type_string( |
| 593 | bt_component_class_get_type(comp_class))); |
| 594 | goto error; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | goto end; |
| 600 | |
| 601 | error: |
| 602 | BT_ASSERT(status != BT_FUNC_STATUS_OK); |
| 603 | log_python_traceback(fail_on_load_error ? BT_LOG_WARNING : BT_LOG_INFO); |
| 604 | pyerr_clear(); |
| 605 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_out); |
| 606 | |
| 607 | end: |
| 608 | Py_XDECREF(py_name); |
| 609 | Py_XDECREF(py_author); |
| 610 | Py_XDECREF(py_description); |
| 611 | Py_XDECREF(py_license); |
| 612 | Py_XDECREF(py_version); |
| 613 | Py_XDECREF(py_comp_class_addrs); |
| 614 | return status; |
| 615 | } |
| 616 | |
| 617 | G_MODULE_EXPORT |
| 618 | int bt_plugin_python_create_all_from_file(const char *path, |
| 619 | bool fail_on_load_error, struct bt_plugin_set **plugin_set_out) |
| 620 | { |
| 621 | bt_plugin *plugin = NULL; |
| 622 | PyObject *py_plugin_info = NULL; |
| 623 | gchar *basename = NULL; |
| 624 | size_t path_len; |
| 625 | int status = BT_FUNC_STATUS_OK; |
| 626 | |
| 627 | BT_ASSERT(path); |
| 628 | |
| 629 | if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) { |
| 630 | /* |
| 631 | * We do not even care about the rest of the function |
| 632 | * here because we already know Python cannot be fully |
| 633 | * initialized. |
| 634 | */ |
| 635 | BT_LIB_LOGE_APPEND_CAUSE( |
| 636 | "Python interpreter could not be initialized previously."); |
| 637 | status = BT_FUNC_STATUS_ERROR; |
| 638 | goto error; |
| 639 | } else if (python_state == PYTHON_STATE_WONT_INITIALIZE) { |
| 640 | /* |
| 641 | * This is not an error: the environment requires that |
| 642 | * Python plugins are disabled, so it's simply not |
| 643 | * found. |
| 644 | */ |
| 645 | BT_LOGI_STR("Python plugin support was disabled previously " |
| 646 | "because the `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` " |
| 647 | "environment variable is set to `1`."); |
| 648 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 649 | goto error; |
| 650 | } |
| 651 | |
| 652 | BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"", |
| 653 | path); |
| 654 | path_len = strlen(path); |
| 655 | |
| 656 | /* File name ends with `.py` */ |
| 657 | if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN, |
| 658 | PYTHON_PLUGIN_FILE_EXT, |
| 659 | PYTHON_PLUGIN_FILE_EXT_LEN) != 0) { |
| 660 | BT_LOGI("Skipping non-Python file: path=\"%s\"", path); |
| 661 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | /* File name starts with `bt_plugin_` */ |
| 666 | basename = g_path_get_basename(path); |
| 667 | if (!basename) { |
| 668 | BT_LIB_LOGE_APPEND_CAUSE( |
| 669 | "Cannot get path's basename: path=\"%s\"", path); |
| 670 | status = BT_FUNC_STATUS_ERROR; |
| 671 | goto error; |
| 672 | } |
| 673 | |
| 674 | if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX, |
| 675 | PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) { |
| 676 | BT_LOGI("Skipping Python file not starting with `%s`: " |
| 677 | "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path); |
| 678 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 679 | goto error; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * Initialize Python now. |
| 684 | * |
| 685 | * This is not done in the library contructor because the |
| 686 | * interpreter is somewhat slow to initialize. If you don't |
| 687 | * have any potential Python plugins, you don't need to endure |
| 688 | * this waiting time everytime you load the library. |
| 689 | */ |
| 690 | status = init_python(); |
| 691 | if (status != BT_FUNC_STATUS_OK) { |
| 692 | /* init_python() logs and append errors */ |
| 693 | goto error; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Call bt2.py_plugin._try_load_plugin_module() with this path |
| 698 | * to get plugin info if the plugin is loadable and complete. |
| 699 | * This function returns None when there's an error, but just in |
| 700 | * case we also manually clear the last Python error state. |
| 701 | */ |
| 702 | BT_LOGD_STR("Getting Python plugin info object from Python module."); |
| 703 | py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func, |
| 704 | "(s)", path); |
| 705 | if (!py_plugin_info || py_plugin_info == Py_None) { |
| 706 | if (fail_on_load_error) { |
| 707 | append_python_traceback_error_cause(); |
| 708 | BT_LIB_LOGW_APPEND_CAUSE( |
| 709 | "Cannot load Python plugin: path=\"%s\"", path); |
| 710 | status = BT_FUNC_STATUS_ERROR; |
| 711 | } else { |
| 712 | BT_LIB_LOGW( |
| 713 | "Cannot load Python plugin: path=\"%s\"", path); |
| 714 | status = BT_FUNC_STATUS_NOT_FOUND; |
| 715 | } |
| 716 | |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Get bt_plugin from plugin info object. |
| 722 | */ |
| 723 | plugin = NULL; |
| 724 | status = bt_plugin_from_python_plugin_info(py_plugin_info, |
| 725 | fail_on_load_error, &plugin); |
| 726 | if (status < 0) { |
| 727 | /* |
| 728 | * bt_plugin_from_python_plugin_info() handles |
| 729 | * `fail_on_load_error`, so this is a "real" error. |
| 730 | */ |
| 731 | BT_LIB_LOGW_APPEND_CAUSE( |
| 732 | "Cannot create plugin object from Python plugin info object: " |
| 733 | "path=\"%s\", py-plugin-info-addr=%p", |
| 734 | path, py_plugin_info); |
| 735 | BT_ASSERT(!plugin); |
| 736 | goto error; |
| 737 | } else if (status == BT_FUNC_STATUS_NOT_FOUND) { |
| 738 | BT_ASSERT(!plugin); |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | BT_ASSERT(status == BT_FUNC_STATUS_OK); |
| 743 | BT_ASSERT(plugin); |
| 744 | bt_plugin_set_path(plugin, path); |
| 745 | *plugin_set_out = bt_plugin_set_create(); |
| 746 | if (!*plugin_set_out) { |
| 747 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set."); |
| 748 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 749 | goto error; |
| 750 | } |
| 751 | |
| 752 | bt_plugin_set_add_plugin(*plugin_set_out, plugin); |
| 753 | BT_LOGD("Created all Python plugins from file: path=\"%s\", " |
| 754 | "plugin-addr=%p, plugin-name=\"%s\"", |
| 755 | path, plugin, bt_plugin_get_name(plugin)); |
| 756 | goto end; |
| 757 | |
| 758 | error: |
| 759 | BT_ASSERT(status != BT_FUNC_STATUS_OK); |
| 760 | log_python_traceback(BT_LOG_WARNING); |
| 761 | pyerr_clear(); |
| 762 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
| 763 | |
| 764 | end: |
| 765 | bt_plugin_put_ref(plugin); |
| 766 | Py_XDECREF(py_plugin_info); |
| 767 | |
| 768 | g_free(basename); |
| 769 | |
| 770 | return status; |
| 771 | } |