aboutsummaryrefslogtreecommitdiffstats
path: root/ctracecmd.i
diff options
context:
space:
mode:
Diffstat (limited to 'ctracecmd.i')
-rw-r--r--ctracecmd.i159
1 files changed, 142 insertions, 17 deletions
diff --git a/ctracecmd.i b/ctracecmd.i
index 51e98d5..a0baa3c 100644
--- a/ctracecmd.i
+++ b/ctracecmd.i
@@ -3,13 +3,14 @@
3%include "typemaps.i" 3%include "typemaps.i"
4%include "constraints.i" 4%include "constraints.i"
5 5
6%nodefaultctor record;
7%nodefaultdtor record;
8
6%apply Pointer NONNULL { struct tracecmd_input *handle }; 9%apply Pointer NONNULL { struct tracecmd_input *handle };
7%apply Pointer NONNULL { struct pevent *pevent }; 10%apply Pointer NONNULL { struct pevent *pevent };
8 11%apply Pointer NONNULL { struct format_field * };
9/* return a (rec,cpu) tuple in python */ 12%apply unsigned long long *OUTPUT {unsigned long long *}
10extern struct record *tracecmd_read_at(struct tracecmd_input *handle, 13%apply int *OUTPUT {int *}
11 unsigned long long offset,
12 int *OUTPUT);
13 14
14 15
15%{ 16%{
@@ -17,26 +18,150 @@ extern struct record *tracecmd_read_at(struct tracecmd_input *handle,
17%} 18%}
18 19
19 20
20/* return python longs from unsigned long long functions */ 21%typemap(in) PyObject *pyfunc {
21%typemap(out) unsigned long long { 22 if (!PyCallable_Check($input)) {
22$result = PyLong_FromUnsignedLongLong((unsigned long long) $1); 23 PyErr_SetString(PyExc_TypeError, "Need a callable object!");
24 return NULL;
25 }
26 $1 = $input;
23} 27}
24 28
29%ignore python_callback;
25 30
26%inline %{ 31%inline %{
27PyObject *pevent_read_number_field_py(struct format_field *f, void *data) 32static int python_callback(struct trace_seq *s,
33 struct record *record,
34 struct event_format *event,
35 void *context);
36
37PyObject *convert_pevent(unsigned long pevent)
38{
39 void *pev = (void *)pevent;
40 return SWIG_NewPointerObj(SWIG_as_voidptr(pev), SWIGTYPE_p_pevent, 0);
41}
42
43void py_pevent_register_event_handler(struct pevent *pevent, int id,
44 char *subsys, char *evname,
45 PyObject *pyfunc)
28{ 46{
29 unsigned long long val; 47 Py_INCREF(pyfunc);
30 int ret; 48 pevent_register_event_handler(pevent, id, subsys, evname,
31 49 python_callback, pyfunc);
32 ret = pevent_read_number_field(f, data, &val); 50}
33 if (ret) 51
34 Py_RETURN_NONE; 52static PyObject *py_field_get_data(struct format_field *f, struct record *r)
35 else 53{
36 return PyLong_FromUnsignedLongLong(val); 54 if (!strncmp(f->type, "__data_loc ", 11)) {
55 unsigned long long val;
56 int len, offset;
57
58 if (pevent_read_number_field(f, r->data, &val)) {
59 PyErr_SetString(PyExc_TypeError,
60 "Field is not a valid number");
61 return NULL;
62 }
63
64 /*
65 * The actual length of the dynamic array is stored
66 * in the top half of the field, and the offset
67 * is in the bottom half of the 32 bit field.
68 */
69 offset = val & 0xffff;
70 len = val >> 16;
71
72 return PyBuffer_FromMemory((char *)r->data + offset, len);
73 }
74
75 return PyBuffer_FromMemory((char *)r->data + f->offset, f->size);
76}
77
78static PyObject *py_field_get_str(struct format_field *f, struct record *r)
79{
80 if (!strncmp(f->type, "__data_loc ", 11)) {
81 unsigned long long val;
82 int offset;
83
84 if (pevent_read_number_field(f, r->data, &val)) {
85 PyErr_SetString(PyExc_TypeError,
86 "Field is not a valid number");
87 return NULL;
88 }
89
90 /*
91 * The actual length of the dynamic array is stored
92 * in the top half of the field, and the offset
93 * is in the bottom half of the 32 bit field.
94 */
95 offset = val & 0xffff;
96
97 return PyString_FromString((char *)r->data + offset);
98 }
99
100 return PyString_FromStringAndSize((char *)r->data + f->offset,
101 strnlen((char *)r->data + f->offset, f->size));
102}
103
104static PyObject *py_format_get_keys(struct event_format *ef)
105{
106 PyObject *list;
107 struct format_field *f;
108
109 list = PyList_New(0);
110
111 for (f = ef->format.fields; f; f = f->next) {
112 if (PyList_Append(list, PyString_FromString(f->name))) {
113 Py_DECREF(list);
114 return NULL;
115 }
116 }
117
118 return list;
119}
120%}
121
122
123%wrapper %{
124static int python_callback(struct trace_seq *s,
125 struct record *record,
126 struct event_format *event,
127 void *context)
128{
129 PyObject *arglist, *result;
130 int r = 0;
131
132 record->ref_count++;
133
134 arglist = Py_BuildValue("(OOO)",
135 SWIG_NewPointerObj(SWIG_as_voidptr(s),
136 SWIGTYPE_p_trace_seq, 0),
137 SWIG_NewPointerObj(SWIG_as_voidptr(record),
138 SWIGTYPE_p_record, 0),
139 SWIG_NewPointerObj(SWIG_as_voidptr(event),
140 SWIGTYPE_p_event_format, 0));
141
142 result = PyEval_CallObject(context, arglist);
143 Py_XDECREF(arglist);
144 if (result && result != Py_None) {
145 if (!PyInt_Check(result)) {
146 PyErr_SetString(PyExc_TypeError,
147 "callback must return int");
148 PyErr_Print();
149 Py_XDECREF(result);
150 return 0;
151 }
152 r = PyInt_AS_LONG(result);
153 } else if (result == Py_None)
154 r = 0;
155 else
156 PyErr_Print();
157
158 Py_XDECREF(result);
159
160 return r;
37} 161}
38%} 162%}
39 163
164
40%ignore trace_seq_vprintf; 165%ignore trace_seq_vprintf;
41 166
42/* SWIG can't grok these, define them to nothing */ 167/* SWIG can't grok these, define them to nothing */