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