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/ref.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/plugin/plugin-internal.h>
35 #include <babeltrace/graph/component-class.h>
36 #include <babeltrace/graph/component-class-internal.h>
43 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
44 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
45 #define PYTHON_PLUGIN_FILE_EXT ".py"
46 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
49 /* init_python() not called yet */
50 PYTHON_STATE_NOT_INITED
,
52 /* init_python() called once with success */
53 PYTHON_STATE_FULLY_INITIALIZED
,
55 /* init_python() called once without success */
56 PYTHON_STATE_CANNOT_INITIALIZE
,
57 } python_state
= PYTHON_STATE_NOT_INITED
;
59 static PyObject
*py_try_load_plugin_module_func
= NULL
;
60 static bool python_was_initialized_by_us
;
63 void print_python_traceback_warn(void)
65 if (BT_LOG_ON_WARN
&& Py_IsInitialized() && PyErr_Occurred()) {
66 BT_LOGW_STR("Exception occured: traceback: ");
72 void pyerr_clear(void)
74 if (Py_IsInitialized()) {
80 void init_python(void)
82 PyObject
*py_bt2_py_plugin_mod
= NULL
;
83 const char *dis_python_env
;
85 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
88 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
93 * User can disable Python plugin support with the
94 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
97 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
98 if (dis_python_env
&& strcmp(dis_python_env
, "1") == 0) {
99 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
100 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
104 if (!Py_IsInitialized()) {
105 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
107 python_was_initialized_by_us
= true;
108 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
111 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
115 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
116 if (!py_bt2_py_plugin_mod
) {
117 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
118 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
122 py_try_load_plugin_module_func
=
123 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
124 if (!py_try_load_plugin_module_func
) {
125 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
126 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
130 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
134 if (old_sigint
!= SIG_ERR
) {
135 (void) signal(SIGINT
, old_sigint
);
139 print_python_traceback_warn();
141 Py_XDECREF(py_bt2_py_plugin_mod
);
145 __attribute__((destructor
)) static
146 void fini_python(void) {
147 if (Py_IsInitialized() && python_was_initialized_by_us
) {
148 if (py_try_load_plugin_module_func
) {
149 Py_DECREF(py_try_load_plugin_module_func
);
150 py_try_load_plugin_module_func
= NULL
;
154 BT_LOGI_STR("Finalized Python interpreter.");
157 python_state
= PYTHON_STATE_NOT_INITED
;
161 struct bt_plugin
*bt_plugin_from_python_plugin_info(PyObject
*plugin_info
)
163 struct bt_plugin
*plugin
= NULL
;
164 PyObject
*py_name
= NULL
;
165 PyObject
*py_author
= NULL
;
166 PyObject
*py_description
= NULL
;
167 PyObject
*py_license
= NULL
;
168 PyObject
*py_version
= NULL
;
169 PyObject
*py_comp_class_addrs
= NULL
;
170 const char *name
= NULL
;
171 const char *author
= NULL
;
172 const char *description
= NULL
;
173 const char *license
= NULL
;
174 unsigned int major
= 0, minor
= 0, patch
= 0;
175 const char *version_extra
= NULL
;
179 assert(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
180 py_name
= PyObject_GetAttrString(plugin_info
, "name");
182 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
183 "py-plugin-info-addr=%p", plugin_info
);
187 py_author
= PyObject_GetAttrString(plugin_info
, "author");
189 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
190 "py-plugin-info-addr=%p", plugin_info
);
194 py_description
= PyObject_GetAttrString(plugin_info
, "description");
195 if (!py_description
) {
196 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
197 "py-plugin-info-addr=%p", plugin_info
);
201 py_license
= PyObject_GetAttrString(plugin_info
, "license");
203 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
204 "py-plugin-info-addr=%p", plugin_info
);
208 py_version
= PyObject_GetAttrString(plugin_info
, "version");
210 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
211 "py-plugin-info-addr=%p", plugin_info
);
215 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
217 if (!py_comp_class_addrs
) {
218 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
219 "py-plugin-info-addr=%p", plugin_info
);
223 if (PyUnicode_Check(py_name
)) {
224 name
= PyUnicode_AsUTF8(py_name
);
226 BT_LOGW("Cannot decode Python plugin name string: "
227 "py-plugin-info-addr=%p", plugin_info
);
231 /* Plugin name is mandatory */
232 BT_LOGW("Plugin name is not a string: "
233 "py-plugin-info-addr=%p", plugin_info
);
237 if (PyUnicode_Check(py_author
)) {
238 author
= PyUnicode_AsUTF8(py_author
);
240 BT_LOGW("Cannot decode Python plugin author string: "
241 "py-plugin-info-addr=%p", plugin_info
);
246 if (PyUnicode_Check(py_description
)) {
247 description
= PyUnicode_AsUTF8(py_description
);
249 BT_LOGW("Cannot decode Python plugin description string: "
250 "py-plugin-info-addr=%p", plugin_info
);
255 if (PyUnicode_Check(py_license
)) {
256 license
= PyUnicode_AsUTF8(py_license
);
258 BT_LOGW("Cannot decode Python plugin license string: "
259 "py-plugin-info-addr=%p", plugin_info
);
264 if (PyTuple_Check(py_version
)) {
265 if (PyTuple_Size(py_version
) >= 3) {
266 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
267 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
268 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
274 if (PyLong_Check(py_major
)) {
275 major
= PyLong_AsUnsignedLong(py_major
);
278 if (PyLong_Check(py_minor
)) {
279 minor
= PyLong_AsUnsignedLong(py_minor
);
282 if (PyLong_Check(py_patch
)) {
283 patch
= PyLong_AsUnsignedLong(py_patch
);
286 if (PyErr_Occurred()) {
287 /* Overflow error, most probably */
288 BT_LOGW("Invalid Python plugin version format: "
289 "py-plugin-info-addr=%p", plugin_info
);
294 if (PyTuple_Size(py_version
) >= 4) {
295 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
299 if (PyUnicode_Check(py_extra
)) {
300 version_extra
= PyUnicode_AsUTF8(py_extra
);
301 if (!version_extra
) {
302 BT_LOGW("Cannot decode Python plugin version's extra string: "
303 "py-plugin-info-addr=%p", plugin_info
);
310 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
312 BT_LOGE_STR("Cannot create empty plugin object.");
316 bt_plugin_set_name(plugin
, name
);
319 bt_plugin_set_description(plugin
, description
);
323 bt_plugin_set_author(plugin
, author
);
327 bt_plugin_set_license(plugin
, license
);
330 bt_plugin_set_version(plugin
, major
, minor
, patch
, version_extra
);
332 if (PyList_Check(py_comp_class_addrs
)) {
335 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
336 struct bt_component_class
*comp_class
;
337 PyObject
*py_comp_class_addr
;
340 PyList_GetItem(py_comp_class_addrs
, i
);
341 assert(py_comp_class_addr
);
342 if (PyLong_Check(py_comp_class_addr
)) {
343 comp_class
= (struct bt_component_class
*)
344 PyLong_AsUnsignedLongLong(py_comp_class_addr
);
346 BT_LOGW("Component class address is not an integer in Python plugin info object: "
347 "py-plugin-info-addr=%p, index=%zu",
352 ret
= bt_plugin_add_component_class(plugin
, comp_class
);
354 BT_LOGE("Cannot add component class to plugin: "
355 "py-plugin-info-addr=%p, "
356 "plugin-addr=%p, plugin-name=\"%s\", "
357 "comp-class-addr=%p, "
358 "comp-class-name=\"%s\", "
359 "comp-class-type=%s",
361 plugin
, bt_plugin_get_name(plugin
),
363 bt_component_class_get_name(comp_class
),
364 bt_component_class_type_string(
365 bt_component_class_get_type(comp_class
)));
371 bt_plugin_freeze(plugin
);
376 print_python_traceback_warn();
382 Py_XDECREF(py_author
);
383 Py_XDECREF(py_description
);
384 Py_XDECREF(py_license
);
385 Py_XDECREF(py_version
);
386 Py_XDECREF(py_comp_class_addrs
);
391 struct bt_plugin_set
*bt_plugin_python_create_all_from_file(const char *path
)
393 struct bt_plugin_set
*plugin_set
= NULL
;
394 struct bt_plugin
*plugin
= NULL
;
395 PyObject
*py_plugin_info
= NULL
;
396 gchar
*basename
= NULL
;
401 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
403 * We do not even care about the rest of the function
404 * here because we already know Python cannot be fully
410 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path
);
411 path_len
= strlen(path
);
413 /* File name ends with `.py` */
414 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
415 PYTHON_PLUGIN_FILE_EXT
,
416 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
417 BT_LOGD("Skipping non-Python file: path=\"%s\"", path
);
421 /* File name starts with `bt_plugin_` */
422 basename
= g_path_get_basename(path
);
424 BT_LOGW("Cannot get path's basename: path=\"%s\"", path
);
428 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
429 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
430 BT_LOGD("Skipping Python file not starting with `%s`: "
431 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX
, path
);
436 * Initialize Python now.
438 * This is not done in the library contructor because the
439 * interpreter is somewhat slow to initialize. If you don't
440 * have any potential Python plugins, you don't need to endure
441 * this waiting time everytime you load the library.
444 if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
446 * For some reason we cannot initialize Python,
447 * import the required modules, and get the required
448 * attributes from them.
450 BT_LOGI("Failed to initialize Python interpreter.");
455 * Call bt2.py_plugin._try_load_plugin_module() with this path
456 * to get plugin info if the plugin is loadable and complete.
457 * This function returns None when there's an error, but just in
458 * case we also manually clear the last Python error state.
460 BT_LOGD_STR("Getting Python plugin info object from Python module.");
461 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
463 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
464 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path
);
465 print_python_traceback_warn();
471 * Get bt_plugin from plugin info object.
473 plugin
= bt_plugin_from_python_plugin_info(py_plugin_info
);
475 BT_LOGW("Cannot create plugin object from Python plugin info object: "
476 "path=\"%s\", py-plugin-info-addr=%p",
477 path
, py_plugin_info
);
481 bt_plugin_set_path(plugin
, path
);
482 plugin_set
= bt_plugin_set_create();
484 BT_LOGE_STR("Cannot create empty plugin set.");
488 bt_plugin_set_add_plugin(plugin_set
, plugin
);
489 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
490 "plugin-addr=%p, plugin-name=\"%s\"",
491 path
, plugin
, bt_plugin_get_name(plugin
));
499 Py_XDECREF(py_plugin_info
);