Commit | Line | Data |
---|---|---|
7e4b21b8 TZ |
1 | /* |
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> | |
7e4b21b8 TZ |
27 | #include <errno.h> |
28 | ||
29 | #include "../../perf.h" | |
fcf65bf1 | 30 | #include "../evsel.h" |
7e4b21b8 | 31 | #include "../util.h" |
743eb868 ACM |
32 | #include "../event.h" |
33 | #include "../thread.h" | |
7e4b21b8 | 34 | #include "../trace-event.h" |
6a6daec2 | 35 | #include "../evsel.h" |
7e4b21b8 TZ |
36 | |
37 | PyMODINIT_FUNC initperf_trace_context(void); | |
38 | ||
39 | #define FTRACE_MAX_EVENT \ | |
40 | ((1 << (sizeof(unsigned short) * 8)) - 1) | |
41 | ||
aaf045f7 | 42 | struct event_format *events[FTRACE_MAX_EVENT]; |
7e4b21b8 TZ |
43 | |
44 | #define MAX_FIELDS 64 | |
45 | #define N_COMMON_FIELDS 7 | |
46 | ||
47 | extern struct scripting_context *scripting_context; | |
48 | ||
49 | static char *cur_field_name; | |
50 | static int zero_flag_atom; | |
51 | ||
52 | static PyObject *main_module, *main_dict; | |
53 | ||
54 | static void handler_call_die(const char *handler_name) | |
55 | { | |
56 | PyErr_Print(); | |
57 | Py_FatalError("problem in Python trace event handler"); | |
58 | } | |
59 | ||
60 | static 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 | ||
44ad9cd8 | 74 | t = PyTuple_New(4); |
7e4b21b8 TZ |
75 | 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 | ||
7e4b21b8 TZ |
85 | 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 | ||
95 | static 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 | ||
107 | static 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 | ||
44ad9cd8 TZ |
119 | if (field_type == PRINT_FLAGS) |
120 | t = PyTuple_New(3); | |
121 | else | |
122 | t = PyTuple_New(2); | |
7e4b21b8 TZ |
123 | 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 | ||
7e4b21b8 TZ |
131 | 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 | ||
aaf045f7 | 141 | static void define_event_symbols(struct event_format *event, |
7e4b21b8 TZ |
142 | 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; | |
e080e6f1 NK |
171 | 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; | |
7e4b21b8 TZ |
175 | 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: | |
aaf045f7 SR |
187 | /* gcc warns for these? */ |
188 | case PRINT_BSTRING: | |
189 | case PRINT_DYNAMIC_ARRAY: | |
190 | case PRINT_FUNC: | |
7e4b21b8 TZ |
191 | /* we should warn... */ |
192 | return; | |
193 | } | |
194 | ||
195 | if (args->next) | |
196 | define_event_symbols(event, ev_name, args->next); | |
197 | } | |
198 | ||
fcf65bf1 | 199 | static inline struct event_format *find_cache_event(struct perf_evsel *evsel) |
7e4b21b8 TZ |
200 | { |
201 | static char ev_name[256]; | |
aaf045f7 | 202 | struct event_format *event; |
fcf65bf1 | 203 | int type = evsel->attr.config; |
7e4b21b8 | 204 | |
fcf65bf1 ACM |
205 | /* |
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 | */ | |
7e4b21b8 TZ |
210 | if (events[type]) |
211 | return events[type]; | |
212 | ||
fcf65bf1 | 213 | events[type] = event = evsel->tp_format; |
7e4b21b8 TZ |
214 | 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 | ||
6a6daec2 | 224 | static void python_process_tracepoint(union perf_event *perf_event __unused, |
be6d842a | 225 | struct perf_sample *sample, |
fcf65bf1 | 226 | struct perf_evsel *evsel, |
743eb868 | 227 | struct machine *machine __unused, |
73994dc1 | 228 | struct addr_location *al) |
7e4b21b8 | 229 | { |
c0251485 | 230 | PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; |
7e4b21b8 TZ |
231 | static char handler_name[256]; |
232 | struct format_field *field; | |
233 | unsigned long long val; | |
234 | unsigned long s, ns; | |
aaf045f7 | 235 | struct event_format *event; |
7e4b21b8 | 236 | unsigned n = 0; |
7e4b21b8 | 237 | int pid; |
be6d842a DA |
238 | int cpu = sample->cpu; |
239 | void *data = sample->raw_data; | |
240 | unsigned long long nsecs = sample->time; | |
73994dc1 | 241 | struct thread *thread = al->thread; |
be6d842a | 242 | char *comm = thread->comm; |
7e4b21b8 TZ |
243 | |
244 | t = PyTuple_New(MAX_FIELDS); | |
245 | if (!t) | |
246 | Py_FatalError("couldn't create Python tuple"); | |
247 | ||
fcf65bf1 | 248 | event = find_cache_event(evsel); |
7e4b21b8 | 249 | if (!event) |
fcf65bf1 | 250 | die("ug! no event found for type %d", (int)evsel->attr.config); |
7e4b21b8 | 251 | |
97822433 | 252 | pid = raw_field_value(event, "common_pid", data); |
7e4b21b8 TZ |
253 | |
254 | sprintf(handler_name, "%s__%s", event->system, event->name); | |
255 | ||
c0251485 PT |
256 | 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 | } | |
7e4b21b8 TZ |
264 | 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)); | |
fb7d0b3c | 272 | PyTuple_SetItem(t, n++, context); |
7e4b21b8 | 273 | |
c0251485 PT |
274 | 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 | } | |
7e4b21b8 TZ |
287 | 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; | |
b1dcc03c | 295 | obj = PyString_FromString((char *)data + offset); |
7e4b21b8 | 296 | } else { /* FIELD_IS_NUMERIC */ |
97822433 | 297 | val = read_size(event, data + field->offset, |
da378962 | 298 | field->size); |
7e4b21b8 | 299 | if (field->flags & FIELD_IS_SIGNED) { |
b1dcc03c TZ |
300 | if ((long long)val >= LONG_MIN && |
301 | (long long)val <= LONG_MAX) | |
302 | obj = PyInt_FromLong(val); | |
303 | else | |
304 | obj = PyLong_FromLongLong(val); | |
7e4b21b8 | 305 | } else { |
b1dcc03c TZ |
306 | if (val <= LONG_MAX) |
307 | obj = PyInt_FromLong(val); | |
308 | else | |
309 | obj = PyLong_FromUnsignedLongLong(val); | |
7e4b21b8 TZ |
310 | } |
311 | } | |
c0251485 PT |
312 | if (handler) |
313 | PyTuple_SetItem(t, n++, obj); | |
314 | else | |
315 | PyDict_SetItemString(dict, field->name, obj); | |
316 | ||
7e4b21b8 | 317 | } |
c0251485 PT |
318 | if (!handler) |
319 | PyTuple_SetItem(t, n++, dict); | |
7e4b21b8 TZ |
320 | |
321 | if (_PyTuple_Resize(&t, n) == -1) | |
322 | Py_FatalError("error resizing Python tuple"); | |
323 | ||
c0251485 | 324 | if (handler) { |
7e4b21b8 TZ |
325 | 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)) { | |
7e4b21b8 TZ |
331 | |
332 | retval = PyObject_CallObject(handler, t); | |
333 | if (retval == NULL) | |
334 | handler_call_die("trace_unhandled"); | |
335 | } | |
c0251485 | 336 | Py_DECREF(dict); |
7e4b21b8 TZ |
337 | } |
338 | ||
339 | Py_DECREF(t); | |
340 | } | |
341 | ||
6a6daec2 FT |
342 | static 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, | |
87b6a3ad | 346 | struct addr_location *al) |
6a6daec2 | 347 | { |
fd6b858a | 348 | PyObject *handler, *retval, *t, *dict; |
6a6daec2 FT |
349 | static char handler_name[64]; |
350 | unsigned n = 0; | |
fd6b858a | 351 | struct thread *thread = al->thread; |
6a6daec2 | 352 | |
fd6b858a FT |
353 | /* |
354 | * Use the MAX_FIELDS to make the function expandable, though | |
87b6a3ad | 355 | * currently there is only one item for the tuple. |
fd6b858a | 356 | */ |
6a6daec2 FT |
357 | t = PyTuple_New(MAX_FIELDS); |
358 | if (!t) | |
359 | Py_FatalError("couldn't create Python tuple"); | |
360 | ||
fd6b858a FT |
361 | dict = PyDict_New(); |
362 | if (!dict) | |
363 | Py_FatalError("couldn't create Python dictionary"); | |
364 | ||
6a6daec2 FT |
365 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
366 | ||
367 | handler = PyDict_GetItemString(main_dict, handler_name); | |
87b6a3ad | 368 | if (!handler || !PyCallable_Check(handler)) |
6a6daec2 | 369 | goto exit; |
6a6daec2 | 370 | |
fd6b858a FT |
371 | PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
372 | PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize( | |
373 | (const char *)&evsel->attr, sizeof(evsel->attr))); | |
374 | PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize( | |
375 | (const char *)sample, sizeof(*sample))); | |
376 | PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize( | |
377 | (const char *)sample->raw_data, sample->raw_size)); | |
378 | PyDict_SetItemString(dict, "comm", | |
379 | PyString_FromString(thread->comm)); | |
380 | if (al->map) { | |
381 | PyDict_SetItemString(dict, "dso", | |
382 | PyString_FromString(al->map->dso->name)); | |
383 | } | |
384 | if (al->sym) { | |
385 | PyDict_SetItemString(dict, "symbol", | |
386 | PyString_FromString(al->sym->name)); | |
387 | } | |
6a6daec2 | 388 | |
fd6b858a | 389 | PyTuple_SetItem(t, n++, dict); |
6a6daec2 FT |
390 | if (_PyTuple_Resize(&t, n) == -1) |
391 | Py_FatalError("error resizing Python tuple"); | |
392 | ||
393 | retval = PyObject_CallObject(handler, t); | |
394 | if (retval == NULL) | |
395 | handler_call_die(handler_name); | |
396 | exit: | |
fd6b858a | 397 | Py_DECREF(dict); |
6a6daec2 FT |
398 | Py_DECREF(t); |
399 | } | |
400 | ||
401 | static void python_process_event(union perf_event *perf_event, | |
402 | struct perf_sample *sample, | |
403 | struct perf_evsel *evsel, | |
404 | struct machine *machine, | |
73994dc1 | 405 | struct addr_location *al) |
6a6daec2 FT |
406 | { |
407 | switch (evsel->attr.type) { | |
408 | case PERF_TYPE_TRACEPOINT: | |
409 | python_process_tracepoint(perf_event, sample, evsel, | |
73994dc1 | 410 | machine, al); |
6a6daec2 FT |
411 | break; |
412 | /* Reserve for future process_hw/sw/raw APIs */ | |
413 | default: | |
414 | python_process_general_event(perf_event, sample, evsel, | |
73994dc1 | 415 | machine, al); |
6a6daec2 FT |
416 | } |
417 | } | |
418 | ||
7e4b21b8 TZ |
419 | static int run_start_sub(void) |
420 | { | |
421 | PyObject *handler, *retval; | |
422 | int err = 0; | |
423 | ||
424 | main_module = PyImport_AddModule("__main__"); | |
425 | if (main_module == NULL) | |
426 | return -1; | |
427 | Py_INCREF(main_module); | |
428 | ||
429 | main_dict = PyModule_GetDict(main_module); | |
430 | if (main_dict == NULL) { | |
431 | err = -1; | |
432 | goto error; | |
433 | } | |
434 | Py_INCREF(main_dict); | |
435 | ||
436 | handler = PyDict_GetItemString(main_dict, "trace_begin"); | |
437 | if (handler == NULL || !PyCallable_Check(handler)) | |
438 | goto out; | |
439 | ||
440 | retval = PyObject_CallObject(handler, NULL); | |
441 | if (retval == NULL) | |
442 | handler_call_die("trace_begin"); | |
443 | ||
444 | Py_DECREF(retval); | |
445 | return err; | |
446 | error: | |
447 | Py_XDECREF(main_dict); | |
448 | Py_XDECREF(main_module); | |
449 | out: | |
450 | return err; | |
451 | } | |
452 | ||
453 | /* | |
454 | * Start trace script | |
455 | */ | |
456 | static int python_start_script(const char *script, int argc, const char **argv) | |
457 | { | |
458 | const char **command_line; | |
459 | char buf[PATH_MAX]; | |
460 | int i, err = 0; | |
461 | FILE *fp; | |
462 | ||
463 | command_line = malloc((argc + 1) * sizeof(const char *)); | |
464 | command_line[0] = script; | |
465 | for (i = 1; i < argc + 1; i++) | |
466 | command_line[i] = argv[i - 1]; | |
467 | ||
468 | Py_Initialize(); | |
469 | ||
470 | initperf_trace_context(); | |
471 | ||
472 | PySys_SetArgv(argc + 1, (char **)command_line); | |
473 | ||
474 | fp = fopen(script, "r"); | |
475 | if (!fp) { | |
476 | sprintf(buf, "Can't open python script \"%s\"", script); | |
477 | perror(buf); | |
478 | err = -1; | |
479 | goto error; | |
480 | } | |
481 | ||
482 | err = PyRun_SimpleFile(fp, script); | |
483 | if (err) { | |
484 | fprintf(stderr, "Error running python script %s\n", script); | |
485 | goto error; | |
486 | } | |
487 | ||
488 | err = run_start_sub(); | |
489 | if (err) { | |
490 | fprintf(stderr, "Error starting python script %s\n", script); | |
491 | goto error; | |
492 | } | |
493 | ||
494 | free(command_line); | |
7e4b21b8 TZ |
495 | |
496 | return err; | |
497 | error: | |
498 | Py_Finalize(); | |
499 | free(command_line); | |
500 | ||
501 | return err; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Stop trace script | |
506 | */ | |
507 | static int python_stop_script(void) | |
508 | { | |
509 | PyObject *handler, *retval; | |
510 | int err = 0; | |
511 | ||
512 | handler = PyDict_GetItemString(main_dict, "trace_end"); | |
513 | if (handler == NULL || !PyCallable_Check(handler)) | |
514 | goto out; | |
515 | ||
516 | retval = PyObject_CallObject(handler, NULL); | |
517 | if (retval == NULL) | |
518 | handler_call_die("trace_end"); | |
519 | else | |
520 | Py_DECREF(retval); | |
521 | out: | |
522 | Py_XDECREF(main_dict); | |
523 | Py_XDECREF(main_module); | |
524 | Py_Finalize(); | |
525 | ||
7e4b21b8 TZ |
526 | return err; |
527 | } | |
528 | ||
da378962 | 529 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
7e4b21b8 | 530 | { |
aaf045f7 | 531 | struct event_format *event = NULL; |
7e4b21b8 TZ |
532 | struct format_field *f; |
533 | char fname[PATH_MAX]; | |
534 | int not_first, count; | |
535 | FILE *ofp; | |
536 | ||
537 | sprintf(fname, "%s.py", outfile); | |
538 | ofp = fopen(fname, "w"); | |
539 | if (ofp == NULL) { | |
540 | fprintf(stderr, "couldn't open %s\n", fname); | |
541 | return -1; | |
542 | } | |
133dc4c3 IM |
543 | fprintf(ofp, "# perf script event handlers, " |
544 | "generated by perf script -g python\n"); | |
7e4b21b8 TZ |
545 | |
546 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" | |
547 | " License version 2\n\n"); | |
548 | ||
549 | fprintf(ofp, "# The common_* event handler fields are the most useful " | |
550 | "fields common to\n"); | |
551 | ||
552 | fprintf(ofp, "# all events. They don't necessarily correspond to " | |
553 | "the 'common_*' fields\n"); | |
554 | ||
555 | fprintf(ofp, "# in the format files. Those fields not available as " | |
556 | "handler params can\n"); | |
557 | ||
558 | fprintf(ofp, "# be retrieved using Python functions of the form " | |
559 | "common_*(context).\n"); | |
560 | ||
561 | fprintf(ofp, "# See the perf-trace-python Documentation for the list " | |
562 | "of available functions.\n\n"); | |
563 | ||
564 | fprintf(ofp, "import os\n"); | |
565 | fprintf(ofp, "import sys\n\n"); | |
566 | ||
567 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); | |
568 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); | |
569 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); | |
570 | fprintf(ofp, "from Core import *\n\n\n"); | |
571 | ||
572 | fprintf(ofp, "def trace_begin():\n"); | |
573 | fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); | |
574 | ||
575 | fprintf(ofp, "def trace_end():\n"); | |
576 | fprintf(ofp, "\tprint \"in trace_end\"\n\n"); | |
577 | ||
da378962 | 578 | while ((event = trace_find_next_event(pevent, event))) { |
7e4b21b8 TZ |
579 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
580 | fprintf(ofp, "event_name, "); | |
581 | fprintf(ofp, "context, "); | |
582 | fprintf(ofp, "common_cpu,\n"); | |
583 | fprintf(ofp, "\tcommon_secs, "); | |
584 | fprintf(ofp, "common_nsecs, "); | |
585 | fprintf(ofp, "common_pid, "); | |
586 | fprintf(ofp, "common_comm,\n\t"); | |
587 | ||
588 | not_first = 0; | |
589 | count = 0; | |
590 | ||
591 | for (f = event->format.fields; f; f = f->next) { | |
592 | if (not_first++) | |
593 | fprintf(ofp, ", "); | |
594 | if (++count % 5 == 0) | |
595 | fprintf(ofp, "\n\t"); | |
596 | ||
597 | fprintf(ofp, "%s", f->name); | |
598 | } | |
599 | fprintf(ofp, "):\n"); | |
600 | ||
601 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " | |
602 | "common_secs, common_nsecs,\n\t\t\t" | |
603 | "common_pid, common_comm)\n\n"); | |
604 | ||
605 | fprintf(ofp, "\t\tprint \""); | |
606 | ||
607 | not_first = 0; | |
608 | count = 0; | |
609 | ||
610 | for (f = event->format.fields; f; f = f->next) { | |
611 | if (not_first++) | |
612 | fprintf(ofp, ", "); | |
613 | if (count && count % 3 == 0) { | |
614 | fprintf(ofp, "\" \\\n\t\t\""); | |
615 | } | |
616 | count++; | |
617 | ||
618 | fprintf(ofp, "%s=", f->name); | |
619 | if (f->flags & FIELD_IS_STRING || | |
620 | f->flags & FIELD_IS_FLAG || | |
621 | f->flags & FIELD_IS_SYMBOLIC) | |
622 | fprintf(ofp, "%%s"); | |
623 | else if (f->flags & FIELD_IS_SIGNED) | |
624 | fprintf(ofp, "%%d"); | |
625 | else | |
626 | fprintf(ofp, "%%u"); | |
627 | } | |
628 | ||
629 | fprintf(ofp, "\\n\" %% \\\n\t\t("); | |
630 | ||
631 | not_first = 0; | |
632 | count = 0; | |
633 | ||
634 | for (f = event->format.fields; f; f = f->next) { | |
635 | if (not_first++) | |
636 | fprintf(ofp, ", "); | |
637 | ||
638 | if (++count % 5 == 0) | |
639 | fprintf(ofp, "\n\t\t"); | |
640 | ||
641 | if (f->flags & FIELD_IS_FLAG) { | |
642 | if ((count - 1) % 5 != 0) { | |
643 | fprintf(ofp, "\n\t\t"); | |
644 | count = 4; | |
645 | } | |
646 | fprintf(ofp, "flag_str(\""); | |
647 | fprintf(ofp, "%s__%s\", ", event->system, | |
648 | event->name); | |
649 | fprintf(ofp, "\"%s\", %s)", f->name, | |
650 | f->name); | |
651 | } else if (f->flags & FIELD_IS_SYMBOLIC) { | |
652 | if ((count - 1) % 5 != 0) { | |
653 | fprintf(ofp, "\n\t\t"); | |
654 | count = 4; | |
655 | } | |
656 | fprintf(ofp, "symbol_str(\""); | |
657 | fprintf(ofp, "%s__%s\", ", event->system, | |
658 | event->name); | |
659 | fprintf(ofp, "\"%s\", %s)", f->name, | |
660 | f->name); | |
661 | } else | |
662 | fprintf(ofp, "%s", f->name); | |
663 | } | |
664 | ||
665 | fprintf(ofp, "),\n\n"); | |
666 | } | |
667 | ||
668 | fprintf(ofp, "def trace_unhandled(event_name, context, " | |
c0251485 | 669 | "event_fields_dict):\n"); |
7e4b21b8 | 670 | |
c0251485 PT |
671 | fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" |
672 | "for k,v in sorted(event_fields_dict.items())])\n\n"); | |
7e4b21b8 TZ |
673 | |
674 | fprintf(ofp, "def print_header(" | |
675 | "event_name, cpu, secs, nsecs, pid, comm):\n" | |
676 | "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" | |
677 | "(event_name, cpu, secs, nsecs, pid, comm),\n"); | |
678 | ||
679 | fclose(ofp); | |
680 | ||
681 | fprintf(stderr, "generated Python script: %s\n", fname); | |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
686 | struct scripting_ops python_scripting_ops = { | |
687 | .name = "Python", | |
688 | .start_script = python_start_script, | |
689 | .stop_script = python_stop_script, | |
690 | .process_event = python_process_event, | |
691 | .generate_script = python_generate_script, | |
692 | }; |