Fix: common: improve color support handling
[babeltrace.git] / python-plugin-provider / python-plugin-provider.c
CommitLineData
55bb57e0 1/*
6fbd4105 2 * python-plugin-provider.c
55bb57e0 3 *
6fbd4105 4 * Babeltrace Python plugin provider
55bb57e0
PP
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
3fe0bf43 27#define BT_LOG_TAG "PLUGIN-PY"
86f21ef3 28#include "logging.h"
3fe0bf43 29
55bb57e0 30#include <babeltrace/babeltrace-internal.h>
3d9990ac 31#include <babeltrace/compiler-internal.h>
55bb57e0 32#include <babeltrace/ref.h>
9e0bf9b0 33#include <babeltrace/plugin/plugin.h>
55bb57e0 34#include <babeltrace/plugin/plugin-internal.h>
9e0bf9b0
PP
35#include <babeltrace/graph/component-class.h>
36#include <babeltrace/graph/component-class-internal.h>
55bb57e0
PP
37#include <stdlib.h>
38#include <signal.h>
39#include <Python.h>
6fbd4105
PP
40#include <glib.h>
41#include <gmodule.h>
55bb57e0
PP
42
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)
47
48enum python_state {
49 /* init_python() not called yet */
50 PYTHON_STATE_NOT_INITED,
51
52 /* init_python() called once with success */
53 PYTHON_STATE_FULLY_INITIALIZED,
54
55 /* init_python() called once without success */
56 PYTHON_STATE_CANNOT_INITIALIZE,
57} python_state = PYTHON_STATE_NOT_INITED;
58
59static PyObject *py_try_load_plugin_module_func = NULL;
60
61static
9e0bf9b0 62void print_python_traceback_warn(void)
55bb57e0 63{
9e0bf9b0
PP
64 if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) {
65 BT_LOGW_STR("Exception occured: traceback: ");
55bb57e0
PP
66 PyErr_Print();
67 }
68}
69
70static
71void pyerr_clear(void)
72{
73 if (Py_IsInitialized()) {
74 PyErr_Clear();
75 }
76}
77
78static
79void init_python(void)
80{
81 PyObject *py_bt2_py_plugin_mod = NULL;
82 const char *dis_python_env;
83 sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
84
85 if (python_state != PYTHON_STATE_NOT_INITED) {
86 goto end;
87 }
88
89 /*
90 * User can disable Python plugin support with the
91 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
92 * 1.
93 */
94 dis_python_env = getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
9e0bf9b0
PP
95 if (dis_python_env && strcmp(dis_python_env, "1") == 0) {
96 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
55bb57e0
PP
97 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
98 goto end;
99 }
100
101 if (!Py_IsInitialized()) {
13e4a3e3 102 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
55bb57e0 103 Py_InitializeEx(0);
9e0bf9b0
PP
104 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
105 Py_GetVersion());
13e4a3e3
PP
106 } else {
107 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
108 Py_GetVersion());
55bb57e0
PP
109 }
110
111 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
112 if (!py_bt2_py_plugin_mod) {
9e0bf9b0 113 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
55bb57e0
PP
114 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
115 goto end;
116 }
117
118 py_try_load_plugin_module_func =
119 PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module");
120 if (!py_try_load_plugin_module_func) {
9e0bf9b0 121 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
55bb57e0
PP
122 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
123 goto end;
124 }
125
126 python_state = PYTHON_STATE_FULLY_INITIALIZED;
127
128end:
129 if (old_sigint != SIG_ERR) {
130 (void) signal(SIGINT, old_sigint);
131 }
132
9e0bf9b0 133 print_python_traceback_warn();
55bb57e0
PP
134 pyerr_clear();
135 Py_XDECREF(py_bt2_py_plugin_mod);
136 return;
137}
138
139__attribute__((destructor)) static
140void fini_python(void) {
141 if (Py_IsInitialized()) {
142 if (py_try_load_plugin_module_func) {
143 Py_DECREF(py_try_load_plugin_module_func);
144 py_try_load_plugin_module_func = NULL;
145 }
146
147 Py_Finalize();
9e0bf9b0 148 BT_LOGI_STR("Finalized Python interpreter.");
55bb57e0
PP
149 }
150
151 python_state = PYTHON_STATE_NOT_INITED;
152}
153
154static
155struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
156{
157 struct bt_plugin *plugin = NULL;
158 PyObject *py_name = NULL;
159 PyObject *py_author = NULL;
160 PyObject *py_description = NULL;
161 PyObject *py_license = NULL;
162 PyObject *py_version = NULL;
163 PyObject *py_comp_class_addrs = NULL;
164 const char *name = NULL;
165 const char *author = NULL;
166 const char *description = NULL;
167 const char *license = NULL;
168 unsigned int major = 0, minor = 0, patch = 0;
169 const char *version_extra = NULL;
170 int ret;
171
172 assert(plugin_info);
173 assert(python_state == PYTHON_STATE_FULLY_INITIALIZED);
174 py_name = PyObject_GetAttrString(plugin_info, "name");
175 if (!py_name) {
9e0bf9b0
PP
176 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
177 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
178 goto error;
179 }
180
181 py_author = PyObject_GetAttrString(plugin_info, "author");
182 if (!py_author) {
9e0bf9b0
PP
183 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
184 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
185 goto error;
186 }
187
188 py_description = PyObject_GetAttrString(plugin_info, "description");
189 if (!py_description) {
9e0bf9b0
PP
190 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
191 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
192 goto error;
193 }
194
195 py_license = PyObject_GetAttrString(plugin_info, "license");
196 if (!py_license) {
9e0bf9b0
PP
197 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
198 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
199 goto error;
200 }
201
202 py_version = PyObject_GetAttrString(plugin_info, "version");
203 if (!py_version) {
9e0bf9b0
PP
204 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
205 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
206 goto error;
207 }
208
209 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
210 "comp_class_addrs");
211 if (!py_comp_class_addrs) {
9e0bf9b0
PP
212 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
213 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
214 goto error;
215 }
216
217 if (PyUnicode_Check(py_name)) {
218 name = PyUnicode_AsUTF8(py_name);
219 if (!name) {
9e0bf9b0
PP
220 BT_LOGW("Cannot decode Python plugin name string: "
221 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
222 goto error;
223 }
224 } else {
225 /* Plugin name is mandatory */
9e0bf9b0
PP
226 BT_LOGW("Plugin name is not a string: "
227 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
228 goto error;
229 }
230
231 if (PyUnicode_Check(py_author)) {
232 author = PyUnicode_AsUTF8(py_author);
233 if (!author) {
9e0bf9b0
PP
234 BT_LOGW("Cannot decode Python plugin author string: "
235 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
236 goto error;
237 }
238 }
239
240 if (PyUnicode_Check(py_description)) {
241 description = PyUnicode_AsUTF8(py_description);
242 if (!description) {
9e0bf9b0
PP
243 BT_LOGW("Cannot decode Python plugin description string: "
244 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
245 goto error;
246 }
247 }
248
249 if (PyUnicode_Check(py_license)) {
250 license = PyUnicode_AsUTF8(py_license);
251 if (!license) {
9e0bf9b0
PP
252 BT_LOGW("Cannot decode Python plugin license string: "
253 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
254 goto error;
255 }
256 }
257
258 if (PyTuple_Check(py_version)) {
259 if (PyTuple_Size(py_version) >= 3) {
260 PyObject *py_major = PyTuple_GetItem(py_version, 0);
261 PyObject *py_minor = PyTuple_GetItem(py_version, 1);
262 PyObject *py_patch = PyTuple_GetItem(py_version, 2);
263
264 assert(py_major);
265 assert(py_minor);
266 assert(py_patch);
267
268 if (PyLong_Check(py_major)) {
269 major = PyLong_AsUnsignedLong(py_major);
270 }
271
272 if (PyLong_Check(py_minor)) {
273 minor = PyLong_AsUnsignedLong(py_minor);
274 }
275
276 if (PyLong_Check(py_patch)) {
277 patch = PyLong_AsUnsignedLong(py_patch);
278 }
279
280 if (PyErr_Occurred()) {
281 /* Overflow error, most probably */
9e0bf9b0
PP
282 BT_LOGW("Invalid Python plugin version format: "
283 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
284 goto error;
285 }
286 }
287
288 if (PyTuple_Size(py_version) >= 4) {
289 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
290
291 assert(py_extra);
292
293 if (PyUnicode_Check(py_extra)) {
294 version_extra = PyUnicode_AsUTF8(py_extra);
295 if (!version_extra) {
9e0bf9b0
PP
296 BT_LOGW("Cannot decode Python plugin version's extra string: "
297 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
298 goto error;
299 }
300 }
301 }
302 }
303
304 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
305 if (!plugin) {
9e0bf9b0 306 BT_LOGE_STR("Cannot create empty plugin object.");
55bb57e0
PP
307 goto error;
308 }
309
310 bt_plugin_set_name(plugin, name);
311
312 if (description) {
313 bt_plugin_set_description(plugin, description);
314 }
315
316 if (author) {
317 bt_plugin_set_author(plugin, author);
318 }
319
320 if (license) {
321 bt_plugin_set_license(plugin, license);
322 }
323
324 bt_plugin_set_version(plugin, major, minor, patch, version_extra);
325
326 if (PyList_Check(py_comp_class_addrs)) {
327 size_t i;
328
329 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
330 struct bt_component_class *comp_class;
331 PyObject *py_comp_class_addr;
332
333 py_comp_class_addr =
334 PyList_GetItem(py_comp_class_addrs, i);
335 assert(py_comp_class_addr);
336 if (PyLong_Check(py_comp_class_addr)) {
337 comp_class = (struct bt_component_class *)
338 PyLong_AsUnsignedLongLong(py_comp_class_addr);
339 } else {
9e0bf9b0
PP
340 BT_LOGW("Component class address is not an integer in Python plugin info object: "
341 "py-plugin-info-addr=%p, index=%zu",
342 plugin_info, i);
55bb57e0
PP
343 continue;
344 }
345
346 ret = bt_plugin_add_component_class(plugin, comp_class);
347 if (ret < 0) {
9e0bf9b0
PP
348 BT_LOGE("Cannot add component class to plugin: "
349 "py-plugin-info-addr=%p, "
350 "plugin-addr=%p, plugin-name=\"%s\", "
351 "comp-class-addr=%p, "
352 "comp-class-name=\"%s\", "
353 "comp-class-type=%s",
354 plugin_info,
355 plugin, bt_plugin_get_name(plugin),
356 comp_class,
357 bt_component_class_get_name(comp_class),
358 bt_component_class_type_string(
359 bt_component_class_get_type(comp_class)));
55bb57e0
PP
360 continue;
361 }
362 }
363 }
364
365 bt_plugin_freeze(plugin);
366
367 goto end;
368
369error:
9e0bf9b0 370 print_python_traceback_warn();
55bb57e0
PP
371 pyerr_clear();
372 BT_PUT(plugin);
373
374end:
375 Py_XDECREF(py_name);
376 Py_XDECREF(py_author);
377 Py_XDECREF(py_description);
378 Py_XDECREF(py_license);
379 Py_XDECREF(py_version);
380 Py_XDECREF(py_comp_class_addrs);
381 return plugin;
382}
383
6fbd4105 384G_MODULE_EXPORT
a8ff38ef 385struct bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
55bb57e0 386{
a8ff38ef
PP
387 struct bt_plugin_set *plugin_set = NULL;
388 struct bt_plugin *plugin = NULL;
55bb57e0
PP
389 PyObject *py_plugin_info = NULL;
390 gchar *basename = NULL;
391 size_t path_len;
392
393 assert(path);
394
395 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
396 /*
397 * We do not even care about the rest of the function
398 * here because we already know Python cannot be fully
399 * initialized.
400 */
401 goto error;
402 }
403
9e0bf9b0 404 BT_LOGD("Creating all Python plugins from file: path=\"%s\"", path);
55bb57e0
PP
405 path_len = strlen(path);
406
407 /* File name ends with `.py` */
408 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
409 PYTHON_PLUGIN_FILE_EXT,
410 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
9e0bf9b0 411 BT_LOGD("Skipping non-Python file: path=\"%s\"", path);
55bb57e0
PP
412 goto error;
413 }
414
415 /* File name starts with `bt_plugin_` */
416 basename = g_path_get_basename(path);
417 if (!basename) {
9e0bf9b0 418 BT_LOGW("Cannot get path's basename: path=\"%s\"", path);
55bb57e0
PP
419 goto error;
420 }
421
422 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
423 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
9e0bf9b0
PP
424 BT_LOGD("Skipping Python file not starting with `%s`: "
425 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
55bb57e0
PP
426 goto error;
427 }
428
429 /*
430 * Initialize Python now.
431 *
432 * This is not done in the library contructor because the
433 * interpreter is somewhat slow to initialize. If you don't
434 * have any potential Python plugins, you don't need to endure
435 * this waiting time everytime you load the library.
436 */
437 init_python();
438 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
439 /*
440 * For some reason we cannot initialize Python,
441 * import the required modules, and get the required
442 * attributes from them.
443 */
9e0bf9b0 444 BT_LOGI("Failed to initialize Python interpreter.");
55bb57e0
PP
445 goto error;
446 }
447
448 /*
449 * Call bt2.py_plugin._try_load_plugin_module() with this path
450 * to get plugin info if the plugin is loadable and complete.
451 * This function returns None when there's an error, but just in
452 * case we also manually clear the last Python error state.
453 */
9e0bf9b0 454 BT_LOGD_STR("Getting Python plugin info object from Python module.");
55bb57e0
PP
455 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
456 "(s)", path);
457 if (!py_plugin_info || py_plugin_info == Py_None) {
9e0bf9b0
PP
458 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path);
459 print_python_traceback_warn();
55bb57e0
PP
460 PyErr_Clear();
461 goto error;
462 }
463
464 /*
465 * Get bt_plugin from plugin info object.
55bb57e0 466 */
a8ff38ef
PP
467 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
468 if (!plugin) {
9e0bf9b0
PP
469 BT_LOGW("Cannot create plugin object from Python plugin info object: "
470 "path=\"%s\", py-plugin-info-addr=%p",
471 path, py_plugin_info);
55bb57e0
PP
472 goto error;
473 }
474
a8ff38ef
PP
475 bt_plugin_set_path(plugin, path);
476 plugin_set = bt_plugin_set_create();
477 if (!plugin_set) {
9e0bf9b0 478 BT_LOGE_STR("Cannot create empty plugin set.");
55bb57e0
PP
479 goto error;
480 }
481
a8ff38ef 482 bt_plugin_set_add_plugin(plugin_set, plugin);
9e0bf9b0
PP
483 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
484 "plugin-addr=%p, plugin-name=\"%s\"",
485 path, plugin, bt_plugin_get_name(plugin));
55bb57e0
PP
486 goto end;
487
488error:
a8ff38ef 489 BT_PUT(plugin_set);
55bb57e0
PP
490
491end:
a8ff38ef 492 bt_put(plugin);
55bb57e0
PP
493 Py_XDECREF(py_plugin_info);
494 g_free(basename);
a8ff38ef 495 return plugin_set;
55bb57e0 496}
This page took 0.05023 seconds and 4 git commands to generate.