blob: b6c1a69f2b188248e940404718a77c16f44d57f1 [file] [log] [blame]
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001/*
2 * trace-event-python. Feed trace events to an embedded Python interpreter.
3 *
4 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <Python.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060027#include <errno.h>
28
29#include "../../perf.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -030030#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060031#include "../util.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020032#include "../event.h"
33#include "../thread.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060034#include "../trace-event.h"
35
36PyMODINIT_FUNC initperf_trace_context(void);
37
38#define FTRACE_MAX_EVENT \
39 ((1 << (sizeof(unsigned short) * 8)) - 1)
40
Steven Rostedtaaf045f2012-04-06 00:47:56 +020041struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060042
43#define MAX_FIELDS 64
44#define N_COMMON_FIELDS 7
45
46extern struct scripting_context *scripting_context;
47
48static char *cur_field_name;
49static int zero_flag_atom;
50
51static PyObject *main_module, *main_dict;
52
Joseph Schuchart05f832e2014-07-09 16:16:31 +020053static void handler_call_die(const char *handler_name) NORETURN;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060054static void handler_call_die(const char *handler_name)
55{
56 PyErr_Print();
57 Py_FatalError("problem in Python trace event handler");
Joseph Schuchart05f832e2014-07-09 16:16:31 +020058 // Py_FatalError does not return
59 // but we have to make the compiler happy
60 abort();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060061}
62
Joseph Schuchartc0268e82013-10-24 10:10:51 -030063/*
64 * Insert val into into the dictionary and decrement the reference counter.
65 * This is necessary for dictionaries since PyDict_SetItemString() does not
66 * steal a reference, as opposed to PyTuple_SetItem().
67 */
68static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
69{
70 PyDict_SetItemString(dict, key, val);
71 Py_DECREF(val);
72}
73
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060074static void define_value(enum print_arg_type field_type,
75 const char *ev_name,
76 const char *field_name,
77 const char *field_value,
78 const char *field_str)
79{
80 const char *handler_name = "define_flag_value";
81 PyObject *handler, *t, *retval;
82 unsigned long long value;
83 unsigned n = 0;
84
85 if (field_type == PRINT_SYMBOL)
86 handler_name = "define_symbolic_value";
87
Tom Zanussi44ad9cd2010-02-22 01:12:59 -060088 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060089 if (!t)
90 Py_FatalError("couldn't create Python tuple");
91
92 value = eval_flag(field_value);
93
94 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
95 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
96 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
97 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
98
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060099 handler = PyDict_GetItemString(main_dict, handler_name);
100 if (handler && PyCallable_Check(handler)) {
101 retval = PyObject_CallObject(handler, t);
102 if (retval == NULL)
103 handler_call_die(handler_name);
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200104 Py_DECREF(retval);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600105 }
106
107 Py_DECREF(t);
108}
109
110static void define_values(enum print_arg_type field_type,
111 struct print_flag_sym *field,
112 const char *ev_name,
113 const char *field_name)
114{
115 define_value(field_type, ev_name, field_name, field->value,
116 field->str);
117
118 if (field->next)
119 define_values(field_type, field->next, ev_name, field_name);
120}
121
122static void define_field(enum print_arg_type field_type,
123 const char *ev_name,
124 const char *field_name,
125 const char *delim)
126{
127 const char *handler_name = "define_flag_field";
128 PyObject *handler, *t, *retval;
129 unsigned n = 0;
130
131 if (field_type == PRINT_SYMBOL)
132 handler_name = "define_symbolic_field";
133
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600134 if (field_type == PRINT_FLAGS)
135 t = PyTuple_New(3);
136 else
137 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600138 if (!t)
139 Py_FatalError("couldn't create Python tuple");
140
141 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
142 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
143 if (field_type == PRINT_FLAGS)
144 PyTuple_SetItem(t, n++, PyString_FromString(delim));
145
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600146 handler = PyDict_GetItemString(main_dict, handler_name);
147 if (handler && PyCallable_Check(handler)) {
148 retval = PyObject_CallObject(handler, t);
149 if (retval == NULL)
150 handler_call_die(handler_name);
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200151 Py_DECREF(retval);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600152 }
153
154 Py_DECREF(t);
155}
156
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200157static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600158 const char *ev_name,
159 struct print_arg *args)
160{
161 switch (args->type) {
162 case PRINT_NULL:
163 break;
164 case PRINT_ATOM:
165 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
166 args->atom.atom);
167 zero_flag_atom = 0;
168 break;
169 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300170 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600171 cur_field_name = strdup(args->field.name);
172 break;
173 case PRINT_FLAGS:
174 define_event_symbols(event, ev_name, args->flags.field);
175 define_field(PRINT_FLAGS, ev_name, cur_field_name,
176 args->flags.delim);
177 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
178 cur_field_name);
179 break;
180 case PRINT_SYMBOL:
181 define_event_symbols(event, ev_name, args->symbol.field);
182 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
183 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
184 cur_field_name);
185 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900186 case PRINT_HEX:
187 define_event_symbols(event, ev_name, args->hex.field);
188 define_event_symbols(event, ev_name, args->hex.size);
189 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600190 case PRINT_STRING:
191 break;
192 case PRINT_TYPE:
193 define_event_symbols(event, ev_name, args->typecast.item);
194 break;
195 case PRINT_OP:
196 if (strcmp(args->op.op, ":") == 0)
197 zero_flag_atom = 1;
198 define_event_symbols(event, ev_name, args->op.left);
199 define_event_symbols(event, ev_name, args->op.right);
200 break;
201 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200202 /* gcc warns for these? */
203 case PRINT_BSTRING:
204 case PRINT_DYNAMIC_ARRAY:
205 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400206 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600207 /* we should warn... */
208 return;
209 }
210
211 if (args->next)
212 define_event_symbols(event, ev_name, args->next);
213}
214
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300215static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600216{
217 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200218 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300219 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600220
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300221 /*
222 * XXX: Do we really need to cache this since now we have evsel->tp_format
223 * cached already? Need to re-read this "cache" routine that as well calls
224 * define_event_symbols() :-\
225 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600226 if (events[type])
227 return events[type];
228
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300229 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600230 if (!event)
231 return NULL;
232
233 sprintf(ev_name, "%s__%s", event->system, event->name);
234
235 define_event_symbols(event, ev_name, event->print_fmt.args);
236
237 return event;
238}
239
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200240static PyObject *get_field_numeric_entry(struct event_format *event,
241 struct format_field *field, void *data)
242{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200243 bool is_array = field->flags & FIELD_IS_ARRAY;
244 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200245 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200246 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200247
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200248 if (is_array) {
249 list = PyList_New(field->arraylen);
250 item_size = field->size / field->arraylen;
251 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200252 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200253 item_size = field->size;
254 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200255 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200256
257 for (i = 0; i < n_items; i++) {
258
259 val = read_size(event, data + field->offset + i * item_size,
260 item_size);
261 if (field->flags & FIELD_IS_SIGNED) {
262 if ((long long)val >= LONG_MIN &&
263 (long long)val <= LONG_MAX)
264 obj = PyInt_FromLong(val);
265 else
266 obj = PyLong_FromLongLong(val);
267 } else {
268 if (val <= LONG_MAX)
269 obj = PyInt_FromLong(val);
270 else
271 obj = PyLong_FromUnsignedLongLong(val);
272 }
273 if (is_array)
274 PyList_SET_ITEM(list, i, obj);
275 }
276 if (is_array)
277 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200278 return obj;
279}
280
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300281static void python_process_tracepoint(struct perf_sample *sample,
282 struct perf_evsel *evsel,
283 struct thread *thread,
284 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600285{
Pierre Tardyc0251482010-05-31 23:12:09 +0200286 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600287 static char handler_name[256];
288 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600289 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200290 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600291 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600292 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700293 int cpu = sample->cpu;
294 void *data = sample->raw_data;
295 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200296 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600297
298 t = PyTuple_New(MAX_FIELDS);
299 if (!t)
300 Py_FatalError("couldn't create Python tuple");
301
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300302 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600303 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300304 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600305
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300306 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600307
308 sprintf(handler_name, "%s__%s", event->system, event->name);
309
Pierre Tardyc0251482010-05-31 23:12:09 +0200310 handler = PyDict_GetItemString(main_dict, handler_name);
311 if (handler && !PyCallable_Check(handler))
312 handler = NULL;
313 if (!handler) {
314 dict = PyDict_New();
315 if (!dict)
316 Py_FatalError("couldn't create Python dict");
317 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600318 s = nsecs / NSECS_PER_SEC;
319 ns = nsecs - s * NSECS_PER_SEC;
320
321 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600322 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600323
324 context = PyCObject_FromVoidPtr(scripting_context, NULL);
325
326 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500327 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600328
Pierre Tardyc0251482010-05-31 23:12:09 +0200329 if (handler) {
330 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
331 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
332 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
333 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
334 PyTuple_SetItem(t, n++, PyString_FromString(comm));
335 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300336 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
337 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
338 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
339 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
340 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Pierre Tardyc0251482010-05-31 23:12:09 +0200341 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600342 for (field = event->format.fields; field; field = field->next) {
343 if (field->flags & FIELD_IS_STRING) {
344 int offset;
345 if (field->flags & FIELD_IS_DYNAMIC) {
346 offset = *(int *)(data + field->offset);
347 offset &= 0xffff;
348 } else
349 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500350 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600351 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200352 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600353 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200354 if (handler)
355 PyTuple_SetItem(t, n++, obj);
356 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300357 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200358
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600359 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200360 if (!handler)
361 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600362
363 if (_PyTuple_Resize(&t, n) == -1)
364 Py_FatalError("error resizing Python tuple");
365
Pierre Tardyc0251482010-05-31 23:12:09 +0200366 if (handler) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600367 retval = PyObject_CallObject(handler, t);
368 if (retval == NULL)
369 handler_call_die(handler_name);
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200370 Py_DECREF(retval);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600371 } else {
372 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
373 if (handler && PyCallable_Check(handler)) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600374
375 retval = PyObject_CallObject(handler, t);
376 if (retval == NULL)
377 handler_call_die("trace_unhandled");
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200378 Py_DECREF(retval);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600379 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200380 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600381 }
382
383 Py_DECREF(t);
384}
385
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300386static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800387 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600388 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800389 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800390{
Feng Tangfd6b8582012-08-08 17:57:53 +0800391 PyObject *handler, *retval, *t, *dict;
Feng Tang6a6daec2012-08-08 17:57:51 +0800392 static char handler_name[64];
393 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800394
Feng Tangfd6b8582012-08-08 17:57:53 +0800395 /*
396 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800397 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800398 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800399 t = PyTuple_New(MAX_FIELDS);
400 if (!t)
401 Py_FatalError("couldn't create Python tuple");
402
Feng Tangfd6b8582012-08-08 17:57:53 +0800403 dict = PyDict_New();
404 if (!dict)
405 Py_FatalError("couldn't create Python dictionary");
406
Feng Tang6a6daec2012-08-08 17:57:51 +0800407 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
408
409 handler = PyDict_GetItemString(main_dict, handler_name);
Feng Tang87b6a3a2012-08-09 13:46:13 +0800410 if (!handler || !PyCallable_Check(handler))
Feng Tang6a6daec2012-08-08 17:57:51 +0800411 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800412
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300413 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
414 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800415 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300416 pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800417 (const char *)sample, sizeof(*sample)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300418 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800419 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300420 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200421 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800422 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300423 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800424 PyString_FromString(al->map->dso->name));
425 }
426 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300427 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800428 PyString_FromString(al->sym->name));
429 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800430
Feng Tangfd6b8582012-08-08 17:57:53 +0800431 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800432 if (_PyTuple_Resize(&t, n) == -1)
433 Py_FatalError("error resizing Python tuple");
434
435 retval = PyObject_CallObject(handler, t);
436 if (retval == NULL)
437 handler_call_die(handler_name);
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200438 Py_DECREF(retval);
Feng Tang6a6daec2012-08-08 17:57:51 +0800439exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800440 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800441 Py_DECREF(t);
442}
443
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300444static void python_process_event(union perf_event *event __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800445 struct perf_sample *sample,
446 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600447 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800448 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800449{
450 switch (evsel->attr.type) {
451 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300452 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800453 break;
454 /* Reserve for future process_hw/sw/raw APIs */
455 default:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300456 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800457 }
458}
459
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600460static int run_start_sub(void)
461{
462 PyObject *handler, *retval;
463 int err = 0;
464
465 main_module = PyImport_AddModule("__main__");
466 if (main_module == NULL)
467 return -1;
468 Py_INCREF(main_module);
469
470 main_dict = PyModule_GetDict(main_module);
471 if (main_dict == NULL) {
472 err = -1;
473 goto error;
474 }
475 Py_INCREF(main_dict);
476
477 handler = PyDict_GetItemString(main_dict, "trace_begin");
478 if (handler == NULL || !PyCallable_Check(handler))
479 goto out;
480
481 retval = PyObject_CallObject(handler, NULL);
482 if (retval == NULL)
483 handler_call_die("trace_begin");
484
485 Py_DECREF(retval);
486 return err;
487error:
488 Py_XDECREF(main_dict);
489 Py_XDECREF(main_module);
490out:
491 return err;
492}
493
494/*
495 * Start trace script
496 */
497static int python_start_script(const char *script, int argc, const char **argv)
498{
499 const char **command_line;
500 char buf[PATH_MAX];
501 int i, err = 0;
502 FILE *fp;
503
504 command_line = malloc((argc + 1) * sizeof(const char *));
505 command_line[0] = script;
506 for (i = 1; i < argc + 1; i++)
507 command_line[i] = argv[i - 1];
508
509 Py_Initialize();
510
511 initperf_trace_context();
512
513 PySys_SetArgv(argc + 1, (char **)command_line);
514
515 fp = fopen(script, "r");
516 if (!fp) {
517 sprintf(buf, "Can't open python script \"%s\"", script);
518 perror(buf);
519 err = -1;
520 goto error;
521 }
522
523 err = PyRun_SimpleFile(fp, script);
524 if (err) {
525 fprintf(stderr, "Error running python script %s\n", script);
526 goto error;
527 }
528
529 err = run_start_sub();
530 if (err) {
531 fprintf(stderr, "Error starting python script %s\n", script);
532 goto error;
533 }
534
535 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600536
537 return err;
538error:
539 Py_Finalize();
540 free(command_line);
541
542 return err;
543}
544
545/*
546 * Stop trace script
547 */
548static int python_stop_script(void)
549{
550 PyObject *handler, *retval;
551 int err = 0;
552
553 handler = PyDict_GetItemString(main_dict, "trace_end");
554 if (handler == NULL || !PyCallable_Check(handler))
555 goto out;
556
557 retval = PyObject_CallObject(handler, NULL);
558 if (retval == NULL)
559 handler_call_die("trace_end");
Joseph Schuchart05f832e2014-07-09 16:16:31 +0200560 Py_DECREF(retval);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600561out:
562 Py_XDECREF(main_dict);
563 Py_XDECREF(main_module);
564 Py_Finalize();
565
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600566 return err;
567}
568
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300569static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600570{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200571 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600572 struct format_field *f;
573 char fname[PATH_MAX];
574 int not_first, count;
575 FILE *ofp;
576
577 sprintf(fname, "%s.py", outfile);
578 ofp = fopen(fname, "w");
579 if (ofp == NULL) {
580 fprintf(stderr, "couldn't open %s\n", fname);
581 return -1;
582 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100583 fprintf(ofp, "# perf script event handlers, "
584 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600585
586 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
587 " License version 2\n\n");
588
589 fprintf(ofp, "# The common_* event handler fields are the most useful "
590 "fields common to\n");
591
592 fprintf(ofp, "# all events. They don't necessarily correspond to "
593 "the 'common_*' fields\n");
594
595 fprintf(ofp, "# in the format files. Those fields not available as "
596 "handler params can\n");
597
598 fprintf(ofp, "# be retrieved using Python functions of the form "
599 "common_*(context).\n");
600
601 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
602 "of available functions.\n\n");
603
604 fprintf(ofp, "import os\n");
605 fprintf(ofp, "import sys\n\n");
606
607 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
608 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
609 fprintf(ofp, "\nfrom perf_trace_context import *\n");
610 fprintf(ofp, "from Core import *\n\n\n");
611
612 fprintf(ofp, "def trace_begin():\n");
613 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
614
615 fprintf(ofp, "def trace_end():\n");
616 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
617
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300618 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600619 fprintf(ofp, "def %s__%s(", event->system, event->name);
620 fprintf(ofp, "event_name, ");
621 fprintf(ofp, "context, ");
622 fprintf(ofp, "common_cpu,\n");
623 fprintf(ofp, "\tcommon_secs, ");
624 fprintf(ofp, "common_nsecs, ");
625 fprintf(ofp, "common_pid, ");
626 fprintf(ofp, "common_comm,\n\t");
627
628 not_first = 0;
629 count = 0;
630
631 for (f = event->format.fields; f; f = f->next) {
632 if (not_first++)
633 fprintf(ofp, ", ");
634 if (++count % 5 == 0)
635 fprintf(ofp, "\n\t");
636
637 fprintf(ofp, "%s", f->name);
638 }
639 fprintf(ofp, "):\n");
640
641 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
642 "common_secs, common_nsecs,\n\t\t\t"
643 "common_pid, common_comm)\n\n");
644
645 fprintf(ofp, "\t\tprint \"");
646
647 not_first = 0;
648 count = 0;
649
650 for (f = event->format.fields; f; f = f->next) {
651 if (not_first++)
652 fprintf(ofp, ", ");
653 if (count && count % 3 == 0) {
654 fprintf(ofp, "\" \\\n\t\t\"");
655 }
656 count++;
657
658 fprintf(ofp, "%s=", f->name);
659 if (f->flags & FIELD_IS_STRING ||
660 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +0900661 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600662 f->flags & FIELD_IS_SYMBOLIC)
663 fprintf(ofp, "%%s");
664 else if (f->flags & FIELD_IS_SIGNED)
665 fprintf(ofp, "%%d");
666 else
667 fprintf(ofp, "%%u");
668 }
669
670 fprintf(ofp, "\\n\" %% \\\n\t\t(");
671
672 not_first = 0;
673 count = 0;
674
675 for (f = event->format.fields; f; f = f->next) {
676 if (not_first++)
677 fprintf(ofp, ", ");
678
679 if (++count % 5 == 0)
680 fprintf(ofp, "\n\t\t");
681
682 if (f->flags & FIELD_IS_FLAG) {
683 if ((count - 1) % 5 != 0) {
684 fprintf(ofp, "\n\t\t");
685 count = 4;
686 }
687 fprintf(ofp, "flag_str(\"");
688 fprintf(ofp, "%s__%s\", ", event->system,
689 event->name);
690 fprintf(ofp, "\"%s\", %s)", f->name,
691 f->name);
692 } else if (f->flags & FIELD_IS_SYMBOLIC) {
693 if ((count - 1) % 5 != 0) {
694 fprintf(ofp, "\n\t\t");
695 count = 4;
696 }
697 fprintf(ofp, "symbol_str(\"");
698 fprintf(ofp, "%s__%s\", ", event->system,
699 event->name);
700 fprintf(ofp, "\"%s\", %s)", f->name,
701 f->name);
702 } else
703 fprintf(ofp, "%s", f->name);
704 }
705
706 fprintf(ofp, "),\n\n");
707 }
708
709 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200710 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600711
Pierre Tardyc0251482010-05-31 23:12:09 +0200712 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
713 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600714
715 fprintf(ofp, "def print_header("
716 "event_name, cpu, secs, nsecs, pid, comm):\n"
717 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
718 "(event_name, cpu, secs, nsecs, pid, comm),\n");
719
720 fclose(ofp);
721
722 fprintf(stderr, "generated Python script: %s\n", fname);
723
724 return 0;
725}
726
727struct scripting_ops python_scripting_ops = {
728 .name = "Python",
729 .start_script = python_start_script,
730 .stop_script = python_stop_script,
731 .process_event = python_process_event,
732 .generate_script = python_generate_script,
733};