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 "PLUGIN-PY"
30 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/plugin/plugin-const.h>
33 #include <babeltrace/plugin/plugin-internal.h>
34 #include <babeltrace/graph/component-class.h>
35 #include <babeltrace/graph/component-class-internal.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
,
56 } python_state
= PYTHON_STATE_NOT_INITED
;
58 static PyObject
*py_try_load_plugin_module_func
= NULL
;
59 static bool python_was_initialized_by_us
;
62 void print_python_traceback_warn(void)
64 if (BT_LOG_ON_WARN
&& Py_IsInitialized() && PyErr_Occurred()) {
65 BT_LOGW_STR("Exception occured: traceback: ");
71 void pyerr_clear(void)
73 if (Py_IsInitialized()) {
79 void init_python(void)
81 PyObject
*py_bt2_py_plugin_mod
= NULL
;
82 const char *dis_python_env
;
84 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
87 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
92 * User can disable Python plugin support with the
93 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
96 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
97 if (dis_python_env
&& strcmp(dis_python_env
, "1") == 0) {
98 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
99 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
103 if (!Py_IsInitialized()) {
104 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
106 python_was_initialized_by_us
= true;
107 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
110 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
114 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
115 if (!py_bt2_py_plugin_mod
) {
116 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
117 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
121 py_try_load_plugin_module_func
=
122 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
123 if (!py_try_load_plugin_module_func
) {
124 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
125 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
129 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
133 if (old_sigint
!= SIG_ERR
) {
134 (void) signal(SIGINT
, old_sigint
);
138 print_python_traceback_warn();
140 Py_XDECREF(py_bt2_py_plugin_mod
);
144 __attribute__((destructor
)) static
145 void fini_python(void) {
146 if (Py_IsInitialized() && python_was_initialized_by_us
) {
147 if (py_try_load_plugin_module_func
) {
148 Py_DECREF(py_try_load_plugin_module_func
);
149 py_try_load_plugin_module_func
= NULL
;
153 BT_LOGI_STR("Finalized Python interpreter.");
156 python_state
= PYTHON_STATE_NOT_INITED
;
160 const bt_plugin
*bt_plugin_from_python_plugin_info(PyObject
*plugin_info
)
162 const bt_plugin
*plugin
= NULL
;
163 PyObject
*py_name
= NULL
;
164 PyObject
*py_author
= NULL
;
165 PyObject
*py_description
= NULL
;
166 PyObject
*py_license
= NULL
;
167 PyObject
*py_version
= NULL
;
168 PyObject
*py_comp_class_addrs
= NULL
;
169 const char *name
= NULL
;
170 const char *author
= NULL
;
171 const char *description
= NULL
;
172 const char *license
= NULL
;
173 unsigned int major
= 0, minor
= 0, patch
= 0;
174 const char *version_extra
= NULL
;
177 BT_ASSERT(plugin_info
);
178 BT_ASSERT(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
179 py_name
= PyObject_GetAttrString(plugin_info
, "name");
181 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
182 "py-plugin-info-addr=%p", plugin_info
);
186 py_author
= PyObject_GetAttrString(plugin_info
, "author");
188 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
189 "py-plugin-info-addr=%p", plugin_info
);
193 py_description
= PyObject_GetAttrString(plugin_info
, "description");
194 if (!py_description
) {
195 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
196 "py-plugin-info-addr=%p", plugin_info
);
200 py_license
= PyObject_GetAttrString(plugin_info
, "license");
202 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
203 "py-plugin-info-addr=%p", plugin_info
);
207 py_version
= PyObject_GetAttrString(plugin_info
, "version");
209 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
210 "py-plugin-info-addr=%p", plugin_info
);
214 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
216 if (!py_comp_class_addrs
) {
217 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
218 "py-plugin-info-addr=%p", plugin_info
);
222 if (PyUnicode_Check(py_name
)) {
223 name
= PyUnicode_AsUTF8(py_name
);
225 BT_LOGW("Cannot decode Python plugin name string: "
226 "py-plugin-info-addr=%p", plugin_info
);
230 /* Plugin name is mandatory */
231 BT_LOGW("Plugin name is not a string: "
232 "py-plugin-info-addr=%p", plugin_info
);
236 if (PyUnicode_Check(py_author
)) {
237 author
= PyUnicode_AsUTF8(py_author
);
239 BT_LOGW("Cannot decode Python plugin author string: "
240 "py-plugin-info-addr=%p", plugin_info
);
245 if (PyUnicode_Check(py_description
)) {
246 description
= PyUnicode_AsUTF8(py_description
);
248 BT_LOGW("Cannot decode Python plugin description string: "
249 "py-plugin-info-addr=%p", plugin_info
);
254 if (PyUnicode_Check(py_license
)) {
255 license
= PyUnicode_AsUTF8(py_license
);
257 BT_LOGW("Cannot decode Python plugin license string: "
258 "py-plugin-info-addr=%p", plugin_info
);
263 if (PyTuple_Check(py_version
)) {
264 if (PyTuple_Size(py_version
) >= 3) {
265 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
266 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
267 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
273 if (PyLong_Check(py_major
)) {
274 major
= PyLong_AsUnsignedLong(py_major
);
277 if (PyLong_Check(py_minor
)) {
278 minor
= PyLong_AsUnsignedLong(py_minor
);
281 if (PyLong_Check(py_patch
)) {
282 patch
= PyLong_AsUnsignedLong(py_patch
);
285 if (PyErr_Occurred()) {
286 /* Overflow error, most probably */
287 BT_LOGW("Invalid Python plugin version format: "
288 "py-plugin-info-addr=%p", plugin_info
);
293 if (PyTuple_Size(py_version
) >= 4) {
294 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
298 if (PyUnicode_Check(py_extra
)) {
299 version_extra
= PyUnicode_AsUTF8(py_extra
);
300 if (!version_extra
) {
301 BT_LOGW("Cannot decode Python plugin version's extra string: "
302 "py-plugin-info-addr=%p", plugin_info
);
309 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
311 BT_LOGE_STR("Cannot create empty plugin object.");
315 bt_plugin_set_name(plugin
, name
);
318 bt_plugin_set_description(plugin
, description
);
322 bt_plugin_set_author(plugin
, author
);
326 bt_plugin_set_license(plugin
, license
);
329 bt_plugin_set_version(plugin
, major
, minor
, patch
, version_extra
);
331 if (PyList_Check(py_comp_class_addrs
)) {
334 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
335 bt_component_class
*comp_class
;
336 PyObject
*py_comp_class_addr
;
339 PyList_GetItem(py_comp_class_addrs
, i
);
340 BT_ASSERT(py_comp_class_addr
);
341 if (PyLong_Check(py_comp_class_addr
)) {
342 comp_class
= (bt_component_class
*)
343 PyLong_AsUnsignedLongLong(py_comp_class_addr
);
345 BT_LOGW("Component class address is not an integer in Python plugin info object: "
346 "py-plugin-info-addr=%p, index=%zu",
351 ret
= bt_plugin_add_component_class(plugin
, comp_class
);
353 BT_LOGE("Cannot add component class to plugin: "
354 "py-plugin-info-addr=%p, "
355 "plugin-addr=%p, plugin-name=\"%s\", "
356 "comp-class-addr=%p, "
357 "comp-class-name=\"%s\", "
358 "comp-class-type=%s",
360 plugin
, bt_plugin_get_name(plugin
),
362 bt_component_class_get_name(comp_class
),
363 bt_component_class_type_string(
364 bt_component_class_get_type(comp_class
)));
370 bt_plugin_freeze(plugin
);
375 print_python_traceback_warn();
377 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
381 Py_XDECREF(py_author
);
382 Py_XDECREF(py_description
);
383 Py_XDECREF(py_license
);
384 Py_XDECREF(py_version
);
385 Py_XDECREF(py_comp_class_addrs
);
390 bt_plugin_set
*bt_plugin_python_create_all_from_file(const char *path
)
392 bt_plugin_set
*plugin_set
= NULL
;
393 const bt_plugin
*plugin
= NULL
;
394 PyObject
*py_plugin_info
= NULL
;
395 gchar
*basename
= NULL
;
400 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
402 * We do not even care about the rest of the function
403 * here because we already know Python cannot be fully
409 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path
);
410 path_len
= strlen(path
);
412 /* File name ends with `.py` */
413 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
414 PYTHON_PLUGIN_FILE_EXT
,
415 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
416 BT_LOGD("Skipping non-Python file: path=\"%s\"", path
);
420 /* File name starts with `bt_plugin_` */
421 basename
= g_path_get_basename(path
);
423 BT_LOGW("Cannot get path's basename: path=\"%s\"", path
);
427 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
428 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
429 BT_LOGD("Skipping Python file not starting with `%s`: "
430 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX
, path
);
435 * Initialize Python now.
437 * This is not done in the library contructor because the
438 * interpreter is somewhat slow to initialize. If you don't
439 * have any potential Python plugins, you don't need to endure
440 * this waiting time everytime you load the library.
443 if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
445 * For some reason we cannot initialize Python,
446 * import the required modules, and get the required
447 * attributes from them.
449 BT_LOGI("Failed to initialize Python interpreter.");
454 * Call bt2.py_plugin._try_load_plugin_module() with this path
455 * to get plugin info if the plugin is loadable and complete.
456 * This function returns None when there's an error, but just in
457 * case we also manually clear the last Python error state.
459 BT_LOGD_STR("Getting Python plugin info object from Python module.");
460 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
462 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
463 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path
);
464 print_python_traceback_warn();
470 * Get bt_plugin from plugin info object.
472 plugin
= bt_plugin_from_python_plugin_info(py_plugin_info
);
474 BT_LOGW("Cannot create plugin object from Python plugin info object: "
475 "path=\"%s\", py-plugin-info-addr=%p",
476 path
, py_plugin_info
);
480 bt_plugin_set_path(plugin
, path
);
481 plugin_set
= bt_plugin_set_create();
483 BT_LOGE_STR("Cannot create empty plugin set.");
487 bt_plugin_set_add_plugin(plugin_set
, plugin
);
488 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
489 "plugin-addr=%p, plugin-name=\"%s\"",
490 path
, plugin
, bt_plugin_get_name(plugin
));
494 BT_OBJECT_PUT_REF_AND_RESET(plugin_set
);
497 bt_plugin_put_ref(plugin
);
498 Py_XDECREF(py_plugin_info
);