lib: remove `BT_GRAPH_RUN_STATUS_END`
[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"
c2d9d9cf 28#include "lib/logging.h"
7c7301d5
SM
29
30#include "python-plugin-provider.h"
31
91d81473 32#include "common/macros.h"
578e048b 33#include "compat/compiler.h"
3fadfbc0 34#include <babeltrace2/plugin/plugin-const.h>
578e048b 35#include "lib/plugin/plugin.h"
3fadfbc0 36#include <babeltrace2/graph/component-class.h>
870631a2 37#include <babeltrace2/current-thread.h>
578e048b 38#include "lib/graph/component-class.h"
870631a2 39#include "py-common/py-common.h"
55bb57e0
PP
40#include <stdlib.h>
41#include <signal.h>
42#include <Python.h>
6fbd4105
PP
43#include <glib.h>
44#include <gmodule.h>
55bb57e0
PP
45
46#define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
47#define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
48#define PYTHON_PLUGIN_FILE_EXT ".py"
49#define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
50
51enum python_state {
52 /* init_python() not called yet */
53 PYTHON_STATE_NOT_INITED,
54
55 /* init_python() called once with success */
56 PYTHON_STATE_FULLY_INITIALIZED,
57
58 /* init_python() called once without success */
59 PYTHON_STATE_CANNOT_INITIALIZE,
9736d991
PP
60
61 /*
62 * init_python() called, but environment variable asks the
63 * Python interpreter not to be loaded.
64 */
65 PYTHON_STATE_WONT_INITIALIZE,
55bb57e0
PP
66} python_state = PYTHON_STATE_NOT_INITED;
67
68static PyObject *py_try_load_plugin_module_func = NULL;
edc733b9 69static bool python_was_initialized_by_us;
55bb57e0
PP
70
71static
870631a2 72void append_python_traceback_error_cause(void)
55bb57e0 73{
870631a2
PP
74 GString *exc = NULL;
75
76 if (Py_IsInitialized() && PyErr_Occurred()) {
cacd0713 77 exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
870631a2
PP
78 if (!exc) {
79 BT_LOGE_STR("Failed to format Python exception.");
80 goto end;
81 }
82
83 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
84 "Babeltrace library", "%s", exc->str);
85 }
86
87end:
88 if (exc) {
89 g_string_free(exc, TRUE);
90 }
91}
92
93static
94void log_python_traceback(int log_level)
95{
96 GString *exc = NULL;
97
98 if (Py_IsInitialized() && PyErr_Occurred()) {
cacd0713 99 exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
870631a2
PP
100 if (!exc) {
101 BT_LOGE_STR("Failed to format Python exception.");
102 goto end;
103 }
104
9736d991 105 BT_LOG_WRITE(log_level, BT_LOG_TAG,
870631a2
PP
106 "Exception occured: Python traceback:\n%s", exc->str);
107 }
108
109end:
110 if (exc) {
111 g_string_free(exc, TRUE);
55bb57e0
PP
112 }
113}
114
115static
116void pyerr_clear(void)
117{
118 if (Py_IsInitialized()) {
119 PyErr_Clear();
120 }
121}
122
123static
870631a2 124int init_python(void)
55bb57e0 125{
870631a2 126 int ret = BT_FUNC_STATUS_OK;
55bb57e0
PP
127 PyObject *py_bt2_py_plugin_mod = NULL;
128 const char *dis_python_env;
c9028270 129#ifndef __MINGW32__
86f0fb55 130 sig_t old_sigint = signal(SIGINT, SIG_DFL);
c9028270 131#endif
55bb57e0 132
870631a2
PP
133 switch (python_state) {
134 case PYTHON_STATE_NOT_INITED:
135 break;
136 case PYTHON_STATE_FULLY_INITIALIZED:
137 goto end;
138 case PYTHON_STATE_WONT_INITIALIZE:
139 ret = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0 140 goto end;
870631a2
PP
141 case PYTHON_STATE_CANNOT_INITIALIZE:
142 ret = BT_FUNC_STATUS_ERROR;
143 goto end;
144 default:
145 abort();
55bb57e0
PP
146 }
147
148 /*
149 * User can disable Python plugin support with the
f03cdb2b
PP
150 * `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment variable
151 * set to 1.
55bb57e0 152 */
f03cdb2b 153 dis_python_env = getenv("LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS");
9e0bf9b0 154 if (dis_python_env && strcmp(dis_python_env, "1") == 0) {
f03cdb2b
PP
155 BT_LOGI_STR("Python plugin support is disabled because the "
156 "`LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment "
157 "variable is set to `1`.");
9736d991 158 python_state = PYTHON_STATE_WONT_INITIALIZE;
870631a2 159 ret = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
160 goto end;
161 }
162
163 if (!Py_IsInitialized()) {
13e4a3e3 164 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
55bb57e0 165 Py_InitializeEx(0);
edc733b9 166 python_was_initialized_by_us = true;
9e0bf9b0
PP
167 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
168 Py_GetVersion());
13e4a3e3
PP
169 } else {
170 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
171 Py_GetVersion());
55bb57e0
PP
172 }
173
174 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
175 if (!py_bt2_py_plugin_mod) {
870631a2
PP
176 append_python_traceback_error_cause();
177 BT_LIB_LOGW_APPEND_CAUSE(
178 "Cannot import `bt2.py_plugin` Python module: "
179 "Python plugin support is disabled.");
55bb57e0 180 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
870631a2 181 ret = BT_FUNC_STATUS_ERROR;
55bb57e0
PP
182 goto end;
183 }
184
185 py_try_load_plugin_module_func =
186 PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module");
187 if (!py_try_load_plugin_module_func) {
870631a2
PP
188 append_python_traceback_error_cause();
189 BT_LIB_LOGW_APPEND_CAUSE(
190 "Cannot get `_try_load_plugin_module` attribute from `bt2.py_plugin` Python module: "
191 "Python plugin support is disabled.");
55bb57e0 192 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
870631a2 193 ret = BT_FUNC_STATUS_ERROR;
55bb57e0
PP
194 goto end;
195 }
196
197 python_state = PYTHON_STATE_FULLY_INITIALIZED;
198
199end:
c9028270 200#ifndef __MINGW32__
55bb57e0
PP
201 if (old_sigint != SIG_ERR) {
202 (void) signal(SIGINT, old_sigint);
203 }
c9028270 204#endif
55bb57e0 205
870631a2 206 log_python_traceback(ret == BT_FUNC_STATUS_ERROR ?
770538dd 207 BT_LOG_WARNING : BT_LOG_INFO);
55bb57e0
PP
208 pyerr_clear();
209 Py_XDECREF(py_bt2_py_plugin_mod);
870631a2 210 return ret;
55bb57e0
PP
211}
212
213__attribute__((destructor)) static
214void fini_python(void) {
edc733b9 215 if (Py_IsInitialized() && python_was_initialized_by_us) {
55bb57e0
PP
216 if (py_try_load_plugin_module_func) {
217 Py_DECREF(py_try_load_plugin_module_func);
218 py_try_load_plugin_module_func = NULL;
219 }
220
221 Py_Finalize();
9e0bf9b0 222 BT_LOGI_STR("Finalized Python interpreter.");
55bb57e0
PP
223 }
224
225 python_state = PYTHON_STATE_NOT_INITED;
226}
227
228static
d24d5663 229int bt_plugin_from_python_plugin_info(PyObject *plugin_info,
9736d991 230 bool fail_on_load_error, bt_plugin **plugin_out)
55bb57e0 231{
870631a2 232 int status = BT_FUNC_STATUS_OK;
55bb57e0
PP
233 PyObject *py_name = NULL;
234 PyObject *py_author = NULL;
235 PyObject *py_description = NULL;
236 PyObject *py_license = NULL;
237 PyObject *py_version = NULL;
238 PyObject *py_comp_class_addrs = NULL;
239 const char *name = NULL;
240 const char *author = NULL;
241 const char *description = NULL;
242 const char *license = NULL;
243 unsigned int major = 0, minor = 0, patch = 0;
244 const char *version_extra = NULL;
55bb57e0 245
9736d991
PP
246 BT_ASSERT(plugin_out);
247 *plugin_out = NULL;
f6ccaed9
PP
248 BT_ASSERT(plugin_info);
249 BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
55bb57e0
PP
250 py_name = PyObject_GetAttrString(plugin_info, "name");
251 if (!py_name) {
870631a2
PP
252 if (fail_on_load_error) {
253 append_python_traceback_error_cause();
254 BT_LIB_LOGW_APPEND_CAUSE(
255 "Cannot find `name` attribute in Python plugin info object: "
256 "py-plugin-info-addr=%p", plugin_info);
b189a968 257 status = BT_FUNC_STATUS_ERROR;
870631a2 258 } else {
a0cdfce8 259 BT_LIB_LOGW(
870631a2
PP
260 "Cannot find `name` attribute in Python plugin info object: "
261 "py-plugin-info-addr=%p", plugin_info);
262 status = BT_FUNC_STATUS_NOT_FOUND;
263 }
264
55bb57e0
PP
265 goto error;
266 }
267
268 py_author = PyObject_GetAttrString(plugin_info, "author");
269 if (!py_author) {
870631a2
PP
270 if (fail_on_load_error) {
271 append_python_traceback_error_cause();
272 BT_LIB_LOGW_APPEND_CAUSE(
273 "Cannot find `author` attribute in Python plugin info object: "
274 "py-plugin-info-addr=%p", plugin_info);
b189a968 275 status = BT_FUNC_STATUS_ERROR;
870631a2 276 } else {
a0cdfce8 277 BT_LIB_LOGW(
870631a2
PP
278 "Cannot find `author` attribute in Python plugin info object: "
279 "py-plugin-info-addr=%p", plugin_info);
280 status = BT_FUNC_STATUS_NOT_FOUND;
281 }
282
55bb57e0
PP
283 goto error;
284 }
285
286 py_description = PyObject_GetAttrString(plugin_info, "description");
287 if (!py_description) {
870631a2
PP
288 if (fail_on_load_error) {
289 append_python_traceback_error_cause();
290 BT_LIB_LOGW_APPEND_CAUSE(
291 "Cannot find `description` attribute in Python plugin info object: "
292 "py-plugin-info-addr=%p", plugin_info);
b189a968 293 status = BT_FUNC_STATUS_ERROR;
870631a2 294 } else {
a0cdfce8 295 BT_LIB_LOGW(
870631a2
PP
296 "Cannot find `description` attribute in Python plugin info object: "
297 "py-plugin-info-addr=%p", plugin_info);
298 status = BT_FUNC_STATUS_NOT_FOUND;
299 }
300
55bb57e0
PP
301 goto error;
302 }
303
304 py_license = PyObject_GetAttrString(plugin_info, "license");
305 if (!py_license) {
870631a2
PP
306 if (fail_on_load_error) {
307 append_python_traceback_error_cause();
308 BT_LIB_LOGW_APPEND_CAUSE(
309 "Cannot find `license` attribute in Python plugin info object: "
310 "py-plugin-info-addr=%p", plugin_info);
b189a968 311 status = BT_FUNC_STATUS_ERROR;
870631a2 312 } else {
a0cdfce8 313 BT_LIB_LOGW(
870631a2
PP
314 "Cannot find `license` attribute in Python plugin info object: "
315 "py-plugin-info-addr=%p", plugin_info);
316 status = BT_FUNC_STATUS_NOT_FOUND;
317 }
318
55bb57e0
PP
319 goto error;
320 }
321
322 py_version = PyObject_GetAttrString(plugin_info, "version");
323 if (!py_version) {
870631a2
PP
324 if (fail_on_load_error) {
325 append_python_traceback_error_cause();
326 BT_LIB_LOGW_APPEND_CAUSE(
327 "Cannot find `version` attribute in Python plugin info object: "
328 "py-plugin-info-addr=%p", plugin_info);
b189a968 329 status = BT_FUNC_STATUS_ERROR;
870631a2 330 } else {
a0cdfce8 331 BT_LIB_LOGW(
870631a2
PP
332 "Cannot find `version` attribute in Python plugin info object: "
333 "py-plugin-info-addr=%p", plugin_info);
334 status = BT_FUNC_STATUS_NOT_FOUND;
335 }
336
55bb57e0
PP
337 goto error;
338 }
339
340 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
341 "comp_class_addrs");
342 if (!py_comp_class_addrs) {
870631a2
PP
343 if (fail_on_load_error) {
344 append_python_traceback_error_cause();
345 BT_LIB_LOGW_APPEND_CAUSE(
346 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
347 "py-plugin-info-addr=%p", plugin_info);
b189a968 348 status = BT_FUNC_STATUS_ERROR;
870631a2 349 } else {
a0cdfce8 350 BT_LIB_LOGW(
870631a2
PP
351 "Cannot find `comp_class_addrs` attribute in Python plugin info object: "
352 "py-plugin-info-addr=%p", plugin_info);
353 status = BT_FUNC_STATUS_NOT_FOUND;
354 }
355
55bb57e0
PP
356 goto error;
357 }
358
359 if (PyUnicode_Check(py_name)) {
360 name = PyUnicode_AsUTF8(py_name);
361 if (!name) {
870631a2
PP
362 if (fail_on_load_error) {
363 append_python_traceback_error_cause();
364 BT_LIB_LOGW_APPEND_CAUSE(
365 "Cannot decode Python plugin name string: "
366 "py-plugin-info-addr=%p", plugin_info);
b189a968 367 status = BT_FUNC_STATUS_ERROR;
870631a2 368 } else {
a0cdfce8 369 BT_LIB_LOGW(
870631a2
PP
370 "Cannot decode Python plugin name string: "
371 "py-plugin-info-addr=%p", plugin_info);
372 status = BT_FUNC_STATUS_NOT_FOUND;
373 }
374
55bb57e0
PP
375 goto error;
376 }
377 } else {
378 /* Plugin name is mandatory */
870631a2
PP
379 if (fail_on_load_error) {
380 append_python_traceback_error_cause();
381 BT_LIB_LOGW_APPEND_CAUSE(
382 "Plugin name is not a string: "
383 "py-plugin-info-addr=%p", plugin_info);
b189a968 384 status = BT_FUNC_STATUS_ERROR;
870631a2 385 } else {
a0cdfce8 386 BT_LIB_LOGW(
870631a2
PP
387 "Plugin name is not a string: "
388 "py-plugin-info-addr=%p", plugin_info);
389 status = BT_FUNC_STATUS_NOT_FOUND;
390 }
391
55bb57e0
PP
392 goto error;
393 }
394
395 if (PyUnicode_Check(py_author)) {
396 author = PyUnicode_AsUTF8(py_author);
397 if (!author) {
870631a2
PP
398 if (fail_on_load_error) {
399 append_python_traceback_error_cause();
400 BT_LIB_LOGW_APPEND_CAUSE(
401 "Cannot decode Python plugin author string: "
402 "py-plugin-info-addr=%p", plugin_info);
b189a968 403 status = BT_FUNC_STATUS_ERROR;
870631a2 404 } else {
a0cdfce8 405 BT_LIB_LOGW(
870631a2
PP
406 "Cannot decode Python plugin author string: "
407 "py-plugin-info-addr=%p", plugin_info);
408 status = BT_FUNC_STATUS_NOT_FOUND;
409 }
410
55bb57e0
PP
411 goto error;
412 }
413 }
414
415 if (PyUnicode_Check(py_description)) {
416 description = PyUnicode_AsUTF8(py_description);
417 if (!description) {
870631a2
PP
418 if (fail_on_load_error) {
419 append_python_traceback_error_cause();
420 BT_LIB_LOGW_APPEND_CAUSE(
421 "Cannot decode Python plugin description string: "
422 "py-plugin-info-addr=%p", plugin_info);
b189a968 423 status = BT_FUNC_STATUS_ERROR;
870631a2 424 } else {
a0cdfce8 425 BT_LIB_LOGW(
870631a2
PP
426 "Cannot decode Python plugin description string: "
427 "py-plugin-info-addr=%p", plugin_info);
428 status = BT_FUNC_STATUS_NOT_FOUND;
429 }
430
55bb57e0
PP
431 goto error;
432 }
433 }
434
435 if (PyUnicode_Check(py_license)) {
436 license = PyUnicode_AsUTF8(py_license);
437 if (!license) {
870631a2
PP
438 if (fail_on_load_error) {
439 append_python_traceback_error_cause();
440 BT_LIB_LOGW_APPEND_CAUSE(
441 "Cannot decode Python plugin license string: "
442 "py-plugin-info-addr=%p", plugin_info);
b189a968 443 status = BT_FUNC_STATUS_ERROR;
870631a2 444 } else {
a0cdfce8 445 BT_LIB_LOGW(
870631a2
PP
446 "Cannot decode Python plugin license string: "
447 "py-plugin-info-addr=%p", plugin_info);
448 status = BT_FUNC_STATUS_NOT_FOUND;
449 }
450
55bb57e0
PP
451 goto error;
452 }
453 }
454
455 if (PyTuple_Check(py_version)) {
456 if (PyTuple_Size(py_version) >= 3) {
457 PyObject *py_major = PyTuple_GetItem(py_version, 0);
458 PyObject *py_minor = PyTuple_GetItem(py_version, 1);
459 PyObject *py_patch = PyTuple_GetItem(py_version, 2);
460
f6ccaed9
PP
461 BT_ASSERT(py_major);
462 BT_ASSERT(py_minor);
463 BT_ASSERT(py_patch);
55bb57e0
PP
464
465 if (PyLong_Check(py_major)) {
466 major = PyLong_AsUnsignedLong(py_major);
467 }
468
469 if (PyLong_Check(py_minor)) {
470 minor = PyLong_AsUnsignedLong(py_minor);
471 }
472
473 if (PyLong_Check(py_patch)) {
474 patch = PyLong_AsUnsignedLong(py_patch);
475 }
476
477 if (PyErr_Occurred()) {
478 /* Overflow error, most probably */
870631a2
PP
479 if (fail_on_load_error) {
480 append_python_traceback_error_cause();
481 BT_LIB_LOGW_APPEND_CAUSE(
482 "Invalid Python plugin version format: "
483 "py-plugin-info-addr=%p", plugin_info);
b189a968 484 status = BT_FUNC_STATUS_ERROR;
870631a2 485 } else {
a0cdfce8 486 BT_LIB_LOGW(
870631a2
PP
487 "Invalid Python plugin version format: "
488 "py-plugin-info-addr=%p", plugin_info);
489 status = BT_FUNC_STATUS_NOT_FOUND;
490 }
491
55bb57e0
PP
492 goto error;
493 }
494 }
495
496 if (PyTuple_Size(py_version) >= 4) {
497 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
498
f6ccaed9 499 BT_ASSERT(py_extra);
55bb57e0
PP
500
501 if (PyUnicode_Check(py_extra)) {
502 version_extra = PyUnicode_AsUTF8(py_extra);
503 if (!version_extra) {
870631a2
PP
504 if (fail_on_load_error) {
505 append_python_traceback_error_cause();
506 BT_LIB_LOGW_APPEND_CAUSE(
507 "Cannot decode Python plugin version's extra string: "
508 "py-plugin-info-addr=%p", plugin_info);
b189a968 509 status = BT_FUNC_STATUS_ERROR;
870631a2 510 } else {
a0cdfce8 511 BT_LIB_LOGW(
870631a2
PP
512 "Cannot decode Python plugin version's extra string: "
513 "py-plugin-info-addr=%p", plugin_info);
514 status = BT_FUNC_STATUS_NOT_FOUND;
515 }
516
55bb57e0
PP
517 goto error;
518 }
519 }
520 }
521 }
522
9736d991
PP
523 *plugin_out = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
524 if (!*plugin_out) {
870631a2
PP
525 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin object.");
526 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
527 goto error;
528 }
529
9736d991 530 bt_plugin_set_name(*plugin_out, name);
55bb57e0
PP
531
532 if (description) {
9736d991 533 bt_plugin_set_description(*plugin_out, description);
55bb57e0
PP
534 }
535
536 if (author) {
9736d991 537 bt_plugin_set_author(*plugin_out, author);
55bb57e0
PP
538 }
539
540 if (license) {
9736d991 541 bt_plugin_set_license(*plugin_out, license);
55bb57e0
PP
542 }
543
9736d991 544 bt_plugin_set_version(*plugin_out, major, minor, patch, version_extra);
55bb57e0
PP
545
546 if (PyList_Check(py_comp_class_addrs)) {
547 size_t i;
548
549 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
b19ff26f 550 bt_component_class *comp_class;
55bb57e0
PP
551 PyObject *py_comp_class_addr;
552
553 py_comp_class_addr =
554 PyList_GetItem(py_comp_class_addrs, i);
f6ccaed9 555 BT_ASSERT(py_comp_class_addr);
55bb57e0 556 if (PyLong_Check(py_comp_class_addr)) {
babe0791 557 comp_class = PyLong_AsVoidPtr(py_comp_class_addr);
55bb57e0 558 } else {
9736d991 559 if (fail_on_load_error) {
870631a2
PP
560 append_python_traceback_error_cause();
561 BT_LIB_LOGW_APPEND_CAUSE(
562 "Component class address is not an integer in Python plugin info object: "
563 "py-plugin-info-addr=%p, index=%zu",
564 plugin_info, i);
b189a968 565 status = BT_FUNC_STATUS_ERROR;
870631a2 566 } else {
a0cdfce8 567 BT_LIB_LOGW(
870631a2
PP
568 "Component class address is not an integer in Python plugin info object: "
569 "py-plugin-info-addr=%p, index=%zu",
570 plugin_info, i);
571 status = BT_FUNC_STATUS_NOT_FOUND;
9736d991
PP
572 }
573
55bb57e0
PP
574 continue;
575 }
576
9736d991
PP
577 status = bt_plugin_add_component_class(*plugin_out,
578 comp_class);
579 if (status < 0) {
870631a2
PP
580 BT_LIB_LOGE_APPEND_CAUSE(
581 "Cannot add component class to plugin: "
9e0bf9b0
PP
582 "py-plugin-info-addr=%p, "
583 "plugin-addr=%p, plugin-name=\"%s\", "
584 "comp-class-addr=%p, "
585 "comp-class-name=\"%s\", "
586 "comp-class-type=%s",
9736d991
PP
587 plugin_info, *plugin_out,
588 bt_plugin_get_name(*plugin_out),
9e0bf9b0
PP
589 comp_class,
590 bt_component_class_get_name(comp_class),
591 bt_component_class_type_string(
592 bt_component_class_get_type(comp_class)));
9736d991 593 goto error;
55bb57e0
PP
594 }
595 }
596 }
597
55bb57e0
PP
598 goto end;
599
600error:
870631a2 601 BT_ASSERT(status != BT_FUNC_STATUS_OK);
770538dd 602 log_python_traceback(fail_on_load_error ? BT_LOG_WARNING : BT_LOG_INFO);
55bb57e0 603 pyerr_clear();
9736d991 604 BT_OBJECT_PUT_REF_AND_RESET(*plugin_out);
55bb57e0
PP
605
606end:
607 Py_XDECREF(py_name);
608 Py_XDECREF(py_author);
609 Py_XDECREF(py_description);
610 Py_XDECREF(py_license);
611 Py_XDECREF(py_version);
612 Py_XDECREF(py_comp_class_addrs);
9736d991 613 return status;
55bb57e0
PP
614}
615
6fbd4105 616G_MODULE_EXPORT
d24d5663 617int bt_plugin_python_create_all_from_file(const char *path,
9736d991 618 bool fail_on_load_error, struct bt_plugin_set **plugin_set_out)
55bb57e0 619{
88b3fc9c 620 bt_plugin *plugin = NULL;
55bb57e0
PP
621 PyObject *py_plugin_info = NULL;
622 gchar *basename = NULL;
623 size_t path_len;
870631a2 624 int status = BT_FUNC_STATUS_OK;
55bb57e0 625
f6ccaed9 626 BT_ASSERT(path);
55bb57e0
PP
627
628 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
629 /*
630 * We do not even care about the rest of the function
631 * here because we already know Python cannot be fully
632 * initialized.
633 */
870631a2
PP
634 BT_LIB_LOGE_APPEND_CAUSE(
635 "Python interpreter could not be initialized previously.");
636 status = BT_FUNC_STATUS_ERROR;
9736d991
PP
637 goto error;
638 } else if (python_state == PYTHON_STATE_WONT_INITIALIZE) {
639 /*
640 * This is not an error: the environment requires that
641 * Python plugins are disabled, so it's simply not
642 * found.
643 */
f03cdb2b
PP
644 BT_LOGI_STR("Python plugin support was disabled previously "
645 "because the `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` "
646 "environment variable is set to `1`.");
870631a2 647 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
648 goto error;
649 }
650
3f7d4d90
PP
651 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
652 path);
55bb57e0
PP
653 path_len = strlen(path);
654
655 /* File name ends with `.py` */
656 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
657 PYTHON_PLUGIN_FILE_EXT,
658 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
3f7d4d90 659 BT_LOGI("Skipping non-Python file: path=\"%s\"", path);
870631a2 660 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
661 goto error;
662 }
663
664 /* File name starts with `bt_plugin_` */
665 basename = g_path_get_basename(path);
666 if (!basename) {
870631a2
PP
667 BT_LIB_LOGE_APPEND_CAUSE(
668 "Cannot get path's basename: path=\"%s\"", path);
669 status = BT_FUNC_STATUS_ERROR;
55bb57e0
PP
670 goto error;
671 }
672
673 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
674 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
3f7d4d90 675 BT_LOGI("Skipping Python file not starting with `%s`: "
9e0bf9b0 676 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
870631a2 677 status = BT_FUNC_STATUS_NOT_FOUND;
55bb57e0
PP
678 goto error;
679 }
680
681 /*
682 * Initialize Python now.
683 *
684 * This is not done in the library contructor because the
685 * interpreter is somewhat slow to initialize. If you don't
686 * have any potential Python plugins, you don't need to endure
687 * this waiting time everytime you load the library.
688 */
870631a2
PP
689 status = init_python();
690 if (status != BT_FUNC_STATUS_OK) {
691 /* init_python() logs and append errors */
55bb57e0
PP
692 goto error;
693 }
694
695 /*
696 * Call bt2.py_plugin._try_load_plugin_module() with this path
697 * to get plugin info if the plugin is loadable and complete.
698 * This function returns None when there's an error, but just in
699 * case we also manually clear the last Python error state.
700 */
9e0bf9b0 701 BT_LOGD_STR("Getting Python plugin info object from Python module.");
55bb57e0
PP
702 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
703 "(s)", path);
704 if (!py_plugin_info || py_plugin_info == Py_None) {
870631a2
PP
705 if (fail_on_load_error) {
706 append_python_traceback_error_cause();
707 BT_LIB_LOGW_APPEND_CAUSE(
708 "Cannot load Python plugin: path=\"%s\"", path);
b189a968 709 status = BT_FUNC_STATUS_ERROR;
870631a2 710 } else {
a0cdfce8 711 BT_LIB_LOGW(
870631a2
PP
712 "Cannot load Python plugin: path=\"%s\"", path);
713 status = BT_FUNC_STATUS_NOT_FOUND;
714 }
715
55bb57e0
PP
716 goto error;
717 }
718
719 /*
720 * Get bt_plugin from plugin info object.
55bb57e0 721 */
9736d991
PP
722 plugin = NULL;
723 status = bt_plugin_from_python_plugin_info(py_plugin_info,
724 fail_on_load_error, &plugin);
725 if (status < 0) {
726 /*
727 * bt_plugin_from_python_plugin_info() handles
728 * `fail_on_load_error`, so this is a "real" error.
729 */
870631a2
PP
730 BT_LIB_LOGW_APPEND_CAUSE(
731 "Cannot create plugin object from Python plugin info object: "
9e0bf9b0
PP
732 "path=\"%s\", py-plugin-info-addr=%p",
733 path, py_plugin_info);
9736d991
PP
734 BT_ASSERT(!plugin);
735 goto error;
870631a2 736 } else if (status == BT_FUNC_STATUS_NOT_FOUND) {
9736d991 737 BT_ASSERT(!plugin);
55bb57e0
PP
738 goto error;
739 }
740
870631a2 741 BT_ASSERT(status == BT_FUNC_STATUS_OK);
9736d991 742 BT_ASSERT(plugin);
a8ff38ef 743 bt_plugin_set_path(plugin, path);
9736d991
PP
744 *plugin_set_out = bt_plugin_set_create();
745 if (!*plugin_set_out) {
870631a2
PP
746 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty plugin set.");
747 status = BT_FUNC_STATUS_MEMORY_ERROR;
55bb57e0
PP
748 goto error;
749 }
750
9736d991 751 bt_plugin_set_add_plugin(*plugin_set_out, plugin);
9e0bf9b0
PP
752 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
753 "plugin-addr=%p, plugin-name=\"%s\"",
754 path, plugin, bt_plugin_get_name(plugin));
55bb57e0
PP
755 goto end;
756
757error:
870631a2 758 BT_ASSERT(status != BT_FUNC_STATUS_OK);
a0cdfce8 759 log_python_traceback(BT_LOG_WARNING);
870631a2 760 pyerr_clear();
9736d991 761 BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out);
55bb57e0
PP
762
763end:
c5b9b441 764 bt_plugin_put_ref(plugin);
55bb57e0 765 Py_XDECREF(py_plugin_info);
870631a2 766
19bbdc9b 767 g_free(basename);
870631a2 768
9736d991 769 return status;
55bb57e0 770}
This page took 0.087552 seconds and 4 git commands to generate.