-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_python.c
97 lines (76 loc) · 1.84 KB
/
plugin_python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <Python.h>
#include <stdio.h>
#include "trace-cmd.h"
#ifndef PYTHON_DIR
#define PYTHON_DIR "."
#endif
static const char pypath[] =
"import sys\n"
"sys.path.append(\"" PYTHON_DIR "\")\n";
static const char pyload[] =
"import imp, tracecmd, ctracecmd\n"
"fn = r'%s'\n"
"file = open(fn, 'r')\n"
"try:\n"
" module = imp.load_source('%s', fn, file)\n"
" module.register(tracecmd.PEvent(ctracecmd.convert_pevent(pevent)))\n"
"finally:\n"
" file.close()\n";
static void load_plugin(struct pevent *pevent, const char *path,
const char *name, void *data)
{
PyObject *globals = data;
int len = strlen(path) + strlen(name) + 2;
int nlen = strlen(name) + 1;
char *full = malloc(len);
char *n = malloc(nlen);
char *load;
PyObject *res;
if (!full || !n)
return;
strcpy(full, path);
strcat(full, "/");
strcat(full, name);
strcpy(n, name);
n[nlen - 4] = '\0';
asprintf(&load, pyload, full, n);
if (!load)
return;
res = PyRun_String(load, Py_file_input, globals, globals);
if (!res) {
fprintf(stderr, "failed loading %s\n", full);
PyErr_Print();
} else
Py_DECREF(res);
free(load);
}
int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
{
PyObject *globals, *m, *py_pevent, *str, *res;
Py_Initialize();
m = PyImport_AddModule("__main__");
globals = PyModule_GetDict(m);
res = PyRun_String(pypath, Py_file_input, globals, globals);
if (!res) {
PyErr_Print();
return -1;
} else
Py_DECREF(res);
str = PyString_FromString("pevent");
if (!str)
return -ENOMEM;
py_pevent = PyLong_FromUnsignedLong((unsigned long)pevent);
if (!py_pevent)
return -ENOMEM;
if (PyDict_SetItem(globals, str, py_pevent))
fprintf(stderr, "failed to insert pevent\n");
Py_DECREF(py_pevent);
Py_DECREF(str);
trace_util_load_plugins(pevent, ".py", load_plugin, globals);
return 0;
}
int PEVENT_PLUGIN_UNLOADER(void)
{
Py_Finalize();
return 0;
}