blob: e55b65a655587e67bfc8a7847368b65ea2c5ff0e [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
53static void handler_call_die(const char *handler_name)
54{
55 PyErr_Print();
56 Py_FatalError("problem in Python trace event handler");
57}
58
Joseph Schuchartc0268e82013-10-24 10:10:51 -030059/*
60 * Insert val into into the dictionary and decrement the reference counter.
61 * This is necessary for dictionaries since PyDict_SetItemString() does not
62 * steal a reference, as opposed to PyTuple_SetItem().
63 */
64static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
65{
66 PyDict_SetItemString(dict, key, val);
67 Py_DECREF(val);
68}
69
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060070static void define_value(enum print_arg_type field_type,
71 const char *ev_name,
72 const char *field_name,
73 const char *field_value,
74 const char *field_str)
75{
76 const char *handler_name = "define_flag_value";
77 PyObject *handler, *t, *retval;
78 unsigned long long value;
79 unsigned n = 0;
80
81 if (field_type == PRINT_SYMBOL)
82 handler_name = "define_symbolic_value";
83
Tom Zanussi44ad9cd2010-02-22 01:12:59 -060084 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085 if (!t)
86 Py_FatalError("couldn't create Python tuple");
87
88 value = eval_flag(field_value);
89
90 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
91 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
92 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
93 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
94
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060095 handler = PyDict_GetItemString(main_dict, handler_name);
96 if (handler && PyCallable_Check(handler)) {
97 retval = PyObject_CallObject(handler, t);
98 if (retval == NULL)
99 handler_call_die(handler_name);
100 }
101
102 Py_DECREF(t);
103}
104
105static void define_values(enum print_arg_type field_type,
106 struct print_flag_sym *field,
107 const char *ev_name,
108 const char *field_name)
109{
110 define_value(field_type, ev_name, field_name, field->value,
111 field->str);
112
113 if (field->next)
114 define_values(field_type, field->next, ev_name, field_name);
115}
116
117static void define_field(enum print_arg_type field_type,
118 const char *ev_name,
119 const char *field_name,
120 const char *delim)
121{
122 const char *handler_name = "define_flag_field";
123 PyObject *handler, *t, *retval;
124 unsigned n = 0;
125
126 if (field_type == PRINT_SYMBOL)
127 handler_name = "define_symbolic_field";
128
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600129 if (field_type == PRINT_FLAGS)
130 t = PyTuple_New(3);
131 else
132 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600133 if (!t)
134 Py_FatalError("couldn't create Python tuple");
135
136 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
137 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
138 if (field_type == PRINT_FLAGS)
139 PyTuple_SetItem(t, n++, PyString_FromString(delim));
140
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600141 handler = PyDict_GetItemString(main_dict, handler_name);
142 if (handler && PyCallable_Check(handler)) {
143 retval = PyObject_CallObject(handler, t);
144 if (retval == NULL)
145 handler_call_die(handler_name);
146 }
147
148 Py_DECREF(t);
149}
150
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200151static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600152 const char *ev_name,
153 struct print_arg *args)
154{
155 switch (args->type) {
156 case PRINT_NULL:
157 break;
158 case PRINT_ATOM:
159 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
160 args->atom.atom);
161 zero_flag_atom = 0;
162 break;
163 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300164 free(cur_field_name);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600165 cur_field_name = strdup(args->field.name);
166 break;
167 case PRINT_FLAGS:
168 define_event_symbols(event, ev_name, args->flags.field);
169 define_field(PRINT_FLAGS, ev_name, cur_field_name,
170 args->flags.delim);
171 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
172 cur_field_name);
173 break;
174 case PRINT_SYMBOL:
175 define_event_symbols(event, ev_name, args->symbol.field);
176 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
177 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
178 cur_field_name);
179 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900180 case PRINT_HEX:
181 define_event_symbols(event, ev_name, args->hex.field);
182 define_event_symbols(event, ev_name, args->hex.size);
183 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600184 case PRINT_STRING:
185 break;
186 case PRINT_TYPE:
187 define_event_symbols(event, ev_name, args->typecast.item);
188 break;
189 case PRINT_OP:
190 if (strcmp(args->op.op, ":") == 0)
191 zero_flag_atom = 1;
192 define_event_symbols(event, ev_name, args->op.left);
193 define_event_symbols(event, ev_name, args->op.right);
194 break;
195 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200196 /* gcc warns for these? */
197 case PRINT_BSTRING:
198 case PRINT_DYNAMIC_ARRAY:
199 case PRINT_FUNC:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400200 case PRINT_BITMASK:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600201 /* we should warn... */
202 return;
203 }
204
205 if (args->next)
206 define_event_symbols(event, ev_name, args->next);
207}
208
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300209static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210{
211 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200212 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300213 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600214
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300215 /*
216 * XXX: Do we really need to cache this since now we have evsel->tp_format
217 * cached already? Need to re-read this "cache" routine that as well calls
218 * define_event_symbols() :-\
219 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600220 if (events[type])
221 return events[type];
222
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300223 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600224 if (!event)
225 return NULL;
226
227 sprintf(ev_name, "%s__%s", event->system, event->name);
228
229 define_event_symbols(event, ev_name, event->print_fmt.args);
230
231 return event;
232}
233
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200234static PyObject *get_field_numeric_entry(struct event_format *event,
235 struct format_field *field, void *data)
236{
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200237 bool is_array = field->flags & FIELD_IS_ARRAY;
238 PyObject *obj, *list = NULL;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200239 unsigned long long val;
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200240 unsigned int item_size, n_items, i;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200241
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200242 if (is_array) {
243 list = PyList_New(field->arraylen);
244 item_size = field->size / field->arraylen;
245 n_items = field->arraylen;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200246 } else {
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200247 item_size = field->size;
248 n_items = 1;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200249 }
Sebastian Andrzej Siewior8ac631c2014-05-27 18:14:34 +0200250
251 for (i = 0; i < n_items; i++) {
252
253 val = read_size(event, data + field->offset + i * item_size,
254 item_size);
255 if (field->flags & FIELD_IS_SIGNED) {
256 if ((long long)val >= LONG_MIN &&
257 (long long)val <= LONG_MAX)
258 obj = PyInt_FromLong(val);
259 else
260 obj = PyLong_FromLongLong(val);
261 } else {
262 if (val <= LONG_MAX)
263 obj = PyInt_FromLong(val);
264 else
265 obj = PyLong_FromUnsignedLongLong(val);
266 }
267 if (is_array)
268 PyList_SET_ITEM(list, i, obj);
269 }
270 if (is_array)
271 obj = list;
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200272 return obj;
273}
274
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300275static void python_process_tracepoint(struct perf_sample *sample,
276 struct perf_evsel *evsel,
277 struct thread *thread,
278 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600279{
Pierre Tardyc0251482010-05-31 23:12:09 +0200280 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600281 static char handler_name[256];
282 struct format_field *field;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600283 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200284 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600285 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600286 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700287 int cpu = sample->cpu;
288 void *data = sample->raw_data;
289 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200290 const char *comm = thread__comm_str(thread);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600291
292 t = PyTuple_New(MAX_FIELDS);
293 if (!t)
294 Py_FatalError("couldn't create Python tuple");
295
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300296 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600297 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300298 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600299
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300300 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600301
302 sprintf(handler_name, "%s__%s", event->system, event->name);
303
Pierre Tardyc0251482010-05-31 23:12:09 +0200304 handler = PyDict_GetItemString(main_dict, handler_name);
305 if (handler && !PyCallable_Check(handler))
306 handler = NULL;
307 if (!handler) {
308 dict = PyDict_New();
309 if (!dict)
310 Py_FatalError("couldn't create Python dict");
311 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600312 s = nsecs / NSECS_PER_SEC;
313 ns = nsecs - s * NSECS_PER_SEC;
314
315 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600316 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600317
318 context = PyCObject_FromVoidPtr(scripting_context, NULL);
319
320 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500321 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600322
Pierre Tardyc0251482010-05-31 23:12:09 +0200323 if (handler) {
324 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
325 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
326 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
327 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
328 PyTuple_SetItem(t, n++, PyString_FromString(comm));
329 } else {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300330 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
331 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
332 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
333 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
334 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
Pierre Tardyc0251482010-05-31 23:12:09 +0200335 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600336 for (field = event->format.fields; field; field = field->next) {
337 if (field->flags & FIELD_IS_STRING) {
338 int offset;
339 if (field->flags & FIELD_IS_DYNAMIC) {
340 offset = *(int *)(data + field->offset);
341 offset &= 0xffff;
342 } else
343 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500344 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600345 } else { /* FIELD_IS_NUMERIC */
Sebastian Andrzej Siewior33058b92014-05-27 18:14:33 +0200346 obj = get_field_numeric_entry(event, field, data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600347 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200348 if (handler)
349 PyTuple_SetItem(t, n++, obj);
350 else
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300351 pydict_set_item_string_decref(dict, field->name, obj);
Pierre Tardyc0251482010-05-31 23:12:09 +0200352
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600353 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200354 if (!handler)
355 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600356
357 if (_PyTuple_Resize(&t, n) == -1)
358 Py_FatalError("error resizing Python tuple");
359
Pierre Tardyc0251482010-05-31 23:12:09 +0200360 if (handler) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600361 retval = PyObject_CallObject(handler, t);
362 if (retval == NULL)
363 handler_call_die(handler_name);
364 } else {
365 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
366 if (handler && PyCallable_Check(handler)) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600367
368 retval = PyObject_CallObject(handler, t);
369 if (retval == NULL)
370 handler_call_die("trace_unhandled");
371 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200372 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600373 }
374
375 Py_DECREF(t);
376}
377
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300378static void python_process_general_event(struct perf_sample *sample,
Feng Tang6a6daec2012-08-08 17:57:51 +0800379 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600380 struct thread *thread,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800381 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800382{
Feng Tangfd6b8582012-08-08 17:57:53 +0800383 PyObject *handler, *retval, *t, *dict;
Feng Tang6a6daec2012-08-08 17:57:51 +0800384 static char handler_name[64];
385 unsigned n = 0;
Feng Tang6a6daec2012-08-08 17:57:51 +0800386
Feng Tangfd6b8582012-08-08 17:57:53 +0800387 /*
388 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800389 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800390 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800391 t = PyTuple_New(MAX_FIELDS);
392 if (!t)
393 Py_FatalError("couldn't create Python tuple");
394
Feng Tangfd6b8582012-08-08 17:57:53 +0800395 dict = PyDict_New();
396 if (!dict)
397 Py_FatalError("couldn't create Python dictionary");
398
Feng Tang6a6daec2012-08-08 17:57:51 +0800399 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
400
401 handler = PyDict_GetItemString(main_dict, handler_name);
Feng Tang87b6a3a2012-08-09 13:46:13 +0800402 if (!handler || !PyCallable_Check(handler))
Feng Tang6a6daec2012-08-08 17:57:51 +0800403 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800404
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300405 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
406 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800407 (const char *)&evsel->attr, sizeof(evsel->attr)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300408 pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800409 (const char *)sample, sizeof(*sample)));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300410 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
Feng Tangfd6b8582012-08-08 17:57:53 +0800411 (const char *)sample->raw_data, sample->raw_size));
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300412 pydict_set_item_string_decref(dict, "comm",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200413 PyString_FromString(thread__comm_str(thread)));
Feng Tangfd6b8582012-08-08 17:57:53 +0800414 if (al->map) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300415 pydict_set_item_string_decref(dict, "dso",
Feng Tangfd6b8582012-08-08 17:57:53 +0800416 PyString_FromString(al->map->dso->name));
417 }
418 if (al->sym) {
Joseph Schuchartc0268e82013-10-24 10:10:51 -0300419 pydict_set_item_string_decref(dict, "symbol",
Feng Tangfd6b8582012-08-08 17:57:53 +0800420 PyString_FromString(al->sym->name));
421 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800422
Feng Tangfd6b8582012-08-08 17:57:53 +0800423 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800424 if (_PyTuple_Resize(&t, n) == -1)
425 Py_FatalError("error resizing Python tuple");
426
427 retval = PyObject_CallObject(handler, t);
428 if (retval == NULL)
429 handler_call_die(handler_name);
430exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800431 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800432 Py_DECREF(t);
433}
434
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300435static void python_process_event(union perf_event *event __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800436 struct perf_sample *sample,
437 struct perf_evsel *evsel,
David Ahern2eaa1b42013-07-18 16:06:15 -0600438 struct thread *thread,
Feng Tang73994dc2012-08-08 17:57:52 +0800439 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800440{
441 switch (evsel->attr.type) {
442 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300443 python_process_tracepoint(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800444 break;
445 /* Reserve for future process_hw/sw/raw APIs */
446 default:
Arnaldo Carvalho de Melob7fff6b52013-12-19 16:34:52 -0300447 python_process_general_event(sample, evsel, thread, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800448 }
449}
450
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600451static int run_start_sub(void)
452{
453 PyObject *handler, *retval;
454 int err = 0;
455
456 main_module = PyImport_AddModule("__main__");
457 if (main_module == NULL)
458 return -1;
459 Py_INCREF(main_module);
460
461 main_dict = PyModule_GetDict(main_module);
462 if (main_dict == NULL) {
463 err = -1;
464 goto error;
465 }
466 Py_INCREF(main_dict);
467
468 handler = PyDict_GetItemString(main_dict, "trace_begin");
469 if (handler == NULL || !PyCallable_Check(handler))
470 goto out;
471
472 retval = PyObject_CallObject(handler, NULL);
473 if (retval == NULL)
474 handler_call_die("trace_begin");
475
476 Py_DECREF(retval);
477 return err;
478error:
479 Py_XDECREF(main_dict);
480 Py_XDECREF(main_module);
481out:
482 return err;
483}
484
485/*
486 * Start trace script
487 */
488static int python_start_script(const char *script, int argc, const char **argv)
489{
490 const char **command_line;
491 char buf[PATH_MAX];
492 int i, err = 0;
493 FILE *fp;
494
495 command_line = malloc((argc + 1) * sizeof(const char *));
496 command_line[0] = script;
497 for (i = 1; i < argc + 1; i++)
498 command_line[i] = argv[i - 1];
499
500 Py_Initialize();
501
502 initperf_trace_context();
503
504 PySys_SetArgv(argc + 1, (char **)command_line);
505
506 fp = fopen(script, "r");
507 if (!fp) {
508 sprintf(buf, "Can't open python script \"%s\"", script);
509 perror(buf);
510 err = -1;
511 goto error;
512 }
513
514 err = PyRun_SimpleFile(fp, script);
515 if (err) {
516 fprintf(stderr, "Error running python script %s\n", script);
517 goto error;
518 }
519
520 err = run_start_sub();
521 if (err) {
522 fprintf(stderr, "Error starting python script %s\n", script);
523 goto error;
524 }
525
526 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600527
528 return err;
529error:
530 Py_Finalize();
531 free(command_line);
532
533 return err;
534}
535
536/*
537 * Stop trace script
538 */
539static int python_stop_script(void)
540{
541 PyObject *handler, *retval;
542 int err = 0;
543
544 handler = PyDict_GetItemString(main_dict, "trace_end");
545 if (handler == NULL || !PyCallable_Check(handler))
546 goto out;
547
548 retval = PyObject_CallObject(handler, NULL);
549 if (retval == NULL)
550 handler_call_die("trace_end");
551 else
552 Py_DECREF(retval);
553out:
554 Py_XDECREF(main_dict);
555 Py_XDECREF(main_module);
556 Py_Finalize();
557
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600558 return err;
559}
560
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300561static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600562{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200563 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600564 struct format_field *f;
565 char fname[PATH_MAX];
566 int not_first, count;
567 FILE *ofp;
568
569 sprintf(fname, "%s.py", outfile);
570 ofp = fopen(fname, "w");
571 if (ofp == NULL) {
572 fprintf(stderr, "couldn't open %s\n", fname);
573 return -1;
574 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100575 fprintf(ofp, "# perf script event handlers, "
576 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600577
578 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
579 " License version 2\n\n");
580
581 fprintf(ofp, "# The common_* event handler fields are the most useful "
582 "fields common to\n");
583
584 fprintf(ofp, "# all events. They don't necessarily correspond to "
585 "the 'common_*' fields\n");
586
587 fprintf(ofp, "# in the format files. Those fields not available as "
588 "handler params can\n");
589
590 fprintf(ofp, "# be retrieved using Python functions of the form "
591 "common_*(context).\n");
592
593 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
594 "of available functions.\n\n");
595
596 fprintf(ofp, "import os\n");
597 fprintf(ofp, "import sys\n\n");
598
599 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
600 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
601 fprintf(ofp, "\nfrom perf_trace_context import *\n");
602 fprintf(ofp, "from Core import *\n\n\n");
603
604 fprintf(ofp, "def trace_begin():\n");
605 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
606
607 fprintf(ofp, "def trace_end():\n");
608 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
609
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300610 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600611 fprintf(ofp, "def %s__%s(", event->system, event->name);
612 fprintf(ofp, "event_name, ");
613 fprintf(ofp, "context, ");
614 fprintf(ofp, "common_cpu,\n");
615 fprintf(ofp, "\tcommon_secs, ");
616 fprintf(ofp, "common_nsecs, ");
617 fprintf(ofp, "common_pid, ");
618 fprintf(ofp, "common_comm,\n\t");
619
620 not_first = 0;
621 count = 0;
622
623 for (f = event->format.fields; f; f = f->next) {
624 if (not_first++)
625 fprintf(ofp, ", ");
626 if (++count % 5 == 0)
627 fprintf(ofp, "\n\t");
628
629 fprintf(ofp, "%s", f->name);
630 }
631 fprintf(ofp, "):\n");
632
633 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
634 "common_secs, common_nsecs,\n\t\t\t"
635 "common_pid, common_comm)\n\n");
636
637 fprintf(ofp, "\t\tprint \"");
638
639 not_first = 0;
640 count = 0;
641
642 for (f = event->format.fields; f; f = f->next) {
643 if (not_first++)
644 fprintf(ofp, ", ");
645 if (count && count % 3 == 0) {
646 fprintf(ofp, "\" \\\n\t\t\"");
647 }
648 count++;
649
650 fprintf(ofp, "%s=", f->name);
651 if (f->flags & FIELD_IS_STRING ||
652 f->flags & FIELD_IS_FLAG ||
Namhyung Kime646fe72014-05-29 13:44:55 +0900653 f->flags & FIELD_IS_ARRAY ||
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600654 f->flags & FIELD_IS_SYMBOLIC)
655 fprintf(ofp, "%%s");
656 else if (f->flags & FIELD_IS_SIGNED)
657 fprintf(ofp, "%%d");
658 else
659 fprintf(ofp, "%%u");
660 }
661
662 fprintf(ofp, "\\n\" %% \\\n\t\t(");
663
664 not_first = 0;
665 count = 0;
666
667 for (f = event->format.fields; f; f = f->next) {
668 if (not_first++)
669 fprintf(ofp, ", ");
670
671 if (++count % 5 == 0)
672 fprintf(ofp, "\n\t\t");
673
674 if (f->flags & FIELD_IS_FLAG) {
675 if ((count - 1) % 5 != 0) {
676 fprintf(ofp, "\n\t\t");
677 count = 4;
678 }
679 fprintf(ofp, "flag_str(\"");
680 fprintf(ofp, "%s__%s\", ", event->system,
681 event->name);
682 fprintf(ofp, "\"%s\", %s)", f->name,
683 f->name);
684 } else if (f->flags & FIELD_IS_SYMBOLIC) {
685 if ((count - 1) % 5 != 0) {
686 fprintf(ofp, "\n\t\t");
687 count = 4;
688 }
689 fprintf(ofp, "symbol_str(\"");
690 fprintf(ofp, "%s__%s\", ", event->system,
691 event->name);
692 fprintf(ofp, "\"%s\", %s)", f->name,
693 f->name);
694 } else
695 fprintf(ofp, "%s", f->name);
696 }
697
698 fprintf(ofp, "),\n\n");
699 }
700
701 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200702 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600703
Pierre Tardyc0251482010-05-31 23:12:09 +0200704 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
705 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600706
707 fprintf(ofp, "def print_header("
708 "event_name, cpu, secs, nsecs, pid, comm):\n"
709 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
710 "(event_name, cpu, secs, nsecs, pid, comm),\n");
711
712 fclose(ofp);
713
714 fprintf(stderr, "generated Python script: %s\n", fname);
715
716 return 0;
717}
718
719struct scripting_ops python_scripting_ops = {
720 .name = "Python",
721 .start_script = python_start_script,
722 .stop_script = python_stop_script,
723 .process_event = python_process_event,
724 .generate_script = python_generate_script,
725};