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