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 TZ |
34 | #include "../trace-event.h" |
35 | ||
36 | PyMODINIT_FUNC initperf_trace_context(void); | |
37 | ||
38 | #define FTRACE_MAX_EVENT \ | |
39 | ((1 << (sizeof(unsigned short) * 8)) - 1) | |
40 | ||
aaf045f7 | 41 | struct event_format *events[FTRACE_MAX_EVENT]; |
7e4b21b8 TZ |
42 | |
43 | #define MAX_FIELDS 64 | |
44 | #define N_COMMON_FIELDS 7 | |
45 | ||
46 | extern struct scripting_context *scripting_context; | |
47 | ||
48 | static char *cur_field_name; | |
49 | static int zero_flag_atom; | |
50 | ||
51 | static PyObject *main_module, *main_dict; | |
52 | ||
53 | static void handler_call_die(const char *handler_name) | |
54 | { | |
55 | PyErr_Print(); | |
56 | Py_FatalError("problem in Python trace event handler"); | |
57 | } | |
58 | ||
c0268e8d JS |
59 | /* |
60 | * Insert val into into the dictionary and decrement the reference counter. | |
61 | * This is necessary for dictionaries since PyDict_SetItemString() does not | |
62 | * steal a reference, as opposed to PyTuple_SetItem(). | |
63 | */ | |
64 | static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val) | |
65 | { | |
66 | PyDict_SetItemString(dict, key, val); | |
67 | Py_DECREF(val); | |
68 | } | |
69 | ||
7e4b21b8 TZ |
70 | static void define_value(enum print_arg_type field_type, |
71 | const char *ev_name, | |
72 | const char *field_name, | |
73 | const char *field_value, | |
74 | const char *field_str) | |
75 | { | |
76 | const char *handler_name = "define_flag_value"; | |
77 | PyObject *handler, *t, *retval; | |
78 | unsigned long long value; | |
79 | unsigned n = 0; | |
80 | ||
81 | if (field_type == PRINT_SYMBOL) | |
82 | handler_name = "define_symbolic_value"; | |
83 | ||
44ad9cd8 | 84 | t = PyTuple_New(4); |
7e4b21b8 TZ |
85 | if (!t) |
86 | Py_FatalError("couldn't create Python tuple"); | |
87 | ||
88 | value = eval_flag(field_value); | |
89 | ||
90 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | |
91 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | |
92 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); | |
93 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); | |
94 | ||
7e4b21b8 TZ |
95 | handler = PyDict_GetItemString(main_dict, handler_name); |
96 | if (handler && PyCallable_Check(handler)) { | |
97 | retval = PyObject_CallObject(handler, t); | |
98 | if (retval == NULL) | |
99 | handler_call_die(handler_name); | |
100 | } | |
101 | ||
102 | Py_DECREF(t); | |
103 | } | |
104 | ||
105 | static void define_values(enum print_arg_type field_type, | |
106 | struct print_flag_sym *field, | |
107 | const char *ev_name, | |
108 | const char *field_name) | |
109 | { | |
110 | define_value(field_type, ev_name, field_name, field->value, | |
111 | field->str); | |
112 | ||
113 | if (field->next) | |
114 | define_values(field_type, field->next, ev_name, field_name); | |
115 | } | |
116 | ||
117 | static void define_field(enum print_arg_type field_type, | |
118 | const char *ev_name, | |
119 | const char *field_name, | |
120 | const char *delim) | |
121 | { | |
122 | const char *handler_name = "define_flag_field"; | |
123 | PyObject *handler, *t, *retval; | |
124 | unsigned n = 0; | |
125 | ||
126 | if (field_type == PRINT_SYMBOL) | |
127 | handler_name = "define_symbolic_field"; | |
128 | ||
44ad9cd8 TZ |
129 | if (field_type == PRINT_FLAGS) |
130 | t = PyTuple_New(3); | |
131 | else | |
132 | t = PyTuple_New(2); | |
7e4b21b8 TZ |
133 | if (!t) |
134 | Py_FatalError("couldn't create Python tuple"); | |
135 | ||
136 | PyTuple_SetItem(t, n++, PyString_FromString(ev_name)); | |
137 | PyTuple_SetItem(t, n++, PyString_FromString(field_name)); | |
138 | if (field_type == PRINT_FLAGS) | |
139 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); | |
140 | ||
7e4b21b8 TZ |
141 | handler = PyDict_GetItemString(main_dict, handler_name); |
142 | if (handler && PyCallable_Check(handler)) { | |
143 | retval = PyObject_CallObject(handler, t); | |
144 | if (retval == NULL) | |
145 | handler_call_die(handler_name); | |
146 | } | |
147 | ||
148 | Py_DECREF(t); | |
149 | } | |
150 | ||
aaf045f7 | 151 | static void define_event_symbols(struct event_format *event, |
7e4b21b8 TZ |
152 | const char *ev_name, |
153 | struct print_arg *args) | |
154 | { | |
155 | switch (args->type) { | |
156 | case PRINT_NULL: | |
157 | break; | |
158 | case PRINT_ATOM: | |
159 | define_value(PRINT_FLAGS, ev_name, cur_field_name, "0", | |
160 | args->atom.atom); | |
161 | zero_flag_atom = 0; | |
162 | break; | |
163 | case PRINT_FIELD: | |
164 | if (cur_field_name) | |
165 | free(cur_field_name); | |
166 | cur_field_name = strdup(args->field.name); | |
167 | break; | |
168 | case PRINT_FLAGS: | |
169 | define_event_symbols(event, ev_name, args->flags.field); | |
170 | define_field(PRINT_FLAGS, ev_name, cur_field_name, | |
171 | args->flags.delim); | |
172 | define_values(PRINT_FLAGS, args->flags.flags, ev_name, | |
173 | cur_field_name); | |
174 | break; | |
175 | case PRINT_SYMBOL: | |
176 | define_event_symbols(event, ev_name, args->symbol.field); | |
177 | define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL); | |
178 | define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name, | |
179 | cur_field_name); | |
180 | break; | |
e080e6f1 NK |
181 | case PRINT_HEX: |
182 | define_event_symbols(event, ev_name, args->hex.field); | |
183 | define_event_symbols(event, ev_name, args->hex.size); | |
184 | break; | |
7e4b21b8 TZ |
185 | case PRINT_STRING: |
186 | break; | |
187 | case PRINT_TYPE: | |
188 | define_event_symbols(event, ev_name, args->typecast.item); | |
189 | break; | |
190 | case PRINT_OP: | |
191 | if (strcmp(args->op.op, ":") == 0) | |
192 | zero_flag_atom = 1; | |
193 | define_event_symbols(event, ev_name, args->op.left); | |
194 | define_event_symbols(event, ev_name, args->op.right); | |
195 | break; | |
196 | default: | |
aaf045f7 SR |
197 | /* gcc warns for these? */ |
198 | case PRINT_BSTRING: | |
199 | case PRINT_DYNAMIC_ARRAY: | |
200 | case PRINT_FUNC: | |
7e4b21b8 TZ |
201 | /* we should warn... */ |
202 | return; | |
203 | } | |
204 | ||
205 | if (args->next) | |
206 | define_event_symbols(event, ev_name, args->next); | |
207 | } | |
208 | ||
fcf65bf1 | 209 | static inline struct event_format *find_cache_event(struct perf_evsel *evsel) |
7e4b21b8 TZ |
210 | { |
211 | static char ev_name[256]; | |
aaf045f7 | 212 | struct event_format *event; |
fcf65bf1 | 213 | int type = evsel->attr.config; |
7e4b21b8 | 214 | |
fcf65bf1 ACM |
215 | /* |
216 | * XXX: Do we really need to cache this since now we have evsel->tp_format | |
217 | * cached already? Need to re-read this "cache" routine that as well calls | |
218 | * define_event_symbols() :-\ | |
219 | */ | |
7e4b21b8 TZ |
220 | if (events[type]) |
221 | return events[type]; | |
222 | ||
fcf65bf1 | 223 | events[type] = event = evsel->tp_format; |
7e4b21b8 TZ |
224 | if (!event) |
225 | return NULL; | |
226 | ||
227 | sprintf(ev_name, "%s__%s", event->system, event->name); | |
228 | ||
229 | define_event_symbols(event, ev_name, event->print_fmt.args); | |
230 | ||
231 | return event; | |
232 | } | |
233 | ||
b7fff6b5 ACM |
234 | static void python_process_tracepoint(struct perf_sample *sample, |
235 | struct perf_evsel *evsel, | |
236 | struct thread *thread, | |
237 | struct addr_location *al) | |
7e4b21b8 | 238 | { |
c0251485 | 239 | PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; |
7e4b21b8 TZ |
240 | static char handler_name[256]; |
241 | struct format_field *field; | |
242 | unsigned long long val; | |
243 | unsigned long s, ns; | |
aaf045f7 | 244 | struct event_format *event; |
7e4b21b8 | 245 | unsigned n = 0; |
7e4b21b8 | 246 | int pid; |
be6d842a DA |
247 | int cpu = sample->cpu; |
248 | void *data = sample->raw_data; | |
249 | unsigned long long nsecs = sample->time; | |
b9c5143a | 250 | const char *comm = thread__comm_str(thread); |
7e4b21b8 TZ |
251 | |
252 | t = PyTuple_New(MAX_FIELDS); | |
253 | if (!t) | |
254 | Py_FatalError("couldn't create Python tuple"); | |
255 | ||
fcf65bf1 | 256 | event = find_cache_event(evsel); |
7e4b21b8 | 257 | if (!event) |
fcf65bf1 | 258 | die("ug! no event found for type %d", (int)evsel->attr.config); |
7e4b21b8 | 259 | |
97822433 | 260 | pid = raw_field_value(event, "common_pid", data); |
7e4b21b8 TZ |
261 | |
262 | sprintf(handler_name, "%s__%s", event->system, event->name); | |
263 | ||
c0251485 PT |
264 | handler = PyDict_GetItemString(main_dict, handler_name); |
265 | if (handler && !PyCallable_Check(handler)) | |
266 | handler = NULL; | |
267 | if (!handler) { | |
268 | dict = PyDict_New(); | |
269 | if (!dict) | |
270 | Py_FatalError("couldn't create Python dict"); | |
271 | } | |
7e4b21b8 TZ |
272 | s = nsecs / NSECS_PER_SEC; |
273 | ns = nsecs - s * NSECS_PER_SEC; | |
274 | ||
275 | scripting_context->event_data = data; | |
2de9533d | 276 | scripting_context->pevent = evsel->tp_format->pevent; |
7e4b21b8 TZ |
277 | |
278 | context = PyCObject_FromVoidPtr(scripting_context, NULL); | |
279 | ||
280 | PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); | |
fb7d0b3c | 281 | PyTuple_SetItem(t, n++, context); |
7e4b21b8 | 282 | |
c0251485 PT |
283 | if (handler) { |
284 | PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); | |
285 | PyTuple_SetItem(t, n++, PyInt_FromLong(s)); | |
286 | PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); | |
287 | PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); | |
288 | PyTuple_SetItem(t, n++, PyString_FromString(comm)); | |
289 | } else { | |
c0268e8d JS |
290 | pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu)); |
291 | pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s)); | |
292 | pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns)); | |
293 | pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid)); | |
294 | pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm)); | |
c0251485 | 295 | } |
7e4b21b8 TZ |
296 | for (field = event->format.fields; field; field = field->next) { |
297 | if (field->flags & FIELD_IS_STRING) { | |
298 | int offset; | |
299 | if (field->flags & FIELD_IS_DYNAMIC) { | |
300 | offset = *(int *)(data + field->offset); | |
301 | offset &= 0xffff; | |
302 | } else | |
303 | offset = field->offset; | |
b1dcc03c | 304 | obj = PyString_FromString((char *)data + offset); |
7e4b21b8 | 305 | } else { /* FIELD_IS_NUMERIC */ |
97822433 | 306 | val = read_size(event, data + field->offset, |
da378962 | 307 | field->size); |
7e4b21b8 | 308 | if (field->flags & FIELD_IS_SIGNED) { |
b1dcc03c TZ |
309 | if ((long long)val >= LONG_MIN && |
310 | (long long)val <= LONG_MAX) | |
311 | obj = PyInt_FromLong(val); | |
312 | else | |
313 | obj = PyLong_FromLongLong(val); | |
7e4b21b8 | 314 | } else { |
b1dcc03c TZ |
315 | if (val <= LONG_MAX) |
316 | obj = PyInt_FromLong(val); | |
317 | else | |
318 | obj = PyLong_FromUnsignedLongLong(val); | |
7e4b21b8 TZ |
319 | } |
320 | } | |
c0251485 PT |
321 | if (handler) |
322 | PyTuple_SetItem(t, n++, obj); | |
323 | else | |
c0268e8d | 324 | pydict_set_item_string_decref(dict, field->name, obj); |
c0251485 | 325 | |
7e4b21b8 | 326 | } |
c0251485 PT |
327 | if (!handler) |
328 | PyTuple_SetItem(t, n++, dict); | |
7e4b21b8 TZ |
329 | |
330 | if (_PyTuple_Resize(&t, n) == -1) | |
331 | Py_FatalError("error resizing Python tuple"); | |
332 | ||
c0251485 | 333 | if (handler) { |
7e4b21b8 TZ |
334 | retval = PyObject_CallObject(handler, t); |
335 | if (retval == NULL) | |
336 | handler_call_die(handler_name); | |
337 | } else { | |
338 | handler = PyDict_GetItemString(main_dict, "trace_unhandled"); | |
339 | if (handler && PyCallable_Check(handler)) { | |
7e4b21b8 TZ |
340 | |
341 | retval = PyObject_CallObject(handler, t); | |
342 | if (retval == NULL) | |
343 | handler_call_die("trace_unhandled"); | |
344 | } | |
c0251485 | 345 | Py_DECREF(dict); |
7e4b21b8 TZ |
346 | } |
347 | ||
348 | Py_DECREF(t); | |
349 | } | |
350 | ||
b7fff6b5 | 351 | static void python_process_general_event(struct perf_sample *sample, |
6a6daec2 | 352 | struct perf_evsel *evsel, |
2eaa1b40 | 353 | struct thread *thread, |
87b6a3ad | 354 | struct addr_location *al) |
6a6daec2 | 355 | { |
fd6b858a | 356 | PyObject *handler, *retval, *t, *dict; |
6a6daec2 FT |
357 | static char handler_name[64]; |
358 | unsigned n = 0; | |
6a6daec2 | 359 | |
fd6b858a FT |
360 | /* |
361 | * Use the MAX_FIELDS to make the function expandable, though | |
87b6a3ad | 362 | * currently there is only one item for the tuple. |
fd6b858a | 363 | */ |
6a6daec2 FT |
364 | t = PyTuple_New(MAX_FIELDS); |
365 | if (!t) | |
366 | Py_FatalError("couldn't create Python tuple"); | |
367 | ||
fd6b858a FT |
368 | dict = PyDict_New(); |
369 | if (!dict) | |
370 | Py_FatalError("couldn't create Python dictionary"); | |
371 | ||
6a6daec2 FT |
372 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
373 | ||
374 | handler = PyDict_GetItemString(main_dict, handler_name); | |
87b6a3ad | 375 | if (!handler || !PyCallable_Check(handler)) |
6a6daec2 | 376 | goto exit; |
6a6daec2 | 377 | |
c0268e8d JS |
378 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
379 | pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( | |
fd6b858a | 380 | (const char *)&evsel->attr, sizeof(evsel->attr))); |
c0268e8d | 381 | pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize( |
fd6b858a | 382 | (const char *)sample, sizeof(*sample))); |
c0268e8d | 383 | pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( |
fd6b858a | 384 | (const char *)sample->raw_data, sample->raw_size)); |
c0268e8d | 385 | pydict_set_item_string_decref(dict, "comm", |
b9c5143a | 386 | PyString_FromString(thread__comm_str(thread))); |
fd6b858a | 387 | if (al->map) { |
c0268e8d | 388 | pydict_set_item_string_decref(dict, "dso", |
fd6b858a FT |
389 | PyString_FromString(al->map->dso->name)); |
390 | } | |
391 | if (al->sym) { | |
c0268e8d | 392 | pydict_set_item_string_decref(dict, "symbol", |
fd6b858a FT |
393 | PyString_FromString(al->sym->name)); |
394 | } | |
6a6daec2 | 395 | |
fd6b858a | 396 | PyTuple_SetItem(t, n++, dict); |
6a6daec2 FT |
397 | if (_PyTuple_Resize(&t, n) == -1) |
398 | Py_FatalError("error resizing Python tuple"); | |
399 | ||
400 | retval = PyObject_CallObject(handler, t); | |
401 | if (retval == NULL) | |
402 | handler_call_die(handler_name); | |
403 | exit: | |
fd6b858a | 404 | Py_DECREF(dict); |
6a6daec2 FT |
405 | Py_DECREF(t); |
406 | } | |
407 | ||
b7fff6b5 | 408 | static void python_process_event(union perf_event *event __maybe_unused, |
6a6daec2 FT |
409 | struct perf_sample *sample, |
410 | struct perf_evsel *evsel, | |
2eaa1b40 | 411 | struct thread *thread, |
73994dc1 | 412 | struct addr_location *al) |
6a6daec2 FT |
413 | { |
414 | switch (evsel->attr.type) { | |
415 | case PERF_TYPE_TRACEPOINT: | |
b7fff6b5 | 416 | python_process_tracepoint(sample, evsel, thread, al); |
6a6daec2 FT |
417 | break; |
418 | /* Reserve for future process_hw/sw/raw APIs */ | |
419 | default: | |
b7fff6b5 | 420 | python_process_general_event(sample, evsel, thread, al); |
6a6daec2 FT |
421 | } |
422 | } | |
423 | ||
7e4b21b8 TZ |
424 | static int run_start_sub(void) |
425 | { | |
426 | PyObject *handler, *retval; | |
427 | int err = 0; | |
428 | ||
429 | main_module = PyImport_AddModule("__main__"); | |
430 | if (main_module == NULL) | |
431 | return -1; | |
432 | Py_INCREF(main_module); | |
433 | ||
434 | main_dict = PyModule_GetDict(main_module); | |
435 | if (main_dict == NULL) { | |
436 | err = -1; | |
437 | goto error; | |
438 | } | |
439 | Py_INCREF(main_dict); | |
440 | ||
441 | handler = PyDict_GetItemString(main_dict, "trace_begin"); | |
442 | if (handler == NULL || !PyCallable_Check(handler)) | |
443 | goto out; | |
444 | ||
445 | retval = PyObject_CallObject(handler, NULL); | |
446 | if (retval == NULL) | |
447 | handler_call_die("trace_begin"); | |
448 | ||
449 | Py_DECREF(retval); | |
450 | return err; | |
451 | error: | |
452 | Py_XDECREF(main_dict); | |
453 | Py_XDECREF(main_module); | |
454 | out: | |
455 | return err; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Start trace script | |
460 | */ | |
461 | static int python_start_script(const char *script, int argc, const char **argv) | |
462 | { | |
463 | const char **command_line; | |
464 | char buf[PATH_MAX]; | |
465 | int i, err = 0; | |
466 | FILE *fp; | |
467 | ||
468 | command_line = malloc((argc + 1) * sizeof(const char *)); | |
469 | command_line[0] = script; | |
470 | for (i = 1; i < argc + 1; i++) | |
471 | command_line[i] = argv[i - 1]; | |
472 | ||
473 | Py_Initialize(); | |
474 | ||
475 | initperf_trace_context(); | |
476 | ||
477 | PySys_SetArgv(argc + 1, (char **)command_line); | |
478 | ||
479 | fp = fopen(script, "r"); | |
480 | if (!fp) { | |
481 | sprintf(buf, "Can't open python script \"%s\"", script); | |
482 | perror(buf); | |
483 | err = -1; | |
484 | goto error; | |
485 | } | |
486 | ||
487 | err = PyRun_SimpleFile(fp, script); | |
488 | if (err) { | |
489 | fprintf(stderr, "Error running python script %s\n", script); | |
490 | goto error; | |
491 | } | |
492 | ||
493 | err = run_start_sub(); | |
494 | if (err) { | |
495 | fprintf(stderr, "Error starting python script %s\n", script); | |
496 | goto error; | |
497 | } | |
498 | ||
499 | free(command_line); | |
7e4b21b8 TZ |
500 | |
501 | return err; | |
502 | error: | |
503 | Py_Finalize(); | |
504 | free(command_line); | |
505 | ||
506 | return err; | |
507 | } | |
508 | ||
509 | /* | |
510 | * Stop trace script | |
511 | */ | |
512 | static int python_stop_script(void) | |
513 | { | |
514 | PyObject *handler, *retval; | |
515 | int err = 0; | |
516 | ||
517 | handler = PyDict_GetItemString(main_dict, "trace_end"); | |
518 | if (handler == NULL || !PyCallable_Check(handler)) | |
519 | goto out; | |
520 | ||
521 | retval = PyObject_CallObject(handler, NULL); | |
522 | if (retval == NULL) | |
523 | handler_call_die("trace_end"); | |
524 | else | |
525 | Py_DECREF(retval); | |
526 | out: | |
527 | Py_XDECREF(main_dict); | |
528 | Py_XDECREF(main_module); | |
529 | Py_Finalize(); | |
530 | ||
7e4b21b8 TZ |
531 | return err; |
532 | } | |
533 | ||
da378962 | 534 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
7e4b21b8 | 535 | { |
aaf045f7 | 536 | struct event_format *event = NULL; |
7e4b21b8 TZ |
537 | struct format_field *f; |
538 | char fname[PATH_MAX]; | |
539 | int not_first, count; | |
540 | FILE *ofp; | |
541 | ||
542 | sprintf(fname, "%s.py", outfile); | |
543 | ofp = fopen(fname, "w"); | |
544 | if (ofp == NULL) { | |
545 | fprintf(stderr, "couldn't open %s\n", fname); | |
546 | return -1; | |
547 | } | |
133dc4c3 IM |
548 | fprintf(ofp, "# perf script event handlers, " |
549 | "generated by perf script -g python\n"); | |
7e4b21b8 TZ |
550 | |
551 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" | |
552 | " License version 2\n\n"); | |
553 | ||
554 | fprintf(ofp, "# The common_* event handler fields are the most useful " | |
555 | "fields common to\n"); | |
556 | ||
557 | fprintf(ofp, "# all events. They don't necessarily correspond to " | |
558 | "the 'common_*' fields\n"); | |
559 | ||
560 | fprintf(ofp, "# in the format files. Those fields not available as " | |
561 | "handler params can\n"); | |
562 | ||
563 | fprintf(ofp, "# be retrieved using Python functions of the form " | |
564 | "common_*(context).\n"); | |
565 | ||
566 | fprintf(ofp, "# See the perf-trace-python Documentation for the list " | |
567 | "of available functions.\n\n"); | |
568 | ||
569 | fprintf(ofp, "import os\n"); | |
570 | fprintf(ofp, "import sys\n\n"); | |
571 | ||
572 | fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n"); | |
573 | fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n"); | |
574 | fprintf(ofp, "\nfrom perf_trace_context import *\n"); | |
575 | fprintf(ofp, "from Core import *\n\n\n"); | |
576 | ||
577 | fprintf(ofp, "def trace_begin():\n"); | |
578 | fprintf(ofp, "\tprint \"in trace_begin\"\n\n"); | |
579 | ||
580 | fprintf(ofp, "def trace_end():\n"); | |
581 | fprintf(ofp, "\tprint \"in trace_end\"\n\n"); | |
582 | ||
da378962 | 583 | while ((event = trace_find_next_event(pevent, event))) { |
7e4b21b8 TZ |
584 | fprintf(ofp, "def %s__%s(", event->system, event->name); |
585 | fprintf(ofp, "event_name, "); | |
586 | fprintf(ofp, "context, "); | |
587 | fprintf(ofp, "common_cpu,\n"); | |
588 | fprintf(ofp, "\tcommon_secs, "); | |
589 | fprintf(ofp, "common_nsecs, "); | |
590 | fprintf(ofp, "common_pid, "); | |
591 | fprintf(ofp, "common_comm,\n\t"); | |
592 | ||
593 | not_first = 0; | |
594 | count = 0; | |
595 | ||
596 | for (f = event->format.fields; f; f = f->next) { | |
597 | if (not_first++) | |
598 | fprintf(ofp, ", "); | |
599 | if (++count % 5 == 0) | |
600 | fprintf(ofp, "\n\t"); | |
601 | ||
602 | fprintf(ofp, "%s", f->name); | |
603 | } | |
604 | fprintf(ofp, "):\n"); | |
605 | ||
606 | fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " | |
607 | "common_secs, common_nsecs,\n\t\t\t" | |
608 | "common_pid, common_comm)\n\n"); | |
609 | ||
610 | fprintf(ofp, "\t\tprint \""); | |
611 | ||
612 | not_first = 0; | |
613 | count = 0; | |
614 | ||
615 | for (f = event->format.fields; f; f = f->next) { | |
616 | if (not_first++) | |
617 | fprintf(ofp, ", "); | |
618 | if (count && count % 3 == 0) { | |
619 | fprintf(ofp, "\" \\\n\t\t\""); | |
620 | } | |
621 | count++; | |
622 | ||
623 | fprintf(ofp, "%s=", f->name); | |
624 | if (f->flags & FIELD_IS_STRING || | |
625 | f->flags & FIELD_IS_FLAG || | |
626 | f->flags & FIELD_IS_SYMBOLIC) | |
627 | fprintf(ofp, "%%s"); | |
628 | else if (f->flags & FIELD_IS_SIGNED) | |
629 | fprintf(ofp, "%%d"); | |
630 | else | |
631 | fprintf(ofp, "%%u"); | |
632 | } | |
633 | ||
634 | fprintf(ofp, "\\n\" %% \\\n\t\t("); | |
635 | ||
636 | not_first = 0; | |
637 | count = 0; | |
638 | ||
639 | for (f = event->format.fields; f; f = f->next) { | |
640 | if (not_first++) | |
641 | fprintf(ofp, ", "); | |
642 | ||
643 | if (++count % 5 == 0) | |
644 | fprintf(ofp, "\n\t\t"); | |
645 | ||
646 | if (f->flags & FIELD_IS_FLAG) { | |
647 | if ((count - 1) % 5 != 0) { | |
648 | fprintf(ofp, "\n\t\t"); | |
649 | count = 4; | |
650 | } | |
651 | fprintf(ofp, "flag_str(\""); | |
652 | fprintf(ofp, "%s__%s\", ", event->system, | |
653 | event->name); | |
654 | fprintf(ofp, "\"%s\", %s)", f->name, | |
655 | f->name); | |
656 | } else if (f->flags & FIELD_IS_SYMBOLIC) { | |
657 | if ((count - 1) % 5 != 0) { | |
658 | fprintf(ofp, "\n\t\t"); | |
659 | count = 4; | |
660 | } | |
661 | fprintf(ofp, "symbol_str(\""); | |
662 | fprintf(ofp, "%s__%s\", ", event->system, | |
663 | event->name); | |
664 | fprintf(ofp, "\"%s\", %s)", f->name, | |
665 | f->name); | |
666 | } else | |
667 | fprintf(ofp, "%s", f->name); | |
668 | } | |
669 | ||
670 | fprintf(ofp, "),\n\n"); | |
671 | } | |
672 | ||
673 | fprintf(ofp, "def trace_unhandled(event_name, context, " | |
c0251485 | 674 | "event_fields_dict):\n"); |
7e4b21b8 | 675 | |
c0251485 PT |
676 | fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))" |
677 | "for k,v in sorted(event_fields_dict.items())])\n\n"); | |
7e4b21b8 TZ |
678 | |
679 | fprintf(ofp, "def print_header(" | |
680 | "event_name, cpu, secs, nsecs, pid, comm):\n" | |
681 | "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t" | |
682 | "(event_name, cpu, secs, nsecs, pid, comm),\n"); | |
683 | ||
684 | fclose(ofp); | |
685 | ||
686 | fprintf(stderr, "generated Python script: %s\n", fname); | |
687 | ||
688 | return 0; | |
689 | } | |
690 | ||
691 | struct scripting_ops python_scripting_ops = { | |
692 | .name = "Python", | |
693 | .start_script = python_start_script, | |
694 | .stop_script = python_stop_script, | |
695 | .process_event = python_process_event, | |
696 | .generate_script = python_generate_script, | |
697 | }; |