test_ctf_writer.c: add "trace is static" listener tests
[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
27#include <babeltrace/babeltrace-internal.h>
3d9990ac 28#include <babeltrace/compiler-internal.h>
55bb57e0
PP
29#include <babeltrace/ref.h>
30#include <babeltrace/plugin/plugin-internal.h>
55bb57e0
PP
31#include <stdlib.h>
32#include <signal.h>
33#include <Python.h>
6fbd4105
PP
34#include <glib.h>
35#include <gmodule.h>
55bb57e0
PP
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
6fbd4105 347G_MODULE_EXPORT
a8ff38ef 348struct bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
55bb57e0 349{
a8ff38ef
PP
350 struct bt_plugin_set *plugin_set = NULL;
351 struct bt_plugin *plugin = NULL;
55bb57e0
PP
352 PyObject *py_plugin_info = NULL;
353 gchar *basename = NULL;
354 size_t path_len;
355
356 assert(path);
357
358 if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
359 /*
360 * We do not even care about the rest of the function
361 * here because we already know Python cannot be fully
362 * initialized.
363 */
364 goto error;
365 }
366
367 path_len = strlen(path);
368
369 /* File name ends with `.py` */
370 if (strncmp(path + path_len - PYTHON_PLUGIN_FILE_EXT_LEN,
371 PYTHON_PLUGIN_FILE_EXT,
372 PYTHON_PLUGIN_FILE_EXT_LEN) != 0) {
373 printf_verbose("Skipping non-Python file: `%s`\n",
374 path);
375 goto error;
376 }
377
378 /* File name starts with `bt_plugin_` */
379 basename = g_path_get_basename(path);
380 if (!basename) {
381 goto error;
382 }
383
384 if (strncmp(basename, PYTHON_PLUGIN_FILE_PREFIX,
385 PYTHON_PLUGIN_FILE_PREFIX_LEN) != 0) {
386 printf_verbose("Skipping Python file not starting with `%s`: `%s`\n",
387 PYTHON_PLUGIN_FILE_PREFIX, path);
388 goto error;
389 }
390
391 /*
392 * Initialize Python now.
393 *
394 * This is not done in the library contructor because the
395 * interpreter is somewhat slow to initialize. If you don't
396 * have any potential Python plugins, you don't need to endure
397 * this waiting time everytime you load the library.
398 */
399 init_python();
400 if (python_state != PYTHON_STATE_FULLY_INITIALIZED) {
401 /*
402 * For some reason we cannot initialize Python,
403 * import the required modules, and get the required
404 * attributes from them.
405 */
406 goto error;
407 }
408
409 /*
410 * Call bt2.py_plugin._try_load_plugin_module() with this path
411 * to get plugin info if the plugin is loadable and complete.
412 * This function returns None when there's an error, but just in
413 * case we also manually clear the last Python error state.
414 */
415 py_plugin_info = PyObject_CallFunction(py_try_load_plugin_module_func,
416 "(s)", path);
417 if (!py_plugin_info || py_plugin_info == Py_None) {
418 printf_verbose("Cannot load Python plugin `%s`:\n", path);
419 print_python_traceback_verbose();
420 PyErr_Clear();
421 goto error;
422 }
423
424 /*
425 * Get bt_plugin from plugin info object.
55bb57e0 426 */
a8ff38ef
PP
427 plugin = bt_plugin_from_python_plugin_info(py_plugin_info);
428 if (!plugin) {
55bb57e0
PP
429 goto error;
430 }
431
a8ff38ef
PP
432 bt_plugin_set_path(plugin, path);
433 plugin_set = bt_plugin_set_create();
434 if (!plugin_set) {
55bb57e0
PP
435 goto error;
436 }
437
a8ff38ef 438 bt_plugin_set_add_plugin(plugin_set, plugin);
55bb57e0
PP
439 goto end;
440
441error:
a8ff38ef 442 BT_PUT(plugin_set);
55bb57e0
PP
443
444end:
a8ff38ef 445 bt_put(plugin);
55bb57e0
PP
446 Py_XDECREF(py_plugin_info);
447 g_free(basename);
a8ff38ef 448 return plugin_set;
55bb57e0 449}
This page took 0.042386 seconds and 4 git commands to generate.