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