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