f4747ef7e6972769abae94c6341ba4d4a3a1c8ae
[babeltrace.git] / python-plugin-provider / python-plugin-provider.c
1 /*
2 * python-plugin-provider.c
3 *
4 * Babeltrace Python plugin provider
5 *
6 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_TAG "PLUGIN-PY"
28 #include "logging.h"
29
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>
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <Python.h>
39 #include <glib.h>
40 #include <gmodule.h>
41
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)
46
47 enum python_state {
48 /* init_python() not called yet */
49 PYTHON_STATE_NOT_INITED,
50
51 /* init_python() called once with success */
52 PYTHON_STATE_FULLY_INITIALIZED,
53
54 /* init_python() called once without success */
55 PYTHON_STATE_CANNOT_INITIALIZE,
56 } python_state = PYTHON_STATE_NOT_INITED;
57
58 static PyObject *py_try_load_plugin_module_func = NULL;
59 static bool python_was_initialized_by_us;
60
61 static
62 void print_python_traceback_warn(void)
63 {
64 if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) {
65 BT_LOGW_STR("Exception occured: traceback: ");
66 PyErr_Print();
67 }
68 }
69
70 static
71 void pyerr_clear(void)
72 {
73 if (Py_IsInitialized()) {
74 PyErr_Clear();
75 }
76 }
77
78 static
79 void init_python(void)
80 {
81 PyObject *py_bt2_py_plugin_mod = NULL;
82 const char *dis_python_env;
83 #ifndef __MINGW32__
84 sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
85 #endif
86
87 if (python_state != PYTHON_STATE_NOT_INITED) {
88 goto end;
89 }
90
91 /*
92 * User can disable Python plugin support with the
93 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
94 * 1.
95 */
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;
100 goto end;
101 }
102
103 if (!Py_IsInitialized()) {
104 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
105 Py_InitializeEx(0);
106 python_was_initialized_by_us = true;
107 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
108 Py_GetVersion());
109 } else {
110 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
111 Py_GetVersion());
112 }
113
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;
118 goto end;
119 }
120
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;
126 goto end;
127 }
128
129 python_state = PYTHON_STATE_FULLY_INITIALIZED;
130
131 end:
132 #ifndef __MINGW32__
133 if (old_sigint != SIG_ERR) {
134 (void) signal(SIGINT, old_sigint);
135 }
136 #endif
137
138 print_python_traceback_warn();
139 pyerr_clear();
140 Py_XDECREF(py_bt2_py_plugin_mod);
141 return;
142 }
143
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;
150 }
151
152 Py_Finalize();
153 BT_LOGI_STR("Finalized Python interpreter.");
154 }
155
156 python_state = PYTHON_STATE_NOT_INITED;
157 }
158
159 static
160 const bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
161 {
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;
175 int ret;
176
177 BT_ASSERT(plugin_info);
178 BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
179 py_name = PyObject_GetAttrString(plugin_info, "name");
180 if (!py_name) {
181 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
182 "py-plugin-info-addr=%p", plugin_info);
183 goto error;
184 }
185
186 py_author = PyObject_GetAttrString(plugin_info, "author");
187 if (!py_author) {
188 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
189 "py-plugin-info-addr=%p", plugin_info);
190 goto error;
191 }
192
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);
197 goto error;
198 }
199
200 py_license = PyObject_GetAttrString(plugin_info, "license");
201 if (!py_license) {
202 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
203 "py-plugin-info-addr=%p", plugin_info);
204 goto error;
205 }
206
207 py_version = PyObject_GetAttrString(plugin_info, "version");
208 if (!py_version) {
209 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
210 "py-plugin-info-addr=%p", plugin_info);
211 goto error;
212 }
213
214 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
215 "comp_class_addrs");
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);
219 goto error;
220 }
221
222 if (PyUnicode_Check(py_name)) {
223 name = PyUnicode_AsUTF8(py_name);
224 if (!name) {
225 BT_LOGW("Cannot decode Python plugin name string: "
226 "py-plugin-info-addr=%p", plugin_info);
227 goto error;
228 }
229 } else {
230 /* Plugin name is mandatory */
231 BT_LOGW("Plugin name is not a string: "
232 "py-plugin-info-addr=%p", plugin_info);
233 goto error;
234 }
235
236 if (PyUnicode_Check(py_author)) {
237 author = PyUnicode_AsUTF8(py_author);
238 if (!author) {
239 BT_LOGW("Cannot decode Python plugin author string: "
240 "py-plugin-info-addr=%p", plugin_info);
241 goto error;
242 }
243 }
244
245 if (PyUnicode_Check(py_description)) {
246 description = PyUnicode_AsUTF8(py_description);
247 if (!description) {
248 BT_LOGW("Cannot decode Python plugin description string: "
249 "py-plugin-info-addr=%p", plugin_info);
250 goto error;
251 }
252 }
253
254 if (PyUnicode_Check(py_license)) {
255 license = PyUnicode_AsUTF8(py_license);
256 if (!license) {
257 BT_LOGW("Cannot decode Python plugin license string: "
258 "py-plugin-info-addr=%p", plugin_info);
259 goto error;
260 }
261 }
262
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);
268
269 BT_ASSERT(py_major);
270 BT_ASSERT(py_minor);
271 BT_ASSERT(py_patch);
272
273 if (PyLong_Check(py_major)) {
274 major = PyLong_AsUnsignedLong(py_major);
275 }
276
277 if (PyLong_Check(py_minor)) {
278 minor = PyLong_AsUnsignedLong(py_minor);
279 }
280
281 if (PyLong_Check(py_patch)) {
282 patch = PyLong_AsUnsignedLong(py_patch);
283 }
284
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);
289 goto error;
290 }
291 }
292
293 if (PyTuple_Size(py_version) >= 4) {
294 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
295
296 BT_ASSERT(py_extra);
297
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);
303 goto error;
304 }
305 }
306 }
307 }
308
309 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
310 if (!plugin) {
311 BT_LOGE_STR("Cannot create empty plugin object.");
312 goto error;
313 }
314
315 bt_plugin_set_name(plugin, name);
316
317 if (description) {
318 bt_plugin_set_description(plugin, description);
319 }
320
321 if (author) {
322 bt_plugin_set_author(plugin, author);
323 }
324
325 if (license) {
326 bt_plugin_set_license(plugin, license);
327 }
328
329 bt_plugin_set_version(plugin, major, minor, patch, version_extra);
330
331 if (PyList_Check(py_comp_class_addrs)) {
332 size_t i;
333
334 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
335 bt_component_class *comp_class;
336 PyObject *py_comp_class_addr;
337
338 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);
344 } else {
345 BT_LOGW("Component class address is not an integer in Python plugin info object: "
346 "py-plugin-info-addr=%p, index=%zu",
347 plugin_info, i);
348 continue;
349 }
350
351 ret = bt_plugin_add_component_class(plugin, comp_class);
352 if (ret < 0) {
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",
359 plugin_info,
360 plugin, bt_plugin_get_name(plugin),
361 comp_class,
362 bt_component_class_get_name(comp_class),
363 bt_component_class_type_string(
364 bt_component_class_get_type(comp_class)));
365 continue;
366 }
367 }
368 }
369
370 bt_plugin_freeze(plugin);
371
372 goto end;
373
374 error:
375 print_python_traceback_warn();
376 pyerr_clear();
377 BT_OBJECT_PUT_REF_AND_RESET(plugin);
378
379 end:
380 Py_XDECREF(py_name);
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);
386 return plugin;
387 }
388
389 G_MODULE_EXPORT
390 bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
391 {
392 bt_plugin_set *plugin_set = NULL;
393 const bt_plugin *plugin = NULL;
394 PyObject *py_plugin_info = NULL;
395 gchar *basename = NULL;
396 size_t path_len;
397
398 BT_ASSERT(path);
399
400 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
401 /*
402 * We do not even care about the rest of the function
403 * here because we already know Python cannot be fully
404 * initialized.
405 */
406 goto error;
407 }
408
409 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path);
410 path_len = strlen(path);
411
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);
417 goto error;
418 }
419
420 /* File name starts with `bt_plugin_` */
421 basename = g_path_get_basename(path);
422 if (!basename) {
423 BT_LOGW("Cannot get path's basename: path=\"%s\"", path);
424 goto error;
425 }
426
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);
431 goto error;
432 }
433
434 /*
435 * Initialize Python now.
436 *
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.
441 */
442 init_python();
443 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
444 /*
445 * For some reason we cannot initialize Python,
446 * import the required modules, and get the required
447 * attributes from them.
448 */
449 BT_LOGI("Failed to initialize Python interpreter.");
450 goto error;
451 }
452
453 /*
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.
458 */
459 BT_LOGD_STR("Getting Python plugin info object from Python module.");
460 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
461 "(s)", path);
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();
465 PyErr_Clear();
466 goto error;
467 }
468
469 /*
470 * Get bt_plugin from plugin info object.
471 */
472 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
473 if (!plugin) {
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);
477 goto error;
478 }
479
480 bt_plugin_set_path(plugin, path);
481 plugin_set = bt_plugin_set_create();
482 if (!plugin_set) {
483 BT_LOGE_STR("Cannot create empty plugin set.");
484 goto error;
485 }
486
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));
491 goto end;
492
493 error:
494 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
495
496 end:
497 bt_plugin_put_ref(plugin);
498 Py_XDECREF(py_plugin_info);
499 g_free(basename);
500 return plugin_set;
501 }
This page took 0.038377 seconds and 3 git commands to generate.