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"
32 #include "../thread.h"
33 #include "../trace-event.h"
35 PyMODINIT_FUNC
initperf_trace_context(void);
37 #define FTRACE_MAX_EVENT \
38 ((1 << (sizeof(unsigned short) * 8)) - 1)
40 struct event_format
*events
[FTRACE_MAX_EVENT
];
43 #define N_COMMON_FIELDS 7
45 extern struct scripting_context
*scripting_context
;
47 static char *cur_field_name
;
48 static int zero_flag_atom
;
50 static PyObject
*main_module
, *main_dict
;
52 static void handler_call_die(const char *handler_name
)
55 Py_FatalError("problem in Python trace event handler");
58 static void define_value(enum print_arg_type field_type
,
60 const char *field_name
,
61 const char *field_value
,
62 const char *field_str
)
64 const char *handler_name
= "define_flag_value";
65 PyObject
*handler
, *t
, *retval
;
66 unsigned long long value
;
69 if (field_type
== PRINT_SYMBOL
)
70 handler_name
= "define_symbolic_value";
74 Py_FatalError("couldn't create Python tuple");
76 value
= eval_flag(field_value
);
78 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
79 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
80 PyTuple_SetItem(t
, n
++, PyInt_FromLong(value
));
81 PyTuple_SetItem(t
, n
++, PyString_FromString(field_str
));
83 handler
= PyDict_GetItemString(main_dict
, handler_name
);
84 if (handler
&& PyCallable_Check(handler
)) {
85 retval
= PyObject_CallObject(handler
, t
);
87 handler_call_die(handler_name
);
93 static void define_values(enum print_arg_type field_type
,
94 struct print_flag_sym
*field
,
96 const char *field_name
)
98 define_value(field_type
, ev_name
, field_name
, field
->value
,
102 define_values(field_type
, field
->next
, ev_name
, field_name
);
105 static void define_field(enum print_arg_type field_type
,
107 const char *field_name
,
110 const char *handler_name
= "define_flag_field";
111 PyObject
*handler
, *t
, *retval
;
114 if (field_type
== PRINT_SYMBOL
)
115 handler_name
= "define_symbolic_field";
117 if (field_type
== PRINT_FLAGS
)
122 Py_FatalError("couldn't create Python tuple");
124 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
125 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
126 if (field_type
== PRINT_FLAGS
)
127 PyTuple_SetItem(t
, n
++, PyString_FromString(delim
));
129 handler
= PyDict_GetItemString(main_dict
, handler_name
);
130 if (handler
&& PyCallable_Check(handler
)) {
131 retval
= PyObject_CallObject(handler
, t
);
133 handler_call_die(handler_name
);
139 static void define_event_symbols(struct event_format
*event
,
141 struct print_arg
*args
)
143 switch (args
->type
) {
147 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
153 free(cur_field_name
);
154 cur_field_name
= strdup(args
->field
.name
);
157 define_event_symbols(event
, ev_name
, args
->flags
.field
);
158 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
160 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
164 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
165 define_field(PRINT_SYMBOL
, ev_name
, cur_field_name
, NULL
);
166 define_values(PRINT_SYMBOL
, args
->symbol
.symbols
, ev_name
,
172 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
175 if (strcmp(args
->op
.op
, ":") == 0)
177 define_event_symbols(event
, ev_name
, args
->op
.left
);
178 define_event_symbols(event
, ev_name
, args
->op
.right
);
181 /* gcc warns for these? */
183 case PRINT_DYNAMIC_ARRAY
:
185 /* we should warn... */
190 define_event_symbols(event
, ev_name
, args
->next
);
193 static inline struct event_format
*find_cache_event(int type
)
195 static char ev_name
[256];
196 struct event_format
*event
;
201 events
[type
] = event
= trace_find_event(type
);
205 sprintf(ev_name
, "%s__%s", event
->system
, event
->name
);
207 define_event_symbols(event
, ev_name
, event
->print_fmt
.args
);
212 static void python_process_event(union perf_event
*pevent __unused
,
213 struct perf_sample
*sample
,
214 struct perf_evsel
*evsel __unused
,
215 struct machine
*machine __unused
,
216 struct thread
*thread
)
218 PyObject
*handler
, *retval
, *context
, *t
, *obj
, *dict
= NULL
;
219 static char handler_name
[256];
220 struct format_field
*field
;
221 unsigned long long val
;
223 struct event_format
*event
;
227 int cpu
= sample
->cpu
;
228 void *data
= sample
->raw_data
;
229 unsigned long long nsecs
= sample
->time
;
230 char *comm
= thread
->comm
;
232 t
= PyTuple_New(MAX_FIELDS
);
234 Py_FatalError("couldn't create Python tuple");
236 type
= trace_parse_common_type(data
);
238 event
= find_cache_event(type
);
240 die("ug! no event found for type %d", type
);
242 pid
= trace_parse_common_pid(data
);
244 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
246 handler
= PyDict_GetItemString(main_dict
, handler_name
);
247 if (handler
&& !PyCallable_Check(handler
))
252 Py_FatalError("couldn't create Python dict");
254 s
= nsecs
/ NSECS_PER_SEC
;
255 ns
= nsecs
- s
* NSECS_PER_SEC
;
257 scripting_context
->event_data
= data
;
259 context
= PyCObject_FromVoidPtr(scripting_context
, NULL
);
261 PyTuple_SetItem(t
, n
++, PyString_FromString(handler_name
));
262 PyTuple_SetItem(t
, n
++, context
);
265 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
266 PyTuple_SetItem(t
, n
++, PyInt_FromLong(s
));
267 PyTuple_SetItem(t
, n
++, PyInt_FromLong(ns
));
268 PyTuple_SetItem(t
, n
++, PyInt_FromLong(pid
));
269 PyTuple_SetItem(t
, n
++, PyString_FromString(comm
));
271 PyDict_SetItemString(dict
, "common_cpu", PyInt_FromLong(cpu
));
272 PyDict_SetItemString(dict
, "common_s", PyInt_FromLong(s
));
273 PyDict_SetItemString(dict
, "common_ns", PyInt_FromLong(ns
));
274 PyDict_SetItemString(dict
, "common_pid", PyInt_FromLong(pid
));
275 PyDict_SetItemString(dict
, "common_comm", PyString_FromString(comm
));
277 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
278 if (field
->flags
& FIELD_IS_STRING
) {
280 if (field
->flags
& FIELD_IS_DYNAMIC
) {
281 offset
= *(int *)(data
+ field
->offset
);
284 offset
= field
->offset
;
285 obj
= PyString_FromString((char *)data
+ offset
);
286 } else { /* FIELD_IS_NUMERIC */
287 val
= read_size(data
+ field
->offset
, field
->size
);
288 if (field
->flags
& FIELD_IS_SIGNED
) {
289 if ((long long)val
>= LONG_MIN
&&
290 (long long)val
<= LONG_MAX
)
291 obj
= PyInt_FromLong(val
);
293 obj
= PyLong_FromLongLong(val
);
296 obj
= PyInt_FromLong(val
);
298 obj
= PyLong_FromUnsignedLongLong(val
);
302 PyTuple_SetItem(t
, n
++, obj
);
304 PyDict_SetItemString(dict
, field
->name
, obj
);
308 PyTuple_SetItem(t
, n
++, dict
);
310 if (_PyTuple_Resize(&t
, n
) == -1)
311 Py_FatalError("error resizing Python tuple");
314 retval
= PyObject_CallObject(handler
, t
);
316 handler_call_die(handler_name
);
318 handler
= PyDict_GetItemString(main_dict
, "trace_unhandled");
319 if (handler
&& PyCallable_Check(handler
)) {
321 retval
= PyObject_CallObject(handler
, t
);
323 handler_call_die("trace_unhandled");
331 static int run_start_sub(void)
333 PyObject
*handler
, *retval
;
336 main_module
= PyImport_AddModule("__main__");
337 if (main_module
== NULL
)
339 Py_INCREF(main_module
);
341 main_dict
= PyModule_GetDict(main_module
);
342 if (main_dict
== NULL
) {
346 Py_INCREF(main_dict
);
348 handler
= PyDict_GetItemString(main_dict
, "trace_begin");
349 if (handler
== NULL
|| !PyCallable_Check(handler
))
352 retval
= PyObject_CallObject(handler
, NULL
);
354 handler_call_die("trace_begin");
359 Py_XDECREF(main_dict
);
360 Py_XDECREF(main_module
);
368 static int python_start_script(const char *script
, int argc
, const char **argv
)
370 const char **command_line
;
375 command_line
= malloc((argc
+ 1) * sizeof(const char *));
376 command_line
[0] = script
;
377 for (i
= 1; i
< argc
+ 1; i
++)
378 command_line
[i
] = argv
[i
- 1];
382 initperf_trace_context();
384 PySys_SetArgv(argc
+ 1, (char **)command_line
);
386 fp
= fopen(script
, "r");
388 sprintf(buf
, "Can't open python script \"%s\"", script
);
394 err
= PyRun_SimpleFile(fp
, script
);
396 fprintf(stderr
, "Error running python script %s\n", script
);
400 err
= run_start_sub();
402 fprintf(stderr
, "Error starting python script %s\n", script
);
419 static int python_stop_script(void)
421 PyObject
*handler
, *retval
;
424 handler
= PyDict_GetItemString(main_dict
, "trace_end");
425 if (handler
== NULL
|| !PyCallable_Check(handler
))
428 retval
= PyObject_CallObject(handler
, NULL
);
430 handler_call_die("trace_end");
434 Py_XDECREF(main_dict
);
435 Py_XDECREF(main_module
);
441 static int python_generate_script(const char *outfile
)
443 struct event_format
*event
= NULL
;
444 struct format_field
*f
;
445 char fname
[PATH_MAX
];
446 int not_first
, count
;
449 sprintf(fname
, "%s.py", outfile
);
450 ofp
= fopen(fname
, "w");
452 fprintf(stderr
, "couldn't open %s\n", fname
);
455 fprintf(ofp
, "# perf script event handlers, "
456 "generated by perf script -g python\n");
458 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
459 " License version 2\n\n");
461 fprintf(ofp
, "# The common_* event handler fields are the most useful "
462 "fields common to\n");
464 fprintf(ofp
, "# all events. They don't necessarily correspond to "
465 "the 'common_*' fields\n");
467 fprintf(ofp
, "# in the format files. Those fields not available as "
468 "handler params can\n");
470 fprintf(ofp
, "# be retrieved using Python functions of the form "
471 "common_*(context).\n");
473 fprintf(ofp
, "# See the perf-trace-python Documentation for the list "
474 "of available functions.\n\n");
476 fprintf(ofp
, "import os\n");
477 fprintf(ofp
, "import sys\n\n");
479 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
480 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
481 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
482 fprintf(ofp
, "from Core import *\n\n\n");
484 fprintf(ofp
, "def trace_begin():\n");
485 fprintf(ofp
, "\tprint \"in trace_begin\"\n\n");
487 fprintf(ofp
, "def trace_end():\n");
488 fprintf(ofp
, "\tprint \"in trace_end\"\n\n");
490 while ((event
= trace_find_next_event(event
))) {
491 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
492 fprintf(ofp
, "event_name, ");
493 fprintf(ofp
, "context, ");
494 fprintf(ofp
, "common_cpu,\n");
495 fprintf(ofp
, "\tcommon_secs, ");
496 fprintf(ofp
, "common_nsecs, ");
497 fprintf(ofp
, "common_pid, ");
498 fprintf(ofp
, "common_comm,\n\t");
503 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
506 if (++count
% 5 == 0)
507 fprintf(ofp
, "\n\t");
509 fprintf(ofp
, "%s", f
->name
);
511 fprintf(ofp
, "):\n");
513 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
514 "common_secs, common_nsecs,\n\t\t\t"
515 "common_pid, common_comm)\n\n");
517 fprintf(ofp
, "\t\tprint \"");
522 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
525 if (count
&& count
% 3 == 0) {
526 fprintf(ofp
, "\" \\\n\t\t\"");
530 fprintf(ofp
, "%s=", f
->name
);
531 if (f
->flags
& FIELD_IS_STRING
||
532 f
->flags
& FIELD_IS_FLAG
||
533 f
->flags
& FIELD_IS_SYMBOLIC
)
535 else if (f
->flags
& FIELD_IS_SIGNED
)
541 fprintf(ofp
, "\\n\" %% \\\n\t\t(");
546 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
550 if (++count
% 5 == 0)
551 fprintf(ofp
, "\n\t\t");
553 if (f
->flags
& FIELD_IS_FLAG
) {
554 if ((count
- 1) % 5 != 0) {
555 fprintf(ofp
, "\n\t\t");
558 fprintf(ofp
, "flag_str(\"");
559 fprintf(ofp
, "%s__%s\", ", event
->system
,
561 fprintf(ofp
, "\"%s\", %s)", f
->name
,
563 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
564 if ((count
- 1) % 5 != 0) {
565 fprintf(ofp
, "\n\t\t");
568 fprintf(ofp
, "symbol_str(\"");
569 fprintf(ofp
, "%s__%s\", ", event
->system
,
571 fprintf(ofp
, "\"%s\", %s)", f
->name
,
574 fprintf(ofp
, "%s", f
->name
);
577 fprintf(ofp
, "),\n\n");
580 fprintf(ofp
, "def trace_unhandled(event_name, context, "
581 "event_fields_dict):\n");
583 fprintf(ofp
, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
584 "for k,v in sorted(event_fields_dict.items())])\n\n");
586 fprintf(ofp
, "def print_header("
587 "event_name, cpu, secs, nsecs, pid, comm):\n"
588 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
589 "(event_name, cpu, secs, nsecs, pid, comm),\n");
593 fprintf(stderr
, "generated Python script: %s\n", fname
);
598 struct scripting_ops python_scripting_ops
= {
600 .start_script
= python_start_script
,
601 .stop_script
= python_stop_script
,
602 .process_event
= python_process_event
,
603 .generate_script
= python_generate_script
,