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