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"
36 PyMODINIT_FUNC
initperf_trace_context(void);
38 #define FTRACE_MAX_EVENT \
39 ((1 << (sizeof(unsigned short) * 8)) - 1)
41 struct event_format
*events
[FTRACE_MAX_EVENT
];
44 #define N_COMMON_FIELDS 7
46 extern struct scripting_context
*scripting_context
;
48 static char *cur_field_name
;
49 static int zero_flag_atom
;
51 static PyObject
*main_module
, *main_dict
;
53 static void handler_call_die(const char *handler_name
)
56 Py_FatalError("problem in Python trace event handler");
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().
64 static void pydict_set_item_string_decref(PyObject
*dict
, const char *key
, PyObject
*val
)
66 PyDict_SetItemString(dict
, key
, val
);
70 static void define_value(enum print_arg_type field_type
,
72 const char *field_name
,
73 const char *field_value
,
74 const char *field_str
)
76 const char *handler_name
= "define_flag_value";
77 PyObject
*handler
, *t
, *retval
;
78 unsigned long long value
;
81 if (field_type
== PRINT_SYMBOL
)
82 handler_name
= "define_symbolic_value";
86 Py_FatalError("couldn't create Python tuple");
88 value
= eval_flag(field_value
);
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
));
95 handler
= PyDict_GetItemString(main_dict
, handler_name
);
96 if (handler
&& PyCallable_Check(handler
)) {
97 retval
= PyObject_CallObject(handler
, t
);
99 handler_call_die(handler_name
);
105 static void define_values(enum print_arg_type field_type
,
106 struct print_flag_sym
*field
,
108 const char *field_name
)
110 define_value(field_type
, ev_name
, field_name
, field
->value
,
114 define_values(field_type
, field
->next
, ev_name
, field_name
);
117 static void define_field(enum print_arg_type field_type
,
119 const char *field_name
,
122 const char *handler_name
= "define_flag_field";
123 PyObject
*handler
, *t
, *retval
;
126 if (field_type
== PRINT_SYMBOL
)
127 handler_name
= "define_symbolic_field";
129 if (field_type
== PRINT_FLAGS
)
134 Py_FatalError("couldn't create Python tuple");
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
));
141 handler
= PyDict_GetItemString(main_dict
, handler_name
);
142 if (handler
&& PyCallable_Check(handler
)) {
143 retval
= PyObject_CallObject(handler
, t
);
145 handler_call_die(handler_name
);
151 static void define_event_symbols(struct event_format
*event
,
153 struct print_arg
*args
)
155 switch (args
->type
) {
159 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
164 free(cur_field_name
);
165 cur_field_name
= strdup(args
->field
.name
);
168 define_event_symbols(event
, ev_name
, args
->flags
.field
);
169 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
171 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
175 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
176 define_field(PRINT_SYMBOL
, ev_name
, cur_field_name
, NULL
);
177 define_values(PRINT_SYMBOL
, args
->symbol
.symbols
, ev_name
,
181 define_event_symbols(event
, ev_name
, args
->hex
.field
);
182 define_event_symbols(event
, ev_name
, args
->hex
.size
);
187 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
190 if (strcmp(args
->op
.op
, ":") == 0)
192 define_event_symbols(event
, ev_name
, args
->op
.left
);
193 define_event_symbols(event
, ev_name
, args
->op
.right
);
196 /* gcc warns for these? */
198 case PRINT_DYNAMIC_ARRAY
:
200 /* we should warn... */
205 define_event_symbols(event
, ev_name
, args
->next
);
208 static inline struct event_format
*find_cache_event(struct perf_evsel
*evsel
)
210 static char ev_name
[256];
211 struct event_format
*event
;
212 int type
= evsel
->attr
.config
;
215 * XXX: Do we really need to cache this since now we have evsel->tp_format
216 * cached already? Need to re-read this "cache" routine that as well calls
217 * define_event_symbols() :-\
222 events
[type
] = event
= evsel
->tp_format
;
226 sprintf(ev_name
, "%s__%s", event
->system
, event
->name
);
228 define_event_symbols(event
, ev_name
, event
->print_fmt
.args
);
233 static void python_process_tracepoint(struct perf_sample
*sample
,
234 struct perf_evsel
*evsel
,
235 struct thread
*thread
,
236 struct addr_location
*al
)
238 PyObject
*handler
, *retval
, *context
, *t
, *obj
, *dict
= NULL
;
239 static char handler_name
[256];
240 struct format_field
*field
;
241 unsigned long long val
;
243 struct event_format
*event
;
246 int cpu
= sample
->cpu
;
247 void *data
= sample
->raw_data
;
248 unsigned long long nsecs
= sample
->time
;
249 const char *comm
= thread__comm_str(thread
);
251 t
= PyTuple_New(MAX_FIELDS
);
253 Py_FatalError("couldn't create Python tuple");
255 event
= find_cache_event(evsel
);
257 die("ug! no event found for type %d", (int)evsel
->attr
.config
);
259 pid
= raw_field_value(event
, "common_pid", data
);
261 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
263 handler
= PyDict_GetItemString(main_dict
, handler_name
);
264 if (handler
&& !PyCallable_Check(handler
))
269 Py_FatalError("couldn't create Python dict");
271 s
= nsecs
/ NSECS_PER_SEC
;
272 ns
= nsecs
- s
* NSECS_PER_SEC
;
274 scripting_context
->event_data
= data
;
275 scripting_context
->pevent
= evsel
->tp_format
->pevent
;
277 context
= PyCObject_FromVoidPtr(scripting_context
, NULL
);
279 PyTuple_SetItem(t
, n
++, PyString_FromString(handler_name
));
280 PyTuple_SetItem(t
, n
++, context
);
283 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
284 PyTuple_SetItem(t
, n
++, PyInt_FromLong(s
));
285 PyTuple_SetItem(t
, n
++, PyInt_FromLong(ns
));
286 PyTuple_SetItem(t
, n
++, PyInt_FromLong(pid
));
287 PyTuple_SetItem(t
, n
++, PyString_FromString(comm
));
289 pydict_set_item_string_decref(dict
, "common_cpu", PyInt_FromLong(cpu
));
290 pydict_set_item_string_decref(dict
, "common_s", PyInt_FromLong(s
));
291 pydict_set_item_string_decref(dict
, "common_ns", PyInt_FromLong(ns
));
292 pydict_set_item_string_decref(dict
, "common_pid", PyInt_FromLong(pid
));
293 pydict_set_item_string_decref(dict
, "common_comm", PyString_FromString(comm
));
295 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
296 if (field
->flags
& FIELD_IS_STRING
) {
298 if (field
->flags
& FIELD_IS_DYNAMIC
) {
299 offset
= *(int *)(data
+ field
->offset
);
302 offset
= field
->offset
;
303 obj
= PyString_FromString((char *)data
+ offset
);
304 } else { /* FIELD_IS_NUMERIC */
305 val
= read_size(event
, data
+ field
->offset
,
307 if (field
->flags
& FIELD_IS_SIGNED
) {
308 if ((long long)val
>= LONG_MIN
&&
309 (long long)val
<= LONG_MAX
)
310 obj
= PyInt_FromLong(val
);
312 obj
= PyLong_FromLongLong(val
);
315 obj
= PyInt_FromLong(val
);
317 obj
= PyLong_FromUnsignedLongLong(val
);
321 PyTuple_SetItem(t
, n
++, obj
);
323 pydict_set_item_string_decref(dict
, field
->name
, obj
);
327 PyTuple_SetItem(t
, n
++, dict
);
329 if (_PyTuple_Resize(&t
, n
) == -1)
330 Py_FatalError("error resizing Python tuple");
333 retval
= PyObject_CallObject(handler
, t
);
335 handler_call_die(handler_name
);
337 handler
= PyDict_GetItemString(main_dict
, "trace_unhandled");
338 if (handler
&& PyCallable_Check(handler
)) {
340 retval
= PyObject_CallObject(handler
, t
);
342 handler_call_die("trace_unhandled");
350 static void python_process_general_event(struct perf_sample
*sample
,
351 struct perf_evsel
*evsel
,
352 struct thread
*thread
,
353 struct addr_location
*al
)
355 PyObject
*handler
, *retval
, *t
, *dict
;
356 static char handler_name
[64];
360 * Use the MAX_FIELDS to make the function expandable, though
361 * currently there is only one item for the tuple.
363 t
= PyTuple_New(MAX_FIELDS
);
365 Py_FatalError("couldn't create Python tuple");
369 Py_FatalError("couldn't create Python dictionary");
371 snprintf(handler_name
, sizeof(handler_name
), "%s", "process_event");
373 handler
= PyDict_GetItemString(main_dict
, handler_name
);
374 if (!handler
|| !PyCallable_Check(handler
))
377 pydict_set_item_string_decref(dict
, "ev_name", PyString_FromString(perf_evsel__name(evsel
)));
378 pydict_set_item_string_decref(dict
, "attr", PyString_FromStringAndSize(
379 (const char *)&evsel
->attr
, sizeof(evsel
->attr
)));
380 pydict_set_item_string_decref(dict
, "sample", PyString_FromStringAndSize(
381 (const char *)sample
, sizeof(*sample
)));
382 pydict_set_item_string_decref(dict
, "raw_buf", PyString_FromStringAndSize(
383 (const char *)sample
->raw_data
, sample
->raw_size
));
384 pydict_set_item_string_decref(dict
, "comm",
385 PyString_FromString(thread__comm_str(thread
)));
387 pydict_set_item_string_decref(dict
, "dso",
388 PyString_FromString(al
->map
->dso
->name
));
391 pydict_set_item_string_decref(dict
, "symbol",
392 PyString_FromString(al
->sym
->name
));
395 PyTuple_SetItem(t
, n
++, dict
);
396 if (_PyTuple_Resize(&t
, n
) == -1)
397 Py_FatalError("error resizing Python tuple");
399 retval
= PyObject_CallObject(handler
, t
);
401 handler_call_die(handler_name
);
407 static void python_process_event(union perf_event
*event __maybe_unused
,
408 struct perf_sample
*sample
,
409 struct perf_evsel
*evsel
,
410 struct thread
*thread
,
411 struct addr_location
*al
)
413 switch (evsel
->attr
.type
) {
414 case PERF_TYPE_TRACEPOINT
:
415 python_process_tracepoint(sample
, evsel
, thread
, al
);
417 /* Reserve for future process_hw/sw/raw APIs */
419 python_process_general_event(sample
, evsel
, thread
, al
);
423 static int run_start_sub(void)
425 PyObject
*handler
, *retval
;
428 main_module
= PyImport_AddModule("__main__");
429 if (main_module
== NULL
)
431 Py_INCREF(main_module
);
433 main_dict
= PyModule_GetDict(main_module
);
434 if (main_dict
== NULL
) {
438 Py_INCREF(main_dict
);
440 handler
= PyDict_GetItemString(main_dict
, "trace_begin");
441 if (handler
== NULL
|| !PyCallable_Check(handler
))
444 retval
= PyObject_CallObject(handler
, NULL
);
446 handler_call_die("trace_begin");
451 Py_XDECREF(main_dict
);
452 Py_XDECREF(main_module
);
460 static int python_start_script(const char *script
, int argc
, const char **argv
)
462 const char **command_line
;
467 command_line
= malloc((argc
+ 1) * sizeof(const char *));
468 command_line
[0] = script
;
469 for (i
= 1; i
< argc
+ 1; i
++)
470 command_line
[i
] = argv
[i
- 1];
474 initperf_trace_context();
476 PySys_SetArgv(argc
+ 1, (char **)command_line
);
478 fp
= fopen(script
, "r");
480 sprintf(buf
, "Can't open python script \"%s\"", script
);
486 err
= PyRun_SimpleFile(fp
, script
);
488 fprintf(stderr
, "Error running python script %s\n", script
);
492 err
= run_start_sub();
494 fprintf(stderr
, "Error starting python script %s\n", script
);
511 static int python_stop_script(void)
513 PyObject
*handler
, *retval
;
516 handler
= PyDict_GetItemString(main_dict
, "trace_end");
517 if (handler
== NULL
|| !PyCallable_Check(handler
))
520 retval
= PyObject_CallObject(handler
, NULL
);
522 handler_call_die("trace_end");
526 Py_XDECREF(main_dict
);
527 Py_XDECREF(main_module
);
533 static int python_generate_script(struct pevent
*pevent
, const char *outfile
)
535 struct event_format
*event
= NULL
;
536 struct format_field
*f
;
537 char fname
[PATH_MAX
];
538 int not_first
, count
;
541 sprintf(fname
, "%s.py", outfile
);
542 ofp
= fopen(fname
, "w");
544 fprintf(stderr
, "couldn't open %s\n", fname
);
547 fprintf(ofp
, "# perf script event handlers, "
548 "generated by perf script -g python\n");
550 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
551 " License version 2\n\n");
553 fprintf(ofp
, "# The common_* event handler fields are the most useful "
554 "fields common to\n");
556 fprintf(ofp
, "# all events. They don't necessarily correspond to "
557 "the 'common_*' fields\n");
559 fprintf(ofp
, "# in the format files. Those fields not available as "
560 "handler params can\n");
562 fprintf(ofp
, "# be retrieved using Python functions of the form "
563 "common_*(context).\n");
565 fprintf(ofp
, "# See the perf-trace-python Documentation for the list "
566 "of available functions.\n\n");
568 fprintf(ofp
, "import os\n");
569 fprintf(ofp
, "import sys\n\n");
571 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
572 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
573 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
574 fprintf(ofp
, "from Core import *\n\n\n");
576 fprintf(ofp
, "def trace_begin():\n");
577 fprintf(ofp
, "\tprint \"in trace_begin\"\n\n");
579 fprintf(ofp
, "def trace_end():\n");
580 fprintf(ofp
, "\tprint \"in trace_end\"\n\n");
582 while ((event
= trace_find_next_event(pevent
, event
))) {
583 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
584 fprintf(ofp
, "event_name, ");
585 fprintf(ofp
, "context, ");
586 fprintf(ofp
, "common_cpu,\n");
587 fprintf(ofp
, "\tcommon_secs, ");
588 fprintf(ofp
, "common_nsecs, ");
589 fprintf(ofp
, "common_pid, ");
590 fprintf(ofp
, "common_comm,\n\t");
595 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
598 if (++count
% 5 == 0)
599 fprintf(ofp
, "\n\t");
601 fprintf(ofp
, "%s", f
->name
);
603 fprintf(ofp
, "):\n");
605 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
606 "common_secs, common_nsecs,\n\t\t\t"
607 "common_pid, common_comm)\n\n");
609 fprintf(ofp
, "\t\tprint \"");
614 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
617 if (count
&& count
% 3 == 0) {
618 fprintf(ofp
, "\" \\\n\t\t\"");
622 fprintf(ofp
, "%s=", f
->name
);
623 if (f
->flags
& FIELD_IS_STRING
||
624 f
->flags
& FIELD_IS_FLAG
||
625 f
->flags
& FIELD_IS_SYMBOLIC
)
627 else if (f
->flags
& FIELD_IS_SIGNED
)
633 fprintf(ofp
, "\\n\" %% \\\n\t\t(");
638 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
642 if (++count
% 5 == 0)
643 fprintf(ofp
, "\n\t\t");
645 if (f
->flags
& FIELD_IS_FLAG
) {
646 if ((count
- 1) % 5 != 0) {
647 fprintf(ofp
, "\n\t\t");
650 fprintf(ofp
, "flag_str(\"");
651 fprintf(ofp
, "%s__%s\", ", event
->system
,
653 fprintf(ofp
, "\"%s\", %s)", f
->name
,
655 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
656 if ((count
- 1) % 5 != 0) {
657 fprintf(ofp
, "\n\t\t");
660 fprintf(ofp
, "symbol_str(\"");
661 fprintf(ofp
, "%s__%s\", ", event
->system
,
663 fprintf(ofp
, "\"%s\", %s)", f
->name
,
666 fprintf(ofp
, "%s", f
->name
);
669 fprintf(ofp
, "),\n\n");
672 fprintf(ofp
, "def trace_unhandled(event_name, context, "
673 "event_fields_dict):\n");
675 fprintf(ofp
, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
676 "for k,v in sorted(event_fields_dict.items())])\n\n");
678 fprintf(ofp
, "def print_header("
679 "event_name, cpu, secs, nsecs, pid, comm):\n"
680 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
681 "(event_name, cpu, secs, nsecs, pid, comm),\n");
685 fprintf(stderr
, "generated Python script: %s\n", fname
);
690 struct scripting_ops python_scripting_ops
= {
692 .start_script
= python_start_script
,
693 .stop_script
= python_stop_script
,
694 .process_event
= python_process_event
,
695 .generate_script
= python_generate_script
,