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