Logging: standardize logging tags
[babeltrace.git] / src / python-plugin-provider / python-plugin-provider.c
1 /*
2 * python-plugin-provider.c
3 *
4 * Babeltrace Python plugin provider
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
27 #define BT_LOG_TAG "LIB/PLUGIN-PY"
28
29 #include "lib/lib-logging.h"
30 #include "common/macros.h"
31 #include "compat/compiler.h"
32 #include <babeltrace2/plugin/plugin-const.h>
33 #include "lib/plugin/plugin.h"
34 #include <babeltrace2/graph/component-class.h>
35 #include "lib/graph/component-class.h"
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <Python.h>
39 #include <glib.h>
40 #include <gmodule.h>
41
42 #define PYTHON_PLUGIN_FILE_PREFIX "bt_plugin_"
43 #define PYTHON_PLUGIN_FILE_PREFIX_LEN (sizeof(PYTHON_PLUGIN_FILE_PREFIX) - 1)
44 #define PYTHON_PLUGIN_FILE_EXT ".py"
45 #define PYTHON_PLUGIN_FILE_EXT_LEN (sizeof(PYTHON_PLUGIN_FILE_EXT) - 1)
46
47 enum python_state {
48 /* init_python() not called yet */
49 PYTHON_STATE_NOT_INITED,
50
51 /* init_python() called once with success */
52 PYTHON_STATE_FULLY_INITIALIZED,
53
54 /* init_python() called once without success */
55 PYTHON_STATE_CANNOT_INITIALIZE,
56 } python_state = PYTHON_STATE_NOT_INITED;
57
58 static PyObject *py_try_load_plugin_module_func = NULL;
59 static bool python_was_initialized_by_us;
60
61 static
62 void print_python_traceback_warn(void)
63 {
64 if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) {
65 BT_LOGW_STR("Exception occured: traceback: ");
66 PyErr_Print();
67 }
68 }
69
70 static
71 void pyerr_clear(void)
72 {
73 if (Py_IsInitialized()) {
74 PyErr_Clear();
75 }
76 }
77
78 static
79 void init_python(void)
80 {
81 PyObject *py_bt2_py_plugin_mod = NULL;
82 const char *dis_python_env;
83 #ifndef __MINGW32__
84 sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
85 #endif
86
87 if (python_state != PYTHON_STATE_NOT_INITED) {
88 goto end;
89 }
90
91 /*
92 * User can disable Python plugin support with the
93 * BABELTRACE_DISABLE_PYTHON_PLUGINS environment variable set to
94 * 1.
95 */
96 dis_python_env = getenv("BABELTRACE_DISABLE_PYTHON_PLUGINS");
97 if (dis_python_env && strcmp(dis_python_env, "1") == 0) {
98 BT_LOGI_STR("Python plugin support is disabled because `BABELTRACE_DISABLE_PYTHON_PLUGINS=1`.");
99 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
100 goto end;
101 }
102
103 if (!Py_IsInitialized()) {
104 BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
105 Py_InitializeEx(0);
106 python_was_initialized_by_us = true;
107 BT_LOGI("Initialized Python interpreter: version=\"%s\"",
108 Py_GetVersion());
109 } else {
110 BT_LOGI("Python interpreter is already initialized: version=\"%s\"",
111 Py_GetVersion());
112 }
113
114 py_bt2_py_plugin_mod = PyImport_ImportModule("bt2.py_plugin");
115 if (!py_bt2_py_plugin_mod) {
116 BT_LOGI_STR("Cannot import bt2.py_plugin Python module: Python plugin support is disabled.");
117 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
118 goto end;
119 }
120
121 py_try_load_plugin_module_func =
122 PyObject_GetAttrString(py_bt2_py_plugin_mod, "_try_load_plugin_module");
123 if (!py_try_load_plugin_module_func) {
124 BT_LOGI_STR("Cannot get _try_load_plugin_module attribute from bt2.py_plugin Python module: Python plugin support is disabled.");
125 python_state = PYTHON_STATE_CANNOT_INITIALIZE;
126 goto end;
127 }
128
129 python_state = PYTHON_STATE_FULLY_INITIALIZED;
130
131 end:
132 #ifndef __MINGW32__
133 if (old_sigint != SIG_ERR) {
134 (void) signal(SIGINT, old_sigint);
135 }
136 #endif
137
138 print_python_traceback_warn();
139 pyerr_clear();
140 Py_XDECREF(py_bt2_py_plugin_mod);
141 return;
142 }
143
144 __attribute__((destructor)) static
145 void fini_python(void) {
146 if (Py_IsInitialized() && python_was_initialized_by_us) {
147 if (py_try_load_plugin_module_func) {
148 Py_DECREF(py_try_load_plugin_module_func);
149 py_try_load_plugin_module_func = NULL;
150 }
151
152 Py_Finalize();
153 BT_LOGI_STR("Finalized Python interpreter.");
154 }
155
156 python_state = PYTHON_STATE_NOT_INITED;
157 }
158
159 static
160 bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
161 {
162 bt_plugin *plugin = NULL;
163 PyObject *py_name = NULL;
164 PyObject *py_author = NULL;
165 PyObject *py_description = NULL;
166 PyObject *py_license = NULL;
167 PyObject *py_version = NULL;
168 PyObject *py_comp_class_addrs = NULL;
169 const char *name = NULL;
170 const char *author = NULL;
171 const char *description = NULL;
172 const char *license = NULL;
173 unsigned int major = 0, minor = 0, patch = 0;
174 const char *version_extra = NULL;
175 int ret;
176
177 BT_ASSERT(plugin_info);
178 BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
179 py_name = PyObject_GetAttrString(plugin_info, "name");
180 if (!py_name) {
181 BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
182 "py-plugin-info-addr=%p", plugin_info);
183 goto error;
184 }
185
186 py_author = PyObject_GetAttrString(plugin_info, "author");
187 if (!py_author) {
188 BT_LOGW("Cannot find `author` attribute in Python plugin info object: "
189 "py-plugin-info-addr=%p", plugin_info);
190 goto error;
191 }
192
193 py_description = PyObject_GetAttrString(plugin_info, "description");
194 if (!py_description) {
195 BT_LOGW("Cannot find `desciption` attribute in Python plugin info object: "
196 "py-plugin-info-addr=%p", plugin_info);
197 goto error;
198 }
199
200 py_license = PyObject_GetAttrString(plugin_info, "license");
201 if (!py_license) {
202 BT_LOGW("Cannot find `license` attribute in Python plugin info object: "
203 "py-plugin-info-addr=%p", plugin_info);
204 goto error;
205 }
206
207 py_version = PyObject_GetAttrString(plugin_info, "version");
208 if (!py_version) {
209 BT_LOGW("Cannot find `version` attribute in Python plugin info object: "
210 "py-plugin-info-addr=%p", plugin_info);
211 goto error;
212 }
213
214 py_comp_class_addrs = PyObject_GetAttrString(plugin_info,
215 "comp_class_addrs");
216 if (!py_comp_class_addrs) {
217 BT_LOGW("Cannot find `comp_class_addrs` attribute in Python plugin info object: "
218 "py-plugin-info-addr=%p", plugin_info);
219 goto error;
220 }
221
222 if (PyUnicode_Check(py_name)) {
223 name = PyUnicode_AsUTF8(py_name);
224 if (!name) {
225 BT_LOGW("Cannot decode Python plugin name string: "
226 "py-plugin-info-addr=%p", plugin_info);
227 goto error;
228 }
229 } else {
230 /* Plugin name is mandatory */
231 BT_LOGW("Plugin name is not a string: "
232 "py-plugin-info-addr=%p", plugin_info);
233 goto error;
234 }
235
236 if (PyUnicode_Check(py_author)) {
237 author = PyUnicode_AsUTF8(py_author);
238 if (!author) {
239 BT_LOGW("Cannot decode Python plugin author string: "
240 "py-plugin-info-addr=%p", plugin_info);
241 goto error;
242 }
243 }
244
245 if (PyUnicode_Check(py_description)) {
246 description = PyUnicode_AsUTF8(py_description);
247 if (!description) {
248 BT_LOGW("Cannot decode Python plugin description string: "
249 "py-plugin-info-addr=%p", plugin_info);
250 goto error;
251 }
252 }
253
254 if (PyUnicode_Check(py_license)) {
255 license = PyUnicode_AsUTF8(py_license);
256 if (!license) {
257 BT_LOGW("Cannot decode Python plugin license string: "
258 "py-plugin-info-addr=%p", plugin_info);
259 goto error;
260 }
261 }
262
263 if (PyTuple_Check(py_version)) {
264 if (PyTuple_Size(py_version) >= 3) {
265 PyObject *py_major = PyTuple_GetItem(py_version, 0);
266 PyObject *py_minor = PyTuple_GetItem(py_version, 1);
267 PyObject *py_patch = PyTuple_GetItem(py_version, 2);
268
269 BT_ASSERT(py_major);
270 BT_ASSERT(py_minor);
271 BT_ASSERT(py_patch);
272
273 if (PyLong_Check(py_major)) {
274 major = PyLong_AsUnsignedLong(py_major);
275 }
276
277 if (PyLong_Check(py_minor)) {
278 minor = PyLong_AsUnsignedLong(py_minor);
279 }
280
281 if (PyLong_Check(py_patch)) {
282 patch = PyLong_AsUnsignedLong(py_patch);
283 }
284
285 if (PyErr_Occurred()) {
286 /* Overflow error, most probably */
287 BT_LOGW("Invalid Python plugin version format: "
288 "py-plugin-info-addr=%p", plugin_info);
289 goto error;
290 }
291 }
292
293 if (PyTuple_Size(py_version) >= 4) {
294 PyObject *py_extra = PyTuple_GetItem(py_version, 3);
295
296 BT_ASSERT(py_extra);
297
298 if (PyUnicode_Check(py_extra)) {
299 version_extra = PyUnicode_AsUTF8(py_extra);
300 if (!version_extra) {
301 BT_LOGW("Cannot decode Python plugin version's extra string: "
302 "py-plugin-info-addr=%p", plugin_info);
303 goto error;
304 }
305 }
306 }
307 }
308
309 plugin = bt_plugin_create_empty(BT_PLUGIN_TYPE_PYTHON);
310 if (!plugin) {
311 BT_LOGE_STR("Cannot create empty plugin object.");
312 goto error;
313 }
314
315 bt_plugin_set_name(plugin, name);
316
317 if (description) {
318 bt_plugin_set_description(plugin, description);
319 }
320
321 if (author) {
322 bt_plugin_set_author(plugin, author);
323 }
324
325 if (license) {
326 bt_plugin_set_license(plugin, license);
327 }
328
329 bt_plugin_set_version(plugin, major, minor, patch, version_extra);
330
331 if (PyList_Check(py_comp_class_addrs)) {
332 size_t i;
333
334 for (i = 0; i < PyList_Size(py_comp_class_addrs); i++) {
335 bt_component_class *comp_class;
336 PyObject *py_comp_class_addr;
337
338 py_comp_class_addr =
339 PyList_GetItem(py_comp_class_addrs, i);
340 BT_ASSERT(py_comp_class_addr);
341 if (PyLong_Check(py_comp_class_addr)) {
342 comp_class = PyLong_AsVoidPtr(py_comp_class_addr);
343 } else {
344 BT_LOGW("Component class address is not an integer in Python plugin info object: "
345 "py-plugin-info-addr=%p, index=%zu",
346 plugin_info, i);
347 continue;
348 }
349
350 ret = bt_plugin_add_component_class(plugin, comp_class);
351 if (ret < 0) {
352 BT_LOGE("Cannot add component class to plugin: "
353 "py-plugin-info-addr=%p, "
354 "plugin-addr=%p, plugin-name=\"%s\", "
355 "comp-class-addr=%p, "
356 "comp-class-name=\"%s\", "
357 "comp-class-type=%s",
358 plugin_info,
359 plugin, bt_plugin_get_name(plugin),
360 comp_class,
361 bt_component_class_get_name(comp_class),
362 bt_component_class_type_string(
363 bt_component_class_get_type(comp_class)));
364 continue;
365 }
366 }
367 }
368
369 goto end;
370
371 error:
372 print_python_traceback_warn();
373 pyerr_clear();
374 BT_OBJECT_PUT_REF_AND_RESET(plugin);
375
376 end:
377 Py_XDECREF(py_name);
378 Py_XDECREF(py_author);
379 Py_XDECREF(py_description);
380 Py_XDECREF(py_license);
381 Py_XDECREF(py_version);
382 Py_XDECREF(py_comp_class_addrs);
383 return plugin;
384 }
385
386 G_MODULE_EXPORT
387 bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
388 {
389 bt_plugin_set *plugin_set = NULL;
390 bt_plugin *plugin = NULL;
391 PyObject *py_plugin_info = NULL;
392 gchar *basename = NULL;
393 size_t path_len;
394
395 BT_ASSERT(path);
396
397 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
398 /*
399 * We do not even care about the rest of the function
400 * here because we already know Python cannot be fully
401 * initialized.
402 */
403 goto error;
404 }
405
406 BT_LOGI("Trying to create all Python plugins from file: path=\"%s\"",
407 path);
408 path_len = strlen(path);
409
410 /* File name ends with `.py` */
411 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
412 PYTHON_PLUGIN_FILE_EXT,
413 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
414 BT_LOGI("Skipping non-Python file: path=\"%s\"", path);
415 goto error;
416 }
417
418 /* File name starts with `bt_plugin_` */
419 basename = g_path_get_basename(path);
420 if (!basename) {
421 BT_LOGW("Cannot get path's basename: path=\"%s\"", path);
422 goto error;
423 }
424
425 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
426 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
427 BT_LOGI("Skipping Python file not starting with `%s`: "
428 "path=\"%s\"", PYTHON_PLUGIN_FILE_PREFIX, path);
429 goto error;
430 }
431
432 /*
433 * Initialize Python now.
434 *
435 * This is not done in the library contructor because the
436 * interpreter is somewhat slow to initialize. If you don't
437 * have any potential Python plugins, you don't need to endure
438 * this waiting time everytime you load the library.
439 */
440 init_python();
441 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
442 /*
443 * For some reason we cannot initialize Python,
444 * import the required modules, and get the required
445 * attributes from them.
446 */
447 BT_LOGI("Failed to initialize Python interpreter.");
448 goto error;
449 }
450
451 /*
452 * Call bt2.py_plugin._try_load_plugin_module() with this path
453 * to get plugin info if the plugin is loadable and complete.
454 * This function returns None when there's an error, but just in
455 * case we also manually clear the last Python error state.
456 */
457 BT_LOGD_STR("Getting Python plugin info object from Python module.");
458 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
459 "(s)", path);
460 if (!py_plugin_info || py_plugin_info == Py_None) {
461 BT_LOGW("Cannot load Python plugin: path=\"%s\"", path);
462 print_python_traceback_warn();
463 PyErr_Clear();
464 goto error;
465 }
466
467 /*
468 * Get bt_plugin from plugin info object.
469 */
470 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
471 if (!plugin) {
472 BT_LOGW("Cannot create plugin object from Python plugin info object: "
473 "path=\"%s\", py-plugin-info-addr=%p",
474 path, py_plugin_info);
475 goto error;
476 }
477
478 bt_plugin_set_path(plugin, path);
479 plugin_set = bt_plugin_set_create();
480 if (!plugin_set) {
481 BT_LOGE_STR("Cannot create empty plugin set.");
482 goto error;
483 }
484
485 bt_plugin_set_add_plugin(plugin_set, plugin);
486 BT_LOGD("Created all Python plugins from file: path=\"%s\", "
487 "plugin-addr=%p, plugin-name=\"%s\"",
488 path, plugin, bt_plugin_get_name(plugin));
489 goto end;
490
491 error:
492 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
493
494 end:
495 bt_plugin_put_ref(plugin);
496 Py_XDECREF(py_plugin_info);
497 g_free(basename);
498 return plugin_set;
499 }
This page took 0.039266 seconds and 4 git commands to generate.