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