blob: 14683dfca2eeb9cfaeac762dc5964d48c093328c [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
59static void define_value(enum print_arg_type field_type,
60 const char *ev_name,
61 const char *field_name,
62 const char *field_value,
63 const char *field_str)
64{
65 const char *handler_name = "define_flag_value";
66 PyObject *handler, *t, *retval;
67 unsigned long long value;
68 unsigned n = 0;
69
70 if (field_type == PRINT_SYMBOL)
71 handler_name = "define_symbolic_value";
72
Tom Zanussi44ad9cd2010-02-22 01:12:59 -060073 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060074 if (!t)
75 Py_FatalError("couldn't create Python tuple");
76
77 value = eval_flag(field_value);
78
79 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
80 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
81 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
82 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
83
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060084 handler = PyDict_GetItemString(main_dict, handler_name);
85 if (handler && PyCallable_Check(handler)) {
86 retval = PyObject_CallObject(handler, t);
87 if (retval == NULL)
88 handler_call_die(handler_name);
89 }
90
91 Py_DECREF(t);
92}
93
94static void define_values(enum print_arg_type field_type,
95 struct print_flag_sym *field,
96 const char *ev_name,
97 const char *field_name)
98{
99 define_value(field_type, ev_name, field_name, field->value,
100 field->str);
101
102 if (field->next)
103 define_values(field_type, field->next, ev_name, field_name);
104}
105
106static void define_field(enum print_arg_type field_type,
107 const char *ev_name,
108 const char *field_name,
109 const char *delim)
110{
111 const char *handler_name = "define_flag_field";
112 PyObject *handler, *t, *retval;
113 unsigned n = 0;
114
115 if (field_type == PRINT_SYMBOL)
116 handler_name = "define_symbolic_field";
117
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600118 if (field_type == PRINT_FLAGS)
119 t = PyTuple_New(3);
120 else
121 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600122 if (!t)
123 Py_FatalError("couldn't create Python tuple");
124
125 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
126 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
127 if (field_type == PRINT_FLAGS)
128 PyTuple_SetItem(t, n++, PyString_FromString(delim));
129
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600130 handler = PyDict_GetItemString(main_dict, handler_name);
131 if (handler && PyCallable_Check(handler)) {
132 retval = PyObject_CallObject(handler, t);
133 if (retval == NULL)
134 handler_call_die(handler_name);
135 }
136
137 Py_DECREF(t);
138}
139
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200140static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600141 const char *ev_name,
142 struct print_arg *args)
143{
144 switch (args->type) {
145 case PRINT_NULL:
146 break;
147 case PRINT_ATOM:
148 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
149 args->atom.atom);
150 zero_flag_atom = 0;
151 break;
152 case PRINT_FIELD:
153 if (cur_field_name)
154 free(cur_field_name);
155 cur_field_name = strdup(args->field.name);
156 break;
157 case PRINT_FLAGS:
158 define_event_symbols(event, ev_name, args->flags.field);
159 define_field(PRINT_FLAGS, ev_name, cur_field_name,
160 args->flags.delim);
161 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
162 cur_field_name);
163 break;
164 case PRINT_SYMBOL:
165 define_event_symbols(event, ev_name, args->symbol.field);
166 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
167 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
168 cur_field_name);
169 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900170 case PRINT_HEX:
171 define_event_symbols(event, ev_name, args->hex.field);
172 define_event_symbols(event, ev_name, args->hex.size);
173 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600174 case PRINT_STRING:
175 break;
176 case PRINT_TYPE:
177 define_event_symbols(event, ev_name, args->typecast.item);
178 break;
179 case PRINT_OP:
180 if (strcmp(args->op.op, ":") == 0)
181 zero_flag_atom = 1;
182 define_event_symbols(event, ev_name, args->op.left);
183 define_event_symbols(event, ev_name, args->op.right);
184 break;
185 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200186 /* gcc warns for these? */
187 case PRINT_BSTRING:
188 case PRINT_DYNAMIC_ARRAY:
189 case PRINT_FUNC:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600190 /* we should warn... */
191 return;
192 }
193
194 if (args->next)
195 define_event_symbols(event, ev_name, args->next);
196}
197
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300198static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600199{
200 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200201 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300202 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600203
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300204 /*
205 * XXX: Do we really need to cache this since now we have evsel->tp_format
206 * cached already? Need to re-read this "cache" routine that as well calls
207 * define_event_symbols() :-\
208 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600209 if (events[type])
210 return events[type];
211
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300212 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600213 if (!event)
214 return NULL;
215
216 sprintf(ev_name, "%s__%s", event->system, event->name);
217
218 define_event_symbols(event, ev_name, event->print_fmt.args);
219
220 return event;
221}
222
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300223static void python_process_tracepoint(union perf_event *perf_event
224 __maybe_unused,
David Ahernbe6d8422011-03-09 22:23:23 -0700225 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300226 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300227 struct machine *machine __maybe_unused,
Feng Tang73994dc2012-08-08 17:57:52 +0800228 struct addr_location *al)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600229{
Pierre Tardyc0251482010-05-31 23:12:09 +0200230 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600231 static char handler_name[256];
232 struct format_field *field;
233 unsigned long long val;
234 unsigned long s, ns;
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200235 struct event_format *event;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600236 unsigned n = 0;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600237 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700238 int cpu = sample->cpu;
239 void *data = sample->raw_data;
240 unsigned long long nsecs = sample->time;
Feng Tang73994dc2012-08-08 17:57:52 +0800241 struct thread *thread = al->thread;
David Ahernbe6d8422011-03-09 22:23:23 -0700242 char *comm = thread->comm;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600243
244 t = PyTuple_New(MAX_FIELDS);
245 if (!t)
246 Py_FatalError("couldn't create Python tuple");
247
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300248 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600249 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300250 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600251
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300252 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600253
254 sprintf(handler_name, "%s__%s", event->system, event->name);
255
Pierre Tardyc0251482010-05-31 23:12:09 +0200256 handler = PyDict_GetItemString(main_dict, handler_name);
257 if (handler && !PyCallable_Check(handler))
258 handler = NULL;
259 if (!handler) {
260 dict = PyDict_New();
261 if (!dict)
262 Py_FatalError("couldn't create Python dict");
263 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600264 s = nsecs / NSECS_PER_SEC;
265 ns = nsecs - s * NSECS_PER_SEC;
266
267 scripting_context->event_data = data;
268
269 context = PyCObject_FromVoidPtr(scripting_context, NULL);
270
271 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500272 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600273
Pierre Tardyc0251482010-05-31 23:12:09 +0200274 if (handler) {
275 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
276 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
277 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
278 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
279 PyTuple_SetItem(t, n++, PyString_FromString(comm));
280 } else {
281 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
282 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
283 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
284 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
285 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
286 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600287 for (field = event->format.fields; field; field = field->next) {
288 if (field->flags & FIELD_IS_STRING) {
289 int offset;
290 if (field->flags & FIELD_IS_DYNAMIC) {
291 offset = *(int *)(data + field->offset);
292 offset &= 0xffff;
293 } else
294 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500295 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600296 } else { /* FIELD_IS_NUMERIC */
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300297 val = read_size(event, data + field->offset,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300298 field->size);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600299 if (field->flags & FIELD_IS_SIGNED) {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500300 if ((long long)val >= LONG_MIN &&
301 (long long)val <= LONG_MAX)
302 obj = PyInt_FromLong(val);
303 else
304 obj = PyLong_FromLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600305 } else {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500306 if (val <= LONG_MAX)
307 obj = PyInt_FromLong(val);
308 else
309 obj = PyLong_FromUnsignedLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600310 }
311 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200312 if (handler)
313 PyTuple_SetItem(t, n++, obj);
314 else
315 PyDict_SetItemString(dict, field->name, obj);
316
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600317 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200318 if (!handler)
319 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600320
321 if (_PyTuple_Resize(&t, n) == -1)
322 Py_FatalError("error resizing Python tuple");
323
Pierre Tardyc0251482010-05-31 23:12:09 +0200324 if (handler) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600325 retval = PyObject_CallObject(handler, t);
326 if (retval == NULL)
327 handler_call_die(handler_name);
328 } else {
329 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
330 if (handler && PyCallable_Check(handler)) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600331
332 retval = PyObject_CallObject(handler, t);
333 if (retval == NULL)
334 handler_call_die("trace_unhandled");
335 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200336 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600337 }
338
339 Py_DECREF(t);
340}
341
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300342static void python_process_general_event(union perf_event *perf_event
343 __maybe_unused,
Feng Tang6a6daec2012-08-08 17:57:51 +0800344 struct perf_sample *sample,
345 struct perf_evsel *evsel,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300346 struct machine *machine __maybe_unused,
Feng Tang87b6a3a2012-08-09 13:46:13 +0800347 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800348{
Feng Tangfd6b8582012-08-08 17:57:53 +0800349 PyObject *handler, *retval, *t, *dict;
Feng Tang6a6daec2012-08-08 17:57:51 +0800350 static char handler_name[64];
351 unsigned n = 0;
Feng Tangfd6b8582012-08-08 17:57:53 +0800352 struct thread *thread = al->thread;
Feng Tang6a6daec2012-08-08 17:57:51 +0800353
Feng Tangfd6b8582012-08-08 17:57:53 +0800354 /*
355 * Use the MAX_FIELDS to make the function expandable, though
Feng Tang87b6a3a2012-08-09 13:46:13 +0800356 * currently there is only one item for the tuple.
Feng Tangfd6b8582012-08-08 17:57:53 +0800357 */
Feng Tang6a6daec2012-08-08 17:57:51 +0800358 t = PyTuple_New(MAX_FIELDS);
359 if (!t)
360 Py_FatalError("couldn't create Python tuple");
361
Feng Tangfd6b8582012-08-08 17:57:53 +0800362 dict = PyDict_New();
363 if (!dict)
364 Py_FatalError("couldn't create Python dictionary");
365
Feng Tang6a6daec2012-08-08 17:57:51 +0800366 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
367
368 handler = PyDict_GetItemString(main_dict, handler_name);
Feng Tang87b6a3a2012-08-09 13:46:13 +0800369 if (!handler || !PyCallable_Check(handler))
Feng Tang6a6daec2012-08-08 17:57:51 +0800370 goto exit;
Feng Tang6a6daec2012-08-08 17:57:51 +0800371
Feng Tangfd6b8582012-08-08 17:57:53 +0800372 PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
373 PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize(
374 (const char *)&evsel->attr, sizeof(evsel->attr)));
375 PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize(
376 (const char *)sample, sizeof(*sample)));
377 PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize(
378 (const char *)sample->raw_data, sample->raw_size));
379 PyDict_SetItemString(dict, "comm",
380 PyString_FromString(thread->comm));
381 if (al->map) {
382 PyDict_SetItemString(dict, "dso",
383 PyString_FromString(al->map->dso->name));
384 }
385 if (al->sym) {
386 PyDict_SetItemString(dict, "symbol",
387 PyString_FromString(al->sym->name));
388 }
Feng Tang6a6daec2012-08-08 17:57:51 +0800389
Feng Tangfd6b8582012-08-08 17:57:53 +0800390 PyTuple_SetItem(t, n++, dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800391 if (_PyTuple_Resize(&t, n) == -1)
392 Py_FatalError("error resizing Python tuple");
393
394 retval = PyObject_CallObject(handler, t);
395 if (retval == NULL)
396 handler_call_die(handler_name);
397exit:
Feng Tangfd6b8582012-08-08 17:57:53 +0800398 Py_DECREF(dict);
Feng Tang6a6daec2012-08-08 17:57:51 +0800399 Py_DECREF(t);
400}
401
402static void python_process_event(union perf_event *perf_event,
403 struct perf_sample *sample,
404 struct perf_evsel *evsel,
405 struct machine *machine,
Feng Tang73994dc2012-08-08 17:57:52 +0800406 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800407{
408 switch (evsel->attr.type) {
409 case PERF_TYPE_TRACEPOINT:
410 python_process_tracepoint(perf_event, sample, evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800411 machine, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800412 break;
413 /* Reserve for future process_hw/sw/raw APIs */
414 default:
415 python_process_general_event(perf_event, sample, evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800416 machine, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800417 }
418}
419
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600420static int run_start_sub(void)
421{
422 PyObject *handler, *retval;
423 int err = 0;
424
425 main_module = PyImport_AddModule("__main__");
426 if (main_module == NULL)
427 return -1;
428 Py_INCREF(main_module);
429
430 main_dict = PyModule_GetDict(main_module);
431 if (main_dict == NULL) {
432 err = -1;
433 goto error;
434 }
435 Py_INCREF(main_dict);
436
437 handler = PyDict_GetItemString(main_dict, "trace_begin");
438 if (handler == NULL || !PyCallable_Check(handler))
439 goto out;
440
441 retval = PyObject_CallObject(handler, NULL);
442 if (retval == NULL)
443 handler_call_die("trace_begin");
444
445 Py_DECREF(retval);
446 return err;
447error:
448 Py_XDECREF(main_dict);
449 Py_XDECREF(main_module);
450out:
451 return err;
452}
453
454/*
455 * Start trace script
456 */
457static int python_start_script(const char *script, int argc, const char **argv)
458{
459 const char **command_line;
460 char buf[PATH_MAX];
461 int i, err = 0;
462 FILE *fp;
463
464 command_line = malloc((argc + 1) * sizeof(const char *));
465 command_line[0] = script;
466 for (i = 1; i < argc + 1; i++)
467 command_line[i] = argv[i - 1];
468
469 Py_Initialize();
470
471 initperf_trace_context();
472
473 PySys_SetArgv(argc + 1, (char **)command_line);
474
475 fp = fopen(script, "r");
476 if (!fp) {
477 sprintf(buf, "Can't open python script \"%s\"", script);
478 perror(buf);
479 err = -1;
480 goto error;
481 }
482
483 err = PyRun_SimpleFile(fp, script);
484 if (err) {
485 fprintf(stderr, "Error running python script %s\n", script);
486 goto error;
487 }
488
489 err = run_start_sub();
490 if (err) {
491 fprintf(stderr, "Error starting python script %s\n", script);
492 goto error;
493 }
494
495 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600496
497 return err;
498error:
499 Py_Finalize();
500 free(command_line);
501
502 return err;
503}
504
505/*
506 * Stop trace script
507 */
508static int python_stop_script(void)
509{
510 PyObject *handler, *retval;
511 int err = 0;
512
513 handler = PyDict_GetItemString(main_dict, "trace_end");
514 if (handler == NULL || !PyCallable_Check(handler))
515 goto out;
516
517 retval = PyObject_CallObject(handler, NULL);
518 if (retval == NULL)
519 handler_call_die("trace_end");
520 else
521 Py_DECREF(retval);
522out:
523 Py_XDECREF(main_dict);
524 Py_XDECREF(main_module);
525 Py_Finalize();
526
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600527 return err;
528}
529
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300530static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600531{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200532 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600533 struct format_field *f;
534 char fname[PATH_MAX];
535 int not_first, count;
536 FILE *ofp;
537
538 sprintf(fname, "%s.py", outfile);
539 ofp = fopen(fname, "w");
540 if (ofp == NULL) {
541 fprintf(stderr, "couldn't open %s\n", fname);
542 return -1;
543 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100544 fprintf(ofp, "# perf script event handlers, "
545 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600546
547 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
548 " License version 2\n\n");
549
550 fprintf(ofp, "# The common_* event handler fields are the most useful "
551 "fields common to\n");
552
553 fprintf(ofp, "# all events. They don't necessarily correspond to "
554 "the 'common_*' fields\n");
555
556 fprintf(ofp, "# in the format files. Those fields not available as "
557 "handler params can\n");
558
559 fprintf(ofp, "# be retrieved using Python functions of the form "
560 "common_*(context).\n");
561
562 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
563 "of available functions.\n\n");
564
565 fprintf(ofp, "import os\n");
566 fprintf(ofp, "import sys\n\n");
567
568 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
569 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
570 fprintf(ofp, "\nfrom perf_trace_context import *\n");
571 fprintf(ofp, "from Core import *\n\n\n");
572
573 fprintf(ofp, "def trace_begin():\n");
574 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
575
576 fprintf(ofp, "def trace_end():\n");
577 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
578
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300579 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600580 fprintf(ofp, "def %s__%s(", event->system, event->name);
581 fprintf(ofp, "event_name, ");
582 fprintf(ofp, "context, ");
583 fprintf(ofp, "common_cpu,\n");
584 fprintf(ofp, "\tcommon_secs, ");
585 fprintf(ofp, "common_nsecs, ");
586 fprintf(ofp, "common_pid, ");
587 fprintf(ofp, "common_comm,\n\t");
588
589 not_first = 0;
590 count = 0;
591
592 for (f = event->format.fields; f; f = f->next) {
593 if (not_first++)
594 fprintf(ofp, ", ");
595 if (++count % 5 == 0)
596 fprintf(ofp, "\n\t");
597
598 fprintf(ofp, "%s", f->name);
599 }
600 fprintf(ofp, "):\n");
601
602 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
603 "common_secs, common_nsecs,\n\t\t\t"
604 "common_pid, common_comm)\n\n");
605
606 fprintf(ofp, "\t\tprint \"");
607
608 not_first = 0;
609 count = 0;
610
611 for (f = event->format.fields; f; f = f->next) {
612 if (not_first++)
613 fprintf(ofp, ", ");
614 if (count && count % 3 == 0) {
615 fprintf(ofp, "\" \\\n\t\t\"");
616 }
617 count++;
618
619 fprintf(ofp, "%s=", f->name);
620 if (f->flags & FIELD_IS_STRING ||
621 f->flags & FIELD_IS_FLAG ||
622 f->flags & FIELD_IS_SYMBOLIC)
623 fprintf(ofp, "%%s");
624 else if (f->flags & FIELD_IS_SIGNED)
625 fprintf(ofp, "%%d");
626 else
627 fprintf(ofp, "%%u");
628 }
629
630 fprintf(ofp, "\\n\" %% \\\n\t\t(");
631
632 not_first = 0;
633 count = 0;
634
635 for (f = event->format.fields; f; f = f->next) {
636 if (not_first++)
637 fprintf(ofp, ", ");
638
639 if (++count % 5 == 0)
640 fprintf(ofp, "\n\t\t");
641
642 if (f->flags & FIELD_IS_FLAG) {
643 if ((count - 1) % 5 != 0) {
644 fprintf(ofp, "\n\t\t");
645 count = 4;
646 }
647 fprintf(ofp, "flag_str(\"");
648 fprintf(ofp, "%s__%s\", ", event->system,
649 event->name);
650 fprintf(ofp, "\"%s\", %s)", f->name,
651 f->name);
652 } else if (f->flags & FIELD_IS_SYMBOLIC) {
653 if ((count - 1) % 5 != 0) {
654 fprintf(ofp, "\n\t\t");
655 count = 4;
656 }
657 fprintf(ofp, "symbol_str(\"");
658 fprintf(ofp, "%s__%s\", ", event->system,
659 event->name);
660 fprintf(ofp, "\"%s\", %s)", f->name,
661 f->name);
662 } else
663 fprintf(ofp, "%s", f->name);
664 }
665
666 fprintf(ofp, "),\n\n");
667 }
668
669 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200670 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600671
Pierre Tardyc0251482010-05-31 23:12:09 +0200672 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
673 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600674
675 fprintf(ofp, "def print_header("
676 "event_name, cpu, secs, nsecs, pid, comm):\n"
677 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
678 "(event_name, cpu, secs, nsecs, pid, comm),\n");
679
680 fclose(ofp);
681
682 fprintf(stderr, "generated Python script: %s\n", fname);
683
684 return 0;
685}
686
687struct scripting_ops python_scripting_ops = {
688 .name = "Python",
689 .start_script = python_start_script,
690 .stop_script = python_stop_script,
691 .process_event = python_process_event,
692 .generate_script = python_generate_script,
693};