blob: 8006978d83981103b5e6da0844c24fd052dfbbee [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
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300223static void python_process_event(union perf_event *perf_event __unused,
224 struct pevent *pevent,
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 Meloda378962012-06-27 13:08:42 -0300251 pid = trace_parse_common_pid(pevent, 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 Meloda378962012-06-27 13:08:42 -0300296 val = read_size(pevent, data + field->offset,
297 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
341static int run_start_sub(void)
342{
343 PyObject *handler, *retval;
344 int err = 0;
345
346 main_module = PyImport_AddModule("__main__");
347 if (main_module == NULL)
348 return -1;
349 Py_INCREF(main_module);
350
351 main_dict = PyModule_GetDict(main_module);
352 if (main_dict == NULL) {
353 err = -1;
354 goto error;
355 }
356 Py_INCREF(main_dict);
357
358 handler = PyDict_GetItemString(main_dict, "trace_begin");
359 if (handler == NULL || !PyCallable_Check(handler))
360 goto out;
361
362 retval = PyObject_CallObject(handler, NULL);
363 if (retval == NULL)
364 handler_call_die("trace_begin");
365
366 Py_DECREF(retval);
367 return err;
368error:
369 Py_XDECREF(main_dict);
370 Py_XDECREF(main_module);
371out:
372 return err;
373}
374
375/*
376 * Start trace script
377 */
378static int python_start_script(const char *script, int argc, const char **argv)
379{
380 const char **command_line;
381 char buf[PATH_MAX];
382 int i, err = 0;
383 FILE *fp;
384
385 command_line = malloc((argc + 1) * sizeof(const char *));
386 command_line[0] = script;
387 for (i = 1; i < argc + 1; i++)
388 command_line[i] = argv[i - 1];
389
390 Py_Initialize();
391
392 initperf_trace_context();
393
394 PySys_SetArgv(argc + 1, (char **)command_line);
395
396 fp = fopen(script, "r");
397 if (!fp) {
398 sprintf(buf, "Can't open python script \"%s\"", script);
399 perror(buf);
400 err = -1;
401 goto error;
402 }
403
404 err = PyRun_SimpleFile(fp, script);
405 if (err) {
406 fprintf(stderr, "Error running python script %s\n", script);
407 goto error;
408 }
409
410 err = run_start_sub();
411 if (err) {
412 fprintf(stderr, "Error starting python script %s\n", script);
413 goto error;
414 }
415
416 free(command_line);
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600417
418 return err;
419error:
420 Py_Finalize();
421 free(command_line);
422
423 return err;
424}
425
426/*
427 * Stop trace script
428 */
429static int python_stop_script(void)
430{
431 PyObject *handler, *retval;
432 int err = 0;
433
434 handler = PyDict_GetItemString(main_dict, "trace_end");
435 if (handler == NULL || !PyCallable_Check(handler))
436 goto out;
437
438 retval = PyObject_CallObject(handler, NULL);
439 if (retval == NULL)
440 handler_call_die("trace_end");
441 else
442 Py_DECREF(retval);
443out:
444 Py_XDECREF(main_dict);
445 Py_XDECREF(main_module);
446 Py_Finalize();
447
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600448 return err;
449}
450
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300451static int python_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600452{
Steven Rostedtaaf045f2012-04-06 00:47:56 +0200453 struct event_format *event = NULL;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600454 struct format_field *f;
455 char fname[PATH_MAX];
456 int not_first, count;
457 FILE *ofp;
458
459 sprintf(fname, "%s.py", outfile);
460 ofp = fopen(fname, "w");
461 if (ofp == NULL) {
462 fprintf(stderr, "couldn't open %s\n", fname);
463 return -1;
464 }
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100465 fprintf(ofp, "# perf script event handlers, "
466 "generated by perf script -g python\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600467
468 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
469 " License version 2\n\n");
470
471 fprintf(ofp, "# The common_* event handler fields are the most useful "
472 "fields common to\n");
473
474 fprintf(ofp, "# all events. They don't necessarily correspond to "
475 "the 'common_*' fields\n");
476
477 fprintf(ofp, "# in the format files. Those fields not available as "
478 "handler params can\n");
479
480 fprintf(ofp, "# be retrieved using Python functions of the form "
481 "common_*(context).\n");
482
483 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
484 "of available functions.\n\n");
485
486 fprintf(ofp, "import os\n");
487 fprintf(ofp, "import sys\n\n");
488
489 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
490 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
491 fprintf(ofp, "\nfrom perf_trace_context import *\n");
492 fprintf(ofp, "from Core import *\n\n\n");
493
494 fprintf(ofp, "def trace_begin():\n");
495 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
496
497 fprintf(ofp, "def trace_end():\n");
498 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
499
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300500 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600501 fprintf(ofp, "def %s__%s(", event->system, event->name);
502 fprintf(ofp, "event_name, ");
503 fprintf(ofp, "context, ");
504 fprintf(ofp, "common_cpu,\n");
505 fprintf(ofp, "\tcommon_secs, ");
506 fprintf(ofp, "common_nsecs, ");
507 fprintf(ofp, "common_pid, ");
508 fprintf(ofp, "common_comm,\n\t");
509
510 not_first = 0;
511 count = 0;
512
513 for (f = event->format.fields; f; f = f->next) {
514 if (not_first++)
515 fprintf(ofp, ", ");
516 if (++count % 5 == 0)
517 fprintf(ofp, "\n\t");
518
519 fprintf(ofp, "%s", f->name);
520 }
521 fprintf(ofp, "):\n");
522
523 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
524 "common_secs, common_nsecs,\n\t\t\t"
525 "common_pid, common_comm)\n\n");
526
527 fprintf(ofp, "\t\tprint \"");
528
529 not_first = 0;
530 count = 0;
531
532 for (f = event->format.fields; f; f = f->next) {
533 if (not_first++)
534 fprintf(ofp, ", ");
535 if (count && count % 3 == 0) {
536 fprintf(ofp, "\" \\\n\t\t\"");
537 }
538 count++;
539
540 fprintf(ofp, "%s=", f->name);
541 if (f->flags & FIELD_IS_STRING ||
542 f->flags & FIELD_IS_FLAG ||
543 f->flags & FIELD_IS_SYMBOLIC)
544 fprintf(ofp, "%%s");
545 else if (f->flags & FIELD_IS_SIGNED)
546 fprintf(ofp, "%%d");
547 else
548 fprintf(ofp, "%%u");
549 }
550
551 fprintf(ofp, "\\n\" %% \\\n\t\t(");
552
553 not_first = 0;
554 count = 0;
555
556 for (f = event->format.fields; f; f = f->next) {
557 if (not_first++)
558 fprintf(ofp, ", ");
559
560 if (++count % 5 == 0)
561 fprintf(ofp, "\n\t\t");
562
563 if (f->flags & FIELD_IS_FLAG) {
564 if ((count - 1) % 5 != 0) {
565 fprintf(ofp, "\n\t\t");
566 count = 4;
567 }
568 fprintf(ofp, "flag_str(\"");
569 fprintf(ofp, "%s__%s\", ", event->system,
570 event->name);
571 fprintf(ofp, "\"%s\", %s)", f->name,
572 f->name);
573 } else if (f->flags & FIELD_IS_SYMBOLIC) {
574 if ((count - 1) % 5 != 0) {
575 fprintf(ofp, "\n\t\t");
576 count = 4;
577 }
578 fprintf(ofp, "symbol_str(\"");
579 fprintf(ofp, "%s__%s\", ", event->system,
580 event->name);
581 fprintf(ofp, "\"%s\", %s)", f->name,
582 f->name);
583 } else
584 fprintf(ofp, "%s", f->name);
585 }
586
587 fprintf(ofp, "),\n\n");
588 }
589
590 fprintf(ofp, "def trace_unhandled(event_name, context, "
Pierre Tardyc0251482010-05-31 23:12:09 +0200591 "event_fields_dict):\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600592
Pierre Tardyc0251482010-05-31 23:12:09 +0200593 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
594 "for k,v in sorted(event_fields_dict.items())])\n\n");
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600595
596 fprintf(ofp, "def print_header("
597 "event_name, cpu, secs, nsecs, pid, comm):\n"
598 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
599 "(event_name, cpu, secs, nsecs, pid, comm),\n");
600
601 fclose(ofp);
602
603 fprintf(stderr, "generated Python script: %s\n", fname);
604
605 return 0;
606}
607
608struct scripting_ops python_scripting_ops = {
609 .name = "Python",
610 .start_script = python_start_script,
611 .stop_script = python_stop_script,
612 .process_event = python_process_event,
613 .generate_script = python_generate_script,
614};