2 * trace-event-python. Feed trace events to an embedded Python interpreter.
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 *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 *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 /* we should warn... */
186 define_event_symbols(event, ev_name, args->next);
189 static inline struct event *find_cache_event(int type)
191 static char ev_name[256];
197 events[type] = event = trace_find_event(type);
201 sprintf(ev_name, "%s__%s", event->system, event->name);
203 define_event_symbols(event, ev_name, event->print_fmt.args);
208 static void python_process_event(union perf_event *pevent __unused,
209 struct perf_sample *sample,
210 struct perf_evsel *evsel __unused,
211 struct machine *machine __unused,
212 struct thread *thread)
214 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
215 static char handler_name[256];
216 struct format_field *field;
217 unsigned long long val;
223 int cpu = sample->cpu;
224 void *data = sample->raw_data;
225 unsigned long long nsecs = sample->time;
226 char *comm = thread->comm;
228 t = PyTuple_New(MAX_FIELDS);
230 Py_FatalError("couldn't create Python tuple");
232 type = trace_parse_common_type(data);
234 event = find_cache_event(type);
236 die("ug! no event found for type %d", type);
238 pid = trace_parse_common_pid(data);
240 sprintf(handler_name, "%s__%s", event->system, event->name);
242 handler = PyDict_GetItemString(main_dict, handler_name);
243 if (handler && !PyCallable_Check(handler))
248 Py_FatalError("couldn't create Python dict");
250 s = nsecs / NSECS_PER_SEC;
251 ns = nsecs - s * NSECS_PER_SEC;
253 scripting_context->event_data = data;
255 context = PyCObject_FromVoidPtr(scripting_context, NULL);
257 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
258 PyTuple_SetItem(t, n++, context);
261 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
262 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
263 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
264 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
265 PyTuple_SetItem(t, n++, PyString_FromString(comm));
267 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
268 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
269 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
270 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
271 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
273 for (field = event->format.fields; field; field = field->next) {
274 if (field->flags & FIELD_IS_STRING) {
276 if (field->flags & FIELD_IS_DYNAMIC) {
277 offset = *(int *)(data + field->offset);
280 offset = field->offset;
281 obj = PyString_FromString((char *)data + offset);
282 } else { /* FIELD_IS_NUMERIC */
283 val = read_size(data + field->offset, field->size);
284 if (field->flags & FIELD_IS_SIGNED) {
285 if ((long long)val >= LONG_MIN &&
286 (long long)val <= LONG_MAX)
287 obj = PyInt_FromLong(val);
289 obj = PyLong_FromLongLong(val);
292 obj = PyInt_FromLong(val);
294 obj = PyLong_FromUnsignedLongLong(val);
298 PyTuple_SetItem(t, n++, obj);
300 PyDict_SetItemString(dict, field->name, obj);
304 PyTuple_SetItem(t, n++, dict);
306 if (_PyTuple_Resize(&t, n) == -1)
307 Py_FatalError("error resizing Python tuple");
310 retval = PyObject_CallObject(handler, t);
312 handler_call_die(handler_name);
314 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
315 if (handler && PyCallable_Check(handler)) {
317 retval = PyObject_CallObject(handler, t);
319 handler_call_die("trace_unhandled");
327 static int run_start_sub(void)
329 PyObject *handler, *retval;
332 main_module = PyImport_AddModule("__main__");
333 if (main_module == NULL)
335 Py_INCREF(main_module);
337 main_dict = PyModule_GetDict(main_module);
338 if (main_dict == NULL) {
342 Py_INCREF(main_dict);
344 handler = PyDict_GetItemString(main_dict, "trace_begin");
345 if (handler == NULL || !PyCallable_Check(handler))
348 retval = PyObject_CallObject(handler, NULL);
350 handler_call_die("trace_begin");
355 Py_XDECREF(main_dict);
356 Py_XDECREF(main_module);
364 static int python_start_script(const char *script, int argc, const char **argv)
366 const char **command_line;
371 command_line = malloc((argc + 1) * sizeof(const char *));
372 command_line[0] = script;
373 for (i = 1; i < argc + 1; i++)
374 command_line[i] = argv[i - 1];
378 initperf_trace_context();
380 PySys_SetArgv(argc + 1, (char **)command_line);
382 fp = fopen(script, "r");
384 sprintf(buf, "Can't open python script \"%s\"", script);
390 err = PyRun_SimpleFile(fp, script);
392 fprintf(stderr, "Error running python script %s\n", script);
396 err = run_start_sub();
398 fprintf(stderr, "Error starting python script %s\n", script);
415 static int python_stop_script(void)
417 PyObject *handler, *retval;
420 handler = PyDict_GetItemString(main_dict, "trace_end");
421 if (handler == NULL || !PyCallable_Check(handler))
424 retval = PyObject_CallObject(handler, NULL);
426 handler_call_die("trace_end");
430 Py_XDECREF(main_dict);
431 Py_XDECREF(main_module);
437 static int python_generate_script(const char *outfile)
439 struct event *event = NULL;
440 struct format_field *f;
441 char fname[PATH_MAX];
442 int not_first, count;
445 sprintf(fname, "%s.py", outfile);
446 ofp = fopen(fname, "w");
448 fprintf(stderr, "couldn't open %s\n", fname);
451 fprintf(ofp, "# perf script event handlers, "
452 "generated by perf script -g python\n");
454 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
455 " License version 2\n\n");
457 fprintf(ofp, "# The common_* event handler fields are the most useful "
458 "fields common to\n");
460 fprintf(ofp, "# all events. They don't necessarily correspond to "
461 "the 'common_*' fields\n");
463 fprintf(ofp, "# in the format files. Those fields not available as "
464 "handler params can\n");
466 fprintf(ofp, "# be retrieved using Python functions of the form "
467 "common_*(context).\n");
469 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
470 "of available functions.\n\n");
472 fprintf(ofp, "import os\n");
473 fprintf(ofp, "import sys\n\n");
475 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
476 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
477 fprintf(ofp, "\nfrom perf_trace_context import *\n");
478 fprintf(ofp, "from Core import *\n\n\n");
480 fprintf(ofp, "def trace_begin():\n");
481 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
483 fprintf(ofp, "def trace_end():\n");
484 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
486 while ((event = trace_find_next_event(event))) {
487 fprintf(ofp, "def %s__%s(", event->system, event->name);
488 fprintf(ofp, "event_name, ");
489 fprintf(ofp, "context, ");
490 fprintf(ofp, "common_cpu,\n");
491 fprintf(ofp, "\tcommon_secs, ");
492 fprintf(ofp, "common_nsecs, ");
493 fprintf(ofp, "common_pid, ");
494 fprintf(ofp, "common_comm,\n\t");
499 for (f = event->format.fields; f; f = f->next) {
502 if (++count % 5 == 0)
503 fprintf(ofp, "\n\t");
505 fprintf(ofp, "%s", f->name);
507 fprintf(ofp, "):\n");
509 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
510 "common_secs, common_nsecs,\n\t\t\t"
511 "common_pid, common_comm)\n\n");
513 fprintf(ofp, "\t\tprint \"");
518 for (f = event->format.fields; f; f = f->next) {
521 if (count && count % 3 == 0) {
522 fprintf(ofp, "\" \\\n\t\t\"");
526 fprintf(ofp, "%s=", f->name);
527 if (f->flags & FIELD_IS_STRING ||
528 f->flags & FIELD_IS_FLAG ||
529 f->flags & FIELD_IS_SYMBOLIC)
531 else if (f->flags & FIELD_IS_SIGNED)
537 fprintf(ofp, "\\n\" %% \\\n\t\t(");
542 for (f = event->format.fields; f; f = f->next) {
546 if (++count % 5 == 0)
547 fprintf(ofp, "\n\t\t");
549 if (f->flags & FIELD_IS_FLAG) {
550 if ((count - 1) % 5 != 0) {
551 fprintf(ofp, "\n\t\t");
554 fprintf(ofp, "flag_str(\"");
555 fprintf(ofp, "%s__%s\", ", event->system,
557 fprintf(ofp, "\"%s\", %s)", f->name,
559 } else if (f->flags & FIELD_IS_SYMBOLIC) {
560 if ((count - 1) % 5 != 0) {
561 fprintf(ofp, "\n\t\t");
564 fprintf(ofp, "symbol_str(\"");
565 fprintf(ofp, "%s__%s\", ", event->system,
567 fprintf(ofp, "\"%s\", %s)", f->name,
570 fprintf(ofp, "%s", f->name);
573 fprintf(ofp, "),\n\n");
576 fprintf(ofp, "def trace_unhandled(event_name, context, "
577 "event_fields_dict):\n");
579 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
580 "for k,v in sorted(event_fields_dict.items())])\n\n");
582 fprintf(ofp, "def print_header("
583 "event_name, cpu, secs, nsecs, pid, comm):\n"
584 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
585 "(event_name, cpu, secs, nsecs, pid, comm),\n");
589 fprintf(stderr, "generated Python script: %s\n", fname);
594 struct scripting_ops python_scripting_ops = {
596 .start_script = python_start_script,
597 .stop_script = python_stop_script,
598 .process_event = python_process_event,
599 .generate_script = python_generate_script,