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