2 * python-plugin-provider.c
4 * Babeltrace Python plugin provider
6 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #define BT_LOG_TAG "LIB/PLUGIN-PY"
29 #include "lib/logging.h"
30 #include "common/macros.h"
31 #include "compat/compiler.h"
32 #include <babeltrace2/plugin/plugin-const.h>
33 #include "lib/plugin/plugin.h"
34 #include <babeltrace2/graph/component-class.h>
35 #include "lib/graph/component-class.h"
42 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
43 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
44 #define PYTHON_PLUGIN_FILE_EXT ".py"
45 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
48 /* init_python() not called yet */
49 PYTHON_STATE_NOT_INITED
,
51 /* init_python() called once with success */
52 PYTHON_STATE_FULLY_INITIALIZED
,
54 /* init_python() called once without success */
55 PYTHON_STATE_CANNOT_INITIALIZE
,
58 * init_python() called, but environment variable asks the
59 * Python interpreter not to be loaded.
61 PYTHON_STATE_WONT_INITIALIZE
,
62 } python_state
= PYTHON_STATE_NOT_INITED
;
64 static PyObject
*py_try_load_plugin_module_func
= NULL
;
65 static bool python_was_initialized_by_us
;
68 void print_python_traceback(int log_level
)
70 if (BT_LOG_ON(log_level
) && Py_IsInitialized() && PyErr_Occurred()) {
71 BT_LOG_WRITE(log_level
, BT_LOG_TAG
,
72 "Exception occured: Python traceback: ");
78 void pyerr_clear(void)
80 if (Py_IsInitialized()) {
86 void init_python(void)
88 PyObject
*py_bt2_py_plugin_mod
= NULL
;
89 const char *dis_python_env
;
91 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
94 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
99 * User can disable Python plugin support with the
100 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
103 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
104 if (dis_python_env
&& strcmp(dis_python_env
, "1") == 0) {
105 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
106 python_state
= PYTHON_STATE_WONT_INITIALIZE
;
110 if (!Py_IsInitialized()) {
111 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
113 python_was_initialized_by_us
= true;
114 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
117 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
121 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
122 if (!py_bt2_py_plugin_mod
) {
123 BT_LOGW_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
124 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
128 py_try_load_plugin_module_func
=
129 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
130 if (!py_try_load_plugin_module_func
) {
131 BT_LOGW_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
132 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
136 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
140 if (old_sigint
!= SIG_ERR
) {
141 (void) signal(SIGINT
, old_sigint
);
145 print_python_traceback(BT_LOG_WARN
);
147 Py_XDECREF(py_bt2_py_plugin_mod
);
151 __attribute__((destructor
)) static
152 void fini_python(void) {
153 if (Py_IsInitialized() && python_was_initialized_by_us
) {
154 if (py_try_load_plugin_module_func
) {
155 Py_DECREF(py_try_load_plugin_module_func
);
156 py_try_load_plugin_module_func
= NULL
;
160 BT_LOGI_STR("Finalized Python interpreter.");
163 python_state
= PYTHON_STATE_NOT_INITED
;
167 enum bt_plugin_status
bt_plugin_from_python_plugin_info(PyObject
*plugin_info
,
168 bool fail_on_load_error
, bt_plugin
**plugin_out
)
170 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
171 PyObject
*py_name
= NULL
;
172 PyObject
*py_author
= NULL
;
173 PyObject
*py_description
= NULL
;
174 PyObject
*py_license
= NULL
;
175 PyObject
*py_version
= NULL
;
176 PyObject
*py_comp_class_addrs
= NULL
;
177 const char *name
= NULL
;
178 const char *author
= NULL
;
179 const char *description
= NULL
;
180 const char *license
= NULL
;
181 unsigned int major
= 0, minor
= 0, patch
= 0;
182 const char *version_extra
= NULL
;
184 BT_ASSERT(plugin_out
);
186 BT_ASSERT(plugin_info
);
187 BT_ASSERT(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
188 py_name
= PyObject_GetAttrString(plugin_info
, "name");
190 BT_LOG_WRITE(fail_on_load_error
?
191 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
192 "Cannot find `name` attribute in Python plugin info object: "
193 "py-plugin-info-addr=%p", plugin_info
);
194 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
195 BT_PLUGIN_STATUS_NOT_FOUND
;
199 py_author
= PyObject_GetAttrString(plugin_info
, "author");
201 BT_LOG_WRITE(fail_on_load_error
?
202 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
203 "Cannot find `author` attribute in Python plugin info object: "
204 "py-plugin-info-addr=%p", plugin_info
);
205 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
206 BT_PLUGIN_STATUS_NOT_FOUND
;
210 py_description
= PyObject_GetAttrString(plugin_info
, "description");
211 if (!py_description
) {
212 BT_LOG_WRITE(fail_on_load_error
?
213 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
214 "Cannot find `desciption` attribute in Python plugin info object: "
215 "py-plugin-info-addr=%p", plugin_info
);
216 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
217 BT_PLUGIN_STATUS_NOT_FOUND
;
221 py_license
= PyObject_GetAttrString(plugin_info
, "license");
223 BT_LOG_WRITE(fail_on_load_error
?
224 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
225 "Cannot find `license` attribute in Python plugin info object: "
226 "py-plugin-info-addr=%p", plugin_info
);
227 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
228 BT_PLUGIN_STATUS_NOT_FOUND
;
232 py_version
= PyObject_GetAttrString(plugin_info
, "version");
234 BT_LOG_WRITE(fail_on_load_error
?
235 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
236 "Cannot find `version` attribute in Python plugin info object: "
237 "py-plugin-info-addr=%p", plugin_info
);
238 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
239 BT_PLUGIN_STATUS_NOT_FOUND
;
243 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
245 if (!py_comp_class_addrs
) {
246 BT_LOG_WRITE(fail_on_load_error
?
247 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
248 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
249 "py-plugin-info-addr=%p", plugin_info
);
250 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
251 BT_PLUGIN_STATUS_NOT_FOUND
;
255 if (PyUnicode_Check(py_name
)) {
256 name
= PyUnicode_AsUTF8(py_name
);
258 BT_LOG_WRITE(fail_on_load_error
?
259 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
260 "Cannot decode Python plugin name string: "
261 "py-plugin-info-addr=%p", plugin_info
);
262 status
= fail_on_load_error
?
263 BT_PLUGIN_STATUS_LOADING_ERROR
:
264 BT_PLUGIN_STATUS_NOT_FOUND
;
268 /* Plugin name is mandatory */
269 BT_LOG_WRITE(fail_on_load_error
?
270 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
271 "Plugin name is not a string: "
272 "py-plugin-info-addr=%p", plugin_info
);
273 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
274 BT_PLUGIN_STATUS_NOT_FOUND
;
278 if (PyUnicode_Check(py_author
)) {
279 author
= PyUnicode_AsUTF8(py_author
);
281 BT_LOG_WRITE(fail_on_load_error
?
282 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
283 "Cannot decode Python plugin author string: "
284 "py-plugin-info-addr=%p", plugin_info
);
285 status
= fail_on_load_error
?
286 BT_PLUGIN_STATUS_LOADING_ERROR
:
287 BT_PLUGIN_STATUS_NOT_FOUND
;
292 if (PyUnicode_Check(py_description
)) {
293 description
= PyUnicode_AsUTF8(py_description
);
295 BT_LOG_WRITE(fail_on_load_error
?
296 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
297 "Cannot decode Python plugin description string: "
298 "py-plugin-info-addr=%p", plugin_info
);
299 status
= fail_on_load_error
?
300 BT_PLUGIN_STATUS_LOADING_ERROR
:
301 BT_PLUGIN_STATUS_NOT_FOUND
;
306 if (PyUnicode_Check(py_license
)) {
307 license
= PyUnicode_AsUTF8(py_license
);
309 BT_LOG_WRITE(fail_on_load_error
?
310 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
311 "Cannot decode Python plugin license string: "
312 "py-plugin-info-addr=%p", plugin_info
);
313 status
= fail_on_load_error
?
314 BT_PLUGIN_STATUS_LOADING_ERROR
:
315 BT_PLUGIN_STATUS_NOT_FOUND
;
320 if (PyTuple_Check(py_version
)) {
321 if (PyTuple_Size(py_version
) >= 3) {
322 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
323 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
324 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
330 if (PyLong_Check(py_major
)) {
331 major
= PyLong_AsUnsignedLong(py_major
);
334 if (PyLong_Check(py_minor
)) {
335 minor
= PyLong_AsUnsignedLong(py_minor
);
338 if (PyLong_Check(py_patch
)) {
339 patch
= PyLong_AsUnsignedLong(py_patch
);
342 if (PyErr_Occurred()) {
343 /* Overflow error, most probably */
344 BT_LOG_WRITE(fail_on_load_error
?
345 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
346 "Invalid Python plugin version format: "
347 "py-plugin-info-addr=%p", plugin_info
);
348 status
= fail_on_load_error
?
349 BT_PLUGIN_STATUS_LOADING_ERROR
:
350 BT_PLUGIN_STATUS_NOT_FOUND
;
355 if (PyTuple_Size(py_version
) >= 4) {
356 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
360 if (PyUnicode_Check(py_extra
)) {
361 version_extra
= PyUnicode_AsUTF8(py_extra
);
362 if (!version_extra
) {
363 BT_LOG_WRITE(fail_on_load_error
?
364 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
365 "Cannot decode Python plugin version's extra string: "
366 "py-plugin-info-addr=%p", plugin_info
);
367 status
= fail_on_load_error
?
368 BT_PLUGIN_STATUS_LOADING_ERROR
:
369 BT_PLUGIN_STATUS_NOT_FOUND
;
376 *plugin_out
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
378 BT_LOGE_STR("Cannot create empty plugin object.");
379 status
= BT_PLUGIN_STATUS_NOMEM
;
383 bt_plugin_set_name(*plugin_out
, name
);
386 bt_plugin_set_description(*plugin_out
, description
);
390 bt_plugin_set_author(*plugin_out
, author
);
394 bt_plugin_set_license(*plugin_out
, license
);
397 bt_plugin_set_version(*plugin_out
, major
, minor
, patch
, version_extra
);
399 if (PyList_Check(py_comp_class_addrs
)) {
402 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
403 bt_component_class
*comp_class
;
404 PyObject
*py_comp_class_addr
;
407 PyList_GetItem(py_comp_class_addrs
, i
);
408 BT_ASSERT(py_comp_class_addr
);
409 if (PyLong_Check(py_comp_class_addr
)) {
410 comp_class
= PyLong_AsVoidPtr(py_comp_class_addr
);
412 BT_LOG_WRITE(fail_on_load_error
?
413 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
414 "Component class address is not an integer in Python plugin info object: "
415 "py-plugin-info-addr=%p, index=%zu",
418 if (fail_on_load_error
) {
419 status
= BT_PLUGIN_STATUS_LOADING_ERROR
;
426 status
= bt_plugin_add_component_class(*plugin_out
,
429 BT_LOGE("Cannot add component class to plugin: "
430 "py-plugin-info-addr=%p, "
431 "plugin-addr=%p, plugin-name=\"%s\", "
432 "comp-class-addr=%p, "
433 "comp-class-name=\"%s\", "
434 "comp-class-type=%s",
435 plugin_info
, *plugin_out
,
436 bt_plugin_get_name(*plugin_out
),
438 bt_component_class_get_name(comp_class
),
439 bt_component_class_type_string(
440 bt_component_class_get_type(comp_class
)));
449 BT_ASSERT(status
!= BT_PLUGIN_STATUS_OK
);
450 print_python_traceback(fail_on_load_error
? BT_LOG_WARN
: BT_LOG_INFO
);
452 BT_OBJECT_PUT_REF_AND_RESET(*plugin_out
);
456 Py_XDECREF(py_author
);
457 Py_XDECREF(py_description
);
458 Py_XDECREF(py_license
);
459 Py_XDECREF(py_version
);
460 Py_XDECREF(py_comp_class_addrs
);
465 enum bt_plugin_status
bt_plugin_python_create_all_from_file(const char *path
,
466 bool fail_on_load_error
, struct bt_plugin_set
**plugin_set_out
)
468 bt_plugin
*plugin
= NULL
;
469 PyObject
*py_plugin_info
= NULL
;
470 gchar
*basename
= NULL
;
472 enum bt_plugin_status status
= BT_PLUGIN_STATUS_OK
;
476 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
478 * We do not even care about the rest of the function
479 * here because we already know Python cannot be fully
482 status
= BT_PLUGIN_STATUS_ERROR
;
484 } else if (python_state
== PYTHON_STATE_WONT_INITIALIZE
) {
486 * This is not an error: the environment requires that
487 * Python plugins are disabled, so it's simply not
490 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
494 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
496 path_len
= strlen(path
);
498 /* File name ends with `.py` */
499 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
500 PYTHON_PLUGIN_FILE_EXT
,
501 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
502 BT_LOGI("Skipping non-Python file: path=\"%s\"", path
);
503 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
507 /* File name starts with `bt_plugin_` */
508 basename
= g_path_get_basename(path
);
510 BT_LOGE("Cannot get path's basename: path=\"%s\"", path
);
511 status
= BT_PLUGIN_STATUS_ERROR
;
515 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
516 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
517 BT_LOGI("Skipping Python file not starting with `%s`: "
518 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX
, path
);
519 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
524 * Initialize Python now.
526 * This is not done in the library contructor because the
527 * interpreter is somewhat slow to initialize. If you don't
528 * have any potential Python plugins, you don't need to endure
529 * this waiting time everytime you load the library.
532 if (python_state
== PYTHON_STATE_WONT_INITIALIZE
) {
534 * This is not an error: the environment requires that
535 * Python plugins are disabled, so it's simply not
538 status
= BT_PLUGIN_STATUS_NOT_FOUND
;
540 } else if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
542 * For some reason we cannot initialize Python,
543 * import the required modules, and get the required
544 * attributes from them.
546 BT_LOGE("Failed to initialize Python interpreter.");
547 status
= BT_PLUGIN_STATUS_ERROR
;
552 * Call bt2.py_plugin._try_load_plugin_module() with this path
553 * to get plugin info if the plugin is loadable and complete.
554 * This function returns None when there's an error, but just in
555 * case we also manually clear the last Python error state.
557 BT_LOGD_STR("Getting Python plugin info object from Python module.");
558 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
560 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
561 BT_LOG_WRITE(fail_on_load_error
?
562 BT_LOG_WARN
: BT_LOG_INFO
, BT_LOG_TAG
,
563 "Cannot load Python plugin: path=\"%s\"", path
);
564 print_python_traceback(fail_on_load_error
? BT_LOG_WARN
:
567 status
= fail_on_load_error
? BT_PLUGIN_STATUS_LOADING_ERROR
:
568 BT_PLUGIN_STATUS_NOT_FOUND
;
573 * Get bt_plugin from plugin info object.
576 status
= bt_plugin_from_python_plugin_info(py_plugin_info
,
577 fail_on_load_error
, &plugin
);
580 * bt_plugin_from_python_plugin_info() handles
581 * `fail_on_load_error`, so this is a "real" error.
583 BT_LOGW("Cannot create plugin object from Python plugin info object: "
584 "path=\"%s\", py-plugin-info-addr=%p",
585 path
, py_plugin_info
);
588 } else if (status
== BT_PLUGIN_STATUS_NOT_FOUND
) {
593 BT_ASSERT(status
== BT_PLUGIN_STATUS_OK
);
595 bt_plugin_set_path(plugin
, path
);
596 *plugin_set_out
= bt_plugin_set_create();
597 if (!*plugin_set_out
) {
598 BT_LOGE_STR("Cannot create empty plugin set.");
599 status
= BT_PLUGIN_STATUS_NOMEM
;
603 bt_plugin_set_add_plugin(*plugin_set_out
, plugin
);
604 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
605 "plugin-addr=%p, plugin-name=\"%s\"",
606 path
, plugin
, bt_plugin_get_name(plugin
));
610 BT_ASSERT(status
!= BT_PLUGIN_STATUS_OK
);
611 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out
);
614 bt_plugin_put_ref(plugin
);
615 Py_XDECREF(py_plugin_info
);