aboutsummaryrefslogtreecommitdiff
path: root/python/ctracecmd.i
blob: 6d0179e33afc00d568f0f029e7dbfdc519f49046 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// tracecmd.i
%module ctracecmd
%include "typemaps.i"
%include "constraints.i"

%nodefaultctor record;
%nodefaultdtor record;

%apply Pointer NONNULL { struct tracecmd_input *handle };
%apply Pointer NONNULL { struct tep_handle *pevent };
%apply Pointer NONNULL { struct tep_format_field * };
%apply unsigned long long *OUTPUT {unsigned long long *}
%apply int *OUTPUT {int *}


%{
#include "trace-cmd.h"
#include "event-parse.h"
#include "event-utils.h"
#include <Python.h>
%}


%typemap(in) PyObject *pyfunc {
	if (!PyCallable_Check($input)) {
		PyErr_SetString(PyExc_TypeError, "Need a callable object!");
		return NULL;
	}
	$1 = $input;
}

%ignore python_callback;

%inline %{
static int python_callback(struct trace_seq *s,
			   struct tep_record *record,
			   struct tep_event *event,
			   void *context);

static int skip_output = 0;

static void py_supress_trace_output(void)
{
	skip_output = 1;
}

void warning(const char *fmt, ...)
{
	va_list ap;

	if (skip_output)
		return;

	va_start(ap, fmt);
	tep_vprint("tracecmd", TEP_LOG_WARNING, true, fmt, ap);
	va_end(ap);
}

PyObject *convert_pevent(unsigned long pevent)
{
	void *pev = (void *)pevent;
	return SWIG_NewPointerObj(SWIG_as_voidptr(pev), SWIGTYPE_p_tep_handle, 0);
}

void py_pevent_register_event_handler(struct tep_handle *pevent, int id,
				      char *subsys, char *evname,
				      PyObject *pyfunc)
{
	Py_INCREF(pyfunc);
	tep_register_event_handler(pevent, id, subsys, evname,
				   python_callback, pyfunc);
}

static PyObject *py_field_get_stack(struct tep_handle *pevent,
				    struct tep_record *record,
				    struct tep_event *event,
				    int long_size)
{
	PyObject *list;
	struct tep_format_field *field;
	void *data = record->data;
	const char *func = NULL;
	unsigned long addr;

	field = tep_find_any_field(event, "caller");
	if (!field) {
		PyErr_SetString(PyExc_TypeError,
				"Event doesn't have caller field");
		return NULL;
	}

	list = PyList_New(0);

	for (data += field->offset; data < record->data + record->size;
	     data += long_size) {
		addr = tep_read_number(event->tep, data, long_size);

		if ((long_size == 8 && addr == (unsigned long long)-1) ||
		    ((int)addr == -1))
			break;
		func = tep_find_function(event->tep, addr);
		if (PyList_Append(list, PyUnicode_FromString(func))) {
			Py_DECREF(list);
			return NULL;
		}
	}

	return list;
}

#if PY_MAJOR_VERSION >= 3
static PyObject *fromMemory(void *buf, size_t len)
{
		return PyMemoryView_FromMemory(buf, len, PyBUF_READ);
}
#define PY_INT_AS_LONG PyLong_AsLong
#else
static PyObject *fromMemory(void *buf, size_t len)
{
		return PyBuffer_FromMemory(buf, len);
}
#define PY_INT_AS_LONG PyInt_AS_LONG
#endif



static PyObject *py_field_get_data(struct tep_format_field *f, struct tep_record *r)
{
	if (!strncmp(f->type, "__data_loc ", 11)) {
		unsigned long long val;
		int len, offset;

		if (tep_read_number_field(f, r->data, &val)) {
			PyErr_SetString(PyExc_TypeError,
					"Field is not a valid number");
			return NULL;
		}

		/*
		 * The actual length of the dynamic array is stored
		 * in the top half of the field, and the offset
		 * is in the bottom half of the 32 bit field.
		 */
		offset = val & 0xffff;
		len = val >> 16;

		return fromMemory(r->data + offset, len);
	}

	return fromMemory(r->data + f->offset, f->size);
}

static PyObject *py_field_get_str(struct tep_format_field *f, struct tep_record *r)
{
	if (!strncmp(f->type, "__data_loc ", 11)) {
		unsigned long long val;
		int offset;

		if (tep_read_number_field(f, r->data, &val)) {
			PyErr_SetString(PyExc_TypeError,
					"Field is not a valid number");
			return NULL;
		}

		/*
		 * The actual length of the dynamic array is stored
		 * in the top half of the field, and the offset
		 * is in the bottom half of the 32 bit field.
		 */
		offset = val & 0xffff;

		return PyUnicode_FromString((char *)r->data + offset);
	}

	return PyUnicode_FromStringAndSize((char *)r->data + f->offset,
				strnlen((char *)r->data + f->offset, f->size));
}

static PyObject *py_format_get_keys(struct tep_event *ef)
{
	PyObject *list;
	struct tep_format_field *f;

	list = PyList_New(0);

	for (f = ef->format.fields; f; f = f->next) {
		if (PyList_Append(list, PyUnicode_FromString(f->name))) {
			Py_DECREF(list);
			return NULL;
		}
	}

	return list;
}
%}


%wrapper %{
static int python_callback(struct trace_seq *s,
			   struct tep_record *record,
			   struct tep_event *event,
			   void *context)
{
	PyObject *arglist, *result;
	int r = 0;

	record->ref_count++;

	arglist = Py_BuildValue("(OOO)",
		SWIG_NewPointerObj(SWIG_as_voidptr(s),
				   SWIGTYPE_p_trace_seq, 0),
		SWIG_NewPointerObj(SWIG_as_voidptr(record),
				   SWIGTYPE_p_tep_record, 0),
		SWIG_NewPointerObj(SWIG_as_voidptr(event),
				   SWIGTYPE_p_tep_event, 0));

	result = PyEval_CallObject(context, arglist);
	Py_XDECREF(arglist);
	if (result && result != Py_None) {
		if (!PyInt_Check(result)) {
			PyErr_SetString(PyExc_TypeError,
					"callback must return int");
			PyErr_Print();
			Py_XDECREF(result);
			return 0;
		}
		r = PY_INT_AS_LONG(result);
	} else if (result == Py_None)
		r = 0;
	else
		PyErr_Print();

	Py_XDECREF(result);

	return r;
}
%}


%ignore trace_seq_vprintf;
%ignore vpr_stat;

/* SWIG can't grok these, define them to nothing */
#define __trace
#define __attribute__(x)
#define __thread

%include "trace-cmd.h"
%include <trace-seq.h>
%include <event-parse.h>