4 * Babeltrace Plugin (Python)
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 #include <babeltrace/babeltrace-internal.h>
28 #include <babeltrace/compiler.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/plugin/plugin-internal.h>
31 #include <babeltrace/plugin/plugin-internal.h>
32 #include <babeltrace/component/component-class-internal.h>
37 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
38 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
39 #define PYTHON_PLUGIN_FILE_EXT ".py"
40 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
43 /* init_python() not called yet */
44 PYTHON_STATE_NOT_INITED
,
46 /* init_python() called once with success */
47 PYTHON_STATE_FULLY_INITIALIZED
,
49 /* init_python() called once without success */
50 PYTHON_STATE_CANNOT_INITIALIZE
,
51 } python_state
= PYTHON_STATE_NOT_INITED
;
53 static PyObject
*py_try_load_plugin_module_func
= NULL
;
56 void print_python_traceback_verbose(void)
58 if (Py_IsInitialized() && PyErr_Occurred() && babeltrace_verbose
) {
64 void pyerr_clear(void)
66 if (Py_IsInitialized()) {
72 void init_python(void)
74 PyObject
*py_bt2_py_plugin_mod
= NULL
;
75 const char *dis_python_env
;
76 sighandler_t old_sigint
= signal(SIGINT
, SIG_DFL
);
78 if (python_state
!= PYTHON_STATE_NOT_INITED
) {
83 * User can disable Python plugin support with the
84 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
87 dis_python_env
= getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
88 if (dis_python_env
&& dis_python_env
[0] == '1' &&
89 dis_python_env
[1] == '\0') {
90 printf_verbose("Python plugin support is disabled by BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable\n");
91 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
95 if (!Py_IsInitialized()) {
97 printf_verbose("Initialized Python:\n%s\n", Py_GetVersion());
100 py_bt2_py_plugin_mod
= PyImport_ImportModule("bt2.py_plugin");
101 if (!py_bt2_py_plugin_mod
) {
102 printf_verbose("Cannot import bt2.py_plugin Python module\n");
103 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
107 py_try_load_plugin_module_func
=
108 PyObject_GetAttrString(py_bt2_py_plugin_mod
, "_try_load_plugin_module");
109 if (!py_try_load_plugin_module_func
) {
110 printf_verbose("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module\n");
111 python_state
= PYTHON_STATE_CANNOT_INITIALIZE
;
115 python_state
= PYTHON_STATE_FULLY_INITIALIZED
;
118 if (old_sigint
!= SIG_ERR
) {
119 (void) signal(SIGINT
, old_sigint
);
122 print_python_traceback_verbose();
124 Py_XDECREF(py_bt2_py_plugin_mod
);
128 __attribute__((destructor
)) static
129 void fini_python(void) {
130 if (Py_IsInitialized()) {
131 if (py_try_load_plugin_module_func
) {
132 Py_DECREF(py_try_load_plugin_module_func
);
133 py_try_load_plugin_module_func
= NULL
;
139 python_state
= PYTHON_STATE_NOT_INITED
;
143 struct bt_plugin
*bt_plugin_from_python_plugin_info(PyObject
*plugin_info
)
145 struct bt_plugin
*plugin
= NULL
;
146 PyObject
*py_name
= NULL
;
147 PyObject
*py_author
= NULL
;
148 PyObject
*py_description
= NULL
;
149 PyObject
*py_license
= NULL
;
150 PyObject
*py_version
= NULL
;
151 PyObject
*py_comp_class_addrs
= NULL
;
152 const char *name
= NULL
;
153 const char *author
= NULL
;
154 const char *description
= NULL
;
155 const char *license
= NULL
;
156 unsigned int major
= 0, minor
= 0, patch
= 0;
157 const char *version_extra
= NULL
;
161 assert(python_state
== PYTHON_STATE_FULLY_INITIALIZED
);
162 py_name
= PyObject_GetAttrString(plugin_info
, "name");
164 printf_verbose("Cannot find `name` attribute in plugin info\n");
168 py_author
= PyObject_GetAttrString(plugin_info
, "author");
170 printf_verbose("Cannot find `author` attribute in plugin info\n");
174 py_description
= PyObject_GetAttrString(plugin_info
, "description");
175 if (!py_description
) {
176 printf_verbose("Cannot find `description` attribute in plugin info\n");
180 py_license
= PyObject_GetAttrString(plugin_info
, "license");
182 printf_verbose("Cannot find `license` attribute in plugin info\n");
186 py_version
= PyObject_GetAttrString(plugin_info
, "version");
188 printf_verbose("Cannot find `version` attribute in plugin info\n");
192 py_comp_class_addrs
= PyObject_GetAttrString(plugin_info
,
194 if (!py_comp_class_addrs
) {
195 printf_verbose("Cannot find `comp_class_addrs` attribute in plugin info\n");
199 if (PyUnicode_Check(py_name
)) {
200 name
= PyUnicode_AsUTF8(py_name
);
202 printf_verbose("Cannot decode plugin name string\n");
206 /* Plugin name is mandatory */
207 printf_verbose("Plugin name is not a string\n");
211 if (PyUnicode_Check(py_author
)) {
212 author
= PyUnicode_AsUTF8(py_author
);
214 printf_verbose("Cannot decode plugin author string\n");
219 if (PyUnicode_Check(py_description
)) {
220 description
= PyUnicode_AsUTF8(py_description
);
222 printf_verbose("Cannot decode plugin description string\n");
227 if (PyUnicode_Check(py_license
)) {
228 license
= PyUnicode_AsUTF8(py_license
);
230 printf_verbose("Cannot decode plugin license string\n");
235 if (PyTuple_Check(py_version
)) {
236 if (PyTuple_Size(py_version
) >= 3) {
237 PyObject
*py_major
= PyTuple_GetItem(py_version
, 0);
238 PyObject
*py_minor
= PyTuple_GetItem(py_version
, 1);
239 PyObject
*py_patch
= PyTuple_GetItem(py_version
, 2);
245 if (PyLong_Check(py_major
)) {
246 major
= PyLong_AsUnsignedLong(py_major
);
249 if (PyLong_Check(py_minor
)) {
250 minor
= PyLong_AsUnsignedLong(py_minor
);
253 if (PyLong_Check(py_patch
)) {
254 patch
= PyLong_AsUnsignedLong(py_patch
);
257 if (PyErr_Occurred()) {
258 /* Overflow error, most probably */
259 printf_verbose("Invalid plugin version format\n");
264 if (PyTuple_Size(py_version
) >= 4) {
265 PyObject
*py_extra
= PyTuple_GetItem(py_version
, 3);
269 if (PyUnicode_Check(py_extra
)) {
270 version_extra
= PyUnicode_AsUTF8(py_extra
);
271 if (!version_extra
) {
272 printf_verbose("Cannot decode plugin version's extra string\n");
279 plugin
= bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON
);
284 bt_plugin_set_name(plugin
, name
);
287 bt_plugin_set_description(plugin
, description
);
291 bt_plugin_set_author(plugin
, author
);
295 bt_plugin_set_license(plugin
, license
);
298 bt_plugin_set_version(plugin
, major
, minor
, patch
, version_extra
);
300 if (PyList_Check(py_comp_class_addrs
)) {
303 for (i
= 0; i
< PyList_Size(py_comp_class_addrs
); i
++) {
304 struct bt_component_class
*comp_class
;
305 PyObject
*py_comp_class_addr
;
308 PyList_GetItem(py_comp_class_addrs
, i
);
309 assert(py_comp_class_addr
);
310 if (PyLong_Check(py_comp_class_addr
)) {
311 comp_class
= (struct bt_component_class
*)
312 PyLong_AsUnsignedLongLong(py_comp_class_addr
);
314 printf_verbose("Component class address #%zu: not an integer\n",
319 ret
= bt_plugin_add_component_class(plugin
, comp_class
);
321 printf_verbose("Cannot add component class #%zu\n",
328 bt_plugin_freeze(plugin
);
333 print_python_traceback_verbose();
339 Py_XDECREF(py_author
);
340 Py_XDECREF(py_description
);
341 Py_XDECREF(py_license
);
342 Py_XDECREF(py_version
);
343 Py_XDECREF(py_comp_class_addrs
);
348 struct bt_plugin
**bt_plugin_python_create_all_from_file(const char *path
)
350 struct bt_plugin
**plugins
= NULL
;
351 PyObject
*py_plugin_info
= NULL
;
352 gchar
*basename
= NULL
;
357 if (python_state
== PYTHON_STATE_CANNOT_INITIALIZE
) {
359 * We do not even care about the rest of the function
360 * here because we already know Python cannot be fully
366 path_len
= strlen(path
);
368 /* File name ends with `.py` */
369 if (strncmp(path
+ path_len
- PYTHON_PLUGIN_FILE_EXT_LEN
,
370 PYTHON_PLUGIN_FILE_EXT
,
371 PYTHON_PLUGIN_FILE_EXT_LEN
) != 0) {
372 printf_verbose("Skipping non-Python file: `%s`\n",
377 /* File name starts with `bt_plugin_` */
378 basename
= g_path_get_basename(path
);
383 if (strncmp(basename
, PYTHON_PLUGIN_FILE_PREFIX
,
384 PYTHON_PLUGIN_FILE_PREFIX_LEN
) != 0) {
385 printf_verbose("Skipping Python file not starting with `%s`: `%s`\n",
386 PYTHON_PLUGIN_FILE_PREFIX
, path
);
391 * Initialize Python now.
393 * This is not done in the library contructor because the
394 * interpreter is somewhat slow to initialize. If you don't
395 * have any potential Python plugins, you don't need to endure
396 * this waiting time everytime you load the library.
399 if (python_state
!= PYTHON_STATE_FULLY_INITIALIZED
) {
401 * For some reason we cannot initialize Python,
402 * import the required modules, and get the required
403 * attributes from them.
409 * Call bt2.py_plugin._try_load_plugin_module() with this path
410 * to get plugin info if the plugin is loadable and complete.
411 * This function returns None when there's an error, but just in
412 * case we also manually clear the last Python error state.
414 py_plugin_info
= PyObject_CallFunction(py_try_load_plugin_module_func
,
416 if (!py_plugin_info
|| py_plugin_info
== Py_None
) {
417 printf_verbose("Cannot load Python plugin `%s`:\n", path
);
418 print_python_traceback_verbose();
424 * Get bt_plugin from plugin info object.
426 * calloc(2, ...) because a single Python plugin file always
427 * provides a single Babeltrace plugin (second item is the
430 plugins
= calloc(2, sizeof(*plugins
));
435 plugins
[0] = bt_plugin_from_python_plugin_info(py_plugin_info
);
440 bt_plugin_set_path(plugins
[0], path
);
451 Py_XDECREF(py_plugin_info
);