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