blob: 24711b3536d395d16c42794e7238024b141da926 [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"
Feng Tang6a6daec2012-08-08 17:57:51 +080035#include "../evsel.h"
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060036
37PyMODINIT_FUNC initperf_trace_context(void);
38
39#define FTRACE_MAX_EVENT \
40 ((1 << (sizeof(unsigned short) * 8)) - 1)
41
Steven Rostedtaaf045f2012-04-06 00:47:56 +020042struct event_format *events[FTRACE_MAX_EVENT];
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060043
44#define MAX_FIELDS 64
45#define N_COMMON_FIELDS 7
46
47extern struct scripting_context *scripting_context;
48
49static char *cur_field_name;
50static int zero_flag_atom;
51
52static PyObject *main_module, *main_dict;
53
54static void handler_call_die(const char *handler_name)
55{
56 PyErr_Print();
57 Py_FatalError("problem in Python trace event handler");
58}
59
60static void define_value(enum print_arg_type field_type,
61 const char *ev_name,
62 const char *field_name,
63 const char *field_value,
64 const char *field_str)
65{
66 const char *handler_name = "define_flag_value";
67 PyObject *handler, *t, *retval;
68 unsigned long long value;
69 unsigned n = 0;
70
71 if (field_type == PRINT_SYMBOL)
72 handler_name = "define_symbolic_value";
73
Tom Zanussi44ad9cd2010-02-22 01:12:59 -060074 t = PyTuple_New(4);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060075 if (!t)
76 Py_FatalError("couldn't create Python tuple");
77
78 value = eval_flag(field_value);
79
80 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
81 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
82 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
83 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
84
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085 handler = PyDict_GetItemString(main_dict, handler_name);
86 if (handler && PyCallable_Check(handler)) {
87 retval = PyObject_CallObject(handler, t);
88 if (retval == NULL)
89 handler_call_die(handler_name);
90 }
91
92 Py_DECREF(t);
93}
94
95static void define_values(enum print_arg_type field_type,
96 struct print_flag_sym *field,
97 const char *ev_name,
98 const char *field_name)
99{
100 define_value(field_type, ev_name, field_name, field->value,
101 field->str);
102
103 if (field->next)
104 define_values(field_type, field->next, ev_name, field_name);
105}
106
107static void define_field(enum print_arg_type field_type,
108 const char *ev_name,
109 const char *field_name,
110 const char *delim)
111{
112 const char *handler_name = "define_flag_field";
113 PyObject *handler, *t, *retval;
114 unsigned n = 0;
115
116 if (field_type == PRINT_SYMBOL)
117 handler_name = "define_symbolic_field";
118
Tom Zanussi44ad9cd2010-02-22 01:12:59 -0600119 if (field_type == PRINT_FLAGS)
120 t = PyTuple_New(3);
121 else
122 t = PyTuple_New(2);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600123 if (!t)
124 Py_FatalError("couldn't create Python tuple");
125
126 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
127 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
128 if (field_type == PRINT_FLAGS)
129 PyTuple_SetItem(t, n++, PyString_FromString(delim));
130
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600131 handler = PyDict_GetItemString(main_dict, handler_name);
132 if (handler && PyCallable_Check(handler)) {
133 retval = PyObject_CallObject(handler, t);
134 if (retval == NULL)
135 handler_call_die(handler_name);
136 }
137
138 Py_DECREF(t);
139}
140
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200141static void define_event_symbols(struct event_format *event,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600142 const char *ev_name,
143 struct print_arg *args)
144{
145 switch (args->type) {
146 case PRINT_NULL:
147 break;
148 case PRINT_ATOM:
149 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
150 args->atom.atom);
151 zero_flag_atom = 0;
152 break;
153 case PRINT_FIELD:
154 if (cur_field_name)
155 free(cur_field_name);
156 cur_field_name = strdup(args->field.name);
157 break;
158 case PRINT_FLAGS:
159 define_event_symbols(event, ev_name, args->flags.field);
160 define_field(PRINT_FLAGS, ev_name, cur_field_name,
161 args->flags.delim);
162 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
163 cur_field_name);
164 break;
165 case PRINT_SYMBOL:
166 define_event_symbols(event, ev_name, args->symbol.field);
167 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
168 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
169 cur_field_name);
170 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900171 case PRINT_HEX:
172 define_event_symbols(event, ev_name, args->hex.field);
173 define_event_symbols(event, ev_name, args->hex.size);
174 break;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600175 case PRINT_STRING:
176 break;
177 case PRINT_TYPE:
178 define_event_symbols(event, ev_name, args->typecast.item);
179 break;
180 case PRINT_OP:
181 if (strcmp(args->op.op, ":") == 0)
182 zero_flag_atom = 1;
183 define_event_symbols(event, ev_name, args->op.left);
184 define_event_symbols(event, ev_name, args->op.right);
185 break;
186 default:
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200187 /* gcc warns for these? */
188 case PRINT_BSTRING:
189 case PRINT_DYNAMIC_ARRAY:
190 case PRINT_FUNC:
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600191 /* we should warn... */
192 return;
193 }
194
195 if (args->next)
196 define_event_symbols(event, ev_name, args->next);
197}
198
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300199static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600200{
201 static char ev_name[256];
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200202 struct event_format *event;
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300203 int type = evsel->attr.config;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600204
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300205 /*
206 * XXX: Do we really need to cache this since now we have evsel->tp_format
207 * cached already? Need to re-read this "cache" routine that as well calls
208 * define_event_symbols() :-\
209 */
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600210 if (events[type])
211 return events[type];
212
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300213 events[type] = event = evsel->tp_format;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600214 if (!event)
215 return NULL;
216
217 sprintf(ev_name, "%s__%s", event->system, event->name);
218
219 define_event_symbols(event, ev_name, event->print_fmt.args);
220
221 return event;
222}
223
Feng Tang6a6daec2012-08-08 17:57:51 +0800224static void python_process_tracepoint(union perf_event *perf_event __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,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200227 struct machine *machine __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
Feng Tang6a6daec2012-08-08 17:57:51 +0800342static void python_process_general_event(union perf_event *perf_event __unused,
343 struct perf_sample *sample,
344 struct perf_evsel *evsel,
345 struct machine *machine __unused,
Feng Tang73994dc2012-08-08 17:57:52 +0800346 struct addr_location *al __unused)
Feng Tang6a6daec2012-08-08 17:57:51 +0800347{
348 PyObject *handler, *retval, *t;
349 static char handler_name[64];
350 unsigned n = 0;
351 void *data = sample->raw_data;
352
353 t = PyTuple_New(MAX_FIELDS);
354 if (!t)
355 Py_FatalError("couldn't create Python tuple");
356
357 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
358
359 handler = PyDict_GetItemString(main_dict, handler_name);
360 if (handler && !PyCallable_Check(handler)) {
361 handler = NULL;
362 goto exit;
363 }
364
Feng Tang73994dc2012-08-08 17:57:52 +0800365 /* Pass 4 parameters: event_attr, perf_sample, raw data, thread name */
Feng Tang6a6daec2012-08-08 17:57:51 +0800366 PyTuple_SetItem(t, n++, PyString_FromStringAndSize((void *)&evsel->attr, sizeof(evsel->attr)));
367 PyTuple_SetItem(t, n++, PyString_FromStringAndSize((void *)sample, sizeof(*sample)));
368 PyTuple_SetItem(t, n++, PyString_FromStringAndSize(data, sample->raw_size));
369
370 if (_PyTuple_Resize(&t, n) == -1)
371 Py_FatalError("error resizing Python tuple");
372
373 retval = PyObject_CallObject(handler, t);
374 if (retval == NULL)
375 handler_call_die(handler_name);
376exit:
377 Py_DECREF(t);
378}
379
380static void python_process_event(union perf_event *perf_event,
381 struct perf_sample *sample,
382 struct perf_evsel *evsel,
383 struct machine *machine,
Feng Tang73994dc2012-08-08 17:57:52 +0800384 struct addr_location *al)
Feng Tang6a6daec2012-08-08 17:57:51 +0800385{
386 switch (evsel->attr.type) {
387 case PERF_TYPE_TRACEPOINT:
388 python_process_tracepoint(perf_event, sample, evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800389 machine, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800390 break;
391 /* Reserve for future process_hw/sw/raw APIs */
392 default:
393 python_process_general_event(perf_event, sample, evsel,
Feng Tang73994dc2012-08-08 17:57:52 +0800394 machine, al);
Feng Tang6a6daec2012-08-08 17:57:51 +0800395 }
396}
397
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600398static int run_start_sub(void)
399{
400 PyObject *handler, *retval;
401 int err = 0;
402
403 main_module = PyImport_AddModule("__main__");
404 if (main_module == NULL)
405 return -1;
406 Py_INCREF(main_module);
407
408 main_dict = PyModule_GetDict(main_module);
409 if (main_dict == NULL) {
410 err = -1;
411 goto error;
412 }
413 Py_INCREF(main_dict);
414
415 handler = PyDict_GetItemString(main_dict, "trace_begin");
416 if (handler == NULL || !PyCallable_Check(handler))
417 goto out;
418
419 retval = PyObject_CallObject(handler, NULL);
420 if (retval == NULL)
421 handler_call_die("trace_begin");
422
423 Py_DECREF(retval);
424 return err;
425error:
426 Py_XDECREF(main_dict);
427 Py_XDECREF(main_module);
428out:
429 return err;
430}
431
432/*
433 * Start trace script
434 */
435static int python_start_script(const char *script, int argc, const char **argv)
436{
437 const char **command_line;
438 char buf[PATH_MAX];
439 int i, err = 0;
440 FILE *fp;
441
442 command_line = malloc((argc + 1) * sizeof(const char *));
443 command_line[0] = script;
444 for (i = 1; i < argc + 1; i++)
445 command_line[i] = argv[i - 1];
446
447 Py_Initialize();
448
449 initperf_trace_context();
450
451 PySys_SetArgv(argc + 1, (char **)command_line);
452
453 fp = fopen(script, "r");
454 if (!fp) {
455 sprintf(buf, "Can't open python script \"%s\"", script);
456 perror(buf);
457 err = -1;
458 goto error;
459 }
460
461 err = PyRun_SimpleFile(fp, script);
462 if (err) {
463 fprintf(stderr, "Error running python script %s\n", script);
464 goto error;
465 }
466
467 err = run_start_sub();
468 if (err) {
469 fprintf(stderr, "Error starting python script %s\n", script);
470 goto error;
471 }
472
473 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600474
475 return err;
476error:
477 Py_Finalize();
478 free(command_line);
479
480 return err;
481}
482
483/*
484 * Stop trace script
485 */
486static int python_stop_script(void)
487{
488 PyObject *handler, *retval;
489 int err = 0;
490
491 handler = PyDict_GetItemString(main_dict, "trace_end");
492 if (handler == NULL || !PyCallable_Check(handler))
493 goto out;
494
495 retval = PyObject_CallObject(handler, NULL);
496 if (retval == NULL)
497 handler_call_die("trace_end");
498 else
499 Py_DECREF(retval);
500out:
501 Py_XDECREF(main_dict);
502 Py_XDECREF(main_module);
503 Py_Finalize();
504
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600505 return err;
506}
507
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300508static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600509{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200510 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600511 struct format_field *f;
512 char fname[PATH_MAX];
513 int not_first, count;
514 FILE *ofp;
515
516 sprintf(fname, "%s.py", outfile);
517 ofp = fopen(fname, "w");
518 if (ofp == NULL) {
519 fprintf(stderr, "couldn't open %s\n", fname);
520 return -1;
521 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100522 fprintf(ofp, "# perf script event handlers, "
523 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600524
525 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
526 " License version 2\n\n");
527
528 fprintf(ofp, "# The common_* event handler fields are the most useful "
529 "fields common to\n");
530
531 fprintf(ofp, "# all events. They don't necessarily correspond to "
532 "the 'common_*' fields\n");
533
534 fprintf(ofp, "# in the format files. Those fields not available as "
535 "handler params can\n");
536
537 fprintf(ofp, "# be retrieved using Python functions of the form "
538 "common_*(context).\n");
539
540 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
541 "of available functions.\n\n");
542
543 fprintf(ofp, "import os\n");
544 fprintf(ofp, "import sys\n\n");
545
546 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
547 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
548 fprintf(ofp, "\nfrom perf_trace_context import *\n");
549 fprintf(ofp, "from Core import *\n\n\n");
550
551 fprintf(ofp, "def trace_begin():\n");
552 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
553
554 fprintf(ofp, "def trace_end():\n");
555 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
556
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300557 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600558 fprintf(ofp, "def %s__%s(", event->system, event->name);
559 fprintf(ofp, "event_name, ");
560 fprintf(ofp, "context, ");
561 fprintf(ofp, "common_cpu,\n");
562 fprintf(ofp, "\tcommon_secs, ");
563 fprintf(ofp, "common_nsecs, ");
564 fprintf(ofp, "common_pid, ");
565 fprintf(ofp, "common_comm,\n\t");
566
567 not_first = 0;
568 count = 0;
569
570 for (f = event->format.fields; f; f = f->next) {
571 if (not_first++)
572 fprintf(ofp, ", ");
573 if (++count % 5 == 0)
574 fprintf(ofp, "\n\t");
575
576 fprintf(ofp, "%s", f->name);
577 }
578 fprintf(ofp, "):\n");
579
580 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
581 "common_secs, common_nsecs,\n\t\t\t"
582 "common_pid, common_comm)\n\n");
583
584 fprintf(ofp, "\t\tprint \"");
585
586 not_first = 0;
587 count = 0;
588
589 for (f = event->format.fields; f; f = f->next) {
590 if (not_first++)
591 fprintf(ofp, ", ");
592 if (count && count % 3 == 0) {
593 fprintf(ofp, "\" \\\n\t\t\"");
594 }
595 count++;
596
597 fprintf(ofp, "%s=", f->name);
598 if (f->flags & FIELD_IS_STRING ||
599 f->flags & FIELD_IS_FLAG ||
600 f->flags & FIELD_IS_SYMBOLIC)
601 fprintf(ofp, "%%s");
602 else if (f->flags & FIELD_IS_SIGNED)
603 fprintf(ofp, "%%d");
604 else
605 fprintf(ofp, "%%u");
606 }
607
608 fprintf(ofp, "\\n\" %% \\\n\t\t(");
609
610 not_first = 0;
611 count = 0;
612
613 for (f = event->format.fields; f; f = f->next) {
614 if (not_first++)
615 fprintf(ofp, ", ");
616
617 if (++count % 5 == 0)
618 fprintf(ofp, "\n\t\t");
619
620 if (f->flags & FIELD_IS_FLAG) {
621 if ((count - 1) % 5 != 0) {
622 fprintf(ofp, "\n\t\t");
623 count = 4;
624 }
625 fprintf(ofp, "flag_str(\"");
626 fprintf(ofp, "%s__%s\", ", event->system,
627 event->name);
628 fprintf(ofp, "\"%s\", %s)", f->name,
629 f->name);
630 } else if (f->flags & FIELD_IS_SYMBOLIC) {
631 if ((count - 1) % 5 != 0) {
632 fprintf(ofp, "\n\t\t");
633 count = 4;
634 }
635 fprintf(ofp, "symbol_str(\"");
636 fprintf(ofp, "%s__%s\", ", event->system,
637 event->name);
638 fprintf(ofp, "\"%s\", %s)", f->name,
639 f->name);
640 } else
641 fprintf(ofp, "%s", f->name);
642 }
643
644 fprintf(ofp, "),\n\n");
645 }
646
647 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200648 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600649
Pierre Tardyc0251482010-05-31 23:12:09 +0200650 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
651 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600652
653 fprintf(ofp, "def print_header("
654 "event_name, cpu, secs, nsecs, pid, comm):\n"
655 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
656 "(event_name, cpu, secs, nsecs, pid, comm),\n");
657
658 fclose(ofp);
659
660 fprintf(stderr, "generated Python script: %s\n", fname);
661
662 return 0;
663}
664
665struct scripting_ops python_scripting_ops = {
666 .name = "Python",
667 .start_script = python_start_script,
668 .stop_script = python_stop_script,
669 .process_event = python_process_event,
670 .generate_script = python_generate_script,
671};