2 * trace-event-python. Feed trace events to an embedded Python interpreter.
4 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
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.
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.
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
29 #include "../../perf.h"
33 #include "../thread.h"
34 #include "../trace-event.h"
37 PyMODINIT_FUNC
initperf_trace_context(void);
39 #define FTRACE_MAX_EVENT \
40 ((1 << (sizeof(unsigned short) * 8)) - 1)
42 struct event_format
*events
[FTRACE_MAX_EVENT
];
45 #define N_COMMON_FIELDS 7
47 extern struct scripting_context
*scripting_context
;
49 static char *cur_field_name
;
50 static int zero_flag_atom
;
52 static PyObject
*main_module
, *main_dict
;
54 static void handler_call_die(const char *handler_name
)
57 Py_FatalError("problem in Python trace event handler");
60 static void define_value(enum print_arg_type field_type
,
62 const char *field_name
,
63 const char *field_value
,
64 const char *field_str
)
66 const char *handler_name
= "define_flag_value";
67 PyObject
*handler
, *t
, *retval
;
68 unsigned long long value
;
71 if (field_type
== PRINT_SYMBOL
)
72 handler_name
= "define_symbolic_value";
76 Py_FatalError("couldn't create Python tuple");
78 value
= eval_flag(field_value
);
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
));
85 handler
= PyDict_GetItemString(main_dict
, handler_name
);
86 if (handler
&& PyCallable_Check(handler
)) {
87 retval
= PyObject_CallObject(handler
, t
);
89 handler_call_die(handler_name
);
95 static void define_values(enum print_arg_type field_type
,
96 struct print_flag_sym
*field
,
98 const char *field_name
)
100 define_value(field_type
, ev_name
, field_name
, field
->value
,
104 define_values(field_type
, field
->next
, ev_name
, field_name
);
107 static void define_field(enum print_arg_type field_type
,
109 const char *field_name
,
112 const char *handler_name
= "define_flag_field";
113 PyObject
*handler
, *t
, *retval
;
116 if (field_type
== PRINT_SYMBOL
)
117 handler_name
= "define_symbolic_field";
119 if (field_type
== PRINT_FLAGS
)
124 Py_FatalError("couldn't create Python tuple");
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
));
131 handler
= PyDict_GetItemString(main_dict
, handler_name
);
132 if (handler
&& PyCallable_Check(handler
)) {
133 retval
= PyObject_CallObject(handler
, t
);
135 handler_call_die(handler_name
);
141 static void define_event_symbols(struct event_format
*event
,
143 struct print_arg
*args
)
145 switch (args
->type
) {
149 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
155 free(cur_field_name
);
156 cur_field_name
= strdup(args
->field
.name
);
159 define_event_symbols(event
, ev_name
, args
->flags
.field
);
160 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
162 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
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
,
172 define_event_symbols(event
, ev_name
, args
->hex
.field
);
173 define_event_symbols(event
, ev_name
, args
->hex
.size
);
178 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
181 if (strcmp(args
->op
.op
, ":") == 0)
183 define_event_symbols(event
, ev_name
, args
->op
.left
);
184 define_event_symbols(event
, ev_name
, args
->op
.right
);
187 /* gcc warns for these? */
189 case PRINT_DYNAMIC_ARRAY
:
191 /* we should warn... */
196 define_event_symbols(event
, ev_name
, args
->next
);
199 static inline struct event_format
*find_cache_event(struct perf_evsel
*evsel
)
201 static char ev_name
[256];
202 struct event_format
*event
;
203 int type
= evsel
->attr
.config
;
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() :-\
213 events
[type
] = event
= evsel
->tp_format
;
217 sprintf(ev_name
, "%s__%s", event
->system
, event
->name
);
219 define_event_symbols(event
, ev_name
, event
->print_fmt
.args
);
224 static void python_process_tracepoint(union perf_event
*perf_event
226 struct perf_sample
*sample
,
227 struct perf_evsel
*evsel
,
228 struct machine
*machine __maybe_unused
,
229 struct addr_location
*al
)
231 PyObject
*handler
, *retval
, *context
, *t
, *obj
, *dict
= NULL
;
232 static char handler_name
[256];
233 struct format_field
*field
;
234 unsigned long long val
;
236 struct event_format
*event
;
239 int cpu
= sample
->cpu
;
240 void *data
= sample
->raw_data
;
241 unsigned long long nsecs
= sample
->time
;
242 struct thread
*thread
= al
->thread
;
243 char *comm
= thread
->comm
;
245 t
= PyTuple_New(MAX_FIELDS
);
247 Py_FatalError("couldn't create Python tuple");
249 event
= find_cache_event(evsel
);
251 die("ug! no event found for type %d", (int)evsel
->attr
.config
);
253 pid
= raw_field_value(event
, "common_pid", data
);
255 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
257 handler
= PyDict_GetItemString(main_dict
, handler_name
);
258 if (handler
&& !PyCallable_Check(handler
))
263 Py_FatalError("couldn't create Python dict");
265 s
= nsecs
/ NSECS_PER_SEC
;
266 ns
= nsecs
- s
* NSECS_PER_SEC
;
268 scripting_context
->event_data
= data
;
270 context
= PyCObject_FromVoidPtr(scripting_context
, NULL
);
272 PyTuple_SetItem(t
, n
++, PyString_FromString(handler_name
));
273 PyTuple_SetItem(t
, n
++, context
);
276 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
277 PyTuple_SetItem(t
, n
++, PyInt_FromLong(s
));
278 PyTuple_SetItem(t
, n
++, PyInt_FromLong(ns
));
279 PyTuple_SetItem(t
, n
++, PyInt_FromLong(pid
));
280 PyTuple_SetItem(t
, n
++, PyString_FromString(comm
));
282 PyDict_SetItemString(dict
, "common_cpu", PyInt_FromLong(cpu
));
283 PyDict_SetItemString(dict
, "common_s", PyInt_FromLong(s
));
284 PyDict_SetItemString(dict
, "common_ns", PyInt_FromLong(ns
));
285 PyDict_SetItemString(dict
, "common_pid", PyInt_FromLong(pid
));
286 PyDict_SetItemString(dict
, "common_comm", PyString_FromString(comm
));
288 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
289 if (field
->flags
& FIELD_IS_STRING
) {
291 if (field
->flags
& FIELD_IS_DYNAMIC
) {
292 offset
= *(int *)(data
+ field
->offset
);
295 offset
= field
->offset
;
296 obj
= PyString_FromString((char *)data
+ offset
);
297 } else { /* FIELD_IS_NUMERIC */
298 val
= read_size(event
, data
+ field
->offset
,
300 if (field
->flags
& FIELD_IS_SIGNED
) {
301 if ((long long)val
>= LONG_MIN
&&
302 (long long)val
<= LONG_MAX
)
303 obj
= PyInt_FromLong(val
);
305 obj
= PyLong_FromLongLong(val
);
308 obj
= PyInt_FromLong(val
);
310 obj
= PyLong_FromUnsignedLongLong(val
);
314 PyTuple_SetItem(t
, n
++, obj
);
316 PyDict_SetItemString(dict
, field
->name
, obj
);
320 PyTuple_SetItem(t
, n
++, dict
);
322 if (_PyTuple_Resize(&t
, n
) == -1)
323 Py_FatalError("error resizing Python tuple");
326 retval
= PyObject_CallObject(handler
, t
);
328 handler_call_die(handler_name
);
330 handler
= PyDict_GetItemString(main_dict
, "trace_unhandled");
331 if (handler
&& PyCallable_Check(handler
)) {
333 retval
= PyObject_CallObject(handler
, t
);
335 handler_call_die("trace_unhandled");
343 static void python_process_general_event(union perf_event
*perf_event
345 struct perf_sample
*sample
,
346 struct perf_evsel
*evsel
,
347 struct machine
*machine __maybe_unused
,
348 struct addr_location
*al
)
350 PyObject
*handler
, *retval
, *t
, *dict
;
351 static char handler_name
[64];
353 struct thread
*thread
= al
->thread
;
356 * Use the MAX_FIELDS to make the function expandable, though
357 * currently there is only one item for the tuple.
359 t
= PyTuple_New(MAX_FIELDS
);
361 Py_FatalError("couldn't create Python tuple");
365 Py_FatalError("couldn't create Python dictionary");
367 snprintf(handler_name
, sizeof(handler_name
), "%s", "process_event");
369 handler
= PyDict_GetItemString(main_dict
, handler_name
);
370 if (!handler
|| !PyCallable_Check(handler
))
373 PyDict_SetItemString(dict
, "ev_name", PyString_FromString(perf_evsel__name(evsel
)));
374 PyDict_SetItemString(dict
, "attr", PyString_FromStringAndSize(
375 (const char *)&evsel
->attr
, sizeof(evsel
->attr
)));
376 PyDict_SetItemString(dict
, "sample", PyString_FromStringAndSize(
377 (const char *)sample
, sizeof(*sample
)));
378 PyDict_SetItemString(dict
, "raw_buf", PyString_FromStringAndSize(
379 (const char *)sample
->raw_data
, sample
->raw_size
));
380 PyDict_SetItemString(dict
, "comm",
381 PyString_FromString(thread
->comm
));
383 PyDict_SetItemString(dict
, "dso",
384 PyString_FromString(al
->map
->dso
->name
));
387 PyDict_SetItemString(dict
, "symbol",
388 PyString_FromString(al
->sym
->name
));
391 PyTuple_SetItem(t
, n
++, dict
);
392 if (_PyTuple_Resize(&t
, n
) == -1)
393 Py_FatalError("error resizing Python tuple");
395 retval
= PyObject_CallObject(handler
, t
);
397 handler_call_die(handler_name
);
403 static void python_process_event(union perf_event
*perf_event
,
404 struct perf_sample
*sample
,
405 struct perf_evsel
*evsel
,
406 struct machine
*machine
,
407 struct addr_location
*al
)
409 switch (evsel
->attr
.type
) {
410 case PERF_TYPE_TRACEPOINT
:
411 python_process_tracepoint(perf_event
, sample
, evsel
,
414 /* Reserve for future process_hw/sw/raw APIs */
416 python_process_general_event(perf_event
, sample
, evsel
,
421 static int run_start_sub(void)
423 PyObject
*handler
, *retval
;
426 main_module
= PyImport_AddModule("__main__");
427 if (main_module
== NULL
)
429 Py_INCREF(main_module
);
431 main_dict
= PyModule_GetDict(main_module
);
432 if (main_dict
== NULL
) {
436 Py_INCREF(main_dict
);
438 handler
= PyDict_GetItemString(main_dict
, "trace_begin");
439 if (handler
== NULL
|| !PyCallable_Check(handler
))
442 retval
= PyObject_CallObject(handler
, NULL
);
444 handler_call_die("trace_begin");
449 Py_XDECREF(main_dict
);
450 Py_XDECREF(main_module
);
458 static int python_start_script(const char *script
, int argc
, const char **argv
)
460 const char **command_line
;
465 command_line
= malloc((argc
+ 1) * sizeof(const char *));
466 command_line
[0] = script
;
467 for (i
= 1; i
< argc
+ 1; i
++)
468 command_line
[i
] = argv
[i
- 1];
472 initperf_trace_context();
474 PySys_SetArgv(argc
+ 1, (char **)command_line
);
476 fp
= fopen(script
, "r");
478 sprintf(buf
, "Can't open python script \"%s\"", script
);
484 err
= PyRun_SimpleFile(fp
, script
);
486 fprintf(stderr
, "Error running python script %s\n", script
);
490 err
= run_start_sub();
492 fprintf(stderr
, "Error starting python script %s\n", script
);
509 static int python_stop_script(void)
511 PyObject
*handler
, *retval
;
514 handler
= PyDict_GetItemString(main_dict
, "trace_end");
515 if (handler
== NULL
|| !PyCallable_Check(handler
))
518 retval
= PyObject_CallObject(handler
, NULL
);
520 handler_call_die("trace_end");
524 Py_XDECREF(main_dict
);
525 Py_XDECREF(main_module
);
531 static int python_generate_script(struct pevent
*pevent
, const char *outfile
)
533 struct event_format
*event
= NULL
;
534 struct format_field
*f
;
535 char fname
[PATH_MAX
];
536 int not_first
, count
;
539 sprintf(fname
, "%s.py", outfile
);
540 ofp
= fopen(fname
, "w");
542 fprintf(stderr
, "couldn't open %s\n", fname
);
545 fprintf(ofp
, "# perf script event handlers, "
546 "generated by perf script -g python\n");
548 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
549 " License version 2\n\n");
551 fprintf(ofp
, "# The common_* event handler fields are the most useful "
552 "fields common to\n");
554 fprintf(ofp
, "# all events. They don't necessarily correspond to "
555 "the 'common_*' fields\n");
557 fprintf(ofp
, "# in the format files. Those fields not available as "
558 "handler params can\n");
560 fprintf(ofp
, "# be retrieved using Python functions of the form "
561 "common_*(context).\n");
563 fprintf(ofp
, "# See the perf-trace-python Documentation for the list "
564 "of available functions.\n\n");
566 fprintf(ofp
, "import os\n");
567 fprintf(ofp
, "import sys\n\n");
569 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
570 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
571 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
572 fprintf(ofp
, "from Core import *\n\n\n");
574 fprintf(ofp
, "def trace_begin():\n");
575 fprintf(ofp
, "\tprint \"in trace_begin\"\n\n");
577 fprintf(ofp
, "def trace_end():\n");
578 fprintf(ofp
, "\tprint \"in trace_end\"\n\n");
580 while ((event
= trace_find_next_event(pevent
, event
))) {
581 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
582 fprintf(ofp
, "event_name, ");
583 fprintf(ofp
, "context, ");
584 fprintf(ofp
, "common_cpu,\n");
585 fprintf(ofp
, "\tcommon_secs, ");
586 fprintf(ofp
, "common_nsecs, ");
587 fprintf(ofp
, "common_pid, ");
588 fprintf(ofp
, "common_comm,\n\t");
593 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
596 if (++count
% 5 == 0)
597 fprintf(ofp
, "\n\t");
599 fprintf(ofp
, "%s", f
->name
);
601 fprintf(ofp
, "):\n");
603 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
604 "common_secs, common_nsecs,\n\t\t\t"
605 "common_pid, common_comm)\n\n");
607 fprintf(ofp
, "\t\tprint \"");
612 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
615 if (count
&& count
% 3 == 0) {
616 fprintf(ofp
, "\" \\\n\t\t\"");
620 fprintf(ofp
, "%s=", f
->name
);
621 if (f
->flags
& FIELD_IS_STRING
||
622 f
->flags
& FIELD_IS_FLAG
||
623 f
->flags
& FIELD_IS_SYMBOLIC
)
625 else if (f
->flags
& FIELD_IS_SIGNED
)
631 fprintf(ofp
, "\\n\" %% \\\n\t\t(");
636 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
640 if (++count
% 5 == 0)
641 fprintf(ofp
, "\n\t\t");
643 if (f
->flags
& FIELD_IS_FLAG
) {
644 if ((count
- 1) % 5 != 0) {
645 fprintf(ofp
, "\n\t\t");
648 fprintf(ofp
, "flag_str(\"");
649 fprintf(ofp
, "%s__%s\", ", event
->system
,
651 fprintf(ofp
, "\"%s\", %s)", f
->name
,
653 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
654 if ((count
- 1) % 5 != 0) {
655 fprintf(ofp
, "\n\t\t");
658 fprintf(ofp
, "symbol_str(\"");
659 fprintf(ofp
, "%s__%s\", ", event
->system
,
661 fprintf(ofp
, "\"%s\", %s)", f
->name
,
664 fprintf(ofp
, "%s", f
->name
);
667 fprintf(ofp
, "),\n\n");
670 fprintf(ofp
, "def trace_unhandled(event_name, context, "
671 "event_fields_dict):\n");
673 fprintf(ofp
, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
674 "for k,v in sorted(event_fields_dict.items())])\n\n");
676 fprintf(ofp
, "def print_header("
677 "event_name, cpu, secs, nsecs, pid, comm):\n"
678 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
679 "(event_name, cpu, secs, nsecs, pid, comm),\n");
683 fprintf(stderr
, "generated Python script: %s\n", fname
);
688 struct scripting_ops python_scripting_ops
= {
690 .start_script
= python_start_script
,
691 .stop_script
= python_stop_script
,
692 .process_event
= python_process_event
,
693 .generate_script
= python_generate_script
,