Logging: standardize logging tags
[babeltrace.git] / src / 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
350ad6c1 27#define BT_LOG_TAG "LIB/PLUGIN-PY"
3fe0bf43 28
d3207ebf 29#include "lib/lib-logging.h"
91d81473 30#include "common/macros.h"
578e048b 31#include "compat/compiler.h"
3fadfbc0 32#include <babeltrace2/plugin/plugin-const.h>
578e048b 33#include "lib/plugin/plugin.h"
3fadfbc0 34#include <babeltrace2/graph/component-class.h>
578e048b 35#include "lib/graph/component-class.h"
55bb57e0
PP
36#include <stdlib.h>
37#include <signal.h>
38#include <Python.h>
6fbd4105
PP
39#include <glib.h>
40#include <gmodule.h>
55bb57e0
PP
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
47enum 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
58static PyObject *py_try_load_plugin_module_func = NULL;
edc733b9 59static bool python_was_initialized_by_us;
55bb57e0
PP
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;
c9028270 83#ifndef __MINGW32__
55bb57e0 84 sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
c9028270 85#endif
55bb57e0
PP
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");
9e0bf9b0
PP
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`.");
55bb57e0
PP
99 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
100 goto end;
101 }
102
103 if (!Py_IsInitialized()) {
13e4a3e3 104 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
55bb57e0 105 Py_InitializeEx(0);
edc733b9 106 python_was_initialized_by_us = true;
9e0bf9b0
PP
107 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
108 Py_GetVersion());
13e4a3e3
PP
109 } else {
110 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
111 Py_GetVersion());
55bb57e0
PP
112 }
113
114 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
115 if (!py_bt2_py_plugin_mod) {
9e0bf9b0 116 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
55bb57e0
PP
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) {
9e0bf9b0 124 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
55bb57e0
PP
125 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
126 goto end;
127 }
128
129 python_state = PYTHON_STATE_FULLY_INITIALIZED;
130
131end:
c9028270 132#ifndef __MINGW32__
55bb57e0
PP
133 if (old_sigint != SIG_ERR) {
134 (void) signal(SIGINT, old_sigint);
135 }
c9028270 136#endif
55bb57e0 137
9e0bf9b0 138 print_python_traceback_warn();
55bb57e0
PP
139 pyerr_clear();
140 Py_XDECREF(py_bt2_py_plugin_mod);
141 return;
142}
143
144__attribute__((destructor)) static
145void fini_python(void) {
edc733b9 146 if (Py_IsInitialized() && python_was_initialized_by_us) {
55bb57e0
PP
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();
9e0bf9b0 153 BT_LOGI_STR("Finalized Python interpreter.");
55bb57e0
PP
154 }
155
156 python_state = PYTHON_STATE_NOT_INITED;
157}
158
159static
88b3fc9c 160bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
55bb57e0 161{
88b3fc9c 162 bt_plugin *plugin = NULL;
55bb57e0
PP
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
f6ccaed9
PP
177 BT_ASSERT(plugin_info);
178 BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
55bb57e0
PP
179 py_name = PyObject_GetAttrString(plugin_info, "name");
180 if (!py_name) {
9e0bf9b0
PP
181 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
182 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
183 goto error;
184 }
185
186 py_author = PyObject_GetAttrString(plugin_info, "author");
187 if (!py_author) {
9e0bf9b0
PP
188 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
189 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
190 goto error;
191 }
192
193 py_description = PyObject_GetAttrString(plugin_info, "description");
194 if (!py_description) {
9e0bf9b0
PP
195 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
196 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
197 goto error;
198 }
199
200 py_license = PyObject_GetAttrString(plugin_info, "license");
201 if (!py_license) {
9e0bf9b0
PP
202 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
203 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
204 goto error;
205 }
206
207 py_version = PyObject_GetAttrString(plugin_info, "version");
208 if (!py_version) {
9e0bf9b0
PP
209 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
210 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
211 goto error;
212 }
213
214 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
215 "comp_class_addrs");
216 if (!py_comp_class_addrs) {
9e0bf9b0
PP
217 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
218 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
219 goto error;
220 }
221
222 if (PyUnicode_Check(py_name)) {
223 name = PyUnicode_AsUTF8(py_name);
224 if (!name) {
9e0bf9b0
PP
225 BT_LOGW("Cannot decode Python plugin name string: "
226 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
227 goto error;
228 }
229 } else {
230 /* Plugin name is mandatory */
9e0bf9b0
PP
231 BT_LOGW("Plugin name is not a string: "
232 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
233 goto error;
234 }
235
236 if (PyUnicode_Check(py_author)) {
237 author = PyUnicode_AsUTF8(py_author);
238 if (!author) {
9e0bf9b0
PP
239 BT_LOGW("Cannot decode Python plugin author string: "
240 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
241 goto error;
242 }
243 }
244
245 if (PyUnicode_Check(py_description)) {
246 description = PyUnicode_AsUTF8(py_description);
247 if (!description) {
9e0bf9b0
PP
248 BT_LOGW("Cannot decode Python plugin description string: "
249 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
250 goto error;
251 }
252 }
253
254 if (PyUnicode_Check(py_license)) {
255 license = PyUnicode_AsUTF8(py_license);
256 if (!license) {
9e0bf9b0
PP
257 BT_LOGW("Cannot decode Python plugin license string: "
258 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
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
f6ccaed9
PP
269 BT_ASSERT(py_major);
270 BT_ASSERT(py_minor);
271 BT_ASSERT(py_patch);
55bb57e0
PP
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 */
9e0bf9b0
PP
287 BT_LOGW("Invalid Python plugin version format: "
288 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
289 goto error;
290 }
291 }
292
293 if (PyTuple_Size(py_version) >= 4) {
294 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
295
f6ccaed9 296 BT_ASSERT(py_extra);
55bb57e0
PP
297
298 if (PyUnicode_Check(py_extra)) {
299 version_extra = PyUnicode_AsUTF8(py_extra);
300 if (!version_extra) {
9e0bf9b0
PP
301 BT_LOGW("Cannot decode Python plugin version's extra string: "
302 "py-plugin-info-addr=%p", plugin_info);
55bb57e0
PP
303 goto error;
304 }
305 }
306 }
307 }
308
309 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
310 if (!plugin) {
9e0bf9b0 311 BT_LOGE_STR("Cannot create empty plugin object.");
55bb57e0
PP
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++) {
b19ff26f 335 bt_component_class *comp_class;
55bb57e0
PP
336 PyObject *py_comp_class_addr;
337
338 py_comp_class_addr =
339 PyList_GetItem(py_comp_class_addrs, i);
f6ccaed9 340 BT_ASSERT(py_comp_class_addr);
55bb57e0 341 if (PyLong_Check(py_comp_class_addr)) {
babe0791 342 comp_class = PyLong_AsVoidPtr(py_comp_class_addr);
55bb57e0 343 } else {
9e0bf9b0
PP
344 BT_LOGW("Component class address is not an integer in Python plugin info object: "
345 "py-plugin-info-addr=%p, index=%zu",
346 plugin_info, i);
55bb57e0
PP
347 continue;
348 }
349
350 ret = bt_plugin_add_component_class(plugin, comp_class);
351 if (ret < 0) {
9e0bf9b0
PP
352 BT_LOGE("Cannot add component class to plugin: "
353 "py-plugin-info-addr=%p, "
354 "plugin-addr=%p, plugin-name=\"%s\", "
355 "comp-class-addr=%p, "
356 "comp-class-name=\"%s\", "
357 "comp-class-type=%s",
358 plugin_info,
359 plugin, bt_plugin_get_name(plugin),
360 comp_class,
361 bt_component_class_get_name(comp_class),
362 bt_component_class_type_string(
363 bt_component_class_get_type(comp_class)));
55bb57e0
PP
364 continue;
365 }
366 }
367 }
368
55bb57e0
PP
369 goto end;
370
371error:
9e0bf9b0 372 print_python_traceback_warn();
55bb57e0 373 pyerr_clear();
65300d60 374 BT_OBJECT_PUT_REF_AND_RESET(plugin);
55bb57e0
PP
375
376end:
377 Py_XDECREF(py_name);
378 Py_XDECREF(py_author);
379 Py_XDECREF(py_description);
380 Py_XDECREF(py_license);
381 Py_XDECREF(py_version);
382 Py_XDECREF(py_comp_class_addrs);
383 return plugin;
384}
385
6fbd4105 386G_MODULE_EXPORT
b19ff26f 387bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
55bb57e0 388{
b19ff26f 389 bt_plugin_set *plugin_set = NULL;
88b3fc9c 390 bt_plugin *plugin = NULL;
55bb57e0
PP
391 PyObject *py_plugin_info = NULL;
392 gchar *basename = NULL;
393 size_t path_len;
394
f6ccaed9 395 BT_ASSERT(path);
55bb57e0
PP
396
397 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
398 /*
399 * We do not even care about the rest of the function
400 * here because we already know Python cannot be fully
401 * initialized.
402 */
403 goto error;
404 }
405
3f7d4d90
PP
406 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
407 path);
55bb57e0
PP
408 path_len = strlen(path);
409
410 /* File name ends with `.py` */
411 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
412 PYTHON_PLUGIN_FILE_EXT,
413 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
3f7d4d90 414 BT_LOGI("Skipping non-Python file: path=\"%s\"", path);
55bb57e0
PP
415 goto error;
416 }
417
418 /* File name starts with `bt_plugin_` */
419 basename = g_path_get_basename(path);
420 if (!basename) {
9e0bf9b0 421 BT_LOGW("Cannot get path's basename: path=\"%s\"", path);
55bb57e0
PP
422 goto error;
423 }
424
425 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
426 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
3f7d4d90 427 BT_LOGI("Skipping Python file not starting with `%s`: "
9e0bf9b0 428 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
55bb57e0
PP
429 goto error;
430 }
431
432 /*
433 * Initialize Python now.
434 *
435 * This is not done in the library contructor because the
436 * interpreter is somewhat slow to initialize. If you don't
437 * have any potential Python plugins, you don't need to endure
438 * this waiting time everytime you load the library.
439 */
440 init_python();
441 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
442 /*
443 * For some reason we cannot initialize Python,
444 * import the required modules, and get the required
445 * attributes from them.
446 */
9e0bf9b0 447 BT_LOGI("Failed to initialize Python interpreter.");
55bb57e0
PP
448 goto error;
449 }
450
451 /*
452 * Call bt2.py_plugin._try_load_plugin_module() with this path
453 * to get plugin info if the plugin is loadable and complete.
454 * This function returns None when there's an error, but just in
455 * case we also manually clear the last Python error state.
456 */
9e0bf9b0 457 BT_LOGD_STR("Getting Python plugin info object from Python module.");
55bb57e0
PP
458 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
459 "(s)", path);
460 if (!py_plugin_info || py_plugin_info == Py_None) {
9e0bf9b0
PP
461 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path);
462 print_python_traceback_warn();
55bb57e0
PP
463 PyErr_Clear();
464 goto error;
465 }
466
467 /*
468 * Get bt_plugin from plugin info object.
55bb57e0 469 */
a8ff38ef
PP
470 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
471 if (!plugin) {
9e0bf9b0
PP
472 BT_LOGW("Cannot create plugin object from Python plugin info object: "
473 "path=\"%s\", py-plugin-info-addr=%p",
474 path, py_plugin_info);
55bb57e0
PP
475 goto error;
476 }
477
a8ff38ef
PP
478 bt_plugin_set_path(plugin, path);
479 plugin_set = bt_plugin_set_create();
480 if (!plugin_set) {
9e0bf9b0 481 BT_LOGE_STR("Cannot create empty plugin set.");
55bb57e0
PP
482 goto error;
483 }
484
a8ff38ef 485 bt_plugin_set_add_plugin(plugin_set, plugin);
9e0bf9b0
PP
486 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
487 "plugin-addr=%p, plugin-name=\"%s\"",
488 path, plugin, bt_plugin_get_name(plugin));
55bb57e0
PP
489 goto end;
490
491error:
65300d60 492 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
55bb57e0
PP
493
494end:
c5b9b441 495 bt_plugin_put_ref(plugin);
55bb57e0
PP
496 Py_XDECREF(py_plugin_info);
497 g_free(basename);
a8ff38ef 498 return plugin_set;
55bb57e0 499}
This page took 0.060002 seconds and 4 git commands to generate.