2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
6 * Babeltrace Python plugin provider
9 #define BT_LOG_TAG "LIB/PLUGIN-PY"
10 #include "lib/logging.h"
12 #include "python-plugin-provider.h"
14 #include "common/macros.h"
15 #include "compat/compiler.h"
16 #include <babeltrace2/plugin/plugin-loading.h>
17 #include "lib/plugin/plugin.h"
18 #include <babeltrace2/graph/component-class.h>
19 #include <babeltrace2/error-reporting.h>
20 #include "lib/graph/component-class.h"
21 #include "py-common/py-common.h"
28 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
29 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
30 #define PYTHON_PLUGIN_FILE_EXT ".py"
31 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
33 static enum python_state
{
34 /* init_python() not called yet */
35 PYTHON_STATE_NOT_INITED
,
37 /* init_python() called once with success */
38 PYTHON_STATE_FULLY_INITIALIZED
,
40 /* init_python() called once without success */
41 PYTHON_STATE_CANNOT_INITIALIZE
,
44 * init_python() called, but environment variable asks the
45 * Python interpreter not to be loaded.
47 PYTHON_STATE_WONT_INITIALIZE
,
48 } python_state
= PYTHON_STATE_NOT_INITED
;
50 static PyObject
*py_try_load_plugin_module_func
= NULL
;
51 static bool python_was_initialized_by_us
;
54 void append_python_traceback_error_cause(void)
58 if (Py_IsInitialized() && PyErr_Occurred()) {
59 exc
= bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL
);
61 BT_LOGE_STR("Failed to format Python exception.");
65 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
66 BT_LIB_LOG_LIBBABELTRACE2_NAME
, "%s", exc
->str
);
71 g_string_free(exc
, TRUE
);
76 void log_python_traceback(int log_level
)
80 if (Py_IsInitialized() && PyErr_Occurred()) {
81 exc
= bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL
);
83 BT_LOGE_STR("Failed to format Python exception.");
87 BT_LOG_WRITE_PRINTF(log_level
, BT_LOG_TAG
,
88 "Exception occurred: Python traceback:\n%s", exc
->str
);
93 g_string_free(exc
, TRUE
);
98 void pyerr_clear(void)
100 if (Py_IsInitialized()) {
106 int init_python(void)
108 int ret
= BT_FUNC_STATUS_OK
;
109 PyObject
*py_bt2_py_plugin_mod
= NULL
;
110 const char *dis_python_env
;
112 sig_t old_sigint
= signal(SIGINT
, SIG_DFL
);
115 switch (python_state
) {
116 case PYTHON_STATE_NOT_INITED
:
118 case PYTHON_STATE_FULLY_INITIALIZED
:
120 case PYTHON_STATE_WONT_INITIALIZE
:
121 ret
= BT_FUNC_STATUS_NOT_FOUND
;
123 case PYTHON_STATE_CANNOT_INITIALIZE
:
124 ret
= BT_FUNC_STATUS_ERROR
;
131 * User can disable Python plugin support with the
132 * `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment variable
135 dis_python_env
= getenv("LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS");
136 if (dis_python_env
&& strcmp(dis_python_env
, "1") == 0) {
137 BT_LOGI_STR("Python plugin support is disabled because the "
138 "`LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment "
139 "variable is set to `1`.");
140 python_state
= PYTHON_STATE_WONT_INITIALIZE
;
141 ret
= BT_FUNC_STATUS_NOT_FOUND
;
145 if (!Py_IsInitialized()) {
146 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
148 python_was_initialized_by_us
= true;
149 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
152 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
156 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
157 if (!py_bt2_py_plugin_mod
) {
158 append_python_traceback_error_cause();
159 BT_LIB_LOGW_APPEND_CAUSE(
160 "Cannot import `bt2.py_plugin` Python module: "
161 "Python plugin support is disabled.");
162 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
163 ret
= BT_FUNC_STATUS_ERROR
;
167 py_try_load_plugin_module_func
=
168 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
169 if (!py_try_load_plugin_module_func
) {
170 append_python_traceback_error_cause();
171 BT_LIB_LOGW_APPEND_CAUSE(
172 "Cannot get `_try_load_plugin_module` attribute from `bt2.py_plugin` Python module: "
173 "Python plugin support is disabled.");
174 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
175 ret
= BT_FUNC_STATUS_ERROR
;
179 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
183 if (old_sigint
!= SIG_ERR
) {
184 (void) signal(SIGINT
, old_sigint
);
188 log_python_traceback(ret
== BT_FUNC_STATUS_ERROR
?
189 BT_LOG_WARNING
: BT_LOG_INFO
);
191 Py_XDECREF(py_bt2_py_plugin_mod
);
195 __attribute__((destructor
)) static
196 void fini_python(void) {
197 if (Py_IsInitialized() && python_was_initialized_by_us
) {
198 if (py_try_load_plugin_module_func
) {
199 Py_DECREF(py_try_load_plugin_module_func
);
200 py_try_load_plugin_module_func
= NULL
;
204 BT_LOGI_STR("Finalized Python interpreter.");
207 python_state
= PYTHON_STATE_NOT_INITED
;
211 int bt_plugin_from_python_plugin_info(PyObject
*plugin_info
,
212 bool fail_on_load_error
, bt_plugin
**plugin_out
)
214 int status
= BT_FUNC_STATUS_OK
;
215 PyObject
*py_name
= NULL
;
216 PyObject
*py_author
= NULL
;
217 PyObject
*py_description
= NULL
;
218 PyObject
*py_license
= NULL
;
219 PyObject
*py_version
= NULL
;
220 PyObject
*py_comp_class_addrs
= NULL
;
221 const char *name
= NULL
;
222 const char *author
= NULL
;
223 const char *description
= NULL
;
224 const char *license
= NULL
;
225 unsigned int major
= 0, minor
= 0, patch
= 0;
226 const char *version_extra
= NULL
;
228 BT_ASSERT(plugin_out
);
230 BT_ASSERT(plugin_info
);
231 BT_ASSERT(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
232 py_name
= PyObject_GetAttrString(plugin_info
, "name");
234 if (fail_on_load_error
) {
235 append_python_traceback_error_cause();
236 BT_LIB_LOGW_APPEND_CAUSE(
237 "Cannot find `name` attribute in Python plugin info object: "
238 "py-plugin-info-addr=%p", plugin_info
);
239 status
= BT_FUNC_STATUS_ERROR
;
242 "Cannot find `name` attribute in Python plugin info object: "
243 "py-plugin-info-addr=%p", plugin_info
);
244 status
= BT_FUNC_STATUS_NOT_FOUND
;
250 py_author
= PyObject_GetAttrString(plugin_info
, "author");
252 if (fail_on_load_error
) {
253 append_python_traceback_error_cause();
254 BT_LIB_LOGW_APPEND_CAUSE(
255 "Cannot find `author` attribute in Python plugin info object: "
256 "py-plugin-info-addr=%p", plugin_info
);
257 status
= BT_FUNC_STATUS_ERROR
;
260 "Cannot find `author` attribute in Python plugin info object: "
261 "py-plugin-info-addr=%p", plugin_info
);
262 status
= BT_FUNC_STATUS_NOT_FOUND
;
268 py_description
= PyObject_GetAttrString(plugin_info
, "description");
269 if (!py_description
) {
270 if (fail_on_load_error
) {
271 append_python_traceback_error_cause();
272 BT_LIB_LOGW_APPEND_CAUSE(
273 "Cannot find `description` attribute in Python plugin info object: "
274 "py-plugin-info-addr=%p", plugin_info
);
275 status
= BT_FUNC_STATUS_ERROR
;
278 "Cannot find `description` attribute in Python plugin info object: "
279 "py-plugin-info-addr=%p", plugin_info
);
280 status
= BT_FUNC_STATUS_NOT_FOUND
;
286 py_license
= PyObject_GetAttrString(plugin_info
, "license");
288 if (fail_on_load_error
) {
289 append_python_traceback_error_cause();
290 BT_LIB_LOGW_APPEND_CAUSE(
291 "Cannot find `license` attribute in Python plugin info object: "
292 "py-plugin-info-addr=%p", plugin_info
);
293 status
= BT_FUNC_STATUS_ERROR
;
296 "Cannot find `license` attribute in Python plugin info object: "
297 "py-plugin-info-addr=%p", plugin_info
);
298 status
= BT_FUNC_STATUS_NOT_FOUND
;
304 py_version
= PyObject_GetAttrString(plugin_info
, "version");
306 if (fail_on_load_error
) {
307 append_python_traceback_error_cause();
308 BT_LIB_LOGW_APPEND_CAUSE(
309 "Cannot find `version` attribute in Python plugin info object: "
310 "py-plugin-info-addr=%p", plugin_info
);
311 status
= BT_FUNC_STATUS_ERROR
;
314 "Cannot find `version` attribute in Python plugin info object: "
315 "py-plugin-info-addr=%p", plugin_info
);
316 status
= BT_FUNC_STATUS_NOT_FOUND
;
322 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
324 if (!py_comp_class_addrs
) {
325 if (fail_on_load_error
) {
326 append_python_traceback_error_cause();
327 BT_LIB_LOGW_APPEND_CAUSE(
328 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
329 "py-plugin-info-addr=%p", plugin_info
);
330 status
= BT_FUNC_STATUS_ERROR
;
333 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
334 "py-plugin-info-addr=%p", plugin_info
);
335 status
= BT_FUNC_STATUS_NOT_FOUND
;
341 if (PyUnicode_Check(py_name
)) {
342 name
= PyUnicode_AsUTF8(py_name
);
344 if (fail_on_load_error
) {
345 append_python_traceback_error_cause();
346 BT_LIB_LOGW_APPEND_CAUSE(
347 "Cannot decode Python plugin name string: "
348 "py-plugin-info-addr=%p", plugin_info
);
349 status
= BT_FUNC_STATUS_ERROR
;
352 "Cannot decode Python plugin name string: "
353 "py-plugin-info-addr=%p", plugin_info
);
354 status
= BT_FUNC_STATUS_NOT_FOUND
;
360 /* Plugin name is mandatory */
361 if (fail_on_load_error
) {
362 append_python_traceback_error_cause();
363 BT_LIB_LOGW_APPEND_CAUSE(
364 "Plugin name is not a string: "
365 "py-plugin-info-addr=%p", plugin_info
);
366 status
= BT_FUNC_STATUS_ERROR
;
369 "Plugin name is not a string: "
370 "py-plugin-info-addr=%p", plugin_info
);
371 status
= BT_FUNC_STATUS_NOT_FOUND
;
377 if (PyUnicode_Check(py_author
)) {
378 author
= PyUnicode_AsUTF8(py_author
);
380 if (fail_on_load_error
) {
381 append_python_traceback_error_cause();
382 BT_LIB_LOGW_APPEND_CAUSE(
383 "Cannot decode Python plugin author string: "
384 "py-plugin-info-addr=%p", plugin_info
);
385 status
= BT_FUNC_STATUS_ERROR
;
388 "Cannot decode Python plugin author string: "
389 "py-plugin-info-addr=%p", plugin_info
);
390 status
= BT_FUNC_STATUS_NOT_FOUND
;
397 if (PyUnicode_Check(py_description
)) {
398 description
= PyUnicode_AsUTF8(py_description
);
400 if (fail_on_load_error
) {
401 append_python_traceback_error_cause();
402 BT_LIB_LOGW_APPEND_CAUSE(
403 "Cannot decode Python plugin description string: "
404 "py-plugin-info-addr=%p", plugin_info
);
405 status
= BT_FUNC_STATUS_ERROR
;
408 "Cannot decode Python plugin description string: "
409 "py-plugin-info-addr=%p", plugin_info
);
410 status
= BT_FUNC_STATUS_NOT_FOUND
;
417 if (PyUnicode_Check(py_license
)) {
418 license
= PyUnicode_AsUTF8(py_license
);
420 if (fail_on_load_error
) {
421 append_python_traceback_error_cause();
422 BT_LIB_LOGW_APPEND_CAUSE(
423 "Cannot decode Python plugin license string: "
424 "py-plugin-info-addr=%p", plugin_info
);
425 status
= BT_FUNC_STATUS_ERROR
;
428 "Cannot decode Python plugin license string: "
429 "py-plugin-info-addr=%p", plugin_info
);
430 status
= BT_FUNC_STATUS_NOT_FOUND
;
437 if (PyTuple_Check(py_version
)) {
438 if (PyTuple_Size(py_version
) >= 3) {
439 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
440 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
441 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
447 if (PyLong_Check(py_major
)) {
448 major
= PyLong_AsUnsignedLong(py_major
);
451 if (PyLong_Check(py_minor
)) {
452 minor
= PyLong_AsUnsignedLong(py_minor
);
455 if (PyLong_Check(py_patch
)) {
456 patch
= PyLong_AsUnsignedLong(py_patch
);
459 if (PyErr_Occurred()) {
460 /* Overflow error, most probably */
461 if (fail_on_load_error
) {
462 append_python_traceback_error_cause();
463 BT_LIB_LOGW_APPEND_CAUSE(
464 "Invalid Python plugin version format: "
465 "py-plugin-info-addr=%p", plugin_info
);
466 status
= BT_FUNC_STATUS_ERROR
;
469 "Invalid Python plugin version format: "
470 "py-plugin-info-addr=%p", plugin_info
);
471 status
= BT_FUNC_STATUS_NOT_FOUND
;
478 if (PyTuple_Size(py_version
) >= 4) {
479 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
483 if (PyUnicode_Check(py_extra
)) {
484 version_extra
= PyUnicode_AsUTF8(py_extra
);
485 if (!version_extra
) {
486 if (fail_on_load_error
) {
487 append_python_traceback_error_cause();
488 BT_LIB_LOGW_APPEND_CAUSE(
489 "Cannot decode Python plugin version's extra string: "
490 "py-plugin-info-addr=%p", plugin_info
);
491 status
= BT_FUNC_STATUS_ERROR
;
494 "Cannot decode Python plugin version's extra string: "
495 "py-plugin-info-addr=%p", plugin_info
);
496 status
= BT_FUNC_STATUS_NOT_FOUND
;
505 *plugin_out
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
507 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin object.");
508 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
512 bt_plugin_set_name(*plugin_out
, name
);
515 bt_plugin_set_description(*plugin_out
, description
);
519 bt_plugin_set_author(*plugin_out
, author
);
523 bt_plugin_set_license(*plugin_out
, license
);
526 bt_plugin_set_version(*plugin_out
, major
, minor
, patch
, version_extra
);
528 if (PyList_Check(py_comp_class_addrs
)) {
531 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
532 bt_component_class
*comp_class
;
533 PyObject
*py_comp_class_addr
;
536 PyList_GetItem(py_comp_class_addrs
, i
);
537 BT_ASSERT(py_comp_class_addr
);
538 if (PyLong_Check(py_comp_class_addr
)) {
539 comp_class
= PyLong_AsVoidPtr(py_comp_class_addr
);
541 if (fail_on_load_error
) {
542 append_python_traceback_error_cause();
543 BT_LIB_LOGW_APPEND_CAUSE(
544 "Component class address is not an integer in Python plugin info object: "
545 "py-plugin-info-addr=%p, index=%zu",
547 status
= BT_FUNC_STATUS_ERROR
;
550 "Component class address is not an integer in Python plugin info object: "
551 "py-plugin-info-addr=%p, index=%zu",
553 status
= BT_FUNC_STATUS_NOT_FOUND
;
559 status
= bt_plugin_add_component_class(*plugin_out
,
562 BT_LIB_LOGE_APPEND_CAUSE(
563 "Cannot add component class to plugin: "
564 "py-plugin-info-addr=%p, "
565 "plugin-addr=%p, plugin-name=\"%s\", "
566 "comp-class-addr=%p, "
567 "comp-class-name=\"%s\", "
568 "comp-class-type=%s",
569 plugin_info
, *plugin_out
,
570 bt_plugin_get_name(*plugin_out
),
572 bt_component_class_get_name(comp_class
),
573 bt_common_component_class_type_string(
574 bt_component_class_get_type(comp_class
)));
583 BT_ASSERT(status
!= BT_FUNC_STATUS_OK
);
584 log_python_traceback(fail_on_load_error
? BT_LOG_WARNING
: BT_LOG_INFO
);
586 BT_OBJECT_PUT_REF_AND_RESET(*plugin_out
);
590 Py_XDECREF(py_author
);
591 Py_XDECREF(py_description
);
592 Py_XDECREF(py_license
);
593 Py_XDECREF(py_version
);
594 Py_XDECREF(py_comp_class_addrs
);
599 int bt_plugin_python_create_all_from_file(const char *path
,
600 bool fail_on_load_error
, struct bt_plugin_set
**plugin_set_out
)
602 bt_plugin
*plugin
= NULL
;
603 PyObject
*py_plugin_info
= NULL
;
604 gchar
*basename
= NULL
;
606 int status
= BT_FUNC_STATUS_OK
;
610 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
612 * We do not even care about the rest of the function
613 * here because we already know Python cannot be fully
616 BT_LIB_LOGE_APPEND_CAUSE(
617 "Python interpreter could not be initialized previously.");
618 status
= BT_FUNC_STATUS_ERROR
;
620 } else if (python_state
== PYTHON_STATE_WONT_INITIALIZE
) {
622 * This is not an error: the environment requires that
623 * Python plugins are disabled, so it's simply not
626 BT_LOGI_STR("Python plugin support was disabled previously "
627 "because the `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` "
628 "environment variable is set to `1`.");
629 status
= BT_FUNC_STATUS_NOT_FOUND
;
633 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
635 path_len
= strlen(path
);
637 /* File name ends with `.py` */
638 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
639 PYTHON_PLUGIN_FILE_EXT
,
640 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
641 BT_LOGI("Skipping non-Python file: path=\"%s\"", path
);
642 status
= BT_FUNC_STATUS_NOT_FOUND
;
646 /* File name starts with `bt_plugin_` */
647 basename
= g_path_get_basename(path
);
649 BT_LIB_LOGE_APPEND_CAUSE(
650 "Cannot get path's basename: path=\"%s\"", path
);
651 status
= BT_FUNC_STATUS_ERROR
;
655 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
656 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
657 BT_LOGI("Skipping Python file not starting with `%s`: "
658 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX
, path
);
659 status
= BT_FUNC_STATUS_NOT_FOUND
;
664 * Initialize Python now.
666 * This is not done in the library contructor because the
667 * interpreter is somewhat slow to initialize. If you don't
668 * have any potential Python plugins, you don't need to endure
669 * this waiting time everytime you load the library.
671 status
= init_python();
672 if (status
!= BT_FUNC_STATUS_OK
) {
673 /* init_python() logs and append errors */
678 * Call bt2.py_plugin._try_load_plugin_module() with this path
679 * to get plugin info if the plugin is loadable and complete.
680 * This function returns None when there's an error, but just in
681 * case we also manually clear the last Python error state.
683 BT_LOGD_STR("Getting Python plugin info object from Python module.");
684 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
686 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
687 if (fail_on_load_error
) {
688 append_python_traceback_error_cause();
689 BT_LIB_LOGW_APPEND_CAUSE(
690 "Cannot load Python plugin: path=\"%s\"", path
);
691 status
= BT_FUNC_STATUS_ERROR
;
694 "Cannot load Python plugin: path=\"%s\"", path
);
695 status
= BT_FUNC_STATUS_NOT_FOUND
;
702 * Get bt_plugin from plugin info object.
705 status
= bt_plugin_from_python_plugin_info(py_plugin_info
,
706 fail_on_load_error
, &plugin
);
709 * bt_plugin_from_python_plugin_info() handles
710 * `fail_on_load_error`, so this is a "real" error.
712 BT_LIB_LOGW_APPEND_CAUSE(
713 "Cannot create plugin object from Python plugin info object: "
714 "path=\"%s\", py-plugin-info-addr=%p",
715 path
, py_plugin_info
);
718 } else if (status
== BT_FUNC_STATUS_NOT_FOUND
) {
723 BT_ASSERT(status
== BT_FUNC_STATUS_OK
);
725 bt_plugin_set_path(plugin
, path
);
726 *plugin_set_out
= bt_plugin_set_create();
727 if (!*plugin_set_out
) {
728 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
729 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
733 bt_plugin_set_add_plugin(*plugin_set_out
, plugin
);
734 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
735 "plugin-addr=%p, plugin-name=\"%s\"",
736 path
, plugin
, bt_plugin_get_name(plugin
));
740 BT_ASSERT(status
!= BT_FUNC_STATUS_OK
);
741 log_python_traceback(BT_LOG_WARNING
);
743 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
746 bt_plugin_put_ref(plugin
);
747 Py_XDECREF(py_plugin_info
);