- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin_python.c
More file actions
Latest commit
104 lines (82 loc) · 2.03 KB
/
Copy pathplugin_python.c
File metadata and controls
104 lines (82 loc) · 2.03 KB
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
98
99
100
101
102
103
104
#include<Python.h>
#include<stdio.h>
#include"trace-cmd.h"
#ifndefPYTHON_DIR
#definePYTHON_DIR "."
#endif
staticconstcharpypath[] =
"import sys\n"
"sys.path.append(\""PYTHON_DIR"\")\n";
staticconstcharpyload[] =
"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";
staticvoidload_plugin(structpevent*pevent, constchar*path,
constchar*name, void*data)
{
PyObject*globals=data;
intlen=strlen(path) +strlen(name) +2;
intnlen=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);
}
intPEVENT_PLUGIN_LOADER(structpevent*pevent)
{
PyObject*globals, *m, *py_pevent, *str, *res;
char**plugin_list;
/* Only load plugins if they exist */
plugin_list=trace_util_find_plugin_files(".py");
if (!plugin_list)
return0;
trace_util_free_plugin_files(plugin_list);
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);
return0;
}
intPEVENT_PLUGIN_UNLOADER(void)
{
Py_Finalize();
return0;
}