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