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_OUTPUT_LEVEL python_plugin_provider_log_level
28 #define BT_LOG_TAG "PLUGIN-PY"
29 #include <babeltrace/logging-internal.h>
32 * Must be before the rest because some of the headers below could
33 * contain logging statements which need this symbol when expanded.
35 static int python_plugin_provider_log_level
= BT_LOG_NONE
;
37 #include <babeltrace/babeltrace-internal.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/ref.h>
40 #include <babeltrace/plugin/plugin.h>
41 #include <babeltrace/plugin/plugin-internal.h>
42 #include <babeltrace/graph/component-class.h>
43 #include <babeltrace/graph/component-class-internal.h>
50 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
51 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
52 #define PYTHON_PLUGIN_FILE_EXT ".py"
53 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
56 /* init_python() not called yet */
57 PYTHON_STATE_NOT_INITED
,
59 /* init_python() called once with success */
60 PYTHON_STATE_FULLY_INITIALIZED
,
62 /* init_python() called once without success */
63 PYTHON_STATE_CANNOT_INITIALIZE
,
64 } python_state
= PYTHON_STATE_NOT_INITED
;
66 static PyObject
*py_try_load_plugin_module_func
= NULL
;
69 void __attribute__((constructor
)) logging_ctor(void)
71 python_plugin_provider_log_level
=
72 bt_log_get_level_from_env("BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL");
76 void print_python_traceback_warn(void)
78 if (BT_LOG_ON_WARN
&& Py_IsInitialized() && PyErr_Occurred()) {
79 BT_LOGW_STR("Exception occured: traceback: ");
85 void pyerr_clear(void)
87 if (Py_IsInitialized()) {
93 void init_python(void)
95 PyObject
*py_bt2_py_plugin_mod
= NULL
;
96 const char *dis_python_env
;
97 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
99 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
104 * User can disable Python plugin support with the
105 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
108 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
109 if (dis_python_env
&& strcmp(dis_python_env
, "1") == 0) {
110 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
111 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
115 if (!Py_IsInitialized()) {
117 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
121 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
122 if (!py_bt2_py_plugin_mod
) {
123 BT_LOGI_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_LOGI_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
;
139 if (old_sigint
!= SIG_ERR
) {
140 (void) signal(SIGINT
, old_sigint
);
143 print_python_traceback_warn();
145 Py_XDECREF(py_bt2_py_plugin_mod
);
149 __attribute__((destructor
)) static
150 void fini_python(void) {
151 if (Py_IsInitialized()) {
152 if (py_try_load_plugin_module_func
) {
153 Py_DECREF(py_try_load_plugin_module_func
);
154 py_try_load_plugin_module_func
= NULL
;
158 BT_LOGI_STR("Finalized Python interpreter.");
161 python_state
= PYTHON_STATE_NOT_INITED
;
165 struct bt_plugin
*bt_plugin_from_python_plugin_info(PyObject
*plugin_info
)
167 struct bt_plugin
*plugin
= NULL
;
168 PyObject
*py_name
= NULL
;
169 PyObject
*py_author
= NULL
;
170 PyObject
*py_description
= NULL
;
171 PyObject
*py_license
= NULL
;
172 PyObject
*py_version
= NULL
;
173 PyObject
*py_comp_class_addrs
= NULL
;
174 const char *name
= NULL
;
175 const char *author
= NULL
;
176 const char *description
= NULL
;
177 const char *license
= NULL
;
178 unsigned int major
= 0, minor
= 0, patch
= 0;
179 const char *version_extra
= NULL
;
183 assert(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
184 py_name
= PyObject_GetAttrString(plugin_info
, "name");
186 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
187 "py-plugin-info-addr=%p", plugin_info
);
191 py_author
= PyObject_GetAttrString(plugin_info
, "author");
193 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
194 "py-plugin-info-addr=%p", plugin_info
);
198 py_description
= PyObject_GetAttrString(plugin_info
, "description");
199 if (!py_description
) {
200 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
201 "py-plugin-info-addr=%p", plugin_info
);
205 py_license
= PyObject_GetAttrString(plugin_info
, "license");
207 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
208 "py-plugin-info-addr=%p", plugin_info
);
212 py_version
= PyObject_GetAttrString(plugin_info
, "version");
214 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
215 "py-plugin-info-addr=%p", plugin_info
);
219 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
221 if (!py_comp_class_addrs
) {
222 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
223 "py-plugin-info-addr=%p", plugin_info
);
227 if (PyUnicode_Check(py_name
)) {
228 name
= PyUnicode_AsUTF8(py_name
);
230 BT_LOGW("Cannot decode Python plugin name string: "
231 "py-plugin-info-addr=%p", plugin_info
);
235 /* Plugin name is mandatory */
236 BT_LOGW("Plugin name is not a string: "
237 "py-plugin-info-addr=%p", plugin_info
);
241 if (PyUnicode_Check(py_author
)) {
242 author
= PyUnicode_AsUTF8(py_author
);
244 BT_LOGW("Cannot decode Python plugin author string: "
245 "py-plugin-info-addr=%p", plugin_info
);
250 if (PyUnicode_Check(py_description
)) {
251 description
= PyUnicode_AsUTF8(py_description
);
253 BT_LOGW("Cannot decode Python plugin description string: "
254 "py-plugin-info-addr=%p", plugin_info
);
259 if (PyUnicode_Check(py_license
)) {
260 license
= PyUnicode_AsUTF8(py_license
);
262 BT_LOGW("Cannot decode Python plugin license string: "
263 "py-plugin-info-addr=%p", plugin_info
);
268 if (PyTuple_Check(py_version
)) {
269 if (PyTuple_Size(py_version
) >= 3) {
270 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
271 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
272 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
278 if (PyLong_Check(py_major
)) {
279 major
= PyLong_AsUnsignedLong(py_major
);
282 if (PyLong_Check(py_minor
)) {
283 minor
= PyLong_AsUnsignedLong(py_minor
);
286 if (PyLong_Check(py_patch
)) {
287 patch
= PyLong_AsUnsignedLong(py_patch
);
290 if (PyErr_Occurred()) {
291 /* Overflow error, most probably */
292 BT_LOGW("Invalid Python plugin version format: "
293 "py-plugin-info-addr=%p", plugin_info
);
298 if (PyTuple_Size(py_version
) >= 4) {
299 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
303 if (PyUnicode_Check(py_extra
)) {
304 version_extra
= PyUnicode_AsUTF8(py_extra
);
305 if (!version_extra
) {
306 BT_LOGW("Cannot decode Python plugin version's extra string: "
307 "py-plugin-info-addr=%p", plugin_info
);
314 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
316 BT_LOGE_STR("Cannot create empty plugin object.");
320 bt_plugin_set_name(plugin
, name
);
323 bt_plugin_set_description(plugin
, description
);
327 bt_plugin_set_author(plugin
, author
);
331 bt_plugin_set_license(plugin
, license
);
334 bt_plugin_set_version(plugin
, major
, minor
, patch
, version_extra
);
336 if (PyList_Check(py_comp_class_addrs
)) {
339 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
340 struct bt_component_class
*comp_class
;
341 PyObject
*py_comp_class_addr
;
344 PyList_GetItem(py_comp_class_addrs
, i
);
345 assert(py_comp_class_addr
);
346 if (PyLong_Check(py_comp_class_addr
)) {
347 comp_class
= (struct bt_component_class
*)
348 PyLong_AsUnsignedLongLong(py_comp_class_addr
);
350 BT_LOGW("Component class address is not an integer in Python plugin info object: "
351 "py-plugin-info-addr=%p, index=%zu",
356 ret
= bt_plugin_add_component_class(plugin
, comp_class
);
358 BT_LOGE("Cannot add component class to plugin: "
359 "py-plugin-info-addr=%p, "
360 "plugin-addr=%p, plugin-name=\"%s\", "
361 "comp-class-addr=%p, "
362 "comp-class-name=\"%s\", "
363 "comp-class-type=%s",
365 plugin
, bt_plugin_get_name(plugin
),
367 bt_component_class_get_name(comp_class
),
368 bt_component_class_type_string(
369 bt_component_class_get_type(comp_class
)));
375 bt_plugin_freeze(plugin
);
380 print_python_traceback_warn();
386 Py_XDECREF(py_author
);
387 Py_XDECREF(py_description
);
388 Py_XDECREF(py_license
);
389 Py_XDECREF(py_version
);
390 Py_XDECREF(py_comp_class_addrs
);
395 struct bt_plugin_set
*bt_plugin_python_create_all_from_file(const char *path
)
397 struct bt_plugin_set
*plugin_set
= NULL
;
398 struct bt_plugin
*plugin
= NULL
;
399 PyObject
*py_plugin_info
= NULL
;
400 gchar
*basename
= NULL
;
405 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
407 * We do not even care about the rest of the function
408 * here because we already know Python cannot be fully
414 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path
);
415 path_len
= strlen(path
);
417 /* File name ends with `.py` */
418 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
419 PYTHON_PLUGIN_FILE_EXT
,
420 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
421 BT_LOGD("Skipping non-Python file: path=\"%s\"", path
);
425 /* File name starts with `bt_plugin_` */
426 basename
= g_path_get_basename(path
);
428 BT_LOGW("Cannot get path's basename: path=\"%s\"", path
);
432 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
433 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
434 BT_LOGD("Skipping Python file not starting with `%s`: "
435 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX
, path
);
440 * Initialize Python now.
442 * This is not done in the library contructor because the
443 * interpreter is somewhat slow to initialize. If you don't
444 * have any potential Python plugins, you don't need to endure
445 * this waiting time everytime you load the library.
448 if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
450 * For some reason we cannot initialize Python,
451 * import the required modules, and get the required
452 * attributes from them.
454 BT_LOGI("Failed to initialize Python interpreter.");
459 * Call bt2.py_plugin._try_load_plugin_module() with this path
460 * to get plugin info if the plugin is loadable and complete.
461 * This function returns None when there's an error, but just in
462 * case we also manually clear the last Python error state.
464 BT_LOGD_STR("Getting Python plugin info object from Python module.");
465 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
467 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
468 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path
);
469 print_python_traceback_warn();
475 * Get bt_plugin from plugin info object.
477 plugin
= bt_plugin_from_python_plugin_info(py_plugin_info
);
479 BT_LOGW("Cannot create plugin object from Python plugin info object: "
480 "path=\"%s\", py-plugin-info-addr=%p",
481 path
, py_plugin_info
);
485 bt_plugin_set_path(plugin
, path
);
486 plugin_set
= bt_plugin_set_create();
488 BT_LOGE_STR("Cannot create empty plugin set.");
492 bt_plugin_set_add_plugin(plugin_set
, plugin
);
493 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
494 "plugin-addr=%p, plugin-name=\"%s\"",
495 path
, plugin
, bt_plugin_get_name(plugin
));
503 Py_XDECREF(py_plugin_info
);