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"
28 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/babeltrace-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/plugin/plugin-internal.h>
40 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
41 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
42 #define PYTHON_PLUGIN_FILE_EXT ".py"
43 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
46 /* init_python() not called yet */
47 PYTHON_STATE_NOT_INITED
,
49 /* init_python() called once with success */
50 PYTHON_STATE_FULLY_INITIALIZED
,
52 /* init_python() called once without success */
53 PYTHON_STATE_CANNOT_INITIALIZE
,
54 } python_state
= PYTHON_STATE_NOT_INITED
;
56 static PyObject
*py_try_load_plugin_module_func
= NULL
;
59 void print_python_traceback_verbose(void)
61 if (Py_IsInitialized() && PyErr_Occurred() && babeltrace_verbose
) {
67 void pyerr_clear(void)
69 if (Py_IsInitialized()) {
75 void init_python(void)
77 PyObject
*py_bt2_py_plugin_mod
= NULL
;
78 const char *dis_python_env
;
79 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
81 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
86 * User can disable Python plugin support with the
87 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
90 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
91 if (dis_python_env
&& dis_python_env
[0] == '1' &&
92 dis_python_env
[1] == '\0') {
93 printf_verbose("Python plugin support is disabled by BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable\n");
94 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
98 if (!Py_IsInitialized()) {
100 printf_verbose("Initialized Python:\n%s\n", Py_GetVersion());
103 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
104 if (!py_bt2_py_plugin_mod
) {
105 printf_verbose("Cannot import bt2.py_plugin Python module\n");
106 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
110 py_try_load_plugin_module_func
=
111 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
112 if (!py_try_load_plugin_module_func
) {
113 printf_verbose("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module\n");
114 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
118 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
121 if (old_sigint
!= SIG_ERR
) {
122 (void) signal(SIGINT
, old_sigint
);
125 print_python_traceback_verbose();
127 Py_XDECREF(py_bt2_py_plugin_mod
);
131 __attribute__((destructor
)) static
132 void fini_python(void) {
133 if (Py_IsInitialized()) {
134 if (py_try_load_plugin_module_func
) {
135 Py_DECREF(py_try_load_plugin_module_func
);
136 py_try_load_plugin_module_func
= NULL
;
142 python_state
= PYTHON_STATE_NOT_INITED
;
146 struct bt_plugin
*bt_plugin_from_python_plugin_info(PyObject
*plugin_info
)
148 struct bt_plugin
*plugin
= NULL
;
149 PyObject
*py_name
= NULL
;
150 PyObject
*py_author
= NULL
;
151 PyObject
*py_description
= NULL
;
152 PyObject
*py_license
= NULL
;
153 PyObject
*py_version
= NULL
;
154 PyObject
*py_comp_class_addrs
= NULL
;
155 const char *name
= NULL
;
156 const char *author
= NULL
;
157 const char *description
= NULL
;
158 const char *license
= NULL
;
159 unsigned int major
= 0, minor
= 0, patch
= 0;
160 const char *version_extra
= NULL
;
164 assert(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
165 py_name
= PyObject_GetAttrString(plugin_info
, "name");
167 printf_verbose("Cannot find `name` attribute in plugin info\n");
171 py_author
= PyObject_GetAttrString(plugin_info
, "author");
173 printf_verbose("Cannot find `author` attribute in plugin info\n");
177 py_description
= PyObject_GetAttrString(plugin_info
, "description");
178 if (!py_description
) {
179 printf_verbose("Cannot find `description` attribute in plugin info\n");
183 py_license
= PyObject_GetAttrString(plugin_info
, "license");
185 printf_verbose("Cannot find `license` attribute in plugin info\n");
189 py_version
= PyObject_GetAttrString(plugin_info
, "version");
191 printf_verbose("Cannot find `version` attribute in plugin info\n");
195 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
197 if (!py_comp_class_addrs
) {
198 printf_verbose("Cannot find `comp_class_addrs` attribute in plugin info\n");
202 if (PyUnicode_Check(py_name
)) {
203 name
= PyUnicode_AsUTF8(py_name
);
205 printf_verbose("Cannot decode plugin name string\n");
209 /* Plugin name is mandatory */
210 printf_verbose("Plugin name is not a string\n");
214 if (PyUnicode_Check(py_author
)) {
215 author
= PyUnicode_AsUTF8(py_author
);
217 printf_verbose("Cannot decode plugin author string\n");
222 if (PyUnicode_Check(py_description
)) {
223 description
= PyUnicode_AsUTF8(py_description
);
225 printf_verbose("Cannot decode plugin description string\n");
230 if (PyUnicode_Check(py_license
)) {
231 license
= PyUnicode_AsUTF8(py_license
);
233 printf_verbose("Cannot decode plugin license string\n");
238 if (PyTuple_Check(py_version
)) {
239 if (PyTuple_Size(py_version
) >= 3) {
240 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
241 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
242 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
248 if (PyLong_Check(py_major
)) {
249 major
= PyLong_AsUnsignedLong(py_major
);
252 if (PyLong_Check(py_minor
)) {
253 minor
= PyLong_AsUnsignedLong(py_minor
);
256 if (PyLong_Check(py_patch
)) {
257 patch
= PyLong_AsUnsignedLong(py_patch
);
260 if (PyErr_Occurred()) {
261 /* Overflow error, most probably */
262 printf_verbose("Invalid plugin version format\n");
267 if (PyTuple_Size(py_version
) >= 4) {
268 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
272 if (PyUnicode_Check(py_extra
)) {
273 version_extra
= PyUnicode_AsUTF8(py_extra
);
274 if (!version_extra
) {
275 printf_verbose("Cannot decode plugin version's extra string\n");
282 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
287 bt_plugin_set_name(plugin
, name
);
290 bt_plugin_set_description(plugin
, description
);
294 bt_plugin_set_author(plugin
, author
);
298 bt_plugin_set_license(plugin
, license
);
301 bt_plugin_set_version(plugin
, major
, minor
, patch
, version_extra
);
303 if (PyList_Check(py_comp_class_addrs
)) {
306 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
307 struct bt_component_class
*comp_class
;
308 PyObject
*py_comp_class_addr
;
311 PyList_GetItem(py_comp_class_addrs
, i
);
312 assert(py_comp_class_addr
);
313 if (PyLong_Check(py_comp_class_addr
)) {
314 comp_class
= (struct bt_component_class
*)
315 PyLong_AsUnsignedLongLong(py_comp_class_addr
);
317 printf_verbose("Component class address #%zu: not an integer\n",
322 ret
= bt_plugin_add_component_class(plugin
, comp_class
);
324 printf_verbose("Cannot add component class #%zu\n",
331 bt_plugin_freeze(plugin
);
336 print_python_traceback_verbose();
342 Py_XDECREF(py_author
);
343 Py_XDECREF(py_description
);
344 Py_XDECREF(py_license
);
345 Py_XDECREF(py_version
);
346 Py_XDECREF(py_comp_class_addrs
);
351 struct bt_plugin_set
*bt_plugin_python_create_all_from_file(const char *path
)
353 struct bt_plugin_set
*plugin_set
= NULL
;
354 struct bt_plugin
*plugin
= NULL
;
355 PyObject
*py_plugin_info
= NULL
;
356 gchar
*basename
= NULL
;
361 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
363 * We do not even care about the rest of the function
364 * here because we already know Python cannot be fully
370 path_len
= strlen(path
);
372 /* File name ends with `.py` */
373 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
374 PYTHON_PLUGIN_FILE_EXT
,
375 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
376 printf_verbose("Skipping non-Python file: `%s`\n",
381 /* File name starts with `bt_plugin_` */
382 basename
= g_path_get_basename(path
);
387 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
388 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
389 printf_verbose("Skipping Python file not starting with `%s`: `%s`\n",
390 PYTHON_PLUGIN_FILE_PREFIX
, path
);
395 * Initialize Python now.
397 * This is not done in the library contructor because the
398 * interpreter is somewhat slow to initialize. If you don't
399 * have any potential Python plugins, you don't need to endure
400 * this waiting time everytime you load the library.
403 if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
405 * For some reason we cannot initialize Python,
406 * import the required modules, and get the required
407 * attributes from them.
413 * Call bt2.py_plugin._try_load_plugin_module() with this path
414 * to get plugin info if the plugin is loadable and complete.
415 * This function returns None when there's an error, but just in
416 * case we also manually clear the last Python error state.
418 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
420 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
421 printf_verbose("Cannot load Python plugin `%s`:\n", path
);
422 print_python_traceback_verbose();
428 * Get bt_plugin from plugin info object.
430 plugin
= bt_plugin_from_python_plugin_info(py_plugin_info
);
435 bt_plugin_set_path(plugin
, path
);
436 plugin_set
= bt_plugin_set_create();
441 bt_plugin_set_add_plugin(plugin_set
, plugin
);
449 Py_XDECREF(py_plugin_info
);