29 #include "../../perf.h"
33 #include "../thread.h"
34 #include "../trace-event.h"
39 #define FTRACE_MAX_EVENT \
40 ((1 << (sizeof(unsigned short) * 8)) - 1)
45 #define N_COMMON_FIELDS 7
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");
62 const char *field_name,
63 const char *field_value,
64 const char *field_str)
66 const char *handler_name =
"define_flag_value";
68 unsigned long long value;
72 handler_name =
"define_symbolic_value";
76 Py_FatalError(
"couldn't create Python tuple");
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);
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);
109 const char *field_name,
112 const char *handler_name =
"define_flag_field";
113 PyObject *handler, *
t, *
retval;
117 handler_name =
"define_symbolic_field";
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));
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);
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);
166 define_event_symbols(event, ev_name, args->
symbol.field);
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);
183 define_event_symbols(event, ev_name, args->
op.left);
184 define_event_symbols(event, ev_name, args->
op.right);
196 define_event_symbols(event, ev_name, args->
next);
201 static char ev_name[256];
219 define_event_symbols(event, ev_name, event->
print_fmt.args);
232 static char handler_name[256];
234 unsigned long long val;
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);
257 handler = PyDict_GetItemString(main_dict, handler_name);
258 if (handler && !PyCallable_Check(handler))
263 Py_FatalError(
"couldn't create Python dict");
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) {
292 offset = *(
int *)(data + field->
offset);
296 obj = PyString_FromString((
char *)data + offset);
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");
350 PyObject *handler, *
retval, *
t, *dict;
351 static char handler_name[64];
353 struct thread *thread = al->
thread;
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(
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);
409 switch (evsel->
attr.type) {
411 python_process_tracepoint(perf_event, sample, evsel,
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)
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];
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)
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");
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");
585 fprintf(ofp,
"\tcommon_secs, ");
586 fprintf(ofp,
"common_nsecs, ");
588 fprintf(ofp,
"common_comm,\n\t");
593 for (f = event->
format.fields; f; f = f->
next) {
596 if (++count % 5 == 0)
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");
612 for (f = event->
format.fields; f; f = f->
next) {
615 if (count && count % 3 == 0) {
631 fprintf(ofp,
"\\n\" %% \\\n\t\t(");
636 for (f = event->
format.fields; f; f = f->
next) {
640 if (++count % 5 == 0)
644 if ((count - 1) % 5 != 0) {
654 if ((count - 1) % 5 != 0) {
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);
690 .start_script = python_start_script,
691 .stop_script = python_stop_script,
692 .process_event = python_process_event,
693 .generate_script = python_generate_script,