Commit | Line | Data |
---|---|---|
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" |
43c59509 | 34 | #include <babeltrace2/plugin/plugin-loading.h> |
578e048b | 35 | #include "lib/plugin/plugin.h" |
3fadfbc0 | 36 | #include <babeltrace2/graph/component-class.h> |
43c59509 | 37 | #include <babeltrace2/error-reporting.h> |
578e048b | 38 | #include "lib/graph/component-class.h" |
870631a2 | 39 | #include "py-common/py-common.h" |
c4f23e30 | 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 | ||
91ec031c | 52 | static 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, | |
9736d991 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 | ||
69 | static PyObject *py_try_load_plugin_module_func = NULL; | |
edc733b9 | 70 | static bool python_was_initialized_by_us; |
55bb57e0 PP |
71 | |
72 | static | |
870631a2 | 73 | void append_python_traceback_error_cause(void) |
55bb57e0 | 74 | { |
870631a2 PP |
75 | GString *exc = NULL; |
76 | ||
77 | if (Py_IsInitialized() && PyErr_Occurred()) { | |
cacd0713 | 78 | exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL); |
870631a2 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( | |
d5becf10 | 85 | BT_LIB_LOG_LIBBABELTRACE2_NAME, "%s", exc->str); |
870631a2 PP |
86 | } |
87 | ||
88 | end: | |
89 | if (exc) { | |
90 | g_string_free(exc, TRUE); | |
91 | } | |
92 | } | |
93 | ||
94 | static | |
95 | void log_python_traceback(int log_level) | |
96 | { | |
97 | GString *exc = NULL; | |
98 | ||
99 | if (Py_IsInitialized() && PyErr_Occurred()) { | |
cacd0713 | 100 | exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL); |
870631a2 PP |
101 | if (!exc) { |
102 | BT_LOGE_STR("Failed to format Python exception."); | |
103 | goto end; | |
104 | } | |
105 | ||
9736d991 | 106 | BT_LOG_WRITE(log_level, BT_LOG_TAG, |
118ae153 | 107 | "Exception occurred: Python traceback:\n%s", exc->str); |
870631a2 PP |
108 | } |
109 | ||
110 | end: | |
111 | if (exc) { | |
112 | g_string_free(exc, TRUE); | |
55bb57e0 PP |
113 | } |
114 | } | |
115 | ||
116 | static | |
117 | void pyerr_clear(void) | |
118 | { | |
119 | if (Py_IsInitialized()) { | |
120 | PyErr_Clear(); | |
121 | } | |
122 | } | |
123 | ||
124 | static | |
870631a2 | 125 | int init_python(void) |
55bb57e0 | 126 | { |
870631a2 | 127 | int ret = BT_FUNC_STATUS_OK; |
55bb57e0 PP |
128 | PyObject *py_bt2_py_plugin_mod = NULL; |
129 | const char *dis_python_env; | |
c9028270 | 130 | #ifndef __MINGW32__ |
86f0fb55 | 131 | sig_t old_sigint = signal(SIGINT, SIG_DFL); |
c9028270 | 132 | #endif |
55bb57e0 | 133 | |
870631a2 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; |
870631a2 PP |
142 | case PYTHON_STATE_CANNOT_INITIALIZE: |
143 | ret = BT_FUNC_STATUS_ERROR; | |
144 | goto end; | |
145 | default: | |
498e7994 | 146 | bt_common_abort(); |
55bb57e0 PP |
147 | } |
148 | ||
149 | /* | |
150 | * User can disable Python plugin support with the | |
f03cdb2b PP |
151 | * `LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment variable |
152 | * set to 1. | |
55bb57e0 | 153 | */ |
f03cdb2b | 154 | dis_python_env = getenv("LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS"); |
9e0bf9b0 | 155 | if (dis_python_env && strcmp(dis_python_env, "1") == 0) { |
f03cdb2b PP |
156 | BT_LOGI_STR("Python plugin support is disabled because the " |
157 | "`LIBBABELTRACE2_DISABLE_PYTHON_PLUGINS` environment " | |
158 | "variable is set to `1`."); | |
9736d991 | 159 | python_state = PYTHON_STATE_WONT_INITIALIZE; |
870631a2 | 160 | ret = BT_FUNC_STATUS_NOT_FOUND; |
55bb57e0 PP |
161 | goto end; |
162 | } | |
163 | ||
164 | if (!Py_IsInitialized()) { | |
13e4a3e3 | 165 | BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter."); |
55bb57e0 | 166 | Py_InitializeEx(0); |
edc733b9 | 167 | python_was_initialized_by_us = true; |
9e0bf9b0 PP |
168 | BT_LOGI("Initialized Python interpreter: version=\"%s\"", |
169 | Py_GetVersion()); | |
13e4a3e3 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) { | |
870631a2 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; |
870631a2 | 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) { | |
870631a2 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; |
870631a2 | 194 | ret = BT_FUNC_STATUS_ERROR; |
55bb57e0 PP |
195 | goto end; |
196 | } | |
197 | ||
198 | python_state = PYTHON_STATE_FULLY_INITIALIZED; | |
199 | ||
200 | end: | |
c9028270 | 201 | #ifndef __MINGW32__ |
55bb57e0 PP |
202 | if (old_sigint != SIG_ERR) { |
203 | (void) signal(SIGINT, old_sigint); | |
204 | } | |
c9028270 | 205 | #endif |
55bb57e0 | 206 | |
870631a2 | 207 | log_python_traceback(ret == BT_FUNC_STATUS_ERROR ? |
770538dd | 208 | BT_LOG_WARNING : BT_LOG_INFO); |
55bb57e0 PP |
209 | pyerr_clear(); |
210 | Py_XDECREF(py_bt2_py_plugin_mod); | |
870631a2 | 211 | return ret; |
55bb57e0 PP |
212 | } |
213 | ||
214 | __attribute__((destructor)) static | |
215 | void fini_python(void) { | |
edc733b9 | 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 | ||
229 | static | |
d24d5663 | 230 | int bt_plugin_from_python_plugin_info(PyObject *plugin_info, |
9736d991 | 231 | bool fail_on_load_error, bt_plugin **plugin_out) |
55bb57e0 | 232 | { |
870631a2 | 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 | |
9736d991 PP |
247 | BT_ASSERT(plugin_out); |
248 | *plugin_out = NULL; | |
f6ccaed9 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) { | |
870631a2 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); | |
b189a968 | 258 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 259 | } else { |
a0cdfce8 | 260 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 276 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 277 | } else { |
a0cdfce8 | 278 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 294 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 295 | } else { |
a0cdfce8 | 296 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 312 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 313 | } else { |
a0cdfce8 | 314 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 330 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 331 | } else { |
a0cdfce8 | 332 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 349 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 350 | } else { |
a0cdfce8 | 351 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 368 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 369 | } else { |
a0cdfce8 | 370 | BT_LIB_LOGW( |
870631a2 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 */ | |
870631a2 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); | |
b189a968 | 385 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 386 | } else { |
a0cdfce8 | 387 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 404 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 405 | } else { |
a0cdfce8 | 406 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 424 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 425 | } else { |
a0cdfce8 | 426 | BT_LIB_LOGW( |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 444 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 445 | } else { |
a0cdfce8 | 446 | BT_LIB_LOGW( |
870631a2 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 | ||
f6ccaed9 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 */ | |
870631a2 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); | |
b189a968 | 485 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 486 | } else { |
a0cdfce8 | 487 | BT_LIB_LOGW( |
870631a2 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 | ||
f6ccaed9 | 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) { | |
870631a2 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); | |
b189a968 | 510 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 511 | } else { |
a0cdfce8 | 512 | BT_LIB_LOGW( |
870631a2 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 | ||
9736d991 PP |
524 | *plugin_out = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON); |
525 | if (!*plugin_out) { | |
870631a2 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 | ||
9736d991 | 531 | bt_plugin_set_name(*plugin_out, name); |
55bb57e0 PP |
532 | |
533 | if (description) { | |
9736d991 | 534 | bt_plugin_set_description(*plugin_out, description); |
55bb57e0 PP |
535 | } |
536 | ||
537 | if (author) { | |
9736d991 | 538 | bt_plugin_set_author(*plugin_out, author); |
55bb57e0 PP |
539 | } |
540 | ||
541 | if (license) { | |
9736d991 | 542 | bt_plugin_set_license(*plugin_out, license); |
55bb57e0 PP |
543 | } |
544 | ||
9736d991 | 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++) { | |
b19ff26f | 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); | |
f6ccaed9 | 556 | BT_ASSERT(py_comp_class_addr); |
55bb57e0 | 557 | if (PyLong_Check(py_comp_class_addr)) { |
babe0791 | 558 | comp_class = PyLong_AsVoidPtr(py_comp_class_addr); |
55bb57e0 | 559 | } else { |
9736d991 | 560 | if (fail_on_load_error) { |
870631a2 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); | |
b189a968 | 566 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 567 | } else { |
a0cdfce8 | 568 | BT_LIB_LOGW( |
870631a2 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; | |
9736d991 PP |
573 | } |
574 | ||
55bb57e0 PP |
575 | continue; |
576 | } | |
577 | ||
9736d991 PP |
578 | status = bt_plugin_add_component_class(*plugin_out, |
579 | comp_class); | |
580 | if (status < 0) { | |
870631a2 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", | |
9736d991 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))); | |
9736d991 | 594 | goto error; |
55bb57e0 PP |
595 | } |
596 | } | |
597 | } | |
598 | ||
55bb57e0 PP |
599 | goto end; |
600 | ||
601 | error: | |
870631a2 | 602 | BT_ASSERT(status != BT_FUNC_STATUS_OK); |
770538dd | 603 | log_python_traceback(fail_on_load_error ? BT_LOG_WARNING : BT_LOG_INFO); |
55bb57e0 | 604 | pyerr_clear(); |
9736d991 | 605 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_out); |
55bb57e0 PP |
606 | |
607 | end: | |
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); | |
9736d991 | 614 | return status; |
55bb57e0 PP |
615 | } |
616 | ||
6fbd4105 | 617 | G_MODULE_EXPORT |
d24d5663 | 618 | int bt_plugin_python_create_all_from_file(const char *path, |
9736d991 | 619 | bool fail_on_load_error, struct bt_plugin_set **plugin_set_out) |
55bb57e0 | 620 | { |
88b3fc9c | 621 | bt_plugin *plugin = NULL; |
55bb57e0 PP |
622 | PyObject *py_plugin_info = NULL; |
623 | gchar *basename = NULL; | |
624 | size_t path_len; | |
870631a2 | 625 | int status = BT_FUNC_STATUS_OK; |
55bb57e0 | 626 | |
f6ccaed9 | 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 | */ | |
870631a2 PP |
635 | BT_LIB_LOGE_APPEND_CAUSE( |
636 | "Python interpreter could not be initialized previously."); | |
637 | status = BT_FUNC_STATUS_ERROR; | |
9736d991 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 | */ | |
f03cdb2b 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`."); | |
870631a2 | 648 | status = BT_FUNC_STATUS_NOT_FOUND; |
55bb57e0 PP |
649 | goto error; |
650 | } | |
651 | ||
3f7d4d90 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) { | |
3f7d4d90 | 660 | BT_LOGI("Skipping non-Python file: path=\"%s\"", path); |
870631a2 | 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) { | |
870631a2 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) { | |
3f7d4d90 | 676 | BT_LOGI("Skipping Python file not starting with `%s`: " |
9e0bf9b0 | 677 | "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path); |
870631a2 | 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 | */ | |
870631a2 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) { | |
870631a2 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); | |
b189a968 | 710 | status = BT_FUNC_STATUS_ERROR; |
870631a2 | 711 | } else { |
a0cdfce8 | 712 | BT_LIB_LOGW( |
870631a2 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 | */ |
9736d991 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 | */ | |
870631a2 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); | |
9736d991 PP |
735 | BT_ASSERT(!plugin); |
736 | goto error; | |
870631a2 | 737 | } else if (status == BT_FUNC_STATUS_NOT_FOUND) { |
9736d991 | 738 | BT_ASSERT(!plugin); |
55bb57e0 PP |
739 | goto error; |
740 | } | |
741 | ||
870631a2 | 742 | BT_ASSERT(status == BT_FUNC_STATUS_OK); |
9736d991 | 743 | BT_ASSERT(plugin); |
a8ff38ef | 744 | bt_plugin_set_path(plugin, path); |
9736d991 PP |
745 | *plugin_set_out = bt_plugin_set_create(); |
746 | if (!*plugin_set_out) { | |
870631a2 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 | ||
9736d991 | 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 | ||
758 | error: | |
870631a2 | 759 | BT_ASSERT(status != BT_FUNC_STATUS_OK); |
a0cdfce8 | 760 | log_python_traceback(BT_LOG_WARNING); |
870631a2 | 761 | pyerr_clear(); |
9736d991 | 762 | BT_OBJECT_PUT_REF_AND_RESET(*plugin_set_out); |
55bb57e0 PP |
763 | |
764 | end: | |
c5b9b441 | 765 | bt_plugin_put_ref(plugin); |
55bb57e0 | 766 | Py_XDECREF(py_plugin_info); |
870631a2 | 767 | |
19bbdc9b | 768 | g_free(basename); |
870631a2 | 769 | |
9736d991 | 770 | return status; |
55bb57e0 | 771 | } |