blob: b9010d878b4bf0a749a34fff7900423783ce4701 [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,
David Ahernbe6d8422011-03-09 22:23:23 -0700228 struct thread *thread)
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;
241 char *comm = thread->comm;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600242
243 t = PyTuple_New(MAX_FIELDS);
244 if (!t)
245 Py_FatalError("couldn't create Python tuple");
246
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300247 event = find_cache_event(evsel);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600248 if (!event)
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300249 die("ug! no event found for type %d", (int)evsel->attr.config);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600250
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300251 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600252
253 sprintf(handler_name, "%s__%s", event->system, event->name);
254
Pierre Tardyc0251482010-05-31 23:12:09 +0200255 handler = PyDict_GetItemString(main_dict, handler_name);
256 if (handler && !PyCallable_Check(handler))
257 handler = NULL;
258 if (!handler) {
259 dict = PyDict_New();
260 if (!dict)
261 Py_FatalError("couldn't create Python dict");
262 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600263 s = nsecs / NSECS_PER_SEC;
264 ns = nsecs - s * NSECS_PER_SEC;
265
266 scripting_context->event_data = data;
267
268 context = PyCObject_FromVoidPtr(scripting_context, NULL);
269
270 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
Kyle McMartinfb7d0b32011-01-24 11:13:04 -0500271 PyTuple_SetItem(t, n++, context);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600272
Pierre Tardyc0251482010-05-31 23:12:09 +0200273 if (handler) {
274 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
275 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
276 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
277 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
278 PyTuple_SetItem(t, n++, PyString_FromString(comm));
279 } else {
280 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
281 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
282 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
283 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
284 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
285 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600286 for (field = event->format.fields; field; field = field->next) {
287 if (field->flags & FIELD_IS_STRING) {
288 int offset;
289 if (field->flags & FIELD_IS_DYNAMIC) {
290 offset = *(int *)(data + field->offset);
291 offset &= 0xffff;
292 } else
293 offset = field->offset;
Tom Zanussib1dcc032010-04-01 23:58:25 -0500294 obj = PyString_FromString((char *)data + offset);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600295 } else { /* FIELD_IS_NUMERIC */
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300296 val = read_size(event, data + field->offset,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300297 field->size);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600298 if (field->flags & FIELD_IS_SIGNED) {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500299 if ((long long)val >= LONG_MIN &&
300 (long long)val <= LONG_MAX)
301 obj = PyInt_FromLong(val);
302 else
303 obj = PyLong_FromLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600304 } else {
Tom Zanussib1dcc032010-04-01 23:58:25 -0500305 if (val <= LONG_MAX)
306 obj = PyInt_FromLong(val);
307 else
308 obj = PyLong_FromUnsignedLongLong(val);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600309 }
310 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200311 if (handler)
312 PyTuple_SetItem(t, n++, obj);
313 else
314 PyDict_SetItemString(dict, field->name, obj);
315
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600316 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200317 if (!handler)
318 PyTuple_SetItem(t, n++, dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600319
320 if (_PyTuple_Resize(&t, n) == -1)
321 Py_FatalError("error resizing Python tuple");
322
Pierre Tardyc0251482010-05-31 23:12:09 +0200323 if (handler) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600324 retval = PyObject_CallObject(handler, t);
325 if (retval == NULL)
326 handler_call_die(handler_name);
327 } else {
328 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
329 if (handler && PyCallable_Check(handler)) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600330
331 retval = PyObject_CallObject(handler, t);
332 if (retval == NULL)
333 handler_call_die("trace_unhandled");
334 }
Pierre Tardyc0251482010-05-31 23:12:09 +0200335 Py_DECREF(dict);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600336 }
337
338 Py_DECREF(t);
339}
340
Feng Tang6a6daec2012-08-08 17:57:51 +0800341static void python_process_general_event(union perf_event *perf_event __unused,
342 struct perf_sample *sample,
343 struct perf_evsel *evsel,
344 struct machine *machine __unused,
345 struct thread *thread __unused)
346{
347 PyObject *handler, *retval, *t;
348 static char handler_name[64];
349 unsigned n = 0;
350 void *data = sample->raw_data;
351
352 t = PyTuple_New(MAX_FIELDS);
353 if (!t)
354 Py_FatalError("couldn't create Python tuple");
355
356 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
357
358 handler = PyDict_GetItemString(main_dict, handler_name);
359 if (handler && !PyCallable_Check(handler)) {
360 handler = NULL;
361 goto exit;
362 }
363
364 /* Pass 3 parameters: event_attr, perf_sample, raw data */
365 PyTuple_SetItem(t, n++, PyString_FromStringAndSize((void *)&evsel->attr, sizeof(evsel->attr)));
366 PyTuple_SetItem(t, n++, PyString_FromStringAndSize((void *)sample, sizeof(*sample)));
367 PyTuple_SetItem(t, n++, PyString_FromStringAndSize(data, sample->raw_size));
368
369 if (_PyTuple_Resize(&t, n) == -1)
370 Py_FatalError("error resizing Python tuple");
371
372 retval = PyObject_CallObject(handler, t);
373 if (retval == NULL)
374 handler_call_die(handler_name);
375exit:
376 Py_DECREF(t);
377}
378
379static void python_process_event(union perf_event *perf_event,
380 struct perf_sample *sample,
381 struct perf_evsel *evsel,
382 struct machine *machine,
383 struct thread *thread)
384{
385 switch (evsel->attr.type) {
386 case PERF_TYPE_TRACEPOINT:
387 python_process_tracepoint(perf_event, sample, evsel,
388 machine, thread);
389 break;
390 /* Reserve for future process_hw/sw/raw APIs */
391 default:
392 python_process_general_event(perf_event, sample, evsel,
393 machine, thread);
394 }
395}
396
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600397static int run_start_sub(void)
398{
399 PyObject *handler, *retval;
400 int err = 0;
401
402 main_module = PyImport_AddModule("__main__");
403 if (main_module == NULL)
404 return -1;
405 Py_INCREF(main_module);
406
407 main_dict = PyModule_GetDict(main_module);
408 if (main_dict == NULL) {
409 err = -1;
410 goto error;
411 }
412 Py_INCREF(main_dict);
413
414 handler = PyDict_GetItemString(main_dict, "trace_begin");
415 if (handler == NULL || !PyCallable_Check(handler))
416 goto out;
417
418 retval = PyObject_CallObject(handler, NULL);
419 if (retval == NULL)
420 handler_call_die("trace_begin");
421
422 Py_DECREF(retval);
423 return err;
424error:
425 Py_XDECREF(main_dict);
426 Py_XDECREF(main_module);
427out:
428 return err;
429}
430
431/*
432 * Start trace script
433 */
434static int python_start_script(const char *script, int argc, const char **argv)
435{
436 const char **command_line;
437 char buf[PATH_MAX];
438 int i, err = 0;
439 FILE *fp;
440
441 command_line = malloc((argc + 1) * sizeof(const char *));
442 command_line[0] = script;
443 for (i = 1; i < argc + 1; i++)
444 command_line[i] = argv[i - 1];
445
446 Py_Initialize();
447
448 initperf_trace_context();
449
450 PySys_SetArgv(argc + 1, (char **)command_line);
451
452 fp = fopen(script, "r");
453 if (!fp) {
454 sprintf(buf, "Can't open python script \"%s\"", script);
455 perror(buf);
456 err = -1;
457 goto error;
458 }
459
460 err = PyRun_SimpleFile(fp, script);
461 if (err) {
462 fprintf(stderr, "Error running python script %s\n", script);
463 goto error;
464 }
465
466 err = run_start_sub();
467 if (err) {
468 fprintf(stderr, "Error starting python script %s\n", script);
469 goto error;
470 }
471
472 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600473
474 return err;
475error:
476 Py_Finalize();
477 free(command_line);
478
479 return err;
480}
481
482/*
483 * Stop trace script
484 */
485static int python_stop_script(void)
486{
487 PyObject *handler, *retval;
488 int err = 0;
489
490 handler = PyDict_GetItemString(main_dict, "trace_end");
491 if (handler == NULL || !PyCallable_Check(handler))
492 goto out;
493
494 retval = PyObject_CallObject(handler, NULL);
495 if (retval == NULL)
496 handler_call_die("trace_end");
497 else
498 Py_DECREF(retval);
499out:
500 Py_XDECREF(main_dict);
501 Py_XDECREF(main_module);
502 Py_Finalize();
503
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600504 return err;
505}
506
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300507static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600508{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200509 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600510 struct format_field *f;
511 char fname[PATH_MAX];
512 int not_first, count;
513 FILE *ofp;
514
515 sprintf(fname, "%s.py", outfile);
516 ofp = fopen(fname, "w");
517 if (ofp == NULL) {
518 fprintf(stderr, "couldn't open %s\n", fname);
519 return -1;
520 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100521 fprintf(ofp, "# perf script event handlers, "
522 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600523
524 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
525 " License version 2\n\n");
526
527 fprintf(ofp, "# The common_* event handler fields are the most useful "
528 "fields common to\n");
529
530 fprintf(ofp, "# all events. They don't necessarily correspond to "
531 "the 'common_*' fields\n");
532
533 fprintf(ofp, "# in the format files. Those fields not available as "
534 "handler params can\n");
535
536 fprintf(ofp, "# be retrieved using Python functions of the form "
537 "common_*(context).\n");
538
539 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
540 "of available functions.\n\n");
541
542 fprintf(ofp, "import os\n");
543 fprintf(ofp, "import sys\n\n");
544
545 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
546 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
547 fprintf(ofp, "\nfrom perf_trace_context import *\n");
548 fprintf(ofp, "from Core import *\n\n\n");
549
550 fprintf(ofp, "def trace_begin():\n");
551 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
552
553 fprintf(ofp, "def trace_end():\n");
554 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
555
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300556 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600557 fprintf(ofp, "def %s__%s(", event->system, event->name);
558 fprintf(ofp, "event_name, ");
559 fprintf(ofp, "context, ");
560 fprintf(ofp, "common_cpu,\n");
561 fprintf(ofp, "\tcommon_secs, ");
562 fprintf(ofp, "common_nsecs, ");
563 fprintf(ofp, "common_pid, ");
564 fprintf(ofp, "common_comm,\n\t");
565
566 not_first = 0;
567 count = 0;
568
569 for (f = event->format.fields; f; f = f->next) {
570 if (not_first++)
571 fprintf(ofp, ", ");
572 if (++count % 5 == 0)
573 fprintf(ofp, "\n\t");
574
575 fprintf(ofp, "%s", f->name);
576 }
577 fprintf(ofp, "):\n");
578
579 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
580 "common_secs, common_nsecs,\n\t\t\t"
581 "common_pid, common_comm)\n\n");
582
583 fprintf(ofp, "\t\tprint \"");
584
585 not_first = 0;
586 count = 0;
587
588 for (f = event->format.fields; f; f = f->next) {
589 if (not_first++)
590 fprintf(ofp, ", ");
591 if (count && count % 3 == 0) {
592 fprintf(ofp, "\" \\\n\t\t\"");
593 }
594 count++;
595
596 fprintf(ofp, "%s=", f->name);
597 if (f->flags & FIELD_IS_STRING ||
598 f->flags & FIELD_IS_FLAG ||
599 f->flags & FIELD_IS_SYMBOLIC)
600 fprintf(ofp, "%%s");
601 else if (f->flags & FIELD_IS_SIGNED)
602 fprintf(ofp, "%%d");
603 else
604 fprintf(ofp, "%%u");
605 }
606
607 fprintf(ofp, "\\n\" %% \\\n\t\t(");
608
609 not_first = 0;
610 count = 0;
611
612 for (f = event->format.fields; f; f = f->next) {
613 if (not_first++)
614 fprintf(ofp, ", ");
615
616 if (++count % 5 == 0)
617 fprintf(ofp, "\n\t\t");
618
619 if (f->flags & FIELD_IS_FLAG) {
620 if ((count - 1) % 5 != 0) {
621 fprintf(ofp, "\n\t\t");
622 count = 4;
623 }
624 fprintf(ofp, "flag_str(\"");
625 fprintf(ofp, "%s__%s\", ", event->system,
626 event->name);
627 fprintf(ofp, "\"%s\", %s)", f->name,
628 f->name);
629 } else if (f->flags & FIELD_IS_SYMBOLIC) {
630 if ((count - 1) % 5 != 0) {
631 fprintf(ofp, "\n\t\t");
632 count = 4;
633 }
634 fprintf(ofp, "symbol_str(\"");
635 fprintf(ofp, "%s__%s\", ", event->system,
636 event->name);
637 fprintf(ofp, "\"%s\", %s)", f->name,
638 f->name);
639 } else
640 fprintf(ofp, "%s", f->name);
641 }
642
643 fprintf(ofp, "),\n\n");
644 }
645
646 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200647 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600648
Pierre Tardyc0251482010-05-31 23:12:09 +0200649 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
650 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600651
652 fprintf(ofp, "def print_header("
653 "event_name, cpu, secs, nsecs, pid, comm):\n"
654 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
655 "(event_name, cpu, secs, nsecs, pid, comm),\n");
656
657 fclose(ofp);
658
659 fprintf(stderr, "generated Python script: %s\n", fname);
660
661 return 0;
662}
663
664struct scripting_ops python_scripting_ops = {
665 .name = "Python",
666 .start_script = python_start_script,
667 .stop_script = python_stop_script,
668 .process_event = python_process_event,
669 .generate_script = python_generate_script,
670};